2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
35 #include "WorkerThreadableLoader.h"
37 #include "GenericWorkerTask.h"
38 #include "ResourceError.h"
39 #include "ResourceRequest.h"
40 #include "ResourceResponse.h"
41 #include "ThreadableLoader.h"
42 #include "WorkerContext.h"
43 #include "WorkerMessagingProxy.h"
44 #include "WorkerThread.h"
46 #include <wtf/OwnPtr.h>
47 #include <wtf/Threading.h>
48 #include <wtf/Vector.h>
54 // FIXME: The assumption that we can upcast worker object proxy to WorkerMessagingProxy will not be true in multi-process implementation.
55 WorkerThreadableLoader::WorkerThreadableLoader(WorkerContext* workerContext, ThreadableLoaderClient* client, const String& taskMode, const ResourceRequest& request, LoadCallbacks callbacksSetting,
56 ContentSniff contentSniff)
57 : m_workerContext(workerContext)
58 , m_bridge(*(new MainThreadBridge(client, *(static_cast<WorkerMessagingProxy*>(m_workerContext->thread()->workerObjectProxy())), taskMode, request, callbacksSetting, contentSniff)))
62 WorkerThreadableLoader::~WorkerThreadableLoader()
67 void WorkerThreadableLoader::cancel()
72 WorkerThreadableLoader::MainThreadBridge::MainThreadBridge(ThreadableLoaderClient* workerClient, WorkerMessagingProxy& messagingProxy, const String& taskMode, const ResourceRequest& request,
73 LoadCallbacks callbacksSetting, ContentSniff contentSniff)
74 : m_workerClientWrapper(ThreadableLoaderClientWrapper::create(workerClient))
75 , m_messagingProxy(messagingProxy)
76 , m_taskMode(taskMode.copy())
79 m_messagingProxy.postTaskToWorkerObject(createCallbackTask(&MainThreadBridge::mainThreadCreateLoader, this, request, callbacksSetting, contentSniff));
82 WorkerThreadableLoader::MainThreadBridge::~MainThreadBridge()
86 void WorkerThreadableLoader::MainThreadBridge::mainThreadCreateLoader(ScriptExecutionContext* context, MainThreadBridge* thisPtr, auto_ptr<CrossThreadResourceRequestData> requestData, LoadCallbacks callbacksSetting, ContentSniff contentSniff)
88 // FIXME: This assert fails for nested workers. Removing the assert would allow it to work,
89 // but then there would be one WorkerThreadableLoader in every intermediate worker simply
90 // chaining the requests, which is not very good. Instead, the postTaskToWorkerObject should be a
91 // postTaskToDocumentContext.
92 ASSERT(isMainThread());
93 ASSERT(context->isDocument());
95 if (thisPtr->m_messagingProxy.askedToTerminate())
98 // FIXME: the created loader has no knowledge of the origin of the worker doing the load request.
99 // Basically every setting done in SubresourceLoader::create (including the contents of addExtraFieldsToRequest)
100 // needs to be examined for how it should take into account a different originator.
101 OwnPtr<ResourceRequest> request(ResourceRequest::adopt(requestData));
102 // FIXME: If the a site requests a local resource, then this will return a non-zero value but the sync path
103 // will return a 0 value. Either this should return 0 or the other code path should do a callback with
105 thisPtr->m_mainThreadLoader = ThreadableLoader::create(context, thisPtr, *request, callbacksSetting, contentSniff);
106 ASSERT(thisPtr->m_mainThreadLoader);
109 void WorkerThreadableLoader::MainThreadBridge::mainThreadDestroy(ScriptExecutionContext* context, MainThreadBridge* thisPtr)
111 ASSERT(isMainThread());
112 ASSERT_UNUSED(context, context->isDocument());
116 void WorkerThreadableLoader::MainThreadBridge::destroy()
118 // Ensure that no more client callbacks are done in the worker context's thread.
119 clearClientWrapper();
121 // "delete this" and m_mainThreadLoader::deref() on the worker object's thread.
122 m_messagingProxy.postTaskToWorkerObject(createCallbackTask(&MainThreadBridge::mainThreadDestroy, this));
125 void WorkerThreadableLoader::MainThreadBridge::mainThreadCancel(ScriptExecutionContext* context, MainThreadBridge* thisPtr)
127 ASSERT(isMainThread());
128 ASSERT_UNUSED(context, context->isDocument());
130 if (!thisPtr->m_mainThreadLoader)
132 thisPtr->m_mainThreadLoader->cancel();
133 thisPtr->m_mainThreadLoader = 0;
136 void WorkerThreadableLoader::MainThreadBridge::cancel()
138 m_messagingProxy.postTaskToWorkerObject(createCallbackTask(&MainThreadBridge::mainThreadCancel, this));
139 clearClientWrapper();
142 void WorkerThreadableLoader::MainThreadBridge::clearClientWrapper()
144 static_cast<ThreadableLoaderClientWrapper*>(m_workerClientWrapper.get())->clearClient();
147 static void workerContextDidSendData(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
149 ASSERT_UNUSED(context, context->isWorkerContext());
150 workerClientWrapper->didSendData(bytesSent, totalBytesToBeSent);
153 void WorkerThreadableLoader::MainThreadBridge::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
155 m_messagingProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidSendData, m_workerClientWrapper, bytesSent, totalBytesToBeSent), m_taskMode);
158 static void workerContextDidReceiveResponse(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, auto_ptr<CrossThreadResourceResponseData> responseData)
160 ASSERT_UNUSED(context, context->isWorkerContext());
161 OwnPtr<ResourceResponse> response(ResourceResponse::adopt(responseData));
162 workerClientWrapper->didReceiveResponse(*response);
165 void WorkerThreadableLoader::MainThreadBridge::didReceiveResponse(const ResourceResponse& response)
167 m_messagingProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidReceiveResponse, m_workerClientWrapper, response), m_taskMode);
170 static void workerContextDidReceiveData(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, auto_ptr<Vector<char> > vectorData)
172 ASSERT_UNUSED(context, context->isWorkerContext());
173 workerClientWrapper->didReceiveData(vectorData->data(), vectorData->size());
176 void WorkerThreadableLoader::MainThreadBridge::didReceiveData(const char* data, int lengthReceived)
178 auto_ptr<Vector<char> > vector(new Vector<char>(lengthReceived)); // needs to be an auto_ptr for usage with createCallbackTask.
179 memcpy(vector->data(), data, lengthReceived);
180 m_messagingProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidReceiveData, m_workerClientWrapper, vector), m_taskMode);
183 static void workerContextDidFinishLoading(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, int identifier)
185 ASSERT_UNUSED(context, context->isWorkerContext());
186 workerClientWrapper->didFinishLoading(identifier);
189 void WorkerThreadableLoader::MainThreadBridge::didFinishLoading(int identifier)
191 m_messagingProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidFinishLoading, m_workerClientWrapper, identifier), m_taskMode);
194 static void workerContextDidFail(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, const ResourceError& error)
196 ASSERT_UNUSED(context, context->isWorkerContext());
197 workerClientWrapper->didFail(error);
200 void WorkerThreadableLoader::MainThreadBridge::didFail(const ResourceError& error)
202 m_messagingProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidFail, m_workerClientWrapper, error), m_taskMode);
205 static void workerContextDidFailRedirectCheck(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper)
207 ASSERT_UNUSED(context, context->isWorkerContext());
208 workerClientWrapper->didFailRedirectCheck();
211 void WorkerThreadableLoader::MainThreadBridge::didFailRedirectCheck()
213 m_messagingProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidFailRedirectCheck, m_workerClientWrapper), m_taskMode);
216 static void workerContextDidReceiveAuthenticationCancellation(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, auto_ptr<CrossThreadResourceResponseData> responseData)
218 ASSERT_UNUSED(context, context->isWorkerContext());
219 OwnPtr<ResourceResponse> response(ResourceResponse::adopt(responseData));
220 workerClientWrapper->didReceiveAuthenticationCancellation(*response);
223 void WorkerThreadableLoader::MainThreadBridge::didReceiveAuthenticationCancellation(const ResourceResponse& response)
225 m_messagingProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidReceiveAuthenticationCancellation, m_workerClientWrapper, response), m_taskMode);
228 } // namespace WebCore
230 #endif // ENABLE(WORKERS)