• 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_HAPRESOURCE_H
16 #define OHOS_RESOURCE_MANAGER_HAPRESOURCE_H
17 
18 #include <map>
19 #include <set>
20 #include <shared_mutex>
21 #include <string>
22 #include <time.h>
23 #include <unordered_map>
24 #include "res_config_impl.h"
25 #include "res_desc.h"
26 
27 namespace OHOS {
28 namespace Global {
29 namespace Resource {
30 /**
31  * Describe limitpath and value under the path
32  */
33 class ValueUnderQualifierDir {
34 public:
35     ValueUnderQualifierDir(const std::pair<std::string, std::string> &resPath,
36         std::shared_ptr<ResConfigImpl> resConfig, bool isOverlay = false, bool isSystemResource = false);
37 
38     ValueUnderQualifierDir(const std::pair<std::string, std::string> &resPath);
39 
40     virtual ~ValueUnderQualifierDir();
41 
42     virtual std::shared_ptr<IdItem> GetIdItem() const;
43 
GetIndexPath()44     inline const std::string GetIndexPath() const
45     {
46         return indexPath_;
47     }
48 
GetResourcePath()49     inline const std::string GetResourcePath() const
50     {
51         return resourcePath_;
52     }
53 
GetResConfig()54     inline std::shared_ptr<ResConfigImpl> GetResConfig() const
55     {
56         return resConfig_;
57     }
58 
IsOverlayResource()59     inline bool IsOverlayResource() const
60     {
61         return isOverlay_;
62     }
63 
IsSystemResource()64     inline bool IsSystemResource() const
65     {
66         return isSystemResource_;
67     }
68 protected:
69     friend class HapResource;
70 
71     std::string indexPath_;
72 
73     std::string resourcePath_;
74 
75     std::shared_ptr<ResConfigImpl> resConfig_;
76 
77     bool isOverlay_;
78 
79     bool isSystemResource_;
80 };
81 
82 /**
83  * describe value under different Qualifiers Sub-directories
84  */
85 class IdValues {
86 public:
87     virtual ~IdValues();
88     virtual const std::vector<std::shared_ptr<ValueUnderQualifierDir>> &GetLimitPathsConst() const = 0;
89 };
90 
91 class HapResource {
92 public:
93     HapResource(const std::string path, time_t lastModTime);
94 
95     virtual ~HapResource();
96 
97     /**
98      * Get the system flag of HapResource.
99      *
100      * @return true if isSystem_ is true, false otherwise
101      */
102     virtual bool IsSystemResource() const;
103 
104     /**
105      * Get the overlay flag of HapResource.
106      *
107      * @return true if isOverlay_ is true, false otherwise
108      */
109     virtual bool IsOverlayResource() const;
110 
111     /**
112      * Get the resource value by resource id
113      * @param id the resource id
114      * @return the resource value related to id
115      */
116     virtual const std::shared_ptr<IdValues> GetIdValues(const uint32_t id);
117 
118     /**
119      * Get the resource value by resource name
120      * @param name the resource name
121      * @param resType the resource type
122      * @return the resource value related to resource name
123      */
124     virtual const std::shared_ptr<IdValues> GetIdValuesByName(const std::string name, const ResType resType);
125 
126     virtual RState Update(std::shared_ptr<ResConfigImpl> &defaultConfig);
127 
128     /**
129      * Get the resource limit keys value which every binary bit corresponds to existing limit key {@link KeyType}
130      *
131      * @return the resource limit keys
132      */
133     virtual uint32_t GetLimitKeysValue();
134 
135     virtual std::unordered_map<std::string, std::unordered_map<ResType, uint32_t>> BuildNameTypeIdMapping();
136 
137     /**
138      * Get locale list
139      *
140      * @param outValue the locales write to, the locale string is divided into three parts: language,
141      *     script (optional), and region (optional), concatenated by the connector (-).
142      * @param includeSystem the parameter controls whether to include system resources,
143      *     it has no effect when only system resources query the locales list.
144      */
GetLocales(std::set<std::string> & outValue,bool includeSystem)145     virtual void GetLocales(std::set<std::string> &outValue, bool includeSystem) {};
146 
UpdateOverlayInfo(std::unordered_map<std::string,std::unordered_map<ResType,uint32_t>> & nameTypeId)147     virtual void UpdateOverlayInfo(std::unordered_map<std::string,
148         std::unordered_map<ResType, uint32_t>> &nameTypeId) {};
149 
SetLimitKeysValue(uint32_t limitKeyValue)150     inline void SetLimitKeysValue(uint32_t limitKeyValue)
151     {
152         limitKeyValue_ = limitKeyValue;
153     }
154 
155     /**
156      * Get the resource.index file path
157      */
GetIndexPath()158     inline std::string GetIndexPath() const
159     {
160         return indexPath_;
161     }
162 
163     /**
164      * Get the resource path
165      */
GetResourcePath()166     inline std::string GetResourcePath() const
167     {
168         return resourcePath_;
169     }
170 
171     /**
172      * Get resource last mod time.
173      *
174      * @return the resource last mod time.
175      */
GetLastModTime()176     inline time_t GetLastModTime()
177     {
178         return lastModTime_;
179     }
180 
181     /**
182      * Set resource last mod time.
183      *
184      * @param lastModTime resource last mod time
185      */
SetLastModTime(time_t lastModTime)186     inline void SetLastModTime(time_t lastModTime)
187     {
188         lastModTime_ = lastModTime;
189     }
190 
IsThemeSystemResEnable()191     inline bool IsThemeSystemResEnable() const
192     {
193         return isThemeSystemResEnable_;
194     }
195 
196     /**
197      * Determine whether there are dark resource
198      *
199      * @return true if has dark resource, else false
200      */
HasDarkRes()201     inline bool HasDarkRes() const
202     {
203         return hasDarkRes_;
204     }
205 
206     /**
207      * Set the hqf flag of HapResource.
208      */
SetHasPatch(bool hasPatch)209     inline void SetHasPatch(bool hasPatch)
210     {
211         hasPatch_ = hasPatch;
212     }
213 
214     /**
215      * Get the hqf flag of HapResource.
216      *
217      * @return true if hasPatch_ is true, false otherwise
218      */
HasPatch()219     inline bool HasPatch() const
220     {
221         return hasPatch_;
222     }
223 
224     /**
225      * Set the hqf resource path.
226      */
SetPatchPath(const std::string & patchPath)227     inline void SetPatchPath(const std::string& patchPath)
228     {
229         patchPath_ = patchPath;
230     }
231 
232     /**
233      * Get the hqf resource path.
234      */
GetPatchPath()235     inline std::string GetPatchPath() const
236     {
237         return patchPath_;
238     }
239 
SetLocales(const std::set<std::string> & locales)240     inline void SetLocales(const std::set<std::string> &locales)
241     {
242         locales_ = locales;
243     }
244 protected:
245     friend class HapResourceManager;
246     friend class HapParser;
247 
248     std::shared_mutex mutex_;
249 
250     // resources.index file path
251     const std::string indexPath_;
252 
253     // resource path , calculated from indexPath_
254     std::string resourcePath_;
255 
256     // last mod time of hap file
257     time_t lastModTime_;
258 
259     uint32_t limitKeyValue_{0};
260 
261     // hqf resource path.
262     std::string patchPath_;
263 
264     std::set<std::string> locales_;
265 
266     // judge the theme SystemRes is enabled or not.
267     bool isThemeSystemResEnable_{false};
268 
269     // judge the resource is adapt dark mode or not.
270     bool hasDarkRes_{false};
271 
272     // judge the hqf is enabled or not.
273     bool hasPatch_{false};
274 };
275 } // namespace Resource
276 } // namespace Global
277 } // namespace OHOS
278 #endif
279