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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "webkit/plugins/ppapi/ppb_url_loader_impl.h"
#include "base/logging.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/ppb_url_loader.h"
#include "ppapi/c/trusted/ppb_url_loader_trusted.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebKitClient.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoaderOptions.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h"
#include "webkit/appcache/web_application_cache_host_impl.h"
#include "webkit/plugins/ppapi/common.h"
#include "webkit/plugins/ppapi/plugin_module.h"
#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
#include "webkit/plugins/ppapi/ppb_url_request_info_impl.h"
#include "webkit/plugins/ppapi/ppb_url_response_info_impl.h"
using appcache::WebApplicationCacheHostImpl;
using WebKit::WebFrame;
using WebKit::WebString;
using WebKit::WebURL;
using WebKit::WebURLError;
using WebKit::WebURLLoader;
using WebKit::WebURLLoaderOptions;
using WebKit::WebURLRequest;
using WebKit::WebURLResponse;
#ifdef _MSC_VER
// Do not warn about use of std::copy with raw pointers.
#pragma warning(disable : 4996)
#endif
namespace webkit {
namespace ppapi {
namespace {
PP_Resource Create(PP_Instance instance_id) {
PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
if (!instance)
return 0;
PPB_URLLoader_Impl* loader = new PPB_URLLoader_Impl(instance, false);
return loader->GetReference();
}
PP_Bool IsURLLoader(PP_Resource resource) {
return BoolToPPBool(!!Resource::GetAs<PPB_URLLoader_Impl>(resource));
}
int32_t Open(PP_Resource loader_id,
PP_Resource request_id,
PP_CompletionCallback callback) {
scoped_refptr<PPB_URLLoader_Impl> loader(
Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
if (!loader)
return PP_ERROR_BADRESOURCE;
scoped_refptr<PPB_URLRequestInfo_Impl> request(
Resource::GetAs<PPB_URLRequestInfo_Impl>(request_id));
if (!request)
return PP_ERROR_BADRESOURCE;
return loader->Open(request, callback);
}
int32_t FollowRedirect(PP_Resource loader_id,
PP_CompletionCallback callback) {
scoped_refptr<PPB_URLLoader_Impl> loader(
Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
if (!loader)
return PP_ERROR_BADRESOURCE;
return loader->FollowRedirect(callback);
}
PP_Bool GetUploadProgress(PP_Resource loader_id,
int64_t* bytes_sent,
int64_t* total_bytes_to_be_sent) {
scoped_refptr<PPB_URLLoader_Impl> loader(
Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
if (!loader)
return PP_FALSE;
return BoolToPPBool(loader->GetUploadProgress(bytes_sent,
total_bytes_to_be_sent));
}
PP_Bool GetDownloadProgress(PP_Resource loader_id,
int64_t* bytes_received,
int64_t* total_bytes_to_be_received) {
scoped_refptr<PPB_URLLoader_Impl> loader(
Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
if (!loader)
return PP_FALSE;
return BoolToPPBool(loader->GetDownloadProgress(bytes_received,
total_bytes_to_be_received));
}
PP_Resource GetResponseInfo(PP_Resource loader_id) {
scoped_refptr<PPB_URLLoader_Impl> loader(
Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
if (!loader)
return 0;
PPB_URLResponseInfo_Impl* response_info = loader->response_info();
if (!response_info)
return 0;
return response_info->GetReference();
}
int32_t ReadResponseBody(PP_Resource loader_id,
void* buffer,
int32_t bytes_to_read,
PP_CompletionCallback callback) {
scoped_refptr<PPB_URLLoader_Impl> loader(
Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
if (!loader)
return PP_ERROR_BADRESOURCE;
return loader->ReadResponseBody(buffer, bytes_to_read, callback);
}
int32_t FinishStreamingToFile(PP_Resource loader_id,
PP_CompletionCallback callback) {
scoped_refptr<PPB_URLLoader_Impl> loader(
Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
if (!loader)
return PP_ERROR_BADRESOURCE;
return loader->FinishStreamingToFile(callback);
}
void Close(PP_Resource loader_id) {
scoped_refptr<PPB_URLLoader_Impl> loader(
Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
if (!loader)
return;
loader->Close();
}
const PPB_URLLoader ppb_urlloader = {
&Create,
&IsURLLoader,
&Open,
&FollowRedirect,
&GetUploadProgress,
&GetDownloadProgress,
&GetResponseInfo,
&ReadResponseBody,
&FinishStreamingToFile,
&Close
};
void GrantUniversalAccess(PP_Resource loader_id) {
scoped_refptr<PPB_URLLoader_Impl> loader(
Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
if (!loader)
return;
loader->GrantUniversalAccess();
}
void SetStatusCallback(PP_Resource loader_id,
PP_URLLoaderTrusted_StatusCallback cb) {
scoped_refptr<PPB_URLLoader_Impl> loader(
Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
if (!loader)
return;
loader->SetStatusCallback(cb);
}
const PPB_URLLoaderTrusted ppb_urlloadertrusted = {
&GrantUniversalAccess,
&SetStatusCallback
};
WebFrame* GetFrame(PluginInstance* instance) {
return instance->container()->element().document().frame();
}
} // namespace
PPB_URLLoader_Impl::PPB_URLLoader_Impl(PluginInstance* instance,
bool main_document_loader)
: Resource(instance),
main_document_loader_(main_document_loader),
pending_callback_(),
bytes_sent_(0),
total_bytes_to_be_sent_(-1),
bytes_received_(0),
total_bytes_to_be_received_(-1),
user_buffer_(NULL),
user_buffer_size_(0),
done_status_(PP_OK_COMPLETIONPENDING),
is_streaming_to_file_(false),
is_asynchronous_load_suspended_(false),
has_universal_access_(false),
status_callback_(NULL) {
}
PPB_URLLoader_Impl::~PPB_URLLoader_Impl() {
}
// static
const PPB_URLLoader* PPB_URLLoader_Impl::GetInterface() {
return &ppb_urlloader;
}
// static
const PPB_URLLoaderTrusted* PPB_URLLoader_Impl::GetTrustedInterface() {
return &ppb_urlloadertrusted;
}
PPB_URLLoader_Impl* PPB_URLLoader_Impl::AsPPB_URLLoader_Impl() {
return this;
}
void PPB_URLLoader_Impl::LastPluginRefWasDeleted(bool instance_destroyed) {
Resource::LastPluginRefWasDeleted(instance_destroyed);
if (instance_destroyed) {
// Free the WebKit request when the instance has been destroyed to avoid
// using bandwidth just in case this object lives longer than the instance.
loader_.reset();
}
}
int32_t PPB_URLLoader_Impl::Open(PPB_URLRequestInfo_Impl* request,
PP_CompletionCallback callback) {
int32_t rv = ValidateCallback(callback);
if (rv != PP_OK)
return rv;
if (request->RequiresUniversalAccess() && !has_universal_access_)
return PP_ERROR_BADARGUMENT;
if (loader_.get())
return PP_ERROR_INPROGRESS;
WebFrame* frame = GetFrame(instance());
if (!frame)
return PP_ERROR_FAILED;
WebURLRequest web_request(request->ToWebURLRequest(frame));
WebURLLoaderOptions options;
if (has_universal_access_) {
// Universal access allows cross-origin requests and sends credentials.
options.crossOriginRequestPolicy =
WebURLLoaderOptions::CrossOriginRequestPolicyAllow;
options.allowCredentials = true;
} else if (request->allow_cross_origin_requests()) {
// Otherwise, allow cross-origin requests with access control.
options.crossOriginRequestPolicy =
WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl;
options.allowCredentials = request->allow_credentials();
}
is_asynchronous_load_suspended_ = false;
loader_.reset(frame->createAssociatedURLLoader(options));
if (!loader_.get())
return PP_ERROR_FAILED;
loader_->loadAsynchronously(web_request, this);
// Check for immediate failure; The AssociatedURLLoader will call our
// didFail method synchronously for certain kinds of access violations
// so we must return an error to the caller.
// TODO(bbudge) Modify the underlying AssociatedURLLoader to only call
// back asynchronously.
if (done_status_ == PP_ERROR_FAILED)
return PP_ERROR_NOACCESS;
request_info_ = scoped_refptr<PPB_URLRequestInfo_Impl>(request);
// Notify completion when we receive a redirect or response headers.
RegisterCallback(callback);
return PP_OK_COMPLETIONPENDING;
}
int32_t PPB_URLLoader_Impl::FollowRedirect(PP_CompletionCallback callback) {
int32_t rv = ValidateCallback(callback);
if (rv != PP_OK)
return rv;
WebURL redirect_url = GURL(response_info_->redirect_url());
loader_->setDefersLoading(false); // Allow the redirect to continue.
RegisterCallback(callback);
return PP_OK_COMPLETIONPENDING;
}
bool PPB_URLLoader_Impl::GetUploadProgress(int64_t* bytes_sent,
int64_t* total_bytes_to_be_sent) {
if (!RecordUploadProgress()) {
*bytes_sent = 0;
*total_bytes_to_be_sent = 0;
return false;
}
*bytes_sent = bytes_sent_;
*total_bytes_to_be_sent = total_bytes_to_be_sent_;
return true;
}
bool PPB_URLLoader_Impl::GetDownloadProgress(
int64_t* bytes_received,
int64_t* total_bytes_to_be_received) {
if (!RecordDownloadProgress()) {
*bytes_received = 0;
*total_bytes_to_be_received = 0;
return false;
}
*bytes_received = bytes_received_;
*total_bytes_to_be_received = total_bytes_to_be_received_;
return true;
}
int32_t PPB_URLLoader_Impl::ReadResponseBody(void* buffer,
int32_t bytes_to_read,
PP_CompletionCallback callback) {
int32_t rv = ValidateCallback(callback);
if (rv != PP_OK)
return rv;
if (!response_info_ || response_info_->body())
return PP_ERROR_FAILED;
if (bytes_to_read <= 0 || !buffer)
return PP_ERROR_BADARGUMENT;
user_buffer_ = static_cast<char*>(buffer);
user_buffer_size_ = bytes_to_read;
if (!buffer_.empty())
return FillUserBuffer();
// We may have already reached EOF.
if (done_status_ != PP_OK_COMPLETIONPENDING) {
user_buffer_ = NULL;
user_buffer_size_ = 0;
return done_status_;
}
RegisterCallback(callback);
return PP_OK_COMPLETIONPENDING;
}
int32_t PPB_URLLoader_Impl::FinishStreamingToFile(
PP_CompletionCallback callback) {
int32_t rv = ValidateCallback(callback);
if (rv != PP_OK)
return rv;
if (!response_info_ || !response_info_->body())
return PP_ERROR_FAILED;
// We may have already reached EOF.
if (done_status_ != PP_OK_COMPLETIONPENDING)
return done_status_;
is_streaming_to_file_ = true;
if (is_asynchronous_load_suspended_) {
loader_->setDefersLoading(false);
is_asynchronous_load_suspended_ = false;
}
// Wait for didFinishLoading / didFail.
RegisterCallback(callback);
return PP_OK_COMPLETIONPENDING;
}
void PPB_URLLoader_Impl::Close() {
if (loader_.get()) {
loader_->cancel();
} else if (main_document_loader_) {
WebFrame* frame = instance()->container()->element().document().frame();
frame->stopLoading();
}
// TODO(viettrungluu): Check what happens to the callback (probably the
// wrong thing). May need to post abort here. crbug.com/69457
}
void PPB_URLLoader_Impl::GrantUniversalAccess() {
has_universal_access_ = true;
}
void PPB_URLLoader_Impl::SetStatusCallback(
PP_URLLoaderTrusted_StatusCallback cb) {
status_callback_ = cb;
}
void PPB_URLLoader_Impl::willSendRequest(
WebURLLoader* loader,
WebURLRequest& new_request,
const WebURLResponse& redirect_response) {
if (!request_info_->follow_redirects()) {
SaveResponse(redirect_response);
loader_->setDefersLoading(true);
RunCallback(PP_OK);
}
}
void PPB_URLLoader_Impl::didSendData(
WebURLLoader* loader,
unsigned long long bytes_sent,
unsigned long long total_bytes_to_be_sent) {
// TODO(darin): Bounds check input?
bytes_sent_ = static_cast<int64_t>(bytes_sent);
total_bytes_to_be_sent_ = static_cast<int64_t>(total_bytes_to_be_sent);
UpdateStatus();
}
void PPB_URLLoader_Impl::didReceiveResponse(WebURLLoader* loader,
const WebURLResponse& response) {
SaveResponse(response);
// Sets -1 if the content length is unknown.
total_bytes_to_be_received_ = response.expectedContentLength();
UpdateStatus();
RunCallback(PP_OK);
}
void PPB_URLLoader_Impl::didDownloadData(WebURLLoader* loader,
int data_length) {
bytes_received_ += data_length;
UpdateStatus();
}
void PPB_URLLoader_Impl::didReceiveData(WebURLLoader* loader,
const char* data,
int data_length,
int encoded_data_length) {
bytes_received_ += data_length;
buffer_.insert(buffer_.end(), data, data + data_length);
if (user_buffer_) {
RunCallback(FillUserBuffer());
} else {
DCHECK(!pending_callback_.get() || pending_callback_->completed());
}
// To avoid letting the network stack download an entire stream all at once,
// defer loading when we have enough buffer.
// Check the buffer size after potentially moving some to the user buffer.
DCHECK(request_info_->prefetch_buffer_lower_threshold() <
request_info_->prefetch_buffer_upper_threshold());
if (!is_streaming_to_file_ &&
!is_asynchronous_load_suspended_ &&
buffer_.size() >= static_cast<size_t>(
request_info_->prefetch_buffer_upper_threshold())) {
DVLOG(1) << "Suspending async load - buffer size: " << buffer_.size();
loader->setDefersLoading(true);
is_asynchronous_load_suspended_ = true;
}
}
void PPB_URLLoader_Impl::didFinishLoading(WebURLLoader* loader,
double finish_time) {
done_status_ = PP_OK;
RunCallback(done_status_);
}
void PPB_URLLoader_Impl::didFail(WebURLLoader* loader,
const WebURLError& error) {
// TODO(darin): Provide more detailed error information.
done_status_ = PP_ERROR_FAILED;
RunCallback(done_status_);
}
int32_t PPB_URLLoader_Impl::ValidateCallback(PP_CompletionCallback callback) {
// We only support non-blocking calls.
if (!callback.func)
return PP_ERROR_BADARGUMENT;
if (pending_callback_.get() && !pending_callback_->completed())
return PP_ERROR_INPROGRESS;
return PP_OK;
}
void PPB_URLLoader_Impl::RegisterCallback(PP_CompletionCallback callback) {
DCHECK(callback.func);
DCHECK(!pending_callback_.get() || pending_callback_->completed());
PP_Resource resource_id = GetReferenceNoAddRef();
CHECK(resource_id);
pending_callback_ = new TrackedCompletionCallback(
instance()->module()->GetCallbackTracker(), resource_id, callback);
}
void PPB_URLLoader_Impl::RunCallback(int32_t result) {
// This may be null only when this is a main document loader.
if (!pending_callback_.get()) {
// TODO(viettrungluu): put this CHECK back when the callback race condition
// is fixed: http://code.google.com/p/chromium/issues/detail?id=70347.
//CHECK(main_document_loader_);
return;
}
scoped_refptr<TrackedCompletionCallback> callback;
callback.swap(pending_callback_);
callback->Run(result); // Will complete abortively if necessary.
}
size_t PPB_URLLoader_Impl::FillUserBuffer() {
DCHECK(user_buffer_);
DCHECK(user_buffer_size_);
size_t bytes_to_copy = std::min(buffer_.size(), user_buffer_size_);
std::copy(buffer_.begin(), buffer_.begin() + bytes_to_copy, user_buffer_);
buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy);
// If the buffer is getting too empty, resume asynchronous loading.
if (is_asynchronous_load_suspended_ &&
buffer_.size() <= static_cast<size_t>(
request_info_->prefetch_buffer_lower_threshold())) {
DVLOG(1) << "Resuming async load - buffer size: " << buffer_.size();
loader_->setDefersLoading(false);
is_asynchronous_load_suspended_ = false;
}
// Reset for next time.
user_buffer_ = NULL;
user_buffer_size_ = 0;
return bytes_to_copy;
}
void PPB_URLLoader_Impl::SaveResponse(const WebURLResponse& response) {
scoped_refptr<PPB_URLResponseInfo_Impl> response_info(
new PPB_URLResponseInfo_Impl(instance()));
if (response_info->Initialize(response))
response_info_ = response_info;
}
void PPB_URLLoader_Impl::UpdateStatus() {
if (status_callback_ &&
(RecordDownloadProgress() || RecordUploadProgress())) {
PP_Resource pp_resource = GetReferenceNoAddRef();
if (pp_resource) {
// The PP_Resource on the plugin will be NULL if the plugin has no
// reference to this object. That's fine, because then we don't need to
// call UpdateStatus.
//
// Here we go through some effort to only send the exact information that
// the requestor wanted in the request flags. It would be just as
// efficient to send all of it, but we don't want people to rely on
// getting download progress when they happen to set the upload progress
// flag.
status_callback_(
instance()->pp_instance(), pp_resource,
RecordUploadProgress() ? bytes_sent_ : -1,
RecordUploadProgress() ? total_bytes_to_be_sent_ : -1,
RecordDownloadProgress() ? bytes_received_ : -1,
RecordDownloadProgress() ? total_bytes_to_be_received_ : -1);
}
}
}
bool PPB_URLLoader_Impl::RecordDownloadProgress() const {
return request_info_ && request_info_->record_download_progress();
}
bool PPB_URLLoader_Impl::RecordUploadProgress() const {
return request_info_ && request_info_->record_upload_progress();
}
} // namespace ppapi
} // namespace webkit
|