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
|
/*
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
* Copyright (C) 2006-2019 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "WindowProxy.h"
#include "CommonVM.h"
#include "DOMWrapperWorld.h"
#include "GCController.h"
#include "JSDOMWindowBase.h"
#include "JSWindowProxy.h"
#include "LocalFrame.h"
#include "Page.h"
#include "PageConsoleClient.h"
#include "PageGroup.h"
#include "RemoteFrame.h"
#include "ScriptController.h"
#include "runtime_root.h"
#include <JavaScriptCore/JSLock.h>
#include <JavaScriptCore/StrongInlines.h>
#include <JavaScriptCore/WeakGCMapInlines.h>
#include <wtf/MemoryPressureHandler.h>
#include <wtf/TZoneMallocInlines.h>
namespace WebCore {
using namespace JSC;
static void collectGarbageAfterWindowProxyDestruction()
{
// Make sure to GC Extra Soon(tm) during memory pressure conditions
// to soften high peaks of memory usage during navigation.
if (MemoryPressureHandler::singleton().isUnderMemoryPressure()) {
// NOTE: We do the collection on next runloop to ensure that there's no pointer
// to the window object on the stack.
GCController::singleton().garbageCollectOnNextRunLoop();
} else
GCController::singleton().garbageCollectSoon();
}
WTF_MAKE_TZONE_ALLOCATED_IMPL(WindowProxy);
WindowProxy::WindowProxy(Frame& frame)
: m_frame(frame)
, m_jsWindowProxies(makeUniqueRef<ProxyMap>())
{
}
WindowProxy::~WindowProxy()
{
ASSERT(!m_frame);
ASSERT(m_jsWindowProxies->isEmpty());
}
Frame* WindowProxy::frame() const
{
return m_frame.get();
}
void WindowProxy::detachFromFrame()
{
ASSERT(m_frame);
m_frame = nullptr;
// It's likely that destroying windowProxies will create a lot of garbage.
if (!m_jsWindowProxies->isEmpty()) {
while (!m_jsWindowProxies->isEmpty()) {
auto it = m_jsWindowProxies->begin();
it->value->window()->setConsoleClient(nullptr);
destroyJSWindowProxy(*it->key);
}
collectGarbageAfterWindowProxyDestruction();
}
}
void WindowProxy::replaceFrame(Frame& frame)
{
ASSERT(m_frame);
ASSERT(is<LocalFrame>(m_frame) != is<LocalFrame>(frame));
m_frame = frame;
setDOMWindow(frame.window());
}
void WindowProxy::destroyJSWindowProxy(DOMWrapperWorld& world)
{
ASSERT(m_jsWindowProxies->contains(&world));
m_jsWindowProxies->remove(&world);
world.didDestroyWindowProxy(this);
}
JSWindowProxy& WindowProxy::createJSWindowProxy(DOMWrapperWorld& world)
{
ASSERT(m_frame);
ASSERT(!m_jsWindowProxies->contains(&world));
ASSERT(m_frame->window());
VM& vm = world.vm();
Strong<JSWindowProxy> jsWindowProxy(vm, &JSWindowProxy::create(vm, *m_frame->window(), world));
m_jsWindowProxies->add(&world, jsWindowProxy);
world.didCreateWindowProxy(this);
return *jsWindowProxy.get();
}
Vector<JSC::Strong<JSWindowProxy>> WindowProxy::jsWindowProxiesAsVector() const
{
return copyToVector(m_jsWindowProxies->values());
}
JSDOMGlobalObject* WindowProxy::globalObject(DOMWrapperWorld& world)
{
if (auto* windowProxy = jsWindowProxy(world))
return windowProxy->window();
return nullptr;
}
JSWindowProxy& WindowProxy::createJSWindowProxyWithInitializedScript(DOMWrapperWorld& world)
{
ASSERT(m_frame);
JSLockHolder lock(world.vm());
auto& windowProxy = createJSWindowProxy(world);
if (auto* localFrame = dynamicDowncast<LocalFrame>(*m_frame))
localFrame->script().initScriptForWindowProxy(windowProxy);
return windowProxy;
}
void WindowProxy::clearJSWindowProxiesNotMatchingDOMWindow(DOMWindow* newDOMWindow, bool goingIntoBackForwardCache)
{
if (m_jsWindowProxies->isEmpty())
return;
JSLockHolder lock(commonVM());
for (auto& windowProxy : jsWindowProxiesAsVector()) {
if (&windowProxy->wrapped() == newDOMWindow)
continue;
// Clear the debugger and console from the current window before setting the new window.
windowProxy->attachDebugger(nullptr);
windowProxy->window()->setConsoleClient(nullptr);
if (auto* jsDOMWindow = jsDynamicCast<JSDOMWindowBase*>(windowProxy->window()))
jsDOMWindow->willRemoveFromWindowProxy();
}
// It's likely that resetting our windows created a lot of garbage, unless
// it went in a back/forward cache.
if (!goingIntoBackForwardCache)
collectGarbageAfterWindowProxyDestruction();
}
void WindowProxy::setDOMWindow(DOMWindow* newDOMWindow)
{
ASSERT(newDOMWindow);
if (m_jsWindowProxies->isEmpty())
return;
ASSERT(m_frame);
JSLockHolder lock(commonVM());
for (auto& windowProxy : jsWindowProxiesAsVector()) {
if (&windowProxy->wrapped() == newDOMWindow)
continue;
windowProxy->setWindow(*newDOMWindow);
ScriptController* scriptController = nullptr;
Page* page = m_frame->page();
if (auto* localFrame = dynamicDowncast<LocalFrame>(*m_frame))
scriptController = &localFrame->script();
// ScriptController's m_cacheableBindingRootObject persists between page navigations
// so needs to know about the new JSDOMWindow.
if (auto* cacheableBindingRootObject = scriptController ? scriptController->existingCacheableBindingRootObject() : nullptr)
cacheableBindingRootObject->updateGlobalObject(windowProxy->window());
windowProxy->attachDebugger(page ? page->debugger() : nullptr);
if (page) {
windowProxy->window()->setProfileGroup(page->group().identifier());
windowProxy->window()->setConsoleClient(page->console());
}
}
}
void WindowProxy::attachDebugger(JSC::Debugger* debugger)
{
for (auto& windowProxy : m_jsWindowProxies->values())
windowProxy->attachDebugger(debugger);
}
DOMWindow* WindowProxy::window() const
{
return m_frame ? m_frame->window() : nullptr;
}
WindowProxy::ProxyMap WindowProxy::releaseJSWindowProxies()
{
return std::exchange(m_jsWindowProxies, makeUniqueRef<ProxyMap>());
}
void WindowProxy::setJSWindowProxies(ProxyMap&& windowProxies)
{
m_jsWindowProxies = makeUniqueRef<ProxyMap>(WTFMove(windowProxies));
}
} // namespace WebCore
|