| 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 |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/debug/alias.h" | 13 #include "base/debug/alias.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/files/memory_mapped_file.h" | 15 #include "base/files/memory_mapped_file.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
| 20 #include "third_party/icu/source/common/unicode/putil.h" | 20 #include "third_party/icu/source/common/unicode/putil.h" |
| 21 #include "third_party/icu/source/common/unicode/udata.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 |
| 22 | 25 |
| 23 #if defined(OS_MACOSX) | 26 #if defined(OS_MACOSX) |
| 24 #include "base/mac/foundation_util.h" | 27 #include "base/mac/foundation_util.h" |
| 25 #endif | 28 #endif |
| 26 | 29 |
| 27 #define ICU_UTIL_DATA_FILE 0 | 30 #define ICU_UTIL_DATA_FILE 0 |
| 28 #define ICU_UTIL_DATA_SHARED 1 | 31 #define ICU_UTIL_DATA_SHARED 1 |
| 29 #define ICU_UTIL_DATA_STATIC 2 | 32 #define ICU_UTIL_DATA_STATIC 2 |
| 30 | 33 |
| 31 namespace base { | 34 namespace base { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 #endif | 87 #endif |
| 85 | 88 |
| 86 | 89 |
| 87 #if !defined(OS_NACL) | 90 #if !defined(OS_NACL) |
| 88 bool InitializeICU() { | 91 bool InitializeICU() { |
| 89 #if !defined(NDEBUG) | 92 #if !defined(NDEBUG) |
| 90 DCHECK(!g_check_called_once || !g_called_once); | 93 DCHECK(!g_check_called_once || !g_called_once); |
| 91 g_called_once = true; | 94 g_called_once = true; |
| 92 #endif | 95 #endif |
| 93 | 96 |
| 97 bool result; |
| 94 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED) | 98 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED) |
| 95 // We expect to find the ICU data module alongside the current module. | 99 // We expect to find the ICU data module alongside the current module. |
| 96 FilePath data_path; | 100 FilePath data_path; |
| 97 PathService::Get(base::DIR_MODULE, &data_path); | 101 PathService::Get(base::DIR_MODULE, &data_path); |
| 98 data_path = data_path.AppendASCII(ICU_UTIL_DATA_SHARED_MODULE_NAME); | 102 data_path = data_path.AppendASCII(ICU_UTIL_DATA_SHARED_MODULE_NAME); |
| 99 | 103 |
| 100 HMODULE module = LoadLibrary(data_path.value().c_str()); | 104 HMODULE module = LoadLibrary(data_path.value().c_str()); |
| 101 if (!module) { | 105 if (!module) { |
| 102 LOG(ERROR) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME; | 106 LOG(ERROR) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME; |
| 103 return false; | 107 return false; |
| 104 } | 108 } |
| 105 | 109 |
| 106 FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL); | 110 FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL); |
| 107 if (!addr) { | 111 if (!addr) { |
| 108 LOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in " | 112 LOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in " |
| 109 << ICU_UTIL_DATA_SHARED_MODULE_NAME; | 113 << ICU_UTIL_DATA_SHARED_MODULE_NAME; |
| 110 return false; | 114 return false; |
| 111 } | 115 } |
| 112 | 116 |
| 113 UErrorCode err = U_ZERO_ERROR; | 117 UErrorCode err = U_ZERO_ERROR; |
| 114 udata_setCommonData(reinterpret_cast<void*>(addr), &err); | 118 udata_setCommonData(reinterpret_cast<void*>(addr), &err); |
| 115 return err == U_ZERO_ERROR; | 119 result = (err == U_ZERO_ERROR); |
| 116 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) | 120 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) |
| 117 // The ICU data is statically linked. | 121 // The ICU data is statically linked. |
| 118 return true; | 122 result = true; |
| 119 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) | 123 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) |
| 120 // If the ICU data directory is set, ICU won't actually load the data until | 124 // If the ICU data directory is set, ICU won't actually load the data until |
| 121 // it is needed. This can fail if the process is sandboxed at that time. | 125 // it is needed. This can fail if the process is sandboxed at that time. |
| 122 // Instead, we map the file in and hand off the data so the sandbox won't | 126 // Instead, we map the file in and hand off the data so the sandbox won't |
| 123 // cause any problems. | 127 // cause any problems. |
| 124 | 128 |
| 125 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever | 129 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever |
| 126 // be released. | 130 // be released. |
| 127 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile, mapped_file, ()); | 131 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile, mapped_file, ()); |
| 128 if (!mapped_file.IsValid()) { | 132 if (!mapped_file.IsValid()) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 if (!mapped_file.Initialize(data_path)) { | 171 if (!mapped_file.Initialize(data_path)) { |
| 168 #if defined(OS_WIN) | 172 #if defined(OS_WIN) |
| 169 CHECK(false); // TODO(scottmg): http://crbug.com/445616 | 173 CHECK(false); // TODO(scottmg): http://crbug.com/445616 |
| 170 #endif | 174 #endif |
| 171 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe(); | 175 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe(); |
| 172 return false; | 176 return false; |
| 173 } | 177 } |
| 174 } | 178 } |
| 175 UErrorCode err = U_ZERO_ERROR; | 179 UErrorCode err = U_ZERO_ERROR; |
| 176 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); | 180 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); |
| 181 result = (err == U_ZERO_ERROR); |
| 177 #if defined(OS_WIN) | 182 #if defined(OS_WIN) |
| 178 CHECK(err == U_ZERO_ERROR); // TODO(scottmg): http://crbug.com/445616 | 183 CHECK(result); // TODO(scottmg): http://crbug.com/445616 |
| 179 #endif | 184 #endif |
| 180 return err == U_ZERO_ERROR; | |
| 181 #endif | 185 #endif |
| 186 |
| 187 // To respond to the timezone change properly, the default timezone |
| 188 // cache in ICU has to be populated on starting up. |
| 189 // TODO(jungshik): Some callers do not care about tz at all. If necessary, |
| 190 // add a boolean argument to this function to init'd the default tz only |
| 191 // when requested. |
| 192 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 193 if (result) |
| 194 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); |
| 195 #endif |
| 196 return result; |
| 182 } | 197 } |
| 183 #endif | 198 #endif |
| 184 | 199 |
| 185 void AllowMultipleInitializeCallsForTesting() { | 200 void AllowMultipleInitializeCallsForTesting() { |
| 186 #if !defined(NDEBUG) | 201 #if !defined(NDEBUG) |
| 187 g_check_called_once = false; | 202 g_check_called_once = false; |
| 188 #endif | 203 #endif |
| 189 } | 204 } |
| 190 | 205 |
| 191 } // namespace i18n | 206 } // namespace i18n |
| 192 } // namespace base | 207 } // namespace base |
| OLD | NEW |