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" |
11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
19 #include "base/sys_string_conversions.h" | 19 #include "base/sys_string_conversions.h" |
20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
21 #include "content/browser/download/byte_stream.h" | 21 #include "content/browser/download/byte_stream.h" |
22 #include "content/browser/download/download_create_info.h" | 22 #include "content/browser/download/download_create_info.h" |
23 #include "content/browser/download/download_file_manager.h" | 23 #include "content/browser/download/download_file_factory.h" |
24 #include "content/browser/download/download_item_impl.h" | 24 #include "content/browser/download/download_item_impl.h" |
25 #include "content/browser/download/download_stats.h" | 25 #include "content/browser/download/download_stats.h" |
26 #include "content/browser/renderer_host/render_view_host_impl.h" | 26 #include "content/browser/renderer_host/render_view_host_impl.h" |
27 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | 27 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
28 #include "content/browser/web_contents/web_contents_impl.h" | 28 #include "content/browser/web_contents/web_contents_impl.h" |
29 #include "content/public/browser/browser_context.h" | 29 #include "content/public/browser/browser_context.h" |
30 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
31 #include "content/public/browser/content_browser_client.h" | 31 #include "content/public/browser/content_browser_client.h" |
32 #include "content/public/browser/download_interrupt_reasons.h" | 32 #include "content/public/browser/download_interrupt_reasons.h" |
33 #include "content/public/browser/download_manager_delegate.h" | 33 #include "content/public/browser/download_manager_delegate.h" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 136 |
137 bool operator!=(const MapValueIteratorAdapter& that) const { | 137 bool operator!=(const MapValueIteratorAdapter& that) const { |
138 return iter_ != that.iter_; | 138 return iter_ != that.iter_; |
139 } | 139 } |
140 | 140 |
141 private: | 141 private: |
142 base::hash_map<int64, DownloadItem*>::const_iterator iter_; | 142 base::hash_map<int64, DownloadItem*>::const_iterator iter_; |
143 // Allow copy and assign. | 143 // Allow copy and assign. |
144 }; | 144 }; |
145 | 145 |
146 void EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm, | 146 void EnsureNoPendingDownloadJobsOnFile(bool* result) { |
147 bool* result) { | 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
148 if (dfm->NumberOfActiveDownloads()) | 148 *result = (content::DownloadFile::GetNumberOfDownloadFiles() == 0); |
149 *result = false; | |
150 BrowserThread::PostTask( | 149 BrowserThread::PostTask( |
151 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | 150 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
152 } | 151 } |
153 | 152 |
154 void EnsureNoPendingDownloadJobsOnIO(bool* result) { | |
155 scoped_refptr<DownloadFileManager> download_file_manager = | |
156 ResourceDispatcherHostImpl::Get()->download_file_manager(); | |
157 BrowserThread::PostTask( | |
158 BrowserThread::FILE, FROM_HERE, | |
159 base::Bind(&EnsureNoPendingDownloadsOnFile, | |
160 download_file_manager, result)); | |
161 } | |
162 | |
163 class DownloadItemFactoryImpl : public content::DownloadItemFactory { | 153 class DownloadItemFactoryImpl : public content::DownloadItemFactory { |
164 public: | 154 public: |
165 DownloadItemFactoryImpl() {} | 155 DownloadItemFactoryImpl() {} |
166 virtual ~DownloadItemFactoryImpl() {} | 156 virtual ~DownloadItemFactoryImpl() {} |
167 | 157 |
168 virtual DownloadItemImpl* CreatePersistedItem( | 158 virtual DownloadItemImpl* CreatePersistedItem( |
169 DownloadItemImplDelegate* delegate, | 159 DownloadItemImplDelegate* delegate, |
170 content::DownloadId download_id, | 160 content::DownloadId download_id, |
171 const content::DownloadPersistentStoreInfo& info, | 161 const content::DownloadPersistentStoreInfo& info, |
172 const net::BoundNetLog& bound_net_log) OVERRIDE { | 162 const net::BoundNetLog& bound_net_log) OVERRIDE { |
(...skipping 24 matching lines...) Expand all Loading... |
197 }; | 187 }; |
198 | 188 |
199 } // namespace | 189 } // namespace |
200 | 190 |
201 namespace content { | 191 namespace content { |
202 | 192 |
203 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { | 193 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { |
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
205 bool result = true; | 195 bool result = true; |
206 BrowserThread::PostTask( | 196 BrowserThread::PostTask( |
207 BrowserThread::IO, FROM_HERE, | 197 BrowserThread::FILE, FROM_HERE, |
208 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); | 198 base::Bind(&EnsureNoPendingDownloadJobsOnFile, &result)); |
209 MessageLoop::current()->Run(); | 199 MessageLoop::current()->Run(); |
210 return result; | 200 return result; |
211 } | 201 } |
212 | 202 |
213 } // namespace content | 203 } // namespace content |
214 | 204 |
215 DownloadManagerImpl::DownloadManagerImpl( | 205 DownloadManagerImpl::DownloadManagerImpl( |
216 DownloadFileManager* file_manager, | |
217 scoped_ptr<content::DownloadItemFactory> factory, | 206 scoped_ptr<content::DownloadItemFactory> factory, |
218 net::NetLog* net_log) | 207 net::NetLog* net_log) |
219 : factory_(factory.Pass()), | 208 : factory_(factory.Pass()), |
220 history_size_(0), | 209 history_size_(0), |
221 shutdown_needed_(false), | 210 shutdown_needed_(false), |
222 browser_context_(NULL), | 211 browser_context_(NULL), |
223 file_manager_(file_manager), | |
224 delegate_(NULL), | 212 delegate_(NULL), |
225 net_log_(net_log) { | 213 net_log_(net_log) { |
226 DCHECK(file_manager); | |
227 if (!factory_.get()) | 214 if (!factory_.get()) |
228 factory_.reset(new DownloadItemFactoryImpl()); | 215 factory_.reset(new DownloadItemFactoryImpl()); |
| 216 file_factory_.reset(new content::DownloadFileFactory()); |
229 } | 217 } |
230 | 218 |
231 DownloadManagerImpl::~DownloadManagerImpl() { | 219 DownloadManagerImpl::~DownloadManagerImpl() { |
232 DCHECK(!shutdown_needed_); | 220 DCHECK(!shutdown_needed_); |
233 } | 221 } |
234 | 222 |
235 DownloadId DownloadManagerImpl::GetNextId() { | 223 DownloadId DownloadManagerImpl::GetNextId() { |
236 DownloadId id; | 224 DownloadId id; |
237 if (delegate_) | 225 if (delegate_) |
238 id = delegate_->GetNextId(); | 226 id = delegate_->GetNextId(); |
239 if (!id.IsValid()) { | 227 if (!id.IsValid()) { |
240 static int next_id; | 228 static int next_id; |
241 id = DownloadId(browser_context_, ++next_id); | 229 id = DownloadId(browser_context_, ++next_id); |
242 } | 230 } |
243 | 231 |
244 return id; | 232 return id; |
245 } | 233 } |
246 | 234 |
| 235 content::DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactory() { |
| 236 return file_factory_.get(); |
| 237 } |
| 238 |
| 239 void DownloadManagerImpl::DelegateStart(DownloadItemImpl* item) { |
| 240 int32 download_id = item->GetId(); |
| 241 |
| 242 if (!delegate_ || delegate_->ShouldStartDownload(download_id)) |
| 243 RestartDownload(download_id); |
| 244 } |
| 245 |
247 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { | 246 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { |
248 if (!delegate_) | 247 if (!delegate_) |
249 return true; | 248 return true; |
250 | 249 |
251 return delegate_->ShouldOpenDownload(item); | 250 return delegate_->ShouldOpenDownload(item); |
252 } | 251 } |
253 | 252 |
254 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { | 253 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { |
255 if (!delegate_) | 254 if (!delegate_) |
256 return false; | 255 return false; |
(...skipping 13 matching lines...) Expand all Loading... |
270 void DownloadManagerImpl::Shutdown() { | 269 void DownloadManagerImpl::Shutdown() { |
271 VLOG(20) << __FUNCTION__ << "()" | 270 VLOG(20) << __FUNCTION__ << "()" |
272 << " shutdown_needed_ = " << shutdown_needed_; | 271 << " shutdown_needed_ = " << shutdown_needed_; |
273 if (!shutdown_needed_) | 272 if (!shutdown_needed_) |
274 return; | 273 return; |
275 shutdown_needed_ = false; | 274 shutdown_needed_ = false; |
276 | 275 |
277 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); | 276 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); |
278 // TODO(benjhayden): Consider clearing observers_. | 277 // TODO(benjhayden): Consider clearing observers_. |
279 | 278 |
280 DCHECK(file_manager_); | 279 // The DownloadFiles will be canceled and deleted by their DownloadItems. |
281 BrowserThread::PostTask( | |
282 BrowserThread::FILE, FROM_HERE, | |
283 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, | |
284 file_manager_, make_scoped_refptr(this))); | |
285 | 280 |
286 AssertContainersConsistent(); | 281 AssertContainersConsistent(); |
287 | 282 |
288 // Go through all downloads in downloads_. Dangerous ones we need to | 283 // Go through all downloads in downloads_. Dangerous ones we need to |
289 // remove on disk, and in progress ones we need to cancel. | 284 // remove on disk, and in progress ones we need to cancel. |
290 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { | 285 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { |
291 DownloadItemImpl* download = it->second; | 286 DownloadItemImpl* download = it->second; |
292 | 287 |
293 // Save iterator from potential erases in this set done by called code. | 288 // Save iterator from potential erases in this set done by called code. |
294 // Iterators after an erasure point are still valid for lists and | 289 // Iterators after an erasure point are still valid for lists and |
(...skipping 26 matching lines...) Expand all Loading... |
321 // in DownloadItem destruction. | 316 // in DownloadItem destruction. |
322 DownloadMap downloads_to_delete; | 317 DownloadMap downloads_to_delete; |
323 downloads_to_delete.swap(downloads_); | 318 downloads_to_delete.swap(downloads_); |
324 | 319 |
325 active_downloads_.clear(); | 320 active_downloads_.clear(); |
326 STLDeleteValues(&downloads_to_delete); | 321 STLDeleteValues(&downloads_to_delete); |
327 | 322 |
328 // We'll have nothing more to report to the observers after this point. | 323 // We'll have nothing more to report to the observers after this point. |
329 observers_.Clear(); | 324 observers_.Clear(); |
330 | 325 |
331 file_manager_ = NULL; | |
332 if (delegate_) | 326 if (delegate_) |
333 delegate_->Shutdown(); | 327 delegate_->Shutdown(); |
334 } | 328 } |
335 | 329 |
336 void DownloadManagerImpl::GetTemporaryDownloads( | 330 void DownloadManagerImpl::GetTemporaryDownloads( |
337 const FilePath& dir_path, DownloadVector* result) { | 331 const FilePath& dir_path, DownloadVector* result) { |
338 DCHECK(result); | 332 DCHECK(result); |
339 | 333 |
340 for (DownloadMap::iterator it = downloads_.begin(); | 334 for (DownloadMap::iterator it = downloads_.begin(); |
341 it != downloads_.end(); ++it) { | 335 it != downloads_.end(); ++it) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { | 382 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { |
389 DCHECK(browser_context); | 383 DCHECK(browser_context); |
390 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 384 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
391 shutdown_needed_ = true; | 385 shutdown_needed_ = true; |
392 | 386 |
393 browser_context_ = browser_context; | 387 browser_context_ = browser_context; |
394 | 388 |
395 return true; | 389 return true; |
396 } | 390 } |
397 | 391 |
398 // We have received a message from DownloadFileManager about a new download. | |
399 content::DownloadId DownloadManagerImpl::StartDownload( | 392 content::DownloadId DownloadManagerImpl::StartDownload( |
400 scoped_ptr<DownloadCreateInfo> info, | 393 scoped_ptr<DownloadCreateInfo> info, |
401 scoped_ptr<content::ByteStreamReader> stream) { | 394 scoped_ptr<content::ByteStreamReader> stream) { |
402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 395 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
403 | 396 |
404 // |bound_net_log| will be used for logging both the download item's and | 397 net::BoundNetLog bound_net_log = |
405 // the download file's events. | 398 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
406 net::BoundNetLog bound_net_log = CreateDownloadItem(info.get()); | 399 DownloadItemImpl* download = |
| 400 CreateDownloadItem(info.get(), bound_net_log); |
| 401 scoped_ptr<content::DownloadFile> download_file( |
| 402 GetDownloadFileFactory()->CreateFile( |
| 403 info->save_info, info->url(), info->referrer_url, |
| 404 info->received_bytes, GenerateFileHash(), |
| 405 stream.Pass(), bound_net_log, |
| 406 download->DestinationControllerAsWeakPtr())); |
| 407 download->Start(download_file.Pass()); |
407 | 408 |
408 // If info->download_id was unknown on entry to this function, it was | 409 return download->GetGlobalId(); |
409 // assigned in CreateDownloadItem. | |
410 DownloadId download_id = info->download_id; | |
411 | |
412 DownloadFileManager::CreateDownloadFileCallback callback( | |
413 base::Bind(&DownloadManagerImpl::OnDownloadFileCreated, | |
414 this, download_id.local())); | |
415 | |
416 BrowserThread::PostTask( | |
417 BrowserThread::FILE, FROM_HERE, | |
418 base::Bind(&DownloadFileManager::CreateDownloadFile, | |
419 file_manager_, base::Passed(info.Pass()), | |
420 base::Passed(stream.Pass()), | |
421 make_scoped_refptr(this), | |
422 GenerateFileHash(), bound_net_log, | |
423 callback)); | |
424 | |
425 return download_id; | |
426 } | |
427 | |
428 void DownloadManagerImpl::OnDownloadFileCreated( | |
429 int32 download_id, content::DownloadInterruptReason reason) { | |
430 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { | |
431 OnDownloadInterrupted(download_id, reason); | |
432 // TODO(rdsmith): It makes no sense to continue along the | |
433 // regular download path after we've gotten an error. But it's | |
434 // the way the code has historically worked, and this allows us | |
435 // to get the download persisted and observers of the download manager | |
436 // notified, so tests work. When we execute all side effects of cancel | |
437 // (including queue removal) immedately rather than waiting for | |
438 // persistence we should replace this comment with a "return;". | |
439 } | |
440 | |
441 if (!delegate_ || delegate_->ShouldStartDownload(download_id)) | |
442 RestartDownload(download_id); | |
443 } | 410 } |
444 | 411 |
445 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { | 412 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
446 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
447 for (DownloadMap::iterator it = downloads_.begin(); | 414 for (DownloadMap::iterator it = downloads_.begin(); |
448 it != downloads_.end(); ++it) { | 415 it != downloads_.end(); ++it) { |
449 DownloadItemImpl* item = it->second; | 416 DownloadItemImpl* item = it->second; |
450 if (item->IsPersisted()) | 417 if (item->IsPersisted()) |
451 CheckForFileRemoval(item); | 418 CheckForFileRemoval(item); |
452 } | 419 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 } | 476 } |
510 | 477 |
511 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { | 478 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
512 return browser_context_; | 479 return browser_context_; |
513 } | 480 } |
514 | 481 |
515 FilePath DownloadManagerImpl::LastDownloadPath() { | 482 FilePath DownloadManagerImpl::LastDownloadPath() { |
516 return last_download_path_; | 483 return last_download_path_; |
517 } | 484 } |
518 | 485 |
519 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( | 486 DownloadItemImpl* DownloadManagerImpl::CreateDownloadItem( |
520 DownloadCreateInfo* info) { | 487 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log) { |
521 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 488 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
522 | 489 |
523 net::BoundNetLog bound_net_log = | |
524 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | |
525 if (!info->download_id.IsValid()) | 490 if (!info->download_id.IsValid()) |
526 info->download_id = GetNextId(); | 491 info->download_id = GetNextId(); |
527 DownloadItemImpl* download = factory_->CreateActiveItem( | 492 DownloadItemImpl* download = factory_->CreateActiveItem( |
528 this, *info, | 493 this, *info, |
529 scoped_ptr<DownloadRequestHandleInterface>( | 494 scoped_ptr<DownloadRequestHandleInterface>( |
530 new DownloadRequestHandle(info->request_handle)).Pass(), | 495 new DownloadRequestHandle(info->request_handle)).Pass(), |
531 browser_context_->IsOffTheRecord(), bound_net_log); | 496 browser_context_->IsOffTheRecord(), bound_net_log); |
532 | 497 |
533 DCHECK(!ContainsKey(downloads_, download->GetId())); | 498 DCHECK(!ContainsKey(downloads_, download->GetId())); |
534 downloads_[download->GetId()] = download; | 499 downloads_[download->GetId()] = download; |
535 DCHECK(!ContainsKey(active_downloads_, download->GetId())); | 500 DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
536 active_downloads_[download->GetId()] = download; | 501 active_downloads_[download->GetId()] = download; |
537 | 502 |
538 return bound_net_log; | 503 return download; |
539 } | 504 } |
540 | 505 |
541 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( | 506 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
542 const FilePath& main_file_path, | 507 const FilePath& main_file_path, |
543 const GURL& page_url, | 508 const GURL& page_url, |
544 bool is_otr, | 509 bool is_otr, |
545 const std::string& mime_type, | 510 const std::string& mime_type, |
546 DownloadItem::Observer* observer) { | 511 DownloadItem::Observer* observer) { |
547 net::BoundNetLog bound_net_log = | 512 net::BoundNetLog bound_net_log = |
548 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 513 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 if (delegate_) | 555 if (delegate_) |
591 intermediate_path = delegate_->GetIntermediatePath(*download); | 556 intermediate_path = delegate_->GetIntermediatePath(*download); |
592 else | 557 else |
593 intermediate_path = download->GetTargetFilePath(); | 558 intermediate_path = download->GetTargetFilePath(); |
594 | 559 |
595 // We want the intermediate and target paths to refer to the same directory so | 560 // We want the intermediate and target paths to refer to the same directory so |
596 // that they are both on the same device and subject to same | 561 // that they are both on the same device and subject to same |
597 // space/permission/availability constraints. | 562 // space/permission/availability constraints. |
598 DCHECK(intermediate_path.DirName() == | 563 DCHECK(intermediate_path.DirName() == |
599 download->GetTargetFilePath().DirName()); | 564 download->GetTargetFilePath().DirName()); |
600 download->OnIntermediatePathDetermined(file_manager_, intermediate_path); | 565 download->OnIntermediatePathDetermined(intermediate_path); |
601 } | |
602 | |
603 void DownloadManagerImpl::UpdateDownload(int32 download_id, | |
604 int64 bytes_so_far, | |
605 int64 bytes_per_sec, | |
606 const std::string& hash_state) { | |
607 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
608 DownloadMap::iterator it = active_downloads_.find(download_id); | |
609 if (it != active_downloads_.end()) { | |
610 DownloadItemImpl* download = it->second; | |
611 if (download->IsInProgress()) { | |
612 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state); | |
613 if (delegate_) | |
614 delegate_->UpdateItemInPersistentStore(download); | |
615 } | |
616 } | |
617 } | |
618 | |
619 void DownloadManagerImpl::OnResponseCompleted(int32 download_id, | |
620 int64 size, | |
621 const std::string& hash) { | |
622 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
623 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id | |
624 << " size = " << size; | |
625 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
626 | |
627 // If it's not in active_downloads_, that means it was cancelled; just | |
628 // ignore the notification. | |
629 if (active_downloads_.count(download_id) == 0) | |
630 return; | |
631 | |
632 DownloadItemImpl* download = active_downloads_[download_id]; | |
633 download->OnAllDataSaved(size, hash); | |
634 MaybeCompleteDownload(download); | |
635 } | 566 } |
636 | 567 |
637 void DownloadManagerImpl::AssertStateConsistent( | 568 void DownloadManagerImpl::AssertStateConsistent( |
638 DownloadItemImpl* download) const { | 569 DownloadItemImpl* download) const { |
639 if (download->GetState() == DownloadItem::REMOVING) { | 570 if (download->GetState() == DownloadItem::REMOVING) { |
640 DCHECK(!ContainsKey(downloads_, download->GetId())); | 571 DCHECK(!ContainsKey(downloads_, download->GetId())); |
641 DCHECK(!ContainsKey(active_downloads_, download->GetId())); | 572 DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
642 return; | 573 return; |
643 } | 574 } |
644 | 575 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
721 if (delegate_ && !delegate_->ShouldCompleteDownload(download, base::Bind( | 652 if (delegate_ && !delegate_->ShouldCompleteDownload(download, base::Bind( |
722 &DownloadManagerImpl::MaybeCompleteDownloadById, | 653 &DownloadManagerImpl::MaybeCompleteDownloadById, |
723 this, download->GetId()))) | 654 this, download->GetId()))) |
724 return; | 655 return; |
725 | 656 |
726 VLOG(20) << __FUNCTION__ << "()" << " executing: download = " | 657 VLOG(20) << __FUNCTION__ << "()" << " executing: download = " |
727 << download->DebugString(false); | 658 << download->DebugString(false); |
728 | 659 |
729 if (delegate_) | 660 if (delegate_) |
730 delegate_->UpdateItemInPersistentStore(download); | 661 delegate_->UpdateItemInPersistentStore(download); |
731 download->OnDownloadCompleting(file_manager_); | 662 download->OnDownloadCompleting(); |
732 } | 663 } |
733 | 664 |
734 void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) { | 665 void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) { |
735 if (ContainsKey(active_downloads_, download_id)) | 666 if (ContainsKey(active_downloads_, download_id)) |
736 MaybeCompleteDownload(active_downloads_[download_id]); | 667 MaybeCompleteDownload(active_downloads_[download_id]); |
737 } | 668 } |
738 | 669 |
739 void DownloadManagerImpl::DownloadCompleted(DownloadItemImpl* download) { | 670 void DownloadManagerImpl::DownloadCompleted(DownloadItemImpl* download) { |
740 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 671 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
741 DCHECK(download); | 672 DCHECK(download); |
(...skipping 14 matching lines...) Expand all Loading... |
756 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 687 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
757 | 688 |
758 VLOG(20) << __FUNCTION__ << "()" | 689 VLOG(20) << __FUNCTION__ << "()" |
759 << " download = " << download->DebugString(true); | 690 << " download = " << download->DebugString(true); |
760 | 691 |
761 RemoveFromActiveList(download); | 692 RemoveFromActiveList(download); |
762 // This function is called from the DownloadItem, so DI state | 693 // This function is called from the DownloadItem, so DI state |
763 // should already have been updated. | 694 // should already have been updated. |
764 AssertStateConsistent(download); | 695 AssertStateConsistent(download); |
765 | 696 |
766 DCHECK(file_manager_); | 697 download->OffThreadCancel(); |
767 download->OffThreadCancel(file_manager_); | |
768 } | |
769 | |
770 void DownloadManagerImpl::OnDownloadInterrupted( | |
771 int32 download_id, | |
772 content::DownloadInterruptReason reason) { | |
773 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
774 | |
775 if (!ContainsKey(active_downloads_, download_id)) | |
776 return; | |
777 active_downloads_[download_id]->Interrupt(reason); | |
778 } | 698 } |
779 | 699 |
780 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { | 700 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
781 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 701 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
782 DCHECK(download); | 702 DCHECK(download); |
783 | 703 |
784 // Clean up will happen when the history system create callback runs if we | 704 // Clean up will happen when the history system create callback runs if we |
785 // don't have a valid db_handle yet. | 705 // don't have a valid db_handle yet. |
786 if (download->IsPersisted()) { | 706 if (download->IsPersisted()) { |
787 active_downloads_.erase(download->GetId()); | 707 active_downloads_.erase(download->GetId()); |
788 if (delegate_) | 708 if (delegate_) |
789 delegate_->UpdateItemInPersistentStore(download); | 709 delegate_->UpdateItemInPersistentStore(download); |
790 } | 710 } |
791 } | 711 } |
792 | 712 |
793 bool DownloadManagerImpl::GenerateFileHash() { | 713 bool DownloadManagerImpl::GenerateFileHash() { |
794 return delegate_ && delegate_->GenerateFileHash(); | 714 return delegate_ && delegate_->GenerateFileHash(); |
795 } | 715 } |
796 | 716 |
| 717 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( |
| 718 scoped_ptr<content::DownloadFileFactory> file_factory) { |
| 719 file_factory_ = file_factory.Pass(); |
| 720 } |
| 721 |
797 int DownloadManagerImpl::RemoveDownloadItems( | 722 int DownloadManagerImpl::RemoveDownloadItems( |
798 const DownloadItemImplVector& pending_deletes) { | 723 const DownloadItemImplVector& pending_deletes) { |
799 if (pending_deletes.empty()) | 724 if (pending_deletes.empty()) |
800 return 0; | 725 return 0; |
801 | 726 |
802 // Delete from internal maps. | 727 // Delete from internal maps. |
803 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); | 728 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
804 it != pending_deletes.end(); | 729 it != pending_deletes.end(); |
805 ++it) { | 730 ++it) { |
806 DownloadItemImpl* download = *it; | 731 DownloadItemImpl* download = *it; |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1153 if (item->IsComplete() && | 1078 if (item->IsComplete() && |
1154 !item->GetOpened()) | 1079 !item->GetOpened()) |
1155 ++num_unopened; | 1080 ++num_unopened; |
1156 } | 1081 } |
1157 download_stats::RecordOpensOutstanding(num_unopened); | 1082 download_stats::RecordOpensOutstanding(num_unopened); |
1158 } | 1083 } |
1159 | 1084 |
1160 void DownloadManagerImpl::DownloadRenamedToIntermediateName( | 1085 void DownloadManagerImpl::DownloadRenamedToIntermediateName( |
1161 DownloadItemImpl* download) { | 1086 DownloadItemImpl* download) { |
1162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1087 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1163 // If the rename failed, we receive an OnDownloadInterrupted() call before we | 1088 // If the rename failed, we processed an interrupt before we get here. |
1164 // receive the DownloadRenamedToIntermediateName() call. | |
1165 if (delegate_) | 1089 if (delegate_) |
1166 delegate_->AddItemToPersistentStore(download); | 1090 delegate_->AddItemToPersistentStore(download); |
1167 } | 1091 } |
1168 | 1092 |
1169 void DownloadManagerImpl::DownloadRenamedToFinalName( | 1093 void DownloadManagerImpl::DownloadRenamedToFinalName( |
1170 DownloadItemImpl* download) { | 1094 DownloadItemImpl* download) { |
1171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1095 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1172 // If the rename failed, we receive an OnDownloadInterrupted() call before we | 1096 // If the rename failed, we processed an interrupt before we get here. |
1173 // receive the DownloadRenamedToFinalName() call. | |
1174 if (delegate_) { | 1097 if (delegate_) { |
1175 delegate_->UpdatePathForItemInPersistentStore( | 1098 delegate_->UpdatePathForItemInPersistentStore( |
1176 download, download->GetFullPath()); | 1099 download, download->GetFullPath()); |
1177 } | 1100 } |
1178 } | 1101 } |
OLD | NEW |