| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/stl_util-inl.h" |
| 8 #include "base/task.h" | 9 #include "base/task.h" |
| 9 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 10 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 11 #include "chrome/browser/chrome_thread.h" | 12 #include "chrome/browser/chrome_thread.h" |
| 12 #include "chrome/browser/download/download_manager.h" | 13 #include "chrome/browser/download/download_manager.h" |
| 13 #include "chrome/browser/download/download_util.h" | 14 #include "chrome/browser/download/download_util.h" |
| 14 #include "chrome/browser/history/download_types.h" | 15 #include "chrome/browser/history/download_types.h" |
| 15 #include "chrome/browser/net/chrome_url_request_context.h" | 16 #include "chrome/browser/net/chrome_url_request_context.h" |
| 16 #include "chrome/browser/platform_util.h" | 17 #include "chrome/browser/platform_util.h" |
| 17 #include "chrome/browser/profile.h" | 18 #include "chrome/browser/profile.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 52 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 52 StopUpdateTimer(); | 53 StopUpdateTimer(); |
| 53 ChromeThread::PostTask( | 54 ChromeThread::PostTask( |
| 54 ChromeThread::FILE, FROM_HERE, | 55 ChromeThread::FILE, FROM_HERE, |
| 55 NewRunnableMethod(this, &DownloadFileManager::OnShutdown)); | 56 NewRunnableMethod(this, &DownloadFileManager::OnShutdown)); |
| 56 } | 57 } |
| 57 | 58 |
| 58 // Cease download thread operations. | 59 // Cease download thread operations. |
| 59 void DownloadFileManager::OnShutdown() { | 60 void DownloadFileManager::OnShutdown() { |
| 60 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 61 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 61 // Delete any partial downloads during shutdown. | 62 STLDeleteValues(&downloads_); |
| 62 for (DownloadFileMap::iterator it = downloads_.begin(); | |
| 63 it != downloads_.end(); ++it) { | |
| 64 DownloadFile* download = it->second; | |
| 65 if (download->in_progress()) | |
| 66 download->Cancel(); | |
| 67 delete download; | |
| 68 } | |
| 69 downloads_.clear(); | |
| 70 } | 63 } |
| 71 | 64 |
| 72 // Notifications sent from the download thread and run on the UI thread. | 65 // Notifications sent from the download thread and run on the UI thread. |
| 73 | 66 |
| 74 // Lookup the DownloadManager for this TabContents' profile and inform it of | 67 // Lookup the DownloadManager for this TabContents' profile and inform it of |
| 75 // a new download. | 68 // a new download. |
| 76 // TODO(paulg): When implementing download restart via the Downloads tab, | 69 // TODO(paulg): When implementing download restart via the Downloads tab, |
| 77 // there will be no 'render_process_id' or 'render_view_id'. | 70 // there will be no 'render_process_id' or 'render_view_id'. |
| 78 void DownloadFileManager::OnStartDownload(DownloadCreateInfo* info) { | 71 void DownloadFileManager::OnStartDownload(DownloadCreateInfo* info) { |
| 79 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 72 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 // | 432 // |
| 440 // There are 3 possible rename cases where this method can be called: | 433 // There are 3 possible rename cases where this method can be called: |
| 441 // 1. tmp -> foo (need_delete_crdownload=T) | 434 // 1. tmp -> foo (need_delete_crdownload=T) |
| 442 // 2. foo.crdownload -> foo (need_delete_crdownload=F) | 435 // 2. foo.crdownload -> foo (need_delete_crdownload=F) |
| 443 // 3. tmp-> unconfirmed.xxx.crdownload (need_delete_crdownload=F) | 436 // 3. tmp-> unconfirmed.xxx.crdownload (need_delete_crdownload=F) |
| 444 void DownloadFileManager::OnFinalDownloadName(int id, | 437 void DownloadFileManager::OnFinalDownloadName(int id, |
| 445 const FilePath& full_path, | 438 const FilePath& full_path, |
| 446 bool need_delete_crdownload, | 439 bool need_delete_crdownload, |
| 447 DownloadManager* manager) { | 440 DownloadManager* manager) { |
| 448 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 441 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 449 DownloadFileMap::iterator it = downloads_.find(id); | 442 |
| 450 if (it == downloads_.end()) | 443 DownloadFile* download = GetDownloadFile(id); |
| 444 if (!download) |
| 451 return; | 445 return; |
| 452 | 446 |
| 453 DownloadFile* download = it->second; | |
| 454 if (download->Rename(full_path, true)) { | 447 if (download->Rename(full_path, true)) { |
| 455 #if defined(OS_MACOSX) | 448 #if defined(OS_MACOSX) |
| 456 // Done here because we only want to do this once; see | 449 // Done here because we only want to do this once; see |
| 457 // http://crbug.com/13120 for details. | 450 // http://crbug.com/13120 for details. |
| 458 download->AnnotateWithSourceInformation(); | 451 download->AnnotateWithSourceInformation(); |
| 459 #endif | 452 #endif |
| 460 ChromeThread::PostTask( | 453 ChromeThread::PostTask( |
| 461 ChromeThread::UI, FROM_HERE, | 454 ChromeThread::UI, FROM_HERE, |
| 462 NewRunnableMethod( | 455 NewRunnableMethod( |
| 463 manager, &DownloadManager::DownloadRenamedToFinalName, id, | 456 manager, &DownloadManager::DownloadRenamedToFinalName, id, |
| 464 full_path)); | 457 full_path)); |
| 465 } else { | 458 } else { |
| 466 // Error. Between the time the UI thread generated 'full_path' to the time | 459 // Error. Between the time the UI thread generated 'full_path' to the time |
| 467 // this code runs, something happened that prevents us from renaming. | 460 // this code runs, something happened that prevents us from renaming. |
| 468 CancelDownloadOnRename(id); | 461 CancelDownloadOnRename(id); |
| 469 } | 462 } |
| 470 | 463 |
| 471 if (need_delete_crdownload) | 464 if (need_delete_crdownload) |
| 472 download->DeleteCrDownload(); | 465 download->DeleteCrDownload(); |
| 473 | 466 |
| 474 // If the download has completed before we got this final name, we remove it | 467 // If the download has completed before we got this final name, we remove it |
| 475 // from our in progress map. | 468 // from our in progress map. |
| 476 if (!download->in_progress()) { | 469 if (!download->in_progress()) { |
| 477 downloads_.erase(it); | 470 downloads_.erase(id); |
| 478 delete download; | 471 delete download; |
| 479 } | 472 } |
| 480 | 473 |
| 481 if (downloads_.empty()) { | 474 if (downloads_.empty()) { |
| 482 ChromeThread::PostTask( | 475 ChromeThread::PostTask( |
| 483 ChromeThread::UI, FROM_HERE, | 476 ChromeThread::UI, FROM_HERE, |
| 484 NewRunnableMethod(this, &DownloadFileManager::StopUpdateTimer)); | 477 NewRunnableMethod(this, &DownloadFileManager::StopUpdateTimer)); |
| 485 } | 478 } |
| 486 } | 479 } |
| 487 | 480 |
| 488 // Called only from OnFinalDownloadName or OnIntermediateDownloadName | 481 // Called only from OnFinalDownloadName or OnIntermediateDownloadName |
| 489 // on the FILE thread. | 482 // on the FILE thread. |
| 490 void DownloadFileManager::CancelDownloadOnRename(int id) { | 483 void DownloadFileManager::CancelDownloadOnRename(int id) { |
| 491 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 484 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 492 DownloadFileMap::iterator it = downloads_.find(id); | 485 |
| 493 if (it == downloads_.end()) | 486 DownloadFile* download = GetDownloadFile(id); |
| 487 if (!download) |
| 494 return; | 488 return; |
| 495 | 489 |
| 496 DownloadFile* download = it->second; | |
| 497 DownloadManagerMap::iterator dmit = managers_.find(download->id()); | 490 DownloadManagerMap::iterator dmit = managers_.find(download->id()); |
| 498 if (dmit != managers_.end()) { | 491 if (dmit != managers_.end()) { |
| 499 DownloadManager* dlm = dmit->second; | 492 DownloadManager* dlm = dmit->second; |
| 500 ChromeThread::PostTask( | 493 ChromeThread::PostTask( |
| 501 ChromeThread::UI, FROM_HERE, | 494 ChromeThread::UI, FROM_HERE, |
| 502 NewRunnableMethod(dlm, &DownloadManager::DownloadCancelled, id)); | 495 NewRunnableMethod(dlm, &DownloadManager::DownloadCancelled, id)); |
| 503 } else { | 496 } else { |
| 504 download->CancelDownloadRequest(resource_dispatcher_host_); | 497 download->CancelDownloadRequest(resource_dispatcher_host_); |
| 505 } | 498 } |
| 506 } | 499 } |
| OLD | NEW |