1/*
2 * Copyright (C) 2006, 2007, 2009 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#include "config.h"
22#include "HTMLFrameOwnerElement.h"
23
24#include "DOMWindow.h"
25#include "Frame.h"
26#include "FrameLoader.h"
27#include "RenderWidget.h"
28#include "ShadowRoot.h"
29#include "SVGDocument.h"
30#include "StyleTreeResolver.h"
31#include <wtf/IsoMallocInlines.h>
32#include <wtf/Ref.h>
33
34namespace WebCore {
35
36WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLFrameOwnerElement);
37
38HTMLFrameOwnerElement::HTMLFrameOwnerElement(const QualifiedName& tagName, Document& document)
39 : HTMLElement(tagName, document)
40 , m_contentFrame(nullptr)
41 , m_sandboxFlags(SandboxNone)
42{
43}
44
45RenderWidget* HTMLFrameOwnerElement::renderWidget() const
46{
47 // HTMLObjectElement and HTMLEmbedElement may return arbitrary renderers
48 // when using fallback content.
49 if (!is<RenderWidget>(renderer()))
50 return nullptr;
51 return downcast<RenderWidget>(renderer());
52}
53
54void HTMLFrameOwnerElement::setContentFrame(Frame* frame)
55{
56 // Make sure we will not end up with two frames referencing the same owner element.
57 ASSERT(!m_contentFrame || m_contentFrame->ownerElement() != this);
58 ASSERT(frame);
59 // Disconnected frames should not be allowed to load.
60 ASSERT(isConnected());
61 m_contentFrame = frame;
62
63 for (RefPtr<ContainerNode> node = this; node; node = node->parentOrShadowHostNode())
64 node->incrementConnectedSubframeCount();
65}
66
67void HTMLFrameOwnerElement::clearContentFrame()
68{
69 if (!m_contentFrame)
70 return;
71
72 m_contentFrame = 0;
73
74 for (RefPtr<ContainerNode> node = this; node; node = node->parentOrShadowHostNode())
75 node->decrementConnectedSubframeCount();
76}
77
78void HTMLFrameOwnerElement::disconnectContentFrame()
79{
80 if (RefPtr<Frame> frame = contentFrame()) {
81 Ref<Frame> protect(*frame);
82 frame->loader().frameDetached();
83 frame->disconnectOwnerElement();
84 }
85}
86
87HTMLFrameOwnerElement::~HTMLFrameOwnerElement()
88{
89 if (m_contentFrame)
90 m_contentFrame->disconnectOwnerElement();
91}
92
93Document* HTMLFrameOwnerElement::contentDocument() const
94{
95 return m_contentFrame ? m_contentFrame->document() : nullptr;
96}
97
98WindowProxy* HTMLFrameOwnerElement::contentWindow() const
99{
100 return m_contentFrame ? &m_contentFrame->windowProxy() : nullptr;
101}
102
103void HTMLFrameOwnerElement::setSandboxFlags(SandboxFlags flags)
104{
105 m_sandboxFlags = flags;
106}
107
108bool HTMLFrameOwnerElement::isKeyboardFocusable(KeyboardEvent* event) const
109{
110 return m_contentFrame && HTMLElement::isKeyboardFocusable(event);
111}
112
113ExceptionOr<Document&> HTMLFrameOwnerElement::getSVGDocument() const
114{
115 auto* document = contentDocument();
116 if (is<SVGDocument>(document))
117 return *document;
118 // Spec: http://www.w3.org/TR/SVG/struct.html#InterfaceGetSVGDocument
119 return Exception { NotSupportedError };
120}
121
122void HTMLFrameOwnerElement::scheduleInvalidateStyleAndLayerComposition()
123{
124 if (Style::postResolutionCallbacksAreSuspended()) {
125 RefPtr<HTMLFrameOwnerElement> element = this;
126 Style::queuePostResolutionCallback([element] {
127 element->invalidateStyleAndLayerComposition();
128 });
129 } else
130 invalidateStyleAndLayerComposition();
131}
132
133bool SubframeLoadingDisabler::canLoadFrame(HTMLFrameOwnerElement& owner)
134{
135 for (RefPtr<ContainerNode> node = &owner; node; node = node->parentOrShadowHostNode()) {
136 if (disabledSubtreeRoots().contains(node.get()))
137 return false;
138 }
139 return true;
140}
141
142} // namespace WebCore
143