HTMLIFrameElement.cpp 3.8 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 <AK/Debug.h>
  27. #include <LibGUI/Button.h>
  28. #include <LibGUI/TextBox.h>
  29. #include <LibWeb/Bindings/WindowObject.h>
  30. #include <LibWeb/DOM/Document.h>
  31. #include <LibWeb/DOM/Event.h>
  32. #include <LibWeb/DOM/Window.h>
  33. #include <LibWeb/Dump.h>
  34. #include <LibWeb/HTML/EventNames.h>
  35. #include <LibWeb/HTML/HTMLFormElement.h>
  36. #include <LibWeb/HTML/HTMLIFrameElement.h>
  37. #include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
  38. #include <LibWeb/InProcessWebView.h>
  39. #include <LibWeb/Layout/FrameBox.h>
  40. #include <LibWeb/Loader/ResourceLoader.h>
  41. #include <LibWeb/Origin.h>
  42. #include <LibWeb/Page/Frame.h>
  43. namespace Web::HTML {
  44. HTMLIFrameElement::HTMLIFrameElement(DOM::Document& document, QualifiedName qualified_name)
  45. : HTMLElement(document, move(qualified_name))
  46. {
  47. ASSERT(document.frame());
  48. m_content_frame = Frame::create_subframe(*this, document.frame()->main_frame());
  49. }
  50. HTMLIFrameElement::~HTMLIFrameElement()
  51. {
  52. }
  53. RefPtr<Layout::Node> HTMLIFrameElement::create_layout_node()
  54. {
  55. auto style = document().style_resolver().resolve_style(*this);
  56. return adopt(*new Layout::FrameBox(document(), *this, move(style)));
  57. }
  58. void HTMLIFrameElement::parse_attribute(const FlyString& name, const String& value)
  59. {
  60. HTMLElement::parse_attribute(name, value);
  61. if (name == HTML::AttributeNames::src)
  62. load_src(value);
  63. }
  64. void HTMLIFrameElement::load_src(const String& value)
  65. {
  66. auto url = document().complete_url(value);
  67. if (!url.is_valid()) {
  68. dbgln("iframe failed to load URL: Invalid URL: {}", value);
  69. return;
  70. }
  71. if (url.protocol() == "file" && document().origin().protocol() != "file") {
  72. dbgln("iframe failed to load URL: Security violation: {} may not load {}", document().url(), url);
  73. return;
  74. }
  75. dbgln("Loading iframe document from {}", value);
  76. m_content_frame->loader().load(url, FrameLoader::Type::IFrame);
  77. }
  78. Origin HTMLIFrameElement::content_origin() const
  79. {
  80. if (!m_content_frame || !m_content_frame->document())
  81. return {};
  82. return m_content_frame->document()->origin();
  83. }
  84. bool HTMLIFrameElement::may_access_from_origin(const Origin& origin) const
  85. {
  86. return origin.is_same(content_origin());
  87. }
  88. const DOM::Document* HTMLIFrameElement::content_document() const
  89. {
  90. return m_content_frame ? m_content_frame->document() : nullptr;
  91. }
  92. void HTMLIFrameElement::content_frame_did_load(Badge<FrameLoader>)
  93. {
  94. dispatch_event(DOM::Event::create(EventNames::load));
  95. }
  96. }