Chromium Code Reviews| Index: chrome/browser/download/download_util.cc |
| diff --git a/chrome/browser/download/download_util.cc b/chrome/browser/download/download_util.cc |
| index 108aa7a61c8a92da13f4054169177ee2a817d6d8..d1ca384893209682fc67ace9eb6365a434781703 100644 |
| --- a/chrome/browser/download/download_util.cc |
| +++ b/chrome/browser/download/download_util.cc |
| @@ -203,34 +203,73 @@ void GenerateFileNameInternal(const GURL& url, |
| // Download temporary file creation -------------------------------------------- |
| -class DefaultDownloadDirectory { |
| - public: |
| - const FilePath& path() const { return path_; } |
| - private: |
| - DefaultDownloadDirectory() { |
| - if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) { |
| +ScopedDefaultDownloadDirectory::ScopedDefaultDownloadDirectory() { |
| + DefaultDownloadDirectory::override_path_.clear(); |
| +} |
| + |
| +ScopedDefaultDownloadDirectory::~ScopedDefaultDownloadDirectory() { |
| + DefaultDownloadDirectory::override_path_.clear(); |
| +} |
| + |
| +void ScopedDefaultDownloadDirectory::Override(const FilePath& override_path) { |
| + DefaultDownloadDirectory::override_path_ = override_path; |
|
Paweł Hajdan Jr.
2011/06/09 19:10:38
We should still update the pref here, as said befo
haraken1
2011/06/14 11:10:05
I see. Now, DownloadPrefs::OverrideDefaultDownload
|
| +} |
| + |
| +void ScopedDefaultDownloadDirectory::UnOverride() { |
| + DefaultDownloadDirectory::override_path_.clear(); |
| +} |
| + |
| +FilePath DefaultDownloadDirectory::override_path_; |
| + |
| +const FilePath DefaultDownloadDirectory::Get() { |
| + if (!override_path_.empty()) { |
| + return override_path_; |
| + } |
| + FilePath path; |
| + if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path)) |
| + NOTREACHED(); |
| + if (DownloadPathIsDangerous(path)) { |
| + if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &path)) |
| NOTREACHED(); |
| + } |
| + return path; |
| +} |
| + |
| +bool ChooseSavableDirectory(const FilePath& website_save_dir, |
| + const FilePath& download_save_dir, |
| + const FilePath& default_downloads_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 = download_util::DefaultDownloadDirectory::Get(); |
| + 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; |
| } |
| - if (DownloadPathIsDangerous(path_)) { |
| - if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &path_)) { |
| - NOTREACHED(); |
| - } |
| - } |
| + // Make sure that the folder does exist. |
| + if (!file_util::CreateDirectory(*save_dir)) |
| + LOG(ERROR) << "Failed to create " << (*save_dir).value(); |
| } |
| - friend struct base::DefaultLazyInstanceTraits<DefaultDownloadDirectory>; |
| - FilePath path_; |
| -}; |
| - |
| -static base::LazyInstance<DefaultDownloadDirectory> |
| - g_default_download_directory(base::LINKER_INITIALIZED); |
| - |
| -const FilePath& GetDefaultDownloadDirectory() { |
| - return g_default_download_directory.Get().path(); |
| + return prompt_dialog; |
| } |
| bool CreateTemporaryFileForDownload(FilePath* temp_file) { |
| - if (file_util::CreateTemporaryFileInDir(GetDefaultDownloadDirectory(), |
| - temp_file)) |
| + FilePath path = DefaultDownloadDirectory::Get(); |
| + if (file_util::CreateTemporaryFileInDir(path, temp_file)) |
| return true; |
| return file_util::CreateTemporaryFile(temp_file); |
| } |