Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: chrome/browser/download/download_manager.cc

Issue 6973052: When the download folder does not exist, change the download folder to a user's "Downloads" (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Added URLRequestMockHTTPJob.test_dir_ and URLRequestMockHTTPJob.temp_dir_ Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.h" 12 #include "base/stl_util.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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 BrowserThread::PostTask( 396 BrowserThread::PostTask(
398 BrowserThread::FILE, FROM_HERE, 397 BrowserThread::FILE, FROM_HERE,
399 NewRunnableMethod( 398 NewRunnableMethod(
400 this, 399 this,
401 &DownloadManager::CheckIfSuggestedPathExists, 400 &DownloadManager::CheckIfSuggestedPathExists,
402 download->id(), 401 download->id(),
403 state, 402 state,
404 download_prefs()->download_path())); 403 download_prefs()->download_path()));
405 } 404 }
406 405
407 void DownloadManager::CheckIfSuggestedPathExists(int32 download_id, 406 void DownloadManager::CheckIfSuggestedPathExists(
408 DownloadStateInfo state, 407 int32 download_id,
409 const FilePath& default_path) { 408 DownloadStateInfo state,
409 const FilePath& download_save_dir) {
410 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 410 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
411 411
412 // Make sure the default download directory exists. 412 FilePath default_download_dir =
413 // TODO(phajdan.jr): only create the directory when we're sure the user 413 download_util::GetDefaultDownloadDirectoryFromPathService();
414 // is going to save there and not to another directory of his choice. 414 FilePath save_dir;
415 file_util::CreateDirectory(default_path); 415 if (download_util::ChooseSavableDirectory(
416 416 FilePath(), download_save_dir, default_download_dir, &save_dir))
417 // Check writability of the suggested path. If we can't write to it, default
418 // to the user's "My Documents" directory. We'll prompt them in this case.
419 FilePath dir = state.suggested_path.DirName();
420 FilePath filename = state.suggested_path.BaseName();
421 if (!file_util::PathIsWritable(dir)) {
422 VLOG(1) << "Unable to write to directory \"" << dir.value() << "\"";
423 state.prompt_user_for_save_location = true; 417 state.prompt_user_for_save_location = true;
424 PathService::Get(chrome::DIR_USER_DOCUMENTS, &state.suggested_path); 418 state.suggested_path = save_dir.Append(state.suggested_path.BaseName());
425 state.suggested_path = state.suggested_path.Append(filename);
426 }
427 419
428 // If the download is deemed dangerous, we'll use a temporary name for it. 420 // If the download is deemed dangerous, we'll use a temporary name for it.
429 if (state.IsDangerous()) { 421 if (state.IsDangerous()) {
430 state.target_name = FilePath(state.suggested_path).BaseName(); 422 state.target_name = state.suggested_path.BaseName();
431 // Create a temporary file to hold the file until the user approves its 423 // Create a temporary file to hold the file until the user approves its
432 // download. 424 // download.
433 FilePath::StringType file_name; 425 FilePath::StringType file_name;
434 FilePath path; 426 FilePath path;
435 #if defined(OS_WIN) 427 #if defined(OS_WIN)
436 string16 unconfirmed_prefix = 428 string16 unconfirmed_prefix =
437 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); 429 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX);
438 #else 430 #else
439 std::string unconfirmed_prefix = 431 std::string unconfirmed_prefix =
440 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); 432 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX);
441 #endif 433 #endif
442 434
443 while (path.empty()) { 435 while (path.empty()) {
444 base::SStringPrintf( 436 base::SStringPrintf(
445 &file_name, 437 &file_name,
446 unconfirmed_prefix.append( 438 unconfirmed_prefix.append(
447 FILE_PATH_LITERAL(" %d.crdownload")).c_str(), 439 FILE_PATH_LITERAL(" %d.crdownload")).c_str(),
448 base::RandInt(0, 100000)); 440 base::RandInt(0, 100000));
449 path = dir.Append(file_name); 441 path = state.suggested_path.DirName().Append(file_name);
450 if (file_util::PathExists(path)) 442 if (file_util::PathExists(path))
451 path = FilePath(); 443 path = FilePath();
452 } 444 }
453 state.suggested_path = path; 445 state.suggested_path = path;
454 } else { 446 } else {
455 // Do not add the path uniquifier if we are saving to a specific path as in 447 // Do not add the path uniquifier if we are saving to a specific path as in
456 // the drag-out case. 448 // the drag-out case.
457 if (state.force_file_name.empty()) { 449 if (state.force_file_name.empty()) {
458 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( 450 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload(
459 state.suggested_path); 451 state.suggested_path);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 507
516 // |id_ptr| will be deleted in either FileSelected() or 508 // |id_ptr| will be deleted in either FileSelected() or
517 // FileSelectionCancelled(). 509 // FileSelectionCancelled().
518 int32* id_ptr = new int32; 510 int32* id_ptr = new int32;
519 *id_ptr = download_id; 511 *id_ptr = download_id;
520 512
521 content::GetContentClient()->browser()->ChooseDownloadPath( 513 content::GetContentClient()->browser()->ChooseDownloadPath(
522 this, contents, suggested_path, reinterpret_cast<void*>(id_ptr)); 514 this, contents, suggested_path, reinterpret_cast<void*>(id_ptr));
523 515
524 FOR_EACH_OBSERVER(Observer, observers_, 516 FOR_EACH_OBSERVER(Observer, observers_,
525 SelectFileDialogDisplayed(download_id)); 517 SelectFileDialogDisplayed(download_id, suggested_path));
526 } else { 518 } else {
527 // No prompting for download, just continue with the suggested name. 519 // No prompting for download, just continue with the suggested name.
528 ContinueDownloadWithPath(download, suggested_path); 520 ContinueDownloadWithPath(download, suggested_path);
529 } 521 }
530 } 522 }
531 523
532 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { 524 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) {
533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 525 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
534 526
535 DownloadItem* download = new DownloadItem(this, *info, 527 DownloadItem* download = new DownloadItem(this, *info,
(...skipping 782 matching lines...) Expand 10 before | Expand all | Expand 10 after
1318 observed_download_manager_->RemoveObserver(this); 1310 observed_download_manager_->RemoveObserver(this);
1319 } 1311 }
1320 1312
1321 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { 1313 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() {
1322 observing_download_manager_->NotifyModelChanged(); 1314 observing_download_manager_->NotifyModelChanged();
1323 } 1315 }
1324 1316
1325 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { 1317 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() {
1326 observed_download_manager_ = NULL; 1318 observed_download_manager_ = NULL;
1327 } 1319 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698