This source file includes following definitions.
- checkDocumentWrapper
- setInjectedScriptContextDebugId
- create
- m_world
- disposeContext
- clearForClose
- clearForNavigation
- initializeIfNeeded
- initialize
- createContext
- toInnerGlobalObject
- installDOMWindow
- updateDocumentWrapper
- updateDocumentProperty
- clearDocumentProperty
- setSecurityToken
- updateDocument
- getNamedProperty
- getter
- namedItemAdded
- namedItemRemoved
- updateSecurityOrigin
#include "config.h"
#include "bindings/v8/V8WindowShell.h"
#include "RuntimeEnabledFeatures.h"
#include "V8Document.h"
#include "V8HTMLCollection.h"
#include "V8HTMLDocument.h"
#include "V8Window.h"
#include "bindings/v8/DOMWrapperWorld.h"
#include "bindings/v8/ScriptController.h"
#include "bindings/v8/V8Binding.h"
#include "bindings/v8/V8GCForContextDispose.h"
#include "bindings/v8/V8HiddenValue.h"
#include "bindings/v8/V8Initializer.h"
#include "bindings/v8/V8ObjectConstructor.h"
#include "core/html/HTMLCollection.h"
#include "core/html/HTMLIFrameElement.h"
#include "core/inspector/InspectorInstrumentation.h"
#include "core/loader/DocumentLoader.h"
#include "core/loader/FrameLoader.h"
#include "core/loader/FrameLoaderClient.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/csp/ContentSecurityPolicy.h"
#include "heap/Handle.h"
#include "platform/TraceEvent.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "public/platform/Platform.h"
#include "wtf/Assertions.h"
#include "wtf/OwnPtr.h"
#include "wtf/StringExtras.h"
#include "wtf/text/CString.h"
#include <algorithm>
#include <utility>
#include <v8-debug.h>
#include <v8.h>
namespace WebCore {
static void checkDocumentWrapper(v8::Handle<v8::Object> wrapper, Document* document)
{
ASSERT(V8Document::toNative(wrapper) == document);
ASSERT(!document->isHTMLDocument() || (V8Document::toNative(v8::Handle<v8::Object>::Cast(wrapper->GetPrototype())) == document));
}
static void setInjectedScriptContextDebugId(v8::Handle<v8::Context> targetContext, int debugId)
{
V8PerContextDebugData::setContextDebugData(targetContext, "injected", debugId);
}
PassOwnPtr<V8WindowShell> V8WindowShell::create(LocalFrame* frame, DOMWrapperWorld& world, v8::Isolate* isolate)
{
return adoptPtr(new V8WindowShell(frame, &world, isolate));
}
V8WindowShell::V8WindowShell(LocalFrame* frame, PassRefPtr<DOMWrapperWorld> world, v8::Isolate* isolate)
: m_frame(frame)
, m_isolate(isolate)
, m_world(world)
{
}
void V8WindowShell::disposeContext(GlobalDetachmentBehavior behavior)
{
if (!isContextInitialized())
return;
v8::HandleScope handleScope(m_isolate);
v8::Handle<v8::Context> context = m_scriptState->context();
m_frame->loader().client()->willReleaseScriptContext(context, m_world->worldId());
if (behavior == DetachGlobal)
context->DetachGlobal();
m_scriptState->disposePerContextData();
V8GCForContextDispose::instanceTemplate().notifyContextDisposed(m_frame->isMainFrame());
}
void V8WindowShell::clearForClose()
{
if (!isContextInitialized())
return;
m_document.clear();
disposeContext(DoNotDetachGlobal);
}
void V8WindowShell::clearForNavigation()
{
if (!isContextInitialized())
return;
v8::HandleScope handleScope(m_isolate);
m_document.clear();
v8::Handle<v8::Context> context = m_scriptState->context();
v8::Context::Scope contextScope(context);
clearDocumentProperty();
v8::Handle<v8::Object> windowWrapper = V8Window::findInstanceInPrototypeChain(m_global.newLocal(m_isolate), m_isolate);
ASSERT(!windowWrapper.IsEmpty());
windowWrapper->TurnOnAccessCheck();
disposeContext(DetachGlobal);
}
bool V8WindowShell::initializeIfNeeded()
{
if (isContextInitialized())
return true;
DOMWrapperWorld::setWorldOfInitializingWindow(m_world.get());
bool result = initialize();
DOMWrapperWorld::setWorldOfInitializingWindow(0);
return result;
}
bool V8WindowShell::initialize()
{
TRACE_EVENT0("v8", "V8WindowShell::initialize");
TRACE_EVENT_SCOPED_SAMPLING_STATE("Blink", "InitializeWindow");
v8::HandleScope handleScope(m_isolate);
createContext();
if (!isContextInitialized())
return false;
v8::Handle<v8::Context> context = m_scriptState->context();
v8::Context::Scope contextScope(context);
if (m_global.isEmpty()) {
m_global.set(m_isolate, context->Global());
if (m_global.isEmpty()) {
disposeContext(DoNotDetachGlobal);
return false;
}
}
if (!m_world->isMainWorld()) {
V8WindowShell* mainWindow = m_frame->script().existingWindowShell(DOMWrapperWorld::mainWorld());
if (mainWindow && !mainWindow->context().IsEmpty())
setInjectedScriptContextDebugId(context, m_frame->script().contextDebugId(mainWindow->context()));
}
m_scriptState->perContextData()->setActivityLogger(V8DOMActivityLogger::activityLogger(m_world->worldId()));
if (!installDOMWindow()) {
disposeContext(DoNotDetachGlobal);
return false;
}
if (m_world->isMainWorld()) {
updateDocument();
if (m_frame->document()) {
setSecurityToken(m_frame->document()->securityOrigin());
ContentSecurityPolicy* csp = m_frame->document()->contentSecurityPolicy();
context->AllowCodeGenerationFromStrings(csp->allowEval(0, ContentSecurityPolicy::SuppressReport));
context->SetErrorMessageForCodeGenerationFromStrings(v8String(m_isolate, csp->evalDisabledErrorMessage()));
}
} else {
context->UseDefaultSecurityToken();
SecurityOrigin* origin = m_world->isolatedWorldSecurityOrigin();
if (origin && InspectorInstrumentation::hasFrontends()) {
ScriptState* scriptState = ScriptState::forContext(v8::Local<v8::Context>::New(m_isolate, context));
InspectorInstrumentation::didCreateIsolatedContext(m_frame, scriptState, origin);
}
}
m_frame->loader().client()->didCreateScriptContext(context, m_world->extensionGroup(), m_world->worldId());
return true;
}
void V8WindowShell::createContext()
{
if (!m_frame->loader().documentLoader())
return;
v8::Handle<v8::ObjectTemplate> globalTemplate = V8Window::getShadowObjectTemplate(m_isolate);
if (globalTemplate.IsEmpty())
return;
double contextCreationStartInSeconds = currentTime();
const V8Extensions& extensions = ScriptController::registeredExtensions();
OwnPtr<const char*[]> extensionNames = adoptArrayPtr(new const char*[extensions.size()]);
int index = 0;
int extensionGroup = m_world->extensionGroup();
int worldId = m_world->worldId();
for (size_t i = 0; i < extensions.size(); ++i) {
if (!m_frame->loader().client()->allowScriptExtension(extensions[i]->name(), extensionGroup, worldId))
continue;
extensionNames[index++] = extensions[i]->name();
}
v8::ExtensionConfiguration extensionConfiguration(index, extensionNames.get());
v8::Handle<v8::Context> context = v8::Context::New(m_isolate, &extensionConfiguration, globalTemplate, m_global.newLocal(m_isolate));
if (context.IsEmpty())
return;
m_scriptState = NewScriptState::create(context, m_world);
double contextCreationDurationInMilliseconds = (currentTime() - contextCreationStartInSeconds) * 1000;
const char* histogramName = "WebCore.V8WindowShell.createContext.MainWorld";
if (!m_world->isMainWorld())
histogramName = "WebCore.V8WindowShell.createContext.IsolatedWorld";
blink::Platform::current()->histogramCustomCounts(histogramName, contextCreationDurationInMilliseconds, 0, 10000, 50);
}
static v8::Handle<v8::Object> toInnerGlobalObject(v8::Handle<v8::Context> context)
{
return v8::Handle<v8::Object>::Cast(context->Global()->GetPrototype());
}
bool V8WindowShell::installDOMWindow()
{
DOMWindow* window = m_frame->domWindow();
v8::Local<v8::Object> windowWrapper = V8ObjectConstructor::newInstance(m_isolate, m_scriptState->perContextData()->constructorForType(&V8Window::wrapperTypeInfo));
if (windowWrapper.IsEmpty())
return false;
V8Window::installPerContextEnabledProperties(windowWrapper, window, m_isolate);
V8DOMWrapper::setNativeInfoForHiddenWrapper(v8::Handle<v8::Object>::Cast(windowWrapper->GetPrototype()), &V8Window::wrapperTypeInfo, window);
v8::Handle<v8::Object> innerGlobalObject = toInnerGlobalObject(m_scriptState->context());
V8DOMWrapper::setNativeInfoForHiddenWrapper(innerGlobalObject, &V8Window::wrapperTypeInfo, window);
innerGlobalObject->SetPrototype(windowWrapper);
V8DOMWrapper::associateObjectWithWrapper<V8Window>(PassRefPtrWillBeRawPtr<DOMWindow>(window), &V8Window::wrapperTypeInfo, windowWrapper, m_isolate, WrapperConfiguration::Dependent);
return true;
}
void V8WindowShell::updateDocumentWrapper(v8::Handle<v8::Object> wrapper)
{
ASSERT(m_world->isMainWorld());
m_document.set(m_isolate, wrapper);
}
void V8WindowShell::updateDocumentProperty()
{
if (!m_world->isMainWorld())
return;
v8::HandleScope handleScope(m_isolate);
v8::Handle<v8::Context> context = m_scriptState->context();
v8::Context::Scope contextScope(context);
v8::Handle<v8::Value> documentWrapper = toV8(m_frame->document(), v8::Handle<v8::Object>(), context->GetIsolate());
ASSERT(documentWrapper == m_document.newLocal(m_isolate) || m_document.isEmpty());
if (m_document.isEmpty())
updateDocumentWrapper(v8::Handle<v8::Object>::Cast(documentWrapper));
checkDocumentWrapper(m_document.newLocal(m_isolate), m_frame->document());
if (documentWrapper.IsEmpty()) {
clearDocumentProperty();
return;
}
ASSERT(documentWrapper->IsObject());
context->Global()->ForceSet(v8AtomicString(m_isolate, "document"), documentWrapper, static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
V8HiddenValue::setHiddenValue(m_isolate, toInnerGlobalObject(context), V8HiddenValue::document(m_isolate), documentWrapper);
}
void V8WindowShell::clearDocumentProperty()
{
ASSERT(isContextInitialized());
if (!m_world->isMainWorld())
return;
v8::HandleScope handleScope(m_isolate);
m_scriptState->context()->Global()->ForceDelete(v8AtomicString(m_isolate, "document"));
}
void V8WindowShell::setSecurityToken(SecurityOrigin* origin)
{
ASSERT(m_world->isMainWorld());
String token;
if (!origin->domainWasSetInDOM()
&& !m_frame->loader().stateMachine()->isDisplayingInitialEmptyDocument())
token = origin->toString();
v8::HandleScope handleScope(m_isolate);
v8::Handle<v8::Context> context = m_scriptState->context();
if (token.isEmpty() || token == "null") {
context->UseDefaultSecurityToken();
return;
}
CString utf8Token = token.utf8();
context->SetSecurityToken(v8AtomicString(m_isolate, utf8Token.data(), utf8Token.length()));
}
void V8WindowShell::updateDocument()
{
ASSERT(m_world->isMainWorld());
if (!isGlobalInitialized())
return;
if (!isContextInitialized())
return;
updateDocumentProperty();
updateSecurityOrigin(m_frame->document()->securityOrigin());
}
static v8::Handle<v8::Value> getNamedProperty(HTMLDocument* htmlDocument, const AtomicString& key, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
if (!htmlDocument->hasNamedItem(key) && !htmlDocument->hasExtraNamedItem(key))
return v8Undefined();
RefPtr<HTMLCollection> items = htmlDocument->documentNamedItems(key);
if (items->isEmpty())
return v8Undefined();
if (items->hasExactlyOneItem()) {
Element* element = items->item(0);
ASSERT(element);
Frame* frame = 0;
if (isHTMLIFrameElement(*element) && (frame = toHTMLIFrameElement(*element).contentFrame()))
return toV8(frame->domWindow(), creationContext, isolate);
return toV8(element, creationContext, isolate);
}
return toV8(items.release(), creationContext, isolate);
}
static void getter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info)
{
AtomicString name = toCoreAtomicString(property);
HTMLDocument* htmlDocument = V8HTMLDocument::toNative(info.Holder());
ASSERT(htmlDocument);
v8::Handle<v8::Value> result = getNamedProperty(htmlDocument, name, info.Holder(), info.GetIsolate());
if (!result.IsEmpty()) {
v8SetReturnValue(info, result);
return;
}
v8::Handle<v8::Value> prototype = info.Holder()->GetPrototype();
if (prototype->IsObject()) {
v8SetReturnValue(info, prototype.As<v8::Object>()->Get(property));
return;
}
}
void V8WindowShell::namedItemAdded(HTMLDocument* document, const AtomicString& name)
{
ASSERT(m_world->isMainWorld());
if (!isContextInitialized())
return;
v8::HandleScope handleScope(m_isolate);
v8::Context::Scope contextScope(m_scriptState->context());
ASSERT(!m_document.isEmpty());
v8::Handle<v8::Object> documentHandle = m_document.newLocal(m_isolate);
checkDocumentWrapper(documentHandle, document);
documentHandle->SetAccessor(v8String(m_isolate, name), getter);
}
void V8WindowShell::namedItemRemoved(HTMLDocument* document, const AtomicString& name)
{
ASSERT(m_world->isMainWorld());
if (!isContextInitialized())
return;
if (document->hasNamedItem(name) || document->hasExtraNamedItem(name))
return;
v8::HandleScope handleScope(m_isolate);
v8::Context::Scope contextScope(m_scriptState->context());
ASSERT(!m_document.isEmpty());
v8::Handle<v8::Object> documentHandle = m_document.newLocal(m_isolate);
checkDocumentWrapper(documentHandle, document);
documentHandle->Delete(v8String(m_isolate, name));
}
void V8WindowShell::updateSecurityOrigin(SecurityOrigin* origin)
{
ASSERT(m_world->isMainWorld());
if (!isContextInitialized())
return;
setSecurityToken(origin);
}
}