HTMLIFrameElement.cpp 3.7 KB

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