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