Commit bd52a8a1 authored by halliwell's avatar halliwell Committed by Commit bot

Generalizing conditional compilation logic for systems with native utf8 locale.

This change was needed for Chromecast, while avoiding adding yet another
preprocessor condition.

BUG=438331

Review URL: https://codereview.chromium.org/790903003

Cr-Commit-Position: refs/heads/master@{#308109}
parent 9167e642
......@@ -773,6 +773,12 @@ component("base") {
"//third_party/modp_b64",
]
# Allow more direct string conversions on platforms with native utf8
# strings
if (is_mac || is_ios || is_chromeos) {
defines += [ "SYSTEM_NATIVE_UTF8" ]
}
if (is_android) {
sources += [
"memory/discardable_memory_ashmem_allocator.cc",
......
......@@ -981,6 +981,11 @@
['OS == "win" and >(nacl_untrusted_build)==1', {
'sources/': [ ['exclude', '\\.h$'] ],
}],
# Enable more direct string conversions on platforms with native utf8
# strings
['OS=="mac" or OS=="ios" or <(chromeos)==1 or <(chromecast)==1', {
'defines': ['SYSTEM_NATIVE_UTF8'],
}],
],
}],
['base_i18n_target==1', {
......
......@@ -593,7 +593,7 @@ std::string FilePath::MaybeAsASCII() const {
}
std::string FilePath::AsUTF8Unsafe() const {
#if defined(OS_MACOSX) || defined(OS_CHROMEOS)
#if defined(SYSTEM_NATIVE_UTF8)
return value();
#else
return WideToUTF8(SysNativeMBToWide(value()));
......@@ -601,7 +601,7 @@ std::string FilePath::AsUTF8Unsafe() const {
}
string16 FilePath::AsUTF16Unsafe() const {
#if defined(OS_MACOSX) || defined(OS_CHROMEOS)
#if defined(SYSTEM_NATIVE_UTF8)
return UTF8ToUTF16(value());
#else
return WideToUTF16(SysNativeMBToWide(value()));
......@@ -610,7 +610,7 @@ string16 FilePath::AsUTF16Unsafe() const {
// static
FilePath FilePath::FromUTF8Unsafe(const std::string& utf8) {
#if defined(OS_MACOSX) || defined(OS_CHROMEOS)
#if defined(SYSTEM_NATIVE_UTF8)
return FilePath(utf8);
#else
return FilePath(SysWideToNativeMB(UTF8ToWide(utf8)));
......@@ -619,7 +619,7 @@ FilePath FilePath::FromUTF8Unsafe(const std::string& utf8) {
// static
FilePath FilePath::FromUTF16Unsafe(const string16& utf16) {
#if defined(OS_MACOSX) || defined(OS_CHROMEOS)
#if defined(SYSTEM_NATIVE_UTF8)
return FilePath(UTF16ToUTF8(utf16));
#else
return FilePath(SysWideToNativeMB(UTF16ToWide(utf16)));
......
......@@ -24,11 +24,10 @@ std::wstring SysUTF8ToWide(const StringPiece& utf8) {
return out;
}
#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
#if defined(SYSTEM_NATIVE_UTF8) || defined(OS_ANDROID)
// TODO(port): Consider reverting the OS_ANDROID when we have wcrtomb()
// support and a better understanding of what calls these routines.
// ChromeOS always runs in UTF-8 locale.
std::string SysWideToNativeMB(const std::wstring& wide) {
return WideToUTF8(wide);
}
......
......@@ -48,6 +48,25 @@ base::FilePath WStringAsFilePath(const std::wstring& str) {
#endif
}
std::string GetLocaleWarningString() {
#if defined(OS_POSIX) && !defined(OS_ANDROID)
// The generate filename tests can fail on certain OS_POSIX platforms when
// LC_CTYPE is not "utf8" or "utf-8" because some of the string conversions
// fail.
// This warning text is appended to any test failures to save people time if
// this happens to be the cause of failure :)
// Note: some platforms (MACOSX, Chromecast) don't have this problem:
// setlocale returns "c" but it functions as utf8. And Android doesn't
// have setlocale at all.
std::string locale = setlocale(LC_CTYPE, NULL);
return " this test may have failed because the current LC_CTYPE locale is "
"not utf8 (currently set to " +
locale + ")";
#else
return "";
#endif
}
void RunGenerateFileNameTestCase(const GenerateFilenameCase* test_case) {
std::string default_filename(base::WideToUTF8(test_case->default_filename));
base::FilePath file_path = GenerateFileName(
......@@ -55,7 +74,8 @@ void RunGenerateFileNameTestCase(const GenerateFilenameCase* test_case) {
test_case->referrer_charset, test_case->suggested_filename,
test_case->mime_type, default_filename);
EXPECT_EQ(test_case->expected_filename, FilePathAsWString(file_path))
<< "test case at line number: " << test_case->lineno;
<< "test case at line number: " << test_case->lineno << "; "
<< GetLocaleWarningString();
}
} // namespace
......@@ -418,18 +438,6 @@ TEST(FilenameUtilTest, GenerateSafeFileName) {
}
TEST(FilenameUtilTest, GenerateFileName) {
#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
// This test doesn't run when the locale is not UTF-8 because some of the
// string conversions fail. This is OK (we have the default value) but they
// don't match our expectations.
std::string locale = setlocale(LC_CTYPE, NULL);
base::StringToLowerASCII(&locale);
EXPECT_TRUE(locale.find("utf-8") != std::string::npos ||
locale.find("utf8") != std::string::npos)
<< "Your locale (" << locale << ") must be set to UTF-8 "
<< "for this test to pass!";
#endif
// Tests whether the correct filename is selected from the the given
// parameters and that Content-Disposition headers are properly
// handled including failovers when the header is malformed.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment