Upload upstream chromium 71.0.3578.0
[platform/framework/web/chromium-efl.git] / base / i18n / icu_util.cc
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.
4
5 #include "base/i18n/icu_util.h"
6
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #endif
10
11 #include <string>
12
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 "build/build_config.h"
21 #include "third_party/icu/source/common/unicode/putil.h"
22 #include "third_party/icu/source/common/unicode/udata.h"
23 #if (defined(OS_LINUX) && !defined(OS_CHROMEOS)) || defined(OS_ANDROID)
24 #include "third_party/icu/source/i18n/unicode/timezone.h"
25 #endif
26
27 #if defined(OS_ANDROID)
28 #include "base/android/apk_assets.h"
29 #include "base/android/timezone_utils.h"
30 #endif
31
32 #if defined(OS_IOS)
33 #include "base/ios/ios_util.h"
34 #endif
35
36 #if defined(OS_MACOSX)
37 #include "base/mac/foundation_util.h"
38 #endif
39
40 #if defined(OS_FUCHSIA)
41 #include "base/base_paths_fuchsia.h"
42 #endif
43
44 namespace base {
45 namespace i18n {
46
47 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED
48 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat"
49 #if defined(OS_WIN)
50 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll"
51 #endif
52 #endif
53
54 namespace {
55 #if !defined(OS_NACL)
56 #if DCHECK_IS_ON()
57 // Assert that we are not called more than once.  Even though calling this
58 // function isn't harmful (ICU can handle it), being called twice probably
59 // indicates a programming error.
60 bool g_check_called_once = true;
61 bool g_called_once = false;
62 #endif  // DCHECK_IS_ON()
63
64 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
65
66 // To debug http://crbug.com/445616.
67 int g_debug_icu_last_error;
68 int g_debug_icu_load;
69 int g_debug_icu_pf_error_details;
70 int g_debug_icu_pf_last_error;
71 #if defined(OS_WIN)
72 wchar_t g_debug_icu_pf_filename[_MAX_PATH];
73 #endif  // OS_WIN
74 // Use an unversioned file name to simplify a icu version update down the road.
75 // No need to change the filename in multiple places (gyp files, windows
76 // build pkg configurations, etc). 'l' stands for Little Endian.
77 // This variable is exported through the header file.
78 const char kIcuDataFileName[] = "icudtl.dat";
79 #if defined(OS_ANDROID)
80 const char kAndroidAssetsIcuDataFileName[] = "assets/icudtl.dat";
81 #endif
82
83 // File handle intentionally never closed. Not using File here because its
84 // Windows implementation guards against two instances owning the same
85 // PlatformFile (which we allow since we know it is never freed).
86 PlatformFile g_icudtl_pf = kInvalidPlatformFile;
87 MemoryMappedFile* g_icudtl_mapped_file = nullptr;
88 MemoryMappedFile::Region g_icudtl_region;
89
90 void LazyInitIcuDataFile() {
91   if (g_icudtl_pf != kInvalidPlatformFile) {
92     return;
93   }
94 #if defined(OS_ANDROID)
95   int fd = base::android::OpenApkAsset(kAndroidAssetsIcuDataFileName,
96                                        &g_icudtl_region);
97   g_icudtl_pf = fd;
98   if (fd != -1) {
99     return;
100   }
101 // For unit tests, data file is located on disk, so try there as a fallback.
102 #endif  // defined(OS_ANDROID)
103 #if !defined(OS_MACOSX)
104   FilePath data_path;
105   if (!PathService::Get(DIR_ASSETS, &data_path)) {
106     LOG(ERROR) << "Can't find " << kIcuDataFileName;
107     return;
108   }
109 #if defined(OS_WIN)
110   // TODO(brucedawson): http://crbug.com/445616
111   wchar_t tmp_buffer[_MAX_PATH] = {0};
112   wcscpy_s(tmp_buffer, data_path.value().c_str());
113   debug::Alias(tmp_buffer);
114 #endif
115   data_path = data_path.AppendASCII(kIcuDataFileName);
116
117 #if defined(OS_WIN)
118   // TODO(brucedawson): http://crbug.com/445616
119   wchar_t tmp_buffer2[_MAX_PATH] = {0};
120   wcscpy_s(tmp_buffer2, data_path.value().c_str());
121   debug::Alias(tmp_buffer2);
122 #endif
123
124 #else  // !defined(OS_MACOSX)
125   // Assume it is in the framework bundle's Resources directory.
126   ScopedCFTypeRef<CFStringRef> data_file_name(
127       SysUTF8ToCFStringRef(kIcuDataFileName));
128   FilePath data_path = mac::PathForFrameworkBundleResource(data_file_name);
129 #if defined(OS_IOS)
130   FilePath override_data_path = base::ios::FilePathOfEmbeddedICU();
131   if (!override_data_path.empty()) {
132     data_path = override_data_path;
133   }
134 #endif  // !defined(OS_IOS)
135   if (data_path.empty()) {
136     LOG(ERROR) << kIcuDataFileName << " not found in bundle";
137     return;
138   }
139 #endif  // !defined(OS_MACOSX)
140   File file(data_path, File::FLAG_OPEN | File::FLAG_READ);
141   if (file.IsValid()) {
142     // TODO(brucedawson): http://crbug.com/445616.
143     g_debug_icu_pf_last_error = 0;
144     g_debug_icu_pf_error_details = 0;
145 #if defined(OS_WIN)
146     g_debug_icu_pf_filename[0] = 0;
147 #endif  // OS_WIN
148
149     g_icudtl_pf = file.TakePlatformFile();
150     g_icudtl_region = MemoryMappedFile::Region::kWholeFile;
151   }
152 #if defined(OS_WIN)
153   else {
154     // TODO(brucedawson): http://crbug.com/445616.
155     g_debug_icu_pf_last_error = ::GetLastError();
156     g_debug_icu_pf_error_details = file.error_details();
157     wcscpy_s(g_debug_icu_pf_filename, data_path.value().c_str());
158   }
159 #endif  // OS_WIN
160 }
161
162 bool InitializeICUWithFileDescriptorInternal(
163     PlatformFile data_fd,
164     const MemoryMappedFile::Region& data_region) {
165   // This can be called multiple times in tests.
166   if (g_icudtl_mapped_file) {
167     g_debug_icu_load = 0;  // To debug http://crbug.com/445616.
168     return true;
169   }
170   if (data_fd == kInvalidPlatformFile) {
171     g_debug_icu_load = 1;  // To debug http://crbug.com/445616.
172     LOG(ERROR) << "Invalid file descriptor to ICU data received.";
173     return false;
174   }
175
176   std::unique_ptr<MemoryMappedFile> icudtl_mapped_file(new MemoryMappedFile());
177   if (!icudtl_mapped_file->Initialize(File(data_fd), data_region)) {
178     g_debug_icu_load = 2;  // To debug http://crbug.com/445616.
179     LOG(ERROR) << "Couldn't mmap icu data file";
180     return false;
181   }
182   g_icudtl_mapped_file = icudtl_mapped_file.release();
183
184   UErrorCode err = U_ZERO_ERROR;
185   udata_setCommonData(const_cast<uint8_t*>(g_icudtl_mapped_file->data()), &err);
186   if (err != U_ZERO_ERROR) {
187     g_debug_icu_load = 3;  // To debug http://crbug.com/445616.
188     g_debug_icu_last_error = err;
189   }
190 #if defined(OS_ANDROID)
191   else {
192     // On Android, we can't leave it up to ICU to set the default timezone
193     // because ICU's timezone detection does not work in many timezones (e.g.
194     // Australia/Sydney, Asia/Seoul, Europe/Paris ). Use JNI to detect the host
195     // timezone and set the ICU default timezone accordingly in advance of
196     // actual use. See crbug.com/722821 and
197     // https://ssl.icu-project.org/trac/ticket/13208 .
198     base::string16 timezone_id = base::android::GetDefaultTimeZoneId();
199     icu::TimeZone::adoptDefault(icu::TimeZone::createTimeZone(
200         icu::UnicodeString(FALSE, timezone_id.data(), timezone_id.length())));
201   }
202 #endif
203   // Never try to load ICU data from files.
204   udata_setFileAccess(UDATA_ONLY_PACKAGES, &err);
205   return err == U_ZERO_ERROR;
206 }
207 #endif  // ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
208 #endif  // !defined(OS_NACL)
209
210 }  // namespace
211
212 #if !defined(OS_NACL)
213 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
214 #if defined(OS_ANDROID)
215 bool InitializeICUWithFileDescriptor(
216     PlatformFile data_fd,
217     const MemoryMappedFile::Region& data_region) {
218 #if DCHECK_IS_ON()
219   DCHECK(!g_check_called_once || !g_called_once);
220   g_called_once = true;
221 #endif
222   return InitializeICUWithFileDescriptorInternal(data_fd, data_region);
223 }
224
225 PlatformFile GetIcuDataFileHandle(MemoryMappedFile::Region* out_region) {
226   CHECK_NE(g_icudtl_pf, kInvalidPlatformFile);
227   *out_region = g_icudtl_region;
228   return g_icudtl_pf;
229 }
230 #endif
231
232 const uint8_t* GetRawIcuMemory() {
233   CHECK(g_icudtl_mapped_file);
234   return g_icudtl_mapped_file->data();
235 }
236
237 bool InitializeICUFromRawMemory(const uint8_t* raw_memory) {
238 #if !defined(COMPONENT_BUILD)
239 #if DCHECK_IS_ON()
240   DCHECK(!g_check_called_once || !g_called_once);
241   g_called_once = true;
242 #endif
243
244   UErrorCode err = U_ZERO_ERROR;
245   udata_setCommonData(const_cast<uint8_t*>(raw_memory), &err);
246   // Never try to load ICU data from files.
247   udata_setFileAccess(UDATA_ONLY_PACKAGES, &err);
248   return err == U_ZERO_ERROR;
249 #else
250   return true;
251 #endif
252 }
253
254 #endif  // ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
255
256 bool InitializeICU() {
257 #if DCHECK_IS_ON()
258   DCHECK(!g_check_called_once || !g_called_once);
259   g_called_once = true;
260 #endif
261
262   bool result;
263 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED)
264   FilePath data_path;
265   PathService::Get(DIR_ASSETS, &data_path);
266   data_path = data_path.AppendASCII(ICU_UTIL_DATA_SHARED_MODULE_NAME);
267
268   HMODULE module = LoadLibrary(data_path.value().c_str());
269   if (!module) {
270     LOG(ERROR) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME;
271     return false;
272   }
273
274   FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL);
275   if (!addr) {
276     LOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in "
277                << ICU_UTIL_DATA_SHARED_MODULE_NAME;
278     return false;
279   }
280
281   UErrorCode err = U_ZERO_ERROR;
282   udata_setCommonData(reinterpret_cast<void*>(addr), &err);
283   // Never try to load ICU data from files.
284   udata_setFileAccess(UDATA_ONLY_PACKAGES, &err);
285   result = (err == U_ZERO_ERROR);
286 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC)
287   // The ICU data is statically linked.
288   result = true;
289 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE)
290   // If the ICU data directory is set, ICU won't actually load the data until
291   // it is needed.  This can fail if the process is sandboxed at that time.
292   // Instead, we map the file in and hand off the data so the sandbox won't
293   // cause any problems.
294   LazyInitIcuDataFile();
295   result =
296       InitializeICUWithFileDescriptorInternal(g_icudtl_pf, g_icudtl_region);
297 #if defined(OS_WIN)
298   int debug_icu_load = g_debug_icu_load;
299   debug::Alias(&debug_icu_load);
300   int debug_icu_last_error = g_debug_icu_last_error;
301   debug::Alias(&debug_icu_last_error);
302   int debug_icu_pf_last_error = g_debug_icu_pf_last_error;
303   debug::Alias(&debug_icu_pf_last_error);
304   int debug_icu_pf_error_details = g_debug_icu_pf_error_details;
305   debug::Alias(&debug_icu_pf_error_details);
306   wchar_t debug_icu_pf_filename[_MAX_PATH] = {0};
307   wcscpy_s(debug_icu_pf_filename, g_debug_icu_pf_filename);
308   debug::Alias(&debug_icu_pf_filename);
309   CHECK(result);  // TODO(brucedawson): http://crbug.com/445616
310 #endif
311 #endif
312
313 // To respond to the timezone change properly, the default timezone
314 // cache in ICU has to be populated on starting up.
315 // TODO(jungshik): Some callers do not care about tz at all. If necessary,
316 // add a boolean argument to this function to init'd the default tz only
317 // when requested.
318 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
319   if (result)
320     std::unique_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
321 #endif
322   return result;
323 }
324 #endif  // !defined(OS_NACL)
325
326 void AllowMultipleInitializeCallsForTesting() {
327 #if DCHECK_IS_ON() && !defined(OS_NACL)
328   g_check_called_once = false;
329 #endif
330 }
331
332 }  // namespace i18n
333 }  // namespace base