| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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" |
| 11 #include "ppapi/c/ppb_url_loader.h" | 11 #include "ppapi/c/ppb_url_loader.h" |
| 12 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" | 12 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" |
| 13 #include "ppapi/shared_impl/ppapi_globals.h" |
| 14 #include "ppapi/shared_impl/url_response_info_data.h" |
| 13 #include "ppapi/thunk/enter.h" | 15 #include "ppapi/thunk/enter.h" |
| 14 #include "ppapi/thunk/ppb_url_request_info_api.h" | 16 #include "ppapi/thunk/ppb_url_request_info_api.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebKitPlatfo
rmSupport.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebKitPlatfo
rmSupport.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLError.
h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLError.
h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLLoader
.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLLoader
.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoaderOptions.h
" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoaderOptions.h
" |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" |
| 26 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRespon
se.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRespon
se.h" |
| 27 #include "webkit/appcache/web_application_cache_host_impl.h" | 29 #include "webkit/appcache/web_application_cache_host_impl.h" |
| 28 #include "webkit/plugins/ppapi/common.h" | 30 #include "webkit/plugins/ppapi/common.h" |
| 29 #include "webkit/plugins/ppapi/plugin_module.h" | 31 #include "webkit/plugins/ppapi/plugin_module.h" |
| 30 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 32 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 31 #include "webkit/plugins/ppapi/resource_helper.h" | 33 #include "webkit/plugins/ppapi/resource_helper.h" |
| 32 #include "webkit/plugins/ppapi/url_request_info_util.h" | 34 #include "webkit/plugins/ppapi/url_request_info_util.h" |
| 33 #include "webkit/plugins/ppapi/ppb_url_response_info_impl.h" | 35 #include "webkit/plugins/ppapi/url_response_info_util.h" |
| 34 | 36 |
| 35 using appcache::WebApplicationCacheHostImpl; | 37 using appcache::WebApplicationCacheHostImpl; |
| 36 using ppapi::Resource; | 38 using ppapi::Resource; |
| 37 using ppapi::thunk::EnterResourceNoLock; | 39 using ppapi::thunk::EnterResourceNoLock; |
| 38 using ppapi::thunk::PPB_URLLoader_API; | 40 using ppapi::thunk::PPB_URLLoader_API; |
| 39 using ppapi::thunk::PPB_URLRequestInfo_API; | 41 using ppapi::thunk::PPB_URLRequestInfo_API; |
| 40 using ppapi::TrackedCallback; | 42 using ppapi::TrackedCallback; |
| 41 using WebKit::WebFrame; | 43 using WebKit::WebFrame; |
| 42 using WebKit::WebString; | 44 using WebKit::WebString; |
| 43 using WebKit::WebURL; | 45 using WebKit::WebURL; |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 *bytes_received = 0; | 214 *bytes_received = 0; |
| 213 *total_bytes_to_be_received = 0; | 215 *total_bytes_to_be_received = 0; |
| 214 return PP_FALSE; | 216 return PP_FALSE; |
| 215 } | 217 } |
| 216 *bytes_received = bytes_received_; | 218 *bytes_received = bytes_received_; |
| 217 *total_bytes_to_be_received = total_bytes_to_be_received_; | 219 *total_bytes_to_be_received = total_bytes_to_be_received_; |
| 218 return PP_TRUE; | 220 return PP_TRUE; |
| 219 } | 221 } |
| 220 | 222 |
| 221 PP_Resource PPB_URLLoader_Impl::GetResponseInfo() { | 223 PP_Resource PPB_URLLoader_Impl::GetResponseInfo() { |
| 222 if (!response_info_) | 224 ::ppapi::thunk::EnterResourceCreationNoLock enter(pp_instance()); |
| 225 if (enter.failed() || !response_info_.get()) |
| 223 return 0; | 226 return 0; |
| 224 return response_info_->GetReference(); | 227 |
| 228 // Since we're the "host" the process-local resource for the file ref is |
| 229 // the same as the host resource. We pass a ref to the file ref. |
| 230 if (!response_info_->body_as_file_ref.resource.is_null()) { |
| 231 ::ppapi::PpapiGlobals::Get()->GetResourceTracker()->AddRefResource( |
| 232 response_info_->body_as_file_ref.resource.host_resource()); |
| 233 } |
| 234 return enter.functions()->CreateURLResponseInfo( |
| 235 pp_instance(), |
| 236 *response_info_, |
| 237 response_info_->body_as_file_ref.resource.host_resource()); |
| 225 } | 238 } |
| 226 | 239 |
| 227 int32_t PPB_URLLoader_Impl::ReadResponseBody( | 240 int32_t PPB_URLLoader_Impl::ReadResponseBody( |
| 228 void* buffer, | 241 void* buffer, |
| 229 int32_t bytes_to_read, | 242 int32_t bytes_to_read, |
| 230 scoped_refptr<TrackedCallback> callback) { | 243 scoped_refptr<TrackedCallback> callback) { |
| 231 int32_t rv = ValidateCallback(callback); | 244 int32_t rv = ValidateCallback(callback); |
| 232 if (rv != PP_OK) | 245 if (rv != PP_OK) |
| 233 return rv; | 246 return rv; |
| 234 if (!response_info_ || response_info_->body()) | 247 if (!response_info_.get() || |
| 248 !response_info_->body_as_file_ref.resource.is_null()) |
| 235 return PP_ERROR_FAILED; | 249 return PP_ERROR_FAILED; |
| 236 if (bytes_to_read <= 0 || !buffer) | 250 if (bytes_to_read <= 0 || !buffer) |
| 237 return PP_ERROR_BADARGUMENT; | 251 return PP_ERROR_BADARGUMENT; |
| 238 | 252 |
| 239 user_buffer_ = static_cast<char*>(buffer); | 253 user_buffer_ = static_cast<char*>(buffer); |
| 240 user_buffer_size_ = bytes_to_read; | 254 user_buffer_size_ = bytes_to_read; |
| 241 | 255 |
| 242 if (!buffer_.empty()) | 256 if (!buffer_.empty()) |
| 243 return FillUserBuffer(); | 257 return FillUserBuffer(); |
| 244 | 258 |
| 245 // We may have already reached EOF. | 259 // We may have already reached EOF. |
| 246 if (done_status_ != PP_OK_COMPLETIONPENDING) { | 260 if (done_status_ != PP_OK_COMPLETIONPENDING) { |
| 247 user_buffer_ = NULL; | 261 user_buffer_ = NULL; |
| 248 user_buffer_size_ = 0; | 262 user_buffer_size_ = 0; |
| 249 return done_status_; | 263 return done_status_; |
| 250 } | 264 } |
| 251 | 265 |
| 252 RegisterCallback(callback); | 266 RegisterCallback(callback); |
| 253 return PP_OK_COMPLETIONPENDING; | 267 return PP_OK_COMPLETIONPENDING; |
| 254 } | 268 } |
| 255 | 269 |
| 256 int32_t PPB_URLLoader_Impl::FinishStreamingToFile( | 270 int32_t PPB_URLLoader_Impl::FinishStreamingToFile( |
| 257 scoped_refptr<TrackedCallback> callback) { | 271 scoped_refptr<TrackedCallback> callback) { |
| 258 int32_t rv = ValidateCallback(callback); | 272 int32_t rv = ValidateCallback(callback); |
| 259 if (rv != PP_OK) | 273 if (rv != PP_OK) |
| 260 return rv; | 274 return rv; |
| 261 if (!response_info_ || !response_info_->body()) | 275 if (!response_info_.get() || |
| 276 response_info_->body_as_file_ref.resource.is_null()) |
| 262 return PP_ERROR_FAILED; | 277 return PP_ERROR_FAILED; |
| 263 | 278 |
| 264 // We may have already reached EOF. | 279 // We may have already reached EOF. |
| 265 if (done_status_ != PP_OK_COMPLETIONPENDING) | 280 if (done_status_ != PP_OK_COMPLETIONPENDING) |
| 266 return done_status_; | 281 return done_status_; |
| 267 | 282 |
| 268 is_streaming_to_file_ = true; | 283 is_streaming_to_file_ = true; |
| 269 if (is_asynchronous_load_suspended_) | 284 if (is_asynchronous_load_suspended_) |
| 270 SetDefersLoading(false); | 285 SetDefersLoading(false); |
| 271 | 286 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 285 | 300 |
| 286 void PPB_URLLoader_Impl::GrantUniversalAccess() { | 301 void PPB_URLLoader_Impl::GrantUniversalAccess() { |
| 287 has_universal_access_ = true; | 302 has_universal_access_ = true; |
| 288 } | 303 } |
| 289 | 304 |
| 290 void PPB_URLLoader_Impl::SetStatusCallback( | 305 void PPB_URLLoader_Impl::SetStatusCallback( |
| 291 PP_URLLoaderTrusted_StatusCallback cb) { | 306 PP_URLLoaderTrusted_StatusCallback cb) { |
| 292 status_callback_ = cb; | 307 status_callback_ = cb; |
| 293 } | 308 } |
| 294 | 309 |
| 310 bool PPB_URLLoader_Impl::GetResponseInfoData( |
| 311 ::ppapi::URLResponseInfoData* data) { |
| 312 if (!response_info_.get()) |
| 313 return false; |
| 314 |
| 315 *data = *response_info_; |
| 316 |
| 317 // We transfer one plugin reference to the FileRef to the caller. |
| 318 if (!response_info_->body_as_file_ref.resource.is_null()) { |
| 319 ::ppapi::PpapiGlobals::Get()->GetResourceTracker()->AddRefResource( |
| 320 response_info_->body_as_file_ref.resource.host_resource()); |
| 321 } |
| 322 return true; |
| 323 } |
| 324 |
| 295 void PPB_URLLoader_Impl::willSendRequest( | 325 void PPB_URLLoader_Impl::willSendRequest( |
| 296 WebURLLoader* loader, | 326 WebURLLoader* loader, |
| 297 WebURLRequest& new_request, | 327 WebURLRequest& new_request, |
| 298 const WebURLResponse& redirect_response) { | 328 const WebURLResponse& redirect_response) { |
| 299 if (!request_data_.follow_redirects) { | 329 if (!request_data_.follow_redirects) { |
| 300 SaveResponse(redirect_response); | 330 SaveResponse(redirect_response); |
| 301 SetDefersLoading(true); | 331 SetDefersLoading(true); |
| 302 RunCallback(PP_OK); | 332 RunCallback(PP_OK); |
| 303 } | 333 } |
| 304 } | 334 } |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 SetDefersLoading(false); | 496 SetDefersLoading(false); |
| 467 } | 497 } |
| 468 | 498 |
| 469 // Reset for next time. | 499 // Reset for next time. |
| 470 user_buffer_ = NULL; | 500 user_buffer_ = NULL; |
| 471 user_buffer_size_ = 0; | 501 user_buffer_size_ = 0; |
| 472 return bytes_to_copy; | 502 return bytes_to_copy; |
| 473 } | 503 } |
| 474 | 504 |
| 475 void PPB_URLLoader_Impl::SaveResponse(const WebURLResponse& response) { | 505 void PPB_URLLoader_Impl::SaveResponse(const WebURLResponse& response) { |
| 476 scoped_refptr<PPB_URLResponseInfo_Impl> response_info( | 506 // DataFromWebURLResponse returns a file ref with one reference to it, which |
| 477 new PPB_URLResponseInfo_Impl(pp_instance())); | 507 // we take over via our ScopedPPResource. |
| 478 if (response_info->Initialize(response)) | 508 response_info_.reset(new ::ppapi::URLResponseInfoData( |
| 479 response_info_ = response_info; | 509 DataFromWebURLResponse(pp_instance(), response))); |
| 510 response_info_file_ref_ = ::ppapi::ScopedPPResource( |
| 511 ::ppapi::ScopedPPResource::PassRef(), |
| 512 response_info_->body_as_file_ref.resource.host_resource()); |
| 480 } | 513 } |
| 481 | 514 |
| 482 void PPB_URLLoader_Impl::UpdateStatus() { | 515 void PPB_URLLoader_Impl::UpdateStatus() { |
| 483 if (status_callback_ && | 516 if (status_callback_ && |
| 484 (RecordDownloadProgress() || RecordUploadProgress())) { | 517 (RecordDownloadProgress() || RecordUploadProgress())) { |
| 485 // Here we go through some effort to only send the exact information that | 518 // Here we go through some effort to only send the exact information that |
| 486 // the requestor wanted in the request flags. It would be just as | 519 // the requestor wanted in the request flags. It would be just as |
| 487 // efficient to send all of it, but we don't want people to rely on | 520 // efficient to send all of it, but we don't want people to rely on |
| 488 // getting download progress when they happen to set the upload progress | 521 // getting download progress when they happen to set the upload progress |
| 489 // flag. | 522 // flag. |
| 490 status_callback_( | 523 status_callback_( |
| 491 pp_instance(), pp_resource(), | 524 pp_instance(), pp_resource(), |
| 492 RecordUploadProgress() ? bytes_sent_ : -1, | 525 RecordUploadProgress() ? bytes_sent_ : -1, |
| 493 RecordUploadProgress() ? total_bytes_to_be_sent_ : -1, | 526 RecordUploadProgress() ? total_bytes_to_be_sent_ : -1, |
| 494 RecordDownloadProgress() ? bytes_received_ : -1, | 527 RecordDownloadProgress() ? bytes_received_ : -1, |
| 495 RecordDownloadProgress() ? total_bytes_to_be_received_ : -1); | 528 RecordDownloadProgress() ? total_bytes_to_be_received_ : -1); |
| 496 } | 529 } |
| 497 } | 530 } |
| 498 | 531 |
| 499 bool PPB_URLLoader_Impl::RecordDownloadProgress() const { | 532 bool PPB_URLLoader_Impl::RecordDownloadProgress() const { |
| 500 return request_data_.record_download_progress; | 533 return request_data_.record_download_progress; |
| 501 } | 534 } |
| 502 | 535 |
| 503 bool PPB_URLLoader_Impl::RecordUploadProgress() const { | 536 bool PPB_URLLoader_Impl::RecordUploadProgress() const { |
| 504 return request_data_.record_upload_progress; | 537 return request_data_.record_upload_progress; |
| 505 } | 538 } |
| 506 | 539 |
| 507 } // namespace ppapi | 540 } // namespace ppapi |
| 508 } // namespace webkit | 541 } // namespace webkit |
| OLD | NEW |