| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "ppapi/proxy/url_loader_resource.h" | 5 #include "ppapi/proxy/url_loader_resource.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/proxy/dispatch_reply_message.h" | 11 #include "ppapi/proxy/dispatch_reply_message.h" |
| 12 #include "ppapi/proxy/file_ref_resource.h" |
| 12 #include "ppapi/proxy/ppapi_messages.h" | 13 #include "ppapi/proxy/ppapi_messages.h" |
| 13 #include "ppapi/proxy/ppb_file_ref_proxy.h" | |
| 14 #include "ppapi/proxy/url_request_info_resource.h" | 14 #include "ppapi/proxy/url_request_info_resource.h" |
| 15 #include "ppapi/proxy/url_response_info_resource.h" | 15 #include "ppapi/proxy/url_response_info_resource.h" |
| 16 #include "ppapi/shared_impl/ppapi_globals.h" | 16 #include "ppapi/shared_impl/ppapi_globals.h" |
| 17 #include "ppapi/shared_impl/url_response_info_data.h" | 17 #include "ppapi/shared_impl/url_response_info_data.h" |
| 18 #include "ppapi/thunk/enter.h" | 18 #include "ppapi/thunk/enter.h" |
| 19 #include "ppapi/thunk/resource_creation_api.h" | 19 #include "ppapi/thunk/resource_creation_api.h" |
| 20 | 20 |
| 21 using ppapi::thunk::EnterResourceNoLock; | 21 using ppapi::thunk::EnterResourceNoLock; |
| 22 using ppapi::thunk::PPB_URLLoader_API; | 22 using ppapi::thunk::PPB_URLLoader_API; |
| 23 using ppapi::thunk::PPB_URLRequestInfo_API; | 23 using ppapi::thunk::PPB_URLRequestInfo_API; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 return 0; | 151 return 0; |
| 152 } | 152 } |
| 153 | 153 |
| 154 int32_t URLLoaderResource::ReadResponseBody( | 154 int32_t URLLoaderResource::ReadResponseBody( |
| 155 void* buffer, | 155 void* buffer, |
| 156 int32_t bytes_to_read, | 156 int32_t bytes_to_read, |
| 157 scoped_refptr<TrackedCallback> callback) { | 157 scoped_refptr<TrackedCallback> callback) { |
| 158 int32_t rv = ValidateCallback(callback); | 158 int32_t rv = ValidateCallback(callback); |
| 159 if (rv != PP_OK) | 159 if (rv != PP_OK) |
| 160 return rv; | 160 return rv; |
| 161 if (!response_info_.get() || | 161 if (!response_info_.get()) |
| 162 !response_info_->data().body_as_file_ref.resource.is_null()) | |
| 163 return PP_ERROR_FAILED; | 162 return PP_ERROR_FAILED; |
| 163 |
| 164 // Fail if we have a valid file ref. |
| 165 // ReadResponseBody() is for reading to a user-provided buffer. |
| 166 if (response_info_->data().body_as_file_ref.IsValid()) |
| 167 return PP_ERROR_FAILED; |
| 168 |
| 164 if (bytes_to_read <= 0 || !buffer) | 169 if (bytes_to_read <= 0 || !buffer) |
| 165 return PP_ERROR_BADARGUMENT; | 170 return PP_ERROR_BADARGUMENT; |
| 166 | 171 |
| 167 user_buffer_ = static_cast<char*>(buffer); | 172 user_buffer_ = static_cast<char*>(buffer); |
| 168 user_buffer_size_ = bytes_to_read; | 173 user_buffer_size_ = bytes_to_read; |
| 169 | 174 |
| 170 if (!buffer_.empty()) | 175 if (!buffer_.empty()) |
| 171 return FillUserBuffer(); | 176 return FillUserBuffer(); |
| 172 | 177 |
| 173 // We may have already reached EOF. | 178 // We may have already reached EOF. |
| 174 if (done_status_ != PP_OK_COMPLETIONPENDING) { | 179 if (done_status_ != PP_OK_COMPLETIONPENDING) { |
| 175 user_buffer_ = NULL; | 180 user_buffer_ = NULL; |
| 176 user_buffer_size_ = 0; | 181 user_buffer_size_ = 0; |
| 177 return done_status_; | 182 return done_status_; |
| 178 } | 183 } |
| 179 | 184 |
| 180 RegisterCallback(callback); | 185 RegisterCallback(callback); |
| 181 return PP_OK_COMPLETIONPENDING; | 186 return PP_OK_COMPLETIONPENDING; |
| 182 } | 187 } |
| 183 | 188 |
| 184 int32_t URLLoaderResource::FinishStreamingToFile( | 189 int32_t URLLoaderResource::FinishStreamingToFile( |
| 185 scoped_refptr<TrackedCallback> callback) { | 190 scoped_refptr<TrackedCallback> callback) { |
| 186 int32_t rv = ValidateCallback(callback); | 191 int32_t rv = ValidateCallback(callback); |
| 187 if (rv != PP_OK) | 192 if (rv != PP_OK) |
| 188 return rv; | 193 return rv; |
| 189 if (!response_info_.get() || | 194 if (!response_info_.get()) |
| 190 response_info_->data().body_as_file_ref.resource.is_null()) | 195 return PP_ERROR_FAILED; |
| 196 |
| 197 // Fail if we do not have a valid file ref. |
| 198 if (!response_info_->data().body_as_file_ref.IsValid()) |
| 191 return PP_ERROR_FAILED; | 199 return PP_ERROR_FAILED; |
| 192 | 200 |
| 193 // We may have already reached EOF. | 201 // We may have already reached EOF. |
| 194 if (done_status_ != PP_OK_COMPLETIONPENDING) | 202 if (done_status_ != PP_OK_COMPLETIONPENDING) |
| 195 return done_status_; | 203 return done_status_; |
| 196 | 204 |
| 197 is_streaming_to_file_ = true; | 205 is_streaming_to_file_ = true; |
| 198 if (is_asynchronous_load_suspended_) | 206 if (is_asynchronous_load_suspended_) |
| 199 SetDefersLoading(false); | 207 SetDefersLoading(false); |
| 200 | 208 |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 // As a second line of defense, clear the |user_buffer_| in case the | 358 // As a second line of defense, clear the |user_buffer_| in case the |
| 351 // callbacks get called in an unexpected order. | 359 // callbacks get called in an unexpected order. |
| 352 user_buffer_ = NULL; | 360 user_buffer_ = NULL; |
| 353 user_buffer_size_ = 0; | 361 user_buffer_size_ = 0; |
| 354 pending_callback_->Run(result); | 362 pending_callback_->Run(result); |
| 355 } | 363 } |
| 356 | 364 |
| 357 void URLLoaderResource::SaveResponseInfo(const URLResponseInfoData& data) { | 365 void URLLoaderResource::SaveResponseInfo(const URLResponseInfoData& data) { |
| 358 // Create a proxy resource for the the file ref host resource if needed. | 366 // Create a proxy resource for the the file ref host resource if needed. |
| 359 PP_Resource body_as_file_ref = 0; | 367 PP_Resource body_as_file_ref = 0; |
| 360 if (!data.body_as_file_ref.resource.is_null()) { | 368 if (data.body_as_file_ref.IsValid()) { |
| 361 thunk::EnterResourceCreationNoLock enter(pp_instance()); | 369 body_as_file_ref = FileRefResource::CreateFileRef(connection(), |
| 362 body_as_file_ref = | 370 pp_instance(), |
| 363 enter.functions()->CreateFileRef(data.body_as_file_ref); | 371 data.body_as_file_ref); |
| 364 } | 372 } |
| 365 response_info_ = new URLResponseInfoResource( | 373 response_info_ = new URLResponseInfoResource( |
| 366 connection(), pp_instance(), data, body_as_file_ref); | 374 connection(), pp_instance(), data, body_as_file_ref); |
| 367 } | 375 } |
| 368 | 376 |
| 369 size_t URLLoaderResource::FillUserBuffer() { | 377 size_t URLLoaderResource::FillUserBuffer() { |
| 370 DCHECK(user_buffer_); | 378 DCHECK(user_buffer_); |
| 371 DCHECK(user_buffer_size_); | 379 DCHECK(user_buffer_size_); |
| 372 | 380 |
| 373 size_t bytes_to_copy = std::min(buffer_.size(), user_buffer_size_); | 381 size_t bytes_to_copy = std::min(buffer_.size(), user_buffer_size_); |
| 374 std::copy(buffer_.begin(), buffer_.begin() + bytes_to_copy, user_buffer_); | 382 std::copy(buffer_.begin(), buffer_.begin() + bytes_to_copy, user_buffer_); |
| 375 buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy); | 383 buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy); |
| 376 | 384 |
| 377 // If the buffer is getting too empty, resume asynchronous loading. | 385 // If the buffer is getting too empty, resume asynchronous loading. |
| 378 if (is_asynchronous_load_suspended_ && | 386 if (is_asynchronous_load_suspended_ && |
| 379 buffer_.size() <= static_cast<size_t>( | 387 buffer_.size() <= static_cast<size_t>( |
| 380 request_data_.prefetch_buffer_lower_threshold)) { | 388 request_data_.prefetch_buffer_lower_threshold)) { |
| 381 DVLOG(1) << "Resuming async load - buffer size: " << buffer_.size(); | 389 DVLOG(1) << "Resuming async load - buffer size: " << buffer_.size(); |
| 382 SetDefersLoading(false); | 390 SetDefersLoading(false); |
| 383 } | 391 } |
| 384 | 392 |
| 385 // Reset for next time. | 393 // Reset for next time. |
| 386 user_buffer_ = NULL; | 394 user_buffer_ = NULL; |
| 387 user_buffer_size_ = 0; | 395 user_buffer_size_ = 0; |
| 388 return bytes_to_copy; | 396 return bytes_to_copy; |
| 389 } | 397 } |
| 390 | 398 |
| 391 } // namespace proxy | 399 } // namespace proxy |
| 392 } // namespace ppapi | 400 } // namespace ppapi |
| OLD | NEW |