| 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/environment.h" |
| 15 | #include "base/files/file_path.h" |
| 16 | #include "base/files/file_util.h" |
| 17 | #include "base/files/memory_mapped_file.h" |
| 18 | #include "base/logging.h" |
| 19 | #include "base/metrics/histogram_functions.h" |
| 20 | #include "base/metrics/metrics_hashes.h" |
| 21 | #include "base/path_service.h" |
| 22 | #include "base/strings/string_util.h" |
| 23 | #include "base/strings/sys_string_conversions.h" |
| 24 | #include "build/build_config.h" |
| 25 | #include "build/chromecast_buildflags.h" |
| 26 | #include "third_party/icu/source/common/unicode/putil.h" |
| 27 | #include "third_party/icu/source/common/unicode/udata.h" |
| 28 | #include "third_party/icu/source/common/unicode/utrace.h" |
| 29 |
| 30 | #if defined(OS_ANDROID) |
| 31 | #include "base/android/apk_assets.h" |
| 32 | #include "base/android/timezone_utils.h" |
| 33 | #endif |
| 34 |
| 35 | #if defined(OS_IOS) |
| 36 | #include "base/ios/ios_util.h" |
| 37 | #endif |
| 38 |
| 39 | #if defined(OS_MACOSX) |
| 40 | #include "base/mac/foundation_util.h" |
| 41 | #endif |
| 42 |
| 43 | #if defined(OS_FUCHSIA) |
| 44 | #include "base/fuchsia/intl_profile_watcher.h" |
| 45 | #endif |
| 46 |
| 47 | #if defined(OS_ANDROID) || defined(OS_FUCHSIA) |
| 48 | #include "third_party/icu/source/common/unicode/unistr.h" |
| 49 | #endif |
| 50 |
| 51 | #if defined(OS_ANDROID) || defined(OS_FUCHSIA) || \ |
| 52 | (defined(OS_LINUX) && !BUILDFLAG(IS_CHROMECAST)) |
| 53 | #include "third_party/icu/source/i18n/unicode/timezone.h" |
| 54 | #endif |
| 55 |
| 56 | namespace base { |
| 57 | namespace i18n { |
| 58 |
| 59 | #if !defined(OS_NACL) |
| 60 | namespace { |
| 61 |
| 62 | #if DCHECK_IS_ON() |
| 63 | // Assert that we are not called more than once. Even though calling this |
| 64 | // function isn't harmful (ICU can handle it), being called twice probably |
| 65 | // indicates a programming error. |
| 66 | bool g_check_called_once = true; |
| 67 | bool g_called_once = false; |
| 68 | #endif // DCHECK_IS_ON() |
| 69 |
| 70 | #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) |
| 71 |
| 72 | // To debug http://crbug.com/445616. |
| 73 | int g_debug_icu_last_error; |
| 74 | int g_debug_icu_load; |
| 75 | int g_debug_icu_pf_error_details; |
| 76 | int g_debug_icu_pf_last_error; |
| 77 | #if defined(OS_WIN) |
| 78 | wchar_t g_debug_icu_pf_filename[_MAX_PATH]; |
| 79 | #endif // OS_WIN |
| 80 | // Use an unversioned file name to simplify a icu version update down the road. |
| 81 | // No need to change the filename in multiple places (gyp files, windows |
| 82 | // build pkg configurations, etc). 'l' stands for Little Endian. |
| 83 | // This variable is exported through the header file. |
| 84 | const char kIcuDataFileName[] = "icudtl.dat"; |
| 85 | const char kIcuExtraDataFileName[] = "icudtl_extra.dat"; |
| 86 |
| 87 | // Time zone data loading. |
| 88 | // For now, only Fuchsia has a meaningful use case for this feature, so it is |
| 89 | // only implemented for OS_FUCHSIA. |
| 90 | #if defined(OS_FUCHSIA) |
| 91 | // The environment variable used to point the ICU data loader to the directory |
| 92 | // containing time zone data. This is available from ICU version 54. The env |
| 93 | // variable approach is antiquated by today's standards (2019), but is the |
| 94 | // recommended way to configure ICU. |
| 95 | // |
| 96 | // See for details: http://userguide.icu-project.org/datetime/timezone |
| 97 | const char kIcuTimeZoneEnvVariable[] = "ICU_TIMEZONE_FILES_DIR"; |
| 98 |
| 99 | // We assume that Fuchsia will provide time zone data at this path for Chromium |
| 100 | // to load, and that the path will be timely updated when Fuchsia needs to |
| 101 | // uprev the ICU version it is using. There are unit tests that will fail at |
| 102 | // Fuchsia roll time in case either Chromium or Fuchsia get upgraded to |
| 103 | // mutually incompatible ICU versions. That should be enough to alert the |
| 104 | // developers of the need to keep ICU library versions in ICU and Fuchsia in |
| 105 | // reasonable sync. |
| 106 | const char kIcuTimeZoneDataDir[] = "/config/data/tzdata/icu/44/le"; |
| 107 | #endif // defined(OS_FUCHSIA) |
| 108 |
| 109 | #if defined(OS_ANDROID) |
| 110 | const char kAssetsPathPrefix[] = "assets/"; |
| 111 | #endif // defined(OS_ANDROID) |
| 112 |
| 113 | // File handle intentionally never closed. Not using File here because its |
| 114 | // Windows implementation guards against two instances owning the same |
| 115 | // PlatformFile (which we allow since we know it is never freed). |
| 116 | PlatformFile g_icudtl_pf = kInvalidPlatformFile; |
| 117 | MemoryMappedFile* g_icudtl_mapped_file = nullptr; |
| 118 | MemoryMappedFile::Region g_icudtl_region; |
| 119 | PlatformFile g_icudtl_extra_pf = kInvalidPlatformFile; |
| 120 | MemoryMappedFile* g_icudtl_extra_mapped_file = nullptr; |
| 121 | MemoryMappedFile::Region g_icudtl_extra_region; |
| 122 |
| 123 | #if defined(OS_FUCHSIA) |
| 124 | // The directory from which the ICU data loader will be configured to load time |
| 125 | // zone data. It is only changed by SetIcuTimeZoneDataDirForTesting(). |
| 126 | const char* g_icu_time_zone_data_dir = kIcuTimeZoneDataDir; |
| 127 | #endif // defined(OS_FUCHSIA) |
| 128 |
| 129 | struct PfRegion { |
| 130 | public: |
| 131 | PlatformFile pf; |
| 132 | MemoryMappedFile::Region region; |
| 133 | }; |
| 134 |
| 135 | std::unique_ptr<PfRegion> OpenIcuDataFile(const std::string& filename) { |
| 136 | auto result = std::make_unique<PfRegion>(); |
| 137 | #if defined(OS_ANDROID) |
| 138 | result->pf = |
| 139 | android::OpenApkAsset(kAssetsPathPrefix + filename, &result->region); |
| 140 | if (result->pf != -1) { |
| 141 | return result; |
| 142 | } |
| 143 | #endif // defined(OS_ANDROID) |
| 144 | // For unit tests, data file is located on disk, so try there as a fallback. |
| 145 | #if !defined(OS_MACOSX) |
| 146 | FilePath data_path; |
| 147 | if (!PathService::Get(DIR_ASSETS, &data_path)) { |
| 148 | LOG(ERROR) << "Can't find " << filename; |
| 149 | return nullptr; |
| 150 | } |
| 151 | #if defined(OS_WIN) |
| 152 | // TODO(brucedawson): http://crbug.com/445616 |
| 153 | wchar_t tmp_buffer[_MAX_PATH] = {0}; |
| 154 | wcscpy_s(tmp_buffer, data_path.value().c_str()); |
| 155 | debug::Alias(tmp_buffer); |
| 156 | #endif |
| 157 | data_path = data_path.AppendASCII(filename); |
| 158 |
| 159 | #if defined(OS_WIN) |
| 160 | // TODO(brucedawson): http://crbug.com/445616 |
| 161 | wchar_t tmp_buffer2[_MAX_PATH] = {0}; |
| 162 | wcscpy_s(tmp_buffer2, data_path.value().c_str()); |
| 163 | debug::Alias(tmp_buffer2); |
| 164 | #endif |
| 165 |
| 166 | #else // !defined(OS_MACOSX) |
| 167 | // Assume it is in the framework bundle's Resources directory. |
| 168 | ScopedCFTypeRef<CFStringRef> data_file_name(SysUTF8ToCFStringRef(filename)); |
| 169 | FilePath data_path = mac::PathForFrameworkBundleResource(data_file_name); |
| 170 | #if defined(OS_IOS) |
| 171 | FilePath override_data_path = ios::FilePathOfEmbeddedICU(); |
| 172 | if (!override_data_path.empty()) { |
| 173 | data_path = override_data_path; |
| 174 | } |
| 175 | #endif // !defined(OS_IOS) |
| 176 | if (data_path.empty()) { |
| 177 | LOG(ERROR) << filename << " not found in bundle"; |
| 178 | return nullptr; |
| 179 | } |
| 180 | #endif // !defined(OS_MACOSX) |
| 181 | File file(data_path, File::FLAG_OPEN | File::FLAG_READ); |
| 182 | if (file.IsValid()) { |
| 183 | // TODO(brucedawson): http://crbug.com/445616. |
| 184 | g_debug_icu_pf_last_error = 0; |
| 185 | g_debug_icu_pf_error_details = 0; |
| 186 | #if defined(OS_WIN) |
| 187 | g_debug_icu_pf_filename[0] = 0; |
| 188 | #endif // OS_WIN |
| 189 |
| 190 | result->pf = file.TakePlatformFile(); |
| 191 | result->region = MemoryMappedFile::Region::kWholeFile; |
| 192 | } |
| 193 | #if defined(OS_WIN) |
| 194 | else { |
| 195 | // TODO(brucedawson): http://crbug.com/445616. |
| 196 | g_debug_icu_pf_last_error = ::GetLastError(); |
| 197 | g_debug_icu_pf_error_details = file.error_details(); |
| 198 | wcscpy_s(g_debug_icu_pf_filename, data_path.value().c_str()); |
| 199 | } |
| 200 | #endif // OS_WIN |
| 201 |
| 202 | return result; |
| 203 | } |
| 204 |
| 205 | void LazyOpenIcuDataFile() { |
| 206 | if (g_icudtl_pf != kInvalidPlatformFile) { |
| 207 | return; |
| 208 | } |
| 209 | auto pf_region = OpenIcuDataFile(kIcuDataFileName); |
| 210 | if (!pf_region) { |
| 211 | return; |
| 212 | } |
| 213 | g_icudtl_pf = pf_region->pf; |
| 214 | g_icudtl_region = pf_region->region; |
| 215 | } |
| 216 |
| 217 | // Configures ICU to load external time zone data, if appropriate. |
| 218 | void InitializeExternalTimeZoneData() { |
| 219 | #if defined(OS_FUCHSIA) |
| 220 | if (!base::DirectoryExists(base::FilePath(g_icu_time_zone_data_dir))) { |
| 221 | // TODO(https://crbug.com/1061262): Make this FATAL unless expected. |
| 222 | PLOG(WARNING) << "Could not open: '" << g_icu_time_zone_data_dir |
| 223 | << "'. Using built-in timezone database"; |
| 224 | return; |
| 225 | } |
| 226 |
| 227 | // Set the environment variable to override the location used by ICU. |
| 228 | // Loading can still fail if the directory is empty or its data is invalid. |
| 229 | std::unique_ptr<base::Environment> env = base::Environment::Create(); |
| 230 | env->SetVar(kIcuTimeZoneEnvVariable, g_icu_time_zone_data_dir); |
| 231 | #endif // defined(OS_FUCHSIA) |
| 232 | } |
| 233 |
| 234 | int LoadIcuData(PlatformFile data_fd, |
| 235 | const MemoryMappedFile::Region& data_region, |
| 236 | std::unique_ptr<MemoryMappedFile>* out_mapped_data_file, |
| 237 | UErrorCode* out_error_code) { |
| 238 | InitializeExternalTimeZoneData(); |
| 239 |
| 240 | if (data_fd == kInvalidPlatformFile) { |
| 241 | LOG(ERROR) << "Invalid file descriptor to ICU data received."; |
| 242 | return 1; // To debug http://crbug.com/445616. |
| 243 | } |
| 244 |
| 245 | out_mapped_data_file->reset(new MemoryMappedFile()); |
| 246 | if (!(*out_mapped_data_file)->Initialize(File(data_fd), data_region)) { |
| 247 | LOG(ERROR) << "Couldn't mmap icu data file"; |
| 248 | return 2; // To debug http://crbug.com/445616. |
| 249 | } |
| 250 |
| 251 | (*out_error_code) = U_ZERO_ERROR; |
| 252 | udata_setCommonData(const_cast<uint8_t*>((*out_mapped_data_file)->data()), |
| 253 | out_error_code); |
| 254 | if (U_FAILURE(*out_error_code)) { |
| 255 | LOG(ERROR) << "Failed to initialize ICU with data file: " |
| 256 | << u_errorName(*out_error_code); |
| 257 | return 3; // To debug http://crbug.com/445616. |
| 258 | } |
| 259 |
| 260 | return 0; |
| 261 | } |
| 262 |
| 263 | bool InitializeICUWithFileDescriptorInternal( |
| 264 | PlatformFile data_fd, |
| 265 | const MemoryMappedFile::Region& data_region) { |
| 266 | // This can be called multiple times in tests. |
| 267 | if (g_icudtl_mapped_file) { |
| 268 | g_debug_icu_load = 0; // To debug http://crbug.com/445616. |
| 269 | return true; |
| 270 | } |
| 271 |
| 272 | std::unique_ptr<MemoryMappedFile> mapped_file; |
| 273 | UErrorCode err; |
| 274 | g_debug_icu_load = LoadIcuData(data_fd, data_region, &mapped_file, &err); |
| 275 | if (g_debug_icu_load == 1 || g_debug_icu_load == 2) { |
| 276 | return false; |
| 277 | } |
| 278 | g_icudtl_mapped_file = mapped_file.release(); |
| 279 |
| 280 | if (g_debug_icu_load == 3) { |
| 281 | g_debug_icu_last_error = err; |
| 282 | } |
| 283 |
| 284 | // Never try to load ICU data from files. |
| 285 | udata_setFileAccess(UDATA_ONLY_PACKAGES, &err); |
| 286 | return U_SUCCESS(err); |
| 287 | } |
| 288 |
| 289 | bool InitializeICUFromDataFile() { |
| 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 | LazyOpenIcuDataFile(); |
| 295 | bool result = |
| 296 | InitializeICUWithFileDescriptorInternal(g_icudtl_pf, g_icudtl_region); |
| 297 |
| 298 | #if defined(OS_WIN) |
| 299 | int debug_icu_load = g_debug_icu_load; |
| 300 | debug::Alias(&debug_icu_load); |
| 301 | int debug_icu_last_error = g_debug_icu_last_error; |
| 302 | debug::Alias(&debug_icu_last_error); |
| 303 | int debug_icu_pf_last_error = g_debug_icu_pf_last_error; |
| 304 | debug::Alias(&debug_icu_pf_last_error); |
| 305 | int debug_icu_pf_error_details = g_debug_icu_pf_error_details; |
| 306 | debug::Alias(&debug_icu_pf_error_details); |
| 307 | wchar_t debug_icu_pf_filename[_MAX_PATH] = {0}; |
| 308 | wcscpy_s(debug_icu_pf_filename, g_debug_icu_pf_filename); |
| 309 | debug::Alias(&debug_icu_pf_filename); |
| 310 | CHECK(result); // TODO(brucedawson): http://crbug.com/445616 |
| 311 | #endif // defined(OS_WIN) |
| 312 |
| 313 | return result; |
| 314 | } |
| 315 | #endif // (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) |
| 316 |
| 317 | // Explicitly initialize ICU's time zone if necessary. |
| 318 | // On some platforms, the time zone must be explicitly initialized zone rather |
| 319 | // than relying on ICU's internal initialization. |
| 320 | void InitializeIcuTimeZone() { |
| 321 | #if defined(OS_ANDROID) |
| 322 | // On Android, we can't leave it up to ICU to set the default time zone |
| 323 | // because ICU's time zone detection does not work in many time zones (e.g. |
| 324 | // Australia/Sydney, Asia/Seoul, Europe/Paris ). Use JNI to detect the host |
| 325 | // time zone and set the ICU default time zone accordingly in advance of |
| 326 | // actual use. See crbug.com/722821 and |
| 327 | // https://ssl.icu-project.org/trac/ticket/13208 . |
| 328 | string16 zone_id = android::GetDefaultTimeZoneId(); |
| 329 | icu::TimeZone::adoptDefault(icu::TimeZone::createTimeZone( |
| 330 | icu::UnicodeString(FALSE, zone_id.data(), zone_id.length()))); |
| 331 | #elif defined(OS_FUCHSIA) |
| 332 | // The platform-specific mechanisms used by ICU's detectHostTimeZone() to |
| 333 | // determine the default time zone will not work on Fuchsia. Therefore, |
| 334 | // proactively set the default system. |
| 335 | // This is also required by TimeZoneMonitorFuchsia::ProfileMayHaveChanged(), |
| 336 | // which uses the current default to detect whether the time zone changed in |
| 337 | // the new profile. |
| 338 | // If the system time zone cannot be obtained or is not understood by ICU, |
| 339 | // the "unknown" time zone will be returned by createTimeZone() and used. |
| 340 | std::string zone_id = |
| 341 | fuchsia::IntlProfileWatcher::GetPrimaryTimeZoneIdForIcuInitialization(); |
| 342 | icu::TimeZone::adoptDefault( |
| 343 | icu::TimeZone::createTimeZone(icu::UnicodeString::fromUTF8(zone_id))); |
| 344 | #elif defined(OS_LINUX) && !BUILDFLAG(IS_CHROMECAST) |
| 345 | // To respond to the time zone change properly, the default time zone |
| 346 | // cache in ICU has to be populated on starting up. |
| 347 | // See TimeZoneMonitorLinux::NotifyClientsFromImpl(). |
| 348 | std::unique_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); |
| 349 | #endif // defined(OS_ANDROID) |
| 350 | } |
| 351 |
| 352 | const char kICUDataFile[] = "ICU.DataFile"; |
| 353 | const char kICUCreateInstance[] = "ICU.CreateInstance"; |
| 354 |
| 355 | enum class ICUCreateInstance { |
| 356 | kCharacterBreakIterator = 0, |
| 357 | kWordBreakIterator = 1, |
| 358 | kLineBreakIterator = 2, |
| 359 | kLineBreakIteratorTypeLoose = 3, |
| 360 | kLineBreakIteratorTypeNormal = 4, |
| 361 | kLineBreakIteratorTypeStrict = 5, |
| 362 | kSentenceBreakIterator = 6, |
| 363 | kTitleBreakIterator = 7, |
| 364 | kThaiBreakEngine = 8, |
| 365 | kLaoBreakEngine = 9, |
| 366 | kBurmeseBreakEngine = 10, |
| 367 | kKhmerBreakEngine = 11, |
| 368 | kChineseJapaneseBreakEngine = 12, |
| 369 |
| 370 | kMaxValue = kChineseJapaneseBreakEngine |
| 371 | }; |
| 372 |
| 373 | // Callback functions to report the opening of ICU Data File, and creation of |
| 374 | // key objects to UMA. This help us to understand what built-in ICU data files |
| 375 | // are rarely used in the user's machines and the distribution of ICU usage. |
| 376 | static void U_CALLCONV TraceICUEntry(const void*, int32_t fn_number) { |
| 377 | switch (fn_number) { |
| 378 | case UTRACE_UBRK_CREATE_CHARACTER: |
| 379 | base::UmaHistogramEnumeration(kICUCreateInstance, |
| 380 | ICUCreateInstance::kCharacterBreakIterator); |
| 381 | break; |
| 382 | case UTRACE_UBRK_CREATE_SENTENCE: |
| 383 | base::UmaHistogramEnumeration(kICUCreateInstance, |
| 384 | ICUCreateInstance::kSentenceBreakIterator); |
| 385 | break; |
| 386 | case UTRACE_UBRK_CREATE_TITLE: |
| 387 | base::UmaHistogramEnumeration(kICUCreateInstance, |
| 388 | ICUCreateInstance::kTitleBreakIterator); |
| 389 | break; |
| 390 | case UTRACE_UBRK_CREATE_WORD: |
| 391 | base::UmaHistogramEnumeration(kICUCreateInstance, |
| 392 | ICUCreateInstance::kWordBreakIterator); |
| 393 | break; |
| 394 | default: |
| 395 | return; |
| 396 | } |
| 397 | } |
| 398 |
| 399 | static void U_CALLCONV TraceICUData(const void* context, |
| 400 | int32_t fn_number, |
| 401 | int32_t level, |
| 402 | const char* fmt, |
| 403 | va_list args) { |
| 404 | switch (fn_number) { |
| 405 | case UTRACE_UDATA_DATA_FILE: { |
| 406 | std::string icu_data_file_name(va_arg(args, const char*)); |
| 407 | va_end(args); |
| 408 | // Skip icu version specified prefix if exist. |
| 409 | // path is prefixed with icu version prefix such as "icudt65l-". |
| 410 | // Histogram only the part after the -. |
| 411 | if (icu_data_file_name.find("icudt") == 0) { |
| 412 | size_t dash = icu_data_file_name.find("-"); |
| 413 | if (dash != std::string::npos) { |
| 414 | icu_data_file_name = icu_data_file_name.substr(dash + 1); |
| 415 | } |
| 416 | } |
| 417 | // UmaHistogramSparse should track less than 100 values. |
| 418 | // We currently have about total 55 built-in data files inside ICU |
| 419 | // so it fit the UmaHistogramSparse usage. |
| 420 | int hash = base::HashMetricName(icu_data_file_name); |
| 421 | base::UmaHistogramSparse(kICUDataFile, hash); |
| 422 | return; |
| 423 | } |
| 424 | case UTRACE_UBRK_CREATE_LINE: { |
| 425 | const char* lb_type = va_arg(args, const char*); |
| 426 | va_end(args); |
| 427 | ICUCreateInstance value; |
| 428 | switch (lb_type[0]) { |
| 429 | case '\0': |
| 430 | value = ICUCreateInstance::kLineBreakIterator; |
| 431 | break; |
| 432 | case 'l': |
| 433 | DCHECK(strcmp(lb_type, "loose") == 0); |
| 434 | value = ICUCreateInstance::kLineBreakIteratorTypeLoose; |
| 435 | break; |
| 436 | case 'n': |
| 437 | DCHECK(strcmp(lb_type, "normal") == 0); |
| 438 | value = ICUCreateInstance::kLineBreakIteratorTypeNormal; |
| 439 | break; |
| 440 | case 's': |
| 441 | DCHECK(strcmp(lb_type, "strict") == 0); |
| 442 | value = ICUCreateInstance::kLineBreakIteratorTypeStrict; |
| 443 | break; |
| 444 | default: |
| 445 | return; |
| 446 | } |
| 447 | base::UmaHistogramEnumeration(kICUCreateInstance, value); |
| 448 | return; |
| 449 | } |
| 450 | case UTRACE_UBRK_CREATE_BREAK_ENGINE: { |
| 451 | const char* script = va_arg(args, const char*); |
| 452 | va_end(args); |
| 453 | ICUCreateInstance value; |
| 454 | switch (script[0]) { |
| 455 | case 'H': |
| 456 | DCHECK(strcmp(script, "Hani") == 0); |
| 457 | value = ICUCreateInstance::kChineseJapaneseBreakEngine; |
| 458 | break; |
| 459 | case 'K': |
| 460 | DCHECK(strcmp(script, "Khmr") == 0); |
| 461 | value = ICUCreateInstance::kKhmerBreakEngine; |
| 462 | break; |
| 463 | case 'L': |
| 464 | DCHECK(strcmp(script, "Laoo") == 0); |
| 465 | value = ICUCreateInstance::kLaoBreakEngine; |
| 466 | break; |
| 467 | case 'M': |
| 468 | DCHECK(strcmp(script, "Mymr") == 0); |
| 469 | value = ICUCreateInstance::kBurmeseBreakEngine; |
| 470 | break; |
| 471 | case 'T': |
| 472 | DCHECK(strcmp(script, "Thai") == 0); |
| 473 | value = ICUCreateInstance::kThaiBreakEngine; |
| 474 | break; |
| 475 | default: |
| 476 | return; |
| 477 | } |
| 478 | base::UmaHistogramEnumeration(kICUCreateInstance, value); |
| 479 | return; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 |
| 484 | // Common initialization to run regardless of how ICU is initialized. |
| 485 | // There are multiple exposed InitializeIcu* functions. This should be called |
| 486 | // as at the end of (the last functions in the sequence of) these functions. |
| 487 | bool DoCommonInitialization() { |
| 488 | // TODO(jungshik): Some callers do not care about tz at all. If necessary, |
| 489 | // add a boolean argument to this function to init the default tz only |
| 490 | // when requested. |
| 491 | InitializeIcuTimeZone(); |
| 492 |
| 493 | const void* context = nullptr; |
| 494 | utrace_setFunctions(context, TraceICUEntry, nullptr, TraceICUData); |
| 495 | utrace_setLevel(UTRACE_VERBOSE); |
| 496 | return true; |
| 497 | } |
| 498 |
| 499 | } // namespace |
| 500 |
| 501 | #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) |
| 502 | bool InitializeExtraICUWithFileDescriptor( |
| 503 | PlatformFile data_fd, |
| 504 | const MemoryMappedFile::Region& data_region) { |
| 505 | if (g_icudtl_pf != kInvalidPlatformFile) { |
| 506 | // Must call InitializeExtraICUWithFileDescriptor() before |
| 507 | // InitializeICUWithFileDescriptor(). |
| 508 | return false; |
| 509 | } |
| 510 | std::unique_ptr<MemoryMappedFile> mapped_file; |
| 511 | UErrorCode err; |
| 512 | if (LoadIcuData(data_fd, data_region, &mapped_file, &err) != 0) { |
| 513 | return false; |
| 514 | } |
| 515 | g_icudtl_extra_mapped_file = mapped_file.release(); |
| 516 | return true; |
| 517 | } |
| 518 |
| 519 | bool InitializeICUWithFileDescriptor( |
| 520 | PlatformFile data_fd, |
| 521 | const MemoryMappedFile::Region& data_region) { |
| 522 | #if DCHECK_IS_ON() |
| 523 | DCHECK(!g_check_called_once || !g_called_once); |
| 524 | g_called_once = true; |
| 525 | #endif |
| 526 | if (!InitializeICUWithFileDescriptorInternal(data_fd, data_region)) |
| 527 | return false; |
| 528 |
| 529 | return DoCommonInitialization(); |
| 530 | } |
| 531 |
| 532 | PlatformFile GetIcuDataFileHandle(MemoryMappedFile::Region* out_region) { |
| 533 | CHECK_NE(g_icudtl_pf, kInvalidPlatformFile); |
| 534 | *out_region = g_icudtl_region; |
| 535 | return g_icudtl_pf; |
| 536 | } |
| 537 |
| 538 | PlatformFile GetIcuExtraDataFileHandle(MemoryMappedFile::Region* out_region) { |
| 539 | if (g_icudtl_extra_pf == kInvalidPlatformFile) { |
| 540 | return kInvalidPlatformFile; |
| 541 | } |
| 542 | *out_region = g_icudtl_extra_region; |
| 543 | return g_icudtl_extra_pf; |
| 544 | } |
| 545 |
| 546 | bool InitializeExtraICU() { |
| 547 | if (g_icudtl_pf != kInvalidPlatformFile) { |
| 548 | // Must call InitializeExtraICU() before InitializeICU(). |
| 549 | return false; |
| 550 | } |
| 551 | auto pf_region = OpenIcuDataFile(kIcuExtraDataFileName); |
| 552 | if (!pf_region) { |
| 553 | return false; |
| 554 | } |
| 555 | g_icudtl_extra_pf = pf_region->pf; |
| 556 | g_icudtl_extra_region = pf_region->region; |
| 557 | std::unique_ptr<MemoryMappedFile> mapped_file; |
| 558 | UErrorCode err; |
| 559 | if (LoadIcuData(g_icudtl_extra_pf, g_icudtl_extra_region, &mapped_file, |
| 560 | &err) != 0) { |
| 561 | return false; |
| 562 | } |
| 563 | g_icudtl_extra_mapped_file = mapped_file.release(); |
| 564 | return true; |
| 565 | } |
| 566 |
| 567 | void ResetGlobalsForTesting() { |
| 568 | g_icudtl_pf = kInvalidPlatformFile; |
| 569 | g_icudtl_mapped_file = nullptr; |
| 570 | g_icudtl_extra_pf = kInvalidPlatformFile; |
| 571 | g_icudtl_extra_mapped_file = nullptr; |
| 572 | #if defined(OS_FUCHSIA) |
| 573 | g_icu_time_zone_data_dir = kIcuTimeZoneDataDir; |
| 574 | #endif // defined(OS_FUCHSIA) |
| 575 | } |
| 576 |
| 577 | #if defined(OS_FUCHSIA) |
| 578 | // |dir| must remain valid until ResetGlobalsForTesting() is called. |
| 579 | void SetIcuTimeZoneDataDirForTesting(const char* dir) { |
| 580 | g_icu_time_zone_data_dir = dir; |
| 581 | } |
| 582 | #endif // defined(OS_FUCHSIA) |
| 583 | #endif // (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) |
| 584 |
| 585 | bool InitializeICU() { |
| 586 | #if DCHECK_IS_ON() |
| 587 | DCHECK(!g_check_called_once || !g_called_once); |
| 588 | g_called_once = true; |
| 589 | #endif |
| 590 |
| 591 | #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) |
| 592 | // The ICU data is statically linked. |
| 593 | #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) |
| 594 | if (!InitializeICUFromDataFile()) |
| 595 | return false; |
| 596 | #else |
| 597 | #error Unsupported ICU_UTIL_DATA_IMPL value |
| 598 | #endif // (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) |
| 599 |
| 600 | return DoCommonInitialization(); |
| 601 | } |
| 602 |
| 603 | void AllowMultipleInitializeCallsForTesting() { |
| 604 | #if DCHECK_IS_ON() |
| 605 | g_check_called_once = false; |
| 606 | #endif |
| 607 | } |
| 608 |
| 609 | #endif // !defined(OS_NACL) |
| 610 |
| 611 | } // namespace i18n |
| 612 | } // namespace base |
| 613 |