| 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 718 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 823 // Initiate a download of a specific URL. We send the request to the | 827 // Initiate a download of a specific URL. We send the request to the |
| 824 // ResourceDispatcherHost, and let it send us responses like a regular | 828 // ResourceDispatcherHost, and let it send us responses like a regular |
| 825 // download. | 829 // download. |
| 826 void DownloadManagerImpl::DownloadUrl( | 830 void DownloadManagerImpl::DownloadUrl( |
| 827 const GURL& url, | 831 const GURL& url, |
| 828 const GURL& referrer, | 832 const GURL& referrer, |
| 829 const std::string& referrer_charset, | 833 const std::string& referrer_charset, |
| 830 bool prefer_cache, | 834 bool prefer_cache, |
| 831 int64 post_id, | 835 int64 post_id, |
| 832 const DownloadSaveInfo& save_info, | 836 const DownloadSaveInfo& save_info, |
| 833 WebContents* web_contents) { | 837 WebContents* web_contents, |
| 838 const OnStartedCallback& callback) { |
| 834 ResourceDispatcherHost* resource_dispatcher_host = | 839 ResourceDispatcherHost* resource_dispatcher_host = |
| 835 ResourceDispatcherHost::Get(); | 840 ResourceDispatcherHost::Get(); |
| 836 | 841 |
| 837 // We send a pointer to content::ResourceContext, instead of the usual | 842 // We send a pointer to content::ResourceContext, instead of the usual |
| 838 // reference, so that a copy of the object isn't made. | 843 // reference, so that a copy of the object isn't made. |
| 839 // base::Bind can't handle 7 args, so we use URLParams and RenderParams. | 844 // base::Bind can't handle 7 args, so we use URLParams and RenderParams. |
| 840 BrowserThread::PostTask( | 845 BrowserThread::PostTask( |
| 841 BrowserThread::IO, FROM_HERE, | 846 BrowserThread::IO, FROM_HERE, |
| 842 base::Bind( | 847 base::Bind( |
| 843 &BeginDownload, | 848 &BeginDownload, |
| 844 URLParams(url, referrer, post_id), | 849 URLParams(url, referrer, post_id, prefer_cache), |
| 845 prefer_cache, | |
| 846 save_info, | 850 save_info, |
| 847 resource_dispatcher_host, | 851 resource_dispatcher_host, |
| 848 RenderParams(web_contents->GetRenderProcessHost()->GetID(), | 852 RenderParams(web_contents->GetRenderProcessHost()->GetID(), |
| 849 web_contents->GetRenderViewHost()->routing_id()), | 853 web_contents->GetRenderViewHost()->routing_id()), |
| 850 web_contents->GetBrowserContext()->GetResourceContext())); | 854 web_contents->GetBrowserContext()->GetResourceContext(), |
| 855 callback)); |
| 851 } | 856 } |
| 852 | 857 |
| 853 void DownloadManagerImpl::AddObserver(Observer* observer) { | 858 void DownloadManagerImpl::AddObserver(Observer* observer) { |
| 854 observers_.AddObserver(observer); | 859 observers_.AddObserver(observer); |
| 855 // TODO: It is the responsibility of the observers to query the | 860 // TODO: It is the responsibility of the observers to query the |
| 856 // DownloadManager. Remove the following call from here and update all | 861 // DownloadManager. Remove the following call from here and update all |
| 857 // observers. | 862 // observers. |
| 858 observer->ModelChanged(this); | 863 observer->ModelChanged(this); |
| 859 } | 864 } |
| 860 | 865 |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1171 it != history_downloads_.end(); ++it) { | 1176 it != history_downloads_.end(); ++it) { |
| 1172 if (it->second->IsComplete() && !it->second->GetOpened()) | 1177 if (it->second->IsComplete() && !it->second->GetOpened()) |
| 1173 ++num_unopened; | 1178 ++num_unopened; |
| 1174 } | 1179 } |
| 1175 download_stats::RecordOpensOutstanding(num_unopened); | 1180 download_stats::RecordOpensOutstanding(num_unopened); |
| 1176 } | 1181 } |
| 1177 | 1182 |
| 1178 void DownloadManagerImpl::SetFileManager(DownloadFileManager* file_manager) { | 1183 void DownloadManagerImpl::SetFileManager(DownloadFileManager* file_manager) { |
| 1179 file_manager_ = file_manager; | 1184 file_manager_ = file_manager; |
| 1180 } | 1185 } |
| OLD | NEW |