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 OHOS_RESOURCE_MANAGER_HAPMANAGER_H 16 #define OHOS_RESOURCE_MANAGER_HAPMANAGER_H 17 18 #include "res_config_impl.h" 19 #include "hap_resource.h" 20 #include "res_desc.h" 21 #include "resource_manager.h" 22 #include "lock.h" 23 #include <set> 24 25 #ifdef SUPPORT_GRAPHICS 26 #include <unicode/plurrule.h> 27 #endif 28 29 namespace OHOS { 30 namespace Global { 31 namespace Resource { 32 class HapManager { 33 public: 34 /** 35 * The constructor of HapManager 36 * 37 * @param resConfig resource config 38 * @param isSystem system flag, default value is false 39 */ 40 HapManager(ResConfigImpl *resConfig, bool isSystem = false); 41 42 /** 43 * The destructor of HapManager 44 */ 45 ~HapManager(); 46 47 /** 48 * Update the resConfig 49 * @param resConfig the resource config 50 * @return SUCCESS if the resConfig updated success, else HAP_INIT_FAILED 51 */ 52 RState UpdateResConfig(ResConfig &resConfig); 53 54 /** 55 * Get the resConfig 56 * @param resConfig the resource config 57 */ 58 void GetResConfig(ResConfig &resConfig); 59 60 /** 61 * Add resource path to hap paths 62 * @param path the resource path 63 * @return true if add resource path success, else false 64 */ 65 bool AddResource(const char *path); 66 67 /** 68 * Add resource path to overlay paths 69 * @param path the resource path 70 * @param overlayPaths the exist overlay resource path 71 * @return true if add resource path success, else false 72 */ 73 bool AddResource(const std::string &path, const std::vector<std::string> &overlayPaths); 74 75 /** 76 * Remove resource path to overlay paths 77 * @param path the resource path 78 * @param overlayPaths the exist overlay resource path 79 * @return true if add resource path success, else false 80 */ 81 bool RemoveResource(const std::string &path, const std::vector<std::string> &overlayPaths); 82 83 /** 84 * Add the overlay resource for current application 85 * 86 * @param path the overlay resource path 87 * @return true if add resource path success, else false 88 */ 89 bool AddAppOverlay(const std::string &path); 90 91 /** 92 * Remove the overlay resource for current application 93 * 94 * @param path the overlay resource path 95 * @return true if add resource path success, else false 96 */ 97 bool RemoveAppOverlay(const std::string &path); 98 99 /** 100 * Obtain the hap path of the current application 101 * 102 * @return the current application hap path 103 */ 104 std::string GetValidAppPath(); 105 106 /** 107 * Find resource by resource id 108 * @param id the resource id 109 * @return the resources related to resource id 110 */ 111 const IdItem *FindResourceById(uint32_t id); 112 113 /** 114 * Find resource by resource name 115 * @param name the resource name 116 * @param resType the resource type 117 * @return the resources related to resource name 118 */ 119 const IdItem *FindResourceByName(const char *name, const ResType resType); 120 121 /** 122 * Find best resource path by resource id 123 * @param id the resource id 124 * @param density the input screen density 125 * @return the best resource path 126 */ 127 const HapResource::ValueUnderQualifierDir *FindQualifierValueById(uint32_t id, uint32_t density = 0); 128 129 /** 130 * Find best resource path by resource name 131 * @param name the resource name 132 * @param resType the resource type 133 * @param density the input screen density 134 * @return the best resource path 135 */ 136 const HapResource::ValueUnderQualifierDir *FindQualifierValueByName(const char *name, const ResType resType, 137 uint32_t density = 0); 138 139 /** 140 * Find the raw file path 141 * @param name the resource name 142 * @param outValue raw file path 143 * @return SUCCESS if find the raw file path success, else NOT_FOUND 144 */ 145 RState FindRawFile(const std::string &name, std::string &outValue); 146 147 /** 148 * Get the language pluralRule related to quantity 149 * @param quantity the language quantity 150 * @return the language pluralRule related to quantity 151 */ 152 std::string GetPluralRulesAndSelect(int quantity); 153 154 /** 155 * Get resource paths vector 156 */ 157 std::vector<std::string> GetResourcePaths(); 158 159 /** 160 * Get the media data 161 * @param qd the QualifierDir 162 * @param len the data len 163 * @param outValue the media data 164 * @return SUCCESS if get the media data success, else NOT_FOUND 165 */ 166 RState GetMediaData(const HapResource::ValueUnderQualifierDir *qd, size_t& len, 167 std::unique_ptr<uint8_t[]> &outValue); 168 169 /** 170 * Get the mediabase64 data 171 * @param qd the QualifierDir 172 * @param outValue the mediabase64 data 173 * @return SUCCESS if get the mediabase64 data success, else NOT_FOUND 174 */ 175 RState GetMediaBase64Data(const HapResource::ValueUnderQualifierDir *qd, std::string &outValue); 176 177 /** 178 * Get the Profile data 179 * @param qd the QualifierDir 180 * @param len the data len write to 181 * @param outValue the profile data 182 * @return SUCCESS if get the profile data success, else NOT_FOUND 183 */ 184 RState GetProfileData(const HapResource::ValueUnderQualifierDir *qd, size_t &len, 185 std::unique_ptr<uint8_t[]> &outValue); 186 187 /** 188 * Find raw file from hap 189 * @param rawFileName the rawFileName 190 * @param len the data len write to 191 * @param outValue the rawfile data 192 * @return SUCCESS if resource exist, else NOT_FOUND 193 */ 194 RState FindRawFileFromHap(const std::string &rawFileName, size_t &len, 195 std::unique_ptr<uint8_t[]> &outValue); 196 197 /** 198 * Find raw file descriptor from hap 199 * @param rawFileName the rawFileName 200 * @param descriptor the rawfile descriptor 201 * @return SUCCESS if resource exist, else NOT_FOUND 202 */ 203 RState FindRawFileDescriptorFromHap(const std::string &rawFileName, 204 ResourceManager::RawFileDescriptor &descriptor); 205 206 /** 207 * Is load hap 208 * @param hapPath the hap path 209 */ 210 bool IsLoadHap(std::string &hapPath); 211 212 /** 213 * Get the valid hapPath 214 * @param hapPath the hap path 215 * @return OK if the hapPath exist, else NOT_FOUND 216 */ 217 int32_t GetValidHapPath(std::string& hapPath); 218 219 /** 220 * Get the raw file list 221 * @param rawDirPath the rawfile directory path 222 * @param rawfileList the rawfile list write to 223 * @return SUCCESS if resource exist, else not found 224 */ 225 RState GetRawFileList(const std::string &rawDirPath, std::vector<std::string>& rawfileList); 226 227 /** 228 * Get the raw file list 229 * @param qd the QualifierDir 230 * @param resType the resource type 231 * @param outValue the resource path write to 232 * @return SUCCESS if resource exist, else not found 233 */ 234 RState GetFilePath(const HapResource::ValueUnderQualifierDir *qd, const ResType resType, 235 std::string &outValue); 236 237 /** 238 * Get the raw file list 239 * @param qd the QualifierDir 240 * @param len the resource type 241 * @param outValue the media data write to 242 * @return SUCCESS if resource exist, else not found 243 */ 244 RState GetMediaDataFromHap(const HapResource::ValueUnderQualifierDir *qd, size_t &len, 245 std::unique_ptr<uint8_t[]> &outValue); 246 247 /** 248 * Get the raw file list 249 * @param qd the QualifierDir 250 * @param len the resource type 251 * @param outValue the media data write to 252 * @return SUCCESS if resource exist, else not found 253 */ 254 RState GetMediaDataFromIndex(const HapResource::ValueUnderQualifierDir *qd, size_t &len, 255 std::unique_ptr<uint8_t[]> &outValue); 256 257 /** 258 * Get the raw file list 259 * @param qd the QualifierDir 260 * @param outValue the mediabase64 data write to 261 * @return SUCCESS if resource exist, else not found 262 */ 263 RState GetMediaBase64DataFromHap(const HapResource::ValueUnderQualifierDir *qd, std::string &outValue); 264 265 /** 266 * Get the raw file list 267 * @param qd the QualifierDir 268 * @param outValue the mediabase64 data write to 269 * @return SUCCESS if resource exist, else not found 270 */ 271 RState GetMediaBase64DataFromIndex(const HapResource::ValueUnderQualifierDir *qd, std::string &outValue); 272 273 /** 274 * Get the raw file list 275 * @param name the rawfile name 276 * @param descriptor the file descriptor write to 277 * @return SUCCESS if resource exist, else not found 278 */ 279 RState FindRawFileDescriptor(const std::string &name, ResourceManager::RawFileDescriptor &descriptor); 280 281 /** 282 * Close rawFile descriptor by resource name 283 * @param name the resource name 284 * @return SUCCESS if close the rawFile descriptor, else ERROR 285 */ 286 RState CloseRawFileDescriptor(const std::string &name); 287 288 /** 289 * Get hap resource of manager 290 * 291 * @return HapResource vector of manager 292 */ 293 std::vector<HapResource *> GetHapResource(); 294 295 /** 296 * Add system resourc to app resource vector 297 * 298 * @param systemHapManager system manager 299 */ 300 void AddSystemResource(const HapManager *systemHapManager); 301 302 /** 303 * Get the resource limit keys value which every binary bit corresponds to existing limit key {@link KeyType} 304 * 305 * @return the resource limit keys 306 */ 307 uint32_t GetResourceLimitKeys(); 308 309 private: 310 void UpdateResConfigImpl(ResConfigImpl &resConfig); 311 312 void GetResConfigImpl(ResConfigImpl &resConfig); 313 314 std::vector<const HapResource::IdValues *> GetResourceList(uint32_t ident) const; 315 316 std::vector<const HapResource::IdValues *> GetResourceListByName(const char *name, const ResType resType) const; 317 318 const HapResource::ValueUnderQualifierDir *GetBestMatchResource(std::vector<const HapResource::IdValues *> 319 candidates, uint32_t density); 320 321 bool AddResourcePath(const char *path); 322 323 // when resConfig_ updated we must call ReloadAll() 324 RState ReloadAll(); 325 326 static bool Init(); 327 328 static bool icuInitialized; 329 330 // app res config 331 ResConfigImpl *resConfig_; 332 333 // set of hap Resources 334 std::vector<HapResource *> hapResources_; 335 336 // set of loaded hap path 337 std::unordered_map<std::string, std::vector<std::string>> loadedHapPaths_; 338 339 std::unordered_map<std::string, ResourceManager::RawFileDescriptor> rawFileDescriptor_; 340 341 #ifdef SUPPORT_GRAPHICS 342 // key is language 343 std::vector<std::pair<std::string, icu::PluralRules *>> plurRulesCache_; 344 #endif 345 346 Lock lock_; 347 348 // indicate manager is system hap manager 349 bool isSystem_; 350 }; 351 } // namespace Resource 352 } // namespace Global 353 } // namespace OHOS 354 #endif