Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / WorkerScriptController.h
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
56     // Returns true if the evaluation completed with no uncaught exception.
57     bool evaluate(const ScriptSourceCode&, RefPtrWillBeRawPtr<ErrorEvent>* = 0);
58
59     // Prevents future JavaScript execution. See
60     // scheduleExecutionTermination, isExecutionForbidden.
61     void forbidExecution();
62
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();
72
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); }
80
81     // Used by Inspector agents:
82     ScriptState* scriptState() { return m_scriptState.get(); }
83
84     // Used by V8 bindings:
85     v8::Local<v8::Context> context() { return m_scriptState ? m_scriptState->context() : v8::Local<v8::Context>(); }
86
87 private:
88     class WorkerGlobalScopeExecutionState;
89
90     bool isContextInitialized() { return m_scriptState && !!m_scriptState->perContextData(); }
91
92     // Evaluate a script file in the current execution environment.
93     ScriptValue evaluate(const String& script, const String& fileName, const TextPosition& scriptStartPosition);
94
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;
103
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;
113 };
114
115 } // namespace blink
116
117 #endif // WorkerScriptController_h