OLD | NEW |
1 /* | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * | 3 // found in the LICENSE file. |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions | |
6 * are met: | |
7 * 1. Redistributions of source code must retain the above copyright | |
8 * notice, this list of conditions and the following disclaimer. | |
9 * 2. Redistributions in binary form must reproduce the above copyright | |
10 * notice, this list of conditions and the following disclaimer in the | |
11 * documentation and/or other materials provided with the distribution. | |
12 * | |
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 * | |
25 */ | |
26 | 4 |
27 #ifndef InProcessWorkerMessagingProxy_h | 5 #ifndef ThreadedMessagingProxyBase_h |
28 #define InProcessWorkerMessagingProxy_h | 6 #define ThreadedMessagingProxyBase_h |
29 | 7 |
30 #include "core/CoreExport.h" | 8 #include "core/CoreExport.h" |
31 #include "core/dom/ExecutionContext.h" | |
32 #include "core/dom/MessagePort.h" | |
33 #include "core/inspector/ConsoleTypes.h" | 9 #include "core/inspector/ConsoleTypes.h" |
34 #include "core/workers/WorkerLoaderProxy.h" | 10 #include "core/workers/WorkerLoaderProxy.h" |
35 #include "platform/heap/Handle.h" | |
36 #include "wtf/Forward.h" | 11 #include "wtf/Forward.h" |
37 #include "wtf/Noncopyable.h" | |
38 #include "wtf/PassRefPtr.h" | |
39 #include "wtf/RefPtr.h" | |
40 #include "wtf/Vector.h" | |
41 #include <memory> | |
42 | 12 |
43 namespace blink { | 13 namespace blink { |
44 | 14 |
45 class ExecutionContext; | 15 class ExecutionContext; |
46 class InProcessWorkerBase; | |
47 class InProcessWorkerObjectProxy; | |
48 class ParentFrameTaskRunners; | 16 class ParentFrameTaskRunners; |
49 class WorkerClients; | |
50 class WorkerInspectorProxy; | 17 class WorkerInspectorProxy; |
| 18 class WorkerLoaderProxy; |
51 class WorkerThread; | 19 class WorkerThread; |
| 20 class WorkerThreadStartupData; |
52 | 21 |
53 // TODO(nhiroki): "MessagingProxy" is not well-defined term among worker | 22 class CORE_EXPORT ThreadedMessagingProxyBase : private WorkerLoaderProxyProvider
{ |
54 // components. Probably we should rename this to something more suitable. | |
55 // (http://crbug.com/603785) | |
56 class CORE_EXPORT InProcessWorkerMessagingProxy : private WorkerLoaderProxyProvi
der { | |
57 WTF_MAKE_NONCOPYABLE(InProcessWorkerMessagingProxy); | |
58 public: | 23 public: |
59 // These methods should only be used on the parent context thread. | 24 void terminateGlobalScope(); |
60 void startWorkerGlobalScope(const KURL& scriptURL, const String& userAgent,
const String& sourceCode); | |
61 void terminateWorkerGlobalScope(); | |
62 void postMessageToWorkerGlobalScope(PassRefPtr<SerializedScriptValue>, std::
unique_ptr<MessagePortChannelArray>); | |
63 bool hasPendingActivity() const; | |
64 void workerObjectDestroyed(); | |
65 | 25 |
66 // These methods come from worker context thread via | 26 virtual void workerThreadCreated(); |
67 // InProcessWorkerObjectProxy and are called on the parent context thread. | 27 |
68 void postMessageToWorkerObject(PassRefPtr<SerializedScriptValue>, std::uniqu
e_ptr<MessagePortChannelArray>); | 28 // This method should be called in the destructor of the object which |
69 void dispatchErrorEvent(const String& errorMessage, std::unique_ptr<SourceLo
cation>, int exceptionId); | 29 // initially created it. This object could either be a Worker or a Worklet. |
| 30 virtual void parentObjectDestroyed(); |
| 31 |
70 void reportConsoleMessage(MessageSource, MessageLevel, const String& message
, std::unique_ptr<SourceLocation>); | 32 void reportConsoleMessage(MessageSource, MessageLevel, const String& message
, std::unique_ptr<SourceLocation>); |
71 void postMessageToPageInspector(const String&); | 33 void postMessageToPageInspector(const String&); |
72 | 34 |
73 // 'virtual' for testing. | 35 // 'virtual' for testing. |
74 virtual void confirmMessageFromWorkerObject(); | |
75 virtual void pendingActivityFinished(); | |
76 virtual void workerThreadTerminated(); | 36 virtual void workerThreadTerminated(); |
77 | 37 |
78 void workerThreadCreated(); | |
79 | |
80 ExecutionContext* getExecutionContext() const { return m_executionContext.ge
t(); } | 38 ExecutionContext* getExecutionContext() const { return m_executionContext.ge
t(); } |
81 | 39 |
82 ParentFrameTaskRunners* getParentFrameTaskRunners() { return m_parentFrameTa
skRunners.get(); } | 40 ParentFrameTaskRunners* getParentFrameTaskRunners() { return m_parentFrameTa
skRunners.get(); } |
83 | 41 |
84 // Number of live messaging proxies, used by leak detection. | 42 // Number of live messaging proxies, used by leak detection. |
85 static int proxyCount(); | 43 static int proxyCount(); |
86 | 44 |
87 protected: | 45 protected: |
88 InProcessWorkerMessagingProxy(InProcessWorkerBase*, WorkerClients*); | 46 ThreadedMessagingProxyBase(ExecutionContext*); |
89 ~InProcessWorkerMessagingProxy() override; | 47 ~ThreadedMessagingProxyBase() override; |
90 | 48 |
| 49 void initializeWorkerThread(std::unique_ptr<WorkerThreadStartupData>); |
91 virtual std::unique_ptr<WorkerThread> createWorkerThread(double originTime)
= 0; | 50 virtual std::unique_ptr<WorkerThread> createWorkerThread(double originTime)
= 0; |
92 | 51 |
| 52 WorkerThread* workerThread() const { return m_workerThread.get(); } |
| 53 |
| 54 bool askedToTerminate() const { return m_askedToTerminate; } |
| 55 |
93 PassRefPtr<WorkerLoaderProxy> loaderProxy() { return m_loaderProxy; } | 56 PassRefPtr<WorkerLoaderProxy> loaderProxy() { return m_loaderProxy; } |
94 InProcessWorkerObjectProxy& workerObjectProxy() { return *m_workerObjectProx
y.get(); } | 57 WorkerInspectorProxy* workerInspectorProxy() { return m_workerInspectorProxy
.get(); } |
95 | 58 |
96 private: | 59 // Returns true if this is called on the parent context thread. |
97 friend class InProcessWorkerMessagingProxyForTest; | 60 bool isParentContextThread() const; |
98 InProcessWorkerMessagingProxy(ExecutionContext*, InProcessWorkerBase*, Worke
rClients*); | |
99 | |
100 void workerObjectDestroyedInternal(); | |
101 | 61 |
102 // WorkerLoaderProxyProvider | 62 // WorkerLoaderProxyProvider |
103 // These methods are called on different threads to schedule loading | 63 // These methods are called on different threads to schedule loading |
104 // requests and to send callbacks back to WorkerGlobalScope. | 64 // requests and to send callbacks back to WorkerGlobalScope. |
105 void postTaskToLoader(const WebTraceLocation&, std::unique_ptr<ExecutionCont
extTask>) override; | 65 void postTaskToLoader(const WebTraceLocation&, std::unique_ptr<ExecutionCont
extTask>) override; |
106 void postTaskToWorkerGlobalScope(const WebTraceLocation&, std::unique_ptr<Ex
ecutionContextTask>) override; | 66 void postTaskToWorkerGlobalScope(const WebTraceLocation&, std::unique_ptr<Ex
ecutionContextTask>) override; |
107 | 67 private: |
108 // Returns true if this is called on the parent context thread. | 68 friend class InProcessWorkerMessagingProxyForTest; |
109 bool isParentContextThread() const; | 69 void parentObjectDestroyedInternal(); |
110 | 70 |
111 Persistent<ExecutionContext> m_executionContext; | 71 Persistent<ExecutionContext> m_executionContext; |
112 std::unique_ptr<InProcessWorkerObjectProxy> m_workerObjectProxy; | 72 Persistent<WorkerInspectorProxy> m_workerInspectorProxy; |
113 WeakPersistent<InProcessWorkerBase> m_workerObject; | 73 Persistent<ParentFrameTaskRunners> m_parentFrameTaskRunners; |
114 bool m_mayBeDestroyed; | 74 |
115 std::unique_ptr<WorkerThread> m_workerThread; | 75 std::unique_ptr<WorkerThread> m_workerThread; |
116 | 76 |
117 // Unconfirmed messages from the parent context thread to the worker thread. | 77 RefPtr<WorkerLoaderProxy> m_loaderProxy; |
118 unsigned m_unconfirmedMessageCount; | |
119 | 78 |
120 bool m_workerGlobalScopeMayHavePendingActivity; | 79 bool m_mayBeDestroyed; |
121 bool m_askedToTerminate; | 80 bool m_askedToTerminate; |
122 | |
123 // Tasks are queued here until there's a thread object created. | |
124 Vector<std::unique_ptr<ExecutionContextTask>> m_queuedEarlyTasks; | |
125 | |
126 Persistent<WorkerInspectorProxy> m_workerInspectorProxy; | |
127 | |
128 Persistent<WorkerClients> m_workerClients; | |
129 | |
130 Persistent<ParentFrameTaskRunners> m_parentFrameTaskRunners; | |
131 | |
132 RefPtr<WorkerLoaderProxy> m_loaderProxy; | |
133 }; | 81 }; |
134 | 82 |
135 } // namespace blink | 83 } // namespace blink |
136 | 84 |
137 #endif // InProcessWorkerMessagingProxy_h | 85 #endif // ThreadedMessagingProxyBase_h |
OLD | NEW |