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 #ifdef __ANDROID__ 25 #include <android-base/logging.h> 26 #include <android-base/unique_fd.h> 27 #include <log/log.h> 28 #define AICU_LOGE(...) ALOGE(__VA_ARGS__) 29 #define AICU_LOGW(...) ALOGW(__VA_ARGS__) 30 #define AICU_LOGD(...) ALOGD(__VA_ARGS__) 31 #define AICU_LOGV(...) ALOGV(__VA_ARGS__) 32 #else 33 // http://b/171371690 Avoid dependency on liblog and libbase on host for 34 // downstream unbundled branches. In this case, liblog and libbase are not 35 // very useful on host and we just try to avoid it here in our best effort. 36 37 // Check if a host should log a message. 38 // 39 // This method checks the priority argument against the wildcard level set 40 // in the ANDROID_LOG_TAGS environment variable. The priority specified 41 // corresponds to the standard Android set: 42 // 43 // V - verbose D - debug 44 // I - info W - warn 45 // E - error F - fatal 46 // 47 // If the ANDROID_LOG_TAGS variable is not set then this method returns true. 48 // Otherwise, the priority is compared to the level in ANDROID_LOG_TAGS. 49 // 50 // Example: if ANDROID_LOG_TAGS has the value "*:W" then this method will 51 // return true if the priority is warn or above. 52 bool AIcuHostShouldLog(char priority); 53 54 #define AICU_LOG_PRINTLN(priority, ...) \ 55 do { \ 56 if (AIcuHostShouldLog(priority)) { \ 57 fprintf(stderr, __VA_ARGS__); \ 58 fputc('\n', stderr); \ 59 } \ 60 } while (0) 61 #define AICU_LOGE(...) AICU_LOG_PRINTLN('E', __VA_ARGS__) 62 #define AICU_LOGW(...) AICU_LOG_PRINTLN('W', __VA_ARGS__) 63 #define AICU_LOGD(...) AICU_LOG_PRINTLN('D', __VA_ARGS__) 64 #define AICU_LOGV(...) AICU_LOG_PRINTLN('V', __VA_ARGS__) 65 #ifndef CHECK 66 #define CHECK(cond) \ 67 if (!(cond)) { \ 68 AICU_LOGE(#cond "\n"); \ 69 abort(); \ 70 } 71 #endif 72 #endif 73 74 namespace androidicuinit { 75 namespace impl { 76 77 /* 78 * Handles ICU data mapping for a single ICU .dat file. 79 * The Create method handles mapping the file into memory and calling 80 * udata_setCommonData(). The file is unmapped on object destruction. 81 */ 82 class IcuDataMap final { 83 public: 84 // Maps in ICU data at the path and call udata_setCommonData(), returning 85 // null if it failed (prints error to ALOGE). 86 static std::unique_ptr<IcuDataMap> Create(const std::string& path); 87 // Unmaps the ICU data. 88 ~IcuDataMap(); 89 90 private: IcuDataMap(const std::string & path)91 IcuDataMap(const std::string& path) 92 : path_(path), data_(nullptr), data_length_(0) {} 93 bool TryMap(); 94 bool TryUnmap(); 95 96 std::string path_; // Save for error messages. 97 void* data_; // Save for munmap. 98 size_t data_length_; // Save for munmap. 99 100 // Disable copy constructor and assignment operator 101 IcuDataMap(const IcuDataMap&) = delete; 102 void operator=(const IcuDataMap&) = delete; 103 }; 104 105 } // namespace impl 106 107 /* 108 * Handles the mapping of all ICU data files into memory for the various files 109 * used on Android. All data files are unmapped on object destruction. 110 */ 111 class IcuRegistration final { 112 public: 113 static void Register(); 114 static void Deregister(); 115 116 // Required to be public so it can be destructed by unique_ptr. 117 ~IcuRegistration(); 118 119 private: 120 IcuRegistration(); 121 122 static bool pathExists(const std::string& path); 123 static std::string getDataTimeZonePath(); 124 static std::string getTimeZoneModulePath(); 125 static std::string getI18nModulePath(); 126 127 std::unique_ptr<impl::IcuDataMap> icu_datamap_from_data_; 128 std::unique_ptr<impl::IcuDataMap> icu_datamap_from_tz_module_; 129 std::unique_ptr<impl::IcuDataMap> icu_datamap_from_i18n_module_; 130 131 // Disable copy constructor and assignment operator 132 IcuRegistration(const IcuRegistration&) = delete; 133 void operator=(const IcuRegistration&) = delete; 134 }; 135 136 } // namespace androidicuinit 137 138 /** 139 * Initialize the ICU and load the data from .dat files from system image and 140 * various mainline modules. 141 * If ICU has already been registered, the function calls abort() and the process terminates. 142 * This function is NOT thread-safe. 143 */ 144 void android_icu_register(); 145 146 /** 147 * Unregister and unload the data. After this call, user can re-register. 148 */ 149 void android_icu_deregister(); 150 151 /** 152 * @return true if ICU has been registered. 153 */ 154 bool android_icu_is_registered(); 155 156 157 #endif // ICU_REGISTRATION_H_included 158