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/core/v8/ScriptValue.h"
35 #include "bindings/core/v8/V8Binding.h"
36 #include "wtf/OwnPtr.h"
37 #include "wtf/ThreadingPrimitives.h"
38 #include "wtf/text/TextPosition.h"
45 class ScriptSourceCode;
46 class WorkerGlobalScope;
48 class WorkerScriptController {
50 explicit WorkerScriptController(WorkerGlobalScope&);
51 ~WorkerScriptController();
53 bool isExecutionForbidden() const;
54 bool isExecutionTerminating() const;
56 // Returns true if the evaluation completed with no uncaught exception.
57 bool evaluate(const ScriptSourceCode&, RefPtrWillBeRawPtr<ErrorEvent>* = 0);
59 // Prevents future JavaScript execution. See
60 // scheduleExecutionTermination, isExecutionForbidden.
61 void forbidExecution();
63 // Used by WorkerThread:
64 bool initializeContextIfNeeded();
65 // Async request to terminate future JavaScript execution on the
66 // worker thread. JavaScript evaluation exits with a
67 // non-continuable exception and WorkerScriptController calls
68 // forbidExecution to prevent further JavaScript execution. Use
69 // forbidExecution()/isExecutionForbidden() to guard against
70 // reentry into JavaScript.
71 void scheduleExecutionTermination();
73 // Used by WorkerGlobalScope:
74 void rethrowExceptionFromImportedScript(PassRefPtrWillBeRawPtr<ErrorEvent>, ExceptionState&);
75 void disableEval(const String&);
76 // Send a notification about current thread is going to be idle.
77 // Returns true if the embedder should stop calling idleNotification
78 // until real work has been done.
79 bool idleNotification() { return m_isolate->IdleNotification(1000); }
81 // Used by Inspector agents:
82 ScriptState* scriptState() { return m_scriptState.get(); }
84 // Used by V8 bindings:
85 v8::Local<v8::Context> context() { return m_scriptState ? m_scriptState->context() : v8::Local<v8::Context>(); }
88 class WorkerGlobalScopeExecutionState;
90 bool isContextInitialized() { return m_scriptState && !!m_scriptState->perContextData(); }
92 // Evaluate a script file in the current execution environment.
93 ScriptValue evaluate(const String& script, const String& fileName, const TextPosition& scriptStartPosition);
95 v8::Isolate* m_isolate;
96 WorkerGlobalScope& m_workerGlobalScope;
97 RefPtr<ScriptState> m_scriptState;
98 RefPtr<DOMWrapperWorld> m_world;
99 String m_disableEvalPending;
100 bool m_executionForbidden;
101 bool m_executionScheduledToTerminate;
102 mutable Mutex m_scheduledTerminationMutex;
104 // |m_globalScopeExecutionState| refers to a stack object
105 // that evaluate() allocates; evaluate() ensuring that the
106 // pointer reference to it is removed upon returning. Hence
107 // kept as a bare pointer here, and not a Persistent with
108 // Oilpan enabled; stack scanning will visit the object and
109 // trace its on-heap fields.
110 GC_PLUGIN_IGNORE("394615")
111 WorkerGlobalScopeExecutionState* m_globalScopeExecutionState;
112 OwnPtr<V8IsolateInterruptor> m_interruptor;
117 #endif // WorkerScriptController_h