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 "ExceptionCode.h"
26#include "Frame.h"
27#include "FrameLoader.h"
28#include "RenderWidget.h"
29#include "ShadowRoot.h"
30#include "SVGDocument.h"
31#include <wtf/Ref.h>
32
33namespace WebCore {
34
35HTMLFrameOwnerElement::HTMLFrameOwnerElement(const QualifiedName& tagName, Document& document)
36    : HTMLElement(tagName, document)
37    , m_contentFrame(0)
38    , m_sandboxFlags(SandboxNone)
39{
40}
41
42RenderWidget* HTMLFrameOwnerElement::renderWidget() const
43{
44    // HTMLObjectElement and HTMLEmbedElement may return arbitrary renderers
45    // when using fallback content.
46    if (!renderer() || !renderer()->isWidget())
47        return 0;
48    return toRenderWidget(renderer());
49}
50
51void HTMLFrameOwnerElement::setContentFrame(Frame* frame)
52{
53    // Make sure we will not end up with two frames referencing the same owner element.
54    ASSERT(!m_contentFrame || m_contentFrame->ownerElement() != this);
55    ASSERT(frame);
56    // Disconnected frames should not be allowed to load.
57    ASSERT(inDocument());
58    m_contentFrame = frame;
59
60    for (ContainerNode* node = this; node; node = node->parentOrShadowHostNode())
61        node->incrementConnectedSubframeCount();
62}
63
64void HTMLFrameOwnerElement::clearContentFrame()
65{
66    if (!m_contentFrame)
67        return;
68
69    m_contentFrame = 0;
70
71    for (ContainerNode* node = this; node; node = node->parentOrShadowHostNode())
72        node->decrementConnectedSubframeCount();
73}
74
75void HTMLFrameOwnerElement::disconnectContentFrame()
76{
77    // FIXME: Currently we don't do this in removedFrom because this causes an
78    // unload event in the subframe which could execute script that could then
79    // reach up into this document and then attempt to look back down. We should
80    // see if this behavior is really needed as Gecko does not allow this.
81    if (Frame* frame = contentFrame()) {
82        Ref<Frame> protect(*frame);
83        frame->loader().frameDetached();
84        frame->disconnectOwnerElement();
85    }
86}
87
88HTMLFrameOwnerElement::~HTMLFrameOwnerElement()
89{
90    if (m_contentFrame)
91        m_contentFrame->disconnectOwnerElement();
92}
93
94Document* HTMLFrameOwnerElement::contentDocument() const
95{
96    return m_contentFrame ? m_contentFrame->document() : 0;
97}
98
99DOMWindow* HTMLFrameOwnerElement::contentWindow() const
100{
101    return m_contentFrame ? m_contentFrame->document()->domWindow() : 0;
102}
103
104void HTMLFrameOwnerElement::setSandboxFlags(SandboxFlags flags)
105{
106    m_sandboxFlags = flags;
107}
108
109bool HTMLFrameOwnerElement::isKeyboardFocusable(KeyboardEvent* event) const
110{
111    return m_contentFrame && HTMLElement::isKeyboardFocusable(event);
112}
113
114SVGDocument* HTMLFrameOwnerElement::getSVGDocument(ExceptionCode& ec) const
115{
116    Document* doc = contentDocument();
117    if (doc && doc->isSVGDocument())
118        return toSVGDocument(doc);
119    // Spec: http://www.w3.org/TR/SVG/struct.html#InterfaceGetSVGDocument
120    ec = NOT_SUPPORTED_ERR;
121    return 0;
122}
123
124void HTMLFrameOwnerElement::scheduleSetNeedsStyleRecalc(StyleChangeType changeType)
125{
126    if (Style::postResolutionCallbacksAreSuspended()) {
127        RefPtr<HTMLFrameOwnerElement> element = this;
128        Style::queuePostResolutionCallback([element, changeType]{
129            element->setNeedsStyleRecalc(changeType);
130        });
131    } else
132        setNeedsStyleRecalc(changeType);
133}
134
135bool SubframeLoadingDisabler::canLoadFrame(HTMLFrameOwnerElement& owner)
136{
137    for (ContainerNode* node = &owner; node; node = node->parentOrShadowHostNode()) {
138        if (disabledSubtreeRoots().contains(node))
139            return false;
140    }
141    return true;
142}
143
144} // namespace WebCore
145