OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/download/download_file_manager.h" | 5 #include "chrome/browser/download/download_file_manager.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
10 #include "base/task.h" | 10 #include "base/task.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "chrome/browser/download/download_create_info.h" | 13 #include "chrome/browser/download/download_create_info.h" |
14 #include "chrome/browser/download/download_manager.h" | 14 #include "chrome/browser/download/download_manager.h" |
15 #include "chrome/browser/download/download_process_handle.h" | 15 #include "chrome/browser/download/download_request_handle.h" |
16 #include "chrome/browser/download/download_util.h" | 16 #include "chrome/browser/download/download_util.h" |
17 #include "chrome/browser/net/chrome_url_request_context.h" | 17 #include "chrome/browser/net/chrome_url_request_context.h" |
18 #include "chrome/browser/platform_util.h" | 18 #include "chrome/browser/platform_util.h" |
19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
21 #include "chrome/browser/tab_contents/tab_util.h" | 21 #include "chrome/browser/tab_contents/tab_util.h" |
22 #include "content/browser/browser_thread.h" | 22 #include "content/browser/browser_thread.h" |
23 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 23 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
24 #include "content/browser/tab_contents/tab_contents.h" | 24 #include "content/browser/tab_contents/tab_contents.h" |
25 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 DCHECK(info); | 61 DCHECK(info); |
62 VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString(); | 62 VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString(); |
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
64 | 64 |
65 // Life of |info| ends here. No more references to it after this method. | 65 // Life of |info| ends here. No more references to it after this method. |
66 scoped_ptr<DownloadCreateInfo> infop(info); | 66 scoped_ptr<DownloadCreateInfo> infop(info); |
67 | 67 |
68 scoped_ptr<DownloadFile> | 68 scoped_ptr<DownloadFile> |
69 download_file(new DownloadFile(info, download_manager)); | 69 download_file(new DownloadFile(info, download_manager)); |
70 if (!download_file->Initialize(get_hash)) { | 70 if (!download_file->Initialize(get_hash)) { |
71 download_util::CancelDownloadRequest(resource_dispatcher_host_, | 71 info->request_handle.CancelRequest(); |
72 info->process_handle); | |
73 return; | 72 return; |
74 } | 73 } |
75 | 74 |
76 int32 id = info->download_id; | 75 int32 id = info->download_id; |
77 DCHECK(GetDownloadFile(id) == NULL); | 76 DCHECK(GetDownloadFile(id) == NULL); |
78 downloads_[id] = download_file.release(); | 77 downloads_[id] = download_file.release(); |
79 | 78 |
80 // The file is now ready, we can un-pause the request and start saving data. | 79 // The file is now ready, we can un-pause the request and start saving data. |
81 BrowserThread::PostTask( | 80 info->request_handle.ResumeRequest(); |
82 BrowserThread::IO, FROM_HERE, | |
83 NewRunnableMethod(this, &DownloadFileManager::ResumeDownloadRequest, | |
84 info->process_handle)); | |
85 | 81 |
86 StartUpdateTimer(); | 82 StartUpdateTimer(); |
87 | 83 |
88 BrowserThread::PostTask( | 84 BrowserThread::PostTask( |
89 BrowserThread::UI, FROM_HERE, | 85 BrowserThread::UI, FROM_HERE, |
90 NewRunnableMethod(download_manager, | 86 NewRunnableMethod(download_manager, |
91 &DownloadManager::StartDownload, id)); | 87 &DownloadManager::StartDownload, id)); |
92 } | 88 } |
93 | 89 |
94 void DownloadFileManager::ResumeDownloadRequest( | |
95 DownloadProcessHandle process) { | |
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
97 | |
98 // This balances the pause in DownloadResourceHandler::OnResponseStarted. | |
99 resource_dispatcher_host_->PauseRequest(process.child_id(), | |
100 process.request_id(), | |
101 false); | |
102 } | |
103 | |
104 DownloadFile* DownloadFileManager::GetDownloadFile(int id) { | 90 DownloadFile* DownloadFileManager::GetDownloadFile(int id) { |
105 DownloadFileMap::iterator it = downloads_.find(id); | 91 DownloadFileMap::iterator it = downloads_.find(id); |
106 return it == downloads_.end() ? NULL : it->second; | 92 return it == downloads_.end() ? NULL : it->second; |
107 } | 93 } |
108 | 94 |
109 void DownloadFileManager::StartUpdateTimer() { | 95 void DownloadFileManager::StartUpdateTimer() { |
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
111 if (!update_timer_.IsRunning()) { | 97 if (!update_timer_.IsRunning()) { |
112 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), | 98 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), |
113 this, &DownloadFileManager::UpdateInProgressDownloads); | 99 this, &DownloadFileManager::UpdateInProgressDownloads); |
(...skipping 24 matching lines...) Expand all Loading... |
138 // request is a download. | 124 // request is a download. |
139 int DownloadFileManager::GetNextId() { | 125 int DownloadFileManager::GetNextId() { |
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
141 return next_id_++; | 127 return next_id_++; |
142 } | 128 } |
143 | 129 |
144 void DownloadFileManager::StartDownload(DownloadCreateInfo* info) { | 130 void DownloadFileManager::StartDownload(DownloadCreateInfo* info) { |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
146 DCHECK(info); | 132 DCHECK(info); |
147 | 133 |
148 DownloadManager* manager = info->process_handle.GetDownloadManager(); | 134 DownloadManager* manager = info->request_handle.GetDownloadManager(); |
149 if (!manager) { | 135 if (!manager) { |
150 download_util::CancelDownloadRequest(resource_dispatcher_host_, | 136 info->request_handle.CancelRequest(); |
151 info->process_handle); | |
152 delete info; | 137 delete info; |
153 return; | 138 return; |
154 } | 139 } |
155 | 140 |
156 // TODO(phajdan.jr): fix the duplication of path info below. | 141 // TODO(phajdan.jr): fix the duplication of path info below. |
157 info->path = info->save_info.file_path; | 142 info->path = info->save_info.file_path; |
158 | 143 |
159 manager->CreateDownloadItem(info); | 144 manager->CreateDownloadItem(info); |
160 | 145 |
161 bool hash_needed = resource_dispatcher_host_->safe_browsing_service()-> | 146 bool hash_needed = resource_dispatcher_host_->safe_browsing_service()-> |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 void DownloadFileManager::OnDownloadManagerShutdown(DownloadManager* manager) { | 247 void DownloadFileManager::OnDownloadManagerShutdown(DownloadManager* manager) { |
263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
264 DCHECK(manager); | 249 DCHECK(manager); |
265 | 250 |
266 std::set<DownloadFile*> to_remove; | 251 std::set<DownloadFile*> to_remove; |
267 | 252 |
268 for (DownloadFileMap::iterator i = downloads_.begin(); | 253 for (DownloadFileMap::iterator i = downloads_.begin(); |
269 i != downloads_.end(); ++i) { | 254 i != downloads_.end(); ++i) { |
270 DownloadFile* download_file = i->second; | 255 DownloadFile* download_file = i->second; |
271 if (download_file->GetDownloadManager() == manager) { | 256 if (download_file->GetDownloadManager() == manager) { |
272 download_file->CancelDownloadRequest(resource_dispatcher_host_); | 257 download_file->CancelDownloadRequest(); |
273 to_remove.insert(download_file); | 258 to_remove.insert(download_file); |
274 } | 259 } |
275 } | 260 } |
276 | 261 |
277 for (std::set<DownloadFile*>::iterator i = to_remove.begin(); | 262 for (std::set<DownloadFile*>::iterator i = to_remove.begin(); |
278 i != to_remove.end(); ++i) { | 263 i != to_remove.end(); ++i) { |
279 downloads_.erase((*i)->id()); | 264 downloads_.erase((*i)->id()); |
280 delete *i; | 265 delete *i; |
281 } | 266 } |
282 } | 267 } |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 | 362 |
378 DownloadFile* download_file = GetDownloadFile(id); | 363 DownloadFile* download_file = GetDownloadFile(id); |
379 if (!download_file) | 364 if (!download_file) |
380 return; | 365 return; |
381 | 366 |
382 DownloadManager* download_manager = download_file->GetDownloadManager(); | 367 DownloadManager* download_manager = download_file->GetDownloadManager(); |
383 if (!download_manager) { | 368 if (!download_manager) { |
384 // Without a download manager, we can't cancel the request normally, so we | 369 // Without a download manager, we can't cancel the request normally, so we |
385 // need to do it here. The normal path will also update the download | 370 // need to do it here. The normal path will also update the download |
386 // history before cancelling the request. | 371 // history before cancelling the request. |
387 download_file->CancelDownloadRequest(resource_dispatcher_host_); | 372 download_file->CancelDownloadRequest(); |
388 return; | 373 return; |
389 } | 374 } |
390 | 375 |
391 BrowserThread::PostTask( | 376 BrowserThread::PostTask( |
392 BrowserThread::UI, FROM_HERE, | 377 BrowserThread::UI, FROM_HERE, |
393 NewRunnableMethod(download_manager, | 378 NewRunnableMethod(download_manager, |
394 &DownloadManager::DownloadCancelled, id)); | 379 &DownloadManager::DownloadCancelled, id)); |
395 } | 380 } |
396 | 381 |
397 void DownloadFileManager::EraseDownload(int id) { | 382 void DownloadFileManager::EraseDownload(int id) { |
398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 383 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
399 | 384 |
400 if (!ContainsKey(downloads_, id)) | 385 if (!ContainsKey(downloads_, id)) |
401 return; | 386 return; |
402 | 387 |
403 DownloadFile* download_file = downloads_[id]; | 388 DownloadFile* download_file = downloads_[id]; |
404 | 389 |
405 VLOG(20) << " " << __FUNCTION__ << "()" | 390 VLOG(20) << " " << __FUNCTION__ << "()" |
406 << " id = " << id | 391 << " id = " << id |
407 << " download_file = " << download_file->DebugString(); | 392 << " download_file = " << download_file->DebugString(); |
408 | 393 |
409 downloads_.erase(id); | 394 downloads_.erase(id); |
410 | 395 |
411 delete download_file; | 396 delete download_file; |
412 | 397 |
413 if (downloads_.empty()) | 398 if (downloads_.empty()) |
414 StopUpdateTimer(); | 399 StopUpdateTimer(); |
415 } | 400 } |
OLD | NEW |