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
3 // found in the LICENSE file.
5 #include "base/i18n/icu_util.h"
13 #include "base/debug/alias.h"
14 #include "base/files/file_path.h"
15 #include "base/files/memory_mapped_file.h"
16 #include "base/logging.h"
17 #include "base/path_service.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/sys_string_conversions.h"
20 #include "third_party/icu/source/common/unicode/putil.h"
21 #include "third_party/icu/source/common/unicode/udata.h"
22 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
23 #include "third_party/icu/source/i18n/unicode/timezone.h"
26 #if defined(OS_ANDROID)
27 #include "base/android/apk_assets.h"
30 #if defined(OS_MACOSX)
31 #include "base/mac/foundation_util.h"
34 #define ICU_UTIL_DATA_FILE 0
35 #define ICU_UTIL_DATA_SHARED 1
36 #define ICU_UTIL_DATA_STATIC 2
41 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED
42 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat"
44 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll"
51 // Assert that we are not called more than once. Even though calling this
52 // function isn't harmful (ICU can handle it), being called twice probably
53 // indicates a programming error.
54 bool g_check_called_once
= true;
55 bool g_called_once
= false;
56 #endif // !defined(NDEBUG)
58 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
59 // Use an unversioned file name to simplify a icu version update down the road.
60 // No need to change the filename in multiple places (gyp files, windows
61 // build pkg configurations, etc). 'l' stands for Little Endian.
62 // This variable is exported through the header file.
63 const char kIcuDataFileName
[] = "icudtl.dat";
64 #if defined(OS_ANDROID)
65 const char kAndroidAssetsIcuDataFileName
[] = "assets/icudtl.dat";
68 // File handle intentionally never closed. Not using File here because its
69 // Windows implementation guards against two instances owning the same
70 // PlatformFile (which we allow since we know it is never freed).
71 const PlatformFile kInvalidPlatformFile
=
77 PlatformFile g_icudtl_pf
= kInvalidPlatformFile
;
78 MemoryMappedFile
* g_icudtl_mapped_file
= nullptr;
79 MemoryMappedFile::Region g_icudtl_region
;
81 void LazyInitIcuDataFile() {
82 if (g_icudtl_pf
!= kInvalidPlatformFile
) {
85 #if defined(OS_ANDROID)
86 int fd
= base::android::OpenApkAsset(kAndroidAssetsIcuDataFileName
,
92 // For unit tests, data file is located on disk, so try there as a fallback.
93 #endif // defined(OS_ANDROID)
94 #if !defined(OS_MACOSX)
97 // The data file will be in the same directory as the current module.
98 bool path_ok
= PathService::Get(DIR_MODULE
, &data_path
);
99 wchar_t tmp_buffer
[_MAX_PATH
] = {0};
100 wcscpy_s(tmp_buffer
, data_path
.value().c_str());
101 debug::Alias(tmp_buffer
);
102 CHECK(path_ok
); // TODO(scottmg): http://crbug.com/445616
103 #elif defined(OS_ANDROID)
104 bool path_ok
= PathService::Get(DIR_ANDROID_APP_DATA
, &data_path
);
106 // For now, expect the data file to be alongside the executable.
107 // This is sufficient while we work on unit tests, but will eventually
108 // likely live in a data directory.
109 bool path_ok
= PathService::Get(DIR_EXE
, &data_path
);
112 data_path
= data_path
.AppendASCII(kIcuDataFileName
);
115 // TODO(scottmg): http://crbug.com/445616
116 wchar_t tmp_buffer2
[_MAX_PATH
] = {0};
117 wcscpy_s(tmp_buffer2
, data_path
.value().c_str());
118 debug::Alias(tmp_buffer2
);
122 // Assume it is in the framework bundle's Resources directory.
123 ScopedCFTypeRef
<CFStringRef
> data_file_name(
124 SysUTF8ToCFStringRef(kIcuDataFileName
));
125 FilePath data_path
= mac::PathForFrameworkBundleResource(data_file_name
);
126 if (data_path
.empty()) {
127 LOG(ERROR
) << kIcuDataFileName
<< " not found in bundle";
130 #endif // !defined(OS_MACOSX)
131 File
file(data_path
, File::FLAG_OPEN
| File::FLAG_READ
);
132 if (file
.IsValid()) {
133 g_icudtl_pf
= file
.TakePlatformFile();
134 g_icudtl_region
= MemoryMappedFile::Region::kWholeFile
;
138 bool InitializeICUWithFileDescriptorInternal(
139 PlatformFile data_fd
,
140 const MemoryMappedFile::Region
& data_region
) {
141 // This can be called multiple times in tests.
142 if (g_icudtl_mapped_file
) {
145 if (data_fd
== kInvalidPlatformFile
) {
149 scoped_ptr
<MemoryMappedFile
> icudtl_mapped_file(new MemoryMappedFile());
150 if (!icudtl_mapped_file
->Initialize(File(data_fd
), data_region
)) {
151 LOG(ERROR
) << "Couldn't mmap icu data file";
154 g_icudtl_mapped_file
= icudtl_mapped_file
.release();
156 UErrorCode err
= U_ZERO_ERROR
;
157 udata_setCommonData(const_cast<uint8
*>(g_icudtl_mapped_file
->data()), &err
);
158 return err
== U_ZERO_ERROR
;
160 #endif // ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
161 #endif // !defined(OS_NACL)
165 #if !defined(OS_NACL)
166 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
167 bool InitializeICUWithFileDescriptor(
168 PlatformFile data_fd
,
169 const MemoryMappedFile::Region
& data_region
) {
171 DCHECK(!g_check_called_once
|| !g_called_once
);
172 g_called_once
= true;
174 return InitializeICUWithFileDescriptorInternal(data_fd
, data_region
);
177 PlatformFile
GetIcuDataFileHandle(MemoryMappedFile::Region
* out_region
) {
178 CHECK_NE(g_icudtl_pf
, kInvalidPlatformFile
);
179 *out_region
= g_icudtl_region
;
182 #endif // ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
184 bool InitializeICU() {
186 DCHECK(!g_check_called_once
|| !g_called_once
);
187 g_called_once
= true;
191 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED)
192 // We expect to find the ICU data module alongside the current module.
194 PathService::Get(DIR_MODULE
, &data_path
);
195 data_path
= data_path
.AppendASCII(ICU_UTIL_DATA_SHARED_MODULE_NAME
);
197 HMODULE module
= LoadLibrary(data_path
.value().c_str());
199 LOG(ERROR
) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME
;
203 FARPROC addr
= GetProcAddress(module
, ICU_UTIL_DATA_SYMBOL
);
205 LOG(ERROR
) << ICU_UTIL_DATA_SYMBOL
<< ": not found in "
206 << ICU_UTIL_DATA_SHARED_MODULE_NAME
;
210 UErrorCode err
= U_ZERO_ERROR
;
211 udata_setCommonData(reinterpret_cast<void*>(addr
), &err
);
212 result
= (err
== U_ZERO_ERROR
);
213 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC)
214 // The ICU data is statically linked.
216 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE)
217 // If the ICU data directory is set, ICU won't actually load the data until
218 // it is needed. This can fail if the process is sandboxed at that time.
219 // Instead, we map the file in and hand off the data so the sandbox won't
220 // cause any problems.
221 LazyInitIcuDataFile();
223 InitializeICUWithFileDescriptorInternal(g_icudtl_pf
, g_icudtl_region
);
225 CHECK(result
); // TODO(scottmg): http://crbug.com/445616
229 // To respond to the timezone change properly, the default timezone
230 // cache in ICU has to be populated on starting up.
231 // TODO(jungshik): Some callers do not care about tz at all. If necessary,
232 // add a boolean argument to this function to init'd the default tz only
234 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
236 scoped_ptr
<icu::TimeZone
> zone(icu::TimeZone::createDefault());
240 #endif // !defined(OS_NACL)
242 void AllowMultipleInitializeCallsForTesting() {
243 #if !defined(NDEBUG) && !defined(OS_NACL)
244 g_check_called_once
= false;