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

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: Remove DownloadTest.DownloadFolder2 and SavePageBrowserTest.SaveFolder4 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 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 const int kAllNetErrorCodes[] = { 204 const int kAllNetErrorCodes[] = {
205 #define NET_ERROR(label, value) -(value), 205 #define NET_ERROR(label, value) -(value),
206 #include "net/base/net_error_list.h" 206 #include "net/base/net_error_list.h"
207 #undef NET_ERROR 207 #undef NET_ERROR
208 }; 208 };
209 209
210 } // namespace 210 } // namespace
211 211
212 // Download temporary file creation -------------------------------------------- 212 // Download temporary file creation --------------------------------------------
213 213
214 class DefaultDownloadDirectory { 214 bool ChooseSavableDirectory(
215 public: 215 const FilePath& website_save_dir,
216 const FilePath& path() const { return path_; } 216 const FilePath& download_save_dir,
217 private: 217 const FilePath& default_download_dir,
218 DefaultDownloadDirectory() { 218 FilePath* save_dir) {
219 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) { 219 bool prompt_dialog = false;
220 NOTREACHED(); 220 if (file_util::PathIsWritable(website_save_dir)) {
221 // If the default html/websites save folder exists,
222 // then use the default h5Btml/websites save folder.
223 *save_dir = website_save_dir;
224 } else if (file_util::PathIsWritable(download_save_dir)) {
225 // If the default html/websites save folder does not exist
226 // but the default download folder exists,
227 // then use the default download folder.
228 *save_dir = download_save_dir;
229 } else {
230 // If both the above folders do not exist,
231 // use the user's "Downloads" folder.
232 *save_dir = default_download_dir;
233 prompt_dialog = true;
234 if (!file_util::PathIsWritable(*save_dir)) {
235 VLOG(1) << "Cannot find the user's writable \"Downloads\" folder.";
236 // Create the |download_save_dir| folder if we cannot get
237 // the user's writable "Downloads" folder (This will be a rare case).
238 *save_dir = download_save_dir;
221 } 239 }
222 if (DownloadPathIsDangerous(path_)) { 240 // Make sure that the folder does exist.
223 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &path_)) { 241 if (!file_util::CreateDirectory(*save_dir))
224 NOTREACHED(); 242 LOG(ERROR) << "Failed to create " << (*save_dir).value();
225 }
226 }
227 } 243 }
228 friend struct base::DefaultLazyInstanceTraits<DefaultDownloadDirectory>; 244 return prompt_dialog;
229 FilePath path_;
230 };
231
232 static base::LazyInstance<DefaultDownloadDirectory>
233 g_default_download_directory(base::LINKER_INITIALIZED);
234
235 const FilePath& GetDefaultDownloadDirectory() {
236 return g_default_download_directory.Get().path();
237 } 245 }
238 246
239 bool CreateTemporaryFileForDownload(FilePath* temp_file) { 247 FilePath GetDefaultDownloadDirectoryFromPathService() {
240 if (file_util::CreateTemporaryFileInDir(GetDefaultDownloadDirectory(), 248 FilePath default_download_dir;
241 temp_file)) 249 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &default_download_dir))
242 return true; 250 NOTREACHED();
243 return file_util::CreateTemporaryFile(temp_file); 251 if (DownloadPathIsDangerous(default_download_dir)) {
252 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE,
253 &default_download_dir))
254 NOTREACHED();
255 }
256 return default_download_dir;
244 } 257 }
245 258
246 bool DownloadPathIsDangerous(const FilePath& download_path) { 259 bool DownloadPathIsDangerous(const FilePath& download_path) {
247 FilePath desktop_dir; 260 FilePath desktop_dir;
248 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) { 261 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) {
249 NOTREACHED(); 262 NOTREACHED();
250 return false; 263 return false;
251 } 264 }
252 return (download_path == desktop_dir); 265 return (download_path == desktop_dir);
253 } 266 }
(...skipping 695 matching lines...) Expand 10 before | Expand all | Expand 10 after
949 FilePath GetCrDownloadPath(const FilePath& suggested_path) { 962 FilePath GetCrDownloadPath(const FilePath& suggested_path) {
950 FilePath::StringType file_name; 963 FilePath::StringType file_name;
951 base::SStringPrintf( 964 base::SStringPrintf(
952 &file_name, 965 &file_name,
953 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), 966 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"),
954 suggested_path.value().c_str()); 967 suggested_path.value().c_str());
955 return FilePath(file_name); 968 return FilePath(file_name);
956 } 969 }
957 970
958 } // namespace download_util 971 } // namespace download_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698