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