1 /*
2  * Copyright (c) 2021-2022 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_HAPMANAGER_H
16 #define OHOS_RESOURCE_MANAGER_HAPMANAGER_H
17 
18 #include "res_config_impl.h"
19 #include "hap_resource.h"
20 #include "res_desc.h"
21 #include "resource_manager.h"
22 #include <shared_mutex>
23 #include <set>
24 
25 #ifdef SUPPORT_GRAPHICS
26 #include <unicode/plurrule.h>
27 #endif
28 
29 namespace OHOS {
30 namespace Global {
31 namespace Resource {
32 class HapManager {
33 public:
34     /**
35      * The constructor of HapManager
36      *
37      * @param resConfig resource config
38      * @param isSystem system flag, default value is false
39      */
40     HapManager(std::shared_ptr<ResConfigImpl> resConfig, bool isSystem = false);
41 
42     /**
43      * The destructor of HapManager
44      */
45     ~HapManager();
46 
47     /**
48      * Update the resConfig
49      * @param resConfig the resource config
50      * @return SUCCESS if the resConfig updated success, else HAP_INIT_FAILED
51      */
52     RState UpdateResConfig(ResConfig &resConfig);
53 
54     /**
55      * Update the override resConfig
56      * @param resConfig the override resource config
57      * @return SUCCESS if the override resConfig updated success, else ERROR
58      */
59     RState UpdateOverrideResConfig(ResConfig &resConfig);
60 
61     /**
62      * Get the resConfig
63      * @param resConfig the resource config
64      */
65     void GetResConfig(ResConfig &resConfig);
66 
67     /**
68      * Get the override resConfig
69      * @param resConfig the override resource config
70      * @return SUCCESS if the override resConfig updated success, else ERROR
71      */
72     void GetOverrideResConfig(ResConfig &resConfig);
73 
74     /**
75      * Add resource path to hap paths
76      * @param path the resource path
77      * @return true if add resource path success, else false
78      */
79     bool AddResource(const char *path, const uint32_t &selectedTypes);
80 
81     /**
82      * Add resource path to overlay paths
83      * @param path the resource path
84      * @param overlayPaths the exist overlay resource path
85      * @return true if add resource path success, else false
86      */
87     bool AddResource(const std::string &path, const std::vector<std::string> &overlayPaths);
88 
89     /**
90      * Remove resource path to overlay paths
91      * @param path the resource path
92      * @param overlayPaths the exist overlay resource path
93      * @return true if add resource path success, else false
94      */
95     bool RemoveResource(const std::string &path, const std::vector<std::string> &overlayPaths);
96 
97     /**
98      * Add the overlay resource for current application
99      *
100      * @param path the overlay resource path
101      * @return true if add resource path success, else false
102      */
103     bool AddAppOverlay(const std::string &path);
104 
105     /**
106      * Remove the overlay resource for current application
107      *
108      * @param path the overlay resource path
109      * @return true if add resource path success, else false
110      */
111     bool RemoveAppOverlay(const std::string &path);
112 
113     /**
114      * Obtain the hap path of the current application
115      *
116      * @return the current application hap path
117      */
118     std::string GetValidAppPath();
119 
120     /**
121      * Find resource by resource id
122      * @param id the resource id
123      * @return the resources related to resource id
124      */
125     const std::shared_ptr<IdItem> FindResourceById(uint32_t id, bool isGetOverrideResource = false);
126 
127     /**
128      * Find resource by resource name
129      * @param name the resource name
130      * @param resType the resource type
131      * @return the resources related to resource name
132      */
133     const std::shared_ptr<IdItem> FindResourceByName(
134         const char *name, const ResType resType, bool isGetOverrideResource = false);
135 
136     /**
137      * Find best resource path by resource id
138      * @param id the resource id
139      * @param density the input screen density
140      * @return the best resource path
141      */
142     const std::shared_ptr<HapResource::ValueUnderQualifierDir> FindQualifierValueById(uint32_t id,
143         bool isGetOverrideResource = false, uint32_t density = 0);
144 
145     /**
146      * Find best resource path by resource name
147      * @param name the resource name
148      * @param resType the resource type
149      * @param density the input screen density
150      * @return the best resource path
151      */
152     const std::shared_ptr<HapResource::ValueUnderQualifierDir> FindQualifierValueByName(const char *name,
153         const ResType resType, bool isGetOverrideResource = false, uint32_t density = 0);
154 
155     /**
156      * Find the raw file path
157      * @param name the resource name
158      * @param outValue raw file path
159      * @return SUCCESS if find the raw file path success, else NOT_FOUND
160      */
161     RState FindRawFile(const std::string &name, std::string &outValue);
162 
163     /**
164      * Get the language pluralRule related to quantity
165      * @param quantity the language quantity
166      * @return the language pluralRule related to quantity
167      */
168     std::string GetPluralRulesAndSelect(int quantity, bool isGetOverrideResource = false);
169 
170     /**
171      * Get resource paths vector
172      */
173     std::vector<std::string> GetResourcePaths();
174 
175     /**
176      * Get the media data
177      * @param qd the QualifierDir
178      * @param len the data len
179      * @param outValue the media data
180      * @return SUCCESS if get the media data success, else NOT_FOUND
181      */
182     RState GetMediaData(const std::shared_ptr<HapResource::ValueUnderQualifierDir> qd, size_t& len,
183         std::unique_ptr<uint8_t[]> &outValue);
184 
185     /**
186      * Get the mediabase64 data
187      * @param qd the QualifierDir
188      * @param outValue the mediabase64 data
189      * @return SUCCESS if get the mediabase64 data success, else NOT_FOUND
190      */
191     RState GetMediaBase64Data(const std::shared_ptr<HapResource::ValueUnderQualifierDir> qd, std::string &outValue);
192 
193     /**
194      * Get the Profile data
195      * @param qd the QualifierDir
196      * @param len the data len write to
197      * @param outValue the profile data
198      * @return SUCCESS if get the profile data success, else NOT_FOUND
199      */
200     RState GetProfileData(const std::shared_ptr<HapResource::ValueUnderQualifierDir> qd, size_t &len,
201         std::unique_ptr<uint8_t[]> &outValue);
202 
203     /**
204      * Find raw file from hap
205      * @param rawFileName the rawFileName
206      * @param len the data len write to
207      * @param outValue the rawfile data
208      * @return SUCCESS if resource exist, else NOT_FOUND
209      */
210     RState FindRawFileFromHap(const std::string &rawFileName, size_t &len,
211         std::unique_ptr<uint8_t[]> &outValue);
212 
213     /**
214      * Find raw file descriptor from hap
215      * @param rawFileName the rawFileName
216      * @param descriptor the rawfile descriptor
217      * @return SUCCESS if resource exist, else NOT_FOUND
218      */
219     RState FindRawFileDescriptorFromHap(const std::string &rawFileName,
220         ResourceManager::RawFileDescriptor &descriptor);
221 
222     /**
223      * Is load hap
224      * @param hapPath the hap path
225      */
226     bool IsLoadHap(std::string &hapPath);
227 
228     /**
229      * Get the valid hapPath
230      * @param hapPath the hap path
231      * @return OK if the hapPath exist, else NOT_FOUND
232      */
233     int32_t GetValidHapPath(std::string &hapPath);
234 
235     /**
236      * Get the valid resource.index path
237      * @param hapPath the hap path
238      * @return OK if the hapPath exist, else NOT_FOUND
239      */
240     int32_t GetValidIndexPath(std::string &indexPath);
241 
242     /**
243      * Get the raw file list
244      * @param rawDirPath the rawfile directory path
245      * @param rawfileList the rawfile list write to
246      * @return SUCCESS if resource exist, else not found
247      */
248     RState GetRawFileList(const std::string &rawDirPath, std::vector<std::string> &rawfileList);
249 
250     /**
251      * Get the raw file list
252      * @param qd the QualifierDir
253      * @param resType the resource type
254      * @param outValue the resource path write to
255      * @return SUCCESS if resource exist, else not found
256      */
257     RState GetFilePath(const std::shared_ptr<HapResource::ValueUnderQualifierDir> qd, const ResType resType,
258         std::string &outValue);
259 
260     /**
261      * Get the raw file list
262      * @param qd the QualifierDir
263      * @param len the resource type
264      * @param outValue the media data write to
265      * @return SUCCESS if resource exist, else not found
266      */
267     RState GetMediaDataFromHap(const std::shared_ptr<HapResource::ValueUnderQualifierDir> qd, size_t &len,
268         std::unique_ptr<uint8_t[]> &outValue);
269 
270     /**
271      * Get the raw file list
272      * @param qd the QualifierDir
273      * @param len the resource type
274      * @param outValue the media data write to
275      * @return SUCCESS if resource exist, else not found
276      */
277     RState GetMediaDataFromIndex(const std::shared_ptr<HapResource::ValueUnderQualifierDir> qd, size_t &len,
278         std::unique_ptr<uint8_t[]> &outValue);
279 
280     /**
281      * Get the raw file list
282      * @param qd the QualifierDir
283      * @param outValue the mediabase64 data write to
284      * @return SUCCESS if resource exist, else not found
285      */
286     RState GetMediaBase64DataFromHap(const std::shared_ptr<HapResource::ValueUnderQualifierDir> qd,
287         std::string &outValue);
288 
289     /**
290      * Get the raw file list
291      * @param qd the QualifierDir
292      * @param outValue the mediabase64 data write to
293      * @return SUCCESS if resource exist, else not found
294      */
295     RState GetMediaBase64DataFromIndex(const std::shared_ptr<HapResource::ValueUnderQualifierDir> qd,
296         std::string &outValue);
297 
298     /**
299      * Get the raw file list
300      * @param name the rawfile name
301      * @param descriptor the file descriptor write to
302      * @return SUCCESS if resource exist, else not found
303      */
304     RState FindRawFileDescriptor(const std::string &name, ResourceManager::RawFileDescriptor &descriptor);
305 
306     /**
307      * Close rawFile descriptor by resource name
308      * @param name the resource name
309      * @return SUCCESS if close the rawFile descriptor, else ERROR
310      */
311     RState CloseRawFileDescriptor(const std::string &name);
312 
313     /**
314      * Get hap resource of manager
315      *
316      * @return HapResource vector of manager
317      */
318     std::vector<std::shared_ptr<HapResource>> GetHapResource();
319 
320     /**
321      * Add system resourc to app resource vector
322      *
323      * @param systemHapManager system manager
324      */
325     void AddSystemResource(const std::shared_ptr<HapManager> &systemHapManager);
326 
327     /**
328      * Get the resource limit keys value which every binary bit corresponds to existing limit key {@link KeyType}
329      *
330      * @return the resource limit keys
331      */
332     uint32_t GetResourceLimitKeys();
333 
334     /**
335      * Get the rawFile descriptor by resource name
336      *
337      * @param rawFile the rawfile name or relative path
338      * @param descriptor the obtain raw file member fd, length, offet write to
339      * @return SUCCESS if resource exist, else ERROR
340      */
341     RState GetRawFd(const std::string &rawFile, ResourceManager::RawFileDescriptor &descriptor);
342 
343     /**
344      * Get the resource id by resType and resName
345      *
346      * @param resTypeName the resType and resName
347      * @param resId the resId write to
348      * @return SUCCESS if resource exist, else ERROR
349      */
350     RState GetResId(const std::string &resTypeName, uint32_t &resId);
351 
352     /**
353      * Get locale list
354      *
355      * @param outValue the locales write to, the locale string is divided into three parts: language,
356      *     script (optional), and region (optional), concatenated by the connector (-).
357      * @param includeSystem the parameter controls whether to include system resources,
358      *     the default value is false, it has no effect when only system resources query the locales list.
359      */
360     void GetLocales(std::vector<std::string> &outValue, bool includeSystem = false);
361 
362     /**
363      * Whether this raw resource is a directory
364      *
365      * @param path the raw resource path
366      * @param outValue the obtain boolean value write to
367      * @return SUCCESS if raw resource is a directory, else ERROR
368      */
369     RState IsRawDirFromHap(const std::string &pathName, bool &outValue);
370 
371     /**
372      * Whether this theme system resource is enabled
373      *
374      * @return true is enabled, else not.
375      */
376     bool IsThemeSystemResEnableHap();
377 
378 private:
379     void UpdateResConfigImpl(ResConfigImpl &resConfig);
380 
381     void GetResConfigImpl(ResConfigImpl &resConfig);
382 
383     std::vector<std::shared_ptr<HapResource::IdValues>> GetResourceList(uint32_t ident) const;
384 
385     std::vector<std::shared_ptr<HapResource::IdValues>> GetResourceListByName(const char *name,
386         const ResType resType) const;
387 
388     void MatchBestResource(std::shared_ptr<ResConfigImpl> &bestResConfig,
389         std::shared_ptr<HapResource::ValueUnderQualifierDir> &result,
390         const std::vector<std::shared_ptr<HapResource::ValueUnderQualifierDir>> &paths,
391         uint32_t density, std::shared_ptr<ResConfigImpl> currentResConfig);
392 
393     const std::shared_ptr<HapResource::ValueUnderQualifierDir> GetBestMatchResource(
394         const std::vector<std::shared_ptr<HapResource::IdValues>> &candidates,
395         uint32_t density, bool isGetOverrideResource);
396 
397     bool AddResourcePath(const char *path, const uint32_t &selectedTypes = SELECT_ALL);
398 
399     // when resConfig_ updated we must call ReloadAll()
400     RState ReloadAll();
401 
402     static bool Init();
403 
404     std::shared_ptr<ResConfigImpl> getCompleteOverrideConfig(bool isGetOverrideResource);
405 
406     static bool icuInitialized;
407 
408     // app res config
409     std::shared_ptr<ResConfigImpl> resConfig_;
410 
411     // app override res config
412     std::shared_ptr<ResConfigImpl> overrideResConfig_ = std::make_shared<ResConfigImpl>();
413 
414     // set of hap Resources
415     std::vector<std::shared_ptr<HapResource>> hapResources_;
416 
417     // set of loaded hap path
418     std::unordered_map<std::string, std::vector<std::string>> loadedHapPaths_;
419 
420     std::unordered_map<std::string, ResourceManager::RawFileDescriptor> rawFileDescriptor_;
421 
422 #ifdef SUPPORT_GRAPHICS
423     // key is language
424     std::vector<std::pair<std::string, icu::PluralRules *>> plurRulesCache_;
425 #endif
426 
427     std::shared_mutex mutex_;
428 
429     // indicate manager is system hap manager
430     bool isSystem_;
431 };
432 } // namespace Resource
433 } // namespace Global
434 } // namespace OHOS
435 #endif