2009-02-23 David Levin <levin@chromium.org>
[WebKit-https.git] / WebCore / loader / WorkerThreadableLoader.cpp
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 "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"
45 #include <memory>
46 #include <wtf/OwnPtr.h>
47 #include <wtf/Threading.h>
48 #include <wtf/Vector.h>
49
50 using namespace std;
51
52 namespace WebCore {
53
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 ResourceRequest& request, LoadCallbacks callbacksSetting, ContentSniff contentSniff)
56     : m_workerContext(workerContext)
57     , m_bridge(*(new MainThreadBridge(client, *(static_cast<WorkerMessagingProxy*>(m_workerContext->thread()->workerObjectProxy())), request, callbacksSetting, contentSniff)))
58 {
59 }
60
61 WorkerThreadableLoader::~WorkerThreadableLoader()
62 {
63     m_bridge.destroy();
64 }
65
66 void WorkerThreadableLoader::cancel()
67 {
68     m_bridge.cancel();
69 }
70
71 WorkerThreadableLoader::MainThreadBridge::MainThreadBridge(ThreadableLoaderClient* workerClient, WorkerMessagingProxy& messagingProxy, const ResourceRequest& request, LoadCallbacks callbacksSetting, ContentSniff contentSniff)
72     : m_workerClientWrapper(ThreadableLoaderClientWrapper::create(workerClient))
73     , m_messagingProxy(messagingProxy)
74 {
75     ASSERT(workerClient);
76     m_messagingProxy.postTaskToWorkerObject(createCallbackTask(&MainThreadBridge::mainThreadCreateLoader, this, request, callbacksSetting, contentSniff));
77 }
78
79 WorkerThreadableLoader::MainThreadBridge::~MainThreadBridge()
80 {
81 }
82
83 void WorkerThreadableLoader::MainThreadBridge::mainThreadCreateLoader(ScriptExecutionContext* context, MainThreadBridge* thisPtr, auto_ptr<CrossThreadResourceRequestData> requestData, LoadCallbacks callbacksSetting, ContentSniff contentSniff)
84 {
85     // FIXME: This assert fails for nested workers.  Removing the assert would allow it to work,
86     // but then there would be one WorkerThreadableLoader in every intermediate worker simply
87     // chaining the requests, which is not very good. Instead, the postTaskToWorkerObject should be a
88     // postTaskToDocumentContext.
89     ASSERT(isMainThread());
90     ASSERT(context->isDocument());
91
92     if (thisPtr->m_messagingProxy.askedToTerminate())
93         return;
94
95     // FIXME: the created loader has no knowledge of the origin of the worker doing the load request.
96     // Basically every setting done in SubresourceLoader::create (including the contents of addExtraFieldsToRequest)
97     // needs to be examined for how it should take into account a different originator.
98     OwnPtr<ResourceRequest> request(ResourceRequest::adopt(requestData));
99     // FIXME: If the a site requests a local resource, then this will return a non-zero value but the sync path
100     // will return a 0 value.  Either this should return 0 or the other code path should do a callback with
101     // a failure.
102     thisPtr->m_mainThreadLoader = ThreadableLoader::create(context, thisPtr, *request, callbacksSetting, contentSniff);
103     ASSERT(thisPtr->m_mainThreadLoader);
104 }
105
106 void WorkerThreadableLoader::MainThreadBridge::mainThreadDestroy(ScriptExecutionContext* context, MainThreadBridge* thisPtr)
107 {
108     ASSERT(isMainThread());
109     ASSERT_UNUSED(context, context->isDocument());
110     delete thisPtr;
111 }
112
113 void WorkerThreadableLoader::MainThreadBridge::destroy()
114 {
115     // Ensure that no more client callbacks are done in the worker context's thread.
116     clearClientWrapper();
117
118     // "delete this" and m_mainThreadLoader::deref() on the worker object's thread.
119     m_messagingProxy.postTaskToWorkerObject(createCallbackTask(&MainThreadBridge::mainThreadDestroy, this));
120 }
121
122 void WorkerThreadableLoader::MainThreadBridge::mainThreadCancel(ScriptExecutionContext* context, MainThreadBridge* thisPtr)
123 {
124     ASSERT(isMainThread());
125     ASSERT_UNUSED(context, context->isDocument());
126
127     if (!thisPtr->m_mainThreadLoader)
128         return;
129     thisPtr->m_mainThreadLoader->cancel();
130     thisPtr->m_mainThreadLoader = 0;
131 }
132
133 void WorkerThreadableLoader::MainThreadBridge::cancel()
134 {
135     m_messagingProxy.postTaskToWorkerObject(createCallbackTask(&MainThreadBridge::mainThreadCancel, this));
136     clearClientWrapper();
137 }
138
139 void WorkerThreadableLoader::MainThreadBridge::clearClientWrapper()
140 {
141     static_cast<ThreadableLoaderClientWrapper*>(m_workerClientWrapper.get())->clearClient();
142 }
143
144 static void workerContextDidSendData(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
145 {
146     ASSERT_UNUSED(context, context->isWorkerContext());
147     workerClientWrapper->didSendData(bytesSent, totalBytesToBeSent);
148 }
149
150 void WorkerThreadableLoader::MainThreadBridge::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
151 {
152     m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDidSendData, m_workerClientWrapper, bytesSent, totalBytesToBeSent));
153 }
154
155 static void workerContextDidReceiveResponse(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, auto_ptr<CrossThreadResourceResponseData> responseData)
156 {
157     ASSERT_UNUSED(context, context->isWorkerContext());
158     OwnPtr<ResourceResponse> response(ResourceResponse::adopt(responseData));
159     workerClientWrapper->didReceiveResponse(*response);
160 }
161
162 void WorkerThreadableLoader::MainThreadBridge::didReceiveResponse(const ResourceResponse& response)
163 {
164     m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDidReceiveResponse, m_workerClientWrapper, response));
165 }
166
167 static void workerContextDidReceiveData(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, auto_ptr<Vector<char> > vectorData)
168 {
169     ASSERT_UNUSED(context, context->isWorkerContext());
170     workerClientWrapper->didReceiveData(vectorData->data(), vectorData->size());
171 }
172
173 void WorkerThreadableLoader::MainThreadBridge::didReceiveData(const char* data, int lengthReceived)
174 {
175     auto_ptr<Vector<char> > vector(new Vector<char>(lengthReceived)); // needs to be an auto_ptr for usage with createCallbackTask.
176     memcpy(vector->data(), data, lengthReceived);
177     m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDidReceiveData, m_workerClientWrapper, vector));
178 }
179
180 static void workerContextDidFinishLoading(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, int identifier)
181 {
182     ASSERT_UNUSED(context, context->isWorkerContext());
183     workerClientWrapper->didFinishLoading(identifier);
184 }
185
186 void WorkerThreadableLoader::MainThreadBridge::didFinishLoading(int identifier)
187 {
188     m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDidFinishLoading, m_workerClientWrapper, identifier));
189 }
190
191 static void workerContextDidFail(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, const ResourceError& error)
192 {
193     ASSERT_UNUSED(context, context->isWorkerContext());
194     workerClientWrapper->didFail(error);
195 }
196
197 void WorkerThreadableLoader::MainThreadBridge::didFail(const ResourceError& error)
198 {
199     m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDidFail, m_workerClientWrapper, error));
200 }
201
202 static void workerContextDidFailRedirectCheck(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper)
203 {
204     ASSERT_UNUSED(context, context->isWorkerContext());
205     workerClientWrapper->didFailRedirectCheck();
206 }
207
208 void WorkerThreadableLoader::MainThreadBridge::didFailRedirectCheck()
209 {
210     m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDidFailRedirectCheck, m_workerClientWrapper));
211 }
212
213 static void workerContextDidReceiveAuthenticationCancellation(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, auto_ptr<CrossThreadResourceResponseData> responseData)
214 {
215     ASSERT_UNUSED(context, context->isWorkerContext());
216     OwnPtr<ResourceResponse> response(ResourceResponse::adopt(responseData));
217     workerClientWrapper->didReceiveAuthenticationCancellation(*response);
218 }
219
220 void WorkerThreadableLoader::MainThreadBridge::didReceiveAuthenticationCancellation(const ResourceResponse& response)
221 {
222     m_messagingProxy.postTaskToWorkerContext(createCallbackTask(&workerContextDidReceiveAuthenticationCancellation, m_workerClientWrapper, response));
223 }
224
225 } // namespace WebCore
226
227 #endif // ENABLE(WORKERS)