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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 const int kAllNetErrorCodes[] = { | 118 const int kAllNetErrorCodes[] = { |
119 #define NET_ERROR(label, value) -(value), | 119 #define NET_ERROR(label, value) -(value), |
120 #include "net/base/net_error_list.h" | 120 #include "net/base/net_error_list.h" |
121 #undef NET_ERROR | 121 #undef NET_ERROR |
122 }; | 122 }; |
123 | 123 |
124 } // namespace | 124 } // namespace |
125 | 125 |
126 // Download temporary file creation -------------------------------------------- | 126 // Download temporary file creation -------------------------------------------- |
127 | 127 |
128 class DefaultDownloadDirectory { | 128 bool ChooseSavableDirectory( |
129 public: | 129 const FilePath& website_save_dir, |
130 const FilePath& path() const { return path_; } | 130 const FilePath& download_save_dir, |
131 private: | 131 const FilePath& default_download_dir, |
132 DefaultDownloadDirectory() { | 132 FilePath* save_dir) { |
133 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) { | 133 bool prompt_dialog = false; |
134 NOTREACHED(); | 134 if (file_util::PathIsWritable(website_save_dir)) { |
135 // If the default html/websites save folder exists, | |
136 // then use the default html/websites save folder. | |
137 *save_dir = website_save_dir; | |
138 } else if (file_util::PathIsWritable(download_save_dir)) { | |
139 // If the default html/websites save folder does not exist | |
140 // but the default download folder exists, | |
141 // then use the default download folder. | |
142 *save_dir = download_save_dir; | |
143 } else { | |
144 // If both the above folders do not exist, | |
145 // use the user's "Downloads" folder. | |
146 *save_dir = default_download_dir; | |
147 prompt_dialog = true; | |
148 if (!file_util::PathIsWritable(*save_dir)) { | |
149 VLOG(1) << "Cannot find the user's writable \"Downloads\" folder."; | |
150 // Create the |download_save_dir| folder if we cannot get | |
151 // the user's writable "Downloads" folder (This will be a rare case). | |
152 *save_dir = download_save_dir; | |
135 } | 153 } |
136 if (DownloadPathIsDangerous(path_)) { | 154 // Make sure that the folder does exist. |
137 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &path_)) { | 155 if (!file_util::CreateDirectory(*save_dir)) |
138 NOTREACHED(); | 156 LOG(ERROR) << "Failed to create " << (*save_dir).value(); |
139 } | |
140 } | |
141 } | 157 } |
142 friend struct base::DefaultLazyInstanceTraits<DefaultDownloadDirectory>; | 158 return prompt_dialog; |
143 FilePath path_; | |
144 }; | |
145 | |
146 static base::LazyInstance<DefaultDownloadDirectory> | |
147 g_default_download_directory(base::LINKER_INITIALIZED); | |
148 | |
149 const FilePath& GetDefaultDownloadDirectory() { | |
150 return g_default_download_directory.Get().path(); | |
151 } | 159 } |
152 | 160 |
153 bool CreateTemporaryFileForDownload(FilePath* temp_file) { | 161 FilePath GetDefaultDownloadDirectoryFromPathService() { |
154 if (file_util::CreateTemporaryFileInDir(GetDefaultDownloadDirectory(), | 162 FilePath default_download_dir; |
155 temp_file)) | 163 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &default_download_dir)) |
156 return true; | 164 NOTREACHED(); |
157 return file_util::CreateTemporaryFile(temp_file); | 165 if (DownloadPathIsDangerous(default_download_dir)) { |
166 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, | |
167 &default_download_dir)) | |
Paweł Hajdan Jr.
2011/08/12 17:00:59
nit: Braces {} here.
haraken1
2011/08/15 00:44:15
Done.
| |
168 NOTREACHED(); | |
169 } | |
170 return default_download_dir; | |
158 } | 171 } |
159 | 172 |
160 bool DownloadPathIsDangerous(const FilePath& download_path) { | 173 bool DownloadPathIsDangerous(const FilePath& download_path) { |
161 FilePath desktop_dir; | 174 FilePath desktop_dir; |
162 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) { | 175 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) { |
163 NOTREACHED(); | 176 NOTREACHED(); |
164 return false; | 177 return false; |
165 } | 178 } |
166 return (download_path == desktop_dir); | 179 return (download_path == desktop_dir); |
167 } | 180 } |
(...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
812 FilePath GetCrDownloadPath(const FilePath& suggested_path) { | 825 FilePath GetCrDownloadPath(const FilePath& suggested_path) { |
813 FilePath::StringType file_name; | 826 FilePath::StringType file_name; |
814 base::SStringPrintf( | 827 base::SStringPrintf( |
815 &file_name, | 828 &file_name, |
816 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), | 829 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), |
817 suggested_path.value().c_str()); | 830 suggested_path.value().c_str()); |
818 return FilePath(file_name); | 831 return FilePath(file_name); |
819 } | 832 } |
820 | 833 |
821 } // namespace download_util | 834 } // namespace download_util |
OLD | NEW |