OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/i18n/icu_util.h" | 5 #include "base/i18n/icu_util.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 // Assert that we are not called more than once. Even though calling this | 52 // Assert that we are not called more than once. Even though calling this |
53 // function isn't harmful (ICU can handle it), being called twice probably | 53 // function isn't harmful (ICU can handle it), being called twice probably |
54 // indicates a programming error. | 54 // indicates a programming error. |
55 #if !defined(OS_NACL) | 55 #if !defined(OS_NACL) |
56 bool g_called_once = false; | 56 bool g_called_once = false; |
57 #endif | 57 #endif |
58 bool g_check_called_once = true; | 58 bool g_check_called_once = true; |
59 #endif | 59 #endif |
60 } | 60 } |
61 | 61 |
62 #if defined(OS_ANDROID) | 62 #if !defined(OS_NACL) |
63 bool InitializeICUWithFileDescriptor( | 63 bool InitializeICUWithFileDescriptor( |
64 int data_fd, | 64 PlatformFile data_fd, |
65 base::MemoryMappedFile::Region data_region) { | 65 MemoryMappedFile::Region data_region) { |
66 #if !defined(NDEBUG) | 66 #if !defined(NDEBUG) |
67 DCHECK(!g_check_called_once || !g_called_once); | 67 DCHECK(!g_check_called_once || !g_called_once); |
68 g_called_once = true; | 68 g_called_once = true; |
69 #endif | 69 #endif |
70 | 70 |
71 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) | 71 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) |
72 // The ICU data is statically linked. | 72 // The ICU data is statically linked. |
73 return true; | 73 return true; |
74 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) | 74 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) |
75 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile, mapped_file, ()); | 75 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ()); |
76 if (!mapped_file.IsValid()) { | 76 if (!mapped_file.IsValid()) { |
77 if (!mapped_file.Initialize(base::File(data_fd), data_region)) { | 77 if (!mapped_file.Initialize(File(data_fd), data_region)) { |
78 LOG(ERROR) << "Couldn't mmap icu data file"; | 78 LOG(ERROR) << "Couldn't mmap icu data file"; |
79 return false; | 79 return false; |
80 } | 80 } |
81 } | 81 } |
82 UErrorCode err = U_ZERO_ERROR; | 82 UErrorCode err = U_ZERO_ERROR; |
83 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); | 83 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); |
84 return err == U_ZERO_ERROR; | 84 return err == U_ZERO_ERROR; |
85 #endif // ICU_UTIL_DATA_FILE | 85 #endif // ICU_UTIL_DATA_FILE |
86 } | 86 } |
87 #endif | |
88 | 87 |
89 | 88 |
90 #if !defined(OS_NACL) | |
91 bool InitializeICU() { | 89 bool InitializeICU() { |
92 #if !defined(NDEBUG) | 90 #if !defined(NDEBUG) |
93 DCHECK(!g_check_called_once || !g_called_once); | 91 DCHECK(!g_check_called_once || !g_called_once); |
94 g_called_once = true; | 92 g_called_once = true; |
95 #endif | 93 #endif |
96 | 94 |
97 bool result; | 95 bool result; |
98 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED) | 96 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED) |
99 // We expect to find the ICU data module alongside the current module. | 97 // We expect to find the ICU data module alongside the current module. |
100 FilePath data_path; | 98 FilePath data_path; |
101 PathService::Get(base::DIR_MODULE, &data_path); | 99 PathService::Get(DIR_MODULE, &data_path); |
102 data_path = data_path.AppendASCII(ICU_UTIL_DATA_SHARED_MODULE_NAME); | 100 data_path = data_path.AppendASCII(ICU_UTIL_DATA_SHARED_MODULE_NAME); |
103 | 101 |
104 HMODULE module = LoadLibrary(data_path.value().c_str()); | 102 HMODULE module = LoadLibrary(data_path.value().c_str()); |
105 if (!module) { | 103 if (!module) { |
106 LOG(ERROR) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME; | 104 LOG(ERROR) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME; |
107 return false; | 105 return false; |
108 } | 106 } |
109 | 107 |
110 FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL); | 108 FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL); |
111 if (!addr) { | 109 if (!addr) { |
112 LOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in " | 110 LOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in " |
113 << ICU_UTIL_DATA_SHARED_MODULE_NAME; | 111 << ICU_UTIL_DATA_SHARED_MODULE_NAME; |
114 return false; | 112 return false; |
115 } | 113 } |
116 | 114 |
117 UErrorCode err = U_ZERO_ERROR; | 115 UErrorCode err = U_ZERO_ERROR; |
118 udata_setCommonData(reinterpret_cast<void*>(addr), &err); | 116 udata_setCommonData(reinterpret_cast<void*>(addr), &err); |
119 result = (err == U_ZERO_ERROR); | 117 result = (err == U_ZERO_ERROR); |
120 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) | 118 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) |
121 // The ICU data is statically linked. | 119 // The ICU data is statically linked. |
122 result = true; | 120 result = true; |
123 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) | 121 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) |
124 // If the ICU data directory is set, ICU won't actually load the data until | 122 // If the ICU data directory is set, ICU won't actually load the data until |
125 // it is needed. This can fail if the process is sandboxed at that time. | 123 // it is needed. This can fail if the process is sandboxed at that time. |
126 // Instead, we map the file in and hand off the data so the sandbox won't | 124 // Instead, we map the file in and hand off the data so the sandbox won't |
127 // cause any problems. | 125 // cause any problems. |
128 | 126 |
129 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever | 127 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever |
130 // be released. | 128 // be released. |
131 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile, mapped_file, ()); | 129 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ()); |
132 if (!mapped_file.IsValid()) { | 130 if (!mapped_file.IsValid()) { |
133 #if !defined(OS_MACOSX) | 131 #if !defined(OS_MACOSX) |
134 FilePath data_path; | 132 FilePath data_path; |
135 #if defined(OS_WIN) | 133 #if defined(OS_WIN) |
136 // The data file will be in the same directory as the current module. | 134 // The data file will be in the same directory as the current module. |
137 bool path_ok = PathService::Get(base::DIR_MODULE, &data_path); | 135 bool path_ok = PathService::Get(DIR_MODULE, &data_path); |
138 wchar_t tmp_buffer[_MAX_PATH] = {0}; | 136 wchar_t tmp_buffer[_MAX_PATH] = {0}; |
139 wcscpy_s(tmp_buffer, data_path.value().c_str()); | 137 wcscpy_s(tmp_buffer, data_path.value().c_str()); |
140 base::debug::Alias(tmp_buffer); | 138 debug::Alias(tmp_buffer); |
141 CHECK(path_ok); // TODO(scottmg): http://crbug.com/445616 | 139 CHECK(path_ok); // TODO(scottmg): http://crbug.com/445616 |
142 #elif defined(OS_ANDROID) | 140 #elif defined(OS_ANDROID) |
143 bool path_ok = PathService::Get(base::DIR_ANDROID_APP_DATA, &data_path); | 141 bool path_ok = PathService::Get(DIR_ANDROID_APP_DATA, &data_path); |
144 #else | 142 #else |
145 // For now, expect the data file to be alongside the executable. | 143 // For now, expect the data file to be alongside the executable. |
146 // This is sufficient while we work on unit tests, but will eventually | 144 // This is sufficient while we work on unit tests, but will eventually |
147 // likely live in a data directory. | 145 // likely live in a data directory. |
148 bool path_ok = PathService::Get(base::DIR_EXE, &data_path); | 146 bool path_ok = PathService::Get(DIR_EXE, &data_path); |
149 #endif | 147 #endif |
150 DCHECK(path_ok); | 148 DCHECK(path_ok); |
151 data_path = data_path.AppendASCII(kIcuDataFileName); | 149 data_path = data_path.AppendASCII(kIcuDataFileName); |
152 | 150 |
153 #if defined(OS_WIN) | 151 #if defined(OS_WIN) |
154 // TODO(scottmg): http://crbug.com/445616 | 152 // TODO(scottmg): http://crbug.com/445616 |
155 wchar_t tmp_buffer2[_MAX_PATH] = {0}; | 153 wchar_t tmp_buffer2[_MAX_PATH] = {0}; |
156 wcscpy_s(tmp_buffer2, data_path.value().c_str()); | 154 wcscpy_s(tmp_buffer2, data_path.value().c_str()); |
157 base::debug::Alias(tmp_buffer2); | 155 debug::Alias(tmp_buffer2); |
158 #endif | 156 #endif |
159 | 157 |
160 #else | 158 #else |
161 // Assume it is in the framework bundle's Resources directory. | 159 // Assume it is in the framework bundle's Resources directory. |
162 base::ScopedCFTypeRef<CFStringRef> data_file_name( | 160 ScopedCFTypeRef<CFStringRef> data_file_name( |
163 SysUTF8ToCFStringRef(kIcuDataFileName)); | 161 SysUTF8ToCFStringRef(kIcuDataFileName)); |
164 FilePath data_path = | 162 FilePath data_path = |
165 base::mac::PathForFrameworkBundleResource(data_file_name); | 163 mac::PathForFrameworkBundleResource(data_file_name); |
166 if (data_path.empty()) { | 164 if (data_path.empty()) { |
167 LOG(ERROR) << kIcuDataFileName << " not found in bundle"; | 165 LOG(ERROR) << kIcuDataFileName << " not found in bundle"; |
168 return false; | 166 return false; |
169 } | 167 } |
170 #endif // OS check | 168 #endif // OS check |
171 if (!mapped_file.Initialize(data_path)) { | 169 if (!mapped_file.Initialize(data_path)) { |
172 #if defined(OS_WIN) | 170 #if defined(OS_WIN) |
173 CHECK(false); // TODO(scottmg): http://crbug.com/445616 | 171 CHECK(false); // TODO(scottmg): http://crbug.com/445616 |
174 #endif | 172 #endif |
175 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe(); | 173 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe(); |
(...skipping 22 matching lines...) Expand all Loading... |
198 #endif | 196 #endif |
199 | 197 |
200 void AllowMultipleInitializeCallsForTesting() { | 198 void AllowMultipleInitializeCallsForTesting() { |
201 #if !defined(NDEBUG) | 199 #if !defined(NDEBUG) |
202 g_check_called_once = false; | 200 g_check_called_once = false; |
203 #endif | 201 #endif |
204 } | 202 } |
205 | 203 |
206 } // namespace i18n | 204 } // namespace i18n |
207 } // namespace base | 205 } // namespace base |
OLD | NEW |