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.
31 #ifndef WorkerScriptController_h
32 #define WorkerScriptController_h
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"
44 class ScriptSourceCode;
46 class WorkerGlobalScope;
48 struct WorkerGlobalScopeExecutionState {
49 WorkerGlobalScopeExecutionState()
61 ScriptValue exception;
64 class WorkerScriptController {
66 explicit WorkerScriptController(WorkerGlobalScope&);
67 ~WorkerScriptController();
69 WorkerGlobalScope& workerGlobalScope() { return m_workerGlobalScope; }
71 bool initializeContextIfNeeded();
73 void evaluate(const ScriptSourceCode&, RefPtrWillBeRawPtr<ErrorEvent>* = 0);
75 void rethrowExceptionFromImportedScript(PassRefPtrWillBeRawPtr<ErrorEvent>);
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;
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;
90 void disableEval(const String&);
92 // Evaluate a script file in the current execution environment.
93 ScriptValue evaluate(const String& script, const String& fileName, const TextPosition& scriptStartPosition, WorkerGlobalScopeExecutionState*);
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(); }
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(); }
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;
120 } // namespace WebCore
122 #endif // WorkerScriptController_h