OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 | 106 |
107 if (request->RequiresUniversalAccess() && !has_universal_access_) | 107 if (request->RequiresUniversalAccess() && !has_universal_access_) |
108 return PP_ERROR_NOACCESS; | 108 return PP_ERROR_NOACCESS; |
109 | 109 |
110 if (loader_.get()) | 110 if (loader_.get()) |
111 return PP_ERROR_INPROGRESS; | 111 return PP_ERROR_INPROGRESS; |
112 | 112 |
113 WebFrame* frame = GetFrameForResource(this); | 113 WebFrame* frame = GetFrameForResource(this); |
114 if (!frame) | 114 if (!frame) |
115 return PP_ERROR_FAILED; | 115 return PP_ERROR_FAILED; |
116 WebURLRequest web_request(request->ToWebURLRequest(frame)); | 116 WebURLRequest web_request; |
| 117 if (!request->ToWebURLRequest(frame, &web_request)) |
| 118 return PP_ERROR_FAILED; |
| 119 |
| 120 // Save a copy of the request info so the plugin can continue to use and |
| 121 // change it while we're doing the request without affecting us. We must do |
| 122 // this after ToWebURLRequest since that fills out the file refs. |
| 123 request_data_ = request->GetData(); |
117 | 124 |
118 WebURLLoaderOptions options; | 125 WebURLLoaderOptions options; |
119 if (has_universal_access_) { | 126 if (has_universal_access_) { |
120 // Universal access allows cross-origin requests and sends credentials. | 127 // Universal access allows cross-origin requests and sends credentials. |
121 options.crossOriginRequestPolicy = | 128 options.crossOriginRequestPolicy = |
122 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; | 129 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; |
123 options.allowCredentials = true; | 130 options.allowCredentials = true; |
124 } else if (request->allow_cross_origin_requests()) { | 131 } else if (request_data_.allow_cross_origin_requests) { |
125 // Otherwise, allow cross-origin requests with access control. | 132 // Otherwise, allow cross-origin requests with access control. |
126 options.crossOriginRequestPolicy = | 133 options.crossOriginRequestPolicy = |
127 WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl; | 134 WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl; |
128 options.allowCredentials = request->allow_credentials(); | 135 options.allowCredentials = request_data_.allow_credentials; |
129 } | 136 } |
130 | 137 |
131 is_asynchronous_load_suspended_ = false; | 138 is_asynchronous_load_suspended_ = false; |
132 loader_.reset(frame->createAssociatedURLLoader(options)); | 139 loader_.reset(frame->createAssociatedURLLoader(options)); |
133 if (!loader_.get()) | 140 if (!loader_.get()) |
134 return PP_ERROR_FAILED; | 141 return PP_ERROR_FAILED; |
135 | 142 |
136 loader_->loadAsynchronously(web_request, this); | 143 loader_->loadAsynchronously(web_request, this); |
137 | 144 |
138 request_info_ = scoped_refptr<PPB_URLRequestInfo_Impl>(request); | |
139 | |
140 // Notify completion when we receive a redirect or response headers. | 145 // Notify completion when we receive a redirect or response headers. |
141 RegisterCallback(callback); | 146 RegisterCallback(callback); |
142 return PP_OK_COMPLETIONPENDING; | 147 return PP_OK_COMPLETIONPENDING; |
143 } | 148 } |
144 | 149 |
145 int32_t PPB_URLLoader_Impl::FollowRedirect(PP_CompletionCallback callback) { | 150 int32_t PPB_URLLoader_Impl::FollowRedirect(PP_CompletionCallback callback) { |
146 int32_t rv = ValidateCallback(callback); | 151 int32_t rv = ValidateCallback(callback); |
147 if (rv != PP_OK) | 152 if (rv != PP_OK) |
148 return rv; | 153 return rv; |
149 | 154 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 | 256 |
252 void PPB_URLLoader_Impl::SetStatusCallback( | 257 void PPB_URLLoader_Impl::SetStatusCallback( |
253 PP_URLLoaderTrusted_StatusCallback cb) { | 258 PP_URLLoaderTrusted_StatusCallback cb) { |
254 status_callback_ = cb; | 259 status_callback_ = cb; |
255 } | 260 } |
256 | 261 |
257 void PPB_URLLoader_Impl::willSendRequest( | 262 void PPB_URLLoader_Impl::willSendRequest( |
258 WebURLLoader* loader, | 263 WebURLLoader* loader, |
259 WebURLRequest& new_request, | 264 WebURLRequest& new_request, |
260 const WebURLResponse& redirect_response) { | 265 const WebURLResponse& redirect_response) { |
261 if (!request_info_->follow_redirects()) { | 266 if (!request_data_.follow_redirects) { |
262 SaveResponse(redirect_response); | 267 SaveResponse(redirect_response); |
263 loader_->setDefersLoading(true); | 268 loader_->setDefersLoading(true); |
264 RunCallback(PP_OK); | 269 RunCallback(PP_OK); |
265 } | 270 } |
266 } | 271 } |
267 | 272 |
268 void PPB_URLLoader_Impl::didSendData( | 273 void PPB_URLLoader_Impl::didSendData( |
269 WebURLLoader* loader, | 274 WebURLLoader* loader, |
270 unsigned long long bytes_sent, | 275 unsigned long long bytes_sent, |
271 unsigned long long total_bytes_to_be_sent) { | 276 unsigned long long total_bytes_to_be_sent) { |
(...skipping 29 matching lines...) Expand all Loading... |
301 buffer_.insert(buffer_.end(), data, data + data_length); | 306 buffer_.insert(buffer_.end(), data, data + data_length); |
302 if (user_buffer_) { | 307 if (user_buffer_) { |
303 RunCallback(FillUserBuffer()); | 308 RunCallback(FillUserBuffer()); |
304 } else { | 309 } else { |
305 DCHECK(!pending_callback_.get() || pending_callback_->completed()); | 310 DCHECK(!pending_callback_.get() || pending_callback_->completed()); |
306 } | 311 } |
307 | 312 |
308 // To avoid letting the network stack download an entire stream all at once, | 313 // To avoid letting the network stack download an entire stream all at once, |
309 // defer loading when we have enough buffer. | 314 // defer loading when we have enough buffer. |
310 // Check the buffer size after potentially moving some to the user buffer. | 315 // Check the buffer size after potentially moving some to the user buffer. |
311 DCHECK(!request_info_ || | 316 DCHECK(request_data_.prefetch_buffer_lower_threshold < |
312 (request_info_->prefetch_buffer_lower_threshold() < | 317 request_data_.prefetch_buffer_upper_threshold); |
313 request_info_->prefetch_buffer_upper_threshold())); | |
314 if (!is_streaming_to_file_ && | 318 if (!is_streaming_to_file_ && |
315 !is_asynchronous_load_suspended_ && | 319 !is_asynchronous_load_suspended_ && |
316 request_info_ && | |
317 (buffer_.size() >= static_cast<size_t>( | 320 (buffer_.size() >= static_cast<size_t>( |
318 request_info_->prefetch_buffer_upper_threshold()))) { | 321 request_data_.prefetch_buffer_upper_threshold))) { |
319 DVLOG(1) << "Suspending async load - buffer size: " << buffer_.size(); | 322 DVLOG(1) << "Suspending async load - buffer size: " << buffer_.size(); |
320 loader->setDefersLoading(true); | 323 loader->setDefersLoading(true); |
321 is_asynchronous_load_suspended_ = true; | 324 is_asynchronous_load_suspended_ = true; |
322 } | 325 } |
323 } | 326 } |
324 | 327 |
325 void PPB_URLLoader_Impl::didFinishLoading(WebURLLoader* loader, | 328 void PPB_URLLoader_Impl::didFinishLoading(WebURLLoader* loader, |
326 double finish_time) { | 329 double finish_time) { |
327 FinishLoading(PP_OK); | 330 FinishLoading(PP_OK); |
328 } | 331 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 | 393 |
391 size_t PPB_URLLoader_Impl::FillUserBuffer() { | 394 size_t PPB_URLLoader_Impl::FillUserBuffer() { |
392 DCHECK(user_buffer_); | 395 DCHECK(user_buffer_); |
393 DCHECK(user_buffer_size_); | 396 DCHECK(user_buffer_size_); |
394 | 397 |
395 size_t bytes_to_copy = std::min(buffer_.size(), user_buffer_size_); | 398 size_t bytes_to_copy = std::min(buffer_.size(), user_buffer_size_); |
396 std::copy(buffer_.begin(), buffer_.begin() + bytes_to_copy, user_buffer_); | 399 std::copy(buffer_.begin(), buffer_.begin() + bytes_to_copy, user_buffer_); |
397 buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy); | 400 buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy); |
398 | 401 |
399 // If the buffer is getting too empty, resume asynchronous loading. | 402 // If the buffer is getting too empty, resume asynchronous loading. |
400 DCHECK(!is_asynchronous_load_suspended_ || request_info_); | 403 DCHECK(!is_asynchronous_load_suspended_); |
401 if (is_asynchronous_load_suspended_ && | 404 if (is_asynchronous_load_suspended_ && |
402 buffer_.size() <= static_cast<size_t>( | 405 buffer_.size() <= static_cast<size_t>( |
403 request_info_->prefetch_buffer_lower_threshold())) { | 406 request_data_.prefetch_buffer_lower_threshold)) { |
404 DVLOG(1) << "Resuming async load - buffer size: " << buffer_.size(); | 407 DVLOG(1) << "Resuming async load - buffer size: " << buffer_.size(); |
405 loader_->setDefersLoading(false); | 408 loader_->setDefersLoading(false); |
406 is_asynchronous_load_suspended_ = false; | 409 is_asynchronous_load_suspended_ = false; |
407 } | 410 } |
408 | 411 |
409 // Reset for next time. | 412 // Reset for next time. |
410 user_buffer_ = NULL; | 413 user_buffer_ = NULL; |
411 user_buffer_size_ = 0; | 414 user_buffer_size_ = 0; |
412 return bytes_to_copy; | 415 return bytes_to_copy; |
413 } | 416 } |
(...skipping 16 matching lines...) Expand all Loading... |
430 status_callback_( | 433 status_callback_( |
431 pp_instance(), pp_resource(), | 434 pp_instance(), pp_resource(), |
432 RecordUploadProgress() ? bytes_sent_ : -1, | 435 RecordUploadProgress() ? bytes_sent_ : -1, |
433 RecordUploadProgress() ? total_bytes_to_be_sent_ : -1, | 436 RecordUploadProgress() ? total_bytes_to_be_sent_ : -1, |
434 RecordDownloadProgress() ? bytes_received_ : -1, | 437 RecordDownloadProgress() ? bytes_received_ : -1, |
435 RecordDownloadProgress() ? total_bytes_to_be_received_ : -1); | 438 RecordDownloadProgress() ? total_bytes_to_be_received_ : -1); |
436 } | 439 } |
437 } | 440 } |
438 | 441 |
439 bool PPB_URLLoader_Impl::RecordDownloadProgress() const { | 442 bool PPB_URLLoader_Impl::RecordDownloadProgress() const { |
440 return request_info_ && request_info_->record_download_progress(); | 443 return request_data_.record_download_progress; |
441 } | 444 } |
442 | 445 |
443 bool PPB_URLLoader_Impl::RecordUploadProgress() const { | 446 bool PPB_URLLoader_Impl::RecordUploadProgress() const { |
444 return request_info_ && request_info_->record_upload_progress(); | 447 return request_data_.record_upload_progress; |
445 } | 448 } |
446 | 449 |
447 } // namespace ppapi | 450 } // namespace ppapi |
448 } // namespace webkit | 451 } // namespace webkit |
OLD | NEW |