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