• 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 RESOURCE_MANAGER_HAPRESOURCE_H
16 #define RESOURCE_MANAGER_HAPRESOURCE_H
17 
18 #include <map>
19 #include <string>
20 #include <time.h>
21 #include <unordered_map>
22 #include <set>
23 #include "res_desc.h"
24 #include "res_config_impl.h"
25 
26 namespace OHOS {
27 namespace Global {
28 namespace Resource {
29 /**
30  * HapResource describe a resource of hap zip file.
31  *
32  */
33 class HapResource {
34 public:
35     /**
36      * Creates an HapResource.
37      *
38      * @param path resources.index file path
39      * @param defaultConfig  match defaultConfig to keys of index file, only parse the matched keys.
40      *                       'null' means parse all keys.
41      * @param isSystem If `isSystem` is true, the package is marked as a system package and allows some functions to
42      *     filter out this package when computing what configurations/resources are available.
43      * @param isOverlay If 'isOverlay' is true, the package is marked as an overlay package and overlay resource can
44      *     replace non-overlay resource.
45      * @return pResource if create pResource success, else nullptr
46      */
47     static const std::shared_ptr<HapResource> LoadFromIndex(
48         const char *path, std::shared_ptr<ResConfigImpl> &defaultConfig,
49         bool isSystem = false, bool isOverlay = false, const uint32_t &selectedTypes = SELECT_ALL);
50 
51     /**
52      * Creates an HapResource.
53      *
54      * @param path hap file path
55      * @param defaultConfig  match defaultConfig to keys of index file, only parse the matched keys.
56      *     'null' means parse all keys.
57      * @param isSystem If `isSystem` is true, the package is marked as a system package and allows some functions to
58      *     filter out this package when computing what configurations/resources are available.
59      * @param isOverlay If 'isOverlay' is true, the package is marked as an overlay package and overlay resource can
60      *     replace non-overlay resource.
61      * @return pResource if create pResource success, else nullptr
62      */
63     static const std::shared_ptr<HapResource> LoadFromHap(
64         const char *path, std::shared_ptr<ResConfigImpl> &defaultConfig,
65         bool isSystem = false, bool isOverlay = false, const uint32_t &selectedTypes = SELECT_ALL);
66 
67     /**
68      * Creates an HapResource.
69      *
70      * @param path hap file path
71      * @param defaultConfig  match defaultConfig to keys of index file, only parse the matched keys.
72      *     'null' means parse all keys.
73      * @param isSystem If `isSystem` is true, the package is marked as a system package and allows some functions to
74      *     filter out this package when computing what configurations/resources are available.
75      * @param isOverlay If 'isOverlay' is true, the package is marked as an overlay package and overlay resource can
76      *     replace non-overlay resource.
77      * @return pResource if create pResource success, else nullptr
78      */
79     static const std::shared_ptr<HapResource> Load(
80         const char* path, std::shared_ptr<ResConfigImpl> &defaultConfig,
81         bool isSystem = false, bool isOverlay = false, const uint32_t &selectedTypes = SELECT_ALL);
82 
83     /**
84      * Load overlay resources
85      * @param path the resources.index file path
86      * @param overlayPath  the resource overlay path
87      * @param defaultConfig the resource config
88      * @param isSystem judge the overlay is system or not
89      * @return the map of overlay resource path and resource info if success, else null
90      */
91     static const std::unordered_map<std::string, std::shared_ptr<HapResource>> LoadOverlays(
92         const std::string &path, const std::vector<std::string> &overlayPath,
93         std::shared_ptr<ResConfigImpl> &defaultConfig, bool isSystem = false);
94 
95     /**
96      * The destructor of HapResource
97      */
98     ~HapResource();
99 
100     /**
101      * Get the resource.index file path
102      */
GetIndexPath()103     inline const std::string GetIndexPath() const
104     {
105         return indexPath_;
106     }
107 
108     /**
109      * Get the resource path
110      */
GetResourcePath()111     inline const std::string GetResourcePath() const
112     {
113         return resourcePath_;
114     }
115 
116     /**
117      * Get the system flag of HapResource.
118      *
119      * @return true if isSystem_ is true, false otherwise
120      */
IsSystemResource()121     inline bool IsSystemResource() const
122     {
123         return isSystem_;
124     }
125 
126     /**
127      * Get the overlay flag of HapResource.
128      *
129      * @return true if isOverlay_ is true, false otherwise
130      */
IsOverlayResource()131     inline bool IsOverlayResource() const
132     {
133         return isOverlay_;
134     }
135 
136     /**
137      * Get the resource information
138      */
139     const std::vector<std::string> GetQualifiers() const;
140 
141     /**
142      * Describe limitpath and value under the path
143      */
144     class ValueUnderQualifierDir {
145     public:
GetIdItem()146         inline const std::shared_ptr<IdItem> GetIdItem() const
147         {
148             return idItem_;
149         }
150 
GetResConfig()151         inline const std::shared_ptr<ResConfigImpl> GetResConfig() const
152         {
153             return resConfig_;
154         }
155 
IsOverlay()156         inline bool IsOverlay() const
157         {
158             return isOverlay_;
159         }
160 
IsSystemResource()161         inline bool IsSystemResource() const
162         {
163             return isSystemResource_;
164         }
165 
GetIndexPath()166         inline const std::string GetIndexPath() const
167         {
168             return indexPath_;
169         }
170 
GetResourcePath()171         inline const std::string GetResourcePath() const
172         {
173             return resourcePath_;
174         }
175 
176         /**
177          * The constructor of ValueUnderQualifierDir.
178          *
179          * @param resKey resKey, indicate every limit path item.
180          * @param idItem idItem value, include type and value of id.
181          * @param hapResource hapResource.
182          * @param isOverlay the overlay flag, default value is false.
183          * @param isSystemResource the system flag, default value is false.
184          */
185         ValueUnderQualifierDir(const std::shared_ptr<ResKey> &resKey, const std::shared_ptr<IdItem> &idItem,
186             const std::pair<std::string, std::string> &resPath, bool isOverlay = false, bool isSystemResource = false);
187 
188         ~ValueUnderQualifierDir();
189 
190     private:
191         std::vector<std::shared_ptr<KeyParam>> keyParams_;
192 
193         /**
194          * resConfig_ is to describe Qualifiers Sub-directory
195          * it point to the ResKey resConfig_ and resConfig_ will be unified free in ResKey destruct.
196          */
197         std::shared_ptr<ResConfigImpl> resConfig_;
198 
199         // the value
200         std::shared_ptr<IdItem> idItem_;
201 
202         friend class HapResource;
203 
204         std::string indexPath_;
205 
206         std::string resourcePath_;
207 
208         bool isOverlay_;
209 
210         bool isSystemResource_;
211     };
212 
213     /**
214      * describe value under different Qualifiers Sub-directories
215      */
216     class IdValues {
217     public:
AddLimitPath(std::shared_ptr<ValueUnderQualifierDir> vuqd)218         inline void AddLimitPath(std::shared_ptr<ValueUnderQualifierDir> vuqd)
219         {
220             limitPaths_.push_back(vuqd);
221         }
222 
GetLimitPathsConst()223         inline const std::vector<std::shared_ptr<ValueUnderQualifierDir>> &GetLimitPathsConst() const
224         {
225             return limitPaths_;
226         }
227 
228         ~IdValues();
229 
230     private:
231         // the folder desc
232         std::vector<std::shared_ptr<ValueUnderQualifierDir>> limitPaths_;
233     };
234 
235     /**
236      * Get the resource value by resource id
237      * @param id the resource id
238      * @return the resource value related to id
239      */
240     const std::shared_ptr<IdValues> GetIdValues(const uint32_t id) const;
241 
242     /**
243      * Get the resource value by resource name
244      * @param name the resource name
245      * @param resType the resource type
246      * @return the resource value related to resource name
247      */
248     const std::shared_ptr<IdValues> GetIdValuesByName(const std::string name, const ResType resType) const;
249 
250     /**
251      * Get the resource id by resource name
252      * @param name the resource name
253      * @param resType the resource type
254      * @return the resource id related to resource name
255      */
256     int GetIdByName(const char *name, const ResType resType) const;
257 
IdSize()258     size_t IdSize() const
259     {
260         return idValuesMap_.size();
261     }
262 
263     /**
264      * Get the resource limit keys value which every binary bit corresponds to existing limit key {@link KeyType}
265      *
266      * @return the resource limit keys
267      */
268     uint32_t GetResourceLimitKeys() const;
269 
270     std::unordered_map<std::string, std::unordered_map<ResType, uint32_t>> BuildNameTypeIdMapping() const;
271 
272     /**
273      * Get locale list
274      *
275      * @param outValue the locales write to, the locale string is divided into three parts: language,
276      *     script (optional), and region (optional), concatenated by the connector (-).
277      * @param includeSystem the parameter controls whether to include system resources,
278      *     it has no effect when only system resources query the locales list.
279      */
280     void GetLocales(std::set<std::string> &outValue, bool includeSystem);
281 
282     HapResource(const std::string path, time_t lastModTime, std::shared_ptr<ResDesc> resDes,
283         bool isSystem = false, bool isOverlay = false);
284 
285     bool IsThemeSystemResEnable() const;
286 private:
287 
288     void UpdateOverlayInfo(std::unordered_map<std::string, std::unordered_map<ResType, uint32_t>> &nameTypeId);
289 
290     uint32_t GetLimitPathsKeys(const std::vector<std::shared_ptr<ValueUnderQualifierDir>> &limitPaths,
291         std::vector<bool> &keyTypes) const;
292 
293     void GetKeyParamsLocales(const std::vector<std::shared_ptr<KeyParam>> keyParams, std::set<std::string> &outValue);
294 
295     // must call Init() after constructor
296     bool Init(std::shared_ptr<ResConfigImpl> &defaultConfig);
297 
298     bool InitMap(const std::shared_ptr<ResKey> &resKey, const std::pair<std::string, std::string> &resPath,
299         std::shared_ptr<ResConfigImpl> &defaultConfig);
300 
301     // step of Init(), called in Init()
302     bool InitIdList(std::shared_ptr<ResConfigImpl> &defaultConfig);
303 
304     void IsAppDarkRes(const std::shared_ptr<HapResource::ValueUnderQualifierDir> &limitPath,
305         std::shared_ptr<ResConfigImpl> &defaultConfig);
306 
307     // resources.index file path
308     const std::string indexPath_;
309 
310     // resource path , calculated from indexPath_
311     std::string resourcePath_;
312 
313     // last mod time of hap file
314     time_t lastModTime_;
315 
316     // resource information stored in resDesc_
317     std::shared_ptr<ResDesc> resDesc_;
318 
319     std::map<uint32_t, std::shared_ptr<IdValues>> idValuesMap_;
320 
321     // the key is name, each restype holds one map
322     // name may conflict in same restype !
323     std::vector<std::shared_ptr<std::map<std::string, std::shared_ptr<IdValues>>>> idValuesNameMap_;
324 
325     // judge the hap resource is system or not.
326     bool isSystem_;
327 
328     // judge the hap resource is overlay or not.
329     bool isOverlay_;
330 
331     //judge the theme SystemRes is enabled or not.
332     bool isThemeSystemResEnable_;
333 };
334 } // namespace Resource
335 } // namespace Global
336 } // namespace OHOS
337 #endif
338