Index: chrome/browser/download/download_manager.cc |
diff --git a/chrome/browser/download/download_manager.cc b/chrome/browser/download/download_manager.cc |
index 872464127f82afe8eae42a99d2600fd0e30ccbc8..3e0bdd9d0148bb2a353abce3a27a80554ccc6ad0 100644 |
--- a/chrome/browser/download/download_manager.cc |
+++ b/chrome/browser/download/download_manager.cc |
@@ -8,7 +8,6 @@ |
#include "base/file_util.h" |
#include "base/i18n/case_conversion.h" |
#include "base/logging.h" |
-#include "base/path_service.h" |
#include "base/rand_util.h" |
#include "base/stl_util-inl.h" |
#include "base/stringprintf.h" |
@@ -365,33 +364,26 @@ void DownloadManager::CheckVisitedReferrerBeforeDone( |
&DownloadManager::CheckIfSuggestedPathExists, |
download->id(), |
state, |
- download_prefs()->download_path())); |
+ download_prefs()->download_path(), |
+ download_prefs()->GetDefaultDownloadDirectory())); |
} |
-void DownloadManager::CheckIfSuggestedPathExists(int32 download_id, |
- DownloadStateInfo state, |
- const FilePath& default_path) { |
+void DownloadManager::CheckIfSuggestedPathExists( |
+ int32 download_id, |
+ DownloadStateInfo state, |
+ const FilePath& download_save_dir, |
+ const FilePath& default_download_dir) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
- // Make sure the default download directory exists. |
- // TODO(phajdan.jr): only create the directory when we're sure the user |
- // is going to save there and not to another directory of his choice. |
- file_util::CreateDirectory(default_path); |
- |
- // Check writability of the suggested path. If we can't write to it, default |
- // to the user's "My Documents" directory. We'll prompt them in this case. |
- FilePath dir = state.suggested_path.DirName(); |
- FilePath filename = state.suggested_path.BaseName(); |
- if (!file_util::PathIsWritable(dir)) { |
- VLOG(1) << "Unable to write to directory \"" << dir.value() << "\""; |
+ FilePath save_dir; |
+ if (ChooseSavableDirectory( |
+ FilePath(), download_save_dir, default_download_dir, &save_dir)) |
state.prompt_user_for_save_location = true; |
- PathService::Get(chrome::DIR_USER_DOCUMENTS, &state.suggested_path); |
- state.suggested_path = state.suggested_path.Append(filename); |
- } |
+ state.suggested_path = save_dir.Append(state.suggested_path.BaseName()); |
// If the download is deemed dangerous, we'll use a temporary name for it. |
if (state.IsDangerous()) { |
- state.target_name = FilePath(state.suggested_path).BaseName(); |
+ state.target_name = state.suggested_path.BaseName(); |
// Create a temporary file to hold the file until the user approves its |
// download. |
FilePath::StringType file_name; |
@@ -410,7 +402,7 @@ void DownloadManager::CheckIfSuggestedPathExists(int32 download_id, |
unconfirmed_prefix.append( |
FILE_PATH_LITERAL(" %d.crdownload")).c_str(), |
base::RandInt(0, 100000)); |
- path = dir.Append(file_name); |
+ path = state.suggested_path.DirName().Append(file_name); |
if (file_util::PathExists(path)) |
path = FilePath(); |
} |
@@ -500,7 +492,7 @@ void DownloadManager::OnPathExistenceAvailable(int32 download_id, |
contents, owning_window, |
reinterpret_cast<void*>(id_ptr)); |
FOR_EACH_OBSERVER(Observer, observers_, |
- SelectFileDialogDisplayed(download_id)); |
+ SelectFileDialogDisplayed(download_id, suggested_path)); |
} else { |
// No prompting for download, just continue with the suggested name. |
ContinueDownloadWithPath(download, suggested_path); |
@@ -652,6 +644,40 @@ void DownloadManager::AssertNotInQueues(DownloadItem* download) { |
CHECK(!ContainsKey(history_downloads_, download->db_handle())); |
} |
+// static |
+bool DownloadManager::ChooseSavableDirectory( |
+ const FilePath& website_save_dir, |
+ const FilePath& download_save_dir, |
+ const FilePath& default_download_dir, |
+ FilePath* save_dir) { |
+ bool prompt_dialog = false; |
+ if (file_util::PathIsWritable(website_save_dir)) { |
+ // If the default html/websites save folder exists, |
+ // then use the default h5Btml/websites save folder. |
+ *save_dir = website_save_dir; |
+ } else if (file_util::PathIsWritable(download_save_dir)) { |
+ // If the default html/websites save folder does not exist |
+ // but the default download folder exists, |
+ // then use the default download folder. |
+ *save_dir = download_save_dir; |
+ } else { |
+ // If both the above folders do not exist, |
+ // use the user's "Downloads" folder. |
+ *save_dir = default_download_dir; |
+ prompt_dialog = true; |
+ if (!file_util::PathIsWritable(*save_dir)) { |
+ VLOG(1) << "Cannot find the user's writable \"Downloads\" folder."; |
+ // Create the |download_save_dir| folder if we cannot get |
+ // the user's writable "Downloads" folder (This will be a rare case). |
+ *save_dir = download_save_dir; |
+ } |
+ // Make sure that the folder does exist. |
+ if (!file_util::CreateDirectory(*save_dir)) |
+ LOG(ERROR) << "Failed to create " << (*save_dir).value(); |
+ } |
+ return prompt_dialog; |
+} |
+ |
bool DownloadManager::IsDownloadReadyForCompletion(DownloadItem* download) { |
// If we don't have all the data, the download is not ready for |
// completion. |