Chromium Code Reviews| 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 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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(int32 download_id, |
| 367 DownloadStateInfo state, | 366 DownloadStateInfo state, |
| 368 const FilePath& default_path) { | 367 const FilePath& default_path) { |
| 369 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 368 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 370 | 369 |
| 371 // Make sure the default download directory exists. | |
| 372 // TODO(phajdan.jr): only create the directory when we're sure the user | |
| 373 // is going to save there and not to another directory of his choice. | |
| 374 file_util::CreateDirectory(default_path); | |
| 375 | |
| 376 // Check writability of the suggested path. If we can't write to it, default | 370 // Check writability of the suggested path. If we can't write to it, default |
| 377 // to the user's "My Documents" directory. We'll prompt them in this case. | 371 // to the user's "Downloads" directory. We'll prompt them in this case. |
| 378 FilePath dir = state.suggested_path.DirName(); | 372 FilePath dir = state.suggested_path.DirName(); |
| 379 FilePath filename = state.suggested_path.BaseName(); | 373 FilePath filename = state.suggested_path.BaseName(); |
| 380 if (!file_util::PathIsWritable(dir)) { | 374 if (!file_util::PathIsWritable(dir)) { |
| 381 VLOG(1) << "Unable to write to directory \"" << dir.value() << "\""; | 375 VLOG(1) << "Unable to write to directory \"" << dir.value() << "\""; |
| 382 state.prompt_user_for_save_location = true; | 376 state.prompt_user_for_save_location = true; |
| 383 PathService::Get(chrome::DIR_USER_DOCUMENTS, &state.suggested_path); | 377 if (!download_util::DefaultDownloadDirectory::Get(&state.suggested_path) || |
| 384 state.suggested_path = state.suggested_path.Append(filename); | 378 !file_util::PathIsWritable(state.suggested_path)) { |
| 379 VLOG(1) << "Cannot find the user's writable \"Downloads\" folder."; | |
| 380 // If the user's writable "Downloads" folder does not exist, use the | |
| 381 // originally suggested path even if the path does not exist | |
| 382 // (This is a rare case). | |
| 383 state.suggested_path = dir.Append(filename); | |
| 384 } else { | |
| 385 state.suggested_path = state.suggested_path.Append(filename); | |
| 386 } | |
| 387 // Make sure that the folder does exist. | |
| 388 if (!file_util::CreateDirectory(state.suggested_path.DirName())) | |
|
Paweł Hajdan Jr.
2011/06/08 09:50:23
nit: Please use braces {} for multi-line "if" body
haraken1
2011/06/09 10:16:56
Done.
| |
| 389 LOG(ERROR) << "Failed to create " << | |
|
Paweł Hajdan Jr.
2011/06/08 09:50:23
Logging the error is fine, but shouldn't we exit a
Randy Smith (Not in Mondays)
2011/06/08 22:31:18
I'm inclined to think that the right behavior is t
haraken1
2011/06/09 10:16:56
I would like to agree with Randy's idea, also cons
| |
| 390 state.suggested_path.DirName().value(); | |
| 385 } | 391 } |
| 386 | 392 |
| 387 // If the download is deemed dangerous, we'll use a temporary name for it. | 393 // If the download is deemed dangerous, we'll use a temporary name for it. |
| 388 if (state.IsDangerous()) { | 394 if (state.IsDangerous()) { |
| 389 state.target_name = FilePath(state.suggested_path).BaseName(); | 395 state.target_name = FilePath(state.suggested_path).BaseName(); |
| 390 // Create a temporary file to hold the file until the user approves its | 396 // Create a temporary file to hold the file until the user approves its |
| 391 // download. | 397 // download. |
| 392 FilePath::StringType file_name; | 398 FilePath::StringType file_name; |
| 393 FilePath path; | 399 FilePath path; |
| 394 #if defined(OS_WIN) | 400 #if defined(OS_WIN) |
| 395 string16 unconfirmed_prefix = | 401 string16 unconfirmed_prefix = |
| 396 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); | 402 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); |
| 397 #else | 403 #else |
| 398 std::string unconfirmed_prefix = | 404 std::string unconfirmed_prefix = |
| 399 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); | 405 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); |
| 400 #endif | 406 #endif |
| 401 | 407 |
| 402 while (path.empty()) { | 408 while (path.empty()) { |
| 403 base::SStringPrintf( | 409 base::SStringPrintf( |
| 404 &file_name, | 410 &file_name, |
| 405 unconfirmed_prefix.append( | 411 unconfirmed_prefix.append( |
| 406 FILE_PATH_LITERAL(" %d.crdownload")).c_str(), | 412 FILE_PATH_LITERAL(" %d.crdownload")).c_str(), |
| 407 base::RandInt(0, 100000)); | 413 base::RandInt(0, 100000)); |
| 408 path = dir.Append(file_name); | 414 path = dir.Append(file_name); |
|
haraken1
2011/06/09 10:16:56
I changed this dir.Append(file_name) to state.sugg
| |
| 409 if (file_util::PathExists(path)) | 415 if (file_util::PathExists(path)) |
| 410 path = FilePath(); | 416 path = FilePath(); |
| 411 } | 417 } |
| 412 state.suggested_path = path; | 418 state.suggested_path = path; |
| 413 } else { | 419 } else { |
| 414 // Do not add the path uniquifier if we are saving to a specific path as in | 420 // Do not add the path uniquifier if we are saving to a specific path as in |
| 415 // the drag-out case. | 421 // the drag-out case. |
| 416 if (state.force_file_name.empty()) { | 422 if (state.force_file_name.empty()) { |
| 417 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( | 423 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( |
| 418 state.suggested_path); | 424 state.suggested_path); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 488 // FileSelectionCancelled(). | 494 // FileSelectionCancelled(). |
| 489 int32* id_ptr = new int32; | 495 int32* id_ptr = new int32; |
| 490 *id_ptr = download_id; | 496 *id_ptr = download_id; |
| 491 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, | 497 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, |
| 492 string16(), | 498 string16(), |
| 493 suggested_path, | 499 suggested_path, |
| 494 &file_type_info, 0, FILE_PATH_LITERAL(""), | 500 &file_type_info, 0, FILE_PATH_LITERAL(""), |
| 495 contents, owning_window, | 501 contents, owning_window, |
| 496 reinterpret_cast<void*>(id_ptr)); | 502 reinterpret_cast<void*>(id_ptr)); |
| 497 FOR_EACH_OBSERVER(Observer, observers_, | 503 FOR_EACH_OBSERVER(Observer, observers_, |
| 498 SelectFileDialogDisplayed(download_id)); | 504 SelectFileDialogDisplayed(download_id, suggested_path)); |
| 499 } else { | 505 } else { |
| 500 // No prompting for download, just continue with the suggested name. | 506 // No prompting for download, just continue with the suggested name. |
| 501 ContinueDownloadWithPath(download, suggested_path); | 507 ContinueDownloadWithPath(download, suggested_path); |
| 502 } | 508 } |
| 503 } | 509 } |
| 504 | 510 |
| 505 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { | 511 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { |
| 506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 512 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 507 | 513 |
| 508 DownloadItem* download = new DownloadItem(this, *info, | 514 DownloadItem* download = new DownloadItem(this, *info, |
| (...skipping 768 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1277 observed_download_manager_->RemoveObserver(this); | 1283 observed_download_manager_->RemoveObserver(this); |
| 1278 } | 1284 } |
| 1279 | 1285 |
| 1280 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { | 1286 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
| 1281 observing_download_manager_->NotifyModelChanged(); | 1287 observing_download_manager_->NotifyModelChanged(); |
| 1282 } | 1288 } |
| 1283 | 1289 |
| 1284 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { | 1290 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
| 1285 observed_download_manager_ = NULL; | 1291 observed_download_manager_ = NULL; |
| 1286 } | 1292 } |
| OLD | NEW |