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/files/file_path.h"
14 #include "base/files/memory_mapped_file.h"
15 #include "base/logging.h"
16 #include "base/path_service.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/sys_string_conversions.h"
19 #include "third_party/icu/source/common/unicode/putil.h"
20 #include "third_party/icu/source/common/unicode/udata.h"
22 #if defined(OS_MACOSX)
23 #include "base/mac/foundation_util.h"
26 #define ICU_UTIL_DATA_FILE 0
27 #define ICU_UTIL_DATA_SHARED 1
28 #define ICU_UTIL_DATA_STATIC 2
33 // Use an unversioned file name to simplify a icu version update down the road.
34 // No need to change the filename in multiple places (gyp files, windows
35 // build pkg configurations, etc). 'l' stands for Little Endian.
36 // This variable is exported through the header file.
37 const char kIcuDataFileName
[] = "icudtl.dat";
38 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED
39 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat"
41 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll"
48 // Assert that we are not called more than once. Even though calling this
49 // function isn't harmful (ICU can handle it), being called twice probably
50 // indicates a programming error.
51 bool g_called_once
= false;
52 bool g_check_called_once
= true;
56 #if defined(OS_ANDROID)
57 bool InitializeICUWithFileDescriptor(
59 base::MemoryMappedFile::Region data_region
) {
61 DCHECK(!g_check_called_once
|| !g_called_once
);
65 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC)
66 // The ICU data is statically linked.
68 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE)
69 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile
, mapped_file
, ());
70 if (!mapped_file
.IsValid()) {
71 if (!mapped_file
.Initialize(base::File(data_fd
), data_region
)) {
72 LOG(ERROR
) << "Couldn't mmap icu data file";
76 UErrorCode err
= U_ZERO_ERROR
;
77 udata_setCommonData(const_cast<uint8
*>(mapped_file
.data()), &err
);
78 return err
== U_ZERO_ERROR
;
79 #endif // ICU_UTIL_DATA_FILE
84 bool InitializeICU() {
86 DCHECK(!g_check_called_once
|| !g_called_once
);
90 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED)
91 // We expect to find the ICU data module alongside the current module.
93 PathService::Get(base::DIR_MODULE
, &data_path
);
94 data_path
= data_path
.AppendASCII(ICU_UTIL_DATA_SHARED_MODULE_NAME
);
96 HMODULE module
= LoadLibrary(data_path
.value().c_str());
98 LOG(ERROR
) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME
;
102 FARPROC addr
= GetProcAddress(module
, ICU_UTIL_DATA_SYMBOL
);
104 LOG(ERROR
) << ICU_UTIL_DATA_SYMBOL
<< ": not found in "
105 << ICU_UTIL_DATA_SHARED_MODULE_NAME
;
109 UErrorCode err
= U_ZERO_ERROR
;
110 udata_setCommonData(reinterpret_cast<void*>(addr
), &err
);
111 return err
== U_ZERO_ERROR
;
112 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC)
113 // The ICU data is statically linked.
115 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE)
116 // If the ICU data directory is set, ICU won't actually load the data until
117 // it is needed. This can fail if the process is sandboxed at that time.
118 // Instead, we map the file in and hand off the data so the sandbox won't
119 // cause any problems.
121 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever
123 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile
, mapped_file
, ());
124 if (!mapped_file
.IsValid()) {
125 #if !defined(OS_MACOSX)
128 // The data file will be in the same directory as the current module.
129 bool path_ok
= PathService::Get(base::DIR_MODULE
, &data_path
);
130 #elif defined(OS_ANDROID)
131 bool path_ok
= PathService::Get(base::DIR_ANDROID_APP_DATA
, &data_path
);
133 // For now, expect the data file to be alongside the executable.
134 // This is sufficient while we work on unit tests, but will eventually
135 // likely live in a data directory.
136 bool path_ok
= PathService::Get(base::DIR_EXE
, &data_path
);
139 data_path
= data_path
.AppendASCII(kIcuDataFileName
);
141 // Assume it is in the framework bundle's Resources directory.
142 base::ScopedCFTypeRef
<CFStringRef
> data_file_name(
143 SysUTF8ToCFStringRef(kIcuDataFileName
));
145 base::mac::PathForFrameworkBundleResource(data_file_name
);
146 if (data_path
.empty()) {
147 LOG(ERROR
) << kIcuDataFileName
<< " not found in bundle";
151 if (!mapped_file
.Initialize(data_path
)) {
152 LOG(ERROR
) << "Couldn't mmap " << data_path
.AsUTF8Unsafe();
156 UErrorCode err
= U_ZERO_ERROR
;
157 udata_setCommonData(const_cast<uint8
*>(mapped_file
.data()), &err
);
158 return err
== U_ZERO_ERROR
;
162 void AllowMultipleInitializeCallsForTesting() {
164 g_check_called_once
= false;