| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "chrome/browser/download/save_package.h" | 5 #include "chrome/browser/download/save_package.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/i18n/file_util_icu.h" | 9 #include "base/i18n/file_util_icu.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 explicit SavePackageParam(const std::string& mime_type) | 58 explicit SavePackageParam(const std::string& mime_type) |
| 59 : current_tab_mime_type(mime_type), | 59 : current_tab_mime_type(mime_type), |
| 60 save_type(SavePackage::SAVE_TYPE_UNKNOWN) { | 60 save_type(SavePackage::SAVE_TYPE_UNKNOWN) { |
| 61 } | 61 } |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 namespace { | 64 namespace { |
| 65 | 65 |
| 66 // Default name which will be used when we can not get proper name from | 66 // Default name which will be used when we can not get proper name from |
| 67 // resource URL. | 67 // resource URL. |
| 68 const wchar_t kDefaultSaveName[] = L"saved_resource"; | 68 const char kDefaultSaveName[] = "saved_resource"; |
| 69 | 69 |
| 70 const FilePath::CharType kDefaultHtmlExtension[] = | 70 const FilePath::CharType kDefaultHtmlExtension[] = |
| 71 #if defined(OS_WIN) | 71 #if defined(OS_WIN) |
| 72 FILE_PATH_LITERAL("htm"); | 72 FILE_PATH_LITERAL("htm"); |
| 73 #else | 73 #else |
| 74 FILE_PATH_LITERAL("html"); | 74 FILE_PATH_LITERAL("html"); |
| 75 #endif | 75 #endif |
| 76 | 76 |
| 77 // Maximum number of file ordinal number. I think it's big enough for resolving | 77 // Maximum number of file ordinal number. I think it's big enough for resolving |
| 78 // name-conflict files which has same base file name. | 78 // name-conflict files which has same base file name. |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 return true; | 329 return true; |
| 330 } | 330 } |
| 331 | 331 |
| 332 // Generate name for saving resource. | 332 // Generate name for saving resource. |
| 333 bool SavePackage::GenerateFilename(const std::string& disposition, | 333 bool SavePackage::GenerateFilename(const std::string& disposition, |
| 334 const GURL& url, | 334 const GURL& url, |
| 335 bool need_html_ext, | 335 bool need_html_ext, |
| 336 FilePath::StringType* generated_name) { | 336 FilePath::StringType* generated_name) { |
| 337 // TODO(jungshik): Figure out the referrer charset when having one | 337 // TODO(jungshik): Figure out the referrer charset when having one |
| 338 // makes sense and pass it to GetSuggestedFilename. | 338 // makes sense and pass it to GetSuggestedFilename. |
| 339 FilePath file_path = FilePath::FromWStringHack( | 339 FilePath file_path = net::GetSuggestedFilename(url, disposition, "", |
| 340 net::GetSuggestedFilename(url, disposition, "", kDefaultSaveName)); | 340 kDefaultSaveName); |
| 341 | 341 |
| 342 DCHECK(!file_path.empty()); | 342 DCHECK(!file_path.empty()); |
| 343 FilePath::StringType pure_file_name = | 343 FilePath::StringType pure_file_name = |
| 344 file_path.RemoveExtension().BaseName().value(); | 344 file_path.RemoveExtension().BaseName().value(); |
| 345 FilePath::StringType file_name_ext = file_path.Extension(); | 345 FilePath::StringType file_name_ext = file_path.Extension(); |
| 346 | 346 |
| 347 // If it is HTML resource, use ".htm{l,}" as its extension. | 347 // If it is HTML resource, use ".htm{l,}" as its extension. |
| 348 if (need_html_ext) { | 348 if (need_html_ext) { |
| 349 file_name_ext = FILE_PATH_LITERAL("."); | 349 file_name_ext = FILE_PATH_LITERAL("."); |
| 350 file_name_ext.append(kDefaultHtmlExtension); | 350 file_name_ext.append(kDefaultHtmlExtension); |
| (...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1026 } | 1026 } |
| 1027 if (url_path.empty()) | 1027 if (url_path.empty()) |
| 1028 url_path = url.host(); | 1028 url_path = url.host(); |
| 1029 name_with_proper_ext = FilePath::FromWStringHack(UTF8ToWide(url_path)); | 1029 name_with_proper_ext = FilePath::FromWStringHack(UTF8ToWide(url_path)); |
| 1030 } | 1030 } |
| 1031 | 1031 |
| 1032 // Ask user for getting final saving name. | 1032 // Ask user for getting final saving name. |
| 1033 if (can_save_as_complete) | 1033 if (can_save_as_complete) |
| 1034 name_with_proper_ext = EnsureHtmlExtension(name_with_proper_ext); | 1034 name_with_proper_ext = EnsureHtmlExtension(name_with_proper_ext); |
| 1035 | 1035 |
| 1036 std::wstring file_name = name_with_proper_ext.ToWStringHack(); | 1036 FilePath::StringType file_name = name_with_proper_ext.value(); |
| 1037 // TODO(port): we need a version of ReplaceIllegalCharacters() that takes | 1037 file_util::ReplaceIllegalCharactersInPath(&file_name, ' '); |
| 1038 // FilePaths. | 1038 return FilePath(file_name); |
| 1039 file_util::ReplaceIllegalCharacters(&file_name, L' '); | |
| 1040 TrimWhitespace(file_name, TRIM_ALL, &file_name); | |
| 1041 | |
| 1042 return FilePath::FromWStringHack(file_name); | |
| 1043 } | 1039 } |
| 1044 | 1040 |
| 1045 FilePath SavePackage::EnsureHtmlExtension(const FilePath& name) { | 1041 FilePath SavePackage::EnsureHtmlExtension(const FilePath& name) { |
| 1046 // If the file name doesn't have an extension suitable for HTML files, | 1042 // If the file name doesn't have an extension suitable for HTML files, |
| 1047 // append one. | 1043 // append one. |
| 1048 FilePath::StringType ext = file_util::GetFileExtensionFromPath(name); | 1044 FilePath::StringType ext = file_util::GetFileExtensionFromPath(name); |
| 1049 std::string mime_type; | 1045 std::string mime_type; |
| 1050 if (!net::GetMimeTypeFromExtension(ext, &mime_type) || | 1046 if (!net::GetMimeTypeFromExtension(ext, &mime_type) || |
| 1051 !CanSaveAsComplete(mime_type)) { | 1047 !CanSaveAsComplete(mime_type)) { |
| 1052 return FilePath(name.value() + FILE_PATH_LITERAL(".") + | 1048 return FilePath(name.value() + FILE_PATH_LITERAL(".") + |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1251 int index, void* params) { | 1247 int index, void* params) { |
| 1252 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); | 1248 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); |
| 1253 ContinueSave(save_params, path, index); | 1249 ContinueSave(save_params, path, index); |
| 1254 delete save_params; | 1250 delete save_params; |
| 1255 } | 1251 } |
| 1256 | 1252 |
| 1257 void SavePackage::FileSelectionCanceled(void* params) { | 1253 void SavePackage::FileSelectionCanceled(void* params) { |
| 1258 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); | 1254 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); |
| 1259 delete save_params; | 1255 delete save_params; |
| 1260 } | 1256 } |
| OLD | NEW |