1 /* 2 * Copyright (c) 2021 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 <vector> 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 43 */ 44 static const HapResource *LoadFromIndex(const char *path, const ResConfigImpl *defaultConfig, bool system = false); 45 46 /** 47 * The destructor of HapResource 48 */ 49 ~HapResource(); 50 51 /** 52 * Get the resource.index file path 53 */ GetIndexPath()54 inline const std::string GetIndexPath() const 55 { 56 return indexPath_; 57 } 58 59 /** 60 * Get the resource path 61 */ GetResourcePath()62 inline const std::string GetResourcePath() const 63 { 64 return resourcePath_; 65 } 66 67 /** 68 * Describe limitpath and value under the path 69 */ 70 class ValueUnderQualifierDir { 71 public: GetKeyParams()72 inline const std::vector<KeyParam *> GetKeyParams() const 73 { 74 return keyParams_; 75 } 76 GetFolder()77 inline const std::string GetFolder() const 78 { 79 return folder_; 80 } 81 GetIdItem()82 inline const IdItem *GetIdItem() const 83 { 84 return idItem_; 85 } 86 GetResConfig()87 inline const ResConfigImpl *GetResConfig() const 88 { 89 return resConfig_; 90 } 91 GetHapResource()92 inline const HapResource *GetHapResource() const 93 { 94 return hapResource_; 95 } 96 97 ValueUnderQualifierDir(const std::vector<KeyParam *> &keyParams, IdItem *idItem, 98 HapResource *hapResource); 99 100 ~ValueUnderQualifierDir(); 101 102 private: 103 104 // using keyParams_ to init resconfig_ 105 void InitResConfig(); 106 107 /* 108 * keyParams_, folder_, resConfig_ are 3 different ways to describe Qualifiers Sub-directory 109 */ 110 std::vector<KeyParam *> keyParams_; 111 // the qualifier path name 112 std::string folder_; 113 // ResConfig 114 ResConfigImpl *resConfig_; 115 116 // the value 117 IdItem *idItem_; 118 119 // indicate belong to which hapresource 120 const HapResource *hapResource_; 121 }; 122 123 /** 124 * describe value under different Qualifiers Sub-directories 125 */ 126 class IdValues { 127 public: AddLimitPath(ValueUnderQualifierDir * vuqd)128 inline void AddLimitPath(ValueUnderQualifierDir *vuqd) 129 { 130 limitPaths_.push_back(vuqd); 131 } 132 GetLimitPathsConst()133 inline const std::vector<ValueUnderQualifierDir *> &GetLimitPathsConst() const 134 { 135 return limitPaths_; 136 } 137 138 ~IdValues(); 139 140 private: 141 // the folder desc 142 std::vector<ValueUnderQualifierDir *> limitPaths_; 143 }; 144 145 /** 146 * Get the resource value by resource id 147 * @param id the resource id 148 * @return the resource value related to id 149 */ 150 const IdValues *GetIdValues(const uint32_t id) const; 151 152 /** 153 * Get the resource value by resource name 154 * @param name the resource name 155 * @param resType the resource type 156 * @return the resource value related to resource name 157 */ 158 const IdValues *GetIdValuesByName(const std::string name, const ResType resType) const; 159 160 /** 161 * Get the resource id by resource name 162 * @param name the resource name 163 * @param resType the resource type 164 * @return the resource id related to resource name 165 */ 166 int GetIdByName(const char *name, const ResType resType) const; 167 IdSize()168 size_t IdSize() const 169 { 170 return idValuesMap_.size(); 171 } 172 173 private: 174 HapResource(const std::string path, time_t lastModTime, const ResConfig *defaultConfig, ResDesc *resDes); 175 176 // must call Init() after constructor 177 bool Init(); 178 179 // step of Init(), called in Init() 180 bool InitIdList(); 181 182 // resources.index file path 183 const std::string indexPath_; 184 185 // resource path , calculated from indexPath_ 186 std::string resourcePath_; 187 188 // last mod time of hap file 189 time_t lastModTime_; 190 191 // resource information stored in resDesc_ 192 ResDesc *resDesc_; 193 194 std::map<uint32_t, IdValues *> idValuesMap_; 195 196 // the key is name, each restype holds one map 197 // name may conflict in same restype ! 198 std::vector<std::map<std::string, IdValues *> *> idValuesNameMap_; 199 200 // default resconfig 201 const ResConfig *defaultConfig_; 202 }; 203 } // namespace Resource 204 } // namespace Global 205 } // namespace OHOS 206 #endif 207