1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
/*
* Copyright 2010, The Android Open Source Project
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WebUrlLoaderClient.h"
#include "OwnPtr.h"
#include "ResourceHandle.h"
#include "ResourceHandleClient.h"
#include "ResourceResponse.h"
#include "WebRequest.h"
#include "WebResourceRequest.h"
#include <base/condition_variable.h>
#include <base/lock.h>
#include <base/thread.h>
#include <net/base/io_buffer.h>
namespace android {
LoaderData::~LoaderData()
{
if (buffer)
buffer->Release();
}
base::Thread* WebUrlLoaderClient::ioThread()
{
static base::Thread* networkThread = 0;
if (!networkThread)
networkThread = new base::Thread("network");
if (!networkThread)
return 0;
if (networkThread->IsRunning())
return networkThread;
base::Thread::Options options;
options.message_loop_type = MessageLoop::TYPE_IO;
if (!networkThread->StartWithOptions(options)) {
delete networkThread;
networkThread = 0;
}
return networkThread;
}
Lock* WebUrlLoaderClient::syncLock() {
static Lock s_syncLock;
return &s_syncLock;
}
ConditionVariable* WebUrlLoaderClient::syncCondition() {
static ConditionVariable s_syncCondition(syncLock());
return &s_syncCondition;
}
WebUrlLoaderClient::~WebUrlLoaderClient()
{
base::Thread* thread = ioThread();
if (thread)
thread->message_loop()->ReleaseSoon(FROM_HERE, m_request);
}
bool WebUrlLoaderClient::isActive() const
{
if (m_cancelling)
return false;
if (!m_resourceHandle->client())
return false;
return true;
}
WebUrlLoaderClient::WebUrlLoaderClient(WebCore::ResourceHandle* resourceHandle, const WebCore::ResourceRequest& resourceRequest)
: m_resourceHandle(resourceHandle)
, m_cancelling(false)
, m_sync(false)
, m_finished(false)
{
WebResourceRequest webResourceRequest(resourceRequest);
m_request = new WebRequest(this, webResourceRequest);
m_request->AddRef(); // Matched by ReleaseSoon in destructor
base::Thread* thread = ioThread();
// Set uploads before start is called on the request
if (resourceRequest.httpBody() && !(webResourceRequest.method() == "GET" || webResourceRequest.method() == "HEAD")) {
Vector<FormDataElement>::iterator iter;
Vector<FormDataElement> elements = resourceRequest.httpBody()->elements();
for (iter = elements.begin(); iter != elements.end(); iter++) {
FormDataElement element = *iter;
switch (element.m_type) {
case FormDataElement::data:
if (!element.m_data.isEmpty()) {
// WebKit sometimes gives up empty data to append. These aren't
// necessary so we just optimize those out here.
int size = static_cast<int>(element.m_data.size());
// TODO: do we have to make a copy of this data?
base::Thread* thread = ioThread();
if (thread)
thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(m_request, &WebRequest::AppendBytesToUpload, element.m_data.data(), size));
}
break;
#if ENABLE(BLOB)
case FormDataElement::encodedFile:
if (element.m_fileLength == -1)
continue; // TODO: Not supporting directories yet
else {
// TODO: Add fileuploads after Google log-in is fixed.
// Chrome code is here: webkit/glue/weburlloader_impl.cc:391
}
break;
#endif
default:
// TODO: Add a warning/DCHECK/assert here, should never happen
break;
}
}
}
}
bool WebUrlLoaderClient::start(bool sync)
{
base::Thread* thread = ioThread();
if (!thread) {
return false;
}
m_sync = sync;
if (m_sync) {
AutoLock autoLock(*syncLock());
thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(m_request, &WebRequest::start));
// Run callbacks until the queue is exhausted and m_finished is true.
while(!m_finished) {
while (!m_queue.empty()) {
Callback& callback = m_queue.front();
CallbackFunction* function = callback.a;
void* context = callback.b;
(*function)(context);
m_queue.pop_front();
}
if (m_queue.empty() && !m_finished) {
syncCondition()->Wait();
}
}
// This may be the last reference to us, so we may be deleted now.
// Don't access any more member variables after releasing this reference.
m_resourceHandle = 0;
} else {
// Asynchronous start.
thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(m_request, &WebRequest::start));
}
return true;
}
void WebUrlLoaderClient::cancel()
{
m_cancelling = true;
base::Thread* thread = ioThread();
if (thread)
thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(m_request, &WebRequest::cancel));
}
void WebUrlLoaderClient::finish()
{
m_finished = true;
if (!m_sync) {
// This is the last reference to us, so we will be deleted now.
// We only release the reference here if start() was called asynchronously!
m_resourceHandle = 0;
}
}
// This is called from the IO thread, and dispatches the callback to the main thread.
void WebUrlLoaderClient::maybeCallOnMainThread(CallbackFunction* function, void* context) {
if (m_sync) {
AutoLock autoLock(*syncLock());
if (m_queue.empty()) {
syncCondition()->Broadcast();
}
m_queue.push_back(Callback(function, context));
} else {
// Let WebKit handle it.
callOnMainThread(function, context);
}
}
// Response methods
// static - on main thread
void WebUrlLoaderClient::didReceiveResponse(void* data)
{
OwnPtr<LoaderData> loaderData(static_cast<LoaderData*>(data));
const WebUrlLoaderClient* loader = loaderData->loader;
WebResponse webResponse = loaderData->webResponse;
if (!loader->isActive())
return;
loader->m_resourceHandle->client()->didReceiveResponse(loader->m_resourceHandle.get(), webResponse.createResourceResponse());
}
// static - on main thread
void WebUrlLoaderClient::didReceiveData(void* data)
{
OwnPtr<LoaderData> loaderData(static_cast<LoaderData*>(data));
const WebUrlLoaderClient* loader = loaderData->loader;
net::IOBuffer* buf = loaderData->buffer;
if (!loader->isActive())
return;
// didReceiveData will take a copy of the data
if (loader->m_resourceHandle && loader->m_resourceHandle->client())
loader->m_resourceHandle->client()->didReceiveData(loader->m_resourceHandle.get(), buf->data(), loaderData->size, loaderData->size);
}
// static - on main thread
// For data url's
void WebUrlLoaderClient::didReceiveDataUrl(void* data)
{
OwnPtr<LoaderData> ld(static_cast<LoaderData*>(data));
const WebUrlLoaderClient* loader = ld->loader;
if (!loader->isActive())
return;
std::string* str = ld->string.get();
// didReceiveData will take a copy of the data
loader->m_resourceHandle->client()->didReceiveData(loader->m_resourceHandle.get(), str->data(), str->size(), str->size());
}
// static - on main thread
void WebUrlLoaderClient::didFail(void* data)
{
OwnPtr<LoaderData> loaderData(static_cast<LoaderData*>(data));
WebUrlLoaderClient* loader = loaderData->loader;
if (loader->isActive())
loader->m_resourceHandle->client()->didFail(loader->m_resourceHandle.get(), loaderData->webResponse.createResourceError());
// Always finish a request, if not it will leak
loader->finish();
}
// static - on main thread
void WebUrlLoaderClient::willSendRequest(void* data)
{
OwnPtr<LoaderData> loaderData(static_cast<LoaderData*>(data));
const WebUrlLoaderClient* loader = loaderData->loader;
if (!loader->isActive())
return;
WebResponse webResponse = loaderData->webResponse;
OwnPtr<WebCore::ResourceRequest> resourceRequest(new WebCore::ResourceRequest(webResponse.url()));
loader->m_resourceHandle->client()->willSendRequest(loader->m_resourceHandle.get(), *resourceRequest, webResponse.createResourceResponse());
}
// static - on main thread
void WebUrlLoaderClient::didFinishLoading(void* data)
{
OwnPtr<LoaderData> loaderData(static_cast<LoaderData*>(data));
WebUrlLoaderClient* loader = loaderData->loader;
if (loader->isActive())
loader->m_resourceHandle->client()->didFinishLoading(loader->m_resourceHandle.get());
// Always finish a request, if not it will leak
loader->finish();
}
} // namespace android
|