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

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: Added URLRequestMockHTTPJob.test_dir_ and URLRequestMockHTTPJob.temp_dir_ Created 9 years, 4 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 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 const int kAllNetErrorCodes[] = { 203 const int kAllNetErrorCodes[] = {
204 #define NET_ERROR(label, value) -(value), 204 #define NET_ERROR(label, value) -(value),
205 #include "net/base/net_error_list.h" 205 #include "net/base/net_error_list.h"
206 #undef NET_ERROR 206 #undef NET_ERROR
207 }; 207 };
208 208
209 } // namespace 209 } // namespace
210 210
211 // Download temporary file creation -------------------------------------------- 211 // Download temporary file creation --------------------------------------------
212 212
213 class DefaultDownloadDirectory { 213 bool ChooseSavableDirectory(
214 public: 214 const FilePath& website_save_dir,
215 const FilePath& path() const { return path_; } 215 const FilePath& download_save_dir,
216 private: 216 const FilePath& default_download_dir,
217 DefaultDownloadDirectory() { 217 FilePath* save_dir) {
218 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) { 218 bool prompt_dialog = false;
219 NOTREACHED(); 219 if (file_util::PathIsWritable(website_save_dir)) {
220 // If the default html/websites save folder exists,
221 // then use the default html/websites save folder.
222 *save_dir = website_save_dir;
223 } else if (file_util::PathIsWritable(download_save_dir)) {
224 // If the default html/websites save folder does not exist
225 // but the default download folder exists,
226 // then use the default download folder.
227 *save_dir = download_save_dir;
228 } else {
229 // If both the above folders do not exist,
230 // use the user's "Downloads" folder.
231 *save_dir = default_download_dir;
232 prompt_dialog = true;
233 if (!file_util::PathIsWritable(*save_dir)) {
234 VLOG(1) << "Cannot find the user's writable \"Downloads\" folder.";
235 // Create the |download_save_dir| folder if we cannot get
236 // the user's writable "Downloads" folder (This will be a rare case).
237 *save_dir = download_save_dir;
220 } 238 }
221 if (DownloadPathIsDangerous(path_)) { 239 // Make sure that the folder does exist.
222 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &path_)) { 240 if (!file_util::CreateDirectory(*save_dir))
223 NOTREACHED(); 241 LOG(ERROR) << "Failed to create " << (*save_dir).value();
224 }
225 }
226 } 242 }
227 friend struct base::DefaultLazyInstanceTraits<DefaultDownloadDirectory>; 243 return prompt_dialog;
228 FilePath path_;
229 };
230
231 static base::LazyInstance<DefaultDownloadDirectory>
232 g_default_download_directory(base::LINKER_INITIALIZED);
233
234 const FilePath& GetDefaultDownloadDirectory() {
235 return g_default_download_directory.Get().path();
236 } 244 }
237 245
238 bool CreateTemporaryFileForDownload(FilePath* temp_file) { 246 FilePath GetDefaultDownloadDirectoryFromPathService() {
239 if (file_util::CreateTemporaryFileInDir(GetDefaultDownloadDirectory(), 247 FilePath default_download_dir;
240 temp_file)) 248 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &default_download_dir))
241 return true; 249 NOTREACHED();
242 return file_util::CreateTemporaryFile(temp_file); 250 if (DownloadPathIsDangerous(default_download_dir)) {
251 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE,
252 &default_download_dir))
253 NOTREACHED();
254 }
255 return default_download_dir;
243 } 256 }
244 257
245 bool DownloadPathIsDangerous(const FilePath& download_path) { 258 bool DownloadPathIsDangerous(const FilePath& download_path) {
246 FilePath desktop_dir; 259 FilePath desktop_dir;
247 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) { 260 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) {
248 NOTREACHED(); 261 NOTREACHED();
249 return false; 262 return false;
250 } 263 }
251 return (download_path == desktop_dir); 264 return (download_path == desktop_dir);
252 } 265 }
(...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after
909 FilePath GetCrDownloadPath(const FilePath& suggested_path) { 922 FilePath GetCrDownloadPath(const FilePath& suggested_path) {
910 FilePath::StringType file_name; 923 FilePath::StringType file_name;
911 base::SStringPrintf( 924 base::SStringPrintf(
912 &file_name, 925 &file_name,
913 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), 926 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"),
914 suggested_path.value().c_str()); 927 suggested_path.value().c_str());
915 return FilePath(file_name); 928 return FilePath(file_name);
916 } 929 }
917 930
918 } // namespace download_util 931 } // namespace download_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698