Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / loader / WorkerThreadableLoader.cpp
1 /*
2  * Copyright (C) 2009, 2010 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 #include "core/loader/WorkerThreadableLoader.h"
34
35 #include "core/dom/CrossThreadTask.h"
36 #include "core/dom/Document.h"
37 #include "core/loader/DocumentThreadableLoader.h"
38 #include "core/loader/WorkerLoaderClientBridgeSyncHelper.h"
39 #include "core/workers/WorkerGlobalScope.h"
40 #include "core/workers/WorkerLoaderProxy.h"
41 #include "core/workers/WorkerThread.h"
42 #include "platform/network/ResourceError.h"
43 #include "platform/network/ResourceRequest.h"
44 #include "platform/network/ResourceResponse.h"
45 #include "platform/weborigin/Referrer.h"
46 #include "public/platform/Platform.h"
47 #include "public/platform/WebWaitableEvent.h"
48 #include "wtf/MainThread.h"
49 #include "wtf/OwnPtr.h"
50 #include "wtf/Vector.h"
51
52 namespace WebCore {
53
54 WorkerThreadableLoader::WorkerThreadableLoader(WorkerGlobalScope* workerGlobalScope, PassRefPtr<ThreadableLoaderClientWrapper> clientWrapper, PassOwnPtr<ThreadableLoaderClient> clientBridge, const ResourceRequest& request, const ThreadableLoaderOptions& options)
55     : m_workerGlobalScope(workerGlobalScope)
56     , m_workerClientWrapper(clientWrapper)
57     , m_bridge(*(new MainThreadBridge(m_workerClientWrapper, clientBridge, m_workerGlobalScope->thread()->workerLoaderProxy(), request, options, workerGlobalScope->url().strippedForUseAsReferrer())))
58 {
59 }
60
61 WorkerThreadableLoader::~WorkerThreadableLoader()
62 {
63     m_bridge.destroy();
64 }
65
66 void WorkerThreadableLoader::loadResourceSynchronously(WorkerGlobalScope* workerGlobalScope, const ResourceRequest& request, ThreadableLoaderClient& client, const ThreadableLoaderOptions& options)
67 {
68     blink::WebWaitableEvent* shutdownEvent =
69         workerGlobalScope->thread()->shutdownEvent();
70     OwnPtr<blink::WebWaitableEvent> loaderDone =
71         adoptPtr(blink::Platform::current()->createWaitableEvent());
72
73     Vector<blink::WebWaitableEvent*> events;
74     events.append(shutdownEvent);
75     events.append(loaderDone.get());
76
77     RefPtr<ThreadableLoaderClientWrapper> clientWrapper(ThreadableLoaderClientWrapper::create(&client));
78     OwnPtr<WorkerLoaderClientBridgeSyncHelper> clientBridge(WorkerLoaderClientBridgeSyncHelper::create(client, loaderDone.release()));
79
80     // This must be valid while loader is around.
81     WorkerLoaderClientBridgeSyncHelper* clientBridgePtr = clientBridge.get();
82
83     RefPtr<WorkerThreadableLoader> loader = WorkerThreadableLoader::create(workerGlobalScope, clientWrapper, clientBridge.release(), request, options);
84
85     blink::WebWaitableEvent* signalled = blink::Platform::current()->waitMultipleEvents(events);
86     if (signalled == shutdownEvent) {
87         loader->cancel();
88         return;
89     }
90
91     clientBridgePtr->run();
92 }
93
94 void WorkerThreadableLoader::cancel()
95 {
96     m_bridge.cancel();
97 }
98
99 WorkerThreadableLoader::MainThreadBridge::MainThreadBridge(
100     PassRefPtr<ThreadableLoaderClientWrapper> workerClientWrapper,
101     PassOwnPtr<ThreadableLoaderClient> clientBridge,
102     WorkerLoaderProxy& loaderProxy,
103     const ResourceRequest& request, const ThreadableLoaderOptions& options, const String& outgoingReferrer)
104     : m_clientBridge(clientBridge)
105     , m_workerClientWrapper(workerClientWrapper)
106     , m_loaderProxy(loaderProxy)
107 {
108     ASSERT(m_workerClientWrapper.get());
109     ASSERT(m_clientBridge.get());
110     m_loaderProxy.postTaskToLoader(
111         createCallbackTask(&MainThreadBridge::mainThreadCreateLoader,
112                            AllowCrossThreadAccess(this), request, options, outgoingReferrer));
113 }
114
115 WorkerThreadableLoader::MainThreadBridge::~MainThreadBridge()
116 {
117 }
118
119 void WorkerThreadableLoader::MainThreadBridge::mainThreadCreateLoader(ExecutionContext* context, MainThreadBridge* thisPtr, PassOwnPtr<CrossThreadResourceRequestData> requestData, ThreadableLoaderOptions options, const String& outgoingReferrer)
120 {
121     ASSERT(isMainThread());
122     Document* document = toDocument(context);
123
124     OwnPtr<ResourceRequest> request(ResourceRequest::adopt(requestData));
125     request->setHTTPReferrer(Referrer(outgoingReferrer, ReferrerPolicyDefault));
126     options.requestInitiatorContext = WorkerContext;
127     thisPtr->m_mainThreadLoader = DocumentThreadableLoader::create(document, thisPtr, *request, options);
128     if (!thisPtr->m_mainThreadLoader) {
129         // DocumentThreadableLoader::create may return 0 when the document loader has been already changed.
130         thisPtr->didFail(ResourceError(errorDomainBlinkInternal, 0, request->url().string(), "Can't create DocumentThreadableLoader"));
131     }
132 }
133
134 void WorkerThreadableLoader::MainThreadBridge::mainThreadDestroy(ExecutionContext* context, MainThreadBridge* thisPtr)
135 {
136     ASSERT(isMainThread());
137     ASSERT_UNUSED(context, context->isDocument());
138     delete thisPtr;
139 }
140
141 void WorkerThreadableLoader::MainThreadBridge::destroy()
142 {
143     // Ensure that no more client callbacks are done in the worker context's thread.
144     clearClientWrapper();
145
146     // "delete this" and m_mainThreadLoader::deref() on the worker object's thread.
147     m_loaderProxy.postTaskToLoader(
148         createCallbackTask(&MainThreadBridge::mainThreadDestroy, AllowCrossThreadAccess(this)));
149 }
150
151 void WorkerThreadableLoader::MainThreadBridge::mainThreadCancel(ExecutionContext* context, MainThreadBridge* thisPtr)
152 {
153     ASSERT(isMainThread());
154     ASSERT_UNUSED(context, context->isDocument());
155
156     if (!thisPtr->m_mainThreadLoader)
157         return;
158     thisPtr->m_mainThreadLoader->cancel();
159     thisPtr->m_mainThreadLoader = 0;
160 }
161
162 void WorkerThreadableLoader::MainThreadBridge::cancel()
163 {
164     m_loaderProxy.postTaskToLoader(
165         createCallbackTask(&MainThreadBridge::mainThreadCancel, AllowCrossThreadAccess(this)));
166     ThreadableLoaderClientWrapper* clientWrapper = m_workerClientWrapper.get();
167     if (!clientWrapper->done()) {
168         // If the client hasn't reached a termination state, then transition it by sending a cancellation error.
169         // Note: no more client callbacks will be done after this method -- the clearClientWrapper() call ensures that.
170         ResourceError error(String(), 0, String(), String());
171         error.setIsCancellation(true);
172         clientWrapper->didFail(error);
173     }
174     clearClientWrapper();
175 }
176
177 void WorkerThreadableLoader::MainThreadBridge::clearClientWrapper()
178 {
179     m_workerClientWrapper->clearClient();
180 }
181
182 void WorkerThreadableLoader::MainThreadBridge::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
183 {
184     m_clientBridge->didSendData(bytesSent, totalBytesToBeSent);
185 }
186
187 void WorkerThreadableLoader::MainThreadBridge::didReceiveResponse(unsigned long identifier, const ResourceResponse& response)
188 {
189     m_clientBridge->didReceiveResponse(identifier, response);
190 }
191
192 void WorkerThreadableLoader::MainThreadBridge::didReceiveData(const char* data, int dataLength)
193 {
194     m_clientBridge->didReceiveData(data, dataLength);
195 }
196
197 void WorkerThreadableLoader::MainThreadBridge::didDownloadData(int dataLength)
198 {
199     m_clientBridge->didDownloadData(dataLength);
200 }
201
202 void WorkerThreadableLoader::MainThreadBridge::didReceiveCachedMetadata(const char* data, int dataLength)
203 {
204     m_clientBridge->didReceiveCachedMetadata(data, dataLength);
205 }
206
207 void WorkerThreadableLoader::MainThreadBridge::didFinishLoading(unsigned long identifier, double finishTime)
208 {
209     m_clientBridge->didFinishLoading(identifier, finishTime);
210 }
211
212 void WorkerThreadableLoader::MainThreadBridge::didFail(const ResourceError& error)
213 {
214     m_clientBridge->didFail(error);
215 }
216
217 void WorkerThreadableLoader::MainThreadBridge::didFailAccessControlCheck(const ResourceError& error)
218 {
219     m_clientBridge->didFailAccessControlCheck(error);
220 }
221
222 void WorkerThreadableLoader::MainThreadBridge::didFailRedirectCheck()
223 {
224     m_clientBridge->didFailRedirectCheck();
225 }
226
227 } // namespace WebCore