2 * Copyright (C) 2009, 2010 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 "CrossThreadTask.h"
38 #include "ResourceError.h"
39 #include "ResourceRequest.h"
40 #include "ResourceResponse.h"
41 #include "ThreadableLoader.h"
42 #include "WorkerContext.h"
43 #include "WorkerLoaderProxy.h"
44 #include "WorkerThread.h"
45 #include <wtf/OwnPtr.h>
46 #include <wtf/Threading.h>
47 #include <wtf/Vector.h>
53 static const char loadResourceSynchronouslyMode[] = "loadResourceSynchronouslyMode";
55 WorkerThreadableLoader::WorkerThreadableLoader(WorkerContext* workerContext, ThreadableLoaderClient* client, const String& taskMode, const ResourceRequest& request, const ThreadableLoaderOptions& options)
56 : m_workerContext(workerContext)
57 , m_workerClientWrapper(ThreadableLoaderClientWrapper::create(client))
58 , m_bridge(*(new MainThreadBridge(m_workerClientWrapper, m_workerContext->thread()->workerLoaderProxy(), taskMode, request, options)))
62 WorkerThreadableLoader::~WorkerThreadableLoader()
67 void WorkerThreadableLoader::loadResourceSynchronously(WorkerContext* workerContext, const ResourceRequest& request, ThreadableLoaderClient& client, const ThreadableLoaderOptions& options)
69 WorkerRunLoop& runLoop = workerContext->thread()->runLoop();
71 // Create a unique mode just for this synchronous resource load.
72 String mode = loadResourceSynchronouslyMode;
73 mode.append(String::number(runLoop.createUniqueId()));
75 RefPtr<WorkerThreadableLoader> loader = WorkerThreadableLoader::create(workerContext, &client, mode, request, options);
76 MessageQueueWaitResult result = MessageQueueMessageReceived;
77 while (!loader->done() && result != MessageQueueTerminated)
78 result = runLoop.runInMode(workerContext, mode);
80 if (!loader->done() && result == MessageQueueTerminated)
84 void WorkerThreadableLoader::cancel()
89 WorkerThreadableLoader::MainThreadBridge::MainThreadBridge(PassRefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, WorkerLoaderProxy& loaderProxy, const String& taskMode,
90 const ResourceRequest& request, const ThreadableLoaderOptions& options)
91 : m_workerClientWrapper(workerClientWrapper)
92 , m_loaderProxy(loaderProxy)
93 , m_taskMode(taskMode.crossThreadString())
95 ASSERT(m_workerClientWrapper.get());
96 m_loaderProxy.postTaskToLoader(createCallbackTask(&MainThreadBridge::mainThreadCreateLoader, this, request, options));
99 WorkerThreadableLoader::MainThreadBridge::~MainThreadBridge()
103 void WorkerThreadableLoader::MainThreadBridge::mainThreadCreateLoader(ScriptExecutionContext* context, MainThreadBridge* thisPtr, PassOwnPtr<CrossThreadResourceRequestData> requestData, ThreadableLoaderOptions options)
105 ASSERT(isMainThread());
106 ASSERT(context->isDocument());
108 // FIXME: the created loader has no knowledge of the origin of the worker doing the load request.
109 // Basically every setting done in SubresourceLoader::create (including the contents of addExtraFieldsToRequest)
110 // needs to be examined for how it should take into account a different originator.
111 OwnPtr<ResourceRequest> request(ResourceRequest::adopt(requestData));
112 // FIXME: If the a site requests a local resource, then this will return a non-zero value but the sync path
113 // will return a 0 value. Either this should return 0 or the other code path should do a callback with
115 thisPtr->m_mainThreadLoader = ThreadableLoader::create(context, thisPtr, *request, options);
116 ASSERT(thisPtr->m_mainThreadLoader);
119 void WorkerThreadableLoader::MainThreadBridge::mainThreadDestroy(ScriptExecutionContext* context, MainThreadBridge* thisPtr)
121 ASSERT(isMainThread());
122 ASSERT_UNUSED(context, context->isDocument());
126 void WorkerThreadableLoader::MainThreadBridge::destroy()
128 // Ensure that no more client callbacks are done in the worker context's thread.
129 clearClientWrapper();
131 // "delete this" and m_mainThreadLoader::deref() on the worker object's thread.
132 m_loaderProxy.postTaskToLoader(createCallbackTask(&MainThreadBridge::mainThreadDestroy, this));
135 void WorkerThreadableLoader::MainThreadBridge::mainThreadCancel(ScriptExecutionContext* context, MainThreadBridge* thisPtr)
137 ASSERT(isMainThread());
138 ASSERT_UNUSED(context, context->isDocument());
140 if (!thisPtr->m_mainThreadLoader)
142 thisPtr->m_mainThreadLoader->cancel();
143 thisPtr->m_mainThreadLoader = 0;
146 void WorkerThreadableLoader::MainThreadBridge::cancel()
148 m_loaderProxy.postTaskToLoader(createCallbackTask(&MainThreadBridge::mainThreadCancel, this));
149 ThreadableLoaderClientWrapper* clientWrapper = m_workerClientWrapper.get();
150 if (!clientWrapper->done()) {
151 // If the client hasn't reached a termination state, then transition it by sending a cancellation error.
152 // Note: no more client callbacks will be done after this method -- the clearClientWrapper() call ensures that.
153 ResourceError error(String(), 0, String(), String());
154 error.setIsCancellation(true);
155 clientWrapper->didFail(error);
157 clearClientWrapper();
160 void WorkerThreadableLoader::MainThreadBridge::clearClientWrapper()
162 m_workerClientWrapper->clearClient();
165 static void workerContextDidSendData(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
167 ASSERT_UNUSED(context, context->isWorkerContext());
168 workerClientWrapper->didSendData(bytesSent, totalBytesToBeSent);
171 void WorkerThreadableLoader::MainThreadBridge::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
173 m_loaderProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidSendData, m_workerClientWrapper, bytesSent, totalBytesToBeSent), m_taskMode);
176 static void workerContextDidReceiveResponse(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, PassOwnPtr<CrossThreadResourceResponseData> responseData)
178 ASSERT_UNUSED(context, context->isWorkerContext());
179 OwnPtr<ResourceResponse> response(ResourceResponse::adopt(responseData));
180 workerClientWrapper->didReceiveResponse(*response);
183 void WorkerThreadableLoader::MainThreadBridge::didReceiveResponse(const ResourceResponse& response)
185 m_loaderProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidReceiveResponse, m_workerClientWrapper, response), m_taskMode);
188 static void workerContextDidReceiveData(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, PassOwnPtr<Vector<char> > vectorData)
190 ASSERT_UNUSED(context, context->isWorkerContext());
191 workerClientWrapper->didReceiveData(vectorData->data(), vectorData->size());
194 void WorkerThreadableLoader::MainThreadBridge::didReceiveData(const char* data, int lengthReceived)
196 OwnPtr<Vector<char> > vector = adoptPtr(new Vector<char>(lengthReceived)); // needs to be an OwnPtr for usage with createCallbackTask.
197 memcpy(vector->data(), data, lengthReceived);
198 m_loaderProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidReceiveData, m_workerClientWrapper, vector.release()), m_taskMode);
201 static void workerContextDidFinishLoading(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, unsigned long identifier)
203 ASSERT_UNUSED(context, context->isWorkerContext());
204 workerClientWrapper->didFinishLoading(identifier);
207 void WorkerThreadableLoader::MainThreadBridge::didFinishLoading(unsigned long identifier)
209 m_loaderProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidFinishLoading, m_workerClientWrapper, identifier), m_taskMode);
212 static void workerContextDidFail(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, const ResourceError& error)
214 ASSERT_UNUSED(context, context->isWorkerContext());
215 workerClientWrapper->didFail(error);
218 void WorkerThreadableLoader::MainThreadBridge::didFail(const ResourceError& error)
220 m_loaderProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidFail, m_workerClientWrapper, error), m_taskMode);
223 static void workerContextDidFailRedirectCheck(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper)
225 ASSERT_UNUSED(context, context->isWorkerContext());
226 workerClientWrapper->didFailRedirectCheck();
229 void WorkerThreadableLoader::MainThreadBridge::didFailRedirectCheck()
231 m_loaderProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidFailRedirectCheck, m_workerClientWrapper), m_taskMode);
234 static void workerContextDidReceiveAuthenticationCancellation(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, PassOwnPtr<CrossThreadResourceResponseData> responseData)
236 ASSERT_UNUSED(context, context->isWorkerContext());
237 OwnPtr<ResourceResponse> response(ResourceResponse::adopt(responseData));
238 workerClientWrapper->didReceiveAuthenticationCancellation(*response);
241 void WorkerThreadableLoader::MainThreadBridge::didReceiveAuthenticationCancellation(const ResourceResponse& response)
243 m_loaderProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidReceiveAuthenticationCancellation, m_workerClientWrapper, response), m_taskMode);
246 } // namespace WebCore
248 #endif // ENABLE(WORKERS)