Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / base / i18n / icu_util.cc
blob8bbbc049e8ae2d3606d3781eef13d49e30b32ff1
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 #if !defined(OS_NACL)
52 bool g_called_once = false;
53 #endif
54 bool g_check_called_once = true;
55 #endif
58 #if defined(OS_ANDROID)
59 bool InitializeICUWithFileDescriptor(
60 int data_fd,
61 base::MemoryMappedFile::Region data_region) {
62 #if !defined(NDEBUG)
63 DCHECK(!g_check_called_once || !g_called_once);
64 g_called_once = true;
65 #endif
67 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC)
68 // The ICU data is statically linked.
69 return true;
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";
75 return false;
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
83 #endif
86 #if !defined(OS_NACL)
87 bool InitializeICU() {
88 #if !defined(NDEBUG)
89 DCHECK(!g_check_called_once || !g_called_once);
90 g_called_once = true;
91 #endif
93 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED)
94 // We expect to find the ICU data module alongside the current module.
95 FilePath data_path;
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());
100 if (!module) {
101 LOG(ERROR) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME;
102 return false;
105 FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL);
106 if (!addr) {
107 LOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in "
108 << ICU_UTIL_DATA_SHARED_MODULE_NAME;
109 return false;
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.
117 return true;
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
125 // be released.
126 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile, mapped_file, ());
127 if (!mapped_file.IsValid()) {
128 #if !defined(OS_MACOSX)
129 FilePath data_path;
130 #if defined(OS_WIN)
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);
135 #else
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);
140 #endif
141 DCHECK(path_ok);
142 data_path = data_path.AppendASCII(kIcuDataFileName);
143 #else
144 // Assume it is in the framework bundle's Resources directory.
145 base::ScopedCFTypeRef<CFStringRef> data_file_name(
146 SysUTF8ToCFStringRef(kIcuDataFileName));
147 FilePath data_path =
148 base::mac::PathForFrameworkBundleResource(data_file_name);
149 if (data_path.empty()) {
150 LOG(ERROR) << kIcuDataFileName << " not found in bundle";
151 return false;
153 #endif // OS check
154 if (!mapped_file.Initialize(data_path)) {
155 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe();
156 return false;
159 UErrorCode err = U_ZERO_ERROR;
160 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err);
161 return err == U_ZERO_ERROR;
162 #endif
164 #endif
166 void AllowMultipleInitializeCallsForTesting() {
167 #if !defined(NDEBUG)
168 g_check_called_once = false;
169 #endif
172 } // namespace i18n
173 } // namespace base