summaryrefslogtreecommitdiff
path: root/libandroidicuinit/IcuRegistration.h
blob: ded6f5bf9563aebda00d0400334627a4105caad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ICU_REGISTRATION_H_included
#define ICU_REGISTRATION_H_included

#include <memory>
#include <string>
#include <cstdio>

#ifdef __ANDROID__
  #include <android-base/logging.h>
  #include <android-base/unique_fd.h>
  #include <log/log.h>
  #define AICU_LOGE(...) ALOGE(__VA_ARGS__)
  #define AICU_LOGW(...) ALOGW(__VA_ARGS__)
  #define AICU_LOGD(...) ALOGD(__VA_ARGS__)
  #define AICU_LOGV(...) ALOGV(__VA_ARGS__)
#else
  // http://b/171371690 Avoid dependency on liblog and libbase on host for
  // downstream unbundled branches. In this case, liblog and libbase are not
  // very useful on host and we just try to avoid it here in our best effort.

  // Check if a host should log a message.
  //
  // This method checks the priority argument against the wildcard level set
  // in the ANDROID_LOG_TAGS environment variable. The priority specified
  // corresponds to the standard Android set:
  //
  //   V - verbose    D - debug
  //   I - info       W - warn
  //   E - error      F - fatal
  //
  // If the ANDROID_LOG_TAGS variable is not set then this method returns true.
  // Otherwise, the priority is compared to the level in ANDROID_LOG_TAGS.
  //
  // Example: if ANDROID_LOG_TAGS has the value "*:W" then this method will
  // return true if the priority is warn or above.
  bool AIcuHostShouldLog(char priority);

  #define AICU_LOG_PRINTLN(priority, ...)       \
    do {                                        \
      if (AIcuHostShouldLog(priority)) {        \
        fprintf(stderr, __VA_ARGS__);           \
        fputc('\n', stderr);                    \
      }                                         \
    } while (0)
  #define AICU_LOGE(...) AICU_LOG_PRINTLN('E', __VA_ARGS__)
  #define AICU_LOGW(...) AICU_LOG_PRINTLN('W', __VA_ARGS__)
  #define AICU_LOGD(...) AICU_LOG_PRINTLN('D', __VA_ARGS__)
  #define AICU_LOGV(...) AICU_LOG_PRINTLN('V', __VA_ARGS__)
  #ifndef CHECK
    #define CHECK(cond)       \
      if (!(cond)) {             \
        AICU_LOGE(#cond "\n");     \
        abort();              \
      }
  #endif
#endif

namespace androidicuinit {
namespace impl {

/*
 * Handles ICU data mapping for a single ICU .dat file.
 * The Create method handles mapping the file into memory and calling
 * udata_setCommonData(). The file is unmapped on object destruction.
 */
class IcuDataMap final {
 public:
  // Maps in ICU data at the path and call udata_setCommonData(), returning
  // null if it failed (prints error to ALOGE).
  static std::unique_ptr<IcuDataMap> Create(const std::string& path);
  // Unmaps the ICU data.
  ~IcuDataMap();

 private:
  IcuDataMap(const std::string& path)
      : path_(path), data_(nullptr), data_length_(0) {}
  bool TryMap();
  bool TryUnmap();

  std::string path_;    // Save for error messages.
  void* data_;          // Save for munmap.
  size_t data_length_;  // Save for munmap.

  // Disable copy constructor and assignment operator
  IcuDataMap(const IcuDataMap&) = delete;
  void operator=(const IcuDataMap&) = delete;
};

}  // namespace impl

/*
 * Handles the mapping of all ICU data files into memory for the various files
 * used on Android. All data files are unmapped on object destruction.
 */
class IcuRegistration final {
 public:
  static void Register();
  static void Deregister();

  // Required to be public so it can be destructed by unique_ptr.
  ~IcuRegistration();

 private:
  IcuRegistration();

  static bool pathExists(const std::string& path);
  static std::string getDataTimeZonePath();
  static std::string getTimeZoneModulePath();
  static std::string getI18nModulePath();

  std::unique_ptr<impl::IcuDataMap> icu_datamap_from_data_;
  std::unique_ptr<impl::IcuDataMap> icu_datamap_from_tz_module_;
  std::unique_ptr<impl::IcuDataMap> icu_datamap_from_i18n_module_;

  // Disable copy constructor and assignment operator
  IcuRegistration(const IcuRegistration&) = delete;
  void operator=(const IcuRegistration&) = delete;
};

}  // namespace androidicuinit

/**
 * Initialize the ICU and load the data from .dat files from system image and
 * various mainline modules.
 * If ICU has already been registered, the function calls abort() and the process terminates.
 * This function is NOT thread-safe.
 */
void android_icu_register();

/**
 * Unregister and unload the data. After this call, user can re-register.
 */
void android_icu_deregister();

/**
 * @return true if ICU has been registered.
 */
bool android_icu_is_registered();


#endif  // ICU_REGISTRATION_H_included