OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/plugins/ppapi/ppb_url_loader_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_url_loader_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "ppapi/c/pp_completion_callback.h" | 8 #include "ppapi/c/pp_completion_callback.h" |
9 #include "ppapi/c/pp_errors.h" | 9 #include "ppapi/c/pp_errors.h" |
10 #include "ppapi/c/ppb_url_loader.h" | 10 #include "ppapi/c/ppb_url_loader.h" |
11 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" | 11 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" |
12 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" | 12 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" |
13 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h" | 13 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h" |
14 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" | 14 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" |
15 #include "third_party/WebKit/WebKit/chromium/public/WebKit.h" | 15 #include "third_party/WebKit/WebKit/chromium/public/WebKit.h" |
16 #include "third_party/WebKit/WebKit/chromium/public/WebKitClient.h" | 16 #include "third_party/WebKit/WebKit/chromium/public/WebKitClient.h" |
17 #include "third_party/WebKit/WebKit/chromium/public/WebPluginContainer.h" | 17 #include "third_party/WebKit/WebKit/chromium/public/WebPluginContainer.h" |
18 #include "third_party/WebKit/WebKit/chromium/public/WebSecurityOrigin.h" | 18 #include "third_party/WebKit/WebKit/chromium/public/WebSecurityOrigin.h" |
19 #include "third_party/WebKit/WebKit/chromium/public/WebURLLoader.h" | 19 #include "third_party/WebKit/WebKit/chromium/public/WebURLLoader.h" |
20 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" | 20 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" |
21 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" | 21 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" |
22 #include "webkit/appcache/web_application_cache_host_impl.h" | 22 #include "webkit/appcache/web_application_cache_host_impl.h" |
23 #include "webkit/plugins/ppapi/common.h" | 23 #include "webkit/plugins/ppapi/common.h" |
24 #include "webkit/plugins/ppapi/plugin_module.h" | |
25 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
26 #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" | 25 #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" |
27 #include "webkit/plugins/ppapi/ppb_url_response_info_impl.h" | 26 #include "webkit/plugins/ppapi/ppb_url_response_info_impl.h" |
28 | 27 |
29 using appcache::WebApplicationCacheHostImpl; | 28 using appcache::WebApplicationCacheHostImpl; |
30 using WebKit::WebFrame; | 29 using WebKit::WebFrame; |
31 using WebKit::WebString; | 30 using WebKit::WebString; |
32 using WebKit::WebURL; | 31 using WebKit::WebURL; |
33 using WebKit::WebURLError; | 32 using WebKit::WebURLError; |
34 using WebKit::WebURLLoader; | 33 using WebKit::WebURLLoader; |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 const PPB_URLLoaderTrusted* PPB_URLLoader_Impl::GetTrustedInterface() { | 225 const PPB_URLLoaderTrusted* PPB_URLLoader_Impl::GetTrustedInterface() { |
227 return &ppb_urlloadertrusted; | 226 return &ppb_urlloadertrusted; |
228 } | 227 } |
229 | 228 |
230 PPB_URLLoader_Impl* PPB_URLLoader_Impl::AsPPB_URLLoader_Impl() { | 229 PPB_URLLoader_Impl* PPB_URLLoader_Impl::AsPPB_URLLoader_Impl() { |
231 return this; | 230 return this; |
232 } | 231 } |
233 | 232 |
234 int32_t PPB_URLLoader_Impl::Open(PPB_URLRequestInfo_Impl* request, | 233 int32_t PPB_URLLoader_Impl::Open(PPB_URLRequestInfo_Impl* request, |
235 PP_CompletionCallback callback) { | 234 PP_CompletionCallback callback) { |
236 int32_t rv = ValidateCallback(callback); | |
237 if (rv != PP_OK) | |
238 return rv; | |
239 | |
240 if (loader_.get()) | 235 if (loader_.get()) |
241 return PP_ERROR_INPROGRESS; | 236 return PP_ERROR_INPROGRESS; |
242 | 237 |
| 238 // We only support non-blocking calls. |
| 239 if (!callback.func) |
| 240 return PP_ERROR_BADARGUMENT; |
| 241 |
243 WebFrame* frame = GetFrame(instance_); | 242 WebFrame* frame = GetFrame(instance_); |
244 if (!frame) | 243 if (!frame) |
245 return PP_ERROR_FAILED; | 244 return PP_ERROR_FAILED; |
246 WebURLRequest web_request(request->ToWebURLRequest(frame)); | 245 WebURLRequest web_request(request->ToWebURLRequest(frame)); |
247 | 246 |
248 rv = CanRequest(frame, web_request.url()); | 247 int32_t rv = CanRequest(frame, web_request.url()); |
249 if (rv != PP_OK) | 248 if (rv != PP_OK) |
250 return rv; | 249 return rv; |
251 | 250 |
252 frame->dispatchWillSendRequest(web_request); | 251 frame->dispatchWillSendRequest(web_request); |
253 | 252 |
254 // Sets the appcache host id to allow retrieval from the appcache. | 253 // Sets the appcache host id to allow retrieval from the appcache. |
255 if (WebApplicationCacheHostImpl* appcache_host = | 254 if (WebApplicationCacheHostImpl* appcache_host = |
256 WebApplicationCacheHostImpl::FromFrame(frame)) { | 255 WebApplicationCacheHostImpl::FromFrame(frame)) { |
257 appcache_host->willStartSubResourceRequest(web_request); | 256 appcache_host->willStartSubResourceRequest(web_request); |
258 } | 257 } |
259 | 258 |
260 loader_.reset(WebKit::webKitClient()->createURLLoader()); | 259 loader_.reset(WebKit::webKitClient()->createURLLoader()); |
261 if (!loader_.get()) | 260 if (!loader_.get()) |
262 return PP_ERROR_FAILED; | 261 return PP_ERROR_FAILED; |
263 | 262 |
264 loader_->loadAsynchronously(web_request, this); | 263 loader_->loadAsynchronously(web_request, this); |
265 | 264 |
266 request_info_ = scoped_refptr<PPB_URLRequestInfo_Impl>(request); | 265 request_info_ = scoped_refptr<PPB_URLRequestInfo_Impl>(request); |
| 266 pending_callback_ = callback; |
267 | 267 |
268 // Notify completion when we receive a redirect or response headers. | 268 // Notify completion when we receive a redirect or response headers. |
269 RegisterCallback(callback); | |
270 return PP_ERROR_WOULDBLOCK; | 269 return PP_ERROR_WOULDBLOCK; |
271 } | 270 } |
272 | 271 |
273 int32_t PPB_URLLoader_Impl::FollowRedirect(PP_CompletionCallback callback) { | 272 int32_t PPB_URLLoader_Impl::FollowRedirect(PP_CompletionCallback callback) { |
274 int32_t rv = ValidateCallback(callback); | 273 if (pending_callback_.func) |
| 274 return PP_ERROR_INPROGRESS; |
| 275 |
| 276 // We only support non-blocking calls. |
| 277 if (!callback.func) |
| 278 return PP_ERROR_BADARGUMENT; |
| 279 |
| 280 WebURL redirect_url = GURL(response_info_->redirect_url()); |
| 281 |
| 282 int32_t rv = CanRequest(GetFrame(instance_), redirect_url); |
275 if (rv != PP_OK) | 283 if (rv != PP_OK) |
276 return rv; | 284 return rv; |
277 | 285 |
278 WebURL redirect_url = GURL(response_info_->redirect_url()); | 286 pending_callback_ = callback; |
279 | |
280 rv = CanRequest(GetFrame(instance_), redirect_url); | |
281 if (rv != PP_OK) | |
282 return rv; | |
283 | |
284 loader_->setDefersLoading(false); // Allow the redirect to continue. | 287 loader_->setDefersLoading(false); // Allow the redirect to continue. |
285 RegisterCallback(callback); | |
286 return PP_ERROR_WOULDBLOCK; | 288 return PP_ERROR_WOULDBLOCK; |
287 } | 289 } |
288 | 290 |
289 bool PPB_URLLoader_Impl::GetUploadProgress(int64_t* bytes_sent, | 291 bool PPB_URLLoader_Impl::GetUploadProgress(int64_t* bytes_sent, |
290 int64_t* total_bytes_to_be_sent) { | 292 int64_t* total_bytes_to_be_sent) { |
291 if (!RecordUploadProgress()) { | 293 if (!RecordUploadProgress()) { |
292 *bytes_sent = 0; | 294 *bytes_sent = 0; |
293 *total_bytes_to_be_sent = 0; | 295 *total_bytes_to_be_sent = 0; |
294 return false; | 296 return false; |
295 } | 297 } |
(...skipping 11 matching lines...) Expand all Loading... |
307 return false; | 309 return false; |
308 } | 310 } |
309 *bytes_received = bytes_received_; | 311 *bytes_received = bytes_received_; |
310 *total_bytes_to_be_received = total_bytes_to_be_received_; | 312 *total_bytes_to_be_received = total_bytes_to_be_received_; |
311 return true; | 313 return true; |
312 } | 314 } |
313 | 315 |
314 int32_t PPB_URLLoader_Impl::ReadResponseBody(char* buffer, | 316 int32_t PPB_URLLoader_Impl::ReadResponseBody(char* buffer, |
315 int32_t bytes_to_read, | 317 int32_t bytes_to_read, |
316 PP_CompletionCallback callback) { | 318 PP_CompletionCallback callback) { |
317 int32_t rv = ValidateCallback(callback); | |
318 if (rv != PP_OK) | |
319 return rv; | |
320 if (!response_info_ || response_info_->body()) | 319 if (!response_info_ || response_info_->body()) |
321 return PP_ERROR_FAILED; | 320 return PP_ERROR_FAILED; |
322 if (bytes_to_read <= 0 || !buffer) | 321 if (bytes_to_read <= 0 || !buffer) |
323 return PP_ERROR_BADARGUMENT; | 322 return PP_ERROR_BADARGUMENT; |
| 323 if (pending_callback_.func) |
| 324 return PP_ERROR_INPROGRESS; |
| 325 |
| 326 // We only support non-blocking calls. |
| 327 if (!callback.func) |
| 328 return PP_ERROR_BADARGUMENT; |
324 | 329 |
325 user_buffer_ = buffer; | 330 user_buffer_ = buffer; |
326 user_buffer_size_ = bytes_to_read; | 331 user_buffer_size_ = bytes_to_read; |
327 | 332 |
328 if (!buffer_.empty()) | 333 if (!buffer_.empty()) |
329 return FillUserBuffer(); | 334 return FillUserBuffer(); |
330 | 335 |
331 // We may have already reached EOF. | 336 // We may have already reached EOF. |
332 if (done_status_ != PP_ERROR_WOULDBLOCK) { | 337 if (done_status_ != PP_ERROR_WOULDBLOCK) { |
333 user_buffer_ = NULL; | 338 user_buffer_ = NULL; |
334 user_buffer_size_ = 0; | 339 user_buffer_size_ = 0; |
335 return done_status_; | 340 return done_status_; |
336 } | 341 } |
337 | 342 |
338 RegisterCallback(callback); | 343 pending_callback_ = callback; |
339 return PP_ERROR_WOULDBLOCK; | 344 return PP_ERROR_WOULDBLOCK; |
340 } | 345 } |
341 | 346 |
342 int32_t PPB_URLLoader_Impl::FinishStreamingToFile( | 347 int32_t PPB_URLLoader_Impl::FinishStreamingToFile( |
343 PP_CompletionCallback callback) { | 348 PP_CompletionCallback callback) { |
344 int32_t rv = ValidateCallback(callback); | |
345 if (rv != PP_OK) | |
346 return rv; | |
347 if (!response_info_ || !response_info_->body()) | 349 if (!response_info_ || !response_info_->body()) |
348 return PP_ERROR_FAILED; | 350 return PP_ERROR_FAILED; |
| 351 if (pending_callback_.func) |
| 352 return PP_ERROR_INPROGRESS; |
349 | 353 |
350 // We may have already reached EOF. | 354 // We may have already reached EOF. |
351 if (done_status_ != PP_ERROR_WOULDBLOCK) | 355 if (done_status_ != PP_ERROR_WOULDBLOCK) |
352 return done_status_; | 356 return done_status_; |
353 | 357 |
354 // Wait for didFinishLoading / didFail. | 358 // Wait for didFinishLoading / didFail. |
355 RegisterCallback(callback); | 359 pending_callback_ = callback; |
356 return PP_ERROR_WOULDBLOCK; | 360 return PP_ERROR_WOULDBLOCK; |
357 } | 361 } |
358 | 362 |
359 void PPB_URLLoader_Impl::Close() { | 363 void PPB_URLLoader_Impl::Close() { |
360 if (loader_.get()) { | 364 if (loader_.get()) { |
361 loader_->cancel(); | 365 loader_->cancel(); |
362 } else if (main_document_loader_) { | 366 } else if (main_document_loader_) { |
363 WebFrame* frame = instance_->container()->element().document().frame(); | 367 WebFrame* frame = instance_->container()->element().document().frame(); |
364 frame->stopLoading(); | 368 frame->stopLoading(); |
365 } | 369 } |
366 // TODO(viettrungluu): Check what happens to the callback (probably the | |
367 // wrong thing). May need to post abort here. crbug.com/69457 | |
368 } | 370 } |
369 | 371 |
370 void PPB_URLLoader_Impl::GrantUniversalAccess() { | 372 void PPB_URLLoader_Impl::GrantUniversalAccess() { |
371 has_universal_access_ = true; | 373 has_universal_access_ = true; |
372 } | 374 } |
373 | 375 |
374 void PPB_URLLoader_Impl::SetStatusCallback( | 376 void PPB_URLLoader_Impl::SetStatusCallback( |
375 PP_URLLoaderTrusted_StatusCallback cb) { | 377 PP_URLLoaderTrusted_StatusCallback cb) { |
376 status_callback_ = cb; | 378 status_callback_ = cb; |
377 } | 379 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 | 424 |
423 void PPB_URLLoader_Impl::didReceiveData(WebURLLoader* loader, | 425 void PPB_URLLoader_Impl::didReceiveData(WebURLLoader* loader, |
424 const char* data, | 426 const char* data, |
425 int data_length) { | 427 int data_length) { |
426 bytes_received_ += data_length; | 428 bytes_received_ += data_length; |
427 | 429 |
428 buffer_.insert(buffer_.end(), data, data + data_length); | 430 buffer_.insert(buffer_.end(), data, data + data_length); |
429 if (user_buffer_) { | 431 if (user_buffer_) { |
430 RunCallback(FillUserBuffer()); | 432 RunCallback(FillUserBuffer()); |
431 } else { | 433 } else { |
432 DCHECK(!pending_callback_.get() || pending_callback_->completed()); | 434 DCHECK(!pending_callback_.func); |
433 } | 435 } |
434 } | 436 } |
435 | 437 |
436 void PPB_URLLoader_Impl::didFinishLoading(WebURLLoader* loader, | 438 void PPB_URLLoader_Impl::didFinishLoading(WebURLLoader* loader, |
437 double finish_time) { | 439 double finish_time) { |
438 done_status_ = PP_OK; | 440 done_status_ = PP_OK; |
439 RunCallback(done_status_); | 441 RunCallback(done_status_); |
440 } | 442 } |
441 | 443 |
442 void PPB_URLLoader_Impl::didFail(WebURLLoader* loader, | 444 void PPB_URLLoader_Impl::didFail(WebURLLoader* loader, |
(...skipping 30 matching lines...) Expand all Loading... |
473 response_info_ = NULL; | 475 response_info_ = NULL; |
474 } | 476 } |
475 | 477 |
476 // Free the WebKit request. | 478 // Free the WebKit request. |
477 loader_.reset(); | 479 loader_.reset(); |
478 | 480 |
479 // Often, |this| will be deleted at the end of this function when death_grip | 481 // Often, |this| will be deleted at the end of this function when death_grip |
480 // goes out of scope. | 482 // goes out of scope. |
481 } | 483 } |
482 | 484 |
483 int32_t PPB_URLLoader_Impl::ValidateCallback(PP_CompletionCallback callback) { | 485 void PPB_URLLoader_Impl::RunCallback(int32_t result) { |
484 // We only support non-blocking calls. | 486 if (!pending_callback_.func) |
485 if (!callback.func) | 487 return; |
486 return PP_ERROR_BADARGUMENT; | |
487 | 488 |
488 if (pending_callback_.get() && !pending_callback_->completed()) | 489 PP_CompletionCallback callback = {0}; |
489 return PP_ERROR_INPROGRESS; | 490 std::swap(callback, pending_callback_); |
490 | 491 PP_RunCompletionCallback(&callback, result); |
491 return PP_OK; | |
492 } | |
493 | |
494 void PPB_URLLoader_Impl::RegisterCallback(PP_CompletionCallback callback) { | |
495 DCHECK(callback.func); | |
496 DCHECK(!pending_callback_.get() || pending_callback_->completed()); | |
497 | |
498 PP_Resource resource_id = GetReferenceNoAddRef(); | |
499 CHECK(resource_id); | |
500 pending_callback_ = new TrackedCompletionCallback( | |
501 module()->GetCallbackTracker(), resource_id, callback); | |
502 } | |
503 | |
504 void PPB_URLLoader_Impl::RunCallback(int32_t result) { | |
505 scoped_refptr<TrackedCompletionCallback> callback; | |
506 callback.swap(pending_callback_); | |
507 callback->Run(result); // Will complete abortively if necessary. | |
508 } | 492 } |
509 | 493 |
510 size_t PPB_URLLoader_Impl::FillUserBuffer() { | 494 size_t PPB_URLLoader_Impl::FillUserBuffer() { |
511 DCHECK(user_buffer_); | 495 DCHECK(user_buffer_); |
512 DCHECK(user_buffer_size_); | 496 DCHECK(user_buffer_size_); |
513 | 497 |
514 size_t bytes_to_copy = std::min(buffer_.size(), user_buffer_size_); | 498 size_t bytes_to_copy = std::min(buffer_.size(), user_buffer_size_); |
515 std::copy(buffer_.begin(), buffer_.begin() + bytes_to_copy, user_buffer_); | 499 std::copy(buffer_.begin(), buffer_.begin() + bytes_to_copy, user_buffer_); |
516 buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy); | 500 buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy); |
517 | 501 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 return request_info_ && request_info_->record_download_progress(); | 550 return request_info_ && request_info_->record_download_progress(); |
567 } | 551 } |
568 | 552 |
569 bool PPB_URLLoader_Impl::RecordUploadProgress() const { | 553 bool PPB_URLLoader_Impl::RecordUploadProgress() const { |
570 return request_info_ && request_info_->record_upload_progress(); | 554 return request_info_ && request_info_->record_upload_progress(); |
571 } | 555 } |
572 | 556 |
573 } // namespace ppapi | 557 } // namespace ppapi |
574 } // namespace webkit | 558 } // namespace webkit |
575 | 559 |
OLD | NEW |