Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: content/browser/download/download_manager_impl.cc

Issue 10232010: DownloadUrlParameters (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: " Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 16 matching lines...) Expand all
27 #include "content/public/browser/browser_context.h" 27 #include "content/public/browser/browser_context.h"
28 #include "content/public/browser/browser_thread.h" 28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/content_browser_client.h" 29 #include "content/public/browser/content_browser_client.h"
30 #include "content/public/browser/download_interrupt_reasons.h" 30 #include "content/public/browser/download_interrupt_reasons.h"
31 #include "content/public/browser/download_manager_delegate.h" 31 #include "content/public/browser/download_manager_delegate.h"
32 #include "content/public/browser/download_persistent_store_info.h" 32 #include "content/public/browser/download_persistent_store_info.h"
33 #include "content/public/browser/notification_service.h" 33 #include "content/public/browser/notification_service.h"
34 #include "content/public/browser/notification_types.h" 34 #include "content/public/browser/notification_types.h"
35 #include "content/public/browser/render_process_host.h" 35 #include "content/public/browser/render_process_host.h"
36 #include "content/public/browser/web_contents_delegate.h" 36 #include "content/public/browser/web_contents_delegate.h"
37 #include "net/base/load_flags.h"
37 #include "net/base/upload_data.h" 38 #include "net/base/upload_data.h"
38 39
39 // TODO(benjhayden): Change this to DCHECK when we have more debugging 40 // TODO(benjhayden): Change this to DCHECK when we have more debugging
40 // information from the next dev cycle, before the next stable/beta branch is 41 // information from the next dev cycle, before the next stable/beta branch is
41 // cut, in order to prevent unnecessary crashes on those channels. If we still 42 // cut, in order to prevent unnecessary crashes on those channels. If we still
42 // don't have root cause before the dev cycle after the next stable/beta 43 // don't have root cause before the dev cycle after the next stable/beta
43 // releases, uncomment it out to re-enable debugging checks. Whenever this macro 44 // releases, uncomment it out to re-enable debugging checks. Whenever this macro
44 // is toggled, the corresponding macro in download_database.cc should also 45 // is toggled, the corresponding macro in download_database.cc should also
45 // be toggled. When 96627 is fixed, this macro and all its usages can be 46 // be toggled. When 96627 is fixed, this macro and all its usages can be
46 // deleted or permanently changed to DCHECK as appropriate. 47 // deleted or permanently changed to DCHECK as appropriate.
47 #define CHECK_96627 CHECK 48 #define CHECK_96627 CHECK
48 49
49 using content::BrowserThread; 50 using content::BrowserThread;
50 using content::DownloadId; 51 using content::DownloadId;
51 using content::DownloadItem; 52 using content::DownloadItem;
52 using content::DownloadPersistentStoreInfo; 53 using content::DownloadPersistentStoreInfo;
53 using content::ResourceDispatcherHostImpl; 54 using content::ResourceDispatcherHostImpl;
54 using content::WebContents; 55 using content::WebContents;
55 56
56 namespace { 57 namespace {
57 58
58 // Param structs exist because base::Bind can only handle 6 args. 59 static const char kHeaderNameKey[] = "name";
59 struct URLParams { 60 static const char kHeaderBinaryValueKey[] = "binaryValue";
60 URLParams(const GURL& url, const GURL& referrer, int64 post_id, bool cache) 61 static const char kHeaderValueKey[] = "value";
61 : url_(url), referrer_(referrer), post_id_(post_id), prefer_cache_(cache) {}
62 GURL url_;
63 GURL referrer_;
64 int64 post_id_;
65 bool prefer_cache_;
66 };
67 62
68 struct RenderParams { 63 void BeginDownload(content::DownloadUrlParameters* params) {
69 RenderParams(int rpi, int rvi) 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
70 : render_process_id_(rpi), render_view_id_(rvi) {} 65 scoped_ptr<net::URLRequest> request(new net::URLRequest(
71 int render_process_id_; 66 params->url(), params->resource_dispatcher_host()));
72 int render_view_id_; 67 request->set_referrer(params->referrer().spec());
73 }; 68 request->set_load_flags(request->load_flags() | params->load_flags());
74 69 request->set_method(params->method());
75 void BeginDownload( 70 if (!params->post_body().empty())
76 const URLParams& url_params, 71 request->AppendBytesToUpload(params->post_body().data(),
77 const content::DownloadSaveInfo& save_info, 72 params->post_body().size());
78 ResourceDispatcherHostImpl* resource_dispatcher_host, 73 if (params->post_id() >= 0) {
79 const RenderParams& render_params,
80 content::ResourceContext* context,
81 const content::DownloadManager::OnStartedCallback& callback) {
82 scoped_ptr<net::URLRequest> request(
83 new net::URLRequest(url_params.url_, resource_dispatcher_host));
84 request->set_referrer(url_params.referrer_.spec());
85 if (url_params.post_id_ >= 0) {
86 // The POST in this case does not have an actual body, and only works 74 // The POST in this case does not have an actual body, and only works
87 // when retrieving data from cache. This is done because we don't want 75 // when retrieving data from cache. This is done because we don't want
88 // to do a re-POST without user consent, and currently don't have a good 76 // to do a re-POST without user consent, and currently don't have a good
89 // plan on how to display the UI for that. 77 // plan on how to display the UI for that.
90 DCHECK(url_params.prefer_cache_); 78 DCHECK(params->prefer_cache());
91 request->set_method("POST"); 79 DCHECK(params->method() == "POST");
92 scoped_refptr<net::UploadData> upload_data = new net::UploadData(); 80 scoped_refptr<net::UploadData> upload_data = new net::UploadData();
93 upload_data->set_identifier(url_params.post_id_); 81 upload_data->set_identifier(params->post_id());
94 request->set_upload(upload_data); 82 request->set_upload(upload_data);
95 } 83 }
96 resource_dispatcher_host->BeginDownload( 84 if (params->extra_headers() != NULL) {
85 for (size_t index = 0;
86 index < params->extra_headers()->GetSize();
87 ++index) {
88 base::DictionaryValue* header = NULL;
89 std::string name, value;
90 CHECK(params->extra_headers()->GetDictionary(index, &header));
91 CHECK(header->GetString(kHeaderNameKey, &name));
92 if (header->HasKey(kHeaderBinaryValueKey)) {
93 base::ListValue* binary_value = NULL;
94 CHECK(header->GetList(kHeaderBinaryValueKey, &binary_value));
95 for (size_t char_i = 0; char_i < binary_value->GetSize(); ++char_i) {
96 int char_value = 0;
97 CHECK(binary_value->GetInteger(char_i, &char_value));
98 if ((0 <= char_value) &&
99 (char_value <= 0xff)) {
100 value.push_back(char_value);
101 }
102 }
103 } else if (header->HasKey(kHeaderValueKey)) {
104 CHECK(header->GetString(kHeaderValueKey, &value));
105 }
106 request->SetExtraRequestHeaderByName(name, value, false/*overwrite*/);
107 }
108 }
109 params->resource_dispatcher_host()->BeginDownload(
97 request.Pass(), 110 request.Pass(),
98 context, 111 params->resource_context(),
99 render_params.render_process_id_, 112 params->render_process_host_id(),
100 render_params.render_view_id_, 113 params->render_view_host_routing_id(),
101 url_params.prefer_cache_, 114 params->prefer_cache(),
102 save_info, 115 params->save_info(),
103 callback); 116 params->callback());
104 } 117 }
105 118
106 class MapValueIteratorAdapter { 119 class MapValueIteratorAdapter {
107 public: 120 public:
108 explicit MapValueIteratorAdapter( 121 explicit MapValueIteratorAdapter(
109 base::hash_map<int64, DownloadItem*>::const_iterator iter) 122 base::hash_map<int64, DownloadItem*>::const_iterator iter)
110 : iter_(iter) { 123 : iter_(iter) {
111 } 124 }
112 ~MapValueIteratorAdapter() {} 125 ~MapValueIteratorAdapter() {}
113 126
(...skipping 27 matching lines...) Expand all
141 BrowserThread::PostTask( 154 BrowserThread::PostTask(
142 BrowserThread::FILE, FROM_HERE, 155 BrowserThread::FILE, FROM_HERE,
143 base::Bind(&EnsureNoPendingDownloadsOnFile, 156 base::Bind(&EnsureNoPendingDownloadsOnFile,
144 download_file_manager, result)); 157 download_file_manager, result));
145 } 158 }
146 159
147 } // namespace 160 } // namespace
148 161
149 namespace content { 162 namespace content {
150 163
164 DownloadUrlParameters::DownloadUrlParameters(
165 const GURL& url,
166 int render_process_host_id,
167 int render_view_host_routing_id,
168 content::ResourceContext* resource_context,
169 const content::DownloadSaveInfo& save_info)
170 : extra_headers_(NULL),
171 load_flags_(0),
172 method_("GET"),
173 post_id_(-1),
174 prefer_cache_(false),
175 render_process_host_id_(render_process_host_id),
176 render_view_host_routing_id_(render_view_host_routing_id),
177 resource_context_(resource_context),
178 resource_dispatcher_host_(ResourceDispatcherHostImpl::Get()),
179 save_info_(save_info),
180 url_(url) {
181 DCHECK(resource_dispatcher_host_);
182 }
183
184 DownloadUrlParameters::~DownloadUrlParameters() {
185 }
186
187 // static
188 DownloadUrlParameters* DownloadUrlParameters::FromWebContents(
189 WebContents* web_contents,
190 const GURL& url,
191 const content::DownloadSaveInfo& save_info) {
192 return new DownloadUrlParameters(
193 url,
194 web_contents->GetRenderProcessHost()->GetID(),
195 web_contents->GetRenderViewHost()->GetRoutingID(),
196 web_contents->GetBrowserContext()->GetResourceContext(),
197 save_info);
198 }
199
200
151 // static 201 // static
152 DownloadManager* DownloadManager::Create( 202 DownloadManager* DownloadManager::Create(
153 content::DownloadManagerDelegate* delegate, 203 content::DownloadManagerDelegate* delegate,
154 net::NetLog* net_log) { 204 net::NetLog* net_log) {
155 return new DownloadManagerImpl(delegate, net_log); 205 return new DownloadManagerImpl(delegate, net_log);
156 } 206 }
157 207
158 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { 208 bool DownloadManager::EnsureNoPendingDownloadsForTesting() {
159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
160 bool result = true; 210 bool result = true;
(...skipping 682 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) { 893 int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
844 return RemoveDownloadsBetween(remove_begin, base::Time()); 894 return RemoveDownloadsBetween(remove_begin, base::Time());
845 } 895 }
846 896
847 int DownloadManagerImpl::RemoveAllDownloads() { 897 int DownloadManagerImpl::RemoveAllDownloads() {
848 download_stats::RecordClearAllSize(history_downloads_.size()); 898 download_stats::RecordClearAllSize(history_downloads_.size());
849 // The null times make the date range unbounded. 899 // The null times make the date range unbounded.
850 return RemoveDownloadsBetween(base::Time(), base::Time()); 900 return RemoveDownloadsBetween(base::Time(), base::Time());
851 } 901 }
852 902
853 // Initiate a download of a specific URL. We send the request to the
854 // ResourceDispatcherHost, and let it send us responses like a regular
855 // download.
856 void DownloadManagerImpl::DownloadUrl( 903 void DownloadManagerImpl::DownloadUrl(
857 const GURL& url, 904 content::DownloadUrlParameters* parameters) {
858 const GURL& referrer, 905 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
859 const std::string& referrer_charset, 906 &BeginDownload, base::Owned(parameters)));
860 bool prefer_cache,
861 int64 post_id,
862 const content::DownloadSaveInfo& save_info,
863 WebContents* web_contents,
864 const OnStartedCallback& callback) {
865 ResourceDispatcherHostImpl* resource_dispatcher_host =
866 ResourceDispatcherHostImpl::Get();
867 DCHECK(resource_dispatcher_host);
868
869 // We send a pointer to content::ResourceContext, instead of the usual
870 // reference, so that a copy of the object isn't made.
871 // base::Bind can't handle 7 args, so we use URLParams and RenderParams.
872 BrowserThread::PostTask(
873 BrowserThread::IO, FROM_HERE,
874 base::Bind(
875 &BeginDownload,
876 URLParams(url, referrer, post_id, prefer_cache),
877 save_info,
878 resource_dispatcher_host,
879 RenderParams(web_contents->GetRenderProcessHost()->GetID(),
880 web_contents->GetRenderViewHost()->GetRoutingID()),
881 web_contents->GetBrowserContext()->GetResourceContext(),
882 callback));
883 } 907 }
884 908
885 void DownloadManagerImpl::AddObserver(Observer* observer) { 909 void DownloadManagerImpl::AddObserver(Observer* observer) {
886 observers_.AddObserver(observer); 910 observers_.AddObserver(observer);
887 // TODO: It is the responsibility of the observers to query the 911 // TODO: It is the responsibility of the observers to query the
888 // DownloadManager. Remove the following call from here and update all 912 // DownloadManager. Remove the following call from here and update all
889 // observers. 913 // observers.
890 observer->ModelChanged(this); 914 observer->ModelChanged(this);
891 } 915 }
892 916
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
1208 if (it->second->IsComplete() && !it->second->GetOpened()) 1232 if (it->second->IsComplete() && !it->second->GetOpened())
1209 ++num_unopened; 1233 ++num_unopened;
1210 } 1234 }
1211 download_stats::RecordOpensOutstanding(num_unopened); 1235 download_stats::RecordOpensOutstanding(num_unopened);
1212 } 1236 }
1213 1237
1214 void DownloadManagerImpl::SetFileManagerForTesting( 1238 void DownloadManagerImpl::SetFileManagerForTesting(
1215 DownloadFileManager* file_manager) { 1239 DownloadFileManager* file_manager) {
1216 file_manager_ = file_manager; 1240 file_manager_ = file_manager;
1217 } 1241 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698