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

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: Redirect "http://mock.testfile.http/<random path>" to "chrome/test/data/a.htm" 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 378 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 BrowserThread::PostTask( 399 BrowserThread::PostTask(
401 BrowserThread::FILE, FROM_HERE, 400 BrowserThread::FILE, FROM_HERE,
402 NewRunnableMethod( 401 NewRunnableMethod(
403 this, 402 this,
404 &DownloadManager::CheckIfSuggestedPathExists, 403 &DownloadManager::CheckIfSuggestedPathExists,
405 download->id(), 404 download->id(),
406 state, 405 state,
407 download_prefs()->download_path())); 406 download_prefs()->download_path()));
408 } 407 }
409 408
410 void DownloadManager::CheckIfSuggestedPathExists(int32 download_id, 409 void DownloadManager::CheckIfSuggestedPathExists(
411 DownloadStateInfo state, 410 int32 download_id,
412 const FilePath& default_path) { 411 DownloadStateInfo state,
412 const FilePath& download_save_dir) {
413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
414 414
415 // Make sure the default download directory exists. 415 FilePath default_download_dir =
416 // TODO(phajdan.jr): only create the directory when we're sure the user 416 download_util::GetDefaultDownloadDirectoryFromPathService();
417 // is going to save there and not to another directory of his choice. 417 FilePath save_dir;
418 file_util::CreateDirectory(default_path); 418 if (download_util::ChooseSavableDirectory(
419 419 FilePath(), download_save_dir, default_download_dir, &save_dir))
420 // Check writability of the suggested path. If we can't write to it, default
421 // to the user's "My Documents" directory. We'll prompt them in this case.
422 FilePath dir = state.suggested_path.DirName();
423 FilePath filename = state.suggested_path.BaseName();
424 if (!file_util::PathIsWritable(dir)) {
425 VLOG(1) << "Unable to write to directory \"" << dir.value() << "\"";
426 state.prompt_user_for_save_location = true; 420 state.prompt_user_for_save_location = true;
427 PathService::Get(chrome::DIR_USER_DOCUMENTS, &state.suggested_path); 421 state.suggested_path = save_dir.Append(state.suggested_path.BaseName());
428 state.suggested_path = state.suggested_path.Append(filename);
429 }
430 422
431 // If the download is deemed dangerous, we'll use a temporary name for it. 423 // If the download is deemed dangerous, we'll use a temporary name for it.
432 if (state.IsDangerous()) { 424 if (state.IsDangerous()) {
433 state.target_name = FilePath(state.suggested_path).BaseName(); 425 state.target_name = state.suggested_path.BaseName();
434 // Create a temporary file to hold the file until the user approves its 426 // Create a temporary file to hold the file until the user approves its
435 // download. 427 // download.
436 FilePath::StringType file_name; 428 FilePath::StringType file_name;
437 FilePath path; 429 FilePath path;
438 #if defined(OS_WIN) 430 #if defined(OS_WIN)
439 string16 unconfirmed_prefix = 431 string16 unconfirmed_prefix =
440 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); 432 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX);
441 #else 433 #else
442 std::string unconfirmed_prefix = 434 std::string unconfirmed_prefix =
443 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); 435 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX);
444 #endif 436 #endif
445 437
446 while (path.empty()) { 438 while (path.empty()) {
447 base::SStringPrintf( 439 base::SStringPrintf(
448 &file_name, 440 &file_name,
449 unconfirmed_prefix.append( 441 unconfirmed_prefix.append(
450 FILE_PATH_LITERAL(" %d.crdownload")).c_str(), 442 FILE_PATH_LITERAL(" %d.crdownload")).c_str(),
451 base::RandInt(0, 100000)); 443 base::RandInt(0, 100000));
452 path = dir.Append(file_name); 444 path = state.suggested_path.DirName().Append(file_name);
453 if (file_util::PathExists(path)) 445 if (file_util::PathExists(path))
454 path = FilePath(); 446 path = FilePath();
455 } 447 }
456 state.suggested_path = path; 448 state.suggested_path = path;
457 } else { 449 } else {
458 // Do not add the path uniquifier if we are saving to a specific path as in 450 // Do not add the path uniquifier if we are saving to a specific path as in
459 // the drag-out case. 451 // the drag-out case.
460 if (state.force_file_name.empty()) { 452 if (state.force_file_name.empty()) {
461 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( 453 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload(
462 state.suggested_path); 454 state.suggested_path);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 510
519 // |id_ptr| will be deleted in either FileSelected() or 511 // |id_ptr| will be deleted in either FileSelected() or
520 // FileSelectionCancelled(). 512 // FileSelectionCancelled().
521 int32* id_ptr = new int32; 513 int32* id_ptr = new int32;
522 *id_ptr = download_id; 514 *id_ptr = download_id;
523 515
524 delegate_->ChooseDownloadPath( 516 delegate_->ChooseDownloadPath(
525 this, contents, suggested_path, reinterpret_cast<void*>(id_ptr)); 517 this, contents, suggested_path, reinterpret_cast<void*>(id_ptr));
526 518
527 FOR_EACH_OBSERVER(Observer, observers_, 519 FOR_EACH_OBSERVER(Observer, observers_,
528 SelectFileDialogDisplayed(download_id)); 520 SelectFileDialogDisplayed(download_id, suggested_path));
529 } else { 521 } else {
530 // No prompting for download, just continue with the suggested name. 522 // No prompting for download, just continue with the suggested name.
531 ContinueDownloadWithPath(download, suggested_path); 523 ContinueDownloadWithPath(download, suggested_path);
532 } 524 }
533 } 525 }
534 526
535 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { 527 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) {
536 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 528 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
537 529
538 DownloadItem* download = new DownloadItem(this, *info, 530 DownloadItem* download = new DownloadItem(this, *info,
(...skipping 827 matching lines...) Expand 10 before | Expand all | Expand 10 after
1366 Source<DownloadManager>(this), 1358 Source<DownloadManager>(this),
1367 Details<DownloadItem>(download)); 1359 Details<DownloadItem>(download));
1368 } 1360 }
1369 } 1361 }
1370 1362
1371 int32 DownloadManager::GetNextSavePageId() { 1363 int32 DownloadManager::GetNextSavePageId() {
1372 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1364 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1373 return next_save_page_id_++; 1365 return next_save_page_id_++;
1374 } 1366 }
1375 1367
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698