| 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 // This file contains an implementation of the ResourceLoaderBridge class. | 5 // This file contains an implementation of the ResourceLoaderBridge class. |
| 6 // The class is implemented using net::URLRequest, meaning it is a "simple" | 6 // The class is implemented using net::URLRequest, meaning it is a "simple" |
| 7 // version that directly issues requests. The more complicated one used in the | 7 // version that directly issues requests. The more complicated one used in the |
| 8 // browser uses IPC. | 8 // browser uses IPC. |
| 9 // | 9 // |
| 10 // Because net::URLRequest only provides an asynchronous resource loading API, | 10 // Because net::URLRequest only provides an asynchronous resource loading API, |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 // -------------------------------------------------------------------------- | 230 // -------------------------------------------------------------------------- |
| 231 // The following methods are called on the owner's thread in response to | 231 // The following methods are called on the owner's thread in response to |
| 232 // various net::URLRequest callbacks. The event hooks, defined below, trigger | 232 // various net::URLRequest callbacks. The event hooks, defined below, trigger |
| 233 // these methods asynchronously. | 233 // these methods asynchronously. |
| 234 | 234 |
| 235 void NotifyReceivedRedirect(const GURL& new_url, | 235 void NotifyReceivedRedirect(const GURL& new_url, |
| 236 const ResourceResponseInfo& info) { | 236 const ResourceResponseInfo& info) { |
| 237 bool has_new_first_party_for_cookies = false; | 237 bool has_new_first_party_for_cookies = false; |
| 238 GURL new_first_party_for_cookies; | 238 GURL new_first_party_for_cookies; |
| 239 if (peer_ && peer_->OnReceivedRedirect(new_url, info, | 239 if (peer_ && peer_->OnReceivedRedirect(new_url, info, |
| 240 base::TimeTicks(), base::TimeTicks(), |
| 240 &has_new_first_party_for_cookies, | 241 &has_new_first_party_for_cookies, |
| 241 &new_first_party_for_cookies)) { | 242 &new_first_party_for_cookies)) { |
| 242 g_io_thread->message_loop()->PostTask( | 243 g_io_thread->message_loop()->PostTask( |
| 243 FROM_HERE, | 244 FROM_HERE, |
| 244 base::Bind(&RequestProxy::AsyncFollowDeferredRedirect, this, | 245 base::Bind(&RequestProxy::AsyncFollowDeferredRedirect, this, |
| 245 has_new_first_party_for_cookies, | 246 has_new_first_party_for_cookies, |
| 246 new_first_party_for_cookies)); | 247 new_first_party_for_cookies)); |
| 247 } else { | 248 } else { |
| 248 Cancel(); | 249 Cancel(); |
| 249 } | 250 } |
| 250 } | 251 } |
| 251 | 252 |
| 252 void NotifyReceivedResponse(const ResourceResponseInfo& info) { | 253 void NotifyReceivedResponse(const ResourceResponseInfo& info) { |
| 253 if (peer_) | 254 if (peer_) |
| 254 peer_->OnReceivedResponse(info); | 255 peer_->OnReceivedResponse(info, base::TimeTicks(), base::TimeTicks()); |
| 255 } | 256 } |
| 256 | 257 |
| 257 void NotifyReceivedData(int bytes_read) { | 258 void NotifyReceivedData(int bytes_read) { |
| 258 if (!peer_) | 259 if (!peer_) |
| 259 return; | 260 return; |
| 260 | 261 |
| 261 // Make a local copy of buf_, since AsyncReadData reuses it. | 262 // Make a local copy of buf_, since AsyncReadData reuses it. |
| 262 scoped_array<char> buf_copy(new char[bytes_read]); | 263 scoped_array<char> buf_copy(new char[bytes_read]); |
| 263 memcpy(buf_copy.get(), buf_->data(), bytes_read); | 264 memcpy(buf_copy.get(), buf_->data(), bytes_read); |
| 264 | 265 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 283 // Continue reading more data, see the comment in NotifyReceivedData. | 284 // Continue reading more data, see the comment in NotifyReceivedData. |
| 284 g_io_thread->message_loop()->PostTask( | 285 g_io_thread->message_loop()->PostTask( |
| 285 FROM_HERE, | 286 FROM_HERE, |
| 286 base::Bind(&RequestProxy::AsyncReadData, this)); | 287 base::Bind(&RequestProxy::AsyncReadData, this)); |
| 287 | 288 |
| 288 peer_->OnDownloadedData(bytes_read); | 289 peer_->OnDownloadedData(bytes_read); |
| 289 } | 290 } |
| 290 | 291 |
| 291 void NotifyCompletedRequest(const net::URLRequestStatus& status, | 292 void NotifyCompletedRequest(const net::URLRequestStatus& status, |
| 292 const std::string& security_info, | 293 const std::string& security_info, |
| 293 const base::Time& complete_time) { | 294 const base::TimeTicks& complete_time) { |
| 294 if (peer_) { | 295 if (peer_) { |
| 295 peer_->OnCompletedRequest(status, security_info, complete_time); | 296 peer_->OnCompletedRequest(status, security_info, complete_time); |
| 296 DropPeer(); // ensure no further notifications | 297 DropPeer(); // ensure no further notifications |
| 297 } | 298 } |
| 298 } | 299 } |
| 299 | 300 |
| 300 void NotifyUploadProgress(uint64 position, uint64 size) { | 301 void NotifyUploadProgress(uint64 position, uint64 size) { |
| 301 if (peer_) | 302 if (peer_) |
| 302 peer_->OnUploadProgress(position, size); | 303 peer_->OnUploadProgress(position, size); |
| 303 } | 304 } |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 return; | 419 return; |
| 419 } | 420 } |
| 420 | 421 |
| 421 owner_loop_->PostTask( | 422 owner_loop_->PostTask( |
| 422 FROM_HERE, | 423 FROM_HERE, |
| 423 base::Bind(&RequestProxy::NotifyReceivedData, this, bytes_read)); | 424 base::Bind(&RequestProxy::NotifyReceivedData, this, bytes_read)); |
| 424 } | 425 } |
| 425 | 426 |
| 426 virtual void OnCompletedRequest(const net::URLRequestStatus& status, | 427 virtual void OnCompletedRequest(const net::URLRequestStatus& status, |
| 427 const std::string& security_info, | 428 const std::string& security_info, |
| 428 const base::Time& complete_time) { | 429 const base::TimeTicks& complete_time) { |
| 429 if (download_to_file_) | 430 if (download_to_file_) |
| 430 file_stream_.Close(); | 431 file_stream_.Close(); |
| 431 owner_loop_->PostTask( | 432 owner_loop_->PostTask( |
| 432 FROM_HERE, | 433 FROM_HERE, |
| 433 base::Bind(&RequestProxy::NotifyCompletedRequest, this, status, | 434 base::Bind(&RequestProxy::NotifyCompletedRequest, this, status, |
| 434 security_info, complete_time)); | 435 security_info, complete_time)); |
| 435 } | 436 } |
| 436 | 437 |
| 437 // -------------------------------------------------------------------------- | 438 // -------------------------------------------------------------------------- |
| 438 // net::URLRequest::Delegate implementation: | 439 // net::URLRequest::Delegate implementation: |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 if (upload_progress_timer_.IsRunning()) { | 515 if (upload_progress_timer_.IsRunning()) { |
| 515 MaybeUpdateUploadProgress(); | 516 MaybeUpdateUploadProgress(); |
| 516 upload_progress_timer_.Stop(); | 517 upload_progress_timer_.Stop(); |
| 517 } | 518 } |
| 518 DCHECK(request_.get()); | 519 DCHECK(request_.get()); |
| 519 // If |failed_file_request_status_| is not empty, which means the request | 520 // If |failed_file_request_status_| is not empty, which means the request |
| 520 // was a file request and encountered an error, then we need to use the | 521 // was a file request and encountered an error, then we need to use the |
| 521 // |failed_file_request_status_|. Otherwise use request_'s status. | 522 // |failed_file_request_status_|. Otherwise use request_'s status. |
| 522 OnCompletedRequest(failed_file_request_status_.get() ? | 523 OnCompletedRequest(failed_file_request_status_.get() ? |
| 523 *failed_file_request_status_ : request_->status(), | 524 *failed_file_request_status_ : request_->status(), |
| 524 std::string(), base::Time()); | 525 std::string(), base::TimeTicks()); |
| 525 request_.reset(); // destroy on the io thread | 526 request_.reset(); // destroy on the io thread |
| 526 } | 527 } |
| 527 | 528 |
| 528 // Called on the IO thread. | 529 // Called on the IO thread. |
| 529 void MaybeUpdateUploadProgress() { | 530 void MaybeUpdateUploadProgress() { |
| 530 // If a redirect is received upload is cancelled in net::URLRequest, we | 531 // If a redirect is received upload is cancelled in net::URLRequest, we |
| 531 // should try to stop the |upload_progress_timer_| timer and return. | 532 // should try to stop the |upload_progress_timer_| timer and return. |
| 532 if (!request_->has_upload()) { | 533 if (!request_->has_upload()) { |
| 533 if (upload_progress_timer_.IsRunning()) | 534 if (upload_progress_timer_.IsRunning()) |
| 534 upload_progress_timer_.Stop(); | 535 upload_progress_timer_.Stop(); |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 virtual void OnReceivedData(int bytes_read) { | 728 virtual void OnReceivedData(int bytes_read) { |
| 728 if (download_to_file_) | 729 if (download_to_file_) |
| 729 file_stream_.Write(buf_->data(), bytes_read, NULL); | 730 file_stream_.Write(buf_->data(), bytes_read, NULL); |
| 730 else | 731 else |
| 731 result_->data.append(buf_->data(), bytes_read); | 732 result_->data.append(buf_->data(), bytes_read); |
| 732 AsyncReadData(); // read more (may recurse) | 733 AsyncReadData(); // read more (may recurse) |
| 733 } | 734 } |
| 734 | 735 |
| 735 virtual void OnCompletedRequest(const net::URLRequestStatus& status, | 736 virtual void OnCompletedRequest(const net::URLRequestStatus& status, |
| 736 const std::string& security_info, | 737 const std::string& security_info, |
| 737 const base::Time& complete_time) { | 738 const base::TimeTicks& complete_time) { |
| 738 if (download_to_file_) | 739 if (download_to_file_) |
| 739 file_stream_.Close(); | 740 file_stream_.Close(); |
| 740 result_->status = status; | 741 result_->status = status; |
| 741 event_.Signal(); | 742 event_.Signal(); |
| 742 } | 743 } |
| 743 | 744 |
| 744 private: | 745 private: |
| 745 ResourceLoaderBridge::SyncLoadResponse* result_; | 746 ResourceLoaderBridge::SyncLoadResponse* result_; |
| 746 base::WaitableEvent event_; | 747 base::WaitableEvent event_; |
| 747 }; | 748 }; |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1044 | 1045 |
| 1045 // static | 1046 // static |
| 1046 void SimpleResourceLoaderBridge::AllowFileOverHTTP( | 1047 void SimpleResourceLoaderBridge::AllowFileOverHTTP( |
| 1047 const std::string& file_path_template, const GURL& http_prefix) { | 1048 const std::string& file_path_template, const GURL& http_prefix) { |
| 1048 DCHECK(!file_path_template.empty()); | 1049 DCHECK(!file_path_template.empty()); |
| 1049 DCHECK(http_prefix.is_valid() && | 1050 DCHECK(http_prefix.is_valid() && |
| 1050 (http_prefix.SchemeIs("http") || http_prefix.SchemeIs("https"))); | 1051 (http_prefix.SchemeIs("http") || http_prefix.SchemeIs("https"))); |
| 1051 g_file_over_http_params = new FileOverHTTPParams(file_path_template, | 1052 g_file_over_http_params = new FileOverHTTPParams(file_path_template, |
| 1052 http_prefix); | 1053 http_prefix); |
| 1053 } | 1054 } |
| OLD | NEW |