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" |
(...skipping 50 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->process_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 BrowserThread::PostTask( |
82 BrowserThread::IO, FROM_HERE, | 81 BrowserThread::IO, FROM_HERE, |
83 NewRunnableMethod(this, &DownloadFileManager::ResumeDownloadRequest, | 82 NewRunnableMethod(this, &DownloadFileManager::ResumeDownloadRequest, |
84 info->process_handle)); | 83 info->process_handle)); |
85 | 84 |
86 StartUpdateTimer(); | 85 StartUpdateTimer(); |
87 | 86 |
88 BrowserThread::PostTask( | 87 BrowserThread::PostTask( |
89 BrowserThread::UI, FROM_HERE, | 88 BrowserThread::UI, FROM_HERE, |
90 NewRunnableMethod(download_manager, | 89 NewRunnableMethod(download_manager, |
91 &DownloadManager::StartDownload, id)); | 90 &DownloadManager::StartDownload, id)); |
92 } | 91 } |
93 | 92 |
94 void DownloadFileManager::ResumeDownloadRequest( | 93 void DownloadFileManager::ResumeDownloadRequest( |
95 DownloadProcessHandle process) { | 94 DownloadProcessHandle process) { |
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
97 | 96 |
98 // This balances the pause in DownloadResourceHandler::OnResponseStarted. | 97 // This balances the pause in DownloadResourceHandler::OnResponseStarted. |
99 resource_dispatcher_host_->PauseRequest(process.child_id(), | 98 process.PauseRequest(false); |
100 process.request_id(), | |
101 false); | |
102 } | 99 } |
103 | 100 |
104 DownloadFile* DownloadFileManager::GetDownloadFile(int id) { | 101 DownloadFile* DownloadFileManager::GetDownloadFile(int id) { |
105 DownloadFileMap::iterator it = downloads_.find(id); | 102 DownloadFileMap::iterator it = downloads_.find(id); |
106 return it == downloads_.end() ? NULL : it->second; | 103 return it == downloads_.end() ? NULL : it->second; |
107 } | 104 } |
108 | 105 |
109 void DownloadFileManager::StartUpdateTimer() { | 106 void DownloadFileManager::StartUpdateTimer() { |
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
111 if (!update_timer_.IsRunning()) { | 108 if (!update_timer_.IsRunning()) { |
(...skipping 28 matching lines...) Expand all Loading... | |
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
141 return next_id_++; | 138 return next_id_++; |
142 } | 139 } |
143 | 140 |
144 void DownloadFileManager::StartDownload(DownloadCreateInfo* info) { | 141 void DownloadFileManager::StartDownload(DownloadCreateInfo* info) { |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
146 DCHECK(info); | 143 DCHECK(info); |
147 | 144 |
148 DownloadManager* manager = info->process_handle.GetDownloadManager(); | 145 DownloadManager* manager = info->process_handle.GetDownloadManager(); |
149 if (!manager) { | 146 if (!manager) { |
150 download_util::CancelDownloadRequest(resource_dispatcher_host_, | 147 info->process_handle.CancelRequest(); |
151 info->process_handle); | |
152 delete info; | 148 delete info; |
153 return; | 149 return; |
154 } | 150 } |
155 | 151 |
156 // TODO(phajdan.jr): fix the duplication of path info below. | 152 // TODO(phajdan.jr): fix the duplication of path info below. |
157 info->path = info->save_info.file_path; | 153 info->path = info->save_info.file_path; |
158 | 154 |
159 manager->CreateDownloadItem(info); | 155 manager->CreateDownloadItem(info); |
160 | 156 |
161 bool hash_needed = resource_dispatcher_host_->safe_browsing_service()-> | 157 bool hash_needed = resource_dispatcher_host_->safe_browsing_service()-> |
Paweł Hajdan Jr.
2011/06/04 08:58:51
It seems that this is now the only place where res
Randy Smith (Not in Mondays)
2011/06/07 22:41:11
I'm reluctant to do that, as it doesn't feel like
| |
162 DownloadBinHashNeeded(); | 158 DownloadBinHashNeeded(); |
163 | 159 |
164 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 160 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
165 NewRunnableMethod(this, &DownloadFileManager::CreateDownloadFile, | 161 NewRunnableMethod(this, &DownloadFileManager::CreateDownloadFile, |
166 info, make_scoped_refptr(manager), hash_needed)); | 162 info, make_scoped_refptr(manager), hash_needed)); |
167 } | 163 } |
168 | 164 |
169 // We don't forward an update to the UI thread here, since we want to throttle | 165 // We don't forward an update to the UI thread here, since we want to throttle |
170 // the UI update rate via a periodic timer. If the user has cancelled the | 166 // the UI update rate via a periodic timer. If the user has cancelled the |
171 // download (in the UI thread), we may receive a few more updates before the IO | 167 // download (in the UI thread), we may receive a few more updates before the IO |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
262 void DownloadFileManager::OnDownloadManagerShutdown(DownloadManager* manager) { | 258 void DownloadFileManager::OnDownloadManagerShutdown(DownloadManager* manager) { |
263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
264 DCHECK(manager); | 260 DCHECK(manager); |
265 | 261 |
266 std::set<DownloadFile*> to_remove; | 262 std::set<DownloadFile*> to_remove; |
267 | 263 |
268 for (DownloadFileMap::iterator i = downloads_.begin(); | 264 for (DownloadFileMap::iterator i = downloads_.begin(); |
269 i != downloads_.end(); ++i) { | 265 i != downloads_.end(); ++i) { |
270 DownloadFile* download_file = i->second; | 266 DownloadFile* download_file = i->second; |
271 if (download_file->GetDownloadManager() == manager) { | 267 if (download_file->GetDownloadManager() == manager) { |
272 download_file->CancelDownloadRequest(resource_dispatcher_host_); | 268 download_file->CancelDownloadRequest(); |
273 to_remove.insert(download_file); | 269 to_remove.insert(download_file); |
274 } | 270 } |
275 } | 271 } |
276 | 272 |
277 for (std::set<DownloadFile*>::iterator i = to_remove.begin(); | 273 for (std::set<DownloadFile*>::iterator i = to_remove.begin(); |
278 i != to_remove.end(); ++i) { | 274 i != to_remove.end(); ++i) { |
279 downloads_.erase((*i)->id()); | 275 downloads_.erase((*i)->id()); |
280 delete *i; | 276 delete *i; |
281 } | 277 } |
282 } | 278 } |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
377 | 373 |
378 DownloadFile* download_file = GetDownloadFile(id); | 374 DownloadFile* download_file = GetDownloadFile(id); |
379 if (!download_file) | 375 if (!download_file) |
380 return; | 376 return; |
381 | 377 |
382 DownloadManager* download_manager = download_file->GetDownloadManager(); | 378 DownloadManager* download_manager = download_file->GetDownloadManager(); |
383 if (!download_manager) { | 379 if (!download_manager) { |
384 // Without a download manager, we can't cancel the request normally, so we | 380 // 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 | 381 // need to do it here. The normal path will also update the download |
386 // history before cancelling the request. | 382 // history before cancelling the request. |
387 download_file->CancelDownloadRequest(resource_dispatcher_host_); | 383 download_file->CancelDownloadRequest(); |
388 return; | 384 return; |
389 } | 385 } |
390 | 386 |
391 BrowserThread::PostTask( | 387 BrowserThread::PostTask( |
392 BrowserThread::UI, FROM_HERE, | 388 BrowserThread::UI, FROM_HERE, |
393 NewRunnableMethod(download_manager, | 389 NewRunnableMethod(download_manager, |
394 &DownloadManager::DownloadCancelled, id)); | 390 &DownloadManager::DownloadCancelled, id)); |
395 } | 391 } |
396 | 392 |
397 void DownloadFileManager::EraseDownload(int id) { | 393 void DownloadFileManager::EraseDownload(int id) { |
398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 394 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
399 | 395 |
400 if (!ContainsKey(downloads_, id)) | 396 if (!ContainsKey(downloads_, id)) |
401 return; | 397 return; |
402 | 398 |
403 DownloadFile* download_file = downloads_[id]; | 399 DownloadFile* download_file = downloads_[id]; |
404 | 400 |
405 VLOG(20) << " " << __FUNCTION__ << "()" | 401 VLOG(20) << " " << __FUNCTION__ << "()" |
406 << " id = " << id | 402 << " id = " << id |
407 << " download_file = " << download_file->DebugString(); | 403 << " download_file = " << download_file->DebugString(); |
408 | 404 |
409 downloads_.erase(id); | 405 downloads_.erase(id); |
410 | 406 |
411 delete download_file; | 407 delete download_file; |
412 | 408 |
413 if (downloads_.empty()) | 409 if (downloads_.empty()) |
414 StopUpdateTimer(); | 410 StopUpdateTimer(); |
415 } | 411 } |
OLD | NEW |