HTMLIFrameElement.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2020-2021, 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/DOM/Document.h>
  27. #include <LibWeb/HTML/HTMLIFrameElement.h>
  28. #include <LibWeb/Layout/FrameBox.h>
  29. #include <LibWeb/Origin.h>
  30. #include <LibWeb/Page/Frame.h>
  31. namespace Web::HTML {
  32. HTMLIFrameElement::HTMLIFrameElement(DOM::Document& document, QualifiedName qualified_name)
  33. : FrameHostElement(document, move(qualified_name))
  34. {
  35. }
  36. HTMLIFrameElement::~HTMLIFrameElement()
  37. {
  38. }
  39. RefPtr<Layout::Node> HTMLIFrameElement::create_layout_node()
  40. {
  41. auto style = document().style_resolver().resolve_style(*this);
  42. return adopt(*new Layout::FrameBox(document(), *this, move(style)));
  43. }
  44. void HTMLIFrameElement::parse_attribute(const FlyString& name, const String& value)
  45. {
  46. HTMLElement::parse_attribute(name, value);
  47. if (name == HTML::AttributeNames::src)
  48. load_src(value);
  49. }
  50. void HTMLIFrameElement::inserted()
  51. {
  52. FrameHostElement::inserted();
  53. if (is_connected())
  54. load_src(attribute(HTML::AttributeNames::src));
  55. }
  56. void HTMLIFrameElement::load_src(const String& value)
  57. {
  58. if (!m_content_frame)
  59. return;
  60. auto url = document().complete_url(value);
  61. if (!url.is_valid()) {
  62. dbgln("iframe failed to load URL: Invalid URL: {}", value);
  63. return;
  64. }
  65. if (url.protocol() == "file" && document().origin().protocol() != "file") {
  66. dbgln("iframe failed to load URL: Security violation: {} may not load {}", document().url(), url);
  67. return;
  68. }
  69. dbgln("Loading iframe document from {}", value);
  70. m_content_frame->loader().load(url, FrameLoader::Type::IFrame);
  71. }
  72. }