diff options
author | tony@chromium.org <tony@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-04 18:37:31 +0000 |
---|---|---|
committer | tony@chromium.org <tony@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-04 18:37:31 +0000 |
commit | 630947caffc803a4cc96e3754af45aeedc957c0c (patch) | |
tree | 3950c0a479831c51a008a4ef210829a041498f33 /net | |
parent | 055aedeb2134e49c10c3b3a3593cb8df768d737c (diff) | |
download | chromium_src-630947caffc803a4cc96e3754af45aeedc957c0c.zip chromium_src-630947caffc803a4cc96e3754af45aeedc957c0c.tar.gz chromium_src-630947caffc803a4cc96e3754af45aeedc957c0c.tar.bz2 |
Enable localization of default downloaded filename.
Instead of localizing "download" string in net_util.cc, make a caller,
download_manger, provide a localized string.
BUG=25289
TEST=NetUtilTest.GetSuggestedFilename,DownloadManagerTest.TestDownloadFilename
Original patch by hayato@google.com at:
http://codereview.chromium.org/343014/show
Review URL: http://codereview.chromium.org/367003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30971 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/net_util.cc | 59 | ||||
-rw-r--r-- | net/base/net_util.h | 6 | ||||
-rw-r--r-- | net/base/net_util_unittest.cc | 76 |
3 files changed, 82 insertions, 59 deletions
diff --git a/net/base/net_util.cc b/net/base/net_util.cc index 9715aa0e..85151e9 100644 --- a/net/base/net_util.cc +++ b/net/base/net_util.cc @@ -1042,69 +1042,74 @@ std::wstring StripWWW(const std::wstring& text) { FilePath GetSuggestedFilename(const GURL& url, const std::string& content_disposition, const std::string& referrer_charset, - const char* default_name) { - // TODO(rolandsteiner): as pointed out by darin in the code review, this is - // hardly ideal. "download" should be translated, or another solution found. - // (cf. http://code.google.com/p/chromium/issues/detail?id=25289) - const char kFinalFallbackName[] = "download"; + const FilePath& default_name) { + // We don't translate this fallback string, "download". If localization is + // needed, the caller should provide localized fallback default_name. + static const FilePath::CharType kFinalFallbackName[] = + FILE_PATH_LITERAL("download"); // about: and data: URLs don't have file names, but esp. data: URLs may // contain parts that look like ones (i.e., contain a slash). // Therefore we don't attempt to divine a file name out of them. if (url.SchemeIs("about") || url.SchemeIs("data")) { - return FilePath(UTF8ToFilePathString( - default_name && default_name[0] ? default_name : kFinalFallbackName)); + return default_name.empty() ? FilePath(kFinalFallbackName) : default_name; } - std::string filename = GetFileNameFromCD(content_disposition, - referrer_charset); + const std::string filename_from_cd = GetFileNameFromCD(content_disposition, + referrer_charset); +#if defined(OS_WIN) + FilePath::StringType filename = UTF8ToWide(filename_from_cd); +#elif defined(OS_POSIX) + FilePath::StringType filename = filename_from_cd; +#endif + if (!filename.empty()) { // Remove any path information the server may have sent, take the name // only. -#if defined(OS_WIN) - filename = UTF16ToUTF8(FilePath(UTF8ToUTF16(filename)).BaseName().value()); -#else filename = FilePath(filename).BaseName().value(); -#endif // Next, remove "." from the beginning and end of the file name to avoid // tricks with hidden files, "..", and "." - TrimString(filename, ".", &filename); + TrimString(filename, FILE_PATH_LITERAL("."), &filename); } if (filename.empty()) { if (url.is_valid()) { - filename = UnescapeURLComponent( + const std::string unescaped_url_filename = UnescapeURLComponent( url.ExtractFileName(), UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); +#if defined(OS_WIN) + filename = UTF8ToWide(unescaped_url_filename); +#elif defined(OS_POSIX) + filename = unescaped_url_filename; +#endif } } // Trim '.' once more. - TrimString(filename, ".", &filename); + TrimString(filename, FILE_PATH_LITERAL("."), &filename); // If there's no filename or it gets trimed to be empty, use // the URL hostname or default_name if (filename.empty()) { - if (default_name && default_name[0]) { - filename = default_name; + if (!default_name.empty()) { + filename = default_name.value(); } else if (url.is_valid()) { // Some schemes (e.g. file) do not have a hostname. Even though it's // not likely to reach here, let's hardcode the last fallback name. // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) - filename = url.host().empty() ? std::string(kFinalFallbackName) - : url.host(); + filename = url.host().empty() ? kFinalFallbackName : +#if defined(OS_WIN) + UTF8ToWide(url.host()); +#elif defined(OS_POSIX) + url.host(); +#endif } else { NOTREACHED(); } } -#if defined(OS_WIN) - FilePath::StringType file_path_string = UTF8ToWide(filename); -#else - std::string& file_path_string = filename; -#endif - file_util::ReplaceIllegalCharactersInPath(&file_path_string, '-'); - return FilePath(file_path_string); + file_util::ReplaceIllegalCharactersInPath(&filename, '-'); + return FilePath(filename); } bool IsPortAllowedByDefault(int port) { diff --git a/net/base/net_util.h b/net/base/net_util.h index 9ef4933..1f1516f 100644 --- a/net/base/net_util.h +++ b/net/base/net_util.h @@ -196,8 +196,8 @@ std::wstring StripWWW(const std::wstring& text); // Gets the filename from the raw Content-Disposition header (as read from the // network). Otherwise uses the last path component name or hostname from -// |url|. If there is no filename or it can't be used, the given default name -// will be used if specified. +// |url|. If there is no filename or it can't be used, the given |default_name|, +// will be used unless it is empty. // Note: it's possible for the suggested filename to be empty (e.g., // file:///). referrer_charset is used as one of charsets @@ -206,7 +206,7 @@ std::wstring StripWWW(const std::wstring& text); FilePath GetSuggestedFilename(const GURL& url, const std::string& content_disposition, const std::string& referrer_charset, - const char* default_name); + const FilePath& default_name); // Checks the given port against a list of ports which are restricted by // default. Returns true if the port is allowed, false if it is restricted. diff --git a/net/base/net_util_unittest.cc b/net/base/net_util_unittest.cc index 1445e43..07ec17c 100644 --- a/net/base/net_util_unittest.cc +++ b/net/base/net_util_unittest.cc @@ -5,6 +5,7 @@ #include "base/basictypes.h" #include "base/file_path.h" #include "base/string_util.h" +#include "base/sys_string_conversions.h" #include "base/time.h" #include "googleurl/src/gurl.h" #include "net/base/escape.h" @@ -353,7 +354,7 @@ struct SuggestedFilenameCase { const char* url; const char* content_disp_header; const char* referrer_charset; - const char* default_filename; + const wchar_t* default_filename; const wchar_t* expected_filename; }; @@ -856,32 +857,32 @@ TEST(NetUtilTest, GetSuggestedFilename) { {"http://www.google.com/", "Content-disposition: attachment; filename=test.html", "", - "", + L"", L"test.html"}, {"http://www.google.com/", "Content-disposition: attachment; filename=\"test.html\"", "", - "", + L"", L"test.html"}, {"http://www.google.com/path/test.html", "Content-disposition: attachment", "", - "", + L"", L"test.html"}, {"http://www.google.com/path/test.html", "Content-disposition: attachment;", "", - "", + L"", L"test.html"}, {"http://www.google.com/", "", "", - "", + L"", L"www.google.com"}, {"http://www.google.com/test.html", "", "", - "", + L"", L"test.html"}, // Now that we use googleurl's ExtractFileName, this case falls back // to the hostname. If this behavior is not desirable, we'd better @@ -889,117 +890,134 @@ TEST(NetUtilTest, GetSuggestedFilename) { {"http://www.google.com/path/", "", "", - "", + L"", L"www.google.com"}, {"http://www.google.com/path", "", "", - "", + L"", L"path"}, {"file:///", "", "", - "", + L"", L"download"}, {"non-standard-scheme:", "", "", - "", + L"", L"download"}, {"http://www.google.com/", "Content-disposition: attachment; filename =\"test.html\"", "", - "download", + L"download", L"test.html"}, {"http://www.google.com/", "", "", - "download", + L"download", L"download"}, {"http://www.google.com/", "Content-disposition: attachment; filename=\"../test.html\"", "", - "", + L"", L"test.html"}, {"http://www.google.com/", "Content-disposition: attachment; filename=\"..\"", "", - "download", + L"download", L"download"}, {"http://www.google.com/test.html", "Content-disposition: attachment; filename=\"..\"", "", - "download", + L"download", L"test.html"}, // Below is a small subset of cases taken from GetFileNameFromCD test above. {"http://www.google.com/", "Content-Disposition: attachment; filename=\"%EC%98%88%EC%88%A0%20" "%EC%98%88%EC%88%A0.jpg\"", "", - "", + L"", L"\uc608\uc220 \uc608\uc220.jpg"}, {"http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", "", "", - "download", + L"download", L"\uc608\uc220 \uc608\uc220.jpg"}, {"http://www.google.com/", "Content-disposition: attachment;", "", - "\xEB\x8B\xA4\xEC\x9A\xB4\xEB\xA1\x9C\xEB\x93\x9C", + L"\uB2E4\uC6B4\uB85C\uB4DC", L"\uB2E4\uC6B4\uB85C\uB4DC"}, {"http://www.google.com/", "Content-Disposition: attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD=" "D13=2Epng?=\"", "", - "download", + L"download", L"\u82b8\u88533.png"}, {"http://www.example.com/images?id=3", "Content-Disposition: attachment; filename=caf\xc3\xa9.png", "iso-8859-1", - "", + L"", L"caf\u00e9.png"}, {"http://www.example.com/images?id=3", "Content-Disposition: attachment; filename=caf\xe5.png", "windows-1253", - "", + L"", L"caf\u03b5.png"}, {"http://www.example.com/file?id=3", "Content-Disposition: attachment; name=\xcf\xc2\xd4\xd8.zip", "GBK", - "", + L"", L"\u4e0b\u8f7d.zip"}, // Invalid C-D header. Extracts filename from url. {"http://www.google.com/test.html", "Content-Disposition: attachment; filename==?iiso88591?Q?caf=EG?=", "", - "", + L"", L"test.html"}, // about: and data: URLs {"about:chrome", "", "", - "", + L"", L"download"}, {"data:,looks/like/a.path", "", "", - "", + L"", L"download"}, {"data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=", "", "", - "", + L"", L"download"}, + {"data:,looks/like/a.path", + "", + "", + L"default_filename_is_given", + L"default_filename_is_given"}, + {"data:,looks/like/a.path", + "", + "", + L"\u65e5\u672c\u8a9e", // Japanese Kanji. + L"\u65e5\u672c\u8a9e"}, }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { +#if defined(OS_WIN) + FilePath default_name(test_cases[i].default_filename); +#else + FilePath default_name( + base::SysWideToNativeMB(test_cases[i].default_filename)); +#endif FilePath filename = net::GetSuggestedFilename( GURL(test_cases[i].url), test_cases[i].content_disp_header, - test_cases[i].referrer_charset, test_cases[i].default_filename); + test_cases[i].referrer_charset, default_name); #if defined(OS_WIN) EXPECT_EQ(std::wstring(test_cases[i].expected_filename), filename.value()) #else - EXPECT_EQ(WideToUTF8(test_cases[i].expected_filename), filename.value()) + EXPECT_EQ(base::SysWideToNativeMB(test_cases[i].expected_filename), + filename.value()) #endif << "Iteration " << i << ": " << test_cases[i].url; } |