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" |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 download->id(), | 361 download->id(), |
362 state, | 362 state, |
363 download_prefs()->download_path())); | 363 download_prefs()->download_path())); |
364 } | 364 } |
365 | 365 |
366 void DownloadManager::CheckIfSuggestedPathExists(int32 download_id, | 366 void DownloadManager::CheckIfSuggestedPathExists(int32 download_id, |
367 DownloadStateInfo state, | 367 DownloadStateInfo state, |
368 const FilePath& default_path) { | 368 const FilePath& default_path) { |
369 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 369 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
370 | 370 |
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 | 371 // 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. | 372 // to the user's "Downloads" directory. We'll prompt them in this case. |
378 FilePath dir = state.suggested_path.DirName(); | 373 FilePath dir = state.suggested_path.DirName(); |
379 FilePath filename = state.suggested_path.BaseName(); | 374 FilePath filename = state.suggested_path.BaseName(); |
380 if (!file_util::PathIsWritable(dir)) { | 375 if (!file_util::PathIsWritable(dir)) { |
381 VLOG(1) << "Unable to write to directory \"" << dir.value() << "\""; | 376 VLOG(1) << "Unable to write to directory \"" << dir.value() << "\""; |
382 state.prompt_user_for_save_location = true; | 377 state.prompt_user_for_save_location = true; |
383 PathService::Get(chrome::DIR_USER_DOCUMENTS, &state.suggested_path); | 378 if (!PathService::Get( |
384 state.suggested_path = state.suggested_path.Append(filename); | 379 chrome::DIR_DEFAULT_DOWNLOADS, &state.suggested_path) || |
| 380 !file_util::PathIsWritable(state.suggested_path)) { |
| 381 VLOG(1) << "Cannot find the user's writable \"Downloads\" folder."; |
| 382 // If the user's writable "Downloads" folder does not exist, use the |
| 383 // originally suggested path even if the path does not exist |
| 384 // (This is a rare case). |
| 385 state.suggested_path = dir.Append(filename); |
| 386 } else { |
| 387 state.suggested_path = state.suggested_path.Append(filename); |
| 388 } |
| 389 // Make sure that the folder does exist. |
| 390 if (!file_util::CreateDirectory(state.suggested_path.DirName())) |
| 391 LOG(ERROR) << "Failed to create " << |
| 392 state.suggested_path.DirName().value(); |
385 } | 393 } |
386 | 394 |
387 // If the download is deemed dangerous, we'll use a temporary name for it. | 395 // If the download is deemed dangerous, we'll use a temporary name for it. |
388 if (state.IsDangerous()) { | 396 if (state.IsDangerous()) { |
389 state.target_name = FilePath(state.suggested_path).BaseName(); | 397 state.target_name = FilePath(state.suggested_path).BaseName(); |
390 // Create a temporary file to hold the file until the user approves its | 398 // Create a temporary file to hold the file until the user approves its |
391 // download. | 399 // download. |
392 FilePath::StringType file_name; | 400 FilePath::StringType file_name; |
393 FilePath path; | 401 FilePath path; |
394 #if defined(OS_WIN) | 402 #if defined(OS_WIN) |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 // FileSelectionCancelled(). | 496 // FileSelectionCancelled(). |
489 int32* id_ptr = new int32; | 497 int32* id_ptr = new int32; |
490 *id_ptr = download_id; | 498 *id_ptr = download_id; |
491 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, | 499 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, |
492 string16(), | 500 string16(), |
493 suggested_path, | 501 suggested_path, |
494 &file_type_info, 0, FILE_PATH_LITERAL(""), | 502 &file_type_info, 0, FILE_PATH_LITERAL(""), |
495 contents, owning_window, | 503 contents, owning_window, |
496 reinterpret_cast<void*>(id_ptr)); | 504 reinterpret_cast<void*>(id_ptr)); |
497 FOR_EACH_OBSERVER(Observer, observers_, | 505 FOR_EACH_OBSERVER(Observer, observers_, |
498 SelectFileDialogDisplayed(download_id)); | 506 SelectFileDialogDisplayed(download_id, suggested_path)); |
499 } else { | 507 } else { |
500 // No prompting for download, just continue with the suggested name. | 508 // No prompting for download, just continue with the suggested name. |
501 ContinueDownloadWithPath(download, suggested_path); | 509 ContinueDownloadWithPath(download, suggested_path); |
502 } | 510 } |
503 } | 511 } |
504 | 512 |
505 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { | 513 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { |
506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 514 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
507 | 515 |
508 DownloadItem* download = new DownloadItem(this, *info, | 516 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); | 1285 observed_download_manager_->RemoveObserver(this); |
1278 } | 1286 } |
1279 | 1287 |
1280 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { | 1288 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
1281 observing_download_manager_->NotifyModelChanged(); | 1289 observing_download_manager_->NotifyModelChanged(); |
1282 } | 1290 } |
1283 | 1291 |
1284 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { | 1292 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
1285 observed_download_manager_ = NULL; | 1293 observed_download_manager_ = NULL; |
1286 } | 1294 } |
OLD | NEW |