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