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 #include "content/browser/download/download_manager_impl.h" | 5 #include "content/browser/download/download_manager_impl.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 using content::DownloadId; | 51 using content::DownloadId; |
52 using content::DownloadItem; | 52 using content::DownloadItem; |
53 using content::DownloadPersistentStoreInfo; | 53 using content::DownloadPersistentStoreInfo; |
54 using content::ResourceDispatcherHostImpl; | 54 using content::ResourceDispatcherHostImpl; |
55 using content::WebContents; | 55 using content::WebContents; |
56 | 56 |
57 namespace { | 57 namespace { |
58 | 58 |
59 // Param structs exist because base::Bind can only handle 6 args. | 59 // Param structs exist because base::Bind can only handle 6 args. |
60 struct URLParams { | 60 struct URLParams { |
61 URLParams(const GURL& url, const GURL& referrer, int64 post_id) | 61 URLParams(const GURL& url, const GURL& referrer, int64 post_id, bool cache) |
62 : url_(url), referrer_(referrer), post_id_(post_id) {} | 62 : url_(url), referrer_(referrer), post_id_(post_id), prefer_cache_(cache) {} |
63 GURL url_; | 63 GURL url_; |
64 GURL referrer_; | 64 GURL referrer_; |
65 int64 post_id_; | 65 int64 post_id_; |
| 66 bool prefer_cache_; |
66 }; | 67 }; |
67 | 68 |
68 struct RenderParams { | 69 struct RenderParams { |
69 RenderParams(int rpi, int rvi) | 70 RenderParams(int rpi, int rvi) |
70 : render_process_id_(rpi), render_view_id_(rvi) {} | 71 : render_process_id_(rpi), render_view_id_(rvi) {} |
71 int render_process_id_; | 72 int render_process_id_; |
72 int render_view_id_; | 73 int render_view_id_; |
73 }; | 74 }; |
74 | 75 |
75 void BeginDownload(const URLParams& url_params, | 76 void BeginDownload( |
76 bool prefer_cache, | 77 const URLParams& url_params, |
77 const DownloadSaveInfo& save_info, | 78 const DownloadSaveInfo& save_info, |
78 ResourceDispatcherHostImpl* resource_dispatcher_host, | 79 ResourceDispatcherHostImpl* resource_dispatcher_host, |
79 const RenderParams& render_params, | 80 const RenderParams& render_params, |
80 content::ResourceContext* context) { | 81 content::ResourceContext* context, |
| 82 const content::DownloadManager::OnStartedCallback& callback) { |
81 scoped_ptr<net::URLRequest> request( | 83 scoped_ptr<net::URLRequest> request( |
82 new net::URLRequest(url_params.url_, resource_dispatcher_host)); | 84 new net::URLRequest(url_params.url_, resource_dispatcher_host)); |
83 request->set_referrer(url_params.referrer_.spec()); | 85 request->set_referrer(url_params.referrer_.spec()); |
84 if (url_params.post_id_ >= 0) { | 86 if (url_params.post_id_ >= 0) { |
85 // The POST in this case does not have an actual body, and only works | 87 // The POST in this case does not have an actual body, and only works |
86 // when retrieving data from cache. This is done because we don't want | 88 // when retrieving data from cache. This is done because we don't want |
87 // to do a re-POST without user consent, and currently don't have a good | 89 // to do a re-POST without user consent, and currently don't have a good |
88 // plan on how to display the UI for that. | 90 // plan on how to display the UI for that. |
89 DCHECK(prefer_cache); | 91 DCHECK(url_params.prefer_cache_); |
90 request->set_method("POST"); | 92 request->set_method("POST"); |
91 scoped_refptr<net::UploadData> upload_data = new net::UploadData(); | 93 scoped_refptr<net::UploadData> upload_data = new net::UploadData(); |
92 upload_data->set_identifier(url_params.post_id_); | 94 upload_data->set_identifier(url_params.post_id_); |
93 request->set_upload(upload_data); | 95 request->set_upload(upload_data); |
94 } | 96 } |
95 resource_dispatcher_host->BeginDownload( | 97 resource_dispatcher_host->BeginDownload( |
96 request.Pass(), | 98 request.Pass(), |
97 context, | 99 context, |
98 render_params.render_process_id_, | 100 render_params.render_process_id_, |
99 render_params.render_view_id_, | 101 render_params.render_view_id_, |
100 prefer_cache, | 102 url_params.prefer_cache_, |
101 save_info, | 103 save_info, |
102 ResourceDispatcherHostImpl::DownloadStartedCallback()); | 104 callback); |
103 } | 105 } |
104 | 106 |
105 class MapValueIteratorAdapter { | 107 class MapValueIteratorAdapter { |
106 public: | 108 public: |
107 explicit MapValueIteratorAdapter( | 109 explicit MapValueIteratorAdapter( |
108 base::hash_map<int64, DownloadItem*>::const_iterator iter) | 110 base::hash_map<int64, DownloadItem*>::const_iterator iter) |
109 : iter_(iter) { | 111 : iter_(iter) { |
110 } | 112 } |
111 ~MapValueIteratorAdapter() {} | 113 ~MapValueIteratorAdapter() {} |
112 | 114 |
(...skipping 746 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
859 // Initiate a download of a specific URL. We send the request to the | 861 // Initiate a download of a specific URL. We send the request to the |
860 // ResourceDispatcherHost, and let it send us responses like a regular | 862 // ResourceDispatcherHost, and let it send us responses like a regular |
861 // download. | 863 // download. |
862 void DownloadManagerImpl::DownloadUrl( | 864 void DownloadManagerImpl::DownloadUrl( |
863 const GURL& url, | 865 const GURL& url, |
864 const GURL& referrer, | 866 const GURL& referrer, |
865 const std::string& referrer_charset, | 867 const std::string& referrer_charset, |
866 bool prefer_cache, | 868 bool prefer_cache, |
867 int64 post_id, | 869 int64 post_id, |
868 const DownloadSaveInfo& save_info, | 870 const DownloadSaveInfo& save_info, |
869 WebContents* web_contents) { | 871 WebContents* web_contents, |
| 872 const OnStartedCallback& callback) { |
870 ResourceDispatcherHostImpl* resource_dispatcher_host = | 873 ResourceDispatcherHostImpl* resource_dispatcher_host = |
871 ResourceDispatcherHostImpl::Get(); | 874 ResourceDispatcherHostImpl::Get(); |
872 DCHECK(resource_dispatcher_host); | 875 DCHECK(resource_dispatcher_host); |
873 | 876 |
874 // We send a pointer to content::ResourceContext, instead of the usual | 877 // We send a pointer to content::ResourceContext, instead of the usual |
875 // reference, so that a copy of the object isn't made. | 878 // reference, so that a copy of the object isn't made. |
876 // base::Bind can't handle 7 args, so we use URLParams and RenderParams. | 879 // base::Bind can't handle 7 args, so we use URLParams and RenderParams. |
877 BrowserThread::PostTask( | 880 BrowserThread::PostTask( |
878 BrowserThread::IO, FROM_HERE, | 881 BrowserThread::IO, FROM_HERE, |
879 base::Bind( | 882 base::Bind( |
880 &BeginDownload, | 883 &BeginDownload, |
881 URLParams(url, referrer, post_id), | 884 URLParams(url, referrer, post_id, prefer_cache), |
882 prefer_cache, | |
883 save_info, | 885 save_info, |
884 resource_dispatcher_host, | 886 resource_dispatcher_host, |
885 RenderParams(web_contents->GetRenderProcessHost()->GetID(), | 887 RenderParams(web_contents->GetRenderProcessHost()->GetID(), |
886 web_contents->GetRenderViewHost()->GetRoutingID()), | 888 web_contents->GetRenderViewHost()->GetRoutingID()), |
887 web_contents->GetBrowserContext()->GetResourceContext())); | 889 web_contents->GetBrowserContext()->GetResourceContext(), |
| 890 callback)); |
888 } | 891 } |
889 | 892 |
890 void DownloadManagerImpl::AddObserver(Observer* observer) { | 893 void DownloadManagerImpl::AddObserver(Observer* observer) { |
891 observers_.AddObserver(observer); | 894 observers_.AddObserver(observer); |
892 // TODO: It is the responsibility of the observers to query the | 895 // TODO: It is the responsibility of the observers to query the |
893 // DownloadManager. Remove the following call from here and update all | 896 // DownloadManager. Remove the following call from here and update all |
894 // observers. | 897 // observers. |
895 observer->ModelChanged(this); | 898 observer->ModelChanged(this); |
896 } | 899 } |
897 | 900 |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1201 if (it->second->IsComplete() && !it->second->GetOpened()) | 1204 if (it->second->IsComplete() && !it->second->GetOpened()) |
1202 ++num_unopened; | 1205 ++num_unopened; |
1203 } | 1206 } |
1204 download_stats::RecordOpensOutstanding(num_unopened); | 1207 download_stats::RecordOpensOutstanding(num_unopened); |
1205 } | 1208 } |
1206 | 1209 |
1207 void DownloadManagerImpl::SetFileManagerForTesting( | 1210 void DownloadManagerImpl::SetFileManagerForTesting( |
1208 DownloadFileManager* file_manager) { | 1211 DownloadFileManager* file_manager) { |
1209 file_manager_ = file_manager; | 1212 file_manager_ = file_manager; |
1210 } | 1213 } |
OLD | NEW |