#include "config.h"
#include "JSWindowProxy.h"
#include "AbstractFrame.h"
#include "CommonVM.h"
#include "GCController.h"
#include "JSDOMWindow.h"
#include "JSDOMWindowProperties.h"
#include "JSEventTarget.h"
#include "JSRemoteDOMWindow.h"
#include "ScriptController.h"
#include "WebCoreJSClientData.h"
#include <JavaScriptCore/Debugger.h>
#include <JavaScriptCore/JSObject.h>
#include <JavaScriptCore/StrongInlines.h>
namespace WebCore {
using namespace JSC;
const ClassInfo JSWindowProxy::s_info = { "JSWindowProxy", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSWindowProxy) };
inline JSWindowProxy::JSWindowProxy(VM& vm, Structure& structure, DOMWrapperWorld& world)
: Base(vm, &structure)
, m_world(world)
{
}
void JSWindowProxy::finishCreation(VM& vm, AbstractDOMWindow& window)
{
Base::finishCreation(vm);
ASSERT(inherits(vm, info()));
setWindow(window);
}
JSWindowProxy& JSWindowProxy::create(VM& vm, AbstractDOMWindow& window, DOMWrapperWorld& world)
{
auto& structure = *Structure::create(vm, 0, jsNull(), TypeInfo(PureForwardingProxyType, StructureFlags), info());
auto& proxy = *new (NotNull, allocateCell<JSWindowProxy>(vm.heap)) JSWindowProxy(vm, structure, world);
proxy.finishCreation(vm, window);
return proxy;
}
void JSWindowProxy::destroy(JSCell* cell)
{
static_cast<JSWindowProxy*>(cell)->JSWindowProxy::~JSWindowProxy();
}
void JSWindowProxy::setWindow(VM& vm, JSDOMGlobalObject& window)
{
ASSERT(window.classInfo(vm) == JSDOMWindow::info() || window.classInfo(vm) == JSRemoteDOMWindow::info());
setTarget(vm, &window);
structure(vm)->setGlobalObject(vm, &window);
GCController::singleton().garbageCollectSoon();
}
void JSWindowProxy::setWindow(AbstractDOMWindow& domWindow)
{
ASSERT(!window() || &domWindow != &wrapped());
bool isRemoteDOMWindow = is<RemoteDOMWindow>(domWindow);
VM& vm = commonVM();
auto& prototypeStructure = isRemoteDOMWindow ? *JSRemoteDOMWindowPrototype::createStructure(vm, nullptr, jsNull()) : *JSDOMWindowPrototype::createStructure(vm, nullptr, jsNull());
JSNonFinalObject* prototype = isRemoteDOMWindow ? static_cast<JSNonFinalObject*>(JSRemoteDOMWindowPrototype::create(vm, nullptr, &prototypeStructure)) : static_cast<JSNonFinalObject*>(JSDOMWindowPrototype::create(vm, nullptr, &prototypeStructure));
JSC::EnsureStillAliveScope protectedPrototype(prototype);
JSDOMGlobalObject* window = nullptr;
if (isRemoteDOMWindow) {
auto& windowStructure = *JSRemoteDOMWindow::createStructure(vm, nullptr, prototype);
window = JSRemoteDOMWindow::create(vm, &windowStructure, downcast<RemoteDOMWindow>(domWindow), this);
} else {
auto& windowStructure = *JSDOMWindow::createStructure(vm, nullptr, prototype);
window = JSDOMWindow::create(vm, &windowStructure, downcast<DOMWindow>(domWindow), this);
}
prototype->structure(vm)->setGlobalObject(vm, window);
auto& propertiesStructure = *JSDOMWindowProperties::createStructure(vm, window, JSEventTarget::prototype(vm, *window));
auto& properties = *JSDOMWindowProperties::create(&propertiesStructure, *window);
properties.didBecomePrototype();
prototype->structure(vm)->setPrototypeWithoutTransition(vm, &properties);
setWindow(vm, *window);
ASSERT(window->globalObject() == window);
ASSERT(prototype->globalObject() == window);
}
WindowProxy* JSWindowProxy::windowProxy() const
{
auto& window = wrapped();
return window.frame() ? &window.frame()->windowProxy() : nullptr;
}
void JSWindowProxy::attachDebugger(JSC::Debugger* debugger)
{
auto* globalObject = window();
JSLockHolder lock(globalObject->vm());
if (debugger)
debugger->attach(globalObject);
else if (auto* currentDebugger = globalObject->debugger())
currentDebugger->detach(globalObject, JSC::Debugger::TerminatingDebuggingSession);
}
AbstractDOMWindow& JSWindowProxy::wrapped() const
{
auto* window = this->window();
if (auto* jsWindow = jsDynamicCast<JSRemoteDOMWindowBase*>(window->vm(), window))
return jsWindow->wrapped();
return jsCast<JSDOMWindowBase*>(window)->wrapped();
}
JSValue toJS(JSGlobalObject* lexicalGlobalObject, WindowProxy& windowProxy)
{
auto* jsWindowProxy = windowProxy.jsWindowProxy(currentWorld(*lexicalGlobalObject));
return jsWindowProxy ? JSValue(jsWindowProxy) : jsNull();
}
JSWindowProxy* toJSWindowProxy(WindowProxy& windowProxy, DOMWrapperWorld& world)
{
return windowProxy.jsWindowProxy(world);
}
WindowProxy* JSWindowProxy::toWrapped(VM& vm, JSValue value)
{
if (!value.isObject())
return nullptr;
JSObject* object = asObject(value);
if (object->inherits<JSWindowProxy>(vm))
return jsCast<JSWindowProxy*>(object)->windowProxy();
return nullptr;
}
JSC::IsoSubspace* JSWindowProxy::subspaceForImpl(JSC::VM& vm)
{
return &static_cast<JSVMClientData*>(vm.clientData)->windowProxySpace();
}
}