Chromium Code Reviews| 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 |
| 59 // TODO(yhirano): Draw a diagram to illustrate the class relationship. | |
|
haraken
2016/07/22 08:57:30
Also I'd suggest renaming classes in a follow-up C
yhirano
2016/07/25 02:20:59
Added a TODO.
Note that ThreadableLoaderClient is
| |
| 60 class WorkerThreadableLoader final : public ThreadableLoader { | 60 class WorkerThreadableLoader final : public ThreadableLoader { |
| 61 USING_FAST_MALLOC(WorkerThreadableLoader); | 61 USING_FAST_MALLOC(WorkerThreadableLoader); |
| 62 public: | 62 public: |
| 63 static void loadResourceSynchronously(WorkerGlobalScope&, const ResourceRequ est&, ThreadableLoaderClient&, const ThreadableLoaderOptions&, const ResourceLoa derOptions&); | 63 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) | 64 static std::unique_ptr<WorkerThreadableLoader> create(WorkerGlobalScope& wor kerGlobalScope, ThreadableLoaderClient* client, const ThreadableLoaderOptions& o ptions, const ResourceLoaderOptions& resourceLoaderOptions) |
| 65 { | 65 { |
| 66 return wrapUnique(new WorkerThreadableLoader(workerGlobalScope, client, options, resourceLoaderOptions, LoadAsynchronously)); | 66 return wrapUnique(new WorkerThreadableLoader(workerGlobalScope, client, options, resourceLoaderOptions, LoadAsynchronously)); |
| 67 } | 67 } |
| 68 | 68 |
| 69 ~WorkerThreadableLoader() override; | 69 ~WorkerThreadableLoader() override; |
| 70 | 70 |
| 71 void start(const ResourceRequest&) override; | 71 void start(const ResourceRequest&) override; |
| 72 | |
| 73 void overrideTimeout(unsigned long timeout) override; | 72 void overrideTimeout(unsigned long timeout) override; |
| 74 | |
| 75 void cancel() override; | 73 void cancel() override; |
| 76 | 74 |
| 77 private: | 75 private: |
| 78 enum BlockingBehavior { | 76 enum BlockingBehavior { |
| 79 LoadSynchronously, | 77 LoadSynchronously, |
| 80 LoadAsynchronously | 78 LoadAsynchronously |
| 81 }; | 79 }; |
| 82 | 80 |
| 83 // Creates a loader on the main thread and bridges communication between | 81 // A TaskForwarder forwards an ExecutionContextTask to the worker thread. |
| 84 // the main thread and the worker context's thread where WorkerThreadableLoa der runs. | 82 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: | 83 public: |
| 104 // All executed on the worker context's thread. | 84 virtual ~TaskForwarder() {} |
| 105 MainThreadBridgeBase(ThreadableLoaderClientWrapper*, PassRefPtr<WorkerLo aderProxy>); | 85 virtual void forwardTask(std::unique_ptr<ExecutionContextTask>) = 0; |
| 106 virtual void start(const ResourceRequest&, const WorkerGlobalScope&) = 0 ; | 86 virtual void forwardTaskWithDoneSignal(std::unique_ptr<ExecutionContextT ask>) = 0; |
| 87 virtual void abort() = 0; | |
| 88 | |
| 89 DEFINE_INLINE_VIRTUAL_TRACE() {} | |
| 90 }; | |
| 91 | |
| 92 class AsyncTaskForwarder final : public TaskForwarder { | |
| 93 public: | |
| 94 explicit AsyncTaskForwarder(PassRefPtr<WorkerLoaderProxy>); | |
| 95 ~AsyncTaskForwarder() override; | |
| 96 | |
| 97 void forwardTask(std::unique_ptr<ExecutionContextTask>) override; | |
| 98 void forwardTaskWithDoneSignal(std::unique_ptr<ExecutionContextTask>) ov erride; | |
| 99 void abort() override; | |
| 100 | |
| 101 private: | |
| 102 RefPtr<WorkerLoaderProxy> m_loaderProxy; | |
| 103 }; | |
| 104 | |
| 105 // Observing functions and wait() need to be called on the worker thread. | |
| 106 // Setting functions and signal() need to be called on the main thread. | |
| 107 // All observing functions must be called after wait() returns, and all | |
| 108 // setting functions must be called before signal() is called. | |
| 109 class WaitableEventWithTasks final : public ThreadSafeRefCounted<WaitableEve ntWithTasks> { | |
| 110 public: | |
| 111 static PassRefPtr<WaitableEventWithTasks> create() { return adoptRef(new WaitableEventWithTasks); } | |
| 112 ~WaitableEventWithTasks(); | |
| 113 | |
| 114 void signal(); | |
| 115 void wait(); | |
| 116 | |
| 117 // Observing functions | |
| 118 bool isAborted() const; | |
| 119 Vector<std::unique_ptr<ExecutionContextTask>> take(); | |
| 120 | |
| 121 // Setting functions | |
| 122 void append(std::unique_ptr<ExecutionContextTask>); | |
| 123 void setIsAborted(); | |
| 124 | |
| 125 private: | |
| 126 WaitableEventWithTasks() {} | |
| 127 | |
| 128 WaitableEvent m_event; | |
| 129 Vector<std::unique_ptr<ExecutionContextTask>> m_tasks; | |
| 130 bool m_isAborted = false; | |
| 131 bool m_isSignalCalled = false; | |
| 132 bool m_isWaitDone = false; | |
| 133 }; | |
| 134 | |
| 135 class SyncTaskForwarder final : public TaskForwarder { | |
| 136 public: | |
| 137 explicit SyncTaskForwarder(PassRefPtr<WaitableEventWithTasks>); | |
| 138 ~SyncTaskForwarder() override; | |
| 139 | |
| 140 void forwardTask(std::unique_ptr<ExecutionContextTask>) override; | |
| 141 void forwardTaskWithDoneSignal(std::unique_ptr<ExecutionContextTask>) ov erride; | |
| 142 void abort() override; | |
| 143 | |
| 144 private: | |
| 145 RefPtr<WaitableEventWithTasks> m_eventWithTasks; | |
| 146 }; | |
| 147 | |
| 148 class Peer; | |
| 149 // A Bridge instance lives in the worker thread and requests the associated | |
| 150 // Peer (which lives in the main thread) to process loading tasks. | |
| 151 class Bridge final : public GarbageCollectedFinalized<Bridge> { | |
| 152 public: | |
| 153 Bridge(ThreadableLoaderClientWrapper*, PassRefPtr<WorkerLoaderProxy>, co nst ThreadableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior); | |
| 154 ~Bridge(); | |
| 155 | |
| 156 void start(const ResourceRequest&, const WorkerGlobalScope&); | |
| 107 void overrideTimeout(unsigned long timeoutMilliseconds); | 157 void overrideTimeout(unsigned long timeoutMilliseconds); |
| 108 void cancel(); | 158 void cancel(); |
| 109 void destroy(); | 159 void destroy(); |
| 110 | 160 |
| 111 // All executed on the main thread. | 161 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 | 162 |
| 123 protected: | 163 // These getter functions are thread safe. |
| 124 ~MainThreadBridgeBase() override; | 164 ThreadableLoaderClientWrapper* clientWrapper() { return m_clientWrapper. get(); } |
| 165 const ThreadableLoaderOptions& threadableLoaderOptions() const { return m_threadableLoaderOptions; } | |
| 166 const ResourceLoaderOptions& resourceLoaderOptions() const { return m_re sourceLoaderOptions; } | |
| 125 | 167 |
| 126 // Posts a task to the main thread to run mainThreadCreateLoader(). | 168 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 | 169 |
| 136 private: | 170 private: |
| 137 // The following methods are overridden by the subclasses to implement | 171 const Member<ThreadableLoaderClientWrapper> m_clientWrapper; |
| 138 // code to forward did.* method invocations to the worker context's | 172 const RefPtr<WorkerLoaderProxy> m_loaderProxy; |
| 139 // thread which is specialized for sync and async case respectively. | 173 const ThreadableLoaderOptions m_threadableLoaderOptions; |
| 140 virtual void forwardTaskToWorker(std::unique_ptr<ExecutionContextTask>) = 0; | 174 const ResourceLoaderOptions m_resourceLoaderOptions; |
| 141 virtual void forwardTaskToWorkerOnLoaderDone(std::unique_ptr<ExecutionCo ntextTask>) = 0; | 175 const BlockingBehavior m_blockingBehavior; |
| 142 | 176 |
| 143 // All executed on the main thread. | 177 // |*m_peer| lives in the main thread. |
| 144 void mainThreadCreateLoader(ThreadableLoaderOptions, ResourceLoaderOptio ns, ExecutionContext*); | 178 CrossThreadPersistent<Peer> m_peer; |
| 145 void mainThreadStart(std::unique_ptr<CrossThreadResourceRequestData>); | 179 }; |
| 146 void mainThreadDestroy(ExecutionContext*); | |
| 147 void mainThreadOverrideTimeout(unsigned long timeoutMilliseconds, Execut ionContext*); | |
| 148 void mainThreadCancel(ExecutionContext*); | |
| 149 | 180 |
| 150 // Only to be used on the main thread. | 181 // A Peer instance lives in the main thread. It is a ThreadableLoaderClient |
| 182 // for a DocumentThreadableLoader and forward notifications to the | |
| 183 // ThreadableLoaderClientWrapper which lives in the worker thread. | |
| 184 class Peer final : public GarbageCollectedFinalized<Peer>, public Threadable LoaderClient, public WorkerThreadLifecycleObserver { | |
| 185 USING_GARBAGE_COLLECTED_MIXIN(Peer); | |
| 186 public: | |
| 187 static void createAndStart( | |
| 188 Bridge*, | |
| 189 PassRefPtr<WorkerLoaderProxy>, | |
| 190 WorkerThreadLifecycleContext*, | |
| 191 std::unique_ptr<CrossThreadResourceRequestData>, | |
| 192 const ThreadableLoaderOptions&, | |
| 193 const ResourceLoaderOptions&, | |
| 194 PassRefPtr<WaitableEventWithTasks>, | |
| 195 ExecutionContext*); | |
| 196 ~Peer() override; | |
| 197 | |
| 198 void overrideTimeout(unsigned long timeoutMillisecond); | |
| 199 void cancel(); | |
| 200 | |
| 201 void didSendData(unsigned long long bytesSent, unsigned long long totalB ytesToBeSent) override; | |
| 202 void didReceiveResponse(unsigned long identifier, const ResourceResponse &, std::unique_ptr<WebDataConsumerHandle>) override; | |
| 203 void didReceiveData(const char*, unsigned dataLength) override; | |
| 204 void didDownloadData(int dataLength) override; | |
| 205 void didReceiveCachedMetadata(const char*, int dataLength) override; | |
| 206 void didFinishLoading(unsigned long identifier, double finishTime) overr ide; | |
| 207 void didFail(const ResourceError&) override; | |
| 208 void didFailAccessControlCheck(const ResourceError&) override; | |
| 209 void didFailRedirectCheck() override; | |
| 210 void didReceiveResourceTiming(const ResourceTimingInfo&) override; | |
| 211 | |
| 212 void contextDestroyed() override; | |
| 213 | |
| 214 DECLARE_TRACE(); | |
| 215 | |
| 216 private: | |
| 217 Peer(TaskForwarder*, WorkerThreadLifecycleContext*); | |
| 218 void start(Document&, std::unique_ptr<CrossThreadResourceRequestData>, c onst ThreadableLoaderOptions&, const ResourceLoaderOptions&); | |
| 219 | |
| 220 Member<TaskForwarder> m_forwarder; | |
| 151 std::unique_ptr<ThreadableLoader> m_mainThreadLoader; | 221 std::unique_ptr<ThreadableLoader> m_mainThreadLoader; |
| 152 | 222 |
| 153 // |m_workerClientWrapper| holds an pointer created on the worker | 223 // |*m_clientWrapper| lives in the worker thread. |
| 154 // thread, and |this| instance is created on the main thread. | 224 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(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 }; | 225 }; |
| 192 | 226 |
| 193 WorkerThreadableLoader(WorkerGlobalScope&, ThreadableLoaderClient*, const Th readableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior); | 227 WorkerThreadableLoader(WorkerGlobalScope&, ThreadableLoaderClient*, const Th readableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior); |
| 194 | 228 |
| 195 Persistent<WorkerGlobalScope> m_workerGlobalScope; | 229 Persistent<WorkerGlobalScope> m_workerGlobalScope; |
| 196 const Persistent<ThreadableLoaderClientWrapper> m_workerClientWrapper; | 230 const Persistent<ThreadableLoaderClientWrapper> m_workerClientWrapper; |
| 197 | 231 const Persistent<Bridge> m_bridge; |
| 198 MainThreadBridgeBase* m_bridge; | |
| 199 }; | 232 }; |
| 200 | 233 |
| 201 } // namespace blink | 234 } // namespace blink |
| 202 | 235 |
| 203 #endif // WorkerThreadableLoader_h | 236 #endif // WorkerThreadableLoader_h |
| OLD | NEW |