OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-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/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 SavePackageParam(const std::string& mime_type) | 56 SavePackageParam(const std::string& mime_type) |
57 : current_tab_mime_type(mime_type) { } | 57 : current_tab_mime_type(mime_type) { } |
58 }; | 58 }; |
59 | 59 |
60 namespace { | 60 namespace { |
61 | 61 |
62 // Default name which will be used when we can not get proper name from | 62 // Default name which will be used when we can not get proper name from |
63 // resource URL. | 63 // resource URL. |
64 const wchar_t kDefaultSaveName[] = L"saved_resource"; | 64 const wchar_t kDefaultSaveName[] = L"saved_resource"; |
65 | 65 |
| 66 const FilePath::CharType kDefaultHtmlExtension[] = |
| 67 #if defined(OS_WIN) |
| 68 FILE_PATH_LITERAL("htm"); |
| 69 #else |
| 70 FILE_PATH_LITERAL("html"); |
| 71 #endif |
| 72 |
66 // Maximum number of file ordinal number. I think it's big enough for resolving | 73 // Maximum number of file ordinal number. I think it's big enough for resolving |
67 // name-conflict files which has same base file name. | 74 // name-conflict files which has same base file name. |
68 const int32 kMaxFileOrdinalNumber = 9999; | 75 const int32 kMaxFileOrdinalNumber = 9999; |
69 | 76 |
70 // Maximum length for file path. Since Windows have MAX_PATH limitation for | 77 // Maximum length for file path. Since Windows have MAX_PATH limitation for |
71 // file path, we need to make sure length of file path of every saved file | 78 // file path, we need to make sure length of file path of every saved file |
72 // is less than MAX_PATH | 79 // is less than MAX_PATH |
73 #if defined(OS_WIN) | 80 #if defined(OS_WIN) |
74 const uint32 kMaxFilePathLength = MAX_PATH - 1; | 81 const uint32 kMaxFilePathLength = MAX_PATH - 1; |
75 #elif defined(OS_POSIX) | 82 #elif defined(OS_POSIX) |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 // TODO(jungshik): Figure out the referrer charset when having one | 295 // TODO(jungshik): Figure out the referrer charset when having one |
289 // makes sense and pass it to GetSuggestedFilename. | 296 // makes sense and pass it to GetSuggestedFilename. |
290 FilePath file_path = FilePath::FromWStringHack( | 297 FilePath file_path = FilePath::FromWStringHack( |
291 net::GetSuggestedFilename(url, disposition, "", kDefaultSaveName)); | 298 net::GetSuggestedFilename(url, disposition, "", kDefaultSaveName)); |
292 | 299 |
293 DCHECK(!file_path.empty()); | 300 DCHECK(!file_path.empty()); |
294 FilePath::StringType pure_file_name = | 301 FilePath::StringType pure_file_name = |
295 file_path.RemoveExtension().BaseName().value(); | 302 file_path.RemoveExtension().BaseName().value(); |
296 FilePath::StringType file_name_ext = file_path.Extension(); | 303 FilePath::StringType file_name_ext = file_path.Extension(); |
297 | 304 |
298 // If it is HTML resource, use ".htm" as its extension name. | 305 // If it is HTML resource, use ".htm{l,}" as its extension. |
299 if (need_html_ext) | 306 if (need_html_ext) { |
300 file_name_ext = FILE_PATH_LITERAL(".htm"); | 307 file_name_ext = FILE_PATH_LITERAL("."); |
| 308 file_name_ext.append(kDefaultHtmlExtension); |
| 309 } |
301 | 310 |
302 // Get safe pure file name. | 311 // Get safe pure file name. |
303 if (!GetSafePureFileName(saved_main_directory_path_, file_name_ext, | 312 if (!GetSafePureFileName(saved_main_directory_path_, file_name_ext, |
304 kMaxFilePathLength, &pure_file_name)) | 313 kMaxFilePathLength, &pure_file_name)) |
305 return false; | 314 return false; |
306 | 315 |
307 FilePath::StringType file_name = pure_file_name + file_name_ext; | 316 FilePath::StringType file_name = pure_file_name + file_name_ext; |
308 | 317 |
309 // Check whether we already have same name. | 318 // Check whether we already have same name. |
310 if (file_name_set_.find(file_name) == file_name_set_.end()) { | 319 if (file_name_set_.find(file_name) == file_name_set_.end()) { |
(...skipping 670 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
981 file_util::ReplaceIllegalCharacters(&file_name, L' '); | 990 file_util::ReplaceIllegalCharacters(&file_name, L' '); |
982 TrimWhitespace(file_name, TRIM_ALL, &file_name); | 991 TrimWhitespace(file_name, TRIM_ALL, &file_name); |
983 FilePath suggest_name = FilePath::FromWStringHack(save_file_path.GetValue()); | 992 FilePath suggest_name = FilePath::FromWStringHack(save_file_path.GetValue()); |
984 suggest_name = suggest_name.Append(FilePath::FromWStringHack(file_name)); | 993 suggest_name = suggest_name.Append(FilePath::FromWStringHack(file_name)); |
985 | 994 |
986 return suggest_name; | 995 return suggest_name; |
987 } | 996 } |
988 | 997 |
989 FilePath SavePackage::EnsureHtmlExtension(const FilePath& name) { | 998 FilePath SavePackage::EnsureHtmlExtension(const FilePath& name) { |
990 // If the file name doesn't have an extension suitable for HTML files, | 999 // If the file name doesn't have an extension suitable for HTML files, |
991 // append ".htm". | 1000 // append one. |
992 FilePath::StringType ext = file_util::GetFileExtensionFromPath(name); | 1001 FilePath::StringType ext = file_util::GetFileExtensionFromPath(name); |
993 std::string mime_type; | 1002 std::string mime_type; |
994 if (!net::GetMimeTypeFromExtension(ext, &mime_type) || | 1003 if (!net::GetMimeTypeFromExtension(ext, &mime_type) || |
995 !CanSaveAsComplete(mime_type)) { | 1004 !CanSaveAsComplete(mime_type)) { |
996 return FilePath(name.value() + FILE_PATH_LITERAL(".htm")); | 1005 return FilePath(name.value() + FILE_PATH_LITERAL(".") + |
| 1006 kDefaultHtmlExtension); |
997 } | 1007 } |
998 return name; | 1008 return name; |
999 } | 1009 } |
1000 | 1010 |
1001 void SavePackage::GetSaveInfo() { | 1011 void SavePackage::GetSaveInfo() { |
1002 // Use "Web Page, Complete" option as default choice of saving page. | 1012 // Use "Web Page, Complete" option as default choice of saving page. |
1003 int file_type_index = 2; | 1013 int file_type_index = 2; |
1004 SelectFileDialog::FileTypeInfo file_type_info; | 1014 SelectFileDialog::FileTypeInfo file_type_info; |
1005 FilePath::StringType default_extension; | 1015 FilePath::StringType default_extension; |
1006 | 1016 |
(...skipping 15 matching lines...) Expand all Loading... |
1022 file_type_info.extensions.resize(2); | 1032 file_type_info.extensions.resize(2); |
1023 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("htm")); | 1033 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("htm")); |
1024 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html")); | 1034 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html")); |
1025 file_type_info.extension_description_overrides.push_back( | 1035 file_type_info.extension_description_overrides.push_back( |
1026 WideToUTF16(l10n_util::GetString(IDS_SAVE_PAGE_DESC_HTML_ONLY))); | 1036 WideToUTF16(l10n_util::GetString(IDS_SAVE_PAGE_DESC_HTML_ONLY))); |
1027 file_type_info.extensions[1].push_back(FILE_PATH_LITERAL("htm")); | 1037 file_type_info.extensions[1].push_back(FILE_PATH_LITERAL("htm")); |
1028 file_type_info.extensions[1].push_back(FILE_PATH_LITERAL("html")); | 1038 file_type_info.extensions[1].push_back(FILE_PATH_LITERAL("html")); |
1029 file_type_info.extension_description_overrides.push_back( | 1039 file_type_info.extension_description_overrides.push_back( |
1030 WideToUTF16(l10n_util::GetString(IDS_SAVE_PAGE_DESC_COMPLETE))); | 1040 WideToUTF16(l10n_util::GetString(IDS_SAVE_PAGE_DESC_COMPLETE))); |
1031 file_type_info.include_all_files = false; | 1041 file_type_info.include_all_files = false; |
1032 default_extension = FILE_PATH_LITERAL("htm"); | 1042 default_extension = kDefaultHtmlExtension; |
1033 } else { | 1043 } else { |
1034 file_type_info.extensions.resize(1); | 1044 file_type_info.extensions.resize(1); |
1035 file_type_info.extensions[0].push_back(suggested_path.Extension()); | 1045 file_type_info.extensions[0].push_back(suggested_path.Extension()); |
1036 if (!file_type_info.extensions[0][0].empty()) | 1046 if (!file_type_info.extensions[0][0].empty()) |
1037 file_type_info.extensions[0][0].erase(0, 1); // drop the . | 1047 file_type_info.extensions[0][0].erase(0, 1); // drop the . |
1038 file_type_info.include_all_files = true; | 1048 file_type_info.include_all_files = true; |
1039 file_type_index = 1; | 1049 file_type_index = 1; |
1040 } | 1050 } |
1041 | 1051 |
1042 if (g_should_prompt_for_filename) { | 1052 if (g_should_prompt_for_filename) { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1156 int index, void* params) { | 1166 int index, void* params) { |
1157 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); | 1167 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); |
1158 ContinueSave(save_params, path, index); | 1168 ContinueSave(save_params, path, index); |
1159 delete save_params; | 1169 delete save_params; |
1160 } | 1170 } |
1161 | 1171 |
1162 void SavePackage::FileSelectionCanceled(void* params) { | 1172 void SavePackage::FileSelectionCanceled(void* params) { |
1163 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); | 1173 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); |
1164 delete save_params; | 1174 delete save_params; |
1165 } | 1175 } |
OLD | NEW |