Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: chrome/browser/download/download_util.cc

Issue 6973052: When the download folder does not exist, change the download folder to a user's "Downloads" (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Add ChooseSavableDirectory() and ScopedDefaultDownloadDirectory Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // Download utility implementation 5 // Download utility implementation
6 6
7 #include "chrome/browser/download/download_util.h" 7 #include "chrome/browser/download/download_util.h"
8 8
9 #if defined(OS_WIN) 9 #if defined(OS_WIN)
10 #include <shobjidl.h> 10 #include <shobjidl.h>
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 196
197 DCHECK(!generated_name->empty()); 197 DCHECK(!generated_name->empty());
198 198
199 GenerateSafeFileName(mime_type, generated_name); 199 GenerateSafeFileName(mime_type, generated_name);
200 } 200 }
201 201
202 } // namespace 202 } // namespace
203 203
204 // Download temporary file creation -------------------------------------------- 204 // Download temporary file creation --------------------------------------------
205 205
206 class DefaultDownloadDirectory { 206 ScopedDefaultDownloadDirectory::ScopedDefaultDownloadDirectory() {
207 public: 207 DefaultDownloadDirectory::override_path_.clear();
208 const FilePath& path() const { return path_; } 208 }
209 private: 209
210 DefaultDownloadDirectory() { 210 ScopedDefaultDownloadDirectory::~ScopedDefaultDownloadDirectory() {
211 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) { 211 DefaultDownloadDirectory::override_path_.clear();
212 }
213
214 void ScopedDefaultDownloadDirectory::Override(const FilePath& override_path) {
215 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
216 }
217
218 void ScopedDefaultDownloadDirectory::UnOverride() {
219 DefaultDownloadDirectory::override_path_.clear();
220 }
221
222 FilePath DefaultDownloadDirectory::override_path_;
223
224 const FilePath DefaultDownloadDirectory::Get() {
225 if (!override_path_.empty()) {
226 return override_path_;
227 }
228 FilePath path;
229 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path))
230 NOTREACHED();
231 if (DownloadPathIsDangerous(path)) {
232 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &path))
212 NOTREACHED(); 233 NOTREACHED();
234 }
235 return path;
236 }
237
238 bool ChooseSavableDirectory(const FilePath& website_save_dir,
239 const FilePath& download_save_dir,
240 const FilePath& default_downloads_dir,
241 FilePath* save_dir) {
242 bool prompt_dialog = false;
243 if (file_util::PathIsWritable(website_save_dir)) {
244 // If the default html/websites save folder exists,
245 // then use the default h5Btml/websites save folder.
246 *save_dir = website_save_dir;
247 } else if (file_util::PathIsWritable(download_save_dir)) {
248 // If the default html/websites save folder does not exist
249 // but the default download folder exists,
250 // then use the default download folder.
251 *save_dir = download_save_dir;
252 } else {
253 // If both the above folders do not exist,
254 // use the user's "Downloads" folder.
255 *save_dir = download_util::DefaultDownloadDirectory::Get();
256 prompt_dialog = true;
257 if (!file_util::PathIsWritable(*save_dir)) {
258 VLOG(1) << "Cannot find the user's writable \"Downloads\" folder.";
259 // Create the |download_save_dir| folder if we cannot get
260 // the user's writable "Downloads" folder (This will be a rare case).
261 *save_dir = download_save_dir;
213 } 262 }
214 if (DownloadPathIsDangerous(path_)) { 263 // Make sure that the folder does exist.
215 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &path_)) { 264 if (!file_util::CreateDirectory(*save_dir))
216 NOTREACHED(); 265 LOG(ERROR) << "Failed to create " << (*save_dir).value();
217 }
218 }
219 } 266 }
220 friend struct base::DefaultLazyInstanceTraits<DefaultDownloadDirectory>; 267 return prompt_dialog;
221 FilePath path_;
222 };
223
224 static base::LazyInstance<DefaultDownloadDirectory>
225 g_default_download_directory(base::LINKER_INITIALIZED);
226
227 const FilePath& GetDefaultDownloadDirectory() {
228 return g_default_download_directory.Get().path();
229 } 268 }
230 269
231 bool CreateTemporaryFileForDownload(FilePath* temp_file) { 270 bool CreateTemporaryFileForDownload(FilePath* temp_file) {
232 if (file_util::CreateTemporaryFileInDir(GetDefaultDownloadDirectory(), 271 FilePath path = DefaultDownloadDirectory::Get();
233 temp_file)) 272 if (file_util::CreateTemporaryFileInDir(path, temp_file))
234 return true; 273 return true;
235 return file_util::CreateTemporaryFile(temp_file); 274 return file_util::CreateTemporaryFile(temp_file);
236 } 275 }
237 276
238 bool DownloadPathIsDangerous(const FilePath& download_path) { 277 bool DownloadPathIsDangerous(const FilePath& download_path) {
239 FilePath desktop_dir; 278 FilePath desktop_dir;
240 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) { 279 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) {
241 NOTREACHED(); 280 NOTREACHED();
242 return false; 281 return false;
243 } 282 }
(...skipping 679 matching lines...) Expand 10 before | Expand all | Expand 10 after
923 FilePath GetCrDownloadPath(const FilePath& suggested_path) { 962 FilePath GetCrDownloadPath(const FilePath& suggested_path) {
924 FilePath::StringType file_name; 963 FilePath::StringType file_name;
925 base::SStringPrintf( 964 base::SStringPrintf(
926 &file_name, 965 &file_name,
927 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), 966 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"),
928 suggested_path.value().c_str()); 967 suggested_path.value().c_str());
929 return FilePath(file_name); 968 return FilePath(file_name);
930 } 969 }
931 970
932 } // namespace download_util 971 } // namespace download_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698