| 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 935 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 946 } else { | 946 } else { |
| 947 // No resource files need to be saved, treat it as user cancel. | 947 // No resource files need to be saved, treat it as user cancel. |
| 948 Cancel(true); | 948 Cancel(true); |
| 949 } | 949 } |
| 950 } | 950 } |
| 951 | 951 |
| 952 void SavePackage::SetShouldPromptUser(bool should_prompt) { | 952 void SavePackage::SetShouldPromptUser(bool should_prompt) { |
| 953 g_should_prompt_for_filename = should_prompt; | 953 g_should_prompt_for_filename = should_prompt; |
| 954 } | 954 } |
| 955 | 955 |
| 956 // static | 956 // static. |
| 957 FilePath SavePackage::GetSuggestNameForSaveAs( | 957 FilePath SavePackage::GetSuggestedNameForSaveAs(const FilePath& name, |
| 958 PrefService* prefs, const FilePath& name, bool can_save_as_complete) { | 958 bool can_save_as_complete) { |
| 959 // Check whether the preference has the preferred directory for saving file. | 959 // If the name is a URL, try to use the last path component or if there is |
| 960 // If not, initialize it with default directory. | 960 // none, the domain as the file name. |
| 961 if (!prefs->IsPrefRegistered(prefs::kSaveFileDefaultDirectory)) { | 961 FilePath name_with_proper_ext = name; |
| 962 FilePath default_save_path; | 962 GURL url(WideToUTF8(name_with_proper_ext.ToWStringHack())); |
| 963 if (!prefs->IsPrefRegistered(prefs::kDownloadDefaultDirectory)) { | 963 if (url.is_valid()) { |
| 964 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, | 964 std::string url_path; |
| 965 &default_save_path)) { | 965 std::vector<std::string> url_parts; |
| 966 NOTREACHED(); | 966 SplitString(url.path(), '/', &url_parts); |
| 967 if (!url_parts.empty()) { |
| 968 for (int i = static_cast<int>(url_parts.size()) - 1; i >= 0; --i) { |
| 969 url_path = url_parts[i]; |
| 970 if (!url_path.empty()) |
| 971 break; |
| 967 } | 972 } |
| 968 } else { | |
| 969 StringPrefMember default_download_path; | |
| 970 default_download_path.Init(prefs::kDownloadDefaultDirectory, | |
| 971 prefs, NULL); | |
| 972 default_save_path = FilePath::FromWStringHack( | |
| 973 default_download_path.GetValue()); | |
| 974 } | 973 } |
| 975 prefs->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory, | 974 if (url_path.empty()) |
| 976 default_save_path); | 975 url_path = url.host(); |
| 976 name_with_proper_ext = FilePath::FromWStringHack(UTF8ToWide(url_path)); |
| 977 } | 977 } |
| 978 | 978 |
| 979 // Get the directory from preference. | 979 // Ask user for getting final saving name. |
| 980 StringPrefMember save_file_path; | 980 if (can_save_as_complete) |
| 981 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs, NULL); | 981 name_with_proper_ext = EnsureHtmlExtension(name_with_proper_ext); |
| 982 DCHECK(!(*save_file_path).empty()); | |
| 983 | 982 |
| 984 // Ask user for getting final saving name. | |
| 985 FilePath name_with_proper_ext = | |
| 986 can_save_as_complete ? EnsureHtmlExtension(name) : name; | |
| 987 std::wstring file_name = name_with_proper_ext.ToWStringHack(); | 983 std::wstring file_name = name_with_proper_ext.ToWStringHack(); |
| 988 // TODO(port): we need a version of ReplaceIllegalCharacters() that takes | 984 // TODO(port): we need a version of ReplaceIllegalCharacters() that takes |
| 989 // FilePaths. | 985 // FilePaths. |
| 990 file_util::ReplaceIllegalCharacters(&file_name, L' '); | 986 file_util::ReplaceIllegalCharacters(&file_name, L' '); |
| 991 TrimWhitespace(file_name, TRIM_ALL, &file_name); | 987 TrimWhitespace(file_name, TRIM_ALL, &file_name); |
| 992 FilePath suggest_name = FilePath::FromWStringHack(save_file_path.GetValue()); | |
| 993 suggest_name = suggest_name.Append(FilePath::FromWStringHack(file_name)); | |
| 994 | 988 |
| 995 return suggest_name; | 989 return FilePath::FromWStringHack(file_name); |
| 996 } | 990 } |
| 997 | 991 |
| 998 FilePath SavePackage::EnsureHtmlExtension(const FilePath& name) { | 992 FilePath SavePackage::EnsureHtmlExtension(const FilePath& name) { |
| 999 // If the file name doesn't have an extension suitable for HTML files, | 993 // If the file name doesn't have an extension suitable for HTML files, |
| 1000 // append one. | 994 // append one. |
| 1001 FilePath::StringType ext = file_util::GetFileExtensionFromPath(name); | 995 FilePath::StringType ext = file_util::GetFileExtensionFromPath(name); |
| 1002 std::string mime_type; | 996 std::string mime_type; |
| 1003 if (!net::GetMimeTypeFromExtension(ext, &mime_type) || | 997 if (!net::GetMimeTypeFromExtension(ext, &mime_type) || |
| 1004 !CanSaveAsComplete(mime_type)) { | 998 !CanSaveAsComplete(mime_type)) { |
| 1005 return FilePath(name.value() + FILE_PATH_LITERAL(".") + | 999 return FilePath(name.value() + FILE_PATH_LITERAL(".") + |
| 1006 kDefaultHtmlExtension); | 1000 kDefaultHtmlExtension); |
| 1007 } | 1001 } |
| 1008 return name; | 1002 return name; |
| 1009 } | 1003 } |
| 1010 | 1004 |
| 1005 // static. |
| 1006 // Check whether the preference has the preferred directory for saving file. If |
| 1007 // not, initialize it with default directory. |
| 1008 FilePath SavePackage::GetSaveDirPreference(PrefService* prefs) { |
| 1009 DCHECK(prefs); |
| 1010 |
| 1011 if (!prefs->IsPrefRegistered(prefs::kSaveFileDefaultDirectory)) { |
| 1012 FilePath default_save_path; |
| 1013 if (!prefs->IsPrefRegistered(prefs::kDownloadDefaultDirectory)) { |
| 1014 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, |
| 1015 &default_save_path)) { |
| 1016 NOTREACHED(); |
| 1017 } |
| 1018 } else { |
| 1019 StringPrefMember default_download_path; |
| 1020 default_download_path.Init(prefs::kDownloadDefaultDirectory, |
| 1021 prefs, NULL); |
| 1022 default_save_path = |
| 1023 FilePath::FromWStringHack(default_download_path.GetValue()); |
| 1024 } |
| 1025 prefs->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory, |
| 1026 default_save_path); |
| 1027 } |
| 1028 |
| 1029 // Get the directory from preference. |
| 1030 StringPrefMember save_file_path; |
| 1031 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs, NULL); |
| 1032 DCHECK(!(*save_file_path).empty()); |
| 1033 |
| 1034 return FilePath::FromWStringHack(*save_file_path); |
| 1035 } |
| 1036 |
| 1011 void SavePackage::GetSaveInfo() { | 1037 void SavePackage::GetSaveInfo() { |
| 1012 // Use "Web Page, Complete" option as default choice of saving page. | 1038 // Use "Web Page, Complete" option as default choice of saving page. |
| 1013 int file_type_index = 2; | 1039 int file_type_index = 2; |
| 1014 SelectFileDialog::FileTypeInfo file_type_info; | 1040 SelectFileDialog::FileTypeInfo file_type_info; |
| 1015 FilePath::StringType default_extension; | 1041 FilePath::StringType default_extension; |
| 1016 | 1042 |
| 1017 SavePackageParam* save_params = | 1043 SavePackageParam* save_params = |
| 1018 new SavePackageParam(tab_contents_->contents_mime_type()); | 1044 new SavePackageParam(tab_contents_->contents_mime_type()); |
| 1019 | 1045 |
| 1020 bool can_save_as_complete = | 1046 bool can_save_as_complete = |
| 1021 CanSaveAsComplete(save_params->current_tab_mime_type); | 1047 CanSaveAsComplete(save_params->current_tab_mime_type); |
| 1022 | 1048 |
| 1023 FilePath title = | 1049 FilePath title = |
| 1024 FilePath::FromWStringHack(UTF16ToWideHack(tab_contents_->GetTitle())); | 1050 FilePath::FromWStringHack(UTF16ToWideHack(tab_contents_->GetTitle())); |
| 1051 FilePath save_dir = |
| 1052 GetSaveDirPreference(tab_contents_->profile()->GetPrefs()); |
| 1025 FilePath suggested_path = | 1053 FilePath suggested_path = |
| 1026 GetSuggestNameForSaveAs(tab_contents_->profile()->GetPrefs(), title, | 1054 save_dir.Append(GetSuggestedNameForSaveAs(title, can_save_as_complete)); |
| 1027 can_save_as_complete); | |
| 1028 | 1055 |
| 1029 // If the contents can not be saved as complete-HTML, do not show the | 1056 // If the contents can not be saved as complete-HTML, do not show the |
| 1030 // file filters. | 1057 // file filters. |
| 1031 if (can_save_as_complete) { | 1058 if (can_save_as_complete) { |
| 1032 file_type_info.extensions.resize(2); | 1059 file_type_info.extensions.resize(2); |
| 1033 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("htm")); | 1060 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("htm")); |
| 1034 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html")); | 1061 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html")); |
| 1035 file_type_info.extension_description_overrides.push_back( | 1062 file_type_info.extension_description_overrides.push_back( |
| 1036 WideToUTF16(l10n_util::GetString(IDS_SAVE_PAGE_DESC_HTML_ONLY))); | 1063 WideToUTF16(l10n_util::GetString(IDS_SAVE_PAGE_DESC_HTML_ONLY))); |
| 1037 file_type_info.extensions[1].push_back(FILE_PATH_LITERAL("htm")); | 1064 file_type_info.extensions[1].push_back(FILE_PATH_LITERAL("htm")); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1166 int index, void* params) { | 1193 int index, void* params) { |
| 1167 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); | 1194 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); |
| 1168 ContinueSave(save_params, path, index); | 1195 ContinueSave(save_params, path, index); |
| 1169 delete save_params; | 1196 delete save_params; |
| 1170 } | 1197 } |
| 1171 | 1198 |
| 1172 void SavePackage::FileSelectionCanceled(void* params) { | 1199 void SavePackage::FileSelectionCanceled(void* params) { |
| 1173 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); | 1200 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); |
| 1174 delete save_params; | 1201 delete save_params; |
| 1175 } | 1202 } |
| OLD | NEW |