1 | /* |
2 | * Copyright (C) 2008-2017 Apple Inc. All Rights Reserved. |
3 | * Copyright (C) 2012 Google Inc. All Rights Reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | |
27 | #pragma once |
28 | |
29 | #include <JavaScriptCore/Debugger.h> |
30 | #include <JavaScriptCore/JSRunLoopTimer.h> |
31 | #include <JavaScriptCore/Strong.h> |
32 | #include <wtf/Forward.h> |
33 | #include <wtf/Lock.h> |
34 | #include <wtf/NakedPtr.h> |
35 | |
36 | namespace JSC { |
37 | class VM; |
38 | } |
39 | |
40 | namespace WebCore { |
41 | |
42 | class JSWorkerGlobalScope; |
43 | class ScriptSourceCode; |
44 | class WorkerConsoleClient; |
45 | class WorkerGlobalScope; |
46 | |
47 | class WorkerScriptController { |
48 | WTF_MAKE_NONCOPYABLE(WorkerScriptController); WTF_MAKE_FAST_ALLOCATED; |
49 | public: |
50 | WorkerScriptController(WorkerGlobalScope*); |
51 | ~WorkerScriptController(); |
52 | |
53 | JSWorkerGlobalScope* workerGlobalScopeWrapper() |
54 | { |
55 | initScriptIfNeeded(); |
56 | return m_workerGlobalScopeWrapper.get(); |
57 | } |
58 | |
59 | void evaluate(const ScriptSourceCode&, String* returnedExceptionMessage = nullptr); |
60 | void evaluate(const ScriptSourceCode&, NakedPtr<JSC::Exception>& returnedException, String* returnedExceptionMessage = nullptr); |
61 | |
62 | void setException(JSC::Exception*); |
63 | |
64 | // Async request to terminate a JS run execution. Eventually causes termination |
65 | // exception raised during JS execution, if the worker thread happens to run JS. |
66 | // After JS execution was terminated in this way, the Worker thread has to use |
67 | // forbidExecution()/isExecutionForbidden() to guard against reentry into JS. |
68 | // Can be called from any thread. |
69 | void scheduleExecutionTermination(); |
70 | bool isTerminatingExecution() const; |
71 | |
72 | // Called on Worker thread when JS exits with termination exception caused by forbidExecution() request, |
73 | // or by Worker thread termination code to prevent future entry into JS. |
74 | void forbidExecution(); |
75 | bool isExecutionForbidden() const; |
76 | |
77 | void disableEval(const String& errorMessage); |
78 | void disableWebAssembly(const String& errorMessage); |
79 | |
80 | JSC::VM& vm() { return *m_vm; } |
81 | |
82 | void releaseHeapAccess(); |
83 | void acquireHeapAccess(); |
84 | |
85 | void addTimerSetNotification(JSC::JSRunLoopTimer::TimerNotificationCallback); |
86 | void removeTimerSetNotification(JSC::JSRunLoopTimer::TimerNotificationCallback); |
87 | |
88 | void attachDebugger(JSC::Debugger*); |
89 | void detachDebugger(JSC::Debugger*); |
90 | |
91 | private: |
92 | void initScriptIfNeeded() |
93 | { |
94 | if (!m_workerGlobalScopeWrapper) |
95 | initScript(); |
96 | } |
97 | WEBCORE_EXPORT void initScript(); |
98 | |
99 | RefPtr<JSC::VM> m_vm; |
100 | WorkerGlobalScope* m_workerGlobalScope; |
101 | JSC::Strong<JSWorkerGlobalScope> m_workerGlobalScopeWrapper; |
102 | std::unique_ptr<WorkerConsoleClient> m_consoleClient; |
103 | bool m_executionForbidden { false }; |
104 | bool m_isTerminatingExecution { false }; |
105 | mutable Lock m_scheduledTerminationMutex; |
106 | }; |
107 | |
108 | } // namespace WebCore |
109 | |