ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / base / i18n / icu_util.cc
blobf1fdda7458782d509a8631fb0b6a56d76f96dbf6
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/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"
24 #endif
26 #define ICU_UTIL_DATA_FILE 0
27 #define ICU_UTIL_DATA_SHARED 1
28 #define ICU_UTIL_DATA_STATIC 2
30 namespace base {
31 namespace i18n {
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"
40 #if defined(OS_WIN)
41 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll"
42 #endif
43 #endif
45 namespace {
47 #if !defined(NDEBUG)
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;
53 #endif
56 #if defined(OS_ANDROID)
57 bool InitializeICUWithFileDescriptor(
58 int data_fd,
59 base::MemoryMappedFile::Region data_region) {
60 #if !defined(NDEBUG)
61 DCHECK(!g_check_called_once || !g_called_once);
62 g_called_once = true;
63 #endif
65 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC)
66 // The ICU data is statically linked.
67 return true;
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";
73 return false;
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
81 #endif
84 bool InitializeICU() {
85 #if !defined(NDEBUG)
86 DCHECK(!g_check_called_once || !g_called_once);
87 g_called_once = true;
88 #endif
90 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED)
91 // We expect to find the ICU data module alongside the current module.
92 FilePath data_path;
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());
97 if (!module) {
98 LOG(ERROR) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME;
99 return false;
102 FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL);
103 if (!addr) {
104 LOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in "
105 << ICU_UTIL_DATA_SHARED_MODULE_NAME;
106 return false;
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.
114 return true;
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
122 // be released.
123 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile, mapped_file, ());
124 if (!mapped_file.IsValid()) {
125 #if !defined(OS_MACOSX)
126 FilePath data_path;
127 #if defined(OS_WIN)
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);
132 #else
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);
137 #endif
138 DCHECK(path_ok);
139 data_path = data_path.AppendASCII(kIcuDataFileName);
140 #else
141 // Assume it is in the framework bundle's Resources directory.
142 base::ScopedCFTypeRef<CFStringRef> data_file_name(
143 SysUTF8ToCFStringRef(kIcuDataFileName));
144 FilePath data_path =
145 base::mac::PathForFrameworkBundleResource(data_file_name);
146 if (data_path.empty()) {
147 LOG(ERROR) << kIcuDataFileName << " not found in bundle";
148 return false;
150 #endif // OS check
151 if (!mapped_file.Initialize(data_path)) {
152 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe();
153 return false;
156 UErrorCode err = U_ZERO_ERROR;
157 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err);
158 return err == U_ZERO_ERROR;
159 #endif
162 void AllowMultipleInitializeCallsForTesting() {
163 #if !defined(NDEBUG)
164 g_check_called_once = false;
165 #endif
168 } // namespace i18n
169 } // namespace base