2 * Copyright (C) 2009, 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "bindings/v8/WorkerScriptController.h"
35 #include "V8DedicatedWorkerGlobalScope.h"
36 #include "V8ServiceWorkerGlobalScope.h"
37 #include "V8SharedWorkerGlobalScope.h"
38 #include "V8WorkerGlobalScope.h"
39 #include "bindings/v8/ScriptSourceCode.h"
40 #include "bindings/v8/ScriptValue.h"
41 #include "bindings/v8/V8ErrorHandler.h"
42 #include "bindings/v8/V8GCController.h"
43 #include "bindings/v8/V8Initializer.h"
44 #include "bindings/v8/V8ObjectConstructor.h"
45 #include "bindings/v8/V8ScriptRunner.h"
46 #include "bindings/v8/WrapperTypeInfo.h"
47 #include "core/inspector/ScriptCallStack.h"
48 #include "core/frame/DOMTimer.h"
49 #include "core/workers/SharedWorkerGlobalScope.h"
50 #include "core/workers/WorkerGlobalScope.h"
51 #include "core/workers/WorkerObjectProxy.h"
52 #include "core/workers/WorkerThread.h"
53 #include "heap/ThreadState.h"
56 #include "public/platform/Platform.h"
57 #include "public/platform/WebWorkerRunLoop.h"
61 WorkerScriptController::WorkerScriptController(WorkerGlobalScope& workerGlobalScope)
62 : m_workerGlobalScope(workerGlobalScope)
63 , m_executionForbidden(false)
64 , m_executionScheduledToTerminate(false)
66 v8::Isolate* isolate = v8::Isolate::New();
68 V8Initializer::initializeWorker(isolate);
70 m_isolateHolder = adoptPtr(new gin::IsolateHolder(isolate));
71 V8PerIsolateData* data = V8PerIsolateData::create(isolate);
72 m_domDataStore = adoptPtr(new DOMDataStore(WorkerWorld));
73 data->setWorkerDOMDataStore(m_domDataStore.get());
75 m_interruptor = adoptPtr(new V8IsolateInterruptor(isolate));
76 ThreadState::current()->addInterruptor(m_interruptor.get());
80 WorkerScriptController::~WorkerScriptController()
83 ThreadState::current()->removeInterruptor(m_interruptor.get());
86 m_domDataStore.clear();
88 // The corresponding call to didStartWorkerRunLoop is in
89 // WorkerThread::workerThread().
90 // See http://webkit.org/b/83104#c14 for why this is here.
91 blink::Platform::current()->didStopWorkerRunLoop(blink::WebWorkerRunLoop(&m_workerGlobalScope.thread()->runLoop()));
94 V8PerIsolateData::dispose(isolate());
95 v8::Isolate* v8Isolate = isolate();
97 m_isolateHolder.clear();
101 void WorkerScriptController::disposeContext()
103 m_perContextData.clear();
104 m_contextHolder.clear();
107 bool WorkerScriptController::initializeContextIfNeeded()
112 v8::Handle<v8::Context> context = v8::Context::New(isolate());
113 if (context.IsEmpty())
116 m_contextHolder = adoptPtr(new gin::ContextHolder(isolate()));
117 m_contextHolder->SetContext(context);
119 v8::Context::Scope scope(context);
121 V8PerContextDataHolder::install(context);
123 m_perContextData = V8PerContextData::create(context);
124 if (!m_perContextData->init()) {
129 // Set DebugId for the new context.
130 context->SetEmbedderData(0, v8AtomicString(isolate(), "worker"));
132 // Create a new JS object and use it as the prototype for the shadow global object.
133 const WrapperTypeInfo* contextType = &V8DedicatedWorkerGlobalScope::wrapperTypeInfo;
134 if (m_workerGlobalScope.isServiceWorkerGlobalScope())
135 contextType = &V8ServiceWorkerGlobalScope::wrapperTypeInfo;
136 else if (!m_workerGlobalScope.isDedicatedWorkerGlobalScope())
137 contextType = &V8SharedWorkerGlobalScope::wrapperTypeInfo;
138 v8::Handle<v8::Function> workerGlobalScopeConstructor = m_perContextData->constructorForType(contextType);
139 v8::Local<v8::Object> jsWorkerGlobalScope = V8ObjectConstructor::newInstance(workerGlobalScopeConstructor);
140 if (jsWorkerGlobalScope.IsEmpty()) {
145 V8DOMWrapper::associateObjectWithWrapper<V8WorkerGlobalScope>(PassRefPtr<WorkerGlobalScope>(m_workerGlobalScope), contextType, jsWorkerGlobalScope, isolate(), WrapperConfiguration::Dependent);
147 // Insert the object instance as the prototype of the shadow object.
148 v8::Handle<v8::Object> globalObject = v8::Handle<v8::Object>::Cast(m_contextHolder->context()->Global()->GetPrototype());
149 globalObject->SetPrototype(jsWorkerGlobalScope);
154 ScriptValue WorkerScriptController::evaluate(const String& script, const String& fileName, const TextPosition& scriptStartPosition, WorkerGlobalScopeExecutionState* state)
156 v8::HandleScope handleScope(isolate());
158 if (!initializeContextIfNeeded())
159 return ScriptValue();
161 v8::Handle<v8::Context> context = m_contextHolder->context();
162 if (!m_disableEvalPending.isEmpty()) {
163 context->AllowCodeGenerationFromStrings(false);
164 context->SetErrorMessageForCodeGenerationFromStrings(v8String(isolate(), m_disableEvalPending));
165 m_disableEvalPending = String();
168 v8::Context::Scope scope(context);
172 v8::Handle<v8::String> scriptString = v8String(isolate(), script);
173 v8::Handle<v8::Script> compiledScript = V8ScriptRunner::compileScript(scriptString, fileName, scriptStartPosition, 0, isolate());
174 v8::Local<v8::Value> result = V8ScriptRunner::runCompiledScript(compiledScript, &m_workerGlobalScope, isolate());
176 if (!block.CanContinue()) {
177 m_workerGlobalScope.script()->forbidExecution();
178 return ScriptValue();
181 if (block.HasCaught()) {
182 v8::Local<v8::Message> message = block.Message();
183 state->hadException = true;
184 state->errorMessage = toCoreString(message->Get());
185 state->lineNumber = message->GetLineNumber();
186 state->columnNumber = message->GetStartColumn() + 1;
187 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, sourceURL, message->GetScriptResourceName(), ScriptValue());
188 state->sourceURL = sourceURL;
189 state->exception = ScriptValue(block.Exception(), isolate());
192 state->hadException = false;
194 if (result.IsEmpty() || result->IsUndefined())
195 return ScriptValue();
197 return ScriptValue(result, isolate());
200 void WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, RefPtr<ErrorEvent>* errorEvent)
202 if (isExecutionForbidden())
205 WorkerGlobalScopeExecutionState state;
206 evaluate(sourceCode.source(), sourceCode.url().string(), sourceCode.startPosition(), &state);
207 if (state.hadException) {
209 *errorEvent = m_workerGlobalScope.shouldSanitizeScriptError(state.sourceURL, NotSharableCrossOrigin) ?
210 ErrorEvent::createSanitizedError(0) : ErrorEvent::create(state.errorMessage, state.sourceURL, state.lineNumber, state.columnNumber, 0);
211 V8ErrorHandler::storeExceptionOnErrorEventWrapper(errorEvent->get(), state.exception.v8Value(), isolate());
213 ASSERT(!m_workerGlobalScope.shouldSanitizeScriptError(state.sourceURL, NotSharableCrossOrigin));
214 RefPtr<ErrorEvent> event = m_errorEventFromImportedScript ? m_errorEventFromImportedScript.release() : ErrorEvent::create(state.errorMessage, state.sourceURL, state.lineNumber, state.columnNumber, 0);
215 m_workerGlobalScope.reportException(event, 0, NotSharableCrossOrigin);
220 void WorkerScriptController::scheduleExecutionTermination()
222 // The mutex provides a memory barrier to ensure that once
223 // termination is scheduled, isExecutionTerminating will
224 // accurately reflect that state when called from another thread.
226 MutexLocker locker(m_scheduledTerminationMutex);
227 m_executionScheduledToTerminate = true;
229 v8::V8::TerminateExecution(isolate());
232 bool WorkerScriptController::isExecutionTerminating() const
234 // See comments in scheduleExecutionTermination regarding mutex usage.
235 MutexLocker locker(m_scheduledTerminationMutex);
236 return m_executionScheduledToTerminate;
239 void WorkerScriptController::forbidExecution()
241 ASSERT(m_workerGlobalScope.isContextThread());
242 m_executionForbidden = true;
245 bool WorkerScriptController::isExecutionForbidden() const
247 ASSERT(m_workerGlobalScope.isContextThread());
248 return m_executionForbidden;
251 void WorkerScriptController::disableEval(const String& errorMessage)
253 m_disableEvalPending = errorMessage;
256 void WorkerScriptController::rethrowExceptionFromImportedScript(PassRefPtr<ErrorEvent> errorEvent)
258 m_errorEventFromImportedScript = errorEvent;
259 throwError(V8ThrowException::createError(v8GeneralError, m_errorEventFromImportedScript->message(), isolate()), isolate());
262 WorkerScriptController* WorkerScriptController::controllerForContext()
264 v8::Isolate* isolate = v8::Isolate::GetCurrent();
265 // Happens on frame destruction, check otherwise GetCurrent() will crash.
266 if (!isolate || !isolate->InContext())
268 v8::Handle<v8::Context> context = isolate->GetCurrentContext();
269 v8::Handle<v8::Object> global = context->Global();
270 global = global->FindInstanceInPrototypeChain(V8WorkerGlobalScope::domTemplate(isolate, WorkerWorld));
271 // Return 0 if the current executing context is not the worker context.
272 if (global.IsEmpty())
274 WorkerGlobalScope* workerGlobalScope = V8WorkerGlobalScope::toNative(global);
275 return workerGlobalScope->script();
278 } // namespace WebCore