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 "res_desc.h" 23 #include "res_config_impl.h" 24 25 namespace OHOS { 26 namespace Global { 27 namespace Resource { 28 /** 29 * HapResource describe a resource of hap zip file. 30 * 31 */ 32 class HapResource { 33 public: 34 /** 35 * Creates an HapResource. 36 * 37 * @param path resources.index file path 38 * @param defaultConfig match defaultConfig to keys of index file, only parse the matched keys. 39 * 'null' means parse all keys. 40 * @param system If `system` is true, the package is marked as a system package, and allows some functions to 41 * filter out this package when computing what configurations/resources are available. 42 * @return pResource if create pResource success, else nullptr 43 */ 44 static const HapResource *LoadFromIndex(const char *path, const ResConfigImpl *defaultConfig, bool system = false); 45 46 /** 47 * Creates an HapResource. 48 * 49 * @param path hap file path 50 * @param defaultConfig match defaultConfig to keys of index file, only parse the matched keys. 51 * 'null' means parse all keys. 52 * @param system If `system` is true, the package is marked as a system package, and allows some functions to 53 * filter out this package when computing what configurations/resources are available. 54 * @return pResource if create pResource success, else nullptr 55 */ 56 static const HapResource *LoadFromHap(const char *path, const ResConfigImpl *defaultConfig, bool system = false); 57 58 /** 59 * Creates an HapResource. 60 * 61 * @param path hap file path 62 * @param defaultConfig match defaultConfig to keys of index file, only parse the matched keys. 63 * 'null' means parse all keys. 64 * @param system If `system` is true, the package is marked as a system package, and allows some functions to 65 * filter out this package when computing what configurations/resources are available. 66 * @return pResource if create pResource success, else nullptr 67 */ 68 static const HapResource *Load(const char* path, const ResConfigImpl* defaultConfig, bool system = false); 69 70 /** 71 * Load overlay resources 72 * @param path the resources.index file path 73 * @param overlayPath the resource overlay path 74 * @param defaultConfig the resource config 75 * @return the map of overlay resource path and resource info if success, else null 76 */ 77 static const std::unordered_map<std::string, HapResource *> LoadOverlays(const std::string &path, 78 const std::vector<std::string> &overlayPath, const ResConfigImpl *defaultConfig); 79 80 /** 81 * The destructor of HapResource 82 */ 83 ~HapResource(); 84 85 /** 86 * Get the resource.index file path 87 */ GetIndexPath()88 inline const std::string GetIndexPath() const 89 { 90 return indexPath_; 91 } 92 93 /** 94 * Get the resource path 95 */ GetResourcePath()96 inline const std::string GetResourcePath() const 97 { 98 return resourcePath_; 99 } 100 101 /** 102 * Get the resource information 103 */ 104 const std::vector<std::string> GetQualifiers() const; 105 106 /** 107 * Describe limitpath and value under the path 108 */ 109 class ValueUnderQualifierDir { 110 public: GetKeyParams()111 inline const std::vector<KeyParam *> GetKeyParams() const 112 { 113 return keyParams_; 114 } 115 GetFolder()116 inline const std::string GetFolder() const 117 { 118 return folder_; 119 } 120 GetIdItem()121 inline const IdItem *GetIdItem() const 122 { 123 return idItem_; 124 } 125 GetResConfig()126 inline const ResConfigImpl *GetResConfig() const 127 { 128 return resConfig_; 129 } 130 GetHapResource()131 inline const HapResource *GetHapResource() const 132 { 133 return hapResource_; 134 } 135 IsOverlay()136 inline bool IsOverlay() const 137 { 138 return isOverlay_; 139 } 140 IsSystemResource()141 inline bool IsSystemResource() const 142 { 143 return isSystemResource_; 144 } 145 146 ValueUnderQualifierDir(const std::vector<KeyParam *> &keyParams, IdItem *idItem, 147 HapResource *hapResource, bool isOverlay = false, bool isSystemResource = false); 148 149 ~ValueUnderQualifierDir(); 150 151 private: 152 153 // using keyParams_ to init resconfig_ 154 void InitResConfig(); 155 156 /* 157 * keyParams_, folder_, resConfig_ are 3 different ways to describe Qualifiers Sub-directory 158 */ 159 std::vector<KeyParam *> keyParams_; 160 // the qualifier path name 161 std::string folder_; 162 // ResConfig 163 ResConfigImpl *resConfig_; 164 165 // the value 166 IdItem *idItem_; 167 168 // indicate belong to which hapresource 169 const HapResource *hapResource_; 170 171 friend class HapResource; 172 173 bool isOverlay_; 174 175 bool isSystemResource_; 176 }; 177 178 /** 179 * describe value under different Qualifiers Sub-directories 180 */ 181 class IdValues { 182 public: AddLimitPath(ValueUnderQualifierDir * vuqd)183 inline void AddLimitPath(ValueUnderQualifierDir *vuqd) 184 { 185 limitPaths_.push_back(vuqd); 186 } 187 GetLimitPathsConst()188 inline const std::vector<ValueUnderQualifierDir *> &GetLimitPathsConst() const 189 { 190 return limitPaths_; 191 } 192 193 ~IdValues(); 194 195 private: 196 // the folder desc 197 std::vector<ValueUnderQualifierDir *> limitPaths_; 198 }; 199 200 /** 201 * Get the resource value by resource id 202 * @param id the resource id 203 * @return the resource value related to id 204 */ 205 const IdValues *GetIdValues(const uint32_t id) const; 206 207 /** 208 * Get the resource value by resource name 209 * @param name the resource name 210 * @param resType the resource type 211 * @return the resource value related to resource name 212 */ 213 const IdValues *GetIdValuesByName(const std::string name, const ResType resType) const; 214 215 /** 216 * Get the resource id by resource name 217 * @param name the resource name 218 * @param resType the resource type 219 * @return the resource id related to resource name 220 */ 221 int GetIdByName(const char *name, const ResType resType) const; 222 IdSize()223 size_t IdSize() const 224 { 225 return idValuesMap_.size(); 226 } 227 228 private: 229 HapResource(const std::string path, time_t lastModTime, const ResConfig *defaultConfig, ResDesc *resDes); 230 231 std::unordered_map<std::string, std::unordered_map<ResType, uint32_t>> BuildNameTypeIdMapping() const; 232 233 void UpdateOverlayInfo(std::unordered_map<std::string, std::unordered_map<ResType, uint32_t>> &nameTypeId); 234 235 // must call Init() after constructor 236 bool Init(bool system = false); 237 238 // step of Init(), called in Init() 239 bool InitIdList(bool system = false); 240 241 // resources.index file path 242 const std::string indexPath_; 243 244 // resource path , calculated from indexPath_ 245 std::string resourcePath_; 246 247 // last mod time of hap file 248 time_t lastModTime_; 249 250 // resource information stored in resDesc_ 251 ResDesc *resDesc_; 252 253 std::map<uint32_t, IdValues *> idValuesMap_; 254 255 // the key is name, each restype holds one map 256 // name may conflict in same restype ! 257 std::vector<std::map<std::string, IdValues *> *> idValuesNameMap_; 258 259 // default resconfig 260 const ResConfig *defaultConfig_; 261 }; 262 } // namespace Resource 263 } // namespace Global 264 } // namespace OHOS 265 #endif 266