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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 class ResourceResponse; | 54 class ResourceResponse; |
55 class WaitableEvent; | 55 class WaitableEvent; |
56 class WorkerGlobalScope; | 56 class WorkerGlobalScope; |
57 class WorkerLoaderProxy; | 57 class WorkerLoaderProxy; |
58 struct CrossThreadResourceRequestData; | 58 struct CrossThreadResourceRequestData; |
59 | 59 |
60 class WorkerThreadableLoader final : public ThreadableLoader, private Threadable
LoaderClientWrapper::ResourceTimingClient { | 60 class WorkerThreadableLoader final : public ThreadableLoader, private Threadable
LoaderClientWrapper::ResourceTimingClient { |
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 PassRefPtr<WorkerThreadableLoader> create(WorkerGlobalScope& workerGl
obalScope, ThreadableLoaderClient* client, const ResourceRequest& request, const
ThreadableLoaderOptions& options, const ResourceLoaderOptions& resourceLoaderOp
tions) | 64 static PassRefPtr<WorkerThreadableLoader> create(WorkerGlobalScope& workerGl
obalScope, ThreadableLoaderClient* client, const ThreadableLoaderOptions& option
s, const ResourceLoaderOptions& resourceLoaderOptions) |
65 { | 65 { |
66 return adoptRef(new WorkerThreadableLoader(workerGlobalScope, client, re
quest, options, resourceLoaderOptions, LoadAsynchronously)); | 66 return adoptRef(new WorkerThreadableLoader(workerGlobalScope, client, op
tions, resourceLoaderOptions, LoadAsynchronously)); |
67 } | 67 } |
68 | 68 |
69 ~WorkerThreadableLoader() override; | 69 ~WorkerThreadableLoader() override; |
70 | 70 |
| 71 void start(const ResourceRequest&) override; |
| 72 |
71 void overrideTimeout(unsigned long timeout) override; | 73 void overrideTimeout(unsigned long timeout) override; |
72 | 74 |
73 void cancel() override; | 75 void cancel() override; |
74 | 76 |
75 private: | 77 private: |
76 enum BlockingBehavior { | 78 enum BlockingBehavior { |
77 LoadSynchronously, | 79 LoadSynchronously, |
78 LoadAsynchronously | 80 LoadAsynchronously |
79 }; | 81 }; |
80 | 82 |
(...skipping 13 matching lines...) Expand all Loading... |
94 // case 2. xhr gets aborted and the worker context continues running. | 96 // case 2. xhr gets aborted and the worker context continues running. |
95 // The ThreadableLoaderClientWrapper has the underlying client cleared, s
o no more calls | 97 // The ThreadableLoaderClientWrapper has the underlying client cleared, s
o no more calls |
96 // go through it. All tasks posted from the worker object's thread to th
e worker context's | 98 // go through it. All tasks posted from the worker object's thread to th
e worker context's |
97 // thread do "ThreadableLoaderClientWrapper::ref" (automatically inside o
f the cross thread copy | 99 // thread do "ThreadableLoaderClientWrapper::ref" (automatically inside o
f the cross thread copy |
98 // done in createCrossThreadTask), so the ThreadableLoaderClientWrapper i
nstance is there until all | 100 // done in createCrossThreadTask), so the ThreadableLoaderClientWrapper i
nstance is there until all |
99 // tasks are executed. | 101 // tasks are executed. |
100 class MainThreadBridgeBase : public ThreadableLoaderClient { | 102 class MainThreadBridgeBase : public ThreadableLoaderClient { |
101 public: | 103 public: |
102 // All executed on the worker context's thread. | 104 // All executed on the worker context's thread. |
103 MainThreadBridgeBase(PassRefPtr<ThreadableLoaderClientWrapper>, PassRefP
tr<WorkerLoaderProxy>); | 105 MainThreadBridgeBase(PassRefPtr<ThreadableLoaderClientWrapper>, PassRefP
tr<WorkerLoaderProxy>); |
| 106 virtual void start(const ResourceRequest&, const WorkerGlobalScope&) = 0
; |
104 void overrideTimeout(unsigned long timeoutMilliseconds); | 107 void overrideTimeout(unsigned long timeoutMilliseconds); |
105 void cancel(); | 108 void cancel(); |
106 void destroy(); | 109 void destroy(); |
107 | 110 |
108 // All executed on the main thread. | 111 // All executed on the main thread. |
109 void didSendData(unsigned long long bytesSent, unsigned long long totalB
ytesToBeSent) final; | 112 void didSendData(unsigned long long bytesSent, unsigned long long totalB
ytesToBeSent) final; |
110 void didReceiveResponse(unsigned long identifier, const ResourceResponse
&, PassOwnPtr<WebDataConsumerHandle>) final; | 113 void didReceiveResponse(unsigned long identifier, const ResourceResponse
&, PassOwnPtr<WebDataConsumerHandle>) final; |
111 void didReceiveData(const char*, unsigned dataLength) final; | 114 void didReceiveData(const char*, unsigned dataLength) final; |
112 void didDownloadData(int dataLength) final; | 115 void didDownloadData(int dataLength) final; |
113 void didReceiveCachedMetadata(const char*, int dataLength) final; | 116 void didReceiveCachedMetadata(const char*, int dataLength) final; |
114 void didFinishLoading(unsigned long identifier, double finishTime) final
; | 117 void didFinishLoading(unsigned long identifier, double finishTime) final
; |
115 void didFail(const ResourceError&) final; | 118 void didFail(const ResourceError&) final; |
116 void didFailAccessControlCheck(const ResourceError&) final; | 119 void didFailAccessControlCheck(const ResourceError&) final; |
117 void didFailRedirectCheck() final; | 120 void didFailRedirectCheck() final; |
118 void didReceiveResourceTiming(const ResourceTimingInfo&) final; | 121 void didReceiveResourceTiming(const ResourceTimingInfo&) final; |
119 | 122 |
120 protected: | 123 protected: |
121 ~MainThreadBridgeBase() override; | 124 ~MainThreadBridgeBase() override; |
122 | 125 |
123 // Posts a task to the main thread to run mainThreadCreateLoader(). | 126 // Posts a task to the main thread to run mainThreadCreateLoader(). |
124 void createLoader(const ResourceRequest&, const ThreadableLoaderOptions&
, const ResourceLoaderOptions&, const ReferrerPolicy&, const String&); | 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&)
; |
125 | 130 |
126 WorkerLoaderProxy* loaderProxy() | 131 WorkerLoaderProxy* loaderProxy() |
127 { | 132 { |
128 return m_loaderProxy.get(); | 133 return m_loaderProxy.get(); |
129 } | 134 } |
130 | 135 |
131 PassRefPtr<ThreadableLoaderClientWrapper> workerClientWrapper() | |
132 { | |
133 return m_workerClientWrapper; | |
134 } | |
135 | |
136 private: | 136 private: |
137 // The following methods are overridden by the subclasses to implement | 137 // The following methods are overridden by the subclasses to implement |
138 // code to forward did.* method invocations to the worker context's | 138 // code to forward did.* method invocations to the worker context's |
139 // thread which is specialized for sync and async case respectively. | 139 // thread which is specialized for sync and async case respectively. |
140 virtual void forwardTaskToWorker(PassOwnPtr<ExecutionContextTask>) = 0; | 140 virtual void forwardTaskToWorker(PassOwnPtr<ExecutionContextTask>) = 0; |
141 virtual void forwardTaskToWorkerOnLoaderDone(PassOwnPtr<ExecutionContext
Task>) = 0; | 141 virtual void forwardTaskToWorkerOnLoaderDone(PassOwnPtr<ExecutionContext
Task>) = 0; |
142 | 142 |
143 // All executed on the main thread. | 143 // All executed on the main thread. |
144 void mainThreadCreateLoader(PassOwnPtr<CrossThreadResourceRequestData>,
ThreadableLoaderOptions, ResourceLoaderOptions, const ReferrerPolicy, const Stri
ng& outgoingReferrer, ExecutionContext*); | 144 void mainThreadCreateLoader(ThreadableLoaderOptions, ResourceLoaderOptio
ns, ExecutionContext*); |
| 145 void mainThreadStart(PassOwnPtr<CrossThreadResourceRequestData>, const R
eferrerPolicy, const String& outgoingReferrer); |
145 void mainThreadDestroy(ExecutionContext*); | 146 void mainThreadDestroy(ExecutionContext*); |
146 void mainThreadOverrideTimeout(unsigned long timeoutMilliseconds, Execut
ionContext*); | 147 void mainThreadOverrideTimeout(unsigned long timeoutMilliseconds, Execut
ionContext*); |
147 void mainThreadCancel(ExecutionContext*); | 148 void mainThreadCancel(ExecutionContext*); |
148 | 149 |
149 // Only to be used on the main thread. | 150 // Only to be used on the main thread. |
150 RefPtr<ThreadableLoader> m_mainThreadLoader; | 151 RefPtr<ThreadableLoader> m_mainThreadLoader; |
151 | 152 |
152 // ThreadableLoaderClientWrapper is to be used on the worker context thr
ead. | 153 // ThreadableLoaderClientWrapper is to be used on the worker context thr
ead. |
153 // The ref counting is done on either thread: | 154 // The ref counting is done on either thread: |
154 // - worker context's thread: held by the tasks | 155 // - worker context's thread: held by the tasks |
155 // - main thread: held by MainThreadBridgeBase | 156 // - main thread: held by MainThreadBridgeBase |
156 // Therefore, this must be a ThreadSafeRefCounted. | 157 // Therefore, this must be a ThreadSafeRefCounted. |
157 RefPtr<ThreadableLoaderClientWrapper> m_workerClientWrapper; | 158 RefPtr<ThreadableLoaderClientWrapper> m_workerClientWrapper; |
158 | 159 |
159 // Used on the worker context thread. | 160 // Used on the worker context thread. |
160 RefPtr<WorkerLoaderProxy> m_loaderProxy; | 161 RefPtr<WorkerLoaderProxy> m_loaderProxy; |
161 }; | 162 }; |
162 | 163 |
163 class MainThreadAsyncBridge final : public MainThreadBridgeBase { | 164 class MainThreadAsyncBridge final : public MainThreadBridgeBase { |
164 public: | 165 public: |
165 MainThreadAsyncBridge(WorkerGlobalScope&, PassRefPtr<ThreadableLoaderCli
entWrapper>, const ResourceRequest&, const ThreadableLoaderOptions&, const Resou
rceLoaderOptions&, const ReferrerPolicy, const String& outgoingReferrer); | 166 MainThreadAsyncBridge(WorkerGlobalScope&, PassRefPtr<ThreadableLoaderCli
entWrapper>, const ThreadableLoaderOptions&, const ResourceLoaderOptions&); |
| 167 void start(const ResourceRequest&, const WorkerGlobalScope&) override; |
166 | 168 |
167 private: | 169 private: |
168 ~MainThreadAsyncBridge() override; | 170 ~MainThreadAsyncBridge() override; |
169 | 171 |
170 void forwardTaskToWorker(PassOwnPtr<ExecutionContextTask>) override; | 172 void forwardTaskToWorker(PassOwnPtr<ExecutionContextTask>) override; |
171 void forwardTaskToWorkerOnLoaderDone(PassOwnPtr<ExecutionContextTask>) o
verride; | 173 void forwardTaskToWorkerOnLoaderDone(PassOwnPtr<ExecutionContextTask>) o
verride; |
172 }; | 174 }; |
173 | 175 |
174 class MainThreadSyncBridge final : public MainThreadBridgeBase { | 176 class MainThreadSyncBridge final : public MainThreadBridgeBase { |
175 public: | 177 public: |
176 MainThreadSyncBridge(WorkerGlobalScope&, PassRefPtr<ThreadableLoaderClie
ntWrapper>, const ResourceRequest&, const ThreadableLoaderOptions&, const Resour
ceLoaderOptions&, const ReferrerPolicy, const String& outgoingReferrer); | 178 MainThreadSyncBridge(WorkerGlobalScope&, PassRefPtr<ThreadableLoaderClie
ntWrapper>, const ThreadableLoaderOptions&, const ResourceLoaderOptions&); |
| 179 void start(const ResourceRequest&, const WorkerGlobalScope&) override; |
177 | 180 |
178 private: | 181 private: |
179 ~MainThreadSyncBridge() override; | 182 ~MainThreadSyncBridge() override; |
180 | 183 |
181 void forwardTaskToWorker(PassOwnPtr<ExecutionContextTask>) override; | 184 void forwardTaskToWorker(PassOwnPtr<ExecutionContextTask>) override; |
182 void forwardTaskToWorkerOnLoaderDone(PassOwnPtr<ExecutionContextTask>) o
verride; | 185 void forwardTaskToWorkerOnLoaderDone(PassOwnPtr<ExecutionContextTask>) o
verride; |
183 | 186 |
184 bool m_done; | 187 bool m_done; |
185 OwnPtr<WaitableEvent> m_loaderDoneEvent; | 188 OwnPtr<WaitableEvent> m_loaderDoneEvent; |
186 // Thread-safety: |m_clientTasks| can be written (i.e. Closures are adde
d) | 189 // 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 | 190 // 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. | 191 // on the worker context thread only after |m_loaderDoneEvent| is signal
ed. |
189 Vector<OwnPtr<ExecutionContextTask>> m_clientTasks; | 192 Vector<OwnPtr<ExecutionContextTask>> m_clientTasks; |
190 Mutex m_lock; | 193 Mutex m_lock; |
191 }; | 194 }; |
192 | 195 |
193 WorkerThreadableLoader(WorkerGlobalScope&, ThreadableLoaderClient*, const Re
sourceRequest&, const ThreadableLoaderOptions&, const ResourceLoaderOptions&, Bl
ockingBehavior); | 196 WorkerThreadableLoader(WorkerGlobalScope&, ThreadableLoaderClient*, const Th
readableLoaderOptions&, const ResourceLoaderOptions&, BlockingBehavior); |
194 | 197 |
195 void didReceiveResourceTiming(const ResourceTimingInfo&) override; | 198 void didReceiveResourceTiming(const ResourceTimingInfo&) override; |
196 | 199 |
197 RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope; | 200 RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope; |
198 RefPtr<ThreadableLoaderClientWrapper> m_workerClientWrapper; | 201 RefPtr<ThreadableLoaderClientWrapper> m_workerClientWrapper; |
199 | 202 |
200 MainThreadBridgeBase* m_bridge; | 203 MainThreadBridgeBase* m_bridge; |
201 }; | 204 }; |
202 | 205 |
203 } // namespace blink | 206 } // namespace blink |
204 | 207 |
205 #endif // WorkerThreadableLoader_h | 208 #endif // WorkerThreadableLoader_h |
OLD | NEW |