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