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: GetKeyParams()146 inline const std::vector<std::shared_ptr<KeyParam>> GetKeyParams() const 147 { 148 return keyParams_; 149 } 150 GetFolder()151 inline const std::string GetFolder() const 152 { 153 return folder_; 154 } 155 GetIdItem()156 inline const std::shared_ptr<IdItem> GetIdItem() const 157 { 158 return idItem_; 159 } 160 GetResConfig()161 inline const std::shared_ptr<ResConfigImpl> GetResConfig() const 162 { 163 return resConfig_; 164 } 165 IsOverlay()166 inline bool IsOverlay() const 167 { 168 return isOverlay_; 169 } 170 IsSystemResource()171 inline bool IsSystemResource() const 172 { 173 return isSystemResource_; 174 } 175 GetIndexPath()176 inline const std::string GetIndexPath() const 177 { 178 return indexPath_; 179 } 180 GetResourcePath()181 inline const std::string GetResourcePath() const 182 { 183 return resourcePath_; 184 } 185 186 /** 187 * The constructor of ValueUnderQualifierDir. 188 * 189 * @param resKey resKey, indicate every limit path item. 190 * @param idItem idItem value, include type and value of id. 191 * @param hapResource hapResource. 192 * @param isOverlay the overlay flag, default value is false. 193 * @param isSystemResource the system flag, default value is false. 194 */ 195 ValueUnderQualifierDir(const std::shared_ptr<ResKey> &resKey, const std::shared_ptr<IdItem> &idItem, 196 const std::pair<std::string, std::string> &resPath, bool isOverlay = false, bool isSystemResource = false); 197 198 ~ValueUnderQualifierDir(); 199 200 private: 201 202 /* 203 * keyParams_, folder_, resConfig_ are 3 different ways to describe Qualifiers Sub-directory 204 */ 205 std::vector<std::shared_ptr<KeyParam>> keyParams_; 206 // the qualifier path name 207 std::string folder_; 208 // this resConfig_ point to the ResKey resConfig_ and resConfig_ will be unified free in ResKey destruct. 209 std::shared_ptr<ResConfigImpl> resConfig_; 210 211 // the value 212 std::shared_ptr<IdItem> idItem_; 213 214 friend class HapResource; 215 216 bool isOverlay_; 217 218 bool isSystemResource_; 219 220 std::string indexPath_; 221 222 std::string resourcePath_; 223 }; 224 225 /** 226 * describe value under different Qualifiers Sub-directories 227 */ 228 class IdValues { 229 public: AddLimitPath(std::shared_ptr<ValueUnderQualifierDir> vuqd)230 inline void AddLimitPath(std::shared_ptr<ValueUnderQualifierDir> vuqd) 231 { 232 limitPaths_.push_back(vuqd); 233 } 234 GetLimitPathsConst()235 inline const std::vector<std::shared_ptr<ValueUnderQualifierDir>> &GetLimitPathsConst() const 236 { 237 return limitPaths_; 238 } 239 240 ~IdValues(); 241 242 private: 243 // the folder desc 244 std::vector<std::shared_ptr<ValueUnderQualifierDir>> limitPaths_; 245 }; 246 247 /** 248 * Get the resource value by resource id 249 * @param id the resource id 250 * @return the resource value related to id 251 */ 252 const std::shared_ptr<IdValues> GetIdValues(const uint32_t id) const; 253 254 /** 255 * Get the resource value by resource name 256 * @param name the resource name 257 * @param resType the resource type 258 * @return the resource value related to resource name 259 */ 260 const std::shared_ptr<IdValues> GetIdValuesByName(const std::string name, const ResType resType) const; 261 262 /** 263 * Get the resource id by resource name 264 * @param name the resource name 265 * @param resType the resource type 266 * @return the resource id related to resource name 267 */ 268 int GetIdByName(const char *name, const ResType resType) const; 269 IdSize()270 size_t IdSize() const 271 { 272 return idValuesMap_.size(); 273 } 274 275 /** 276 * Get the resource limit keys value which every binary bit corresponds to existing limit key {@link KeyType} 277 * 278 * @return the resource limit keys 279 */ 280 uint32_t GetResourceLimitKeys() const; 281 282 std::unordered_map<std::string, std::unordered_map<ResType, uint32_t>> BuildNameTypeIdMapping() const; 283 284 /** 285 * Get locale list 286 * 287 * @param outValue the locales write to, the locale string is divided into three parts: language, 288 * script (optional), and region (optional), concatenated by the connector (-). 289 * @param includeSystem the parameter controls whether to include system resources, 290 * it has no effect when only system resources query the locales list. 291 */ 292 void GetLocales(std::set<std::string> &outValue, bool includeSystem); 293 294 HapResource(const std::string path, time_t lastModTime, std::shared_ptr<ResDesc> resDes, 295 bool isSystem = false, bool isOverlay = false); 296 297 bool IsThemeSystemResEnable() const; 298 private: 299 300 void UpdateOverlayInfo(std::unordered_map<std::string, std::unordered_map<ResType, uint32_t>> &nameTypeId); 301 302 uint32_t GetLimitPathsKeys(const std::vector<std::shared_ptr<ValueUnderQualifierDir>> &limitPaths, 303 std::vector<bool> &keyTypes) const; 304 305 void GetKeyParamsLocales(const std::vector<std::shared_ptr<KeyParam>> keyParams, std::set<std::string> &outValue); 306 307 // must call Init() after constructor 308 bool Init(std::shared_ptr<ResConfigImpl> &defaultConfig); 309 310 bool InitMap(const std::shared_ptr<ResKey> &resKey, const std::pair<std::string, std::string> &resPath, 311 std::shared_ptr<ResConfigImpl> &defaultConfig); 312 313 // step of Init(), called in Init() 314 bool InitIdList(std::shared_ptr<ResConfigImpl> &defaultConfig); 315 316 void IsAppDarkRes(const std::shared_ptr<HapResource::ValueUnderQualifierDir> &limitPath, 317 std::shared_ptr<ResConfigImpl> &defaultConfig); 318 319 // resources.index file path 320 const std::string indexPath_; 321 322 // resource path , calculated from indexPath_ 323 std::string resourcePath_; 324 325 // last mod time of hap file 326 time_t lastModTime_; 327 328 // resource information stored in resDesc_ 329 std::shared_ptr<ResDesc> resDesc_; 330 331 std::map<uint32_t, std::shared_ptr<IdValues>> idValuesMap_; 332 333 // the key is name, each restype holds one map 334 // name may conflict in same restype ! 335 std::vector<std::shared_ptr<std::map<std::string, std::shared_ptr<IdValues>>>> idValuesNameMap_; 336 337 // judge the hap resource is system or not. 338 bool isSystem_; 339 340 // judge the hap resource is overlay or not. 341 bool isOverlay_; 342 343 //judge the theme SystemRes is enabled or not. 344 bool isThemeSystemResEnable_; 345 }; 346 } // namespace Resource 347 } // namespace Global 348 } // namespace OHOS 349 #endif 350