xref: /aosp_15_r20/external/cronet/base/i18n/icu_util.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_I18N_ICU_UTIL_H_
6 #define BASE_I18N_ICU_UTIL_H_
7 
8 #include <stdint.h>
9 
10 #include "base/files/memory_mapped_file.h"
11 #include "base/i18n/base_i18n_export.h"
12 #include "build/build_config.h"
13 #include "build/chromeos_buildflags.h"
14 
15 #if BUILDFLAG(IS_CHROMEOS_LACROS)
16 #include "icu_mergeable_data_file.h"
17 #endif  // BUILDFLAG(IS_CHROMEOS_LACROS)
18 
19 #define ICU_UTIL_DATA_FILE 0
20 #define ICU_UTIL_DATA_STATIC 1
21 
22 namespace base::i18n {
23 
24 #if !BUILDFLAG(IS_NACL)
25 // Call this function to load ICU's data tables for the current process.  This
26 // function should be called before ICU is used.
27 BASE_I18N_EXPORT bool InitializeICU();
28 
29 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
30 
31 // In ChromeOS, two versions of icudtl.dat are shipped separately with Ash and
32 // Lacros. Although the two files can differ, there's a big overlap in content.
33 // We can take advantage of that and save memory by scanning the two files and
34 // merging the pages that are in common.
35 #if BUILDFLAG(IS_CHROMEOS_LACROS)
36 using IcuDataFile = IcuMergeableDataFile;
37 #else
38 // Outside of Lacros, we simply memory map the ICU data file.
39 using IcuDataFile = MemoryMappedFile;
40 #endif  // BUILDFLAG(IS_CHROMEOS_LACROS)
41 // Returns the PlatformFile and Region that was initialized by InitializeICU().
42 // Use with InitializeICUWithFileDescriptor().
43 BASE_I18N_EXPORT PlatformFile
44 GetIcuDataFileHandle(MemoryMappedFile::Region* out_region);
45 
46 // Loads ICU data file from file descriptor passed by browser process to
47 // initialize ICU in render processes.
48 BASE_I18N_EXPORT bool InitializeICUWithFileDescriptor(
49     PlatformFile data_fd,
50     const MemoryMappedFile::Region& data_region);
51 
52 // Calls `u_cleanup()` to reset the ICU library, and clears global state,
53 // notably releasing the mapped ICU data file, and handle.
54 BASE_I18N_EXPORT void ResetGlobalsForTesting();
55 
56 #if BUILDFLAG(IS_FUCHSIA)
57 // Overrides the directory used by ICU for external time zone data.
58 BASE_I18N_EXPORT void SetIcuTimeZoneDataDirForTesting(const char* dir);
59 #endif  // BUILDFLAG(IS_FUCHSIA)
60 #endif  // ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
61 
62 // In a test binary, initialize functions might be called twice.
63 BASE_I18N_EXPORT void AllowMultipleInitializeCallsForTesting();
64 #endif  // !BUILDFLAG(IS_NACL)
65 
66 }  // namespace base::i18n
67 
68 #endif  // BASE_I18N_ICU_UTIL_H_
69