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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 | 160 |
161 for (int i = 0; i < arraysize(magic_names); ++i) { | 161 for (int i = 0; i < arraysize(magic_names); ++i) { |
162 if (filename_lower == magic_names[i]) | 162 if (filename_lower == magic_names[i]) |
163 return true; | 163 return true; |
164 } | 164 } |
165 | 165 |
166 return false; | 166 return false; |
167 } | 167 } |
168 #endif // OS_WIN | 168 #endif // OS_WIN |
169 | 169 |
| 170 void GenerateFileNameInternal(const GURL& url, |
| 171 const std::string& content_disposition, |
| 172 const std::string& referrer_charset, |
| 173 const std::string& suggested_name, |
| 174 const std::string& mime_type, |
| 175 FilePath* generated_name) { |
| 176 |
| 177 string16 default_file_name( |
| 178 l10n_util::GetStringUTF16(IDS_DEFAULT_DOWNLOAD_FILENAME)); |
| 179 |
| 180 string16 new_name = net::GetSuggestedFilename(GURL(url), |
| 181 content_disposition, |
| 182 referrer_charset, |
| 183 suggested_name, |
| 184 default_file_name); |
| 185 |
| 186 // TODO(evan): this code is totally wrong -- we should just generate |
| 187 // Unicode filenames and do all this encoding switching at the end. |
| 188 // However, I'm just shuffling wrong code around, at least not adding |
| 189 // to it. |
| 190 #if defined(OS_WIN) |
| 191 *generated_name = FilePath(new_name); |
| 192 #else |
| 193 *generated_name = FilePath( |
| 194 base::SysWideToNativeMB(UTF16ToWide(new_name))); |
| 195 #endif |
| 196 |
| 197 DCHECK(!generated_name->empty()); |
| 198 |
| 199 GenerateSafeFileName(mime_type, generated_name); |
| 200 } |
| 201 |
170 } // namespace | 202 } // namespace |
171 | 203 |
172 // Download temporary file creation -------------------------------------------- | 204 // Download temporary file creation -------------------------------------------- |
173 | 205 |
174 class DefaultDownloadDirectory { | 206 class DefaultDownloadDirectory { |
175 public: | 207 public: |
176 const FilePath& path() const { return path_; } | 208 const FilePath& path() const { return path_; } |
177 private: | 209 private: |
178 DefaultDownloadDirectory() { | 210 DefaultDownloadDirectory() { |
179 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) { | 211 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 // http://crbug.com/61827 | 276 // http://crbug.com/61827 |
245 base::ThreadRestrictions::ScopedAllowIO allow_io; | 277 base::ThreadRestrictions::ScopedAllowIO allow_io; |
246 net::GetPreferredExtensionForMimeType(mime_type, &extension); | 278 net::GetPreferredExtensionForMimeType(mime_type, &extension); |
247 } | 279 } |
248 | 280 |
249 generated_extension->swap(extension); | 281 generated_extension->swap(extension); |
250 } | 282 } |
251 | 283 |
252 void GenerateFileNameFromInfo(DownloadCreateInfo* info, | 284 void GenerateFileNameFromInfo(DownloadCreateInfo* info, |
253 FilePath* generated_name) { | 285 FilePath* generated_name) { |
254 GenerateFileName(GURL(info->url()), | 286 GenerateFileNameInternal(GURL(info->url()), info->content_disposition, |
255 info->content_disposition, | 287 info->referrer_charset, std::string(), |
256 info->referrer_charset, | 288 info->mime_type, generated_name); |
257 info->mime_type, | 289 } |
258 generated_name); | 290 |
| 291 void GenerateFileNameFromSuggestedName(const GURL& url, |
| 292 const std::string& suggested_name, |
| 293 const std::string& mime_type, |
| 294 FilePath* generated_name) { |
| 295 GenerateFileNameInternal(url, std::string(), std::string(), |
| 296 suggested_name, mime_type, generated_name); |
259 } | 297 } |
260 | 298 |
261 void GenerateFileName(const GURL& url, | 299 void GenerateFileName(const GURL& url, |
262 const std::string& content_disposition, | 300 const std::string& content_disposition, |
263 const std::string& referrer_charset, | 301 const std::string& referrer_charset, |
264 const std::string& mime_type, | 302 const std::string& mime_type, |
265 FilePath* generated_name) { | 303 FilePath* generated_name) { |
266 string16 default_file_name( | 304 GenerateFileNameInternal(url, content_disposition, referrer_charset, |
267 l10n_util::GetStringUTF16(IDS_DEFAULT_DOWNLOAD_FILENAME)); | 305 std::string(), mime_type, generated_name); |
268 | |
269 string16 new_name = net::GetSuggestedFilename(GURL(url), | |
270 content_disposition, | |
271 referrer_charset, | |
272 default_file_name); | |
273 | |
274 // TODO(evan): this code is totally wrong -- we should just generate | |
275 // Unicode filenames and do all this encoding switching at the end. | |
276 // However, I'm just shuffling wrong code around, at least not adding | |
277 // to it. | |
278 #if defined(OS_WIN) | |
279 *generated_name = FilePath(new_name); | |
280 #else | |
281 *generated_name = FilePath( | |
282 base::SysWideToNativeMB(UTF16ToWide(new_name))); | |
283 #endif | |
284 | |
285 DCHECK(!generated_name->empty()); | |
286 | |
287 GenerateSafeFileName(mime_type, generated_name); | |
288 } | 306 } |
289 | 307 |
290 void GenerateSafeFileName(const std::string& mime_type, FilePath* file_name) { | 308 void GenerateSafeFileName(const std::string& mime_type, FilePath* file_name) { |
291 // Make sure we get the right file extension | 309 // Make sure we get the right file extension |
292 FilePath::StringType extension; | 310 FilePath::StringType extension; |
293 GenerateExtension(*file_name, mime_type, &extension); | 311 GenerateExtension(*file_name, mime_type, &extension); |
294 *file_name = file_name->ReplaceExtension(extension); | 312 *file_name = file_name->ReplaceExtension(extension); |
295 | 313 |
296 #if defined(OS_WIN) | 314 #if defined(OS_WIN) |
297 // Prepend "_" to the file name if it's a reserved name | 315 // Prepend "_" to the file name if it's a reserved name |
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
901 // Extensions that are not from the gallery are considered dangerous. | 919 // Extensions that are not from the gallery are considered dangerous. |
902 ExtensionService* service = profile->GetExtensionService(); | 920 ExtensionService* service = profile->GetExtensionService(); |
903 if (!service || | 921 if (!service || |
904 !service->IsDownloadFromGallery(info->url(), info->referrer_url)) | 922 !service->IsDownloadFromGallery(info->url(), info->referrer_url)) |
905 return true; | 923 return true; |
906 } | 924 } |
907 return false; | 925 return false; |
908 } | 926 } |
909 | 927 |
910 } // namespace download_util | 928 } // namespace download_util |
OLD | NEW |