| 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 16 matching lines...) Expand all Loading... |
| 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/dom/ExecutionContextTask.h" |
| 35 #include "core/loader/ThreadableLoader.h" | 35 #include "core/loader/ThreadableLoader.h" |
| 36 #include "core/loader/ThreadableLoaderClient.h" | 36 #include "core/loader/ThreadableLoaderClient.h" |
| 37 #include "core/loader/ThreadableLoaderClientWrapper.h" |
| 37 #include "core/workers/WorkerThread.h" | 38 #include "core/workers/WorkerThread.h" |
| 38 #include "core/workers/WorkerThreadLifecycleObserver.h" | 39 #include "core/workers/WorkerThreadLifecycleObserver.h" |
| 39 #include "platform/WaitableEvent.h" | 40 #include "platform/WaitableEvent.h" |
| 40 #include "platform/heap/Handle.h" | 41 #include "platform/heap/Handle.h" |
| 41 #include "public/platform/WebTraceLocation.h" | 42 #include "public/platform/WebTraceLocation.h" |
| 42 #include "wtf/PassRefPtr.h" | 43 #include "wtf/PassRefPtr.h" |
| 44 #include "wtf/PtrUtil.h" |
| 43 #include "wtf/RefPtr.h" | 45 #include "wtf/RefPtr.h" |
| 44 #include "wtf/Threading.h" | 46 #include "wtf/Threading.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 ResourceError; | 53 class ResourceError; |
| 52 class ResourceRequest; | 54 class ResourceRequest; |
| 53 class ResourceResponse; | 55 class ResourceResponse; |
| 54 class WorkerGlobalScope; | 56 class WorkerGlobalScope; |
| 55 class WorkerLoaderProxy; | 57 class WorkerLoaderProxy; |
| 56 struct CrossThreadResourceRequestData; | 58 struct CrossThreadResourceRequestData; |
| 57 struct CrossThreadResourceTimingInfoData; | |
| 58 | 59 |
| 59 // TODO(yhirano): Draw a diagram to illustrate the class relationship. | 60 // TODO(yhirano): Draw a diagram to illustrate the class relationship. |
| 60 // TODO(yhirano): Rename inner classes so that readers can see in which thread | 61 // TODO(yhirano): Rename inner classes so that readers can see in which thread |
| 61 // they are living easily. | 62 // they are living easily. |
| 62 class WorkerThreadableLoader final : public ThreadableLoader { | 63 class WorkerThreadableLoader final : public ThreadableLoader { |
| 64 USING_FAST_MALLOC(WorkerThreadableLoader); |
| 63 public: | 65 public: |
| 64 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&); |
| 65 static WorkerThreadableLoader* create(WorkerGlobalScope& workerGlobalScope,
ThreadableLoaderClient* client, const ThreadableLoaderOptions& options, const Re
sourceLoaderOptions& resourceLoaderOptions) | 67 static std::unique_ptr<WorkerThreadableLoader> create(WorkerGlobalScope& wor
kerGlobalScope, ThreadableLoaderClient* client, const ThreadableLoaderOptions& o
ptions, const ResourceLoaderOptions& resourceLoaderOptions) |
| 66 { | 68 { |
| 67 return new WorkerThreadableLoader(workerGlobalScope, client, options, re
sourceLoaderOptions, LoadAsynchronously); | 69 return wrapUnique(new WorkerThreadableLoader(workerGlobalScope, client,
options, resourceLoaderOptions, LoadAsynchronously)); |
| 68 } | 70 } |
| 69 | 71 |
| 70 ~WorkerThreadableLoader() override; | 72 ~WorkerThreadableLoader() override; |
| 71 | 73 |
| 72 // ThreadableLoader functions | |
| 73 void start(const ResourceRequest&) override; | 74 void start(const ResourceRequest&) override; |
| 74 void overrideTimeout(unsigned long timeout) override; | 75 void overrideTimeout(unsigned long timeout) override; |
| 75 void cancel() override; | 76 void cancel() override; |
| 76 | 77 |
| 77 DECLARE_TRACE(); | |
| 78 | |
| 79 private: | 78 private: |
| 80 enum BlockingBehavior { | 79 enum BlockingBehavior { |
| 81 LoadSynchronously, | 80 LoadSynchronously, |
| 82 LoadAsynchronously | 81 LoadAsynchronously |
| 83 }; | 82 }; |
| 84 | 83 |
| 85 // A TaskForwarder forwards an ExecutionContextTask to the worker thread. | 84 // A TaskForwarder forwards an ExecutionContextTask to the worker thread. |
| 86 class TaskForwarder : public GarbageCollectedFinalized<TaskForwarder> { | 85 class TaskForwarder : public GarbageCollectedFinalized<TaskForwarder> { |
| 87 public: | 86 public: |
| 88 virtual ~TaskForwarder() {} | 87 virtual ~TaskForwarder() {} |
| 89 virtual void forwardTask(const WebTraceLocation&, std::unique_ptr<Execut
ionContextTask>) = 0; | 88 virtual void forwardTask(const WebTraceLocation&, std::unique_ptr<Execut
ionContextTask>) = 0; |
| 90 virtual void forwardTaskWithDoneSignal(const WebTraceLocation&, std::uni
que_ptr<ExecutionContextTask>) = 0; | 89 virtual void forwardTaskWithDoneSignal(const WebTraceLocation&, std::uni
que_ptr<ExecutionContextTask>) = 0; |
| 91 virtual void abort() = 0; | 90 virtual void abort() = 0; |
| 92 | 91 |
| 93 DEFINE_INLINE_VIRTUAL_TRACE() {} | 92 DEFINE_INLINE_VIRTUAL_TRACE() {} |
| 94 }; | 93 }; |
| 95 class AsyncTaskForwarder; | 94 class AsyncTaskForwarder; |
| 96 struct TaskWithLocation; | 95 struct TaskWithLocation; |
| 97 class WaitableEventWithTasks; | 96 class WaitableEventWithTasks; |
| 98 class SyncTaskForwarder; | 97 class SyncTaskForwarder; |
| 99 | 98 |
| 100 // An instance of this class lives in the main thread. It is a | 99 class Peer; |
| 101 // ThreadableLoaderClient for a DocumentThreadableLoader and forward | 100 // A Bridge instance lives in the worker thread and requests the associated |
| 102 // notifications to the associated WorkerThreadableLoader living in the | 101 // Peer (which lives in the main thread) to process loading tasks. |
| 103 // worker thread. | 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&); |
| 108 void overrideTimeout(unsigned long timeoutMilliseconds); |
| 109 void cancel(); |
| 110 void destroy(); |
| 111 |
| 112 void didStart(Peer*); |
| 113 |
| 114 // This getter function is thread safe. |
| 115 ThreadableLoaderClientWrapper* clientWrapper() { return m_clientWrapper.
get(); } |
| 116 |
| 117 DECLARE_VIRTUAL_TRACE(); |
| 118 |
| 119 private: |
| 120 void cancelPeer(); |
| 121 |
| 122 const Member<ThreadableLoaderClientWrapper> m_clientWrapper; |
| 123 const RefPtr<WorkerLoaderProxy> m_loaderProxy; |
| 124 const ThreadableLoaderOptions m_threadableLoaderOptions; |
| 125 const ResourceLoaderOptions m_resourceLoaderOptions; |
| 126 const BlockingBehavior m_blockingBehavior; |
| 127 |
| 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. |
| 104 class Peer final : public GarbageCollectedFinalized<Peer>, public Threadable
LoaderClient, public WorkerThreadLifecycleObserver { | 135 class Peer final : public GarbageCollectedFinalized<Peer>, public Threadable
LoaderClient, public WorkerThreadLifecycleObserver { |
| 105 USING_GARBAGE_COLLECTED_MIXIN(Peer); | 136 USING_GARBAGE_COLLECTED_MIXIN(Peer); |
| 106 public: | 137 public: |
| 107 static void createAndStart( | 138 static void createAndStart( |
| 108 WorkerThreadableLoader*, | 139 Bridge*, |
| 109 PassRefPtr<WorkerLoaderProxy>, | 140 PassRefPtr<WorkerLoaderProxy>, |
| 110 WorkerThreadLifecycleContext*, | 141 WorkerThreadLifecycleContext*, |
| 111 std::unique_ptr<CrossThreadResourceRequestData>, | 142 std::unique_ptr<CrossThreadResourceRequestData>, |
| 112 const ThreadableLoaderOptions&, | 143 const ThreadableLoaderOptions&, |
| 113 const ResourceLoaderOptions&, | 144 const ResourceLoaderOptions&, |
| 114 PassRefPtr<WaitableEventWithTasks>, | 145 PassRefPtr<WaitableEventWithTasks>, |
| 115 ExecutionContext*); | 146 ExecutionContext*); |
| 116 ~Peer() override; | 147 ~Peer() override; |
| 117 | 148 |
| 118 void overrideTimeout(unsigned long timeoutMillisecond); | 149 void overrideTimeout(unsigned long timeoutMillisecond); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 131 | 162 |
| 132 void contextDestroyed() override; | 163 void contextDestroyed() override; |
| 133 | 164 |
| 134 DECLARE_TRACE(); | 165 DECLARE_TRACE(); |
| 135 | 166 |
| 136 private: | 167 private: |
| 137 Peer(TaskForwarder*, WorkerThreadLifecycleContext*); | 168 Peer(TaskForwarder*, WorkerThreadLifecycleContext*); |
| 138 void start(Document&, std::unique_ptr<CrossThreadResourceRequestData>, c
onst ThreadableLoaderOptions&, const ResourceLoaderOptions&); | 169 void start(Document&, std::unique_ptr<CrossThreadResourceRequestData>, c
onst ThreadableLoaderOptions&, const ResourceLoaderOptions&); |
| 139 | 170 |
| 140 Member<TaskForwarder> m_forwarder; | 171 Member<TaskForwarder> m_forwarder; |
| 141 Member<ThreadableLoader> m_mainThreadLoader; | 172 std::unique_ptr<ThreadableLoader> m_mainThreadLoader; |
| 142 | 173 |
| 143 // |*m_workerLoader| lives in the worker thread. | 174 // |*m_clientWrapper| lives in the worker thread. |
| 144 CrossThreadWeakPersistent<WorkerThreadableLoader> m_workerLoader; | 175 CrossThreadWeakPersistent<ThreadableLoaderClientWrapper> m_clientWrapper
; |
| 145 }; | 176 }; |
| 146 | 177 |
| 147 WorkerThreadableLoader(WorkerGlobalScope&, ThreadableLoaderClient*, const Th
readableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior); | 178 WorkerThreadableLoader(WorkerGlobalScope&, ThreadableLoaderClient*, const Th
readableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior); |
| 148 void didStart(Peer*); | |
| 149 | 179 |
| 150 void didSendData(unsigned long long bytesSent, unsigned long long totalBytes
ToBeSent); | 180 Persistent<WorkerGlobalScope> m_workerGlobalScope; |
| 151 void didReceiveResponse(unsigned long identifier, std::unique_ptr<CrossThrea
dResourceResponseData>, std::unique_ptr<WebDataConsumerHandle>); | 181 const Persistent<ThreadableLoaderClientWrapper> m_workerClientWrapper; |
| 152 void didReceiveData(std::unique_ptr<Vector<char>> data); | 182 const Persistent<Bridge> m_bridge; |
| 153 void didReceiveCachedMetadata(std::unique_ptr<Vector<char>> data); | |
| 154 void didFinishLoading(unsigned long identifier, double finishTime); | |
| 155 void didFail(const ResourceError&); | |
| 156 void didFailAccessControlCheck(const ResourceError&); | |
| 157 void didFailRedirectCheck(); | |
| 158 void didDownloadData(int dataLength); | |
| 159 void didReceiveResourceTiming(std::unique_ptr<CrossThreadResourceTimingInfoD
ata>); | |
| 160 | |
| 161 Member<WorkerGlobalScope> m_workerGlobalScope; | |
| 162 RefPtr<WorkerLoaderProxy> m_workerLoaderProxy; | |
| 163 ThreadableLoaderClient* m_client; | |
| 164 | |
| 165 ThreadableLoaderOptions m_threadableLoaderOptions; | |
| 166 ResourceLoaderOptions m_resourceLoaderOptions; | |
| 167 BlockingBehavior m_blockingBehavior; | |
| 168 | |
| 169 // |*m_peer| lives in the main thread. | |
| 170 CrossThreadPersistent<Peer> m_peer; | |
| 171 }; | 183 }; |
| 172 | 184 |
| 173 } // namespace blink | 185 } // namespace blink |
| 174 | 186 |
| 175 #endif // WorkerThreadableLoader_h | 187 #endif // WorkerThreadableLoader_h |
| OLD | NEW |