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