OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 |
| 33 #if ENABLE(WORKERS) |
| 34 |
| 35 #include "WorkerThreadableLoader.h" |
| 36 |
| 37 #include "GenericWorkerTask.h" |
| 38 #include "ResourceRequest.h" |
| 39 #include "ResourceResponse.h" |
| 40 #include "ThreadableLoader.h" |
| 41 #include "WorkerContext.h" |
| 42 #include "WorkerMessagingProxy.h" |
| 43 #include "WorkerThread.h" |
| 44 #include <memory> |
| 45 #include <wtf/OwnPtr.h> |
| 46 #include <wtf/Threading.h> |
| 47 #include <wtf/Vector.h> |
| 48 |
| 49 using namespace std; |
| 50 |
| 51 namespace WebCore { |
| 52 |
| 53 WorkerThreadableLoader::WorkerThreadableLoader(WorkerContext* workerContext, Thr
eadableLoaderClient* client, const ResourceRequest& request, LoadCallbacks callb
acksSetting, ContentSniff contentSniff) |
| 54 : m_workerContext(workerContext) |
| 55 , m_bridge(*(new MainThreadBridge(client, *m_workerContext->thread()->messag
ingProxy(), request, callbacksSetting, contentSniff))) |
| 56 { |
| 57 } |
| 58 |
| 59 WorkerThreadableLoader::~WorkerThreadableLoader() |
| 60 { |
| 61 m_bridge.destroy(); |
| 62 } |
| 63 |
| 64 void WorkerThreadableLoader::cancel() |
| 65 { |
| 66 m_bridge.cancel(); |
| 67 } |
| 68 |
| 69 WorkerThreadableLoader::MainThreadBridge::MainThreadBridge(ThreadableLoaderClien
t* workerClient, WorkerMessagingProxy& messagingProxy, const ResourceRequest& re
quest, LoadCallbacks callbacksSetting, ContentSniff contentSniff) |
| 70 : m_workerClientWrapper(ThreadableLoaderClientWrapper::create(workerClient)) |
| 71 , m_messagingProxy(messagingProxy) |
| 72 { |
| 73 ASSERT(workerClient); |
| 74 m_messagingProxy.postTaskToWorkerObject(createCallbackTask(&MainThreadBridge
::mainThreadCreateLoader, this, request, callbacksSetting, contentSniff)); |
| 75 } |
| 76 |
| 77 WorkerThreadableLoader::MainThreadBridge::~MainThreadBridge() |
| 78 { |
| 79 } |
| 80 |
| 81 void WorkerThreadableLoader::MainThreadBridge::mainThreadCreateLoader(ScriptExec
utionContext* context, MainThreadBridge* thisPtr, auto_ptr<CrossThreadResourceRe
questData> requestData, LoadCallbacks callbacksSetting, ContentSniff contentSnif
f) |
| 82 { |
| 83 // FIXME: This assert fails for nested workers. Removing the assert would a
llow it to work, |
| 84 // but then there would be one WorkerThreadableLoader in every intermediate
worker simply |
| 85 // chaining the requests, which is not very good. Instead, the postTaskToWor
kerObject should be a |
| 86 // postTaskToDocumentContext. |
| 87 ASSERT(isMainThread()); |
| 88 ASSERT(context->isDocument()); |
| 89 |
| 90 if (thisPtr->m_messagingProxy.askedToTerminate()) |
| 91 return; |
| 92 |
| 93 // FIXME: the created loader has no knowledge of the origin of the worker do
ing the load request. |
| 94 // Basically every setting done in SubresourceLoader::create (including the
contents of addExtraFieldsToRequest) |
| 95 // needs to be examined for how it should be take into account a different o
riginator. |
| 96 OwnPtr<ResourceRequest> request(ResourceRequest::adopt(requestData)); |
| 97 thisPtr->m_mainThreadLoader = ThreadableLoader::create(context, thisPtr, *re
quest, callbacksSetting, contentSniff); |
| 98 if (!thisPtr->m_mainThreadLoader) |
| 99 thisPtr->didFail(); |
| 100 } |
| 101 |
| 102 void WorkerThreadableLoader::MainThreadBridge::mainThreadDestroy(ScriptExecution
Context* context, MainThreadBridge* thisPtr) |
| 103 { |
| 104 ASSERT(isMainThread()); |
| 105 ASSERT_UNUSED(context, context->isDocument()); |
| 106 delete thisPtr; |
| 107 } |
| 108 |
| 109 void WorkerThreadableLoader::MainThreadBridge::destroy() |
| 110 { |
| 111 // Ensure that no more client callbacks are done in the worker context's thr
ead. |
| 112 clearClientWrapper(); |
| 113 |
| 114 // "delete this" and m_mainThreadLoader::deref() on the worker object's thre
ad. |
| 115 m_messagingProxy.postTaskToWorkerObject(createCallbackTask(&MainThreadBridge
::mainThreadDestroy, this)); |
| 116 } |
| 117 |
| 118 void WorkerThreadableLoader::MainThreadBridge::mainThreadCancel(ScriptExecutionC
ontext* context, MainThreadBridge* thisPtr) |
| 119 { |
| 120 ASSERT(isMainThread()); |
| 121 ASSERT_UNUSED(context, context->isDocument()); |
| 122 |
| 123 if (!thisPtr->m_mainThreadLoader) |
| 124 return; |
| 125 thisPtr->m_mainThreadLoader->cancel(); |
| 126 thisPtr->m_mainThreadLoader = 0; |
| 127 } |
| 128 |
| 129 void WorkerThreadableLoader::MainThreadBridge::cancel() |
| 130 { |
| 131 m_messagingProxy.postTaskToWorkerObject(createCallbackTask(&MainThreadBridge
::mainThreadCancel, this)); |
| 132 clearClientWrapper(); |
| 133 } |
| 134 |
| 135 void WorkerThreadableLoader::MainThreadBridge::clearClientWrapper() |
| 136 { |
| 137 static_cast<ThreadableLoaderClientWrapper*>(m_workerClientWrapper.get())->cl
earClient(); |
| 138 } |
| 139 |
| 140 static void workerContextDidSendData(ScriptExecutionContext* context, RefPtr<Thr
eadableLoaderClientWrapper> workerClientWrapper, unsigned long long bytesSent, u
nsigned long long totalBytesToBeSent) |
| 141 { |
| 142 ASSERT_UNUSED(context, context->isWorkerContext()); |
| 143 workerClientWrapper->didSendData(bytesSent, totalBytesToBeSent); |
| 144 } |
| 145 |
| 146 void WorkerThreadableLoader::MainThreadBridge::didSendData(unsigned long long by
tesSent, unsigned long long totalBytesToBeSent) |
| 147 { |
| 148 m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDi
dSendData, m_workerClientWrapper, bytesSent, totalBytesToBeSent)); |
| 149 } |
| 150 |
| 151 static void workerContextDidReceiveResponse(ScriptExecutionContext* context, Ref
Ptr<ThreadableLoaderClientWrapper> workerClientWrapper, auto_ptr<CrossThreadReso
urceResponseData> responseData) |
| 152 { |
| 153 ASSERT_UNUSED(context, context->isWorkerContext()); |
| 154 OwnPtr<ResourceResponse> response(ResourceResponse::adopt(responseData)); |
| 155 workerClientWrapper->didReceiveResponse(*response); |
| 156 } |
| 157 |
| 158 void WorkerThreadableLoader::MainThreadBridge::didReceiveResponse(const Resource
Response& response) |
| 159 { |
| 160 m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDi
dReceiveResponse, m_workerClientWrapper, response)); |
| 161 } |
| 162 |
| 163 static void workerContextDidReceiveData(ScriptExecutionContext* context, RefPtr<
ThreadableLoaderClientWrapper> workerClientWrapper, auto_ptr<Vector<char> > vect
orData) |
| 164 { |
| 165 ASSERT_UNUSED(context, context->isWorkerContext()); |
| 166 workerClientWrapper->didReceiveData(vectorData->data(), vectorData->size()); |
| 167 } |
| 168 |
| 169 void WorkerThreadableLoader::MainThreadBridge::didReceiveData(const char* data,
int lengthReceived) |
| 170 { |
| 171 auto_ptr<Vector<char> > vector(new Vector<char>(lengthReceived)); // needs t
o be an auto_ptr for usage with createCallbackTask. |
| 172 memcpy(vector->data(), data, lengthReceived); |
| 173 m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDi
dReceiveData, m_workerClientWrapper, vector)); |
| 174 } |
| 175 |
| 176 static void workerContextDidFinishLoading(ScriptExecutionContext* context, RefPt
r<ThreadableLoaderClientWrapper> workerClientWrapper, int identifier) |
| 177 { |
| 178 ASSERT_UNUSED(context, context->isWorkerContext()); |
| 179 workerClientWrapper->didFinishLoading(identifier); |
| 180 } |
| 181 |
| 182 void WorkerThreadableLoader::MainThreadBridge::didFinishLoading(int identifier) |
| 183 { |
| 184 m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDi
dFinishLoading, m_workerClientWrapper, identifier)); |
| 185 } |
| 186 |
| 187 static void workerContextDidFail(ScriptExecutionContext* context, RefPtr<Threada
bleLoaderClientWrapper> workerClientWrapper) |
| 188 { |
| 189 ASSERT_UNUSED(context, context->isWorkerContext()); |
| 190 workerClientWrapper->didFail(); |
| 191 } |
| 192 |
| 193 void WorkerThreadableLoader::MainThreadBridge::didFail() |
| 194 { |
| 195 m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDi
dFail, m_workerClientWrapper)); |
| 196 } |
| 197 |
| 198 static void workerContextDidGetCancelled(ScriptExecutionContext* context, RefPtr
<ThreadableLoaderClientWrapper> workerClientWrapper) |
| 199 { |
| 200 ASSERT_UNUSED(context, context->isWorkerContext()); |
| 201 workerClientWrapper->didGetCancelled(); |
| 202 } |
| 203 |
| 204 void WorkerThreadableLoader::MainThreadBridge::didGetCancelled() |
| 205 { |
| 206 m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDi
dGetCancelled, m_workerClientWrapper)); |
| 207 } |
| 208 |
| 209 static void workerContextDidReceiveAuthenticationCancellation(ScriptExecutionCon
text* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, auto_p
tr<CrossThreadResourceResponseData> responseData) |
| 210 { |
| 211 ASSERT_UNUSED(context, context->isWorkerContext()); |
| 212 OwnPtr<ResourceResponse> response(ResourceResponse::adopt(responseData)); |
| 213 workerClientWrapper->didReceiveAuthenticationCancellation(*response); |
| 214 } |
| 215 |
| 216 void WorkerThreadableLoader::MainThreadBridge::didReceiveAuthenticationCancellat
ion(const ResourceResponse& response) |
| 217 { |
| 218 m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDi
dReceiveAuthenticationCancellation, m_workerClientWrapper, response)); |
| 219 } |
| 220 |
| 221 } // namespace WebCore |
| 222 |
| 223 #endif // ENABLE(WORKERS) |
OLD | NEW |