Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: third_party/WebKit/Source/core/loader/WorkerThreadableLoader.h

Issue 2151173003: Move WorkerThreadable internal classes to Oilpan heap (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@onheap-threadable-loader-client-wrapper
Patch Set: fix Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/loader/WorkerThreadableLoader.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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.
60 // TODO(yhirano): Rename inner classes so that readers can see in which thread
61 // they are living easily.
60 class WorkerThreadableLoader final : public ThreadableLoader { 62 class WorkerThreadableLoader final : public ThreadableLoader {
61 USING_FAST_MALLOC(WorkerThreadableLoader); 63 USING_FAST_MALLOC(WorkerThreadableLoader);
62 public: 64 public:
63 static void loadResourceSynchronously(WorkerGlobalScope&, const ResourceRequ est&, ThreadableLoaderClient&, const ThreadableLoaderOptions&, const ResourceLoa derOptions&); 65 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) 66 static std::unique_ptr<WorkerThreadableLoader> create(WorkerGlobalScope& wor kerGlobalScope, ThreadableLoaderClient* client, const ThreadableLoaderOptions& o ptions, const ResourceLoaderOptions& resourceLoaderOptions)
65 { 67 {
66 return wrapUnique(new WorkerThreadableLoader(workerGlobalScope, client, options, resourceLoaderOptions, LoadAsynchronously)); 68 return wrapUnique(new WorkerThreadableLoader(workerGlobalScope, client, options, resourceLoaderOptions, LoadAsynchronously));
67 } 69 }
68 70
69 ~WorkerThreadableLoader() override; 71 ~WorkerThreadableLoader() override;
70 72
71 void start(const ResourceRequest&) override; 73 void start(const ResourceRequest&) override;
72
73 void overrideTimeout(unsigned long timeout) override; 74 void overrideTimeout(unsigned long timeout) override;
74
75 void cancel() override; 75 void cancel() override;
76 76
77 private: 77 private:
78 enum BlockingBehavior { 78 enum BlockingBehavior {
79 LoadSynchronously, 79 LoadSynchronously,
80 LoadAsynchronously 80 LoadAsynchronously
81 }; 81 };
82 82
83 // Creates a loader on the main thread and bridges communication between 83 // A TaskForwarder forwards an ExecutionContextTask to the worker thread.
84 // the main thread and the worker context's thread where WorkerThreadableLoa der runs. 84 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: 85 public:
104 // All executed on the worker context's thread. 86 virtual ~TaskForwarder() {}
105 MainThreadBridgeBase(ThreadableLoaderClientWrapper*, PassRefPtr<WorkerLo aderProxy>); 87 virtual void forwardTask(std::unique_ptr<ExecutionContextTask>) = 0;
106 virtual void start(const ResourceRequest&, const WorkerGlobalScope&) = 0 ; 88 virtual void forwardTaskWithDoneSignal(std::unique_ptr<ExecutionContextT ask>) = 0;
89 virtual void abort() = 0;
90
91 DEFINE_INLINE_VIRTUAL_TRACE() {}
92 };
93
94 class AsyncTaskForwarder final : public TaskForwarder {
kinuko 2016/07/27 02:17:49 nit: declaration of TaskForwarder subclasses and W
yhirano 2016/07/28 07:25:12 Done.
95 public:
96 explicit AsyncTaskForwarder(PassRefPtr<WorkerLoaderProxy>);
97 ~AsyncTaskForwarder() override;
98
99 void forwardTask(std::unique_ptr<ExecutionContextTask>) override;
100 void forwardTaskWithDoneSignal(std::unique_ptr<ExecutionContextTask>) ov erride;
101 void abort() override;
102
103 private:
104 RefPtr<WorkerLoaderProxy> m_loaderProxy;
105 };
106
107 // Observing functions and wait() need to be called on the worker thread.
108 // Setting functions and signal() need to be called on the main thread.
109 // All observing functions must be called after wait() returns, and all
110 // setting functions must be called before signal() is called.
111 class WaitableEventWithTasks final : public ThreadSafeRefCounted<WaitableEve ntWithTasks> {
112 public:
113 static PassRefPtr<WaitableEventWithTasks> create() { return adoptRef(new WaitableEventWithTasks); }
114 ~WaitableEventWithTasks();
115
116 void signal();
117 void wait();
118
119 // Observing functions
120 bool isAborted() const;
121 Vector<std::unique_ptr<ExecutionContextTask>> take();
122
123 // Setting functions
124 void append(std::unique_ptr<ExecutionContextTask>);
125 void setIsAborted();
126
127 private:
128 WaitableEventWithTasks() {}
129
130 WaitableEvent m_event;
131 Vector<std::unique_ptr<ExecutionContextTask>> m_tasks;
132 bool m_isAborted = false;
133 bool m_isSignalCalled = false;
134 bool m_isWaitDone = false;
135 };
136
137 class SyncTaskForwarder final : public TaskForwarder {
138 public:
139 explicit SyncTaskForwarder(PassRefPtr<WaitableEventWithTasks>);
140 ~SyncTaskForwarder() override;
141
142 void forwardTask(std::unique_ptr<ExecutionContextTask>) override;
143 void forwardTaskWithDoneSignal(std::unique_ptr<ExecutionContextTask>) ov erride;
144 void abort() override;
145
146 private:
147 RefPtr<WaitableEventWithTasks> m_eventWithTasks;
148 };
149
150 class Peer;
151 // A Bridge instance lives in the worker thread and requests the associated
152 // Peer (which lives in the main thread) to process loading tasks.
153 class Bridge final : public GarbageCollectedFinalized<Bridge> {
154 public:
155 Bridge(ThreadableLoaderClientWrapper*, PassRefPtr<WorkerLoaderProxy>, co nst ThreadableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior);
156 ~Bridge();
157
158 void start(const ResourceRequest&, const WorkerGlobalScope&);
107 void overrideTimeout(unsigned long timeoutMilliseconds); 159 void overrideTimeout(unsigned long timeoutMilliseconds);
108 void cancel(); 160 void cancel();
109 void destroy(); 161 void destroy();
110 162
111 // All executed on the main thread. 163 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 164
123 protected: 165 // This getter function is thread safe.
124 ~MainThreadBridgeBase() override; 166 ThreadableLoaderClientWrapper* clientWrapper() { return m_clientWrapper. get(); }
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 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(std::unique_ptr<ExecutionContextTask>) = 0;
141 virtual void forwardTaskToWorkerOnLoaderDone(std::unique_ptr<ExecutionCo ntextTask>) = 0;
142 172
143 // All executed on the main thread. 173 const Member<ThreadableLoaderClientWrapper> m_clientWrapper;
144 void mainThreadCreateLoader(ThreadableLoaderOptions, ResourceLoaderOptio ns, ExecutionContext*); 174 const RefPtr<WorkerLoaderProxy> m_loaderProxy;
145 void mainThreadStart(std::unique_ptr<CrossThreadResourceRequestData>); 175 const ThreadableLoaderOptions m_threadableLoaderOptions;
146 void mainThreadDestroy(ExecutionContext*); 176 const ResourceLoaderOptions m_resourceLoaderOptions;
147 void mainThreadOverrideTimeout(unsigned long timeoutMilliseconds, Execut ionContext*); 177 const BlockingBehavior m_blockingBehavior;
148 void mainThreadCancel(ExecutionContext*);
149 178
150 // Only to be used on the main thread. 179 // |*m_peer| lives in the main thread.
180 CrossThreadPersistent<Peer> m_peer;
181 };
182
183 // A Peer instance lives in the main thread. It is a ThreadableLoaderClient
184 // for a DocumentThreadableLoader and forward notifications to the
185 // ThreadableLoaderClientWrapper which lives in the worker thread.
186 class Peer final : public GarbageCollectedFinalized<Peer>, public Threadable LoaderClient, public WorkerThreadLifecycleObserver {
187 USING_GARBAGE_COLLECTED_MIXIN(Peer);
188 public:
189 static void createAndStart(
190 Bridge*,
191 PassRefPtr<WorkerLoaderProxy>,
192 WorkerThreadLifecycleContext*,
193 std::unique_ptr<CrossThreadResourceRequestData>,
194 const ThreadableLoaderOptions&,
195 const ResourceLoaderOptions&,
196 PassRefPtr<WaitableEventWithTasks>,
197 ExecutionContext*);
198 ~Peer() override;
199
200 void overrideTimeout(unsigned long timeoutMillisecond);
201 void cancel();
202
203 void didSendData(unsigned long long bytesSent, unsigned long long totalB ytesToBeSent) override;
204 void didReceiveResponse(unsigned long identifier, const ResourceResponse &, std::unique_ptr<WebDataConsumerHandle>) override;
205 void didReceiveData(const char*, unsigned dataLength) override;
206 void didDownloadData(int dataLength) override;
207 void didReceiveCachedMetadata(const char*, int dataLength) override;
208 void didFinishLoading(unsigned long identifier, double finishTime) overr ide;
209 void didFail(const ResourceError&) override;
210 void didFailAccessControlCheck(const ResourceError&) override;
211 void didFailRedirectCheck() override;
212 void didReceiveResourceTiming(const ResourceTimingInfo&) override;
213
214 void contextDestroyed() override;
215
216 DECLARE_TRACE();
217
218 private:
219 Peer(TaskForwarder*, WorkerThreadLifecycleContext*);
220 void start(Document&, std::unique_ptr<CrossThreadResourceRequestData>, c onst ThreadableLoaderOptions&, const ResourceLoaderOptions&);
221
222 Member<TaskForwarder> m_forwarder;
151 std::unique_ptr<ThreadableLoader> m_mainThreadLoader; 223 std::unique_ptr<ThreadableLoader> m_mainThreadLoader;
152 224
153 // |m_workerClientWrapper| holds an pointer created on the worker 225 // |*m_clientWrapper| lives in the worker thread.
154 // thread, and |this| instance is created on the main thread. 226 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 }; 227 };
192 228
193 WorkerThreadableLoader(WorkerGlobalScope&, ThreadableLoaderClient*, const Th readableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior); 229 WorkerThreadableLoader(WorkerGlobalScope&, ThreadableLoaderClient*, const Th readableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior);
194 230
195 Persistent<WorkerGlobalScope> m_workerGlobalScope; 231 Persistent<WorkerGlobalScope> m_workerGlobalScope;
196 const Persistent<ThreadableLoaderClientWrapper> m_workerClientWrapper; 232 const Persistent<ThreadableLoaderClientWrapper> m_workerClientWrapper;
197 233 const Persistent<Bridge> m_bridge;
198 MainThreadBridgeBase* m_bridge;
199 }; 234 };
200 235
201 } // namespace blink 236 } // namespace blink
202 237
203 #endif // WorkerThreadableLoader_h 238 #endif // WorkerThreadableLoader_h
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/loader/WorkerThreadableLoader.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698