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.
52 bool g_called_once
= false;
54 bool g_check_called_once
= true;
58 #if defined(OS_ANDROID)
59 bool InitializeICUWithFileDescriptor(
61 base::MemoryMappedFile::Region data_region
) {
63 DCHECK(!g_check_called_once
|| !g_called_once
);
67 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC)
68 // The ICU data is statically linked.
70 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE)
71 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile
, mapped_file
, ());
72 if (!mapped_file
.IsValid()) {
73 if (!mapped_file
.Initialize(base::File(data_fd
), data_region
)) {
74 LOG(ERROR
) << "Couldn't mmap icu data file";
78 UErrorCode err
= U_ZERO_ERROR
;
79 udata_setCommonData(const_cast<uint8
*>(mapped_file
.data()), &err
);
80 return err
== U_ZERO_ERROR
;
81 #endif // ICU_UTIL_DATA_FILE
87 bool InitializeICU() {
89 DCHECK(!g_check_called_once
|| !g_called_once
);
93 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED)
94 // We expect to find the ICU data module alongside the current module.
96 PathService::Get(base::DIR_MODULE
, &data_path
);
97 data_path
= data_path
.AppendASCII(ICU_UTIL_DATA_SHARED_MODULE_NAME
);
99 HMODULE module
= LoadLibrary(data_path
.value().c_str());
101 LOG(ERROR
) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME
;
105 FARPROC addr
= GetProcAddress(module
, ICU_UTIL_DATA_SYMBOL
);
107 LOG(ERROR
) << ICU_UTIL_DATA_SYMBOL
<< ": not found in "
108 << ICU_UTIL_DATA_SHARED_MODULE_NAME
;
112 UErrorCode err
= U_ZERO_ERROR
;
113 udata_setCommonData(reinterpret_cast<void*>(addr
), &err
);
114 return err
== U_ZERO_ERROR
;
115 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC)
116 // The ICU data is statically linked.
118 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE)
119 // If the ICU data directory is set, ICU won't actually load the data until
120 // it is needed. This can fail if the process is sandboxed at that time.
121 // Instead, we map the file in and hand off the data so the sandbox won't
122 // cause any problems.
124 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever
126 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile
, mapped_file
, ());
127 if (!mapped_file
.IsValid()) {
128 #if !defined(OS_MACOSX)
131 // The data file will be in the same directory as the current module.
132 bool path_ok
= PathService::Get(base::DIR_MODULE
, &data_path
);
133 #elif defined(OS_ANDROID)
134 bool path_ok
= PathService::Get(base::DIR_ANDROID_APP_DATA
, &data_path
);
136 // For now, expect the data file to be alongside the executable.
137 // This is sufficient while we work on unit tests, but will eventually
138 // likely live in a data directory.
139 bool path_ok
= PathService::Get(base::DIR_EXE
, &data_path
);
142 data_path
= data_path
.AppendASCII(kIcuDataFileName
);
144 // Assume it is in the framework bundle's Resources directory.
145 base::ScopedCFTypeRef
<CFStringRef
> data_file_name(
146 SysUTF8ToCFStringRef(kIcuDataFileName
));
148 base::mac::PathForFrameworkBundleResource(data_file_name
);
149 if (data_path
.empty()) {
150 LOG(ERROR
) << kIcuDataFileName
<< " not found in bundle";
154 if (!mapped_file
.Initialize(data_path
)) {
155 LOG(ERROR
) << "Couldn't mmap " << data_path
.AsUTF8Unsafe();
159 UErrorCode err
= U_ZERO_ERROR
;
160 udata_setCommonData(const_cast<uint8
*>(mapped_file
.data()), &err
);
161 return err
== U_ZERO_ERROR
;
166 void AllowMultipleInitializeCallsForTesting() {
168 g_check_called_once
= false;