• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009, 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30 
31 #include "config.h"
32 
33 #include "bindings/core/v8/WorkerScriptController.h"
34 
35 #include "bindings/core/v8/ScriptSourceCode.h"
36 #include "bindings/core/v8/ScriptValue.h"
37 #include "bindings/core/v8/V8DedicatedWorkerGlobalScope.h"
38 #include "bindings/core/v8/V8ErrorHandler.h"
39 #include "bindings/core/v8/V8GCController.h"
40 #include "bindings/core/v8/V8Initializer.h"
41 #include "bindings/core/v8/V8ObjectConstructor.h"
42 #include "bindings/core/v8/V8ScriptRunner.h"
43 #include "bindings/core/v8/V8SharedWorkerGlobalScope.h"
44 #include "bindings/core/v8/V8WorkerGlobalScope.h"
45 #include "bindings/core/v8/WrapperTypeInfo.h"
46 #include "bindings/modules/v8/V8ServiceWorkerGlobalScope.h"
47 #include "core/events/ErrorEvent.h"
48 #include "core/frame/DOMTimer.h"
49 #include "core/inspector/ScriptCallStack.h"
50 #include "core/workers/SharedWorkerGlobalScope.h"
51 #include "core/workers/WorkerGlobalScope.h"
52 #include "core/workers/WorkerObjectProxy.h"
53 #include "core/workers/WorkerThread.h"
54 #include "platform/heap/ThreadState.h"
55 #include "public/platform/Platform.h"
56 #include "public/platform/WebWorkerRunLoop.h"
57 #include <v8.h>
58 
59 namespace blink {
60 
61 class WorkerScriptController::WorkerGlobalScopeExecutionState FINAL {
62     STACK_ALLOCATED();
63 public:
WorkerGlobalScopeExecutionState(WorkerScriptController * controller)64     explicit WorkerGlobalScopeExecutionState(WorkerScriptController* controller)
65         : hadException(false)
66         , lineNumber(0)
67         , columnNumber(0)
68         , m_controller(controller)
69         , m_outerState(controller->m_globalScopeExecutionState)
70     {
71         m_controller->m_globalScopeExecutionState = this;
72     }
73 
~WorkerGlobalScopeExecutionState()74     ~WorkerGlobalScopeExecutionState()
75     {
76         m_controller->m_globalScopeExecutionState = m_outerState;
77     }
78 
trace(Visitor * visitor)79     void trace(Visitor* visitor)
80     {
81         visitor->trace(m_errorEventFromImportedScript);
82     }
83 
84     bool hadException;
85     String errorMessage;
86     int lineNumber;
87     int columnNumber;
88     String sourceURL;
89     ScriptValue exception;
90     RefPtrWillBeMember<ErrorEvent> m_errorEventFromImportedScript;
91 
92     // A WorkerGlobalScopeExecutionState context is stack allocated by
93     // WorkerScriptController::evaluate(), with the contoller using it
94     // during script evaluation. To handle nested evaluate() uses,
95     // WorkerGlobalScopeExecutionStates are chained together;
96     // |m_outerState| keeps a pointer to the context object one level out
97     // (or 0, if outermost.) Upon return from evaluate(), the
98     // WorkerScriptController's WorkerGlobalScopeExecutionState is popped
99     // and the previous one restored (see above dtor.)
100     //
101     // With Oilpan, |m_outerState| isn't traced. It'll be "up the stack"
102     // and its fields will be traced when scanning the stack.
103     WorkerScriptController* m_controller;
104     WorkerGlobalScopeExecutionState* m_outerState;
105 };
106 
WorkerScriptController(WorkerGlobalScope & workerGlobalScope)107 WorkerScriptController::WorkerScriptController(WorkerGlobalScope& workerGlobalScope)
108     : m_isolate(0)
109     , m_workerGlobalScope(workerGlobalScope)
110     , m_executionForbidden(false)
111     , m_executionScheduledToTerminate(false)
112     , m_globalScopeExecutionState(0)
113 {
114     m_isolate = V8PerIsolateData::initialize();
115     V8Initializer::initializeWorker(m_isolate);
116     m_world = DOMWrapperWorld::create(WorkerWorldId);
117     m_interruptor = adoptPtr(new V8IsolateInterruptor(m_isolate));
118     ThreadState::current()->addInterruptor(m_interruptor.get());
119 }
120 
121 // We need to postpone V8 Isolate destruction until the very end of
122 // worker thread finalization when all objects on the worker heap
123 // are destroyed.
124 class IsolateCleanupTask : public ThreadState::CleanupTask {
125 public:
create(v8::Isolate * isolate)126     static PassOwnPtr<IsolateCleanupTask> create(v8::Isolate* isolate)
127     {
128         return adoptPtr(new IsolateCleanupTask(isolate));
129     }
130 
postCleanup()131     virtual void postCleanup()
132     {
133         V8PerIsolateData::destroy(m_isolate);
134     }
135 
136 private:
IsolateCleanupTask(v8::Isolate * isolate)137     explicit IsolateCleanupTask(v8::Isolate* isolate) : m_isolate(isolate)  { }
138 
139     v8::Isolate* m_isolate;
140 };
141 
~WorkerScriptController()142 WorkerScriptController::~WorkerScriptController()
143 {
144     ThreadState::current()->removeInterruptor(m_interruptor.get());
145 
146     m_world->dispose();
147 
148     // The corresponding call to didStartWorkerRunLoop is in
149     // WorkerThread::initialize().
150     // See http://webkit.org/b/83104#c14 for why this is here.
151     blink::Platform::current()->didStopWorkerRunLoop(blink::WebWorkerRunLoop(m_workerGlobalScope.thread()));
152 
153     if (isContextInitialized())
154         m_scriptState->disposePerContextData();
155 
156     V8PerIsolateData::willBeDestroyed(m_isolate);
157 
158     ThreadState::current()->addCleanupTask(IsolateCleanupTask::create(m_isolate));
159 }
160 
initializeContextIfNeeded()161 bool WorkerScriptController::initializeContextIfNeeded()
162 {
163     v8::HandleScope handleScope(m_isolate);
164 
165     if (isContextInitialized())
166         return true;
167 
168     v8::Handle<v8::Context> context = v8::Context::New(m_isolate);
169     if (context.IsEmpty())
170         return false;
171 
172     m_scriptState = ScriptState::create(context, m_world);
173 
174     ScriptState::Scope scope(m_scriptState.get());
175 
176     // Set DebugId for the new context.
177     context->SetEmbedderData(0, v8AtomicString(m_isolate, "worker"));
178 
179     // Create a new JS object and use it as the prototype for the shadow global object.
180     const WrapperTypeInfo* wrapperTypeInfo = m_workerGlobalScope.wrapperTypeInfo();
181     v8::Handle<v8::Function> workerGlobalScopeConstructor = m_scriptState->perContextData()->constructorForType(wrapperTypeInfo);
182     v8::Local<v8::Object> jsWorkerGlobalScope = V8ObjectConstructor::newInstance(m_isolate, workerGlobalScopeConstructor);
183     if (jsWorkerGlobalScope.IsEmpty()) {
184         m_scriptState->disposePerContextData();
185         return false;
186     }
187 
188     V8DOMWrapper::associateObjectWithWrapperNonTemplate(&m_workerGlobalScope, wrapperTypeInfo, jsWorkerGlobalScope, m_isolate);
189 
190     // Insert the object instance as the prototype of the shadow object.
191     v8::Handle<v8::Object> globalObject = v8::Handle<v8::Object>::Cast(m_scriptState->context()->Global()->GetPrototype());
192     globalObject->SetPrototype(jsWorkerGlobalScope);
193 
194     return true;
195 }
196 
evaluate(const String & script,const String & fileName,const TextPosition & scriptStartPosition)197 ScriptValue WorkerScriptController::evaluate(const String& script, const String& fileName, const TextPosition& scriptStartPosition)
198 {
199     if (!initializeContextIfNeeded())
200         return ScriptValue();
201 
202     ScriptState::Scope scope(m_scriptState.get());
203 
204     if (!m_disableEvalPending.isEmpty()) {
205         m_scriptState->context()->AllowCodeGenerationFromStrings(false);
206         m_scriptState->context()->SetErrorMessageForCodeGenerationFromStrings(v8String(m_isolate, m_disableEvalPending));
207         m_disableEvalPending = String();
208     }
209 
210     v8::TryCatch block;
211 
212     v8::Handle<v8::String> scriptString = v8String(m_isolate, script);
213     v8::Handle<v8::Script> compiledScript = V8ScriptRunner::compileScript(scriptString, fileName, scriptStartPosition, 0, 0, m_isolate);
214     v8::Local<v8::Value> result = V8ScriptRunner::runCompiledScript(compiledScript, &m_workerGlobalScope, m_isolate);
215 
216     if (!block.CanContinue()) {
217         m_workerGlobalScope.script()->forbidExecution();
218         return ScriptValue();
219     }
220 
221     if (block.HasCaught()) {
222         v8::Local<v8::Message> message = block.Message();
223         m_globalScopeExecutionState->hadException = true;
224         m_globalScopeExecutionState->errorMessage = toCoreString(message->Get());
225         m_globalScopeExecutionState->lineNumber = message->GetLineNumber();
226         m_globalScopeExecutionState->columnNumber = message->GetStartColumn() + 1;
227         TOSTRING_DEFAULT(V8StringResource<>, sourceURL, message->GetScriptOrigin().ResourceName(), ScriptValue());
228         m_globalScopeExecutionState->sourceURL = sourceURL;
229         m_globalScopeExecutionState->exception = ScriptValue(m_scriptState.get(), block.Exception());
230         block.Reset();
231     } else {
232         m_globalScopeExecutionState->hadException = false;
233     }
234 
235     if (result.IsEmpty() || result->IsUndefined())
236         return ScriptValue();
237 
238     return ScriptValue(m_scriptState.get(), result);
239 }
240 
evaluate(const ScriptSourceCode & sourceCode,RefPtrWillBeRawPtr<ErrorEvent> * errorEvent)241 void WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, RefPtrWillBeRawPtr<ErrorEvent>* errorEvent)
242 {
243     if (isExecutionForbidden())
244         return;
245 
246     WorkerGlobalScopeExecutionState state(this);
247     evaluate(sourceCode.source(), sourceCode.url().string(), sourceCode.startPosition());
248     if (state.hadException) {
249         if (errorEvent) {
250             if (state.m_errorEventFromImportedScript) {
251                 // Propagate inner error event outwards.
252                 *errorEvent = state.m_errorEventFromImportedScript.release();
253                 return;
254             }
255             if (m_workerGlobalScope.shouldSanitizeScriptError(state.sourceURL, NotSharableCrossOrigin))
256                 *errorEvent = ErrorEvent::createSanitizedError(m_world.get());
257             else
258                 *errorEvent = ErrorEvent::create(state.errorMessage, state.sourceURL, state.lineNumber, state.columnNumber, m_world.get());
259             V8ErrorHandler::storeExceptionOnErrorEventWrapper(errorEvent->get(), state.exception.v8Value(), m_scriptState->context()->Global(), m_isolate);
260         } else {
261             ASSERT(!m_workerGlobalScope.shouldSanitizeScriptError(state.sourceURL, NotSharableCrossOrigin));
262             RefPtrWillBeRawPtr<ErrorEvent> event = nullptr;
263             if (state.m_errorEventFromImportedScript)
264                 event = state.m_errorEventFromImportedScript.release();
265             else
266                 event = ErrorEvent::create(state.errorMessage, state.sourceURL, state.lineNumber, state.columnNumber, m_world.get());
267             m_workerGlobalScope.reportException(event, 0, nullptr, NotSharableCrossOrigin);
268         }
269     }
270 }
271 
scheduleExecutionTermination()272 void WorkerScriptController::scheduleExecutionTermination()
273 {
274     // The mutex provides a memory barrier to ensure that once
275     // termination is scheduled, isExecutionTerminating will
276     // accurately reflect that state when called from another thread.
277     {
278         MutexLocker locker(m_scheduledTerminationMutex);
279         m_executionScheduledToTerminate = true;
280     }
281     v8::V8::TerminateExecution(m_isolate);
282 }
283 
isExecutionTerminating() const284 bool WorkerScriptController::isExecutionTerminating() const
285 {
286     // See comments in scheduleExecutionTermination regarding mutex usage.
287     MutexLocker locker(m_scheduledTerminationMutex);
288     return m_executionScheduledToTerminate;
289 }
290 
forbidExecution()291 void WorkerScriptController::forbidExecution()
292 {
293     ASSERT(m_workerGlobalScope.isContextThread());
294     m_executionForbidden = true;
295 }
296 
isExecutionForbidden() const297 bool WorkerScriptController::isExecutionForbidden() const
298 {
299     ASSERT(m_workerGlobalScope.isContextThread());
300     return m_executionForbidden;
301 }
302 
disableEval(const String & errorMessage)303 void WorkerScriptController::disableEval(const String& errorMessage)
304 {
305     m_disableEvalPending = errorMessage;
306 }
307 
rethrowExceptionFromImportedScript(PassRefPtrWillBeRawPtr<ErrorEvent> errorEvent,ExceptionState & exceptionState)308 void WorkerScriptController::rethrowExceptionFromImportedScript(PassRefPtrWillBeRawPtr<ErrorEvent> errorEvent, ExceptionState& exceptionState)
309 {
310     const String& errorMessage = errorEvent->message();
311     if (m_globalScopeExecutionState)
312         m_globalScopeExecutionState->m_errorEventFromImportedScript = errorEvent;
313     exceptionState.rethrowV8Exception(V8ThrowException::createGeneralError(errorMessage, m_isolate));
314 }
315 
316 } // namespace blink
317