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