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