xref: /aosp_15_r20/external/icu/libandroidicuinit/IcuRegistration.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ICU_REGISTRATION_H_included
18 #define ICU_REGISTRATION_H_included
19 
20 #include <memory>
21 #include <string>
22 #include <cstdio>
23 
24 
25 /**
26  * def NO_ANDROID_LOGGING
27  *   This flag turns off the usage of liblog, and logs into stderr instead.
28  *   This is not expected to be used in Android platform build, but is useful
29  *   when building this ICU4C for unbundled library or app.
30  */
31 #if defined(__ANDROID__) && !defined(NO_ANDROID_LIBLOG)
32   #include <android-base/logging.h>
33   #include <android-base/unique_fd.h>
34   #include <log/log.h>
35   #define AICU_LOGE(...) ALOGE(__VA_ARGS__)
36   #define AICU_LOGW(...) ALOGW(__VA_ARGS__)
37   #define AICU_LOGD(...) ALOGD(__VA_ARGS__)
38   #define AICU_LOGV(...) ALOGV(__VA_ARGS__)
39 #else
40   // http://b/171371690 Avoid dependency on liblog and libbase on host for
41   // downstream unbundled branches. In this case, liblog and libbase are not
42   // very useful on host and we just try to avoid it here in our best effort.
43 
44   // Check if a host should log a message.
45   //
46   // This method checks the priority argument against the wildcard level set
47   // in the ANDROID_LOG_TAGS environment variable. The priority specified
48   // corresponds to the standard Android set:
49   //
50   //   V - verbose    D - debug
51   //   I - info       W - warn
52   //   E - error      F - fatal
53   //
54   // If the ANDROID_LOG_TAGS variable is not set then this method returns true.
55   // Otherwise, the priority is compared to the level in ANDROID_LOG_TAGS.
56   //
57   // Example: if ANDROID_LOG_TAGS has the value "*:W" then this method will
58   // return true if the priority is warn or above.
59   bool AIcuHostShouldLog(char priority);
60 
61   #define AICU_LOG_PRINTLN(priority, ...)       \
62     do {                                        \
63       if (AIcuHostShouldLog(priority)) {        \
64         fprintf(stderr, __VA_ARGS__);           \
65         fputc('\n', stderr);                    \
66       }                                         \
67     } while (0)
68   #define AICU_LOGE(...) AICU_LOG_PRINTLN('E', __VA_ARGS__)
69   #define AICU_LOGW(...) AICU_LOG_PRINTLN('W', __VA_ARGS__)
70   #define AICU_LOGD(...) AICU_LOG_PRINTLN('D', __VA_ARGS__)
71   #define AICU_LOGV(...) AICU_LOG_PRINTLN('V', __VA_ARGS__)
72   #ifndef CHECK
73     #define CHECK(cond)       \
74       if (!(cond)) {             \
75         AICU_LOGE(#cond "\n");     \
76         abort();              \
77       }
78   #endif
79 #endif
80 
81 namespace androidicuinit {
82 namespace impl {
83 
84 /*
85  * Handles ICU data mapping for a single ICU .dat file.
86  * The Create method handles mapping the file into memory and calling
87  * udata_setCommonData(). The file is unmapped on object destruction.
88  */
89 class IcuDataMap final {
90  public:
91   // Maps in ICU data at the path and call udata_setCommonData(), returning
92   // null if it failed (prints error to ALOGE).
93   static std::unique_ptr<IcuDataMap> Create(const std::string& path);
94   // Unmaps the ICU data.
95   ~IcuDataMap();
96 
97  private:
IcuDataMap(const std::string & path)98   IcuDataMap(const std::string& path)
99       : path_(path), data_(nullptr), data_length_(0) {}
100   bool TryMap();
101   bool TryUnmap();
102 
103   std::string path_;    // Save for error messages.
104   void* data_;          // Save for munmap.
105   size_t data_length_;  // Save for munmap.
106 
107   // Disable copy constructor and assignment operator
108   IcuDataMap(const IcuDataMap&) = delete;
109   void operator=(const IcuDataMap&) = delete;
110 };
111 
112 }  // namespace impl
113 
114 /*
115  * Handles the mapping of all ICU data files into memory for the various files
116  * used on Android. All data files are unmapped on object destruction.
117  */
118 class IcuRegistration final {
119  public:
120   static void Register();
121   static void Deregister();
122 
123   // Required to be public so it can be destructed by unique_ptr.
124   ~IcuRegistration();
125 
126  private:
127   IcuRegistration();
128 
129   static bool pathExists(const std::string& path);
130   static std::string getTimeZoneModulePath();
131   static std::string getI18nModulePath();
132 
133   std::unique_ptr<impl::IcuDataMap> icu_datamap_from_tz_module_;
134   std::unique_ptr<impl::IcuDataMap> icu_datamap_from_i18n_module_;
135 
136   // Disable copy constructor and assignment operator
137   IcuRegistration(const IcuRegistration&) = delete;
138   void operator=(const IcuRegistration&) = delete;
139 };
140 
141 }  // namespace androidicuinit
142 
143 /**
144  * Initialize the ICU and load the data from .dat files from system image and
145  * various mainline modules.
146  * If ICU has already been registered, the function calls abort() and the process terminates.
147  * This function is NOT thread-safe.
148  */
149 void android_icu_register();
150 
151 /**
152  * Unregister and unload the data. After this call, user can re-register.
153  */
154 void android_icu_deregister();
155 
156 /**
157  * @return true if ICU has been registered.
158  */
159 bool android_icu_is_registered();
160 
161 
162 #endif  // ICU_REGISTRATION_H_included
163