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.
35 #include "WorkerScriptController.h"
38 #include "ScriptCallStack.h"
39 #include "ScriptRunner.h"
40 #include "ScriptSourceCode.h"
41 #include "ScriptValue.h"
42 #include "V8DedicatedWorkerContext.h"
43 #include "V8Initializer.h"
44 #include "V8SharedWorkerContext.h"
45 #include "V8WorkerContext.h"
46 #include "WorkerContext.h"
47 #include "WorkerObjectProxy.h"
48 #include "WorkerThread.h"
51 #if PLATFORM(CHROMIUM)
52 #include <public/Platform.h>
53 #include <public/WebWorkerRunLoop.h>
58 WorkerScriptController::WorkerScriptController(WorkerContext* workerContext)
59 : m_workerContext(workerContext)
60 , m_isolate(v8::Isolate::New())
61 , m_executionForbidden(false)
62 , m_executionScheduledToTerminate(false)
65 V8PerIsolateData* data = V8PerIsolateData::create(m_isolate);
66 m_domDataStore = adoptPtr(new DOMDataStore(DOMDataStore::Worker));
67 data->setDOMDataStore(m_domDataStore.get());
69 V8Initializer::initializeWorker(m_isolate);
72 WorkerScriptController::~WorkerScriptController()
74 m_domDataStore.clear();
75 #if PLATFORM(CHROMIUM)
76 // The corresponding call to didStartWorkerRunLoop is in
77 // WorkerThread::workerThread().
78 // See http://webkit.org/b/83104#c14 for why this is here.
79 WebKit::Platform::current()->didStopWorkerRunLoop(WebKit::WebWorkerRunLoop(&m_workerContext->thread()->runLoop()));
82 V8PerIsolateData::dispose(m_isolate);
87 void WorkerScriptController::disposeContext()
89 m_perContextData.clear();
93 bool WorkerScriptController::initializeContextIfNeeded()
95 if (!m_context.isEmpty())
98 v8::Persistent<v8::ObjectTemplate> globalTemplate;
99 m_context.adopt(v8::Context::New(0, globalTemplate));
100 if (m_context.isEmpty())
103 // Starting from now, use local context only.
104 v8::Local<v8::Context> context = v8::Local<v8::Context>::New(m_context.get());
106 v8::Context::Scope scope(context);
108 m_perContextData = V8PerContextData::create(m_context.get());
109 if (!m_perContextData->init()) {
114 // Set DebugId for the new context.
115 context->SetEmbedderData(0, v8::String::NewSymbol("worker"));
117 // Create a new JS object and use it as the prototype for the shadow global object.
118 WrapperTypeInfo* contextType = &V8DedicatedWorkerContext::info;
119 #if ENABLE(SHARED_WORKERS)
120 if (!m_workerContext->isDedicatedWorkerContext())
121 contextType = &V8SharedWorkerContext::info;
123 v8::Handle<v8::Function> workerContextConstructor = m_perContextData->constructorForType(contextType);
124 v8::Local<v8::Object> jsWorkerContext = V8ObjectConstructor::newInstance(workerContextConstructor);
125 if (jsWorkerContext.IsEmpty()) {
130 V8DOMWrapper::associateObjectWithWrapper(PassRefPtr<WorkerContext>(m_workerContext), contextType, jsWorkerContext, m_isolate, WrapperConfiguration::Dependent);
132 // Insert the object instance as the prototype of the shadow object.
133 v8::Handle<v8::Object> globalObject = v8::Handle<v8::Object>::Cast(m_context->Global()->GetPrototype());
134 globalObject->SetPrototype(jsWorkerContext);
139 ScriptValue WorkerScriptController::evaluate(const String& script, const String& fileName, const TextPosition& scriptStartPosition, WorkerContextExecutionState* state)
141 V8GCController::checkMemoryUsage();
143 v8::HandleScope handleScope;
145 if (!initializeContextIfNeeded())
146 return ScriptValue();
148 if (!m_disableEvalPending.isEmpty()) {
149 m_context->AllowCodeGenerationFromStrings(false);
150 m_context->SetErrorMessageForCodeGenerationFromStrings(v8String(m_disableEvalPending, m_context->GetIsolate()));
151 m_disableEvalPending = String();
154 v8::Isolate* isolate = m_context->GetIsolate();
155 v8::Context::Scope scope(m_context.get());
159 v8::Handle<v8::String> scriptString = v8String(script, isolate);
160 v8::Handle<v8::Script> compiledScript = ScriptSourceCode::compileScript(scriptString, fileName, scriptStartPosition, 0, isolate);
161 v8::Local<v8::Value> result = ScriptRunner::runCompiledScript(compiledScript, m_workerContext);
163 if (!block.CanContinue()) {
164 m_workerContext->script()->forbidExecution();
165 return ScriptValue();
168 if (block.HasCaught()) {
169 v8::Local<v8::Message> message = block.Message();
170 state->hadException = true;
171 state->errorMessage = toWebCoreString(message->Get());
172 state->lineNumber = message->GetLineNumber();
173 state->sourceURL = toWebCoreString(message->GetScriptResourceName());
174 if (m_workerContext->sanitizeScriptError(state->errorMessage, state->lineNumber, state->sourceURL))
175 state->exception = throwError(v8GeneralError, state->errorMessage.utf8().data(), isolate);
177 state->exception = ScriptValue(block.Exception());
181 state->hadException = false;
183 if (result.IsEmpty() || result->IsUndefined())
184 return ScriptValue();
186 return ScriptValue(result);
189 void WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, ScriptValue* exception)
191 if (isExecutionForbidden())
194 WorkerContextExecutionState state;
195 evaluate(sourceCode.source(), sourceCode.url().string(), sourceCode.startPosition(), &state);
196 if (state.hadException) {
198 *exception = state.exception;
200 m_workerContext->reportException(state.errorMessage, state.lineNumber, state.sourceURL, 0);
204 void WorkerScriptController::scheduleExecutionTermination()
206 // The mutex provides a memory barrier to ensure that once
207 // termination is scheduled, isExecutionTerminating will
208 // accurately reflect that state when called from another thread.
210 MutexLocker locker(m_scheduledTerminationMutex);
211 m_executionScheduledToTerminate = true;
213 v8::V8::TerminateExecution(m_isolate);
216 bool WorkerScriptController::isExecutionTerminating() const
218 // See comments in scheduleExecutionTermination regarding mutex usage.
219 MutexLocker locker(m_scheduledTerminationMutex);
220 return m_executionScheduledToTerminate;
223 void WorkerScriptController::forbidExecution()
225 ASSERT(m_workerContext->isContextThread());
226 m_executionForbidden = true;
229 bool WorkerScriptController::isExecutionForbidden() const
231 ASSERT(m_workerContext->isContextThread());
232 return m_executionForbidden;
235 void WorkerScriptController::disableEval(const String& errorMessage)
237 m_disableEvalPending = errorMessage;
240 void WorkerScriptController::setException(const ScriptValue& exception)
242 throwError(*exception.v8Value(), m_context->GetIsolate());
245 WorkerScriptController* WorkerScriptController::controllerForContext()
247 // Happens on frame destruction, check otherwise GetCurrent() will crash.
248 if (!v8::Context::InContext())
250 v8::Handle<v8::Context> context = v8::Context::GetCurrent();
251 v8::Handle<v8::Object> global = context->Global();
252 global = global->FindInstanceInPrototypeChain(V8WorkerContext::GetTemplate(context->GetIsolate()));
253 // Return 0 if the current executing context is not the worker context.
254 if (global.IsEmpty())
256 WorkerContext* workerContext = V8WorkerContext::toNative(global);
257 return workerContext->script();
260 } // namespace WebCore
262 #endif // ENABLE(WORKERS)