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