OLD | NEW |
1 // Copyright (c) 2011 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 "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
9 #include "ppapi/c/pp_completion_callback.h" | 9 #include "ppapi/c/pp_completion_callback.h" |
10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h" |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoaderOptions.h
" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoaderOptions.h
" |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" |
26 #include "webkit/appcache/web_application_cache_host_impl.h" | 26 #include "webkit/appcache/web_application_cache_host_impl.h" |
27 #include "webkit/plugins/ppapi/common.h" | 27 #include "webkit/plugins/ppapi/common.h" |
28 #include "webkit/plugins/ppapi/plugin_module.h" | 28 #include "webkit/plugins/ppapi/plugin_module.h" |
29 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 29 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
30 #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" | 30 #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" |
31 #include "webkit/plugins/ppapi/ppb_url_response_info_impl.h" | 31 #include "webkit/plugins/ppapi/ppb_url_response_info_impl.h" |
| 32 #include "webkit/plugins/ppapi/resource_helper.h" |
32 | 33 |
33 using appcache::WebApplicationCacheHostImpl; | 34 using appcache::WebApplicationCacheHostImpl; |
| 35 using ppapi::Resource; |
34 using ppapi::thunk::EnterResourceNoLock; | 36 using ppapi::thunk::EnterResourceNoLock; |
35 using ppapi::thunk::PPB_URLLoader_API; | 37 using ppapi::thunk::PPB_URLLoader_API; |
36 using ppapi::thunk::PPB_URLRequestInfo_API; | 38 using ppapi::thunk::PPB_URLRequestInfo_API; |
37 using WebKit::WebFrame; | 39 using WebKit::WebFrame; |
38 using WebKit::WebString; | 40 using WebKit::WebString; |
39 using WebKit::WebURL; | 41 using WebKit::WebURL; |
40 using WebKit::WebURLError; | 42 using WebKit::WebURLError; |
41 using WebKit::WebURLLoader; | 43 using WebKit::WebURLLoader; |
42 using WebKit::WebURLLoaderOptions; | 44 using WebKit::WebURLLoaderOptions; |
43 using WebKit::WebURLRequest; | 45 using WebKit::WebURLRequest; |
44 using WebKit::WebURLResponse; | 46 using WebKit::WebURLResponse; |
45 | 47 |
46 #ifdef _MSC_VER | 48 #ifdef _MSC_VER |
47 // Do not warn about use of std::copy with raw pointers. | 49 // Do not warn about use of std::copy with raw pointers. |
48 #pragma warning(disable : 4996) | 50 #pragma warning(disable : 4996) |
49 #endif | 51 #endif |
50 | 52 |
51 namespace webkit { | 53 namespace webkit { |
52 namespace ppapi { | 54 namespace ppapi { |
53 | 55 |
54 PPB_URLLoader_Impl::PPB_URLLoader_Impl(PluginInstance* instance, | 56 namespace { |
| 57 |
| 58 WebFrame* GetFrameForResource(const Resource* resource) { |
| 59 return ResourceHelper::GetPluginInstance(resource)-> |
| 60 container()->element().document().frame(); |
| 61 } |
| 62 |
| 63 } |
| 64 |
| 65 PPB_URLLoader_Impl::PPB_URLLoader_Impl(PP_Instance instance, |
55 bool main_document_loader) | 66 bool main_document_loader) |
56 : Resource(instance), | 67 : Resource(instance), |
57 main_document_loader_(main_document_loader), | 68 main_document_loader_(main_document_loader), |
58 pending_callback_(), | 69 pending_callback_(), |
59 bytes_sent_(0), | 70 bytes_sent_(0), |
60 total_bytes_to_be_sent_(-1), | 71 total_bytes_to_be_sent_(-1), |
61 bytes_received_(0), | 72 bytes_received_(0), |
62 total_bytes_to_be_received_(-1), | 73 total_bytes_to_be_received_(-1), |
63 user_buffer_(NULL), | 74 user_buffer_(NULL), |
64 user_buffer_size_(0), | 75 user_buffer_size_(0), |
(...skipping 27 matching lines...) Expand all Loading... |
92 int32_t rv = ValidateCallback(callback); | 103 int32_t rv = ValidateCallback(callback); |
93 if (rv != PP_OK) | 104 if (rv != PP_OK) |
94 return rv; | 105 return rv; |
95 | 106 |
96 if (request->RequiresUniversalAccess() && !has_universal_access_) | 107 if (request->RequiresUniversalAccess() && !has_universal_access_) |
97 return PP_ERROR_NOACCESS; | 108 return PP_ERROR_NOACCESS; |
98 | 109 |
99 if (loader_.get()) | 110 if (loader_.get()) |
100 return PP_ERROR_INPROGRESS; | 111 return PP_ERROR_INPROGRESS; |
101 | 112 |
102 WebFrame* frame = instance()->container()->element().document().frame(); | 113 WebFrame* frame = GetFrameForResource(this); |
103 if (!frame) | 114 if (!frame) |
104 return PP_ERROR_FAILED; | 115 return PP_ERROR_FAILED; |
105 WebURLRequest web_request(request->ToWebURLRequest(frame)); | 116 WebURLRequest web_request(request->ToWebURLRequest(frame)); |
106 | 117 |
107 WebURLLoaderOptions options; | 118 WebURLLoaderOptions options; |
108 if (has_universal_access_) { | 119 if (has_universal_access_) { |
109 // Universal access allows cross-origin requests and sends credentials. | 120 // Universal access allows cross-origin requests and sends credentials. |
110 options.crossOriginRequestPolicy = | 121 options.crossOriginRequestPolicy = |
111 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; | 122 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; |
112 options.allowCredentials = true; | 123 options.allowCredentials = true; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 loader_->setDefersLoading(false); | 230 loader_->setDefersLoading(false); |
220 is_asynchronous_load_suspended_ = false; | 231 is_asynchronous_load_suspended_ = false; |
221 } | 232 } |
222 | 233 |
223 // Wait for didFinishLoading / didFail. | 234 // Wait for didFinishLoading / didFail. |
224 RegisterCallback(callback); | 235 RegisterCallback(callback); |
225 return PP_OK_COMPLETIONPENDING; | 236 return PP_OK_COMPLETIONPENDING; |
226 } | 237 } |
227 | 238 |
228 void PPB_URLLoader_Impl::Close() { | 239 void PPB_URLLoader_Impl::Close() { |
229 if (loader_.get()) { | 240 if (loader_.get()) |
230 loader_->cancel(); | 241 loader_->cancel(); |
231 } else if (main_document_loader_) { | 242 else if (main_document_loader_) |
232 WebFrame* frame = instance()->container()->element().document().frame(); | 243 GetFrameForResource(this)->stopLoading(); |
233 frame->stopLoading(); | |
234 } | |
235 // TODO(viettrungluu): Check what happens to the callback (probably the | 244 // TODO(viettrungluu): Check what happens to the callback (probably the |
236 // wrong thing). May need to post abort here. crbug.com/69457 | 245 // wrong thing). May need to post abort here. crbug.com/69457 |
237 } | 246 } |
238 | 247 |
239 void PPB_URLLoader_Impl::GrantUniversalAccess() { | 248 void PPB_URLLoader_Impl::GrantUniversalAccess() { |
240 has_universal_access_ = true; | 249 has_universal_access_ = true; |
241 } | 250 } |
242 | 251 |
243 void PPB_URLLoader_Impl::SetStatusCallback( | 252 void PPB_URLLoader_Impl::SetStatusCallback( |
244 PP_URLLoaderTrusted_StatusCallback cb) { | 253 PP_URLLoaderTrusted_StatusCallback cb) { |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 return PP_ERROR_INPROGRESS; | 365 return PP_ERROR_INPROGRESS; |
357 | 366 |
358 return PP_OK; | 367 return PP_OK; |
359 } | 368 } |
360 | 369 |
361 void PPB_URLLoader_Impl::RegisterCallback(PP_CompletionCallback callback) { | 370 void PPB_URLLoader_Impl::RegisterCallback(PP_CompletionCallback callback) { |
362 DCHECK(callback.func); | 371 DCHECK(callback.func); |
363 DCHECK(!pending_callback_.get() || pending_callback_->completed()); | 372 DCHECK(!pending_callback_.get() || pending_callback_->completed()); |
364 | 373 |
365 pending_callback_ = new TrackedCompletionCallback( | 374 pending_callback_ = new TrackedCompletionCallback( |
366 instance()->module()->GetCallbackTracker(), pp_resource(), callback); | 375 ResourceHelper::GetPluginModule(this)->GetCallbackTracker(), |
| 376 pp_resource(), callback); |
367 } | 377 } |
368 | 378 |
369 void PPB_URLLoader_Impl::RunCallback(int32_t result) { | 379 void PPB_URLLoader_Impl::RunCallback(int32_t result) { |
370 // This may be null only when this is a main document loader. | 380 // This may be null only when this is a main document loader. |
371 if (!pending_callback_.get()) { | 381 if (!pending_callback_.get()) { |
372 CHECK(main_document_loader_); | 382 CHECK(main_document_loader_); |
373 return; | 383 return; |
374 } | 384 } |
375 | 385 |
376 scoped_refptr<TrackedCompletionCallback> callback; | 386 scoped_refptr<TrackedCompletionCallback> callback; |
(...skipping 20 matching lines...) Expand all Loading... |
397 } | 407 } |
398 | 408 |
399 // Reset for next time. | 409 // Reset for next time. |
400 user_buffer_ = NULL; | 410 user_buffer_ = NULL; |
401 user_buffer_size_ = 0; | 411 user_buffer_size_ = 0; |
402 return bytes_to_copy; | 412 return bytes_to_copy; |
403 } | 413 } |
404 | 414 |
405 void PPB_URLLoader_Impl::SaveResponse(const WebURLResponse& response) { | 415 void PPB_URLLoader_Impl::SaveResponse(const WebURLResponse& response) { |
406 scoped_refptr<PPB_URLResponseInfo_Impl> response_info( | 416 scoped_refptr<PPB_URLResponseInfo_Impl> response_info( |
407 new PPB_URLResponseInfo_Impl(instance())); | 417 new PPB_URLResponseInfo_Impl(pp_instance())); |
408 if (response_info->Initialize(response)) | 418 if (response_info->Initialize(response)) |
409 response_info_ = response_info; | 419 response_info_ = response_info; |
410 } | 420 } |
411 | 421 |
412 void PPB_URLLoader_Impl::UpdateStatus() { | 422 void PPB_URLLoader_Impl::UpdateStatus() { |
413 if (status_callback_ && | 423 if (status_callback_ && |
414 (RecordDownloadProgress() || RecordUploadProgress())) { | 424 (RecordDownloadProgress() || RecordUploadProgress())) { |
415 // Here we go through some effort to only send the exact information that | 425 // Here we go through some effort to only send the exact information that |
416 // the requestor wanted in the request flags. It would be just as | 426 // the requestor wanted in the request flags. It would be just as |
417 // efficient to send all of it, but we don't want people to rely on | 427 // efficient to send all of it, but we don't want people to rely on |
418 // getting download progress when they happen to set the upload progress | 428 // getting download progress when they happen to set the upload progress |
419 // flag. | 429 // flag. |
420 status_callback_( | 430 status_callback_( |
421 instance()->pp_instance(), pp_resource(), | 431 pp_instance(), pp_resource(), |
422 RecordUploadProgress() ? bytes_sent_ : -1, | 432 RecordUploadProgress() ? bytes_sent_ : -1, |
423 RecordUploadProgress() ? total_bytes_to_be_sent_ : -1, | 433 RecordUploadProgress() ? total_bytes_to_be_sent_ : -1, |
424 RecordDownloadProgress() ? bytes_received_ : -1, | 434 RecordDownloadProgress() ? bytes_received_ : -1, |
425 RecordDownloadProgress() ? total_bytes_to_be_received_ : -1); | 435 RecordDownloadProgress() ? total_bytes_to_be_received_ : -1); |
426 } | 436 } |
427 } | 437 } |
428 | 438 |
429 bool PPB_URLLoader_Impl::RecordDownloadProgress() const { | 439 bool PPB_URLLoader_Impl::RecordDownloadProgress() const { |
430 return request_info_ && request_info_->record_download_progress(); | 440 return request_info_ && request_info_->record_download_progress(); |
431 } | 441 } |
432 | 442 |
433 bool PPB_URLLoader_Impl::RecordUploadProgress() const { | 443 bool PPB_URLLoader_Impl::RecordUploadProgress() const { |
434 return request_info_ && request_info_->record_upload_progress(); | 444 return request_info_ && request_info_->record_upload_progress(); |
435 } | 445 } |
436 | 446 |
437 } // namespace ppapi | 447 } // namespace ppapi |
438 } // namespace webkit | 448 } // namespace webkit |
OLD | NEW |