Update broken references to image assets
[chromium-blink-merge.git] / base / i18n / icu_util.cc
blob1dd54cd606d8b750663a471b4efd277e5ec4980f
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"
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #endif
11 #include <string>
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"
24 #endif
26 #if defined(OS_ANDROID)
27 #include "base/android/apk_assets.h"
28 #endif
30 #if defined(OS_MACOSX)
31 #include "base/mac/foundation_util.h"
32 #endif
34 #define ICU_UTIL_DATA_FILE 0
35 #define ICU_UTIL_DATA_SHARED 1
36 #define ICU_UTIL_DATA_STATIC 2
38 namespace base {
39 namespace i18n {
41 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED
42 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat"
43 #if defined(OS_WIN)
44 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll"
45 #endif
46 #endif
48 namespace {
49 #if !defined(OS_NACL)
50 #if !defined(NDEBUG)
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";
66 #endif
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 =
72 #if defined(OS_WIN)
73 INVALID_HANDLE_VALUE;
74 #else
75 -1;
76 #endif
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) {
83 return;
85 #if defined(OS_ANDROID)
86 int fd = base::android::OpenApkAsset(kAndroidAssetsIcuDataFileName,
87 &g_icudtl_region);
88 g_icudtl_pf = fd;
89 if (fd != -1) {
90 return;
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)
95 FilePath data_path;
96 #if defined(OS_WIN)
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);
105 #else
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);
110 #endif
111 DCHECK(path_ok);
112 data_path = data_path.AppendASCII(kIcuDataFileName);
114 #if defined(OS_WIN)
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);
119 #endif
121 #else
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";
128 return;
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) {
143 return true;
145 if (data_fd == kInvalidPlatformFile) {
146 return false;
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";
152 return false;
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)
163 } // namespace
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) {
170 #if !defined(NDEBUG)
171 DCHECK(!g_check_called_once || !g_called_once);
172 g_called_once = true;
173 #endif
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;
180 return g_icudtl_pf;
182 #endif // ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
184 bool InitializeICU() {
185 #if !defined(NDEBUG)
186 DCHECK(!g_check_called_once || !g_called_once);
187 g_called_once = true;
188 #endif
190 bool result;
191 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED)
192 // We expect to find the ICU data module alongside the current module.
193 FilePath data_path;
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());
198 if (!module) {
199 LOG(ERROR) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME;
200 return false;
203 FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL);
204 if (!addr) {
205 LOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in "
206 << ICU_UTIL_DATA_SHARED_MODULE_NAME;
207 return false;
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.
215 result = true;
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();
222 result =
223 InitializeICUWithFileDescriptorInternal(g_icudtl_pf, g_icudtl_region);
224 #if defined(OS_WIN)
225 CHECK(result); // TODO(scottmg): http://crbug.com/445616
226 #endif
227 #endif
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
233 // when requested.
234 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
235 if (result)
236 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
237 #endif
238 return result;
240 #endif // !defined(OS_NACL)
242 void AllowMultipleInitializeCallsForTesting() {
243 #if !defined(NDEBUG) && !defined(OS_NACL)
244 g_check_called_once = false;
245 #endif
248 } // namespace i18n
249 } // namespace base