1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2000 Simon Hausmann (hausmann@kde.org)
5 *           (C) 2001 Dirk Mueller (mueller@kde.org)
6 * Copyright (C) 2004, 2006, 2008, 2009 Apple Inc. All rights reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB.  If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24#include "config.h"
25#include "core/html/HTMLFrameElementBase.h"
26
27#include "bindings/core/v8/ScriptController.h"
28#include "bindings/core/v8/ScriptEventListener.h"
29#include "core/HTMLNames.h"
30#include "core/dom/Attribute.h"
31#include "core/dom/Document.h"
32#include "core/frame/FrameView.h"
33#include "core/frame/LocalFrame.h"
34#include "core/html/parser/HTMLParserIdioms.h"
35#include "core/loader/FrameLoader.h"
36#include "core/page/ChromeClient.h"
37#include "core/page/FocusController.h"
38#include "core/page/Page.h"
39#include "core/rendering/RenderPart.h"
40
41namespace blink {
42
43using namespace HTMLNames;
44
45HTMLFrameElementBase::HTMLFrameElementBase(const QualifiedName& tagName, Document& document)
46    : HTMLFrameOwnerElement(tagName, document)
47    , m_scrolling(ScrollbarAuto)
48    , m_marginWidth(-1)
49    , m_marginHeight(-1)
50{
51}
52
53bool HTMLFrameElementBase::isURLAllowed() const
54{
55    if (m_URL.isEmpty())
56        return true;
57
58    const KURL& completeURL = document().completeURL(m_URL);
59
60    if (protocolIsJavaScript(completeURL)) {
61        Document* contentDoc = this->contentDocument();
62        if (contentDoc && !ScriptController::canAccessFromCurrentOrigin(contentDoc->frame()))
63            return false;
64    }
65
66    LocalFrame* parentFrame = document().frame();
67    if (parentFrame)
68        return parentFrame->isURLAllowed(completeURL);
69
70    return true;
71}
72
73void HTMLFrameElementBase::openURL(bool lockBackForwardList)
74{
75    if (!isURLAllowed())
76        return;
77
78    if (m_URL.isEmpty())
79        m_URL = AtomicString(blankURL().string());
80
81    LocalFrame* parentFrame = document().frame();
82    if (!parentFrame)
83        return;
84
85    // Support for <frame src="javascript:string">
86    KURL scriptURL;
87    KURL url = document().completeURL(m_URL);
88    if (protocolIsJavaScript(m_URL)) {
89        scriptURL = url;
90        url = blankURL();
91    }
92
93    if (!loadOrRedirectSubframe(url, m_frameName, lockBackForwardList))
94        return;
95    if (!contentFrame() || scriptURL.isEmpty() || !contentFrame()->isLocalFrame())
96        return;
97    toLocalFrame(contentFrame())->script().executeScriptIfJavaScriptURL(scriptURL);
98}
99
100void HTMLFrameElementBase::parseAttribute(const QualifiedName& name, const AtomicString& value)
101{
102    if (name == srcdocAttr)
103        setLocation("about:srcdoc");
104    else if (name == srcAttr && !fastHasAttribute(srcdocAttr))
105        setLocation(stripLeadingAndTrailingHTMLSpaces(value));
106    else if (isIdAttributeName(name)) {
107        // Important to call through to base for the id attribute so the hasID bit gets set.
108        HTMLFrameOwnerElement::parseAttribute(name, value);
109        m_frameName = value;
110    } else if (name == nameAttr) {
111        m_frameName = value;
112        // FIXME: If we are already attached, this doesn't actually change the frame's name.
113        // FIXME: If we are already attached, this doesn't check for frame name
114        // conflicts and generate a unique frame name.
115    } else if (name == marginwidthAttr) {
116        m_marginWidth = value.toInt();
117        // FIXME: If we are already attached, this has no effect.
118    } else if (name == marginheightAttr) {
119        m_marginHeight = value.toInt();
120        // FIXME: If we are already attached, this has no effect.
121    } else if (name == scrollingAttr) {
122        // Auto and yes both simply mean "allow scrolling." No means "don't allow scrolling."
123        if (equalIgnoringCase(value, "auto") || equalIgnoringCase(value, "yes"))
124            m_scrolling = ScrollbarAuto;
125        else if (equalIgnoringCase(value, "no"))
126            m_scrolling = ScrollbarAlwaysOff;
127        // FIXME: If we are already attached, this has no effect.
128    } else if (name == onbeforeunloadAttr) {
129        // FIXME: should <frame> elements have beforeunload handlers?
130        setAttributeEventListener(EventTypeNames::beforeunload, createAttributeEventListener(this, name, value, eventParameterName()));
131    } else
132        HTMLFrameOwnerElement::parseAttribute(name, value);
133}
134
135void HTMLFrameElementBase::setNameAndOpenURL()
136{
137    m_frameName = getNameAttribute();
138    openURL();
139}
140
141Node::InsertionNotificationRequest HTMLFrameElementBase::insertedInto(ContainerNode* insertionPoint)
142{
143    HTMLFrameOwnerElement::insertedInto(insertionPoint);
144    return InsertionShouldCallDidNotifySubtreeInsertions;
145}
146
147void HTMLFrameElementBase::didNotifySubtreeInsertionsToDocument()
148{
149    if (!document().frame())
150        return;
151
152    if (!SubframeLoadingDisabler::canLoadFrame(*this))
153        return;
154
155    setNameAndOpenURL();
156}
157
158void HTMLFrameElementBase::attach(const AttachContext& context)
159{
160    HTMLFrameOwnerElement::attach(context);
161
162    if (renderPart()) {
163        if (Frame* frame = contentFrame()) {
164            if (frame->isLocalFrame())
165                setWidget(toLocalFrame(frame)->view());
166        }
167    }
168}
169
170void HTMLFrameElementBase::setLocation(const String& str)
171{
172    m_URL = AtomicString(str);
173
174    if (inDocument())
175        openURL(false);
176}
177
178bool HTMLFrameElementBase::supportsFocus() const
179{
180    return true;
181}
182
183void HTMLFrameElementBase::setFocus(bool received)
184{
185    HTMLFrameOwnerElement::setFocus(received);
186    if (Page* page = document().page()) {
187        if (received)
188            page->focusController().setFocusedFrame(contentFrame());
189        else if (page->focusController().focusedFrame() == contentFrame()) // Focus may have already been given to another frame, don't take it away.
190            page->focusController().setFocusedFrame(nullptr);
191    }
192}
193
194bool HTMLFrameElementBase::isURLAttribute(const Attribute& attribute) const
195{
196    return attribute.name() == longdescAttr || attribute.name() == srcAttr
197        || HTMLFrameOwnerElement::isURLAttribute(attribute);
198}
199
200bool HTMLFrameElementBase::hasLegalLinkAttribute(const QualifiedName& name) const
201{
202    return name == srcAttr || HTMLFrameOwnerElement::hasLegalLinkAttribute(name);
203}
204
205bool HTMLFrameElementBase::isHTMLContentAttribute(const Attribute& attribute) const
206{
207    return attribute.name() == srcdocAttr || HTMLFrameOwnerElement::isHTMLContentAttribute(attribute);
208}
209
210// FIXME: Remove this code once we have input routing in the browser
211// process. See http://crbug.com/339659.
212void HTMLFrameElementBase::defaultEventHandler(Event* event)
213{
214    if (contentFrame() && contentFrame()->isRemoteFrameTemporary()) {
215        contentFrame()->chromeClient().forwardInputEvent(contentFrame(), event);
216        return;
217    }
218    HTMLFrameOwnerElement::defaultEventHandler(event);
219}
220
221} // namespace blink
222