| 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" | 42 #include "public/platform/WebTraceLocation.h" |
| 39 #include "wtf/Functional.h" | |
| 40 #include "wtf/PassRefPtr.h" | 43 #include "wtf/PassRefPtr.h" |
| 41 #include "wtf/PtrUtil.h" | 44 #include "wtf/PtrUtil.h" |
| 42 #include "wtf/RefPtr.h" | 45 #include "wtf/RefPtr.h" |
| 43 #include "wtf/Threading.h" | 46 #include "wtf/Threading.h" |
| 44 #include "wtf/ThreadingPrimitives.h" | |
| 45 #include "wtf/Vector.h" | 47 #include "wtf/Vector.h" |
| 46 #include "wtf/text/WTFString.h" | 48 #include "wtf/text/WTFString.h" |
| 47 #include <memory> | 49 #include <memory> |
| 48 | 50 |
| 49 namespace blink { | 51 namespace blink { |
| 50 | 52 |
| 51 class ExecutionContextTask; | |
| 52 class ResourceError; | 53 class ResourceError; |
| 53 class ResourceRequest; | 54 class ResourceRequest; |
| 54 class ResourceResponse; | 55 class ResourceResponse; |
| 55 class WaitableEvent; | |
| 56 class WorkerGlobalScope; | 56 class WorkerGlobalScope; |
| 57 class WorkerLoaderProxy; | 57 class WorkerLoaderProxy; |
| 58 struct CrossThreadResourceRequestData; | 58 struct CrossThreadResourceRequestData; |
| 59 | 59 |
| 60 // TODO(yhirano): Draw a diagram to illustrate the class relationship. |
| 61 // TODO(yhirano): Rename inner classes so that readers can see in which thread |
| 62 // they are living easily. |
| 60 class WorkerThreadableLoader final : public ThreadableLoader { | 63 class WorkerThreadableLoader final : public ThreadableLoader { |
| 61 USING_FAST_MALLOC(WorkerThreadableLoader); | 64 USING_FAST_MALLOC(WorkerThreadableLoader); |
| 62 public: | 65 public: |
| 63 static void loadResourceSynchronously(WorkerGlobalScope&, const ResourceRequ
est&, ThreadableLoaderClient&, const ThreadableLoaderOptions&, const ResourceLoa
derOptions&); | 66 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) | 67 static std::unique_ptr<WorkerThreadableLoader> create(WorkerGlobalScope& wor
kerGlobalScope, ThreadableLoaderClient* client, const ThreadableLoaderOptions& o
ptions, const ResourceLoaderOptions& resourceLoaderOptions) |
| 65 { | 68 { |
| 66 return wrapUnique(new WorkerThreadableLoader(workerGlobalScope, client,
options, resourceLoaderOptions, LoadAsynchronously)); | 69 return wrapUnique(new WorkerThreadableLoader(workerGlobalScope, client,
options, resourceLoaderOptions, LoadAsynchronously)); |
| 67 } | 70 } |
| 68 | 71 |
| 69 ~WorkerThreadableLoader() override; | 72 ~WorkerThreadableLoader() override; |
| 70 | 73 |
| 71 void start(const ResourceRequest&) override; | 74 void start(const ResourceRequest&) override; |
| 72 | |
| 73 void overrideTimeout(unsigned long timeout) override; | 75 void overrideTimeout(unsigned long timeout) override; |
| 74 | |
| 75 void cancel() override; | 76 void cancel() override; |
| 76 | 77 |
| 77 private: | 78 private: |
| 78 enum BlockingBehavior { | 79 enum BlockingBehavior { |
| 79 LoadSynchronously, | 80 LoadSynchronously, |
| 80 LoadAsynchronously | 81 LoadAsynchronously |
| 81 }; | 82 }; |
| 82 | 83 |
| 83 // Creates a loader on the main thread and bridges communication between | 84 // A TaskForwarder forwards an ExecutionContextTask to the worker thread. |
| 84 // the main thread and the worker context's thread where WorkerThreadableLoa
der runs. | 85 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: | 86 public: |
| 104 // All executed on the worker context's thread. | 87 virtual ~TaskForwarder() {} |
| 105 MainThreadBridgeBase(ThreadableLoaderClientWrapper*, PassRefPtr<WorkerLo
aderProxy>); | 88 virtual void forwardTask(const WebTraceLocation&, std::unique_ptr<Execut
ionContextTask>) = 0; |
| 106 virtual void start(const ResourceRequest&, const WorkerGlobalScope&) = 0
; | 89 virtual void forwardTaskWithDoneSignal(const WebTraceLocation&, std::uni
que_ptr<ExecutionContextTask>) = 0; |
| 90 virtual void abort() = 0; |
| 91 |
| 92 DEFINE_INLINE_VIRTUAL_TRACE() {} |
| 93 }; |
| 94 class AsyncTaskForwarder; |
| 95 struct TaskWithLocation; |
| 96 class WaitableEventWithTasks; |
| 97 class SyncTaskForwarder; |
| 98 |
| 99 class Peer; |
| 100 // A Bridge instance lives in the worker thread and requests the associated |
| 101 // Peer (which lives in the main thread) to process loading tasks. |
| 102 class Bridge final : public GarbageCollectedFinalized<Bridge> { |
| 103 public: |
| 104 Bridge(ThreadableLoaderClientWrapper*, PassRefPtr<WorkerLoaderProxy>, co
nst ThreadableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior); |
| 105 ~Bridge(); |
| 106 |
| 107 void start(const ResourceRequest&, const WorkerGlobalScope&); |
| 107 void overrideTimeout(unsigned long timeoutMilliseconds); | 108 void overrideTimeout(unsigned long timeoutMilliseconds); |
| 108 void cancel(); | 109 void cancel(); |
| 109 void destroy(); | 110 void destroy(); |
| 110 | 111 |
| 111 // All executed on the main thread. | 112 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 | 113 |
| 123 protected: | 114 // This getter function is thread safe. |
| 124 ~MainThreadBridgeBase() override; | 115 ThreadableLoaderClientWrapper* clientWrapper() { return m_clientWrapper.
get(); } |
| 125 | 116 |
| 126 // Posts a task to the main thread to run mainThreadCreateLoader(). | 117 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 | 118 |
| 136 private: | 119 private: |
| 137 // The following methods are overridden by the subclasses to implement | 120 void cancelPeer(); |
| 138 // code to forward did.* method invocations to the worker context's | |
| 139 // thread which is specialized for sync and async case respectively. | |
| 140 virtual void forwardTaskToWorker(const WebTraceLocation&, std::unique_pt
r<ExecutionContextTask>) = 0; | |
| 141 virtual void forwardTaskToWorkerOnLoaderDone(const WebTraceLocation&, st
d::unique_ptr<ExecutionContextTask>) = 0; | |
| 142 | 121 |
| 143 // All executed on the main thread. | 122 const Member<ThreadableLoaderClientWrapper> m_clientWrapper; |
| 144 void mainThreadCreateLoader(ThreadableLoaderOptions, ResourceLoaderOptio
ns, ExecutionContext*); | 123 const RefPtr<WorkerLoaderProxy> m_loaderProxy; |
| 145 void mainThreadStart(std::unique_ptr<CrossThreadResourceRequestData>); | 124 const ThreadableLoaderOptions m_threadableLoaderOptions; |
| 146 void mainThreadDestroy(ExecutionContext*); | 125 const ResourceLoaderOptions m_resourceLoaderOptions; |
| 147 void mainThreadOverrideTimeout(unsigned long timeoutMilliseconds, Execut
ionContext*); | 126 const BlockingBehavior m_blockingBehavior; |
| 148 void mainThreadCancel(ExecutionContext*); | |
| 149 | 127 |
| 150 // Only to be used on the main thread. | 128 // |*m_peer| lives in the main thread. |
| 129 CrossThreadPersistent<Peer> m_peer; |
| 130 }; |
| 131 |
| 132 // A Peer instance lives in the main thread. It is a ThreadableLoaderClient |
| 133 // for a DocumentThreadableLoader and forward notifications to the |
| 134 // ThreadableLoaderClientWrapper which lives in the worker thread. |
| 135 class Peer final : public GarbageCollectedFinalized<Peer>, public Threadable
LoaderClient, public WorkerThreadLifecycleObserver { |
| 136 USING_GARBAGE_COLLECTED_MIXIN(Peer); |
| 137 public: |
| 138 static void createAndStart( |
| 139 Bridge*, |
| 140 PassRefPtr<WorkerLoaderProxy>, |
| 141 WorkerThreadLifecycleContext*, |
| 142 std::unique_ptr<CrossThreadResourceRequestData>, |
| 143 const ThreadableLoaderOptions&, |
| 144 const ResourceLoaderOptions&, |
| 145 PassRefPtr<WaitableEventWithTasks>, |
| 146 ExecutionContext*); |
| 147 ~Peer() override; |
| 148 |
| 149 void overrideTimeout(unsigned long timeoutMillisecond); |
| 150 void cancel(); |
| 151 |
| 152 void didSendData(unsigned long long bytesSent, unsigned long long totalB
ytesToBeSent) override; |
| 153 void didReceiveResponse(unsigned long identifier, const ResourceResponse
&, std::unique_ptr<WebDataConsumerHandle>) override; |
| 154 void didReceiveData(const char*, unsigned dataLength) override; |
| 155 void didDownloadData(int dataLength) override; |
| 156 void didReceiveCachedMetadata(const char*, int dataLength) override; |
| 157 void didFinishLoading(unsigned long identifier, double finishTime) overr
ide; |
| 158 void didFail(const ResourceError&) override; |
| 159 void didFailAccessControlCheck(const ResourceError&) override; |
| 160 void didFailRedirectCheck() override; |
| 161 void didReceiveResourceTiming(const ResourceTimingInfo&) override; |
| 162 |
| 163 void contextDestroyed() override; |
| 164 |
| 165 DECLARE_TRACE(); |
| 166 |
| 167 private: |
| 168 Peer(TaskForwarder*, WorkerThreadLifecycleContext*); |
| 169 void start(Document&, std::unique_ptr<CrossThreadResourceRequestData>, c
onst ThreadableLoaderOptions&, const ResourceLoaderOptions&); |
| 170 |
| 171 Member<TaskForwarder> m_forwarder; |
| 151 std::unique_ptr<ThreadableLoader> m_mainThreadLoader; | 172 std::unique_ptr<ThreadableLoader> m_mainThreadLoader; |
| 152 | 173 |
| 153 // |m_workerClientWrapper| holds an pointer created on the worker | 174 // |*m_clientWrapper| lives in the worker thread. |
| 154 // thread, and |this| instance is created on the main thread. | 175 CrossThreadWeakPersistent<ThreadableLoaderClientWrapper> m_clientWrapper
; |
| 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(const WebTraceLocation&, std::unique_ptr<Execut
ionContextTask>) override; | |
| 170 void forwardTaskToWorkerOnLoaderDone(const WebTraceLocation&, std::uniqu
e_ptr<ExecutionContextTask>) 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 struct ClientTask { | |
| 180 WebTraceLocation m_location; | |
| 181 std::unique_ptr<ExecutionContextTask> m_task; | |
| 182 | |
| 183 ClientTask(const WebTraceLocation&, std::unique_ptr<ExecutionContext
Task>); | |
| 184 ~ClientTask(); | |
| 185 | |
| 186 ClientTask(ClientTask&&); | |
| 187 }; | |
| 188 | |
| 189 ~MainThreadSyncBridge() override; | |
| 190 | |
| 191 void forwardTaskToWorker(const WebTraceLocation&, std::unique_ptr<Execut
ionContextTask>) override; | |
| 192 void forwardTaskToWorkerOnLoaderDone(const WebTraceLocation&, std::uniqu
e_ptr<ExecutionContextTask>) override; | |
| 193 | |
| 194 bool m_done; | |
| 195 std::unique_ptr<WaitableEvent> m_loaderDoneEvent; | |
| 196 // Thread-safety: |m_clientTasks| can be written (i.e. Closures are adde
d) | |
| 197 // on the main thread only before |m_loaderDoneEvent| is signaled and ca
n be read | |
| 198 // on the worker context thread only after |m_loaderDoneEvent| is signal
ed. | |
| 199 Vector<ClientTask> m_clientTasks; | |
| 200 Mutex m_lock; | |
| 201 }; | 176 }; |
| 202 | 177 |
| 203 WorkerThreadableLoader(WorkerGlobalScope&, ThreadableLoaderClient*, const Th
readableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior); | 178 WorkerThreadableLoader(WorkerGlobalScope&, ThreadableLoaderClient*, const Th
readableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior); |
| 204 | 179 |
| 205 Persistent<WorkerGlobalScope> m_workerGlobalScope; | 180 Persistent<WorkerGlobalScope> m_workerGlobalScope; |
| 206 const Persistent<ThreadableLoaderClientWrapper> m_workerClientWrapper; | 181 const Persistent<ThreadableLoaderClientWrapper> m_workerClientWrapper; |
| 207 | 182 const Persistent<Bridge> m_bridge; |
| 208 MainThreadBridgeBase* m_bridge; | |
| 209 }; | 183 }; |
| 210 | 184 |
| 211 } // namespace blink | 185 } // namespace blink |
| 212 | 186 |
| 213 #endif // WorkerThreadableLoader_h | 187 #endif // WorkerThreadableLoader_h |
| OLD | NEW |