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 #ifndef WorkerScriptController_h 32 #define WorkerScriptController_h 33 34 #include "bindings/v8/ScriptValue.h" 35 #include "bindings/v8/V8Binding.h" 36 #include "core/events/ErrorEvent.h" 37 #include "wtf/OwnPtr.h" 38 #include "wtf/ThreadingPrimitives.h" 39 #include "wtf/text/TextPosition.h" 40 #include <v8.h> 41 42 namespace WebCore { 43 44 class ScriptSourceCode; 45 class ScriptValue; 46 class WorkerGlobalScope; 47 48 struct WorkerGlobalScopeExecutionState { 49 WorkerGlobalScopeExecutionState() 50 : hadException(false) 51 , lineNumber(0) 52 , columnNumber(0) 53 { 54 } 55 56 bool hadException; 57 String errorMessage; 58 int lineNumber; 59 int columnNumber; 60 String sourceURL; 61 ScriptValue exception; 62 }; 63 64 class WorkerScriptController { 65 public: 66 explicit WorkerScriptController(WorkerGlobalScope&); 67 ~WorkerScriptController(); 68 69 WorkerGlobalScope& workerGlobalScope() { return m_workerGlobalScope; } 70 71 bool initializeContextIfNeeded(); 72 73 void evaluate(const ScriptSourceCode&, RefPtrWillBeRawPtr<ErrorEvent>* = 0); 74 75 void rethrowExceptionFromImportedScript(PassRefPtrWillBeRawPtr<ErrorEvent>); 76 77 // Async request to terminate a future JS execution. Eventually causes termination 78 // exception raised during JS execution, if the worker thread happens to run JS. 79 // After JS execution was terminated in this way, the Worker thread has to use 80 // forbidExecution()/isExecutionForbidden() to guard against reentry into JS. 81 // Can be called from any thread. 82 void scheduleExecutionTermination(); 83 bool isExecutionTerminating() const; 84 85 // Called on Worker thread when JS exits with termination exception caused by forbidExecution() request, 86 // or by Worker thread termination code to prevent future entry into JS. 87 void forbidExecution(); 88 bool isExecutionForbidden() const; 89 90 void disableEval(const String&); 91 92 // Evaluate a script file in the current execution environment. 93 ScriptValue evaluate(const String& script, const String& fileName, const TextPosition& scriptStartPosition, WorkerGlobalScopeExecutionState*); 94 95 v8::Isolate* isolate() const { return m_isolate; } 96 DOMWrapperWorld& world() const { return *m_world; } 97 ScriptState* scriptState() { return m_scriptState.get(); } 98 v8::Local<v8::Context> context() { return m_scriptState ? m_scriptState->context() : v8::Local<v8::Context>(); } 99 bool isContextInitialized() { return m_scriptState && !!m_scriptState->perContextData(); } 100 101 // Send a notification about current thread is going to be idle. 102 // Returns true if the embedder should stop calling idleNotification 103 // until real work has been done. 104 bool idleNotification() { return v8::V8::IdleNotification(); } 105 106 107 private: 108 v8::Isolate* m_isolate; 109 WorkerGlobalScope& m_workerGlobalScope; 110 RefPtr<ScriptState> m_scriptState; 111 RefPtr<DOMWrapperWorld> m_world; 112 String m_disableEvalPending; 113 bool m_executionForbidden; 114 bool m_executionScheduledToTerminate; 115 mutable Mutex m_scheduledTerminationMutex; 116 RefPtrWillBePersistent<ErrorEvent> m_errorEventFromImportedScript; 117 OwnPtr<V8IsolateInterruptor> m_interruptor; 118 }; 119 120 } // namespace WebCore 121 122 #endif // WorkerScriptController_h 123