File: HTMLFrameElementBase.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (298 lines) | stat: -rw-r--r-- 10,196 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
 *           (C) 2000 Simon Hausmann (hausmann@kde.org)
 *           (C) 2001 Dirk Mueller (mueller@kde.org)
 * Copyright (C) 2004, 2006, 2008, 2009 Apple Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "core/html/HTMLFrameElementBase.h"

#include "bindings/core/v8/BindingSecurity.h"
#include "bindings/core/v8/ScriptController.h"
#include "bindings/core/v8/ScriptEventListener.h"
#include "core/HTMLNames.h"
#include "core/dom/Attribute.h"
#include "core/dom/Document.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/RemoteFrame.h"
#include "core/frame/RemoteFrameView.h"
#include "core/frame/csp/ContentSecurityPolicy.h"
#include "core/html/parser/HTMLParserIdioms.h"
#include "core/loader/FrameLoader.h"
#include "core/loader/FrameLoaderClient.h"
#include "core/page/FocusController.h"
#include "core/page/Page.h"

namespace blink {

using namespace HTMLNames;

HTMLFrameElementBase::HTMLFrameElementBase(const QualifiedName& tagName,
                                           Document& document)
    : HTMLFrameOwnerElement(tagName, document),
      m_scrollingMode(ScrollbarAuto),
      m_marginWidth(-1),
      m_marginHeight(-1) {}

bool HTMLFrameElementBase::isURLAllowed() const {
  if (m_URL.isEmpty())
    return true;

  const KURL& completeURL = document().completeURL(m_URL);

  if (contentFrame() && protocolIsJavaScript(completeURL)) {
    // Check if the caller can execute script in the context of the content
    // frame. NB: This check can be invoked without any JS on the stack for some
    // parser operations. In such case, we use the origin of the frame element's
    // containing document as the caller context.
    v8::Isolate* isolate = toIsolate(&document());
    LocalDOMWindow* accessingWindow = isolate->InContext()
                                          ? currentDOMWindow(isolate)
                                          : document().domWindow();
    if (!BindingSecurity::shouldAllowAccessToFrame(
            accessingWindow, contentFrame(),
            BindingSecurity::ErrorReportOption::Report))
      return false;
  }

  LocalFrame* parentFrame = document().frame();
  if (parentFrame)
    return parentFrame->isURLAllowed(completeURL);

  return true;
}

void HTMLFrameElementBase::openURL(bool replaceCurrentItem) {
  if (!isURLAllowed())
    return;

  if (m_URL.isEmpty())
    m_URL = AtomicString(blankURL().getString());

  LocalFrame* parentFrame = document().frame();
  if (!parentFrame)
    return;

  // Support for <frame src="javascript:string">
  KURL scriptURL;
  KURL url = document().completeURL(m_URL);
  if (protocolIsJavaScript(m_URL)) {
    // We'll set/execute |scriptURL| iff CSP allows us to execute inline
    // JavaScript. If CSP blocks inline JavaScript, then exit early if
    // we're trying to execute script in an existing document. If we're
    // executing JavaScript to create a new document (e.g.
    // '<iframe src="javascript:...">' then continue loading 'about:blank'
    // so that the frame is populated with something reasonable.
    if (ContentSecurityPolicy::shouldBypassMainWorld(&document()) ||
        document().contentSecurityPolicy()->allowJavaScriptURLs(
            this, document().url(), OrdinalNumber::first())) {
      scriptURL = url;
    } else {
      if (contentFrame())
        return;
    }

    url = blankURL();
  }

  if (!loadOrRedirectSubframe(url, m_frameName, replaceCurrentItem))
    return;
  if (!contentFrame() || scriptURL.isEmpty() || !contentFrame()->isLocalFrame())
    return;
  if (contentFrame()->owner()->getSandboxFlags() & SandboxOrigin)
    return;
  toLocalFrame(contentFrame())
      ->script()
      .executeScriptIfJavaScriptURL(scriptURL, this);
}

void HTMLFrameElementBase::frameOwnerPropertiesChanged() {
  // Don't notify about updates if contentFrame() is null, for example when
  // the subframe hasn't been created yet.
  if (contentFrame())
    document().frame()->loader().client()->didChangeFrameOwnerProperties(this);
}

void HTMLFrameElementBase::parseAttribute(
    const AttributeModificationParams& params) {
  const QualifiedName& name = params.name;
  const AtomicString& value = params.newValue;
  if (name == srcdocAttr) {
    if (!value.isNull()) {
      setLocation(srcdocURL().getString());
    } else {
      const AtomicString& srcValue = fastGetAttribute(srcAttr);
      if (!srcValue.isNull())
        setLocation(stripLeadingAndTrailingHTMLSpaces(srcValue));
    }
  } else if (name == srcAttr && !fastHasAttribute(srcdocAttr)) {
    setLocation(stripLeadingAndTrailingHTMLSpaces(value));
  } else if (name == idAttr) {
    // Important to call through to base for the id attribute so the hasID bit
    // gets set.
    HTMLFrameOwnerElement::parseAttribute(params);
    m_frameName = value;
  } else if (name == nameAttr) {
    m_frameName = value;
  } else if (name == marginwidthAttr) {
    setMarginWidth(value.toInt());
  } else if (name == marginheightAttr) {
    setMarginHeight(value.toInt());
  } else if (name == scrollingAttr) {
    // Auto and yes both simply mean "allow scrolling." No means "don't allow
    // scrolling."
    if (equalIgnoringCase(value, "auto") || equalIgnoringCase(value, "yes"))
      setScrollingMode(ScrollbarAuto);
    else if (equalIgnoringCase(value, "no"))
      setScrollingMode(ScrollbarAlwaysOff);
  } else if (name == onbeforeunloadAttr) {
    // FIXME: should <frame> elements have beforeunload handlers?
    setAttributeEventListener(
        EventTypeNames::beforeunload,
        createAttributeEventListener(this, name, value, eventParameterName()));
  } else {
    HTMLFrameOwnerElement::parseAttribute(params);
  }
}

void HTMLFrameElementBase::setNameAndOpenURL() {
  m_frameName = getNameAttribute();
  openURL();
}

Node::InsertionNotificationRequest HTMLFrameElementBase::insertedInto(
    ContainerNode* insertionPoint) {
  HTMLFrameOwnerElement::insertedInto(insertionPoint);
  return InsertionShouldCallDidNotifySubtreeInsertions;
}

void HTMLFrameElementBase::didNotifySubtreeInsertionsToDocument() {
  if (!document().frame())
    return;

  if (!SubframeLoadingDisabler::canLoadFrame(*this))
    return;

  // We should never have a content frame at the point where we got inserted
  // into a tree.
  SECURITY_CHECK(!contentFrame());

  setNameAndOpenURL();
}

void HTMLFrameElementBase::attachLayoutTree(const AttachContext& context) {
  HTMLFrameOwnerElement::attachLayoutTree(context);

  if (layoutPart()) {
    if (Frame* frame = contentFrame()) {
      if (frame->isLocalFrame())
        setWidget(toLocalFrame(frame)->view());
      else if (frame->isRemoteFrame())
        setWidget(toRemoteFrame(frame)->view());
    }
  }
}

void HTMLFrameElementBase::setLocation(const String& str) {
  m_URL = AtomicString(str);

  if (isConnected())
    openURL(false);
}

bool HTMLFrameElementBase::supportsFocus() const {
  return true;
}

void HTMLFrameElementBase::setFocused(bool received) {
  HTMLFrameOwnerElement::setFocused(received);
  if (Page* page = document().page()) {
    if (received) {
      page->focusController().setFocusedFrame(contentFrame());
    } else if (page->focusController().focusedFrame() == contentFrame()) {
      // Focus may have already been given to another frame, don't take it away.
      page->focusController().setFocusedFrame(nullptr);
    }
  }
}

bool HTMLFrameElementBase::isURLAttribute(const Attribute& attribute) const {
  return attribute.name() == longdescAttr || attribute.name() == srcAttr ||
         HTMLFrameOwnerElement::isURLAttribute(attribute);
}

bool HTMLFrameElementBase::hasLegalLinkAttribute(
    const QualifiedName& name) const {
  return name == srcAttr || HTMLFrameOwnerElement::hasLegalLinkAttribute(name);
}

bool HTMLFrameElementBase::isHTMLContentAttribute(
    const Attribute& attribute) const {
  return attribute.name() == srcdocAttr ||
         HTMLFrameOwnerElement::isHTMLContentAttribute(attribute);
}

// FIXME: Remove this code once we have input routing in the browser
// process. See http://crbug.com/339659.
void HTMLFrameElementBase::defaultEventHandler(Event* event) {
  if (contentFrame() && contentFrame()->isRemoteFrame()) {
    toRemoteFrame(contentFrame())->forwardInputEvent(event);
    return;
  }
  HTMLFrameOwnerElement::defaultEventHandler(event);
}

void HTMLFrameElementBase::setScrollingMode(ScrollbarMode scrollbarMode) {
  if (m_scrollingMode == scrollbarMode)
    return;

  if (contentDocument()) {
    contentDocument()->willChangeFrameOwnerProperties(
        m_marginWidth, m_marginHeight, scrollbarMode);
  }
  m_scrollingMode = scrollbarMode;
  frameOwnerPropertiesChanged();
}

void HTMLFrameElementBase::setMarginWidth(int marginWidth) {
  if (m_marginWidth == marginWidth)
    return;

  if (contentDocument()) {
    contentDocument()->willChangeFrameOwnerProperties(
        marginWidth, m_marginHeight, m_scrollingMode);
  }
  m_marginWidth = marginWidth;
  frameOwnerPropertiesChanged();
}

void HTMLFrameElementBase::setMarginHeight(int marginHeight) {
  if (m_marginHeight == marginHeight)
    return;

  if (contentDocument()) {
    contentDocument()->willChangeFrameOwnerProperties(
        m_marginWidth, marginHeight, m_scrollingMode);
  }
  m_marginHeight = marginHeight;
  frameOwnerPropertiesChanged();
}

}  // namespace blink