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