OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 13 matching lines...) Expand all Loading... | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #ifndef WorkerThreadableLoader_h | 31 #ifndef WorkerThreadableLoader_h |
32 #define WorkerThreadableLoader_h | 32 #define WorkerThreadableLoader_h |
33 | 33 |
34 #include "core/dom/ExecutionContextTask.h" | |
34 #include "core/loader/ThreadableLoader.h" | 35 #include "core/loader/ThreadableLoader.h" |
35 #include "core/loader/ThreadableLoaderClient.h" | 36 #include "core/loader/ThreadableLoaderClient.h" |
36 #include "core/loader/ThreadableLoaderClientWrapper.h" | 37 #include "core/loader/ThreadableLoaderClientWrapper.h" |
38 #include "core/workers/WorkerThread.h" | |
39 #include "core/workers/WorkerThreadLifecycleObserver.h" | |
40 #include "platform/WaitableEvent.h" | |
37 #include "platform/heap/Handle.h" | 41 #include "platform/heap/Handle.h" |
38 #include "platform/weborigin/Referrer.h" | |
39 #include "wtf/Functional.h" | |
40 #include "wtf/PassRefPtr.h" | 42 #include "wtf/PassRefPtr.h" |
41 #include "wtf/PtrUtil.h" | 43 #include "wtf/PtrUtil.h" |
42 #include "wtf/RefPtr.h" | 44 #include "wtf/RefPtr.h" |
43 #include "wtf/Threading.h" | 45 #include "wtf/Threading.h" |
44 #include "wtf/ThreadingPrimitives.h" | |
45 #include "wtf/Vector.h" | 46 #include "wtf/Vector.h" |
46 #include "wtf/text/WTFString.h" | 47 #include "wtf/text/WTFString.h" |
47 #include <memory> | 48 #include <memory> |
48 | 49 |
49 namespace blink { | 50 namespace blink { |
50 | 51 |
51 class ExecutionContextTask; | |
52 class ResourceError; | 52 class ResourceError; |
53 class ResourceRequest; | 53 class ResourceRequest; |
54 class ResourceResponse; | 54 class ResourceResponse; |
55 class WaitableEvent; | |
56 class WorkerGlobalScope; | 55 class WorkerGlobalScope; |
57 class WorkerLoaderProxy; | 56 class WorkerLoaderProxy; |
58 struct CrossThreadResourceRequestData; | 57 struct CrossThreadResourceRequestData; |
59 | 58 |
60 class WorkerThreadableLoader final : public ThreadableLoader { | 59 class WorkerThreadableLoader final : public ThreadableLoader { |
61 USING_FAST_MALLOC(WorkerThreadableLoader); | 60 USING_FAST_MALLOC(WorkerThreadableLoader); |
62 public: | 61 public: |
63 static void loadResourceSynchronously(WorkerGlobalScope&, const ResourceRequ est&, ThreadableLoaderClient&, const ThreadableLoaderOptions&, const ResourceLoa derOptions&); | 62 static void loadResourceSynchronously(WorkerGlobalScope&, const ResourceRequ est&, ThreadableLoaderClient&, const ThreadableLoaderOptions&, const ResourceLoa derOptions&); |
64 static std::unique_ptr<WorkerThreadableLoader> create(WorkerGlobalScope& wor kerGlobalScope, ThreadableLoaderClient* client, const ThreadableLoaderOptions& o ptions, const ResourceLoaderOptions& resourceLoaderOptions) | 63 static std::unique_ptr<WorkerThreadableLoader> create(WorkerGlobalScope& wor kerGlobalScope, ThreadableLoaderClient* client, const ThreadableLoaderOptions& o ptions, const ResourceLoaderOptions& resourceLoaderOptions) |
65 { | 64 { |
66 return wrapUnique(new WorkerThreadableLoader(workerGlobalScope, client, options, resourceLoaderOptions, LoadAsynchronously)); | 65 return wrapUnique(new WorkerThreadableLoader(workerGlobalScope, client, options, resourceLoaderOptions, LoadAsynchronously)); |
67 } | 66 } |
68 | 67 |
69 ~WorkerThreadableLoader() override; | 68 ~WorkerThreadableLoader() override; |
70 | 69 |
71 void start(const ResourceRequest&) override; | 70 void start(const ResourceRequest&) override; |
72 | |
73 void overrideTimeout(unsigned long timeout) override; | 71 void overrideTimeout(unsigned long timeout) override; |
74 | |
75 void cancel() override; | 72 void cancel() override; |
76 | 73 |
77 private: | 74 private: |
78 enum BlockingBehavior { | 75 enum BlockingBehavior { |
79 LoadSynchronously, | 76 LoadSynchronously, |
80 LoadAsynchronously | 77 LoadAsynchronously |
81 }; | 78 }; |
82 | 79 |
83 // Creates a loader on the main thread and bridges communication between | 80 // A TaskForwarder forwards an ExecutionContextTask to the worker thread. |
84 // the main thread and the worker context's thread where WorkerThreadableLoa der runs. | 81 class TaskForwarder : public GarbageCollectedFinalized<TaskForwarder> { |
85 // | |
86 // Regarding the bridge and lifetimes of items used in callbacks, there are a few cases: | |
87 // | |
88 // all cases. All tasks posted from the worker context's thread are ok becau se | |
89 // the last task posted always is "mainThreadDestroy", so MainThreadBridg e is | |
90 // around for all tasks that use it on the main thread. | |
91 // | |
92 // case 1. worker.terminate is called. | |
93 // In this case, no more tasks are posted from the worker object's thread to the worker | |
94 // context's thread -- WorkerGlobalScopeProxy implementation enforces thi s. | |
95 // | |
96 // case 2. xhr gets aborted and the worker context continues running. | |
97 // The ThreadableLoaderClientWrapper has the underlying client cleared, s o no more calls | |
98 // go through it. All tasks posted from the worker object's thread to th e worker context's | |
99 // thread do "ThreadableLoaderClientWrapper::ref" (automatically inside o f the cross thread copy | |
100 // done in createCrossThreadTask), so the ThreadableLoaderClientWrapper i nstance is there until all | |
101 // tasks are executed. | |
102 class MainThreadBridgeBase : public ThreadableLoaderClient { | |
103 public: | 82 public: |
104 // All executed on the worker context's thread. | 83 virtual ~TaskForwarder() {} |
105 MainThreadBridgeBase(ThreadableLoaderClientWrapper*, PassRefPtr<WorkerLo aderProxy>); | 84 virtual void forwardTask(std::unique_ptr<ExecutionContextTask>) = 0; |
106 virtual void start(const ResourceRequest&, const WorkerGlobalScope&) = 0 ; | 85 virtual void forwardTaskWithDoneSignal(std::unique_ptr<ExecutionContextT ask>) = 0; |
86 virtual void abort() = 0; | |
87 | |
88 DEFINE_INLINE_VIRTUAL_TRACE() {} | |
89 }; | |
90 | |
91 class AsyncTaskForwarder final : public TaskForwarder { | |
92 public: | |
93 explicit AsyncTaskForwarder(PassRefPtr<WorkerLoaderProxy>); | |
94 ~AsyncTaskForwarder() override; | |
95 | |
96 void forwardTask(std::unique_ptr<ExecutionContextTask>) override; | |
97 void forwardTaskWithDoneSignal(std::unique_ptr<ExecutionContextTask>) ov erride; | |
98 void abort() override; | |
99 | |
100 private: | |
101 RefPtr<WorkerLoaderProxy> m_loaderProxy; | |
102 }; | |
103 | |
104 // All observing functions must be called after |wait()| returns, and all | |
105 // setting functions must be called before |signal()| is called. | |
106 class WaitableEventWithTasks final : public ThreadSafeRefCounted<WaitableEve ntWithTasks> { | |
107 public: | |
108 static PassRefPtr<WaitableEventWithTasks> create() { return adoptRef(new WaitableEventWithTasks); } | |
109 ~WaitableEventWithTasks(); | |
110 | |
111 void signal(); | |
112 void wait(); | |
113 | |
114 // Observing functions | |
115 bool isAborted() const { return m_isAborted; } | |
116 Vector<std::unique_ptr<ExecutionContextTask>> take(); | |
117 | |
118 // Setting functions | |
119 void append(std::unique_ptr<ExecutionContextTask>); | |
120 void setIsAborted() { m_isAborted = true; } | |
121 | |
122 private: | |
123 WaitableEventWithTasks() {} | |
124 | |
125 WaitableEvent m_event; | |
126 Vector<std::unique_ptr<ExecutionContextTask>> m_tasks; | |
127 bool m_isAborted = false; | |
128 }; | |
129 | |
130 class SyncTaskForwarder final : public TaskForwarder { | |
131 public: | |
132 explicit SyncTaskForwarder(PassRefPtr<WaitableEventWithTasks>); | |
133 ~SyncTaskForwarder() override; | |
134 | |
135 void forwardTask(std::unique_ptr<ExecutionContextTask>) override; | |
136 void forwardTaskWithDoneSignal(std::unique_ptr<ExecutionContextTask>) ov erride; | |
137 void abort() override; | |
138 | |
139 private: | |
140 RefPtr<WaitableEventWithTasks> m_eventWithTasks; | |
141 }; | |
142 | |
143 class Peer; | |
144 // A Bridge instance lives in the worker thread and asks loading tasks the | |
145 // associated Peer object which lives in the main thread. | |
146 class Bridge final : public GarbageCollectedFinalized<Bridge> { | |
147 public: | |
148 Bridge(ThreadableLoaderClientWrapper*, PassRefPtr<WorkerLoaderProxy>, co nst ThreadableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior); | |
149 ~Bridge(); | |
150 | |
151 void start(const ResourceRequest&, const WorkerGlobalScope&); | |
107 void overrideTimeout(unsigned long timeoutMilliseconds); | 152 void overrideTimeout(unsigned long timeoutMilliseconds); |
108 void cancel(); | 153 void cancel(); |
109 void destroy(); | 154 void destroy(); |
110 | 155 |
111 // All executed on the main thread. | 156 void didStart(Peer*); |
112 void didSendData(unsigned long long bytesSent, unsigned long long totalB ytesToBeSent) final; | |
113 void didReceiveResponse(unsigned long identifier, const ResourceResponse &, std::unique_ptr<WebDataConsumerHandle>) final; | |
114 void didReceiveData(const char*, unsigned dataLength) final; | |
115 void didDownloadData(int dataLength) final; | |
116 void didReceiveCachedMetadata(const char*, int dataLength) final; | |
117 void didFinishLoading(unsigned long identifier, double finishTime) final ; | |
118 void didFail(const ResourceError&) final; | |
119 void didFailAccessControlCheck(const ResourceError&) final; | |
120 void didFailRedirectCheck() final; | |
121 void didReceiveResourceTiming(const ResourceTimingInfo&) final; | |
122 | 157 |
123 protected: | 158 // These getter functions are thread safe. |
124 ~MainThreadBridgeBase() override; | 159 ThreadableLoaderClientWrapper* clientWrapper() { return m_clientWrapper. get(); } |
160 const ThreadableLoaderOptions& threadableLoaderOptions() const { return m_threadableLoaderOptions; } | |
161 const ResourceLoaderOptions& resourceLoaderOptions() const { return m_re sourceLoaderOptions; } | |
125 | 162 |
126 // Posts a task to the main thread to run mainThreadCreateLoader(). | 163 DECLARE_VIRTUAL_TRACE(); |
127 void createLoaderInMainThread(const ThreadableLoaderOptions&, const Reso urceLoaderOptions&); | |
128 // Posts a task to the main thread to run mainThreadStart(); | |
129 void startInMainThread(const ResourceRequest&, const WorkerGlobalScope&) ; | |
130 | |
131 WorkerLoaderProxy* loaderProxy() | |
132 { | |
133 return m_loaderProxy.get(); | |
134 } | |
135 | 164 |
136 private: | 165 private: |
137 // The following methods are overridden by the subclasses to implement | 166 const Member<ThreadableLoaderClientWrapper> m_clientWrapper; |
138 // code to forward did.* method invocations to the worker context's | 167 const RefPtr<WorkerLoaderProxy> m_loaderProxy; |
139 // thread which is specialized for sync and async case respectively. | 168 const ThreadableLoaderOptions m_threadableLoaderOptions; |
140 virtual void forwardTaskToWorker(std::unique_ptr<ExecutionContextTask>) = 0; | 169 const ResourceLoaderOptions m_resourceLoaderOptions; |
141 virtual void forwardTaskToWorkerOnLoaderDone(std::unique_ptr<ExecutionCo ntextTask>) = 0; | 170 const BlockingBehavior m_blockingBehavior; |
142 | 171 |
143 // All executed on the main thread. | 172 // |*m_peer| lives in the main thread. |
144 void mainThreadCreateLoader(ThreadableLoaderOptions, ResourceLoaderOptio ns, ExecutionContext*); | 173 CrossThreadPersistent<Peer> m_peer; |
145 void mainThreadStart(std::unique_ptr<CrossThreadResourceRequestData>); | 174 }; |
146 void mainThreadDestroy(ExecutionContext*); | |
147 void mainThreadOverrideTimeout(unsigned long timeoutMilliseconds, Execut ionContext*); | |
148 void mainThreadCancel(ExecutionContext*); | |
149 | 175 |
150 // Only to be used on the main thread. | 176 // A Peer instance lives in the main thread. It is a ThreadableLoaderClient |
177 // for a DocumentThreadableLoader and forward notifications to the | |
178 // ThreadableLoaderClientWrapper which lives in the worker thread. | |
179 class Peer final : public GarbageCollectedFinalized<Peer>, public Threadable LoaderClient, public WorkerThreadLifecycleObserver { | |
180 USING_GARBAGE_COLLECTED_MIXIN(Peer); | |
181 public: | |
182 static void createAndStart( | |
183 Bridge*, | |
184 PassRefPtr<WorkerLoaderProxy>, | |
185 WorkerThreadLifecycleContext*, | |
186 std::unique_ptr<CrossThreadResourceRequestData>, | |
187 const ThreadableLoaderOptions&, | |
188 const ResourceLoaderOptions&, | |
189 PassRefPtr<WaitableEventWithTasks>, | |
190 ExecutionContext*); | |
191 ~Peer() override; | |
192 | |
193 void overrideTimeout(unsigned long timeoutMillisecond); | |
194 void cancel(); | |
195 | |
196 void didSendData(unsigned long long bytesSent, unsigned long long totalB ytesToBeSent) override; | |
197 void didReceiveResponse(unsigned long identifier, const ResourceResponse &, std::unique_ptr<WebDataConsumerHandle>) override; | |
198 void didReceiveData(const char*, unsigned dataLength) override; | |
199 void didDownloadData(int dataLength) override; | |
200 void didReceiveCachedMetadata(const char*, int dataLength) override; | |
201 void didFinishLoading(unsigned long identifier, double finishTime) overr ide; | |
202 void didFail(const ResourceError&) override; | |
203 void didFailAccessControlCheck(const ResourceError&) override; | |
204 void didFailRedirectCheck() override; | |
205 void didReceiveResourceTiming(const ResourceTimingInfo&) override; | |
206 | |
207 void contextDestroyed() override; | |
208 | |
209 DECLARE_TRACE(); | |
210 | |
211 private: | |
212 Peer(TaskForwarder*, WorkerThreadLifecycleContext*); | |
213 void start(Document&, std::unique_ptr<CrossThreadResourceRequestData>, c onst ThreadableLoaderOptions&, const ResourceLoaderOptions&); | |
214 | |
215 Member<TaskForwarder> m_forwarder; | |
151 std::unique_ptr<ThreadableLoader> m_mainThreadLoader; | 216 std::unique_ptr<ThreadableLoader> m_mainThreadLoader; |
152 | 217 |
153 // |m_workerClientWrapper| holds an pointer created on the worker | 218 // |*m_clientWrapper| lives in the worker thread. |
154 // thread, and |this| instance is created on the main thread. | 219 CrossThreadWeakPersistent<ThreadableLoaderClientWrapper> m_clientWrapper ; |
haraken
2016/07/21 10:22:29
Does this need to be a weak pointer?
yhirano
2016/07/21 10:56:09
Having this as a strong persistent may form a refe
haraken
2016/07/21 15:56:14
I want to confirm two things:
- Even if we use a
yhirano
2016/07/22 04:42:14
contextDestroyed is called at the worker thread te
haraken
2016/07/22 08:57:30
Makes sense.
Once we move ThreadableLoader to Oil
| |
155 CrossThreadPersistent<ThreadableLoaderClientWrapper> m_workerClientWrapp er; | |
156 | |
157 // Used on the worker context thread. | |
158 RefPtr<WorkerLoaderProxy> m_loaderProxy; | |
159 }; | |
160 | |
161 class MainThreadAsyncBridge final : public MainThreadBridgeBase { | |
162 public: | |
163 MainThreadAsyncBridge(WorkerGlobalScope&, ThreadableLoaderClientWrapper* , const ThreadableLoaderOptions&, const ResourceLoaderOptions&); | |
164 void start(const ResourceRequest&, const WorkerGlobalScope&) override; | |
165 | |
166 private: | |
167 ~MainThreadAsyncBridge() override; | |
168 | |
169 void forwardTaskToWorker(std::unique_ptr<ExecutionContextTask>) override ; | |
170 void forwardTaskToWorkerOnLoaderDone(std::unique_ptr<ExecutionContextTas k>) override; | |
171 }; | |
172 | |
173 class MainThreadSyncBridge final : public MainThreadBridgeBase { | |
174 public: | |
175 MainThreadSyncBridge(WorkerGlobalScope&, ThreadableLoaderClientWrapper*, const ThreadableLoaderOptions&, const ResourceLoaderOptions&); | |
176 void start(const ResourceRequest&, const WorkerGlobalScope&) override; | |
177 | |
178 private: | |
179 ~MainThreadSyncBridge() override; | |
180 | |
181 void forwardTaskToWorker(std::unique_ptr<ExecutionContextTask>) override ; | |
182 void forwardTaskToWorkerOnLoaderDone(std::unique_ptr<ExecutionContextTas k>) override; | |
183 | |
184 bool m_done; | |
185 std::unique_ptr<WaitableEvent> m_loaderDoneEvent; | |
186 // Thread-safety: |m_clientTasks| can be written (i.e. Closures are adde d) | |
187 // on the main thread only before |m_loaderDoneEvent| is signaled and ca n be read | |
188 // on the worker context thread only after |m_loaderDoneEvent| is signal ed. | |
189 Vector<std::unique_ptr<ExecutionContextTask>> m_clientTasks; | |
190 Mutex m_lock; | |
191 }; | 220 }; |
192 | 221 |
193 WorkerThreadableLoader(WorkerGlobalScope&, ThreadableLoaderClient*, const Th readableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior); | 222 WorkerThreadableLoader(WorkerGlobalScope&, ThreadableLoaderClient*, const Th readableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior); |
194 | 223 |
195 Persistent<WorkerGlobalScope> m_workerGlobalScope; | 224 Persistent<WorkerGlobalScope> m_workerGlobalScope; |
196 const Persistent<ThreadableLoaderClientWrapper> m_workerClientWrapper; | 225 const Persistent<ThreadableLoaderClientWrapper> m_workerClientWrapper; |
197 | 226 const Persistent<Bridge> m_bridge; |
198 MainThreadBridgeBase* m_bridge; | |
199 }; | 227 }; |
200 | 228 |
201 } // namespace blink | 229 } // namespace blink |
202 | 230 |
203 #endif // WorkerThreadableLoader_h | 231 #endif // WorkerThreadableLoader_h |
OLD | NEW |