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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 FilePath GetDefaultDownloadDirectoryFromPathService() { |
207 public: | 207 FilePath default_download_dir; |
208 const FilePath& path() const { return path_; } | 208 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &default_download_dir)) |
209 private: | 209 NOTREACHED(); |
210 DefaultDownloadDirectory() { | 210 if (DownloadPathIsDangerous(default_download_dir)) { |
211 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) { | 211 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, |
| 212 &default_download_dir)) |
212 NOTREACHED(); | 213 NOTREACHED(); |
213 } | |
214 if (DownloadPathIsDangerous(path_)) { | |
215 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &path_)) { | |
216 NOTREACHED(); | |
217 } | |
218 } | |
219 } | 214 } |
220 friend struct base::DefaultLazyInstanceTraits<DefaultDownloadDirectory>; | 215 return default_download_dir; |
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 } | |
230 | |
231 bool CreateTemporaryFileForDownload(FilePath* temp_file) { | |
232 if (file_util::CreateTemporaryFileInDir(GetDefaultDownloadDirectory(), | |
233 temp_file)) | |
234 return true; | |
235 return file_util::CreateTemporaryFile(temp_file); | |
236 } | 216 } |
237 | 217 |
238 bool DownloadPathIsDangerous(const FilePath& download_path) { | 218 bool DownloadPathIsDangerous(const FilePath& download_path) { |
239 FilePath desktop_dir; | 219 FilePath desktop_dir; |
240 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) { | 220 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) { |
241 NOTREACHED(); | 221 NOTREACHED(); |
242 return false; | 222 return false; |
243 } | 223 } |
244 return (download_path == desktop_dir); | 224 return (download_path == desktop_dir); |
245 } | 225 } |
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
903 FilePath GetCrDownloadPath(const FilePath& suggested_path) { | 883 FilePath GetCrDownloadPath(const FilePath& suggested_path) { |
904 FilePath::StringType file_name; | 884 FilePath::StringType file_name; |
905 base::SStringPrintf( | 885 base::SStringPrintf( |
906 &file_name, | 886 &file_name, |
907 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), | 887 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), |
908 suggested_path.value().c_str()); | 888 suggested_path.value().c_str()); |
909 return FilePath(file_name); | 889 return FilePath(file_name); |
910 } | 890 } |
911 | 891 |
912 } // namespace download_util | 892 } // namespace download_util |
OLD | NEW |