Yafei Duan | 69ddcfd9 | 2018-02-02 00:03:42 | [diff] [blame^] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/filename_generation/filename_generation.h" |
| 6 | |
| 7 | #include "base/files/file_path.h" |
| 8 | #include "base/files/file_util.h" |
| 9 | #include "base/i18n/file_util_icu.h" |
| 10 | #include "base/logging.h" |
| 11 | #include "base/strings/string_util.h" |
| 12 | #include "base/strings/sys_string_conversions.h" |
| 13 | #include "base/strings/utf_string_conversions.h" |
| 14 | #include "base/threading/thread_restrictions.h" |
| 15 | #include "components/url_formatter/url_formatter.h" |
| 16 | #include "net/base/filename_util.h" |
| 17 | #include "net/base/mime_util.h" |
| 18 | #include "url/gurl.h" |
| 19 | |
| 20 | namespace filename_generation { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | const base::FilePath::CharType kDefaultHtmlExtension[] = |
| 25 | FILE_PATH_LITERAL("html"); |
| 26 | |
| 27 | // Check whether we can save page as complete-HTML for the contents which |
| 28 | // have specified a MIME type. Now only contents which have the MIME type |
| 29 | // "text/html" can be saved as complete-HTML. |
| 30 | bool CanSaveAsComplete(const std::string& contents_mime_type) { |
| 31 | return contents_mime_type == "text/html" || |
| 32 | contents_mime_type == "application/xhtml+xml"; |
| 33 | } |
| 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | const base::FilePath::CharType* ExtensionForMimeType( |
| 38 | const std::string& contents_mime_type) { |
| 39 | static const struct { |
| 40 | const char* mime_type; |
| 41 | const base::FilePath::CharType* suggested_extension; |
| 42 | } kExtensions[] = { |
| 43 | {"text/html", kDefaultHtmlExtension}, |
| 44 | {"text/xml", FILE_PATH_LITERAL("xml")}, |
| 45 | {"application/xhtml+xml", FILE_PATH_LITERAL("xhtml")}, |
| 46 | {"text/plain", FILE_PATH_LITERAL("txt")}, |
| 47 | {"text/css", FILE_PATH_LITERAL("css")}, |
| 48 | }; |
| 49 | for (const auto& extension : kExtensions) { |
| 50 | if (contents_mime_type == extension.mime_type) |
| 51 | return extension.suggested_extension; |
| 52 | } |
| 53 | return FILE_PATH_LITERAL(""); |
| 54 | } |
| 55 | |
| 56 | base::FilePath EnsureHtmlExtension(const base::FilePath& name) { |
| 57 | base::AssertBlockingAllowed(); |
| 58 | |
| 59 | base::FilePath::StringType ext = name.Extension(); |
| 60 | if (!ext.empty()) |
| 61 | ext.erase(ext.begin()); // Erase preceding '.'. |
| 62 | std::string mime_type; |
| 63 | if (!net::GetMimeTypeFromExtension(ext, &mime_type) || |
| 64 | !CanSaveAsComplete(mime_type)) { |
| 65 | return base::FilePath(name.value() + FILE_PATH_LITERAL(".") + |
| 66 | kDefaultHtmlExtension); |
| 67 | } |
| 68 | return name; |
| 69 | } |
| 70 | |
| 71 | base::FilePath EnsureMimeExtension(const base::FilePath& name, |
| 72 | const std::string& contents_mime_type) { |
| 73 | base::AssertBlockingAllowed(); |
| 74 | |
| 75 | // Start extension at 1 to skip over period if non-empty. |
| 76 | base::FilePath::StringType ext = name.Extension(); |
| 77 | if (!ext.empty()) |
| 78 | ext = ext.substr(1); |
| 79 | base::FilePath::StringType suggested_extension = |
| 80 | ExtensionForMimeType(contents_mime_type); |
| 81 | std::string mime_type; |
| 82 | if (!suggested_extension.empty() && |
| 83 | !net::GetMimeTypeFromExtension(ext, &mime_type)) { |
| 84 | // Extension is absent or needs to be updated. |
| 85 | return base::FilePath(name.value() + FILE_PATH_LITERAL(".") + |
| 86 | suggested_extension); |
| 87 | } |
| 88 | return name; |
| 89 | } |
| 90 | |
| 91 | base::FilePath GenerateFilename(const base::string16& title, |
| 92 | const GURL& url, |
| 93 | bool can_save_as_complete, |
| 94 | std::string contents_mime_type) { |
| 95 | base::FilePath name_with_proper_ext = base::FilePath::FromUTF16Unsafe(title); |
| 96 | |
| 97 | // If the page's title matches its URL, use the URL. Try to use the last path |
| 98 | // component or if there is none, the domain as the file name. |
| 99 | // Normally we want to base the filename on the page title, or if it doesn't |
| 100 | // exist, on the URL. It's not easy to tell if the page has no title, because |
| 101 | // if the page has no title, WebContents::GetTitle() will return the page's |
| 102 | // URL (adjusted for display purposes). Therefore, we convert the "title" |
| 103 | // back to a URL, and if it matches the original page URL, we know the page |
| 104 | // had no title (or had a title equal to its URL, which is fine to treat |
| 105 | // similarly). |
| 106 | if (title == url_formatter::FormatUrl(url)) { |
| 107 | std::string url_path; |
| 108 | if (!url.SchemeIs(url::kDataScheme)) { |
| 109 | name_with_proper_ext = net::GenerateFileName( |
| 110 | url, std::string(), std::string(), std::string(), contents_mime_type, |
| 111 | std::string()); |
| 112 | |
| 113 | // If host is used as file name, try to decode punycode. |
| 114 | if (name_with_proper_ext.AsUTF8Unsafe() == url.host()) { |
| 115 | name_with_proper_ext = base::FilePath::FromUTF16Unsafe( |
| 116 | url_formatter::IDNToUnicode(url.host())); |
| 117 | } |
| 118 | } else { |
| 119 | name_with_proper_ext = base::FilePath::FromUTF8Unsafe("dataurl"); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Ask user for getting final saving name. |
| 124 | name_with_proper_ext = |
| 125 | EnsureMimeExtension(name_with_proper_ext, contents_mime_type); |
| 126 | // Adjust extension for complete types. |
| 127 | if (can_save_as_complete) |
| 128 | name_with_proper_ext = EnsureHtmlExtension(name_with_proper_ext); |
| 129 | |
| 130 | base::FilePath::StringType file_name = name_with_proper_ext.value(); |
| 131 | base::i18n::ReplaceIllegalCharactersInPath(&file_name, '_'); |
| 132 | return base::FilePath(file_name); |
| 133 | } |
| 134 | |
| 135 | } // namespace filename_generation |