Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/i18n/icu_util.h" | 5 #include "base/i18n/icu_util.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/debug/alias.h" | 13 #include "base/debug/alias.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/files/memory_mapped_file.h" | 15 #include "base/files/memory_mapped_file.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
| 20 #include "third_party/icu/source/common/unicode/putil.h" | 20 #include "third_party/icu/source/common/unicode/putil.h" |
| 21 #include "third_party/icu/source/common/unicode/udata.h" | 21 #include "third_party/icu/source/common/unicode/udata.h" |
| 22 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 22 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 23 #include "third_party/icu/source/i18n/unicode/timezone.h" | 23 #include "third_party/icu/source/i18n/unicode/timezone.h" |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 #if defined(OS_ANDROID) | |
| 27 #include "base/android/apk_assets.h" | |
| 28 #endif | |
| 29 | |
| 26 #if defined(OS_MACOSX) | 30 #if defined(OS_MACOSX) |
| 27 #include "base/mac/foundation_util.h" | 31 #include "base/mac/foundation_util.h" |
| 28 #endif | 32 #endif |
| 29 | 33 |
| 30 #define ICU_UTIL_DATA_FILE 0 | 34 #define ICU_UTIL_DATA_FILE 0 |
| 31 #define ICU_UTIL_DATA_SHARED 1 | 35 #define ICU_UTIL_DATA_SHARED 1 |
| 32 #define ICU_UTIL_DATA_STATIC 2 | 36 #define ICU_UTIL_DATA_STATIC 2 |
| 33 | 37 |
| 34 namespace base { | 38 namespace base { |
| 35 namespace i18n { | 39 namespace i18n { |
| 36 | 40 |
| 37 // Use an unversioned file name to simplify a icu version update down the road. | 41 // Use an unversioned file name to simplify a icu version update down the road. |
| 38 // No need to change the filename in multiple places (gyp files, windows | 42 // No need to change the filename in multiple places (gyp files, windows |
| 39 // build pkg configurations, etc). 'l' stands for Little Endian. | 43 // build pkg configurations, etc). 'l' stands for Little Endian. |
| 40 // This variable is exported through the header file. | 44 // This variable is exported through the header file. |
| 45 #if defined(OS_ANDROID) | |
| 46 const char kIcuDataFileName[] = "assets/icudtl.dat"; | |
| 47 #else | |
| 41 const char kIcuDataFileName[] = "icudtl.dat"; | 48 const char kIcuDataFileName[] = "icudtl.dat"; |
| 49 #endif // defined(OS_ANDROID) | |
| 42 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED | 50 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED |
| 43 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat" | 51 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat" |
| 44 #if defined(OS_WIN) | 52 #if defined(OS_WIN) |
| 45 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll" | 53 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll" |
| 46 #endif | 54 #endif |
| 47 #endif | 55 #endif |
| 48 | 56 |
| 49 namespace { | 57 namespace { |
| 50 | 58 |
| 59 #if !defined(OS_NACL) | |
| 51 #if !defined(NDEBUG) | 60 #if !defined(NDEBUG) |
| 52 // Assert that we are not called more than once. Even though calling this | 61 // Assert that we are not called more than once. Even though calling this |
| 53 // function isn't harmful (ICU can handle it), being called twice probably | 62 // function isn't harmful (ICU can handle it), being called twice probably |
| 54 // indicates a programming error. | 63 // indicates a programming error. |
| 55 #if !defined(OS_NACL) | 64 bool g_check_called_once = true; |
| 56 bool g_called_once = false; | 65 bool g_called_once = false; |
| 57 #endif | 66 #endif // !defined(NDEBUG) |
| 58 bool g_check_called_once = true; | |
| 59 #endif | |
| 60 } | |
| 61 | 67 |
| 62 #if !defined(OS_NACL) | 68 |
| 63 bool InitializeICUWithFileDescriptor( | 69 bool InitializeICUWithFileDescriptorInternal( |
| 64 PlatformFile data_fd, | 70 PlatformFile data_fd, |
| 65 MemoryMappedFile::Region data_region) { | 71 const MemoryMappedFile::Region& data_region) { |
| 66 #if !defined(NDEBUG) | |
| 67 DCHECK(!g_check_called_once || !g_called_once); | |
| 68 g_called_once = true; | |
| 69 #endif | |
| 70 | |
| 71 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) | 72 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) |
| 72 // The ICU data is statically linked. | 73 // The ICU data is statically linked. |
| 73 return true; | 74 return true; |
| 74 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) | 75 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) |
| 75 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ()); | 76 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ()); |
| 76 if (!mapped_file.IsValid()) { | 77 if (!mapped_file.IsValid()) { |
| 77 if (!mapped_file.Initialize(File(data_fd), data_region)) { | 78 if (!mapped_file.Initialize(File(data_fd), data_region)) { |
| 78 LOG(ERROR) << "Couldn't mmap icu data file"; | 79 LOG(ERROR) << "Couldn't mmap icu data file"; |
| 79 return false; | 80 return false; |
| 80 } | 81 } |
| 81 } | 82 } |
| 82 UErrorCode err = U_ZERO_ERROR; | 83 UErrorCode err = U_ZERO_ERROR; |
| 83 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); | 84 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); |
| 84 return err == U_ZERO_ERROR; | 85 return err == U_ZERO_ERROR; |
| 85 #endif // ICU_UTIL_DATA_FILE | 86 #endif // ICU_UTIL_DATA_FILE |
| 86 } | 87 } |
| 88 #endif // !defined(OS_NACL) | |
| 87 | 89 |
| 90 } // namespace | |
| 91 | |
| 92 #if !defined(OS_NACL) | |
| 93 bool InitializeICUWithFileDescriptor( | |
| 94 PlatformFile data_fd, | |
| 95 const MemoryMappedFile::Region& data_region) { | |
| 96 #if !defined(NDEBUG) | |
| 97 DCHECK(!g_check_called_once || !g_called_once); | |
| 98 g_called_once = true; | |
| 99 #endif | |
| 100 return InitializeICUWithFileDescriptorInternal(data_fd, data_region); | |
| 101 } | |
| 88 | 102 |
| 89 bool InitializeICU() { | 103 bool InitializeICU() { |
| 90 #if !defined(NDEBUG) | 104 #if !defined(NDEBUG) |
| 91 DCHECK(!g_check_called_once || !g_called_once); | 105 DCHECK(!g_check_called_once || !g_called_once); |
| 92 g_called_once = true; | 106 g_called_once = true; |
| 93 #endif | 107 #endif |
| 94 | 108 |
| 95 bool result; | 109 bool result; |
| 96 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED) | 110 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED) |
| 97 // We expect to find the ICU data module alongside the current module. | 111 // We expect to find the ICU data module alongside the current module. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 111 << ICU_UTIL_DATA_SHARED_MODULE_NAME; | 125 << ICU_UTIL_DATA_SHARED_MODULE_NAME; |
| 112 return false; | 126 return false; |
| 113 } | 127 } |
| 114 | 128 |
| 115 UErrorCode err = U_ZERO_ERROR; | 129 UErrorCode err = U_ZERO_ERROR; |
| 116 udata_setCommonData(reinterpret_cast<void*>(addr), &err); | 130 udata_setCommonData(reinterpret_cast<void*>(addr), &err); |
| 117 result = (err == U_ZERO_ERROR); | 131 result = (err == U_ZERO_ERROR); |
| 118 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) | 132 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) |
| 119 // The ICU data is statically linked. | 133 // The ICU data is statically linked. |
| 120 result = true; | 134 result = true; |
| 121 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) | 135 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) && defined(OS_ANDROID) |
| 136 base::MemoryMappedFile::Region region; | |
| 137 int fd = base::android::OpenApkAsset(kIcuDataFileName, ®ion); | |
| 138 CHECK_GE(fd, 0); | |
| 139 result = InitializeICUWithFileDescriptorInternal(fd, region); | |
| 140 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) && !defined(OS_ANDROID) | |
| 122 // If the ICU data directory is set, ICU won't actually load the data until | 141 // If the ICU data directory is set, ICU won't actually load the data until |
| 123 // it is needed. This can fail if the process is sandboxed at that time. | 142 // it is needed. This can fail if the process is sandboxed at that time. |
| 124 // Instead, we map the file in and hand off the data so the sandbox won't | 143 // Instead, we map the file in and hand off the data so the sandbox won't |
| 125 // cause any problems. | 144 // cause any problems. |
| 126 | 145 |
| 127 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever | 146 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever |
| 128 // be released. | 147 // be released. |
| 129 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ()); | 148 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ()); |
| 130 if (!mapped_file.IsValid()) { | 149 if (!mapped_file.IsValid()) { |
| 131 #if !defined(OS_MACOSX) | 150 #if !defined(OS_MACOSX) |
| 132 FilePath data_path; | 151 FilePath data_path; |
| 133 #if defined(OS_WIN) | 152 #if defined(OS_WIN) |
| 134 // The data file will be in the same directory as the current module. | 153 // The data file will be in the same directory as the current module. |
| 135 bool path_ok = PathService::Get(DIR_MODULE, &data_path); | 154 bool path_ok = PathService::Get(DIR_MODULE, &data_path); |
| 136 wchar_t tmp_buffer[_MAX_PATH] = {0}; | 155 wchar_t tmp_buffer[_MAX_PATH] = {0}; |
| 137 wcscpy_s(tmp_buffer, data_path.value().c_str()); | 156 wcscpy_s(tmp_buffer, data_path.value().c_str()); |
| 138 debug::Alias(tmp_buffer); | 157 debug::Alias(tmp_buffer); |
| 139 CHECK(path_ok); // TODO(scottmg): http://crbug.com/445616 | 158 CHECK(path_ok); // TODO(scottmg): http://crbug.com/445616 |
| 140 #elif defined(OS_ANDROID) | |
| 141 bool path_ok = PathService::Get(DIR_ANDROID_APP_DATA, &data_path); | |
| 142 #else | 159 #else |
| 143 // For now, expect the data file to be alongside the executable. | 160 // For now, expect the data file to be alongside the executable. |
| 144 // This is sufficient while we work on unit tests, but will eventually | 161 // This is sufficient while we work on unit tests, but will eventually |
| 145 // likely live in a data directory. | 162 // likely live in a data directory. |
| 146 bool path_ok = PathService::Get(DIR_EXE, &data_path); | 163 bool path_ok = PathService::Get(DIR_EXE, &data_path); |
| 147 #endif | 164 #endif |
| 148 DCHECK(path_ok); | 165 DCHECK(path_ok); |
| 149 data_path = data_path.AppendASCII(kIcuDataFileName); | 166 data_path = data_path.AppendASCII(kIcuDataFileName); |
| 150 | 167 |
| 151 #if defined(OS_WIN) | 168 #if defined(OS_WIN) |
| 152 // TODO(scottmg): http://crbug.com/445616 | 169 // TODO(scottmg): http://crbug.com/445616 |
| 153 wchar_t tmp_buffer2[_MAX_PATH] = {0}; | 170 wchar_t tmp_buffer2[_MAX_PATH] = {0}; |
| 154 wcscpy_s(tmp_buffer2, data_path.value().c_str()); | 171 wcscpy_s(tmp_buffer2, data_path.value().c_str()); |
| 155 debug::Alias(tmp_buffer2); | 172 debug::Alias(tmp_buffer2); |
| 156 #endif | 173 #endif |
| 157 | 174 |
| 158 #else | 175 #else |
| 159 // Assume it is in the framework bundle's Resources directory. | 176 // Assume it is in the framework bundle's Resources directory. |
| 160 ScopedCFTypeRef<CFStringRef> data_file_name( | 177 ScopedCFTypeRef<CFStringRef> data_file_name( |
| 161 SysUTF8ToCFStringRef(kIcuDataFileName)); | 178 SysUTF8ToCFStringRef(kIcuDataFileName)); |
| 162 FilePath data_path = | 179 FilePath data_path = |
| 163 mac::PathForFrameworkBundleResource(data_file_name); | 180 mac::PathForFrameworkBundleResource(data_file_name); |
| 164 if (data_path.empty()) { | 181 if (data_path.empty()) { |
| 165 LOG(ERROR) << kIcuDataFileName << " not found in bundle"; | 182 LOG(ERROR) << kIcuDataFileName << " not found in bundle"; |
| 166 return false; | 183 return false; |
| 167 } | 184 } |
| 168 #endif // OS check | 185 #endif // OS check |
|
jungshik at Google
2015/06/09 23:45:15
It's not yours (it's my fault), but could you chan
| |
| 169 if (!mapped_file.Initialize(data_path)) { | 186 if (!mapped_file.Initialize(data_path)) { |
| 170 #if defined(OS_WIN) | 187 #if defined(OS_WIN) |
| 171 CHECK(false); // TODO(scottmg): http://crbug.com/445616 | 188 CHECK(false); // TODO(scottmg): http://crbug.com/445616 |
| 172 #endif | 189 #endif |
| 173 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe(); | 190 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe(); |
| 174 return false; | 191 return false; |
| 175 } | 192 } |
| 176 } | 193 } |
| 177 UErrorCode err = U_ZERO_ERROR; | 194 UErrorCode err = U_ZERO_ERROR; |
| 178 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); | 195 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); |
| 179 result = (err == U_ZERO_ERROR); | 196 result = (err == U_ZERO_ERROR); |
| 180 #if defined(OS_WIN) | 197 #if defined(OS_WIN) |
| 181 CHECK(result); // TODO(scottmg): http://crbug.com/445616 | 198 CHECK(result); // TODO(scottmg): http://crbug.com/445616 |
| 182 #endif | 199 #endif |
| 183 #endif | 200 #endif |
| 184 | 201 |
| 185 // To respond to the timezone change properly, the default timezone | 202 // To respond to the timezone change properly, the default timezone |
| 186 // cache in ICU has to be populated on starting up. | 203 // cache in ICU has to be populated on starting up. |
| 187 // TODO(jungshik): Some callers do not care about tz at all. If necessary, | 204 // TODO(jungshik): Some callers do not care about tz at all. If necessary, |
| 188 // add a boolean argument to this function to init'd the default tz only | 205 // add a boolean argument to this function to init'd the default tz only |
| 189 // when requested. | 206 // when requested. |
| 190 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 207 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 191 if (result) | 208 if (result) |
| 192 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); | 209 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); |
| 193 #endif | 210 #endif |
| 194 return result; | 211 return result; |
| 195 } | 212 } |
| 196 #endif | 213 #endif // !defined(OS_NACL) |
| 197 | 214 |
| 198 void AllowMultipleInitializeCallsForTesting() { | 215 void AllowMultipleInitializeCallsForTesting() { |
| 199 #if !defined(NDEBUG) | 216 #if !defined(NDEBUG) && !defined(OS_NACL) |
| 200 g_check_called_once = false; | 217 g_check_called_once = false; |
| 201 #endif | 218 #endif |
| 202 } | 219 } |
| 203 | 220 |
| 204 } // namespace i18n | 221 } // namespace i18n |
| 205 } // namespace base | 222 } // namespace base |
| OLD | NEW |