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