summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download/save_package.cc
diff options
context:
space:
mode:
authorgeorgey@chromium.org <georgey@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-23 23:54:47 +0000
committergeorgey@chromium.org <georgey@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-23 23:54:47 +0000
commit40878e40b0574f7e7390f90da24092a11c4a40df (patch)
treea2434e129fab88ba439654d1f860fe13d03a60f4 /chrome/browser/download/save_package.cc
parent0cac676933f11f6f8a32c9e8aa1d082da211957e (diff)
downloadchromium_src-40878e40b0574f7e7390f90da24092a11c4a40df.zip
chromium_src-40878e40b0574f7e7390f90da24092a11c4a40df.tar.gz
chromium_src-40878e40b0574f7e7390f90da24092a11c4a40df.tar.bz2
Fixes Issue 34722: Meaningless value in 'Save as type' combobox of 'Save page as...' dialog
Adds extension base don MIME type of the page. Adds capability of the complete save of the "application/xhtml+xml" pages BUG=34722 TEST=In the bug + try pages with other MIME types, such as "text/xml", "text/plain" or "text/css" Review URL: http://codereview.chromium.org/650176 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@39804 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/download/save_package.cc')
-rw-r--r--chrome/browser/download/save_package.cc71
1 files changed, 68 insertions, 3 deletions
diff --git a/chrome/browser/download/save_package.cc b/chrome/browser/download/save_package.cc
index 36b090f..a124b3e 100644
--- a/chrome/browser/download/save_package.cc
+++ b/chrome/browser/download/save_package.cc
@@ -1024,7 +1024,8 @@ void SavePackage::SetShouldPromptUser(bool should_prompt) {
// static.
FilePath SavePackage::GetSuggestedNameForSaveAs(const FilePath& name,
- bool can_save_as_complete) {
+ bool can_save_as_complete,
+ const FilePath::StringType& contents_mime_type) {
// If the name is a URL, try to use the last path component or if there is
// none, the domain as the file name.
FilePath name_with_proper_ext = name;
@@ -1046,6 +1047,9 @@ FilePath SavePackage::GetSuggestedNameForSaveAs(const FilePath& name,
}
// Ask user for getting final saving name.
+ name_with_proper_ext = EnsureMimeExtension(name_with_proper_ext,
+ contents_mime_type);
+ // Adjust extension for complete types.
if (can_save_as_complete)
name_with_proper_ext = EnsureHtmlExtension(name_with_proper_ext);
@@ -1067,6 +1071,45 @@ FilePath SavePackage::EnsureHtmlExtension(const FilePath& name) {
return name;
}
+FilePath SavePackage::EnsureMimeExtension(const FilePath& name,
+ const FilePath::StringType& contents_mime_type) {
+ // Start extension at 1 to skip over period if non-empty.
+ FilePath::StringType ext = name.Extension().length() ?
+ name.Extension().substr(1) : name.Extension();
+ FilePath::StringType suggested_extension =
+ ExtensionForMimeType(contents_mime_type);
+ std::string mime_type;
+ if (!suggested_extension.empty() &&
+ (!net::GetMimeTypeFromExtension(ext, &mime_type) ||
+ !IsSavableContents(mime_type))) {
+ // Extension is absent or needs to be updated.
+ return FilePath(name.value() + FILE_PATH_LITERAL(".") +
+ suggested_extension);
+ }
+ return name;
+}
+
+const FilePath::CharType *SavePackage::ExtensionForMimeType(
+ const FilePath::StringType& contents_mime_type) {
+ static const struct {
+ const FilePath::CharType *mime_type;
+ const FilePath::CharType *suggested_extension;
+ } extensions[] = {
+ { FILE_PATH_LITERAL("text/html"), kDefaultHtmlExtension },
+ { FILE_PATH_LITERAL("text/xml"), FILE_PATH_LITERAL("xml") },
+ { FILE_PATH_LITERAL("application/xhtml+xml"), FILE_PATH_LITERAL("xhtml") },
+ { FILE_PATH_LITERAL("text/plain"), FILE_PATH_LITERAL("txt") },
+ { FILE_PATH_LITERAL("text/css"), FILE_PATH_LITERAL("css") },
+ };
+ for (uint32 i = 0; i < ARRAYSIZE_UNSAFE(extensions); ++i) {
+ if (contents_mime_type == extensions[i].mime_type)
+ return extensions[i].suggested_extension;
+ }
+ return FILE_PATH_LITERAL("");
+}
+
+
+
// static.
// Check whether the preference has the preferred directory for saving file. If
// not, initialize it with default directory.
@@ -1119,19 +1162,40 @@ void SavePackage::ContinueGetSaveInfo(FilePath save_dir) {
FilePath title =
FilePath::FromWStringHack(UTF16ToWideHack(tab_contents_->GetTitle()));
+
+#if defined(OS_POSIX)
+ FilePath::StringType mime_type(save_params->current_tab_mime_type);
+#elif defined(OS_WIN)
+ FilePath::StringType mime_type(
+ UTF8ToWide(save_params->current_tab_mime_type));
+#endif // OS_WIN
+
FilePath suggested_path =
- save_dir.Append(GetSuggestedNameForSaveAs(title, can_save_as_complete));
+ save_dir.Append(GetSuggestedNameForSaveAs(title, can_save_as_complete,
+ mime_type));
// If the contents can not be saved as complete-HTML, do not show the
// file filters.
if (can_save_as_complete) {
+ bool add_extra_extension = false;
+ FilePath::StringType extra_extension;
+ if (!suggested_path.Extension().empty() &&
+ suggested_path.Extension().compare(FILE_PATH_LITERAL("htm")) &&
+ suggested_path.Extension().compare(FILE_PATH_LITERAL("html"))) {
+ add_extra_extension = true;
+ extra_extension = suggested_path.Extension().substr(1);
+ }
file_type_info.extensions.resize(2);
file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("htm"));
file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html"));
+ if (add_extra_extension)
+ file_type_info.extensions[0].push_back(extra_extension);
file_type_info.extension_description_overrides.push_back(
WideToUTF16(l10n_util::GetString(IDS_SAVE_PAGE_DESC_HTML_ONLY)));
file_type_info.extensions[1].push_back(FILE_PATH_LITERAL("htm"));
file_type_info.extensions[1].push_back(FILE_PATH_LITERAL("html"));
+ if (add_extra_extension)
+ file_type_info.extensions[1].push_back(extra_extension);
file_type_info.extension_description_overrides.push_back(
WideToUTF16(l10n_util::GetString(IDS_SAVE_PAGE_DESC_COMPLETE)));
file_type_info.include_all_files = false;
@@ -1228,7 +1292,8 @@ bool SavePackage::IsSavableContents(const std::string& contents_mime_type) {
// Static
bool SavePackage::CanSaveAsComplete(const std::string& contents_mime_type) {
- return contents_mime_type == "text/html";
+ return contents_mime_type == "text/html" ||
+ contents_mime_type == "application/xhtml+xml";
}
// Static