1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef OHOS_RESOURCE_MANAGER_RESOURCEMANAGER_H
16 #define OHOS_RESOURCE_MANAGER_RESOURCEMANAGER_H
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 #include <memory>
22 #include <tuple>
23 #include "res_config.h"
24 
25 namespace OHOS {
26 namespace Global {
27 namespace Resource {
28 enum FunctionType {
29     SYNC = 0,
30     ASYNC = 1
31 };
32 class ResourceManager {
33 public:
34     std::pair<std::string, std::string> bundleInfo;
35 
36     int32_t userId = 0;
37 
38     typedef struct {
39         /** the raw file fd */
40         int fd;
41 
42         /** the offset from where the raw file starts in the HAP */
43         int64_t offset;
44 
45         /** the length of the raw file in the HAP. */
46         int64_t length;
47     } RawFileDescriptor;
48 
49     struct Resource {
50         /** the hap bundle name */
51         std::string bundleName;
52 
53         /** the hap module name */
54         std::string moduleName;
55 
56         /** the resource id in hap */
57         int32_t id;
58     };
59 
60     enum class NapiValueType {
61         NAPI_NUMBER = 0,
62         NAPI_STRING = 1
63     };
64 
65     virtual ~ResourceManager() = 0;
66 
67     /**
68      * Add resource of hap.
69      *
70      * @param path The path of hap.
71      * @param selectedTypes If this param is setted, will only add resource of types specified by this param,
72      *     it will be faster than add all resource, you can set this param by combining flags
73      *     named SELECT_XXX defined in res_common.h. What's more, if you call UpdateResConfig()
74      *     before calling this method, will only add resource that matching the config, for example,
75      *     only add resource of current language, which means it will be further faster.
76      * @return true if init success, else false
77      */
78     virtual bool AddResource(const char *path, const uint32_t &selectedTypes = SELECT_ALL) = 0;
79 
80     virtual RState UpdateResConfig(ResConfig &resConfig, bool isUpdateTheme = false) = 0;
81 
82     virtual void GetResConfig(ResConfig &resConfig) = 0;
83 
84     virtual RState GetStringById(uint32_t id, std::string &outValue) = 0;
85 
86     virtual RState GetStringByName(const char *name, std::string &outValue) = 0;
87 
88     virtual RState GetStringFormatById(std::string &outValue, uint32_t id, ...) = 0;
89 
90     virtual RState GetStringFormatByName(std::string &outValue, const char *name, ...) = 0;
91 
92     virtual RState GetStringArrayById(uint32_t id, std::vector<std::string> &outValue) = 0;
93 
94     virtual RState GetStringArrayByName(const char *name, std::vector<std::string> &outValue) = 0;
95 
96     virtual RState GetPatternById(uint32_t id, std::map<std::string, std::string> &outValue) = 0;
97 
98     virtual RState GetPatternByName(const char *name, std::map<std::string, std::string> &outValue) = 0;
99 
100     virtual RState GetPluralStringById(uint32_t id, int quantity, std::string &outValue) = 0;
101 
102     virtual RState GetPluralStringByName(const char *name, int quantity, std::string &outValue) = 0;
103 
104     virtual RState GetPluralStringByIdFormat(std::string &outValue, uint32_t id, int quantity, ...) = 0;
105 
106     virtual RState GetPluralStringByNameFormat(std::string &outValue, const char *name, int quantity, ...) = 0;
107 
108     virtual RState GetThemeById(uint32_t id, std::map<std::string, std::string> &outValue) = 0;
109 
110     virtual RState GetThemeByName(const char *name, std::map<std::string, std::string> &outValue) = 0;
111 
112     virtual RState GetBooleanById(uint32_t id, bool &outValue) = 0;
113 
114     virtual RState GetBooleanByName(const char *name, bool &outValue) = 0;
115 
116     virtual RState GetIntegerById(uint32_t id, int &outValue) = 0;
117 
118     virtual RState GetIntegerByName(const char *name, int &outValue) = 0;
119 
120     virtual RState GetFloatById(uint32_t id, float &outValue) = 0;
121 
122     virtual RState GetFloatById(uint32_t id, float &outValue, std::string &unit) = 0;
123 
124     virtual RState GetFloatByName(const char *name, float &outValue) = 0;
125 
126     virtual RState GetFloatByName(const char *name, float &outValue, std::string &unit) = 0;
127 
128     virtual RState GetIntArrayById(uint32_t id, std::vector<int> &outValue) = 0;
129 
130     virtual RState GetIntArrayByName(const char *name, std::vector<int> &outValue) = 0;
131 
132     virtual RState GetColorById(uint32_t id, uint32_t &outValue) = 0;
133 
134     virtual RState GetColorByName(const char *name, uint32_t &outValue) = 0;
135 
136     virtual RState GetProfileById(uint32_t id, std::string &outValue) = 0;
137 
138     virtual RState GetProfileByName(const char *name, std::string &outValue) = 0;
139 
140     virtual RState GetMediaById(uint32_t id, std::string &outValue, uint32_t density = 0) = 0;
141 
142     virtual RState GetMediaByName(const char *name, std::string &outValue, uint32_t density = 0) = 0;
143 
144     virtual RState GetRawFilePathByName(const std::string &name, std::string &outValue) = 0;
145 
146     virtual RState GetRawFileDescriptor(const std::string &name, RawFileDescriptor &descriptor) = 0;
147 
148     virtual RState CloseRawFileDescriptor(const std::string &name) = 0;
149 
150     virtual RState GetMediaDataById(uint32_t id, size_t& len, std::unique_ptr<uint8_t[]> &outValue,
151         uint32_t density = 0) = 0;
152 
153     virtual RState GetMediaDataByName(const char *name, size_t& len, std::unique_ptr<uint8_t[]> &outValue,
154         uint32_t density = 0) = 0;
155 
156     virtual RState GetMediaBase64DataById(uint32_t id,  std::string &outValue, uint32_t density = 0) = 0;
157 
158     virtual RState GetMediaBase64DataByName(const char *name,  std::string &outValue, uint32_t density = 0) = 0;
159 
160     virtual RState GetProfileDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue) = 0;
161 
162     virtual RState GetProfileDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue) = 0;
163 
164     virtual RState GetRawFileFromHap(const std::string &rawFileName, size_t &len,
165         std::unique_ptr<uint8_t[]> &outValue) = 0;
166 
167     virtual RState GetRawFileDescriptorFromHap(const std::string &rawFileName, RawFileDescriptor &descriptor) = 0;
168 
169     virtual RState IsLoadHap(std::string &hapPath) = 0;
170 
171     virtual RState GetRawFileList(const std::string &rawDirPath, std::vector<std::string> &rawfileList) = 0;
172 
173     virtual RState GetDrawableInfoById(uint32_t id, std::string &type, size_t &len,
174         std::unique_ptr<uint8_t[]> &outValue, uint32_t density = 0) = 0;
175 
176     virtual RState GetDrawableInfoByName(const char *name, std::string &type, size_t &len,
177         std::unique_ptr<uint8_t[]> &outValue, uint32_t density = 0) = 0;
178 
179     virtual bool AddResource(const std::string &path, const std::vector<std::string> &overlayPaths) = 0;
180 
181     virtual bool RemoveResource(const std::string &path, const std::vector<std::string> &overlayPaths) = 0;
182 
183     virtual RState GetStringFormatById(uint32_t id, std::string &outValue,
184         std::vector<std::tuple<NapiValueType, std::string>> &jsParams) = 0;
185 
186     virtual RState GetStringFormatByName(const char *name, std::string &outValue,
187         std::vector<std::tuple<NapiValueType, std::string>> &jsParams) = 0;
188 
189     virtual uint32_t GetResourceLimitKeys() = 0;
190 
191     virtual bool AddAppOverlay(const std::string &path) = 0;
192 
193     virtual bool RemoveAppOverlay(const std::string &path) = 0;
194 
195     virtual RState GetRawFdNdkFromHap(const std::string &rawFileName, RawFileDescriptor &descriptor) = 0;
196 
197     virtual RState GetResId(const std::string &resTypeName, uint32_t &resId) = 0;
198 
199     virtual void GetLocales(std::vector<std::string> &outValue, bool includeSystem = false) = 0;
200 
201     virtual RState GetDrawableInfoById(uint32_t id,
202         std::tuple<std::string, size_t, std::string> &drawableInfo,
203         std::unique_ptr<uint8_t[]> &outValue, uint32_t iconType, uint32_t density = 0) = 0;
204 
205     virtual RState GetDrawableInfoByName(const char *name,
206         std::tuple<std::string, size_t, std::string> &drawableInfo,
207         std::unique_ptr<uint8_t[]> &outValue, uint32_t iconType, uint32_t density = 0) = 0;
208 
209     virtual RState GetSymbolById(uint32_t id, uint32_t &outValue) = 0;
210 
211     virtual RState GetSymbolByName(const char *name, uint32_t &outValue) = 0;
212 
213     virtual RState GetThemeIcons(uint32_t resId, std::pair<std::unique_ptr<uint8_t[]>, size_t> &foregroundInfo,
214         std::pair<std::unique_ptr<uint8_t[]>, size_t> &backgroundInfo, uint32_t density = 0,
215         const std::string &abilityName = "") = 0;
216 
217     virtual std::string GetThemeMask() = 0;
218 
219     virtual bool HasIconInTheme(const std::string &bundleName) = 0;
220 
221     virtual RState GetOtherIconsInfo(const std::string &iconName,
222         std::unique_ptr<uint8_t[]> &outValue, size_t &len, bool isGlobalMask) = 0;
223 
224     virtual RState IsRawDirFromHap(const std::string &pathName, bool &outValue) = 0;
225 
226     virtual std::shared_ptr<ResourceManager> GetOverrideResourceManager(
227         std::shared_ptr<ResConfig> overrideResConfig) = 0;
228 
229     virtual RState UpdateOverrideResConfig(ResConfig &resConfig) = 0;
230 
231     virtual void GetOverrideResConfig(ResConfig &resConfig) = 0;
232 
233     virtual RState GetDynamicIcon(const std::string &resName, std::pair<std::unique_ptr<uint8_t[]>, size_t> &iconInfo,
234         uint32_t density = 0) = 0;
235 
236     virtual RState GetStringFormatById(std::string &outValue, uint32_t id, va_list args) = 0;
237 
238     virtual RState GetStringFormatByName(std::string &outValue, const char *name, va_list args) = 0;
239 
240     virtual RState GetFormatPluralStringById(std::string &outValue, uint32_t id, int quantity,
241         std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams) = 0;
242 
243     virtual RState GetFormatPluralStringByName(std::string &outValue, const char *name, int quantity,
244         std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams) = 0;
245 };
246 
247 EXPORT_FUNC ResourceManager *CreateResourceManager();
248 
249 /**
250  * Get system resource manager, the added system resource is sandbox path. This method should call
251  * after the sandbox mount.
252  *
253  * @return pointer of system resource manager
254  */
255 EXPORT_FUNC ResourceManager *GetSystemResourceManager();
256 
257 /**
258  * Get system resource manager, the added system resource is no sandbox path. This method should call
259  * before the sandbox mount, for example appspawn.
260  *
261  * @return pointer of system resource manager
262  */
263 EXPORT_FUNC ResourceManager *GetSystemResourceManagerNoSandBox();
264 
265 /**
266  * Create app resource manager.
267  *
268  * @param bundleName the hap bundleName
269  * @param moduleName the hap moduleName
270  * @param hapPath the hap resource path
271  * @param overlayPath the hap overlay resource path
272  * @param resConfig the device resConfig
273  * @param appType the app type
274  * @return pointer of app resource manager
275  */
276 EXPORT_FUNC std::shared_ptr<ResourceManager> CreateResourceManager(const std::string &bundleName,
277     const std::string &moduleName, const std::string &hapPath, const std::vector<std::string> &overlayPath,
278     ResConfig &resConfig, int32_t appType = 0, int32_t userId = 100);
279 } // namespace Resource
280 } // namespace Global
281 } // namespace OHOS
282 #endif