void V8WorkerContextEventListener::handleEvent(ScriptExecutionContext* context, Event* event) { if (!context) return; // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it. // See issue 889829. RefPtr<V8AbstractEventListener> protect(this); v8::HandleScope handleScope; WorkerContextExecutionProxy* proxy = workerProxy(context); if (!proxy) return; v8::Handle<v8::Context> v8Context = proxy->context(); if (v8Context.IsEmpty()) return; // Enter the V8 context in which to perform the event handling. v8::Context::Scope scope(v8Context); // Get the V8 wrapper for the event object. v8::Handle<v8::Value> jsEvent = toV8(event); invokeEventHandler(context, event, jsEvent); }
void WorkerScriptDebugServer::addListener(ScriptDebugListener* listener, WorkerContext* workerContext) { v8::HandleScope scope; v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); v8::Context::Scope contextScope(debuggerContext); if (!m_listenersMap.size()) { // FIXME: synchronize access to this code. ensureDebuggerScriptCompiled(); ASSERT(!m_debuggerScript.get()->IsUndefined()); v8::Debug::SetDebugEventListener2(&WorkerScriptDebugServer::v8DebugEventCallback, v8::External::New(this)); } m_listenersMap.set(workerContext, listener); WorkerContextExecutionProxy* proxy = workerContext->script()->proxy(); if (!proxy) return; v8::Handle<v8::Context> context = proxy->context(); v8::Handle<v8::Function> getScriptsFunction = v8::Local<v8::Function>::Cast(m_debuggerScript.get()->Get(v8::String::New("getWorkerScripts"))); v8::Handle<v8::Value> argv[] = { v8::Handle<v8::Value>() }; v8::Handle<v8::Value> value = getScriptsFunction->Call(m_debuggerScript.get(), 0, argv); if (value.IsEmpty()) return; ASSERT(!value->IsUndefined() && value->IsArray()); v8::Handle<v8::Array> scriptsArray = v8::Handle<v8::Array>::Cast(value); for (unsigned i = 0; i < scriptsArray->Length(); ++i) dispatchDidParseSource(listener, v8::Handle<v8::Object>::Cast(scriptsArray->Get(v8::Integer::New(i)))); }
ScriptState* scriptStateFromWorkerContext(WorkerContext* workerContext) { WorkerContextExecutionProxy* proxy = workerContext->script()->proxy(); if (!proxy) return 0; v8::HandleScope handleScope; v8::Local<v8::Context> context = proxy->context(); return ScriptState::forContext(context); }
v8::Local<v8::Function> V8DOMWrapper::getConstructor(V8ClassIndex::V8WrapperType type, WorkerContext*) { WorkerContextExecutionProxy* proxy = WorkerContextExecutionProxy::retrieve(); if (!proxy) return v8::Local<v8::Function>(); v8::Handle<v8::Context> context = proxy->context(); if (context.IsEmpty()) return v8::Local<v8::Function>(); return getConstructorForContext(type, context); }
v8::Handle<v8::Value> toV8(WorkerContext* impl) { if (!impl) return v8::Null(); WorkerContextExecutionProxy* proxy = impl->script()->proxy(); if (!proxy) return v8::Null(); v8::Handle<v8::Object> global = proxy->context()->Global(); ASSERT(!global.IsEmpty()); return global; }
v8::Local<v8::Function> V8DOMWrapper::getConstructor(WrapperTypeInfo* type, WorkerContext*) { WorkerScriptController* controller = WorkerScriptController::controllerForContext(); WorkerContextExecutionProxy* proxy = controller ? controller->proxy() : 0; if (!proxy) return v8::Local<v8::Function>(); v8::Handle<v8::Context> context = proxy->context(); if (context.IsEmpty()) return v8::Local<v8::Function>(); return getConstructorForContext(type, context); }
v8::Handle<v8::Value> toV8(WorkerContext* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) { // Notice that we explicitly ignore creationContext because the WorkerContext is its own creationContext. if (!impl) return v8NullWithCheck(isolate); WorkerContextExecutionProxy* proxy = impl->script()->proxy(); if (!proxy) return v8NullWithCheck(isolate); v8::Handle<v8::Object> global = proxy->context()->Global(); ASSERT(!global.IsEmpty()); return global; }
v8::Handle<v8::Value> SetTimeoutOrInterval(const v8::Arguments& args, bool singleShot) { WorkerContext* workerContext = V8WorkerContext::toNative(args.Holder()); int argumentCount = args.Length(); if (argumentCount < 1) return v8::Undefined(); v8::Handle<v8::Value> function = args[0]; int32_t timeout = argumentCount >= 2 ? args[1]->Int32Value() : 0; int timerId; WorkerContextExecutionProxy* proxy = workerContext->script()->proxy(); if (!proxy) return v8::Undefined(); v8::Handle<v8::Context> v8Context = proxy->context(); if (function->IsString()) { if (ContentSecurityPolicy* policy = workerContext->contentSecurityPolicy()) { if (!policy->allowEval()) return v8Integer(0, args.GetIsolate()); } WTF::String stringFunction = toWebCoreString(function); timerId = DOMTimer::install(workerContext, adoptPtr(new ScheduledAction(v8Context, stringFunction, workerContext->url())), timeout, singleShot); } else if (function->IsFunction()) { size_t paramCount = argumentCount >= 2 ? argumentCount - 2 : 0; v8::Local<v8::Value>* params = 0; if (paramCount > 0) { params = new v8::Local<v8::Value>[paramCount]; for (size_t i = 0; i < paramCount; ++i) params[i] = args[i+2]; } // ScheduledAction takes ownership of actual params and releases them in its destructor. OwnPtr<ScheduledAction> action = adoptPtr(new ScheduledAction(v8Context, v8::Handle<v8::Function>::Cast(function), paramCount, params)); // FIXME: We should use a OwnArrayPtr for params. delete [] params; timerId = DOMTimer::install(workerContext, action.release(), timeout, singleShot); } else return v8::Undefined(); return v8Integer(timerId, args.GetIsolate()); }
v8::Handle<v8::Value> SetTimeoutOrInterval(const v8::Arguments& args, bool singleShot) { WorkerContext* workerContext = V8WorkerContext::toNative(args.Holder()); int argumentCount = args.Length(); if (argumentCount < 1) return v8::Undefined(); v8::Handle<v8::Value> function = args[0]; int32_t timeout = argumentCount >= 2 ? args[1]->Int32Value() : 0; int timerId; WorkerContextExecutionProxy* proxy = workerContext->script()->proxy(); if (!proxy) return v8::Undefined(); v8::Handle<v8::Context> v8Context = proxy->context(); if (function->IsString()) { WTF::String stringFunction = toWebCoreString(function); timerId = DOMTimer::install(workerContext, new ScheduledAction(v8Context, stringFunction, workerContext->url()), timeout, singleShot); } else if (function->IsFunction()) { size_t paramCount = argumentCount >= 2 ? argumentCount - 2 : 0; v8::Local<v8::Value>* params = 0; if (paramCount > 0) { params = new v8::Local<v8::Value>[paramCount]; for (size_t i = 0; i < paramCount; ++i) params[i] = args[i+2]; } // ScheduledAction takes ownership of actual params and releases them in its destructor. ScheduledAction* action = new ScheduledAction(v8Context, v8::Handle<v8::Function>::Cast(function), paramCount, params); delete [] params; timerId = DOMTimer::install(workerContext, action, timeout, singleShot); } else return v8::Undefined(); return v8::Integer::New(timerId); }