| 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 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 // -------------------------------------------------------------------------- | 228 // -------------------------------------------------------------------------- |
| 229 // The following methods are called on the owner's thread in response to | 229 // The following methods are called on the owner's thread in response to |
| 230 // various net::URLRequest callbacks. The event hooks, defined below, trigger | 230 // various net::URLRequest callbacks. The event hooks, defined below, trigger |
| 231 // these methods asynchronously. | 231 // these methods asynchronously. |
| 232 | 232 |
| 233 void NotifyReceivedRedirect(const GURL& new_url, | 233 void NotifyReceivedRedirect(const GURL& new_url, |
| 234 const ResourceResponseInfo& info) { | 234 const ResourceResponseInfo& info) { |
| 235 bool has_new_first_party_for_cookies = false; | 235 bool has_new_first_party_for_cookies = false; |
| 236 GURL new_first_party_for_cookies; | 236 GURL new_first_party_for_cookies; |
| 237 if (peer_ && peer_->OnReceivedRedirect(new_url, info, | 237 if (peer_ && peer_->OnReceivedRedirect(new_url, info, |
| 238 base::TimeTicks(), base::TimeTicks(), |
| 238 &has_new_first_party_for_cookies, | 239 &has_new_first_party_for_cookies, |
| 239 &new_first_party_for_cookies)) { | 240 &new_first_party_for_cookies)) { |
| 240 g_io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 241 g_io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 241 this, &RequestProxy::AsyncFollowDeferredRedirect, | 242 this, &RequestProxy::AsyncFollowDeferredRedirect, |
| 242 has_new_first_party_for_cookies, new_first_party_for_cookies)); | 243 has_new_first_party_for_cookies, new_first_party_for_cookies)); |
| 243 } else { | 244 } else { |
| 244 Cancel(); | 245 Cancel(); |
| 245 } | 246 } |
| 246 } | 247 } |
| 247 | 248 |
| 248 void NotifyReceivedResponse(const ResourceResponseInfo& info) { | 249 void NotifyReceivedResponse(const ResourceResponseInfo& info) { |
| 249 if (peer_) | 250 if (peer_) |
| 250 peer_->OnReceivedResponse(info); | 251 peer_->OnReceivedResponse(info, base::TimeTicks(), base::TimeTicks()); |
| 251 } | 252 } |
| 252 | 253 |
| 253 void NotifyReceivedData(int bytes_read) { | 254 void NotifyReceivedData(int bytes_read) { |
| 254 if (!peer_) | 255 if (!peer_) |
| 255 return; | 256 return; |
| 256 | 257 |
| 257 // Make a local copy of buf_, since AsyncReadData reuses it. | 258 // Make a local copy of buf_, since AsyncReadData reuses it. |
| 258 scoped_array<char> buf_copy(new char[bytes_read]); | 259 scoped_array<char> buf_copy(new char[bytes_read]); |
| 259 memcpy(buf_copy.get(), buf_->data(), bytes_read); | 260 memcpy(buf_copy.get(), buf_->data(), bytes_read); |
| 260 | 261 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 277 | 278 |
| 278 // Continue reading more data, see the comment in NotifyReceivedData. | 279 // Continue reading more data, see the comment in NotifyReceivedData. |
| 279 g_io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 280 g_io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 280 this, &RequestProxy::AsyncReadData)); | 281 this, &RequestProxy::AsyncReadData)); |
| 281 | 282 |
| 282 peer_->OnDownloadedData(bytes_read); | 283 peer_->OnDownloadedData(bytes_read); |
| 283 } | 284 } |
| 284 | 285 |
| 285 void NotifyCompletedRequest(const net::URLRequestStatus& status, | 286 void NotifyCompletedRequest(const net::URLRequestStatus& status, |
| 286 const std::string& security_info, | 287 const std::string& security_info, |
| 287 const base::Time& complete_time) { | 288 const base::TimeTicks& complete_time) { |
| 288 if (peer_) { | 289 if (peer_) { |
| 289 peer_->OnCompletedRequest(status, security_info, complete_time); | 290 peer_->OnCompletedRequest(status, security_info, complete_time); |
| 290 DropPeer(); // ensure no further notifications | 291 DropPeer(); // ensure no further notifications |
| 291 } | 292 } |
| 292 } | 293 } |
| 293 | 294 |
| 294 void NotifyUploadProgress(uint64 position, uint64 size) { | 295 void NotifyUploadProgress(uint64 position, uint64 size) { |
| 295 if (peer_) | 296 if (peer_) |
| 296 peer_->OnUploadProgress(position, size); | 297 peer_->OnUploadProgress(position, size); |
| 297 } | 298 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 this, &RequestProxy::NotifyDownloadedData, bytes_read)); | 409 this, &RequestProxy::NotifyDownloadedData, bytes_read)); |
| 409 return; | 410 return; |
| 410 } | 411 } |
| 411 | 412 |
| 412 owner_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 413 owner_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 413 this, &RequestProxy::NotifyReceivedData, bytes_read)); | 414 this, &RequestProxy::NotifyReceivedData, bytes_read)); |
| 414 } | 415 } |
| 415 | 416 |
| 416 virtual void OnCompletedRequest(const net::URLRequestStatus& status, | 417 virtual void OnCompletedRequest(const net::URLRequestStatus& status, |
| 417 const std::string& security_info, | 418 const std::string& security_info, |
| 418 const base::Time& complete_time) { | 419 const base::TimeTicks& complete_time) { |
| 419 if (download_to_file_) | 420 if (download_to_file_) |
| 420 file_stream_.Close(); | 421 file_stream_.Close(); |
| 421 owner_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 422 owner_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 422 this, | 423 this, |
| 423 &RequestProxy::NotifyCompletedRequest, | 424 &RequestProxy::NotifyCompletedRequest, |
| 424 status, | 425 status, |
| 425 security_info, | 426 security_info, |
| 426 complete_time)); | 427 complete_time)); |
| 427 } | 428 } |
| 428 | 429 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 if (upload_progress_timer_.IsRunning()) { | 507 if (upload_progress_timer_.IsRunning()) { |
| 507 MaybeUpdateUploadProgress(); | 508 MaybeUpdateUploadProgress(); |
| 508 upload_progress_timer_.Stop(); | 509 upload_progress_timer_.Stop(); |
| 509 } | 510 } |
| 510 DCHECK(request_.get()); | 511 DCHECK(request_.get()); |
| 511 // If |failed_file_request_status_| is not empty, which means the request | 512 // If |failed_file_request_status_| is not empty, which means the request |
| 512 // was a file request and encountered an error, then we need to use the | 513 // was a file request and encountered an error, then we need to use the |
| 513 // |failed_file_request_status_|. Otherwise use request_'s status. | 514 // |failed_file_request_status_|. Otherwise use request_'s status. |
| 514 OnCompletedRequest(failed_file_request_status_.get() ? | 515 OnCompletedRequest(failed_file_request_status_.get() ? |
| 515 *failed_file_request_status_ : request_->status(), | 516 *failed_file_request_status_ : request_->status(), |
| 516 std::string(), base::Time()); | 517 std::string(), base::TimeTicks()); |
| 517 request_.reset(); // destroy on the io thread | 518 request_.reset(); // destroy on the io thread |
| 518 } | 519 } |
| 519 | 520 |
| 520 // Called on the IO thread. | 521 // Called on the IO thread. |
| 521 void MaybeUpdateUploadProgress() { | 522 void MaybeUpdateUploadProgress() { |
| 522 // If a redirect is received upload is cancelled in net::URLRequest, we | 523 // If a redirect is received upload is cancelled in net::URLRequest, we |
| 523 // should try to stop the |upload_progress_timer_| timer and return. | 524 // should try to stop the |upload_progress_timer_| timer and return. |
| 524 if (!request_->has_upload()) { | 525 if (!request_->has_upload()) { |
| 525 if (upload_progress_timer_.IsRunning()) | 526 if (upload_progress_timer_.IsRunning()) |
| 526 upload_progress_timer_.Stop(); | 527 upload_progress_timer_.Stop(); |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 717 virtual void OnReceivedData(int bytes_read) { | 718 virtual void OnReceivedData(int bytes_read) { |
| 718 if (download_to_file_) | 719 if (download_to_file_) |
| 719 file_stream_.Write(buf_->data(), bytes_read, NULL); | 720 file_stream_.Write(buf_->data(), bytes_read, NULL); |
| 720 else | 721 else |
| 721 result_->data.append(buf_->data(), bytes_read); | 722 result_->data.append(buf_->data(), bytes_read); |
| 722 AsyncReadData(); // read more (may recurse) | 723 AsyncReadData(); // read more (may recurse) |
| 723 } | 724 } |
| 724 | 725 |
| 725 virtual void OnCompletedRequest(const net::URLRequestStatus& status, | 726 virtual void OnCompletedRequest(const net::URLRequestStatus& status, |
| 726 const std::string& security_info, | 727 const std::string& security_info, |
| 727 const base::Time& complete_time) { | 728 const base::TimeTicks& complete_time) { |
| 728 if (download_to_file_) | 729 if (download_to_file_) |
| 729 file_stream_.Close(); | 730 file_stream_.Close(); |
| 730 result_->status = status; | 731 result_->status = status; |
| 731 event_.Signal(); | 732 event_.Signal(); |
| 732 } | 733 } |
| 733 | 734 |
| 734 private: | 735 private: |
| 735 ResourceLoaderBridge::SyncLoadResponse* result_; | 736 ResourceLoaderBridge::SyncLoadResponse* result_; |
| 736 base::WaitableEvent event_; | 737 base::WaitableEvent event_; |
| 737 }; | 738 }; |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1032 | 1033 |
| 1033 // static | 1034 // static |
| 1034 void SimpleResourceLoaderBridge::AllowFileOverHTTP( | 1035 void SimpleResourceLoaderBridge::AllowFileOverHTTP( |
| 1035 const std::string& file_path_template, const GURL& http_prefix) { | 1036 const std::string& file_path_template, const GURL& http_prefix) { |
| 1036 DCHECK(!file_path_template.empty()); | 1037 DCHECK(!file_path_template.empty()); |
| 1037 DCHECK(http_prefix.is_valid() && | 1038 DCHECK(http_prefix.is_valid() && |
| 1038 (http_prefix.SchemeIs("http") || http_prefix.SchemeIs("https"))); | 1039 (http_prefix.SchemeIs("http") || http_prefix.SchemeIs("https"))); |
| 1039 g_file_over_http_params = new FileOverHTTPParams(file_path_template, | 1040 g_file_over_http_params = new FileOverHTTPParams(file_path_template, |
| 1040 http_prefix); | 1041 http_prefix); |
| 1041 } | 1042 } |
| OLD | NEW |