OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 // Cancel methods are proxied over to the IO thread, where an net::URLRequest | 292 // Cancel methods are proxied over to the IO thread, where an net::URLRequest |
293 // object is instantiated. | 293 // object is instantiated. |
294 struct DeleteOnIOThread; // See below. | 294 struct DeleteOnIOThread; // See below. |
295 class RequestProxy | 295 class RequestProxy |
296 : public net::URLRequest::Delegate, | 296 : public net::URLRequest::Delegate, |
297 public base::RefCountedThreadSafe<RequestProxy, DeleteOnIOThread> { | 297 public base::RefCountedThreadSafe<RequestProxy, DeleteOnIOThread> { |
298 public: | 298 public: |
299 // Takes ownership of the params. | 299 // Takes ownership of the params. |
300 RequestProxy() | 300 RequestProxy() |
301 : download_to_file_(false), | 301 : download_to_file_(false), |
302 file_stream_(NULL), | |
303 buf_(new net::IOBuffer(kDataSize)), | 302 buf_(new net::IOBuffer(kDataSize)), |
304 last_upload_position_(0) { | 303 last_upload_position_(0) { |
305 } | 304 } |
306 | 305 |
307 void DropPeer() { | 306 void DropPeer() { |
308 peer_ = NULL; | 307 peer_ = NULL; |
309 } | 308 } |
310 | 309 |
311 void Start(ResourceLoaderBridge::Peer* peer, RequestParams* params) { | 310 void Start(ResourceLoaderBridge::Peer* peer, RequestParams* params) { |
312 peer_ = peer; | 311 peer_ = peer; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 SimpleAppCacheSystem::SetExtraRequestInfo( | 436 SimpleAppCacheSystem::SetExtraRequestInfo( |
438 request_.get(), params->appcache_host_id, params->request_type); | 437 request_.get(), params->appcache_host_id, params->request_type); |
439 | 438 |
440 download_to_file_ = params->download_to_file; | 439 download_to_file_ = params->download_to_file; |
441 if (download_to_file_) { | 440 if (download_to_file_) { |
442 FilePath path; | 441 FilePath path; |
443 if (file_util::CreateTemporaryFile(&path)) { | 442 if (file_util::CreateTemporaryFile(&path)) { |
444 downloaded_file_ = ShareableFileReference::GetOrCreate( | 443 downloaded_file_ = ShareableFileReference::GetOrCreate( |
445 path, ShareableFileReference::DELETE_ON_FINAL_RELEASE, | 444 path, ShareableFileReference::DELETE_ON_FINAL_RELEASE, |
446 base::MessageLoopProxy::current()); | 445 base::MessageLoopProxy::current()); |
447 file_stream_.OpenSync( | 446 file_stream_.reset(new net::FileStream(NULL)); |
| 447 file_stream_->OpenSync( |
448 path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE); | 448 path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE); |
449 } | 449 } |
450 } | 450 } |
451 | 451 |
452 request_->Start(); | 452 request_->Start(); |
453 | 453 |
454 if (request_->has_upload() && | 454 if (request_->has_upload() && |
455 params->load_flags & net::LOAD_ENABLE_UPLOAD_PROGRESS) { | 455 params->load_flags & net::LOAD_ENABLE_UPLOAD_PROGRESS) { |
456 upload_progress_timer_.Start(FROM_HERE, | 456 upload_progress_timer_.Start(FROM_HERE, |
457 base::TimeDelta::FromMilliseconds(kUpdateUploadProgressIntervalMsec), | 457 base::TimeDelta::FromMilliseconds(kUpdateUploadProgressIntervalMsec), |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 | 515 |
516 virtual void OnReceivedResponse( | 516 virtual void OnReceivedResponse( |
517 const ResourceResponseInfo& info) { | 517 const ResourceResponseInfo& info) { |
518 owner_loop_->PostTask( | 518 owner_loop_->PostTask( |
519 FROM_HERE, | 519 FROM_HERE, |
520 base::Bind(&RequestProxy::NotifyReceivedResponse, this, info)); | 520 base::Bind(&RequestProxy::NotifyReceivedResponse, this, info)); |
521 } | 521 } |
522 | 522 |
523 virtual void OnReceivedData(int bytes_read) { | 523 virtual void OnReceivedData(int bytes_read) { |
524 if (download_to_file_) { | 524 if (download_to_file_) { |
525 file_stream_.WriteSync(buf_->data(), bytes_read); | 525 file_stream_->WriteSync(buf_->data(), bytes_read); |
526 owner_loop_->PostTask( | 526 owner_loop_->PostTask( |
527 FROM_HERE, | 527 FROM_HERE, |
528 base::Bind(&RequestProxy::NotifyDownloadedData, this, bytes_read)); | 528 base::Bind(&RequestProxy::NotifyDownloadedData, this, bytes_read)); |
529 return; | 529 return; |
530 } | 530 } |
531 | 531 |
532 owner_loop_->PostTask( | 532 owner_loop_->PostTask( |
533 FROM_HERE, | 533 FROM_HERE, |
534 base::Bind(&RequestProxy::NotifyReceivedData, this, bytes_read)); | 534 base::Bind(&RequestProxy::NotifyReceivedData, this, bytes_read)); |
535 } | 535 } |
536 | 536 |
537 virtual void OnCompletedRequest(int error_code, | 537 virtual void OnCompletedRequest(int error_code, |
538 const std::string& security_info, | 538 const std::string& security_info, |
539 const base::TimeTicks& complete_time) { | 539 const base::TimeTicks& complete_time) { |
540 if (download_to_file_) | 540 if (download_to_file_) |
541 file_stream_.CloseSync(); | 541 file_stream_.reset(); |
542 owner_loop_->PostTask( | 542 owner_loop_->PostTask( |
543 FROM_HERE, | 543 FROM_HERE, |
544 base::Bind(&RequestProxy::NotifyCompletedRequest, this, error_code, | 544 base::Bind(&RequestProxy::NotifyCompletedRequest, this, error_code, |
545 security_info, complete_time)); | 545 security_info, complete_time)); |
546 } | 546 } |
547 | 547 |
548 // -------------------------------------------------------------------------- | 548 // -------------------------------------------------------------------------- |
549 // net::URLRequest::Delegate implementation: | 549 // net::URLRequest::Delegate implementation: |
550 | 550 |
551 virtual void OnReceivedRedirect(net::URLRequest* request, | 551 virtual void OnReceivedRedirect(net::URLRequest* request, |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
736 net::ERR_ACCESS_DENIED)); | 736 net::ERR_ACCESS_DENIED)); |
737 } | 737 } |
738 } | 738 } |
739 return true; | 739 return true; |
740 } | 740 } |
741 | 741 |
742 scoped_ptr<net::URLRequest> request_; | 742 scoped_ptr<net::URLRequest> request_; |
743 | 743 |
744 // Support for request.download_to_file behavior. | 744 // Support for request.download_to_file behavior. |
745 bool download_to_file_; | 745 bool download_to_file_; |
746 net::FileStream file_stream_; | 746 scoped_ptr<net::FileStream> file_stream_; |
747 scoped_refptr<ShareableFileReference> downloaded_file_; | 747 scoped_refptr<ShareableFileReference> downloaded_file_; |
748 | 748 |
749 // Size of our async IO data buffers | 749 // Size of our async IO data buffers |
750 static const int kDataSize = 16*1024; | 750 static const int kDataSize = 16*1024; |
751 | 751 |
752 // read buffer for async IO | 752 // read buffer for async IO |
753 scoped_refptr<net::IOBuffer> buf_; | 753 scoped_refptr<net::IOBuffer> buf_; |
754 | 754 |
755 MessageLoop* owner_loop_; | 755 MessageLoop* owner_loop_; |
756 | 756 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
812 } | 812 } |
813 result_->url = new_url; | 813 result_->url = new_url; |
814 } | 814 } |
815 | 815 |
816 virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE { | 816 virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE { |
817 *static_cast<ResourceResponseInfo*>(result_) = info; | 817 *static_cast<ResourceResponseInfo*>(result_) = info; |
818 } | 818 } |
819 | 819 |
820 virtual void OnReceivedData(int bytes_read) OVERRIDE { | 820 virtual void OnReceivedData(int bytes_read) OVERRIDE { |
821 if (download_to_file_) | 821 if (download_to_file_) |
822 file_stream_.WriteSync(buf_->data(), bytes_read); | 822 file_stream_->WriteSync(buf_->data(), bytes_read); |
823 else | 823 else |
824 result_->data.append(buf_->data(), bytes_read); | 824 result_->data.append(buf_->data(), bytes_read); |
825 AsyncReadData(); // read more (may recurse) | 825 AsyncReadData(); // read more (may recurse) |
826 } | 826 } |
827 | 827 |
828 virtual void OnCompletedRequest( | 828 virtual void OnCompletedRequest( |
829 int error_code, | 829 int error_code, |
830 const std::string& security_info, | 830 const std::string& security_info, |
831 const base::TimeTicks& complete_time) OVERRIDE { | 831 const base::TimeTicks& complete_time) OVERRIDE { |
832 if (download_to_file_) | 832 if (download_to_file_) |
833 file_stream_.CloseSync(); | 833 file_stream_.reset(); |
834 result_->error_code = error_code; | 834 result_->error_code = error_code; |
835 event_.Signal(); | 835 event_.Signal(); |
836 } | 836 } |
837 | 837 |
838 protected: | 838 protected: |
839 virtual ~SyncRequestProxy() {} | 839 virtual ~SyncRequestProxy() {} |
840 | 840 |
841 private: | 841 private: |
842 ResourceLoaderBridge::SyncLoadResponse* result_; | 842 ResourceLoaderBridge::SyncLoadResponse* result_; |
843 base::WaitableEvent event_; | 843 base::WaitableEvent event_; |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1107 (http_prefix.SchemeIs("http") || http_prefix.SchemeIs("https"))); | 1107 (http_prefix.SchemeIs("http") || http_prefix.SchemeIs("https"))); |
1108 g_file_over_http_params = new FileOverHTTPParams(file_path_template, | 1108 g_file_over_http_params = new FileOverHTTPParams(file_path_template, |
1109 http_prefix); | 1109 http_prefix); |
1110 } | 1110 } |
1111 | 1111 |
1112 // static | 1112 // static |
1113 webkit_glue::ResourceLoaderBridge* SimpleResourceLoaderBridge::Create( | 1113 webkit_glue::ResourceLoaderBridge* SimpleResourceLoaderBridge::Create( |
1114 const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info) { | 1114 const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info) { |
1115 return new ResourceLoaderBridgeImpl(request_info); | 1115 return new ResourceLoaderBridgeImpl(request_info); |
1116 } | 1116 } |
OLD | NEW |