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

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: Add ChooseSavableDirectory() and ScopedDefaultDownloadDirectory Created 9 years, 6 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-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 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 BrowserThread::PostTask( 355 BrowserThread::PostTask(
357 BrowserThread::FILE, FROM_HERE, 356 BrowserThread::FILE, FROM_HERE,
358 NewRunnableMethod( 357 NewRunnableMethod(
359 this, 358 this,
360 &DownloadManager::CheckIfSuggestedPathExists, 359 &DownloadManager::CheckIfSuggestedPathExists,
361 download->id(), 360 download->id(),
362 state, 361 state,
363 download_prefs()->download_path())); 362 download_prefs()->download_path()));
364 } 363 }
365 364
366 void DownloadManager::CheckIfSuggestedPathExists(int32 download_id, 365 void DownloadManager::CheckIfSuggestedPathExists(
367 DownloadStateInfo state, 366 int32 download_id,
368 const FilePath& default_path) { 367 DownloadStateInfo state,
368 const FilePath& download_save_dir) {
369 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 369 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
370 370
371 // Make sure the default download directory exists. 371 FilePath default_downloads_dir =
372 // TODO(phajdan.jr): only create the directory when we're sure the user 372 download_util::DefaultDownloadDirectory::Get();
373 // is going to save there and not to another directory of his choice.
374 file_util::CreateDirectory(default_path);
375 373
376 // Check writability of the suggested path. If we can't write to it, default 374 FilePath save_dir;
377 // to the user's "My Documents" directory. We'll prompt them in this case. 375 state.prompt_user_for_save_location =
378 FilePath dir = state.suggested_path.DirName(); 376 download_util::ChooseSavableDirectory(
379 FilePath filename = state.suggested_path.BaseName(); 377 FilePath(), download_save_dir, default_downloads_dir, &save_dir);
haraken1 2011/06/14 11:10:05 Note: This was wrong... If |state.prompt_user_for_
380 if (!file_util::PathIsWritable(dir)) { 378 state.suggested_path = save_dir.Append(state.suggested_path.BaseName());
381 VLOG(1) << "Unable to write to directory \"" << dir.value() << "\"";
382 state.prompt_user_for_save_location = true;
383 PathService::Get(chrome::DIR_USER_DOCUMENTS, &state.suggested_path);
384 state.suggested_path = state.suggested_path.Append(filename);
385 }
386 379
387 // If the download is deemed dangerous, we'll use a temporary name for it. 380 // If the download is deemed dangerous, we'll use a temporary name for it.
388 if (state.IsDangerous()) { 381 if (state.IsDangerous()) {
389 state.target_name = FilePath(state.suggested_path).BaseName(); 382 state.target_name = state.suggested_path.BaseName();
390 // Create a temporary file to hold the file until the user approves its 383 // Create a temporary file to hold the file until the user approves its
391 // download. 384 // download.
392 FilePath::StringType file_name; 385 FilePath::StringType file_name;
393 FilePath path; 386 FilePath path;
394 #if defined(OS_WIN) 387 #if defined(OS_WIN)
395 string16 unconfirmed_prefix = 388 string16 unconfirmed_prefix =
396 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); 389 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX);
397 #else 390 #else
398 std::string unconfirmed_prefix = 391 std::string unconfirmed_prefix =
399 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); 392 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX);
400 #endif 393 #endif
401 394
402 while (path.empty()) { 395 while (path.empty()) {
403 base::SStringPrintf( 396 base::SStringPrintf(
404 &file_name, 397 &file_name,
405 unconfirmed_prefix.append( 398 unconfirmed_prefix.append(
406 FILE_PATH_LITERAL(" %d.crdownload")).c_str(), 399 FILE_PATH_LITERAL(" %d.crdownload")).c_str(),
407 base::RandInt(0, 100000)); 400 base::RandInt(0, 100000));
408 path = dir.Append(file_name); 401 path = state.suggested_path.DirName().Append(file_name);
409 if (file_util::PathExists(path)) 402 if (file_util::PathExists(path))
410 path = FilePath(); 403 path = FilePath();
411 } 404 }
412 state.suggested_path = path; 405 state.suggested_path = path;
413 } else { 406 } else {
414 // Do not add the path uniquifier if we are saving to a specific path as in 407 // Do not add the path uniquifier if we are saving to a specific path as in
415 // the drag-out case. 408 // the drag-out case.
416 if (state.force_file_name.empty()) { 409 if (state.force_file_name.empty()) {
417 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( 410 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload(
418 state.suggested_path); 411 state.suggested_path);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 // FileSelectionCancelled(). 481 // FileSelectionCancelled().
489 int32* id_ptr = new int32; 482 int32* id_ptr = new int32;
490 *id_ptr = download_id; 483 *id_ptr = download_id;
491 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, 484 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE,
492 string16(), 485 string16(),
493 suggested_path, 486 suggested_path,
494 &file_type_info, 0, FILE_PATH_LITERAL(""), 487 &file_type_info, 0, FILE_PATH_LITERAL(""),
495 contents, owning_window, 488 contents, owning_window,
496 reinterpret_cast<void*>(id_ptr)); 489 reinterpret_cast<void*>(id_ptr));
497 FOR_EACH_OBSERVER(Observer, observers_, 490 FOR_EACH_OBSERVER(Observer, observers_,
498 SelectFileDialogDisplayed(download_id)); 491 SelectFileDialogDisplayed(download_id, suggested_path));
499 } else { 492 } else {
500 // No prompting for download, just continue with the suggested name. 493 // No prompting for download, just continue with the suggested name.
501 ContinueDownloadWithPath(download, suggested_path); 494 ContinueDownloadWithPath(download, suggested_path);
502 } 495 }
503 } 496 }
504 497
505 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { 498 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) {
506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 499 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
507 500
508 DownloadItem* download = new DownloadItem(this, *info, 501 DownloadItem* download = new DownloadItem(this, *info,
(...skipping 768 matching lines...) Expand 10 before | Expand all | Expand 10 after
1277 observed_download_manager_->RemoveObserver(this); 1270 observed_download_manager_->RemoveObserver(this);
1278 } 1271 }
1279 1272
1280 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { 1273 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() {
1281 observing_download_manager_->NotifyModelChanged(); 1274 observing_download_manager_->NotifyModelChanged();
1282 } 1275 }
1283 1276
1284 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { 1277 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() {
1285 observed_download_manager_ = NULL; 1278 observed_download_manager_ = NULL;
1286 } 1279 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698