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 #include <shared_mutex> 26 27 namespace OHOS { 28 namespace Global { 29 namespace Resource { 30 /** 31 * HapResource describe a resource of hap zip file. 32 * 33 */ 34 class HapResource { 35 public: 36 /** 37 * Creates an HapResource. 38 * 39 * @param path resources.index file path 40 * @param defaultConfig match defaultConfig to keys of index file, only parse the matched keys. 41 * 'null' means parse all keys. 42 * @param isSystem If `isSystem` is true, the package is marked as a system package and allows some functions to 43 * filter out this package when computing what configurations/resources are available. 44 * @param isOverlay If 'isOverlay' is true, the package is marked as an overlay package and overlay resource can 45 * replace non-overlay resource. 46 * @return pResource if create pResource success, else nullptr 47 */ 48 static const std::shared_ptr<HapResource> LoadFromIndex( 49 const char *path, std::shared_ptr<ResConfigImpl> &defaultConfig, 50 bool isSystem = false, bool isOverlay = false, const uint32_t &selectedTypes = SELECT_ALL); 51 52 /** 53 * Creates an HapResource. 54 * 55 * @param path hap file path 56 * @param defaultConfig match defaultConfig to keys of index file, only parse the matched keys. 57 * 'null' means parse all keys. 58 * @param isSystem If `isSystem` is true, the package is marked as a system package and allows some functions to 59 * filter out this package when computing what configurations/resources are available. 60 * @param isOverlay If 'isOverlay' is true, the package is marked as an overlay package and overlay resource can 61 * replace non-overlay resource. 62 * @return pResource if create pResource success, else nullptr 63 */ 64 static const std::shared_ptr<HapResource> LoadFromHap( 65 const char *path, std::shared_ptr<ResConfigImpl> &defaultConfig, 66 bool isSystem = false, bool isOverlay = false, const uint32_t &selectedTypes = SELECT_ALL); 67 68 /** 69 * Creates an HapResource. 70 * 71 * @param path hap file path 72 * @param defaultConfig match defaultConfig to keys of index file, only parse the matched keys. 73 * 'null' means parse all keys. 74 * @param isSystem If `isSystem` is true, the package is marked as a system package and allows some functions to 75 * filter out this package when computing what configurations/resources are available. 76 * @param isOverlay If 'isOverlay' is true, the package is marked as an overlay package and overlay resource can 77 * replace non-overlay resource. 78 * @return pResource if create pResource success, else nullptr 79 */ 80 static const std::shared_ptr<HapResource> Load( 81 const char* path, std::shared_ptr<ResConfigImpl> &defaultConfig, 82 bool isSystem = false, bool isOverlay = false, const uint32_t &selectedTypes = SELECT_ALL); 83 84 /** 85 * Load overlay resources 86 * @param path the resources.index file path 87 * @param overlayPath the resource overlay path 88 * @param defaultConfig the resource config 89 * @param isSystem judge the overlay is system or not 90 * @return the map of overlay resource path and resource info if success, else null 91 */ 92 static const std::unordered_map<std::string, std::shared_ptr<HapResource>> LoadOverlays( 93 const std::string &path, const std::vector<std::string> &overlayPath, 94 std::shared_ptr<ResConfigImpl> &defaultConfig, bool isSystem = false); 95 96 /** 97 * The destructor of HapResource 98 */ 99 ~HapResource(); 100 101 /** 102 * Get the resource.index file path 103 */ GetIndexPath()104 inline const std::string GetIndexPath() const 105 { 106 return indexPath_; 107 } 108 109 /** 110 * Get the resource path 111 */ GetResourcePath()112 inline const std::string GetResourcePath() const 113 { 114 return resourcePath_; 115 } 116 117 /** 118 * Get the system flag of HapResource. 119 * 120 * @return true if isSystem_ is true, false otherwise 121 */ IsSystemResource()122 inline bool IsSystemResource() const 123 { 124 return isSystem_; 125 } 126 127 /** 128 * Get the overlay flag of HapResource. 129 * 130 * @return true if isOverlay_ is true, false otherwise 131 */ IsOverlayResource()132 inline bool IsOverlayResource() const 133 { 134 return isOverlay_; 135 } 136 137 /** 138 * Get the hqf resource path. 139 */ GetPatchPath()140 inline std::string GetPatchPath() const 141 { 142 return patchPath_; 143 } 144 145 /** 146 * Get the hqf flag of HapResource. 147 * 148 * @return true if isPatch_ is true, false otherwise 149 */ IsPatch()150 inline bool IsPatch() const 151 { 152 return isPatch_; 153 } 154 155 /** 156 * Set the hqf resource path. 157 */ SetPatchPath(const std::string & patchPath)158 inline void SetPatchPath(const std::string& patchPath) 159 { 160 patchPath_ = patchPath; 161 } 162 163 /** 164 * Set the hqf flag of HapResource. 165 */ SetIsPatch(bool isPatch)166 inline void SetIsPatch(bool isPatch) 167 { 168 isPatch_ = isPatch; 169 } 170 171 /** 172 * Get the resource information 173 */ 174 const std::vector<std::string> GetQualifiers(); 175 176 /** 177 * Get resource last mod time. 178 * 179 * @return the resource last mod time. 180 */ GetLastModTime()181 inline time_t GetLastModTime() 182 { 183 return lastModTime_; 184 } 185 186 /** 187 * Set resource last mod time. 188 * 189 * @param lastModTime resource last mod time 190 */ SetLastModTime(time_t lastModTime)191 inline void SetLastModTime(time_t lastModTime) 192 { 193 lastModTime_ = lastModTime; 194 } 195 196 /** 197 * Describe limitpath and value under the path 198 */ 199 class ValueUnderQualifierDir { 200 public: GetIdItem()201 inline const std::shared_ptr<IdItem> GetIdItem() const 202 { 203 return idItem_; 204 } 205 GetResConfig()206 inline const std::shared_ptr<ResConfigImpl> GetResConfig() const 207 { 208 return resConfig_; 209 } 210 IsOverlay()211 inline bool IsOverlay() const 212 { 213 return isOverlay_; 214 } 215 IsSystemResource()216 inline bool IsSystemResource() const 217 { 218 return isSystemResource_; 219 } 220 GetIndexPath()221 inline const std::string GetIndexPath() const 222 { 223 return indexPath_; 224 } 225 GetResourcePath()226 inline const std::string GetResourcePath() const 227 { 228 return resourcePath_; 229 } 230 231 /** 232 * The constructor of ValueUnderQualifierDir. 233 * 234 * @param resKey resKey, indicate every limit path item. 235 * @param idItem idItem value, include type and value of id. 236 * @param hapResource hapResource. 237 * @param isOverlay the overlay flag, default value is false. 238 * @param isSystemResource the system flag, default value is false. 239 */ 240 ValueUnderQualifierDir(const std::shared_ptr<ResKey> &resKey, const std::shared_ptr<IdItem> &idItem, 241 const std::pair<std::string, std::string> &resPath, bool isOverlay = false, bool isSystemResource = false); 242 243 ~ValueUnderQualifierDir(); 244 245 private: 246 247 /** 248 * resConfig_ is to describe Qualifiers Sub-directory 249 * it point to the ResKey resConfig_ and resConfig_ will be unified free in ResKey destruct. 250 */ 251 std::shared_ptr<ResConfigImpl> resConfig_; 252 253 // the value 254 std::shared_ptr<IdItem> idItem_; 255 256 friend class HapResource; 257 258 std::string indexPath_; 259 260 std::string resourcePath_; 261 262 bool isOverlay_; 263 264 bool isSystemResource_; 265 }; 266 267 /** 268 * describe value under different Qualifiers Sub-directories 269 */ 270 class IdValues { 271 public: AddLimitPath(std::shared_ptr<ValueUnderQualifierDir> vuqd)272 inline void AddLimitPath(std::shared_ptr<ValueUnderQualifierDir> vuqd) 273 { 274 limitPaths_.push_back(vuqd); 275 } 276 GetLimitPathsConst()277 inline const std::vector<std::shared_ptr<ValueUnderQualifierDir>> &GetLimitPathsConst() const 278 { 279 return limitPaths_; 280 } 281 282 ~IdValues(); 283 284 private: 285 // the folder desc 286 std::vector<std::shared_ptr<ValueUnderQualifierDir>> limitPaths_; 287 }; 288 289 /** 290 * Get the resource value by resource id 291 * @param id the resource id 292 * @return the resource value related to id 293 */ 294 const std::shared_ptr<IdValues> GetIdValues(const uint32_t id); 295 296 /** 297 * Get the resource value by resource name 298 * @param name the resource name 299 * @param resType the resource type 300 * @return the resource value related to resource name 301 */ 302 const std::shared_ptr<IdValues> GetIdValuesByName(const std::string name, const ResType resType); 303 304 /** 305 * Get the resource id by resource name 306 * @param name the resource name 307 * @param resType the resource type 308 * @return the resource id related to resource name 309 */ 310 int GetIdByName(const char *name, const ResType resType); 311 312 RState UpdateResConfig(std::shared_ptr<ResConfigImpl> &defaultConfig); 313 IdSize()314 size_t IdSize() const 315 { 316 return idValuesMap_.size(); 317 } 318 319 /** 320 * Get the resource limit keys value which every binary bit corresponds to existing limit key {@link KeyType} 321 * 322 * @return the resource limit keys 323 */ 324 uint32_t GetResourceLimitKeys(); 325 326 std::unordered_map<std::string, std::unordered_map<ResType, uint32_t>> BuildNameTypeIdMapping(); 327 328 /** 329 * Get locale list 330 * 331 * @param outValue the locales write to, the locale string is divided into three parts: language, 332 * script (optional), and region (optional), concatenated by the connector (-). 333 * @param includeSystem the parameter controls whether to include system resources, 334 * it has no effect when only system resources query the locales list. 335 */ 336 void GetLocales(std::set<std::string> &outValue, bool includeSystem); 337 338 HapResource(const std::string path, time_t lastModTime, std::shared_ptr<ResDesc> resDes, 339 bool isSystem = false, bool isOverlay = false); 340 341 bool IsThemeSystemResEnable() const; 342 343 /** 344 * Determine whether there are dark resource 345 * 346 * @return true if has dark resource, else false 347 */ 348 bool HasDarkRes(); 349 private: 350 351 void UpdateOverlayInfo(std::unordered_map<std::string, std::unordered_map<ResType, uint32_t>> &nameTypeId); 352 353 // must call Init() after constructor 354 bool Init(std::shared_ptr<ResConfigImpl> &defaultConfig); 355 356 bool InitMap(const std::shared_ptr<ResKey> &resKey, const std::pair<std::string, std::string> &resPath, 357 std::shared_ptr<ResConfigImpl> &defaultConfig); 358 359 // step of Init(), called in Init() 360 bool InitIdList(std::shared_ptr<ResConfigImpl> &defaultConfig); 361 362 void IsAppDarkRes(const std::shared_ptr<HapResource::ValueUnderQualifierDir> &limitPath, 363 std::shared_ptr<ResConfigImpl> &defaultConfig); 364 SetLimitKeysValue(uint32_t limitKeyValue)365 inline void SetLimitKeysValue(uint32_t limitKeyValue) 366 { 367 limitKeyValue_ = limitKeyValue; 368 } 369 SetLocales(const std::set<std::string> & locales)370 inline void SetLocales(const std::set<std::string> &locales) 371 { 372 locales_ = locales; 373 } 374 SetSelectedType(uint32_t type)375 inline void SetSelectedType(uint32_t type) 376 { 377 selectedTypes_ = type; 378 } 379 380 // resources.index file path 381 const std::string indexPath_; 382 383 // resource path , calculated from indexPath_ 384 std::string resourcePath_; 385 386 // last mod time of hap file 387 time_t lastModTime_; 388 389 uint32_t selectedTypes_{SELECT_ALL}; 390 391 // resource information stored in resDesc_ 392 std::shared_ptr<ResDesc> resDesc_; 393 394 std::shared_mutex mutex_; 395 396 std::set<std::shared_ptr<ResConfigImpl>> loadedConfig_; 397 398 std::map<uint32_t, std::shared_ptr<IdValues>> idValuesMap_; 399 400 // the key is name, each restype holds one map 401 // name may conflict in same restype ! 402 std::vector<std::shared_ptr<std::map<std::string, std::shared_ptr<IdValues>>>> idValuesNameMap_; 403 404 // judge the hap resource is system or not. 405 bool isSystem_; 406 407 // judge the hap resource is overlay or not. 408 bool isOverlay_; 409 410 // judge the theme SystemRes is enabled or not. 411 bool isThemeSystemResEnable_; 412 413 // hqf resource path. 414 std::string patchPath_; 415 416 // judge the hqf is enabled or not. 417 bool isPatch_ = false; 418 419 // judge the resource is adapt dark mode or not. 420 bool hasDarkRes_ = false; 421 422 uint32_t limitKeyValue_ = 0; 423 424 std::set<std::string> locales_; 425 }; 426 } // namespace Resource 427 } // namespace Global 428 } // namespace OHOS 429 #endif 430