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