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 299 300 301 302 303 304 305 306 307 308 309
|
/*
* Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
* 1999 Lars Knoll <knoll@kde.org>
* 1999 Antti Koivisto <koivisto@kde.org>
* 2000 Simon Hausmann <hausmann@kde.org>
* 2000 Stefan Schimanski <1Stein@gmx.de>
* 2001 George Staikos <staikos@kde.org>
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
* Copyright (C) 2005 Alexey Proskuryakov <ap@nypop.com>
* Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
* Copyright (C) 2008 Eric Seidel <eric@webkit.org>
* Copyright (C) 2008 Google Inc.
*
* 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 "config.h"
#include "core/frame/Frame.h"
#include "core/dom/DocumentType.h"
#include "core/events/Event.h"
#include "core/frame/LocalDOMWindow.h"
#include "core/frame/FrameHost.h"
#include "core/frame/Settings.h"
#include "core/html/HTMLFrameElementBase.h"
#include "core/inspector/InspectorInstrumentation.h"
#include "core/loader/EmptyClients.h"
#include "core/loader/FrameLoaderClient.h"
#include "core/page/Chrome.h"
#include "core/page/ChromeClient.h"
#include "core/page/EventHandler.h"
#include "core/page/FocusController.h"
#include "core/page/Page.h"
#include "core/rendering/RenderLayer.h"
#include "core/rendering/RenderPart.h"
#include "platform/graphics/GraphicsLayer.h"
#include "public/platform/WebLayer.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/RefCountedLeakCounter.h"
namespace blink {
using namespace HTMLNames;
DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, frameCounter, ("Frame"));
Frame::~Frame()
{
ASSERT(!m_owner);
#ifndef NDEBUG
frameCounter.decrement();
#endif
}
void Frame::trace(Visitor* visitor)
{
visitor->trace(m_treeNode);
visitor->trace(m_host);
visitor->trace(m_owner);
}
void Frame::detach()
{
ASSERT(m_client);
disconnectOwnerElement();
// After this, we must no longer talk to the client since this clears
// its owning reference back to our owning LocalFrame.
m_client->detached();
m_client = nullptr;
m_host = nullptr;
}
void Frame::detachChildren()
{
typedef WillBeHeapVector<RefPtrWillBeMember<Frame>> FrameVector;
FrameVector childrenToDetach;
childrenToDetach.reserveCapacity(tree().childCount());
for (Frame* child = tree().firstChild(); child; child = child->tree().nextSibling())
childrenToDetach.append(child);
for (const auto& child : childrenToDetach)
child->detach();
}
void Frame::disconnectOwnerElement()
{
if (m_owner) {
if (m_owner->isLocal())
toHTMLFrameOwnerElement(m_owner)->clearContentFrame();
}
m_owner = nullptr;
}
Page* Frame::page() const
{
if (m_host)
return &m_host->page();
return nullptr;
}
FrameHost* Frame::host() const
{
return m_host;
}
bool Frame::isMainFrame() const
{
Page* page = this->page();
return page && this == page->mainFrame();
}
bool Frame::isLocalRoot() const
{
if (isRemoteFrame())
return false;
if (!tree().parent())
return true;
return tree().parent()->isRemoteFrame();
}
HTMLFrameOwnerElement* Frame::deprecatedLocalOwner() const
{
return m_owner && m_owner->isLocal() ? toHTMLFrameOwnerElement(m_owner) : nullptr;
}
static ChromeClient& emptyChromeClient()
{
DEFINE_STATIC_LOCAL(EmptyChromeClient, client, ());
return client;
}
ChromeClient& Frame::chromeClient() const
{
if (Page* page = this->page())
return page->chrome().client();
return emptyChromeClient();
}
Frame* Frame::findFrameForNavigation(const AtomicString& name, Frame& activeFrame)
{
Frame* frame = tree().find(name);
if (!frame || !activeFrame.canNavigate(*frame))
return nullptr;
return frame;
}
static bool canAccessAncestor(const SecurityOrigin& activeSecurityOrigin, const Frame* targetFrame)
{
// targetFrame can be 0 when we're trying to navigate a top-level frame
// that has a 0 opener.
if (!targetFrame)
return false;
const bool isLocalActiveOrigin = activeSecurityOrigin.isLocal();
for (const Frame* ancestorFrame = targetFrame; ancestorFrame; ancestorFrame = ancestorFrame->tree().parent()) {
const SecurityOrigin* ancestorSecurityOrigin = ancestorFrame->securityContext()->securityOrigin();
if (activeSecurityOrigin.canAccess(ancestorSecurityOrigin))
return true;
// Allow file URL descendant navigation even when allowFileAccessFromFileURLs is false.
// FIXME: It's a bit strange to special-case local origins here. Should we be doing
// something more general instead?
if (isLocalActiveOrigin && ancestorSecurityOrigin->isLocal())
return true;
}
return false;
}
bool Frame::canNavigate(const Frame& targetFrame)
{
// Frame-busting is generally allowed, but blocked for sandboxed frames lacking the 'allow-top-navigation' flag.
if (!securityContext()->isSandboxed(SandboxTopNavigation) && targetFrame == tree().top())
return true;
if (securityContext()->isSandboxed(SandboxNavigation)) {
if (targetFrame.tree().isDescendantOf(this))
return true;
const char* reason = "The frame attempting navigation is sandboxed, and is therefore disallowed from navigating its ancestors.";
if (securityContext()->isSandboxed(SandboxTopNavigation) && targetFrame == tree().top())
reason = "The frame attempting navigation of the top-level window is sandboxed, but the 'allow-top-navigation' flag is not set.";
printNavigationErrorMessage(targetFrame, reason);
return false;
}
ASSERT(securityContext()->securityOrigin());
SecurityOrigin& origin = *securityContext()->securityOrigin();
// This is the normal case. A document can navigate its decendant frames,
// or, more generally, a document can navigate a frame if the document is
// in the same origin as any of that frame's ancestors (in the frame
// hierarchy).
//
// See http://www.adambarth.com/papers/2008/barth-jackson-mitchell.pdf for
// historical information about this security check.
if (canAccessAncestor(origin, &targetFrame))
return true;
// Top-level frames are easier to navigate than other frames because they
// display their URLs in the address bar (in most browsers). However, there
// are still some restrictions on navigation to avoid nuisance attacks.
// Specifically, a document can navigate a top-level frame if that frame
// opened the document or if the document is the same-origin with any of
// the top-level frame's opener's ancestors (in the frame hierarchy).
//
// In both of these cases, the document performing the navigation is in
// some way related to the frame being navigate (e.g., by the "opener"
// and/or "parent" relation). Requiring some sort of relation prevents a
// document from navigating arbitrary, unrelated top-level frames.
if (!targetFrame.tree().parent()) {
if (targetFrame == client()->opener())
return true;
if (canAccessAncestor(origin, targetFrame.client()->opener()))
return true;
}
printNavigationErrorMessage(targetFrame, "The frame attempting navigation is neither same-origin with the target, nor is it the target's parent or opener.");
return false;
}
Frame* Frame::findUnsafeParentScrollPropagationBoundary()
{
Frame* currentFrame = this;
Frame* ancestorFrame = tree().parent();
while (ancestorFrame) {
if (!ancestorFrame->securityContext()->securityOrigin()->canAccess(securityContext()->securityOrigin()))
return currentFrame;
currentFrame = ancestorFrame;
ancestorFrame = ancestorFrame->tree().parent();
}
return nullptr;
}
RenderPart* Frame::ownerRenderer() const
{
if (!deprecatedLocalOwner())
return nullptr;
RenderObject* object = deprecatedLocalOwner()->renderer();
if (!object)
return nullptr;
// FIXME: If <object> is ever fixed to disassociate itself from frames
// that it has started but canceled, then this can turn into an ASSERT
// since ownerElement() would be 0 when the load is canceled.
// https://bugs.webkit.org/show_bug.cgi?id=18585
if (!object->isRenderPart())
return nullptr;
return toRenderPart(object);
}
void Frame::setRemotePlatformLayer(WebLayer* layer)
{
if (m_remotePlatformLayer)
GraphicsLayer::unregisterContentsLayer(m_remotePlatformLayer);
m_remotePlatformLayer = layer;
if (m_remotePlatformLayer)
GraphicsLayer::registerContentsLayer(layer);
ASSERT(owner());
toHTMLFrameOwnerElement(owner())->setNeedsCompositingUpdate();
if (RenderPart* renderer = ownerRenderer())
renderer->layer()->updateSelfPaintingLayer();
}
Settings* Frame::settings() const
{
if (m_host)
return &m_host->settings();
return nullptr;
}
Frame::Frame(FrameClient* client, FrameHost* host, FrameOwner* owner)
: m_treeNode(this)
, m_host(host)
, m_owner(owner)
, m_client(client)
, m_remotePlatformLayer(nullptr)
{
ASSERT(page());
#ifndef NDEBUG
frameCounter.increment();
#endif
if (m_owner) {
if (m_owner->isLocal())
toHTMLFrameOwnerElement(m_owner)->setContentFrame(*this);
} else {
page()->setMainFrame(this);
}
}
} // namespace blink
|