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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 | 48 |
49 using content::BrowserThread; | 49 using content::BrowserThread; |
50 using content::DownloadId; | 50 using content::DownloadId; |
51 using content::DownloadItem; | 51 using content::DownloadItem; |
52 using content::WebContents; | 52 using content::WebContents; |
53 | 53 |
54 namespace { | 54 namespace { |
55 | 55 |
56 // Param structs exist because base::Bind can only handle 6 args. | 56 // Param structs exist because base::Bind can only handle 6 args. |
57 struct URLParams { | 57 struct URLParams { |
58 URLParams(const GURL& url, const GURL& referrer, int64 post_id) | 58 URLParams(const GURL& url, const GURL& referrer, int64 post_id, bool cache) |
59 : url_(url), referrer_(referrer), post_id_(post_id) {} | 59 : url_(url), referrer_(referrer), post_id_(post_id), prefer_cache_(cache) {} |
60 GURL url_; | 60 GURL url_; |
61 GURL referrer_; | 61 GURL referrer_; |
62 int64 post_id_; | 62 int64 post_id_; |
| 63 bool prefer_cache_; |
63 }; | 64 }; |
64 | 65 |
65 struct RenderParams { | 66 struct RenderParams { |
66 RenderParams(int rpi, int rvi) | 67 RenderParams(int rpi, int rvi) |
67 : render_process_id_(rpi), render_view_id_(rvi) {} | 68 : render_process_id_(rpi), render_view_id_(rvi) {} |
68 int render_process_id_; | 69 int render_process_id_; |
69 int render_view_id_; | 70 int render_view_id_; |
70 }; | 71 }; |
71 | 72 |
72 void BeginDownload(const URLParams& url_params, | 73 void BeginDownload( |
73 bool prefer_cache, | 74 const URLParams& url_params, |
74 const DownloadSaveInfo& save_info, | 75 const DownloadSaveInfo& save_info, |
75 ResourceDispatcherHost* resource_dispatcher_host, | 76 ResourceDispatcherHost* resource_dispatcher_host, |
76 const RenderParams& render_params, | 77 const RenderParams& render_params, |
77 content::ResourceContext* context) { | 78 content::ResourceContext* context, |
| 79 const content::DownloadManager::OnStartedCallback& callback) { |
78 scoped_ptr<net::URLRequest> request( | 80 scoped_ptr<net::URLRequest> request( |
79 new net::URLRequest(url_params.url_, resource_dispatcher_host)); | 81 new net::URLRequest(url_params.url_, resource_dispatcher_host)); |
80 request->set_referrer(url_params.referrer_.spec()); | 82 request->set_referrer(url_params.referrer_.spec()); |
81 if (url_params.post_id_ >= 0) { | 83 if (url_params.post_id_ >= 0) { |
82 // The POST in this case does not have an actual body, and only works | 84 // The POST in this case does not have an actual body, and only works |
83 // when retrieving data from cache. This is done because we don't want | 85 // when retrieving data from cache. This is done because we don't want |
84 // to do a re-POST without user consent, and currently don't have a good | 86 // to do a re-POST without user consent, and currently don't have a good |
85 // plan on how to display the UI for that. | 87 // plan on how to display the UI for that. |
86 DCHECK(prefer_cache); | 88 DCHECK(url_params.prefer_cache_); |
87 request->set_method("POST"); | 89 request->set_method("POST"); |
88 scoped_refptr<net::UploadData> upload_data = new net::UploadData(); | 90 scoped_refptr<net::UploadData> upload_data = new net::UploadData(); |
89 upload_data->set_identifier(url_params.post_id_); | 91 upload_data->set_identifier(url_params.post_id_); |
90 request->set_upload(upload_data); | 92 request->set_upload(upload_data); |
91 } | 93 } |
92 resource_dispatcher_host->BeginDownload( | 94 resource_dispatcher_host->BeginDownload( |
93 request.Pass(), prefer_cache, save_info, | 95 request.Pass(), |
94 DownloadResourceHandler::OnStartedCallback(), | 96 url_params.prefer_cache_, |
| 97 save_info, |
| 98 callback, |
95 render_params.render_process_id_, | 99 render_params.render_process_id_, |
96 render_params.render_view_id_, | 100 render_params.render_view_id_, |
97 context); | 101 context); |
98 } | 102 } |
99 | 103 |
100 class MapValueIteratorAdapter { | 104 class MapValueIteratorAdapter { |
101 public: | 105 public: |
102 explicit MapValueIteratorAdapter( | 106 explicit MapValueIteratorAdapter( |
103 base::hash_map<int64, DownloadItem*>::const_iterator iter) | 107 base::hash_map<int64, DownloadItem*>::const_iterator iter) |
104 : iter_(iter) { | 108 : iter_(iter) { |
(...skipping 714 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
819 // Initiate a download of a specific URL. We send the request to the | 823 // Initiate a download of a specific URL. We send the request to the |
820 // ResourceDispatcherHost, and let it send us responses like a regular | 824 // ResourceDispatcherHost, and let it send us responses like a regular |
821 // download. | 825 // download. |
822 void DownloadManagerImpl::DownloadUrl( | 826 void DownloadManagerImpl::DownloadUrl( |
823 const GURL& url, | 827 const GURL& url, |
824 const GURL& referrer, | 828 const GURL& referrer, |
825 const std::string& referrer_charset, | 829 const std::string& referrer_charset, |
826 bool prefer_cache, | 830 bool prefer_cache, |
827 int64 post_id, | 831 int64 post_id, |
828 const DownloadSaveInfo& save_info, | 832 const DownloadSaveInfo& save_info, |
829 WebContents* web_contents) { | 833 WebContents* web_contents, |
| 834 const OnStartedCallback& callback) { |
830 ResourceDispatcherHost* resource_dispatcher_host = | 835 ResourceDispatcherHost* resource_dispatcher_host = |
831 ResourceDispatcherHost::Get(); | 836 ResourceDispatcherHost::Get(); |
832 | 837 |
833 // We send a pointer to content::ResourceContext, instead of the usual | 838 // We send a pointer to content::ResourceContext, instead of the usual |
834 // reference, so that a copy of the object isn't made. | 839 // reference, so that a copy of the object isn't made. |
835 // base::Bind can't handle 7 args, so we use URLParams and RenderParams. | 840 // base::Bind can't handle 7 args, so we use URLParams and RenderParams. |
836 BrowserThread::PostTask( | 841 BrowserThread::PostTask( |
837 BrowserThread::IO, FROM_HERE, | 842 BrowserThread::IO, FROM_HERE, |
838 base::Bind( | 843 base::Bind( |
839 &BeginDownload, | 844 &BeginDownload, |
840 URLParams(url, referrer, post_id), | 845 URLParams(url, referrer, post_id, prefer_cache), |
841 prefer_cache, | |
842 save_info, | 846 save_info, |
843 resource_dispatcher_host, | 847 resource_dispatcher_host, |
844 RenderParams(web_contents->GetRenderProcessHost()->GetID(), | 848 RenderParams(web_contents->GetRenderProcessHost()->GetID(), |
845 web_contents->GetRenderViewHost()->GetRoutingID()), | 849 web_contents->GetRenderViewHost()->GetRoutingID()), |
846 web_contents->GetBrowserContext()->GetResourceContext())); | 850 web_contents->GetBrowserContext()->GetResourceContext(), |
| 851 callback)); |
847 } | 852 } |
848 | 853 |
849 void DownloadManagerImpl::AddObserver(Observer* observer) { | 854 void DownloadManagerImpl::AddObserver(Observer* observer) { |
850 observers_.AddObserver(observer); | 855 observers_.AddObserver(observer); |
851 // TODO: It is the responsibility of the observers to query the | 856 // TODO: It is the responsibility of the observers to query the |
852 // DownloadManager. Remove the following call from here and update all | 857 // DownloadManager. Remove the following call from here and update all |
853 // observers. | 858 // observers. |
854 observer->ModelChanged(this); | 859 observer->ModelChanged(this); |
855 } | 860 } |
856 | 861 |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1159 it != history_downloads_.end(); ++it) { | 1164 it != history_downloads_.end(); ++it) { |
1160 if (it->second->IsComplete() && !it->second->GetOpened()) | 1165 if (it->second->IsComplete() && !it->second->GetOpened()) |
1161 ++num_unopened; | 1166 ++num_unopened; |
1162 } | 1167 } |
1163 download_stats::RecordOpensOutstanding(num_unopened); | 1168 download_stats::RecordOpensOutstanding(num_unopened); |
1164 } | 1169 } |
1165 | 1170 |
1166 void DownloadManagerImpl::SetFileManager(DownloadFileManager* file_manager) { | 1171 void DownloadManagerImpl::SetFileManager(DownloadFileManager* file_manager) { |
1167 file_manager_ = file_manager; | 1172 file_manager_ = file_manager; |
1168 } | 1173 } |
OLD | NEW |