V8DOMWindowShell.cpp [plain text]
#include "config.h"
#include "V8DOMWindowShell.h"
#include "PlatformBridge.h"
#include "CSSMutableStyleDeclaration.h"
#include "DateExtension.h"
#include "DocumentLoader.h"
#include "Frame.h"
#include "FrameLoaderClient.h"
#include "InspectorTimelineAgent.h"
#include "Page.h"
#include "PageGroup.h"
#include "ScriptController.h"
#include "StorageNamespace.h"
#include "V8Binding.h"
#include "V8BindingState.h"
#include "V8Collection.h"
#include "V8ConsoleMessage.h"
#include "V8DOMMap.h"
#include "V8DOMWindow.h"
#include "V8Document.h"
#include "V8GCForContextDispose.h"
#include "V8HiddenPropertyName.h"
#include "V8History.h"
#include "V8Location.h"
#include "V8Proxy.h"
#include "WorkerContextExecutionProxy.h"
#include <algorithm>
#include <stdio.h>
#include <utility>
#include <v8-debug.h>
#include <v8.h>
#include <wtf/Assertions.h>
#include <wtf/OwnArrayPtr.h>
#include <wtf/StdLibExtras.h>
#include <wtf/StringExtras.h>
#include <wtf/UnusedParam.h>
#include <wtf/text/CString.h>
namespace WebCore {
static void handleFatalErrorInV8()
{
CRASH();
}
static void reportFatalErrorInV8(const char* location, const char* message)
{
printf("V8 error: %s (%s)\n", message, location);
handleFatalErrorInV8();
}
static Frame* getTargetFrame(v8::Local<v8::Object> host, v8::Local<v8::Value> data)
{
Frame* target = 0;
WrapperTypeInfo* type = WrapperTypeInfo::unwrap(data);
if (V8DOMWindow::info.equals(type)) {
v8::Handle<v8::Object> window = V8DOMWrapper::lookupDOMWrapper(V8DOMWindow::GetTemplate(), host);
if (window.IsEmpty())
return target;
DOMWindow* targetWindow = V8DOMWindow::toNative(window);
target = targetWindow->frame();
} else if (V8History::info.equals(type)) {
History* history = V8History::toNative(host);
target = history->frame();
} else if (V8Location::info.equals(type)) {
Location* location = V8Location::toNative(host);
target = location->frame();
}
return target;
}
static void reportUnsafeJavaScriptAccess(v8::Local<v8::Object> host, v8::AccessType type, v8::Local<v8::Value> data)
{
Frame* target = getTargetFrame(host, data);
if (target)
V8Proxy::reportUnsafeAccessTo(target, V8Proxy::ReportLater);
}
PassRefPtr<V8DOMWindowShell> V8DOMWindowShell::create(Frame* frame)
{
return adoptRef(new V8DOMWindowShell(frame));
}
V8DOMWindowShell::V8DOMWindowShell(Frame* frame)
: m_frame(frame)
{
}
bool V8DOMWindowShell::isContextInitialized()
{
ASSERT(m_context.IsEmpty() || !m_global.IsEmpty());
return !m_context.IsEmpty();
}
void V8DOMWindowShell::disposeContextHandles()
{
if (!m_context.IsEmpty()) {
m_frame->loader()->client()->didDestroyScriptContextForFrame();
m_context.Dispose();
m_context.Clear();
V8GCForContextDispose::instance().notifyContextDisposed();
}
WrapperBoilerplateMap::iterator it = m_wrapperBoilerplates.begin();
for (; it != m_wrapperBoilerplates.end(); ++it) {
v8::Persistent<v8::Object> wrapper = it->second;
wrapper.Dispose();
wrapper.Clear();
}
m_wrapperBoilerplates.clear();
}
void V8DOMWindowShell::destroyGlobal()
{
if (!m_global.IsEmpty()) {
#ifndef NDEBUG
V8GCController::unregisterGlobalHandle(this, m_global);
#endif
m_global.Dispose();
m_global.Clear();
}
}
void V8DOMWindowShell::clearForClose()
{
if (!m_context.IsEmpty()) {
v8::HandleScope handleScope;
clearDocumentWrapper();
disposeContextHandles();
}
}
void V8DOMWindowShell::clearForNavigation()
{
if (!m_context.IsEmpty()) {
v8::HandleScope handle;
clearDocumentWrapper();
v8::Context::Scope contextScope(m_context);
clearDocumentWrapperCache();
v8::Handle<v8::Object> wrapper = V8DOMWrapper::lookupDOMWrapper(V8DOMWindow::GetTemplate(), m_global);
ASSERT(!wrapper.IsEmpty());
wrapper->TurnOnAccessCheck();
m_context->DetachGlobal();
disposeContextHandles();
}
}
void V8DOMWindowShell::initContextIfNeeded()
{
if (!m_context.IsEmpty())
return;
v8::HandleScope handleScope;
static bool isV8Initialized = false;
if (!isV8Initialized) {
v8::V8::IgnoreOutOfMemoryException();
v8::V8::SetFatalErrorHandler(reportFatalErrorInV8);
v8::V8::SetGlobalGCPrologueCallback(&V8GCController::gcPrologue);
v8::V8::SetGlobalGCEpilogueCallback(&V8GCController::gcEpilogue);
v8::V8::AddMessageListener(&V8ConsoleMessage::handler);
v8::V8::SetFailedAccessCheckCallbackFunction(reportUnsafeJavaScriptAccess);
isV8Initialized = true;
}
m_context = createNewContext(m_global, 0);
if (m_context.IsEmpty())
return;
v8::Local<v8::Context> v8Context = v8::Local<v8::Context>::New(m_context);
v8::Context::Scope contextScope(v8Context);
if (m_global.IsEmpty()) {
m_global = v8::Persistent<v8::Object>::New(v8Context->Global());
if (m_global.IsEmpty()) {
disposeContextHandles();
return;
}
#ifndef NDEBUG
V8GCController::registerGlobalHandle(PROXY, this, m_global);
#endif
}
if (!installHiddenObjectPrototype(v8Context)) {
disposeContextHandles();
return;
}
if (!installDOMWindow(v8Context, m_frame->domWindow())) {
disposeContextHandles();
return;
}
updateDocument();
setSecurityToken();
m_frame->loader()->client()->didCreateScriptContextForFrame();
m_frame->loader()->dispatchDidClearWindowObjectInWorld(0);
}
v8::Persistent<v8::Context> V8DOMWindowShell::createNewContext(v8::Handle<v8::Object> global, int extensionGroup)
{
v8::Persistent<v8::Context> result;
if (!m_frame->loader()->activeDocumentLoader())
return result;
v8::Persistent<v8::ObjectTemplate> globalTemplate = V8DOMWindow::GetShadowObjectTemplate();
if (globalTemplate.IsEmpty())
return result;
if (!V8Proxy::registeredExtensionWithV8(DateExtension::get()))
V8Proxy::registerExtension(DateExtension::get(), String());
const V8Extensions& extensions = V8Proxy::extensions();
OwnArrayPtr<const char*> extensionNames(new const char*[extensions.size()]);
int index = 0;
for (size_t i = 0; i < extensions.size(); ++i) {
if (extensions[i].group && extensions[i].group != extensionGroup)
continue;
if (extensions[i].scheme.length() > 0 && (extensions[i].scheme != m_frame->loader()->activeDocumentLoader()->url().protocol()))
continue;
extensionNames[index++] = extensions[i].extension->name();
}
v8::ExtensionConfiguration extensionConfiguration(index, extensionNames.get());
result = v8::Context::New(&extensionConfiguration, globalTemplate, global);
return result;
}
void V8DOMWindowShell::setContext(v8::Handle<v8::Context> context)
{
if (!m_context.IsEmpty()) {
m_context.Dispose();
m_context.Clear();
}
m_context = v8::Persistent<v8::Context>::New(context);
}
bool V8DOMWindowShell::installDOMWindow(v8::Handle<v8::Context> context, DOMWindow* window)
{
v8::Handle<v8::Function> windowConstructor = V8DOMWrapper::getConstructor(&V8DOMWindow::info, getHiddenObjectPrototype(context));
v8::Local<v8::Object> jsWindow = SafeAllocation::newInstance(windowConstructor);
if (jsWindow.IsEmpty())
return false;
V8DOMWrapper::setDOMWrapper(jsWindow, &V8DOMWindow::info, window);
V8DOMWrapper::setDOMWrapper(v8::Handle<v8::Object>::Cast(jsWindow->GetPrototype()), &V8DOMWindow::info, window);
window->ref();
V8DOMWrapper::setJSWrapperForDOMObject(window, v8::Persistent<v8::Object>::New(jsWindow));
v8::Handle<v8::Object> v8RealGlobal = v8::Handle<v8::Object>::Cast(context->Global()->GetPrototype());
V8DOMWrapper::setDOMWrapper(v8RealGlobal, &V8DOMWindow::info, window);
v8RealGlobal->SetPrototype(jsWindow);
return true;
}
void V8DOMWindowShell::updateDocumentWrapper(v8::Handle<v8::Object> wrapper)
{
clearDocumentWrapper();
ASSERT(m_document.IsEmpty());
m_document = v8::Persistent<v8::Object>::New(wrapper);
#ifndef NDEBUG
V8GCController::registerGlobalHandle(PROXY, this, m_document);
#endif
}
void V8DOMWindowShell::clearDocumentWrapper()
{
if (!m_document.IsEmpty()) {
#ifndef NDEBUG
V8GCController::unregisterGlobalHandle(this, m_document);
#endif
m_document.Dispose();
m_document.Clear();
}
}
void V8DOMWindowShell::updateDocumentWrapperCache()
{
v8::HandleScope handleScope;
v8::Context::Scope contextScope(m_context);
if (!m_frame->document()->frame()) {
clearDocumentWrapperCache();
return;
}
v8::Handle<v8::Value> documentWrapper = toV8(m_frame->document());
if (documentWrapper.IsEmpty()) {
clearDocumentWrapperCache();
return;
}
ASSERT(documentWrapper->IsObject());
m_context->Global()->ForceSet(v8::String::New("document"), documentWrapper, static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
}
void V8DOMWindowShell::clearDocumentWrapperCache()
{
ASSERT(!m_context.IsEmpty());
m_context->Global()->ForceDelete(v8::String::New("document"));
}
void V8DOMWindowShell::setSecurityToken()
{
Document* document = m_frame->document();
if (!document) {
m_context->UseDefaultSecurityToken();
return;
}
SecurityOrigin* origin = document->securityOrigin();
String token;
if (!origin->domainWasSetInDOM())
token = document->securityOrigin()->toString();
if (token.isEmpty() || token == "null") {
m_context->UseDefaultSecurityToken();
return;
}
CString utf8Token = token.utf8();
m_context->SetSecurityToken(v8::String::NewSymbol(utf8Token.data(), utf8Token.length()));
}
void V8DOMWindowShell::updateDocument()
{
if (!m_frame->document())
return;
if (m_global.IsEmpty())
return;
initContextIfNeeded();
if (m_context.IsEmpty())
return;
updateDocumentWrapperCache();
updateSecurityOrigin();
}
void V8DOMWindowShell::updateSecurityOrigin()
{
v8::HandleScope scope;
setSecurityToken();
}
v8::Handle<v8::Value> V8DOMWindowShell::getHiddenObjectPrototype(v8::Handle<v8::Context> context)
{
return context->Global()->GetHiddenValue(V8HiddenPropertyName::objectPrototype());
}
bool V8DOMWindowShell::installHiddenObjectPrototype(v8::Handle<v8::Context> context)
{
v8::Handle<v8::String> objectString = v8::String::New("Object");
v8::Handle<v8::String> prototypeString = v8::String::New("prototype");
v8::Handle<v8::String> hiddenObjectPrototypeString = V8HiddenPropertyName::objectPrototype();
if (objectString.IsEmpty() || prototypeString.IsEmpty() || hiddenObjectPrototypeString.IsEmpty())
return false;
v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(context->Global()->Get(objectString));
if (object.IsEmpty())
return false;
v8::Handle<v8::Value> objectPrototype = object->Get(prototypeString);
if (objectPrototype.IsEmpty())
return false;
context->Global()->SetHiddenValue(hiddenObjectPrototypeString, objectPrototype);
return true;
}
v8::Local<v8::Object> V8DOMWindowShell::createWrapperFromCacheSlowCase(WrapperTypeInfo* type)
{
initContextIfNeeded();
v8::Context::Scope scope(m_context);
v8::Local<v8::Function> function = V8DOMWrapper::getConstructor(type, getHiddenObjectPrototype(m_context));
v8::Local<v8::Object> instance = SafeAllocation::newInstance(function);
if (!instance.IsEmpty()) {
m_wrapperBoilerplates.set(type, v8::Persistent<v8::Object>::New(instance));
return instance->Clone();
}
return notHandledByInterceptor();
}
void V8DOMWindowShell::setLocation(DOMWindow* window, const String& relativeURL)
{
Frame* frame = window->frame();
if (!frame)
return;
KURL url = completeURL(relativeURL);
if (url.isNull())
return;
if (!shouldAllowNavigation(frame))
return;
navigateIfAllowed(frame, url, false, false);
}
}