d801c2c5c21c9b84270cadbfec79e08b66ff2465
[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 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)))
59 {
60 }
61
62 WorkerThreadableLoader::~WorkerThreadableLoader()
63 {
64     m_bridge.destroy();
65 }
66
67 void WorkerThreadableLoader::cancel()
68 {
69     m_bridge.cancel();
70 }
71
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())
77 {
78     ASSERT(workerClient);
79     m_messagingProxy.postTaskToWorkerObject(createCallbackTask(&MainThreadBridge::mainThreadCreateLoader, this, request, callbacksSetting, contentSniff));
80 }
81
82 WorkerThreadableLoader::MainThreadBridge::~MainThreadBridge()
83 {
84 }
85
86 void WorkerThreadableLoader::MainThreadBridge::mainThreadCreateLoader(ScriptExecutionContext* context, MainThreadBridge* thisPtr, auto_ptr<CrossThreadResourceRequestData> requestData, LoadCallbacks callbacksSetting, ContentSniff contentSniff)
87 {
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());
94
95     if (thisPtr->m_messagingProxy.askedToTerminate())
96         return;
97
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
104     // a failure.
105     thisPtr->m_mainThreadLoader = ThreadableLoader::create(context, thisPtr, *request, callbacksSetting, contentSniff);
106     ASSERT(thisPtr->m_mainThreadLoader);
107 }
108
109 void WorkerThreadableLoader::MainThreadBridge::mainThreadDestroy(ScriptExecutionContext* context, MainThreadBridge* thisPtr)
110 {
111     ASSERT(isMainThread());
112     ASSERT_UNUSED(context, context->isDocument());
113     delete thisPtr;
114 }
115
116 void WorkerThreadableLoader::MainThreadBridge::destroy()
117 {
118     // Ensure that no more client callbacks are done in the worker context's thread.
119     clearClientWrapper();
120
121     // "delete this" and m_mainThreadLoader::deref() on the worker object's thread.
122     m_messagingProxy.postTaskToWorkerObject(createCallbackTask(&MainThreadBridge::mainThreadDestroy, this));
123 }
124
125 void WorkerThreadableLoader::MainThreadBridge::mainThreadCancel(ScriptExecutionContext* context, MainThreadBridge* thisPtr)
126 {
127     ASSERT(isMainThread());
128     ASSERT_UNUSED(context, context->isDocument());
129
130     if (!thisPtr->m_mainThreadLoader)
131         return;
132     thisPtr->m_mainThreadLoader->cancel();
133     thisPtr->m_mainThreadLoader = 0;
134 }
135
136 void WorkerThreadableLoader::MainThreadBridge::cancel()
137 {
138     m_messagingProxy.postTaskToWorkerObject(createCallbackTask(&MainThreadBridge::mainThreadCancel, this));
139     clearClientWrapper();
140 }
141
142 void WorkerThreadableLoader::MainThreadBridge::clearClientWrapper()
143 {
144     static_cast<ThreadableLoaderClientWrapper*>(m_workerClientWrapper.get())->clearClient();
145 }
146
147 static void workerContextDidSendData(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
148 {
149     ASSERT_UNUSED(context, context->isWorkerContext());
150     workerClientWrapper->didSendData(bytesSent, totalBytesToBeSent);
151 }
152
153 void WorkerThreadableLoader::MainThreadBridge::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
154 {
155     m_messagingProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidSendData, m_workerClientWrapper, bytesSent, totalBytesToBeSent), m_taskMode);
156 }
157
158 static void workerContextDidReceiveResponse(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, auto_ptr<CrossThreadResourceResponseData> responseData)
159 {
160     ASSERT_UNUSED(context, context->isWorkerContext());
161     OwnPtr<ResourceResponse> response(ResourceResponse::adopt(responseData));
162     workerClientWrapper->didReceiveResponse(*response);
163 }
164
165 void WorkerThreadableLoader::MainThreadBridge::didReceiveResponse(const ResourceResponse& response)
166 {
167     m_messagingProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidReceiveResponse, m_workerClientWrapper, response), m_taskMode);
168 }
169
170 static void workerContextDidReceiveData(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, auto_ptr<Vector<char> > vectorData)
171 {
172     ASSERT_UNUSED(context, context->isWorkerContext());
173     workerClientWrapper->didReceiveData(vectorData->data(), vectorData->size());
174 }
175
176 void WorkerThreadableLoader::MainThreadBridge::didReceiveData(const char* data, int lengthReceived)
177 {
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);
181 }
182
183 static void workerContextDidFinishLoading(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, int identifier)
184 {
185     ASSERT_UNUSED(context, context->isWorkerContext());
186     workerClientWrapper->didFinishLoading(identifier);
187 }
188
189 void WorkerThreadableLoader::MainThreadBridge::didFinishLoading(int identifier)
190 {
191     m_messagingProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidFinishLoading, m_workerClientWrapper, identifier), m_taskMode);
192 }
193
194 static void workerContextDidFail(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, const ResourceError& error)
195 {
196     ASSERT_UNUSED(context, context->isWorkerContext());
197     workerClientWrapper->didFail(error);
198 }
199
200 void WorkerThreadableLoader::MainThreadBridge::didFail(const ResourceError& error)
201 {
202     m_messagingProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidFail, m_workerClientWrapper, error), m_taskMode);
203 }
204
205 static void workerContextDidFailRedirectCheck(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper)
206 {
207     ASSERT_UNUSED(context, context->isWorkerContext());
208     workerClientWrapper->didFailRedirectCheck();
209 }
210
211 void WorkerThreadableLoader::MainThreadBridge::didFailRedirectCheck()
212 {
213     m_messagingProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidFailRedirectCheck, m_workerClientWrapper), m_taskMode);
214 }
215
216 static void workerContextDidReceiveAuthenticationCancellation(ScriptExecutionContext* context, RefPtr<ThreadableLoaderClientWrapper> workerClientWrapper, auto_ptr<CrossThreadResourceResponseData> responseData)
217 {
218     ASSERT_UNUSED(context, context->isWorkerContext());
219     OwnPtr<ResourceResponse> response(ResourceResponse::adopt(responseData));
220     workerClientWrapper->didReceiveAuthenticationCancellation(*response);
221 }
222
223 void WorkerThreadableLoader::MainThreadBridge::didReceiveAuthenticationCancellation(const ResourceResponse& response)
224 {
225     m_messagingProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidReceiveAuthenticationCancellation, m_workerClientWrapper, response), m_taskMode);
226 }
227
228 } // namespace WebCore
229
230 #endif // ENABLE(WORKERS)