| 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_manager.h" | 5 #include "chrome/browser/download/download_manager.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/path_service.h" | |
| 12 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
| 13 #include "base/stl_util-inl.h" | 12 #include "base/stl_util-inl.h" |
| 14 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 15 #include "base/sys_string_conversions.h" | 14 #include "base/sys_string_conversions.h" |
| 16 #include "base/task.h" | 15 #include "base/task.h" |
| 17 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 18 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 19 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/download/download_create_info.h" | 19 #include "chrome/browser/download/download_create_info.h" |
| 21 #include "chrome/browser/download/download_extensions.h" | 20 #include "chrome/browser/download/download_extensions.h" |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 // the suggested path on the UI thread. | 399 // the suggested path on the UI thread. |
| 401 // We can only access preferences on the UI thread, so check the download path | 400 // We can only access preferences on the UI thread, so check the download path |
| 402 // now and pass the value to the FILE thread. | 401 // now and pass the value to the FILE thread. |
| 403 BrowserThread::PostTask( | 402 BrowserThread::PostTask( |
| 404 BrowserThread::FILE, FROM_HERE, | 403 BrowserThread::FILE, FROM_HERE, |
| 405 NewRunnableMethod( | 404 NewRunnableMethod( |
| 406 this, | 405 this, |
| 407 &DownloadManager::CheckIfSuggestedPathExists, | 406 &DownloadManager::CheckIfSuggestedPathExists, |
| 408 download->id(), | 407 download->id(), |
| 409 state, | 408 state, |
| 410 download_prefs()->download_path())); | 409 download_prefs()->download_path(), |
| 410 download_prefs()->GetDefaultDownloadDirectory())); |
| 411 } | 411 } |
| 412 | 412 |
| 413 void DownloadManager::CheckIfSuggestedPathExists(int32 download_id, | 413 void DownloadManager::CheckIfSuggestedPathExists( |
| 414 DownloadStateInfo state, | 414 int32 download_id, |
| 415 const FilePath& default_path) { | 415 DownloadStateInfo state, |
| 416 const FilePath& download_save_dir, |
| 417 const FilePath& default_download_dir) { |
| 416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 418 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 417 | 419 |
| 418 // Make sure the default download directory exists. | 420 FilePath save_dir; |
| 419 // TODO(phajdan.jr): only create the directory when we're sure the user | 421 if (download_util::ChooseSavableDirectory( |
| 420 // is going to save there and not to another directory of his choice. | 422 FilePath(), download_save_dir, default_download_dir, &save_dir)) |
| 421 file_util::CreateDirectory(default_path); | |
| 422 | |
| 423 // Check writability of the suggested path. If we can't write to it, default | |
| 424 // to the user's "My Documents" directory. We'll prompt them in this case. | |
| 425 FilePath dir = state.suggested_path.DirName(); | |
| 426 FilePath filename = state.suggested_path.BaseName(); | |
| 427 if (!file_util::PathIsWritable(dir)) { | |
| 428 VLOG(1) << "Unable to write to directory \"" << dir.value() << "\""; | |
| 429 state.prompt_user_for_save_location = true; | 423 state.prompt_user_for_save_location = true; |
| 430 PathService::Get(chrome::DIR_USER_DOCUMENTS, &state.suggested_path); | 424 state.suggested_path = save_dir.Append(state.suggested_path.BaseName()); |
| 431 state.suggested_path = state.suggested_path.Append(filename); | |
| 432 } | |
| 433 | 425 |
| 434 // If the download is deemed dangerous, we'll use a temporary name for it. | 426 // If the download is deemed dangerous, we'll use a temporary name for it. |
| 435 if (state.IsDangerous()) { | 427 if (state.IsDangerous()) { |
| 436 state.target_name = FilePath(state.suggested_path).BaseName(); | 428 state.target_name = state.suggested_path.BaseName(); |
| 437 // Create a temporary file to hold the file until the user approves its | 429 // Create a temporary file to hold the file until the user approves its |
| 438 // download. | 430 // download. |
| 439 FilePath::StringType file_name; | 431 FilePath::StringType file_name; |
| 440 FilePath path; | 432 FilePath path; |
| 441 #if defined(OS_WIN) | 433 #if defined(OS_WIN) |
| 442 string16 unconfirmed_prefix = | 434 string16 unconfirmed_prefix = |
| 443 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); | 435 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); |
| 444 #else | 436 #else |
| 445 std::string unconfirmed_prefix = | 437 std::string unconfirmed_prefix = |
| 446 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); | 438 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); |
| 447 #endif | 439 #endif |
| 448 | 440 |
| 449 while (path.empty()) { | 441 while (path.empty()) { |
| 450 base::SStringPrintf( | 442 base::SStringPrintf( |
| 451 &file_name, | 443 &file_name, |
| 452 unconfirmed_prefix.append( | 444 unconfirmed_prefix.append( |
| 453 FILE_PATH_LITERAL(" %d.crdownload")).c_str(), | 445 FILE_PATH_LITERAL(" %d.crdownload")).c_str(), |
| 454 base::RandInt(0, 100000)); | 446 base::RandInt(0, 100000)); |
| 455 path = dir.Append(file_name); | 447 path = state.suggested_path.DirName().Append(file_name); |
| 456 if (file_util::PathExists(path)) | 448 if (file_util::PathExists(path)) |
| 457 path = FilePath(); | 449 path = FilePath(); |
| 458 } | 450 } |
| 459 state.suggested_path = path; | 451 state.suggested_path = path; |
| 460 } else { | 452 } else { |
| 461 // Do not add the path uniquifier if we are saving to a specific path as in | 453 // Do not add the path uniquifier if we are saving to a specific path as in |
| 462 // the drag-out case. | 454 // the drag-out case. |
| 463 if (state.force_file_name.empty()) { | 455 if (state.force_file_name.empty()) { |
| 464 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( | 456 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( |
| 465 state.suggested_path); | 457 state.suggested_path); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 // FileSelectionCancelled(). | 527 // FileSelectionCancelled(). |
| 536 int32* id_ptr = new int32; | 528 int32* id_ptr = new int32; |
| 537 *id_ptr = download_id; | 529 *id_ptr = download_id; |
| 538 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, | 530 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, |
| 539 string16(), | 531 string16(), |
| 540 suggested_path, | 532 suggested_path, |
| 541 &file_type_info, 0, FILE_PATH_LITERAL(""), | 533 &file_type_info, 0, FILE_PATH_LITERAL(""), |
| 542 contents, owning_window, | 534 contents, owning_window, |
| 543 reinterpret_cast<void*>(id_ptr)); | 535 reinterpret_cast<void*>(id_ptr)); |
| 544 FOR_EACH_OBSERVER(Observer, observers_, | 536 FOR_EACH_OBSERVER(Observer, observers_, |
| 545 SelectFileDialogDisplayed(download_id)); | 537 SelectFileDialogDisplayed(download_id, suggested_path)); |
| 546 } else { | 538 } else { |
| 547 // No prompting for download, just continue with the suggested name. | 539 // No prompting for download, just continue with the suggested name. |
| 548 ContinueDownloadWithPath(download, suggested_path); | 540 ContinueDownloadWithPath(download, suggested_path); |
| 549 } | 541 } |
| 550 } | 542 } |
| 551 | 543 |
| 552 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { | 544 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { |
| 553 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 545 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 554 | 546 |
| 555 DownloadItem* download = new DownloadItem(this, *info, | 547 DownloadItem* download = new DownloadItem(this, *info, |
| (...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1334 observed_download_manager_->RemoveObserver(this); | 1326 observed_download_manager_->RemoveObserver(this); |
| 1335 } | 1327 } |
| 1336 | 1328 |
| 1337 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { | 1329 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
| 1338 observing_download_manager_->NotifyModelChanged(); | 1330 observing_download_manager_->NotifyModelChanged(); |
| 1339 } | 1331 } |
| 1340 | 1332 |
| 1341 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { | 1333 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
| 1342 observed_download_manager_ = NULL; | 1334 observed_download_manager_ = NULL; |
| 1343 } | 1335 } |
| OLD | NEW |