HTMLIFrameElement.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Button.h>
  27. #include <LibGUI/TextBox.h>
  28. #include <LibWeb/Bindings/WindowObject.h>
  29. #include <LibWeb/DOM/Document.h>
  30. #include <LibWeb/DOM/Event.h>
  31. #include <LibWeb/DOM/Window.h>
  32. #include <LibWeb/Dump.h>
  33. #include <LibWeb/HTML/HTMLFormElement.h>
  34. #include <LibWeb/HTML/HTMLIFrameElement.h>
  35. #include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
  36. #include <LibWeb/InProcessWebView.h>
  37. #include <LibWeb/Layout/LayoutFrame.h>
  38. #include <LibWeb/Layout/LayoutWidget.h>
  39. #include <LibWeb/Loader/ResourceLoader.h>
  40. #include <LibWeb/Origin.h>
  41. #include <LibWeb/Page/Frame.h>
  42. namespace Web::HTML {
  43. HTMLIFrameElement::HTMLIFrameElement(DOM::Document& document, const FlyString& tag_name)
  44. : HTMLElement(document, tag_name)
  45. {
  46. }
  47. HTMLIFrameElement::~HTMLIFrameElement()
  48. {
  49. }
  50. RefPtr<LayoutNode> HTMLIFrameElement::create_layout_node(const CSS::StyleProperties* parent_style)
  51. {
  52. auto style = document().style_resolver().resolve_style(*this, parent_style);
  53. return adopt(*new LayoutFrame(document(), *this, move(style)));
  54. }
  55. void HTMLIFrameElement::document_did_attach_to_frame(Frame& frame)
  56. {
  57. ASSERT(!m_content_frame);
  58. m_content_frame = Frame::create_subframe(*this, frame.main_frame());
  59. auto src = attribute(HTML::AttributeNames::src);
  60. if (src.is_null())
  61. return;
  62. load_src(src);
  63. }
  64. void HTMLIFrameElement::document_will_detach_from_frame(Frame&)
  65. {
  66. }
  67. void HTMLIFrameElement::load_src(const String& value)
  68. {
  69. dbg() << "Loading iframe document from " << value;
  70. auto url = document().complete_url(value);
  71. if (!url.is_valid()) {
  72. dbg() << "Actually no I'm not, because the URL is not valid :(";
  73. return;
  74. }
  75. m_content_frame->loader().load(url, FrameLoader::Type::IFrame);
  76. }
  77. Origin HTMLIFrameElement::content_origin() const
  78. {
  79. if (!m_content_frame || !m_content_frame->document())
  80. return {};
  81. return m_content_frame->document()->origin();
  82. }
  83. bool HTMLIFrameElement::may_access_from_origin(const Origin& origin) const
  84. {
  85. return origin.is_same(content_origin());
  86. }
  87. const DOM::Document* HTMLIFrameElement::content_document() const
  88. {
  89. return m_content_frame ? m_content_frame->document() : nullptr;
  90. }
  91. void HTMLIFrameElement::content_frame_did_load(Badge<FrameLoader>)
  92. {
  93. dispatch_event(DOM::Event::create("load"));
  94. }
  95. }