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)) { |
Randy Smith (Not in Mondays)
2011/06/02 19:13:57
Are we testing this case?
| |
380 LOG(ERROR) << "Cannot find the user's \"Downloads\" folder."; | |
Randy Smith (Not in Mondays)
2011/06/02 19:13:57
I'm inclined to suggest you make this a VLOG rathe
haraken1
2011/06/03 06:50:27
Done.
| |
381 // If the user's "Downloads" folder does not exist, use the | |
382 // originally suggested path even if the path does not exist | |
383 // (This is a rare case). | |
384 state.suggested_path = dir.Append(filename); | |
385 } else { | |
386 state.suggested_path = state.suggested_path.Append(filename); | |
387 } | |
385 } | 388 } |
386 | 389 |
387 // If the download is deemed dangerous, we'll use a temporary name for it. | 390 // If the download is deemed dangerous, we'll use a temporary name for it. |
388 if (state.IsDangerous()) { | 391 if (state.IsDangerous()) { |
389 state.target_name = FilePath(state.suggested_path).BaseName(); | 392 state.target_name = FilePath(state.suggested_path).BaseName(); |
390 // Create a temporary file to hold the file until the user approves its | 393 // Create a temporary file to hold the file until the user approves its |
391 // download. | 394 // download. |
392 FilePath::StringType file_name; | 395 FilePath::StringType file_name; |
393 FilePath path; | 396 FilePath path; |
394 #if defined(OS_WIN) | 397 #if defined(OS_WIN) |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
488 // FileSelectionCancelled(). | 491 // FileSelectionCancelled(). |
489 int32* id_ptr = new int32; | 492 int32* id_ptr = new int32; |
490 *id_ptr = download_id; | 493 *id_ptr = download_id; |
491 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, | 494 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, |
492 string16(), | 495 string16(), |
493 suggested_path, | 496 suggested_path, |
494 &file_type_info, 0, FILE_PATH_LITERAL(""), | 497 &file_type_info, 0, FILE_PATH_LITERAL(""), |
495 contents, owning_window, | 498 contents, owning_window, |
496 reinterpret_cast<void*>(id_ptr)); | 499 reinterpret_cast<void*>(id_ptr)); |
497 FOR_EACH_OBSERVER(Observer, observers_, | 500 FOR_EACH_OBSERVER(Observer, observers_, |
498 SelectFileDialogDisplayed(download_id)); | 501 SelectFileDialogDisplayed(download_id, suggested_path)); |
499 } else { | 502 } else { |
500 // No prompting for download, just continue with the suggested name. | 503 // No prompting for download, just continue with the suggested name. |
501 ContinueDownloadWithPath(download, suggested_path); | 504 ContinueDownloadWithPath(download, suggested_path); |
502 } | 505 } |
503 } | 506 } |
504 | 507 |
505 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { | 508 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { |
506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 509 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
507 | 510 |
508 DownloadItem* download = new DownloadItem(this, *info, | 511 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); | 1280 observed_download_manager_->RemoveObserver(this); |
1278 } | 1281 } |
1279 | 1282 |
1280 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { | 1283 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
1281 observing_download_manager_->NotifyModelChanged(); | 1284 observing_download_manager_->NotifyModelChanged(); |
1282 } | 1285 } |
1283 | 1286 |
1284 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { | 1287 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
1285 observed_download_manager_ = NULL; | 1288 observed_download_manager_ = NULL; |
1286 } | 1289 } |
OLD | NEW |