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" |
11 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
12 #include "base/stl_util-inl.h" | 13 #include "base/stl_util-inl.h" |
13 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
14 #include "base/sys_string_conversions.h" | 15 #include "base/sys_string_conversions.h" |
15 #include "base/task.h" | 16 #include "base/task.h" |
16 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
18 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/download/download_create_info.h" | 20 #include "chrome/browser/download/download_create_info.h" |
20 #include "chrome/browser/download/download_extensions.h" | 21 #include "chrome/browser/download/download_extensions.h" |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 BrowserThread::PostTask( | 403 BrowserThread::PostTask( |
403 BrowserThread::FILE, FROM_HERE, | 404 BrowserThread::FILE, FROM_HERE, |
404 NewRunnableMethod( | 405 NewRunnableMethod( |
405 this, | 406 this, |
406 &DownloadManager::CheckIfSuggestedPathExists, | 407 &DownloadManager::CheckIfSuggestedPathExists, |
407 download->id(), | 408 download->id(), |
408 state, | 409 state, |
409 download_prefs()->download_path())); | 410 download_prefs()->download_path())); |
410 } | 411 } |
411 | 412 |
412 void DownloadManager::CheckIfSuggestedPathExists( | 413 void DownloadManager::CheckIfSuggestedPathExists(int32 download_id, |
413 int32 download_id, | 414 DownloadStateInfo state, |
414 DownloadStateInfo state, | 415 const FilePath& default_path) { |
415 const FilePath& download_save_dir) { | |
416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
417 | 417 |
418 FilePath default_download_dir = | 418 // Make sure the default download directory exists. |
419 download_util::GetDefaultDownloadDirectoryFromPathService(); | 419 // TODO(phajdan.jr): only create the directory when we're sure the user |
420 FilePath save_dir; | 420 // is going to save there and not to another directory of his choice. |
421 if (download_util::ChooseSavableDirectory( | 421 file_util::CreateDirectory(default_path); |
422 FilePath(), download_save_dir, default_download_dir, &save_dir)) | 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() << "\""; |
423 state.prompt_user_for_save_location = true; | 429 state.prompt_user_for_save_location = true; |
424 state.suggested_path = save_dir.Append(state.suggested_path.BaseName()); | 430 PathService::Get(chrome::DIR_USER_DOCUMENTS, &state.suggested_path); |
| 431 state.suggested_path = state.suggested_path.Append(filename); |
| 432 } |
425 | 433 |
426 // If the download is deemed dangerous, we'll use a temporary name for it. | 434 // If the download is deemed dangerous, we'll use a temporary name for it. |
427 if (state.IsDangerous()) { | 435 if (state.IsDangerous()) { |
428 state.target_name = state.suggested_path.BaseName(); | 436 state.target_name = FilePath(state.suggested_path).BaseName(); |
429 // Create a temporary file to hold the file until the user approves its | 437 // Create a temporary file to hold the file until the user approves its |
430 // download. | 438 // download. |
431 FilePath::StringType file_name; | 439 FilePath::StringType file_name; |
432 FilePath path; | 440 FilePath path; |
433 #if defined(OS_WIN) | 441 #if defined(OS_WIN) |
434 string16 unconfirmed_prefix = | 442 string16 unconfirmed_prefix = |
435 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); | 443 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); |
436 #else | 444 #else |
437 std::string unconfirmed_prefix = | 445 std::string unconfirmed_prefix = |
438 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); | 446 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); |
439 #endif | 447 #endif |
440 | 448 |
441 while (path.empty()) { | 449 while (path.empty()) { |
442 base::SStringPrintf( | 450 base::SStringPrintf( |
443 &file_name, | 451 &file_name, |
444 unconfirmed_prefix.append( | 452 unconfirmed_prefix.append( |
445 FILE_PATH_LITERAL(" %d.crdownload")).c_str(), | 453 FILE_PATH_LITERAL(" %d.crdownload")).c_str(), |
446 base::RandInt(0, 100000)); | 454 base::RandInt(0, 100000)); |
447 path = state.suggested_path.DirName().Append(file_name); | 455 path = dir.Append(file_name); |
448 if (file_util::PathExists(path)) | 456 if (file_util::PathExists(path)) |
449 path = FilePath(); | 457 path = FilePath(); |
450 } | 458 } |
451 state.suggested_path = path; | 459 state.suggested_path = path; |
452 } else { | 460 } else { |
453 // Do not add the path uniquifier if we are saving to a specific path as in | 461 // Do not add the path uniquifier if we are saving to a specific path as in |
454 // the drag-out case. | 462 // the drag-out case. |
455 if (state.force_file_name.empty()) { | 463 if (state.force_file_name.empty()) { |
456 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( | 464 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( |
457 state.suggested_path); | 465 state.suggested_path); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 // FileSelectionCancelled(). | 535 // FileSelectionCancelled(). |
528 int32* id_ptr = new int32; | 536 int32* id_ptr = new int32; |
529 *id_ptr = download_id; | 537 *id_ptr = download_id; |
530 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, | 538 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, |
531 string16(), | 539 string16(), |
532 suggested_path, | 540 suggested_path, |
533 &file_type_info, 0, FILE_PATH_LITERAL(""), | 541 &file_type_info, 0, FILE_PATH_LITERAL(""), |
534 contents, owning_window, | 542 contents, owning_window, |
535 reinterpret_cast<void*>(id_ptr)); | 543 reinterpret_cast<void*>(id_ptr)); |
536 FOR_EACH_OBSERVER(Observer, observers_, | 544 FOR_EACH_OBSERVER(Observer, observers_, |
537 SelectFileDialogDisplayed(download_id, suggested_path)); | 545 SelectFileDialogDisplayed(download_id)); |
538 } else { | 546 } else { |
539 // No prompting for download, just continue with the suggested name. | 547 // No prompting for download, just continue with the suggested name. |
540 ContinueDownloadWithPath(download, suggested_path); | 548 ContinueDownloadWithPath(download, suggested_path); |
541 } | 549 } |
542 } | 550 } |
543 | 551 |
544 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { | 552 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { |
545 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 553 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
546 | 554 |
547 DownloadItem* download = new DownloadItem(this, *info, | 555 DownloadItem* download = new DownloadItem(this, *info, |
(...skipping 796 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1344 observed_download_manager_->RemoveObserver(this); | 1352 observed_download_manager_->RemoveObserver(this); |
1345 } | 1353 } |
1346 | 1354 |
1347 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { | 1355 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
1348 observing_download_manager_->NotifyModelChanged(); | 1356 observing_download_manager_->NotifyModelChanged(); |
1349 } | 1357 } |
1350 | 1358 |
1351 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { | 1359 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
1352 observed_download_manager_ = NULL; | 1360 observed_download_manager_ = NULL; |
1353 } | 1361 } |
OLD | NEW |