| 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" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRespon
se.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRespon
se.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 #include "webkit/plugins/ppapi/resource_helper.h" |
| 33 | 33 |
| 34 using appcache::WebApplicationCacheHostImpl; | 34 using appcache::WebApplicationCacheHostImpl; |
| 35 using ppapi::ApiCallbackType; |
| 35 using ppapi::Resource; | 36 using ppapi::Resource; |
| 36 using ppapi::thunk::EnterResourceNoLock; | 37 using ppapi::thunk::EnterResourceNoLock; |
| 37 using ppapi::thunk::PPB_URLLoader_API; | 38 using ppapi::thunk::PPB_URLLoader_API; |
| 38 using ppapi::thunk::PPB_URLRequestInfo_API; | 39 using ppapi::thunk::PPB_URLRequestInfo_API; |
| 39 using ppapi::TrackedCallback; | 40 using ppapi::TrackedCallback; |
| 40 using WebKit::WebFrame; | 41 using WebKit::WebFrame; |
| 41 using WebKit::WebString; | 42 using WebKit::WebString; |
| 42 using WebKit::WebURL; | 43 using WebKit::WebURL; |
| 43 using WebKit::WebURLError; | 44 using WebKit::WebURLError; |
| 44 using WebKit::WebURLLoader; | 45 using WebKit::WebURLLoader; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 PPB_URLLoader_API* PPB_URLLoader_Impl::AsPPB_URLLoader_API() { | 90 PPB_URLLoader_API* PPB_URLLoader_Impl::AsPPB_URLLoader_API() { |
| 90 return this; | 91 return this; |
| 91 } | 92 } |
| 92 | 93 |
| 93 void PPB_URLLoader_Impl::InstanceWasDeleted() { | 94 void PPB_URLLoader_Impl::InstanceWasDeleted() { |
| 94 Resource::InstanceWasDeleted(); | 95 Resource::InstanceWasDeleted(); |
| 95 loader_.reset(); | 96 loader_.reset(); |
| 96 } | 97 } |
| 97 | 98 |
| 98 int32_t PPB_URLLoader_Impl::Open(PP_Resource request_id, | 99 int32_t PPB_URLLoader_Impl::Open(PP_Resource request_id, |
| 99 PP_CompletionCallback callback) { | 100 ApiCallbackType callback) { |
| 100 // Main document loads are already open, so don't allow people to open them | 101 // Main document loads are already open, so don't allow people to open them |
| 101 // again. | 102 // again. |
| 102 if (main_document_loader_) | 103 if (main_document_loader_) |
| 103 return PP_ERROR_INPROGRESS; | 104 return PP_ERROR_INPROGRESS; |
| 104 | 105 |
| 105 EnterResourceNoLock<PPB_URLRequestInfo_API> enter_request(request_id, true); | 106 EnterResourceNoLock<PPB_URLRequestInfo_API> enter_request(request_id, true); |
| 106 if (enter_request.failed()) { | 107 if (enter_request.failed()) { |
| 107 Log(PP_LOGLEVEL_ERROR, | 108 Log(PP_LOGLEVEL_ERROR, |
| 108 "PPB_URLLoader.Open: invalid request resource ID. (Hint to C++ wrapper" | 109 "PPB_URLLoader.Open: invalid request resource ID. (Hint to C++ wrapper" |
| 109 " users: use the ResourceRequest constructor that takes an instance or" | 110 " users: use the ResourceRequest constructor that takes an instance or" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 if (!loader_.get()) | 166 if (!loader_.get()) |
| 166 return PP_ERROR_FAILED; | 167 return PP_ERROR_FAILED; |
| 167 | 168 |
| 168 loader_->loadAsynchronously(web_request, this); | 169 loader_->loadAsynchronously(web_request, this); |
| 169 | 170 |
| 170 // Notify completion when we receive a redirect or response headers. | 171 // Notify completion when we receive a redirect or response headers. |
| 171 RegisterCallback(callback); | 172 RegisterCallback(callback); |
| 172 return PP_OK_COMPLETIONPENDING; | 173 return PP_OK_COMPLETIONPENDING; |
| 173 } | 174 } |
| 174 | 175 |
| 175 int32_t PPB_URLLoader_Impl::FollowRedirect(PP_CompletionCallback callback) { | 176 int32_t PPB_URLLoader_Impl::FollowRedirect(ApiCallbackType callback) { |
| 176 int32_t rv = ValidateCallback(callback); | 177 int32_t rv = ValidateCallback(callback); |
| 177 if (rv != PP_OK) | 178 if (rv != PP_OK) |
| 178 return rv; | 179 return rv; |
| 179 | 180 |
| 180 WebURL redirect_url = GURL(response_info_->redirect_url()); | 181 WebURL redirect_url = GURL(response_info_->redirect_url()); |
| 181 | 182 |
| 182 SetDefersLoading(false); // Allow the redirect to continue. | 183 SetDefersLoading(false); // Allow the redirect to continue. |
| 183 RegisterCallback(callback); | 184 RegisterCallback(callback); |
| 184 return PP_OK_COMPLETIONPENDING; | 185 return PP_OK_COMPLETIONPENDING; |
| 185 } | 186 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 210 } | 211 } |
| 211 | 212 |
| 212 PP_Resource PPB_URLLoader_Impl::GetResponseInfo() { | 213 PP_Resource PPB_URLLoader_Impl::GetResponseInfo() { |
| 213 if (!response_info_) | 214 if (!response_info_) |
| 214 return 0; | 215 return 0; |
| 215 return response_info_->GetReference(); | 216 return response_info_->GetReference(); |
| 216 } | 217 } |
| 217 | 218 |
| 218 int32_t PPB_URLLoader_Impl::ReadResponseBody(void* buffer, | 219 int32_t PPB_URLLoader_Impl::ReadResponseBody(void* buffer, |
| 219 int32_t bytes_to_read, | 220 int32_t bytes_to_read, |
| 220 PP_CompletionCallback callback) { | 221 ApiCallbackType callback) { |
| 221 int32_t rv = ValidateCallback(callback); | 222 int32_t rv = ValidateCallback(callback); |
| 222 if (rv != PP_OK) | 223 if (rv != PP_OK) |
| 223 return rv; | 224 return rv; |
| 224 if (!response_info_ || response_info_->body()) | 225 if (!response_info_ || response_info_->body()) |
| 225 return PP_ERROR_FAILED; | 226 return PP_ERROR_FAILED; |
| 226 if (bytes_to_read <= 0 || !buffer) | 227 if (bytes_to_read <= 0 || !buffer) |
| 227 return PP_ERROR_BADARGUMENT; | 228 return PP_ERROR_BADARGUMENT; |
| 228 | 229 |
| 229 user_buffer_ = static_cast<char*>(buffer); | 230 user_buffer_ = static_cast<char*>(buffer); |
| 230 user_buffer_size_ = bytes_to_read; | 231 user_buffer_size_ = bytes_to_read; |
| 231 | 232 |
| 232 if (!buffer_.empty()) | 233 if (!buffer_.empty()) |
| 233 return FillUserBuffer(); | 234 return FillUserBuffer(); |
| 234 | 235 |
| 235 // We may have already reached EOF. | 236 // We may have already reached EOF. |
| 236 if (done_status_ != PP_OK_COMPLETIONPENDING) { | 237 if (done_status_ != PP_OK_COMPLETIONPENDING) { |
| 237 user_buffer_ = NULL; | 238 user_buffer_ = NULL; |
| 238 user_buffer_size_ = 0; | 239 user_buffer_size_ = 0; |
| 239 return done_status_; | 240 return done_status_; |
| 240 } | 241 } |
| 241 | 242 |
| 242 RegisterCallback(callback); | 243 RegisterCallback(callback); |
| 243 return PP_OK_COMPLETIONPENDING; | 244 return PP_OK_COMPLETIONPENDING; |
| 244 } | 245 } |
| 245 | 246 |
| 246 int32_t PPB_URLLoader_Impl::FinishStreamingToFile( | 247 int32_t PPB_URLLoader_Impl::FinishStreamingToFile( |
| 247 PP_CompletionCallback callback) { | 248 ApiCallbackType callback) { |
| 248 int32_t rv = ValidateCallback(callback); | 249 int32_t rv = ValidateCallback(callback); |
| 249 if (rv != PP_OK) | 250 if (rv != PP_OK) |
| 250 return rv; | 251 return rv; |
| 251 if (!response_info_ || !response_info_->body()) | 252 if (!response_info_ || !response_info_->body()) |
| 252 return PP_ERROR_FAILED; | 253 return PP_ERROR_FAILED; |
| 253 | 254 |
| 254 // We may have already reached EOF. | 255 // We may have already reached EOF. |
| 255 if (done_status_ != PP_OK_COMPLETIONPENDING) | 256 if (done_status_ != PP_OK_COMPLETIONPENDING) |
| 256 return done_status_; | 257 return done_status_; |
| 257 | 258 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 | 389 |
| 389 void PPB_URLLoader_Impl::FinishLoading(int32_t done_status) { | 390 void PPB_URLLoader_Impl::FinishLoading(int32_t done_status) { |
| 390 done_status_ = done_status; | 391 done_status_ = done_status; |
| 391 // If the client hasn't called any function that takes a callback since | 392 // If the client hasn't called any function that takes a callback since |
| 392 // the initial call to Open, or called ReadResponseBody and got a | 393 // the initial call to Open, or called ReadResponseBody and got a |
| 393 // synchronous return, then the callback will be NULL. | 394 // synchronous return, then the callback will be NULL. |
| 394 if (TrackedCallback::IsPending(pending_callback_)) | 395 if (TrackedCallback::IsPending(pending_callback_)) |
| 395 RunCallback(done_status_); | 396 RunCallback(done_status_); |
| 396 } | 397 } |
| 397 | 398 |
| 398 int32_t PPB_URLLoader_Impl::ValidateCallback(PP_CompletionCallback callback) { | 399 int32_t PPB_URLLoader_Impl::ValidateCallback( |
| 399 // We only support non-blocking calls. | 400 scoped_refptr<TrackedCallback> callback) { |
| 400 if (!callback.func) | 401 DCHECK(callback); |
| 401 return PP_ERROR_BLOCKS_MAIN_THREAD; | |
| 402 | 402 |
| 403 if (TrackedCallback::IsPending(pending_callback_)) | 403 if (TrackedCallback::IsPending(pending_callback_)) |
| 404 return PP_ERROR_INPROGRESS; | 404 return PP_ERROR_INPROGRESS; |
| 405 | 405 |
| 406 return PP_OK; | 406 return PP_OK; |
| 407 } | 407 } |
| 408 | 408 |
| 409 void PPB_URLLoader_Impl::RegisterCallback(PP_CompletionCallback callback) { | 409 void PPB_URLLoader_Impl::RegisterCallback( |
| 410 DCHECK(callback.func); | 410 scoped_refptr<TrackedCallback> callback) { |
| 411 DCHECK(!TrackedCallback::IsPending(pending_callback_)); | 411 DCHECK(!TrackedCallback::IsPending(pending_callback_)); |
| 412 | 412 |
| 413 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); | 413 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); |
| 414 if (!plugin_module) | 414 if (!plugin_module) |
| 415 return; | 415 return; |
| 416 | 416 |
| 417 pending_callback_ = new TrackedCallback(this, callback); | 417 pending_callback_ = callback; |
| 418 } | 418 } |
| 419 | 419 |
| 420 void PPB_URLLoader_Impl::RunCallback(int32_t result) { | 420 void PPB_URLLoader_Impl::RunCallback(int32_t result) { |
| 421 // This may be null only when this is a main document loader. | 421 // This may be null only when this is a main document loader. |
| 422 if (!pending_callback_.get()) { | 422 if (!pending_callback_.get()) { |
| 423 CHECK(main_document_loader_); | 423 CHECK(main_document_loader_); |
| 424 return; | 424 return; |
| 425 } | 425 } |
| 426 TrackedCallback::ClearAndRun(&pending_callback_, result); | 426 TrackedCallback::ClearAndRun(&pending_callback_, result); |
| 427 } | 427 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 bool PPB_URLLoader_Impl::RecordDownloadProgress() const { | 475 bool PPB_URLLoader_Impl::RecordDownloadProgress() const { |
| 476 return request_data_.record_download_progress; | 476 return request_data_.record_download_progress; |
| 477 } | 477 } |
| 478 | 478 |
| 479 bool PPB_URLLoader_Impl::RecordUploadProgress() const { | 479 bool PPB_URLLoader_Impl::RecordUploadProgress() const { |
| 480 return request_data_.record_upload_progress; | 480 return request_data_.record_upload_progress; |
| 481 } | 481 } |
| 482 | 482 |
| 483 } // namespace ppapi | 483 } // namespace ppapi |
| 484 } // namespace webkit | 484 } // namespace webkit |
| OLD | NEW |