1 /* 2 * Copyright (c) 2025 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 16 #ifndef OHOS_RESOURCE_MANAGER_HAPRESOURCEV2_H 17 #define OHOS_RESOURCE_MANAGER_HAPRESOURCEV2_H 18 19 #include <atomic> 20 21 #include "hap_resource.h" 22 #include "mmap_file.h" 23 24 namespace OHOS { 25 namespace Global { 26 namespace Resource { 27 class ValueUnderQualifierDirV2 : public ValueUnderQualifierDir { 28 public: 29 static const uint32_t DATA_HEAD_LEN = 2; 30 ValueUnderQualifierDirV2(const std::pair<std::string, std::string> &resPath, uint32_t offset, 31 std::shared_ptr<ResConfigImpl> resConfig, bool isOverlay = false, bool isSystemResource = false); 32 33 virtual ~ValueUnderQualifierDirV2(); 34 35 virtual std::shared_ptr<IdItem> GetIdItem() const; 36 Init(std::shared_ptr<MmapFile> mMapFile,ResType resType,uint32_t id,std::string name)37 inline void Init(std::shared_ptr<MmapFile> mMapFile, ResType resType, uint32_t id, std::string name) 38 { 39 mMapFile_ = mMapFile; 40 resType_ = resType; 41 id_ = id; 42 name_ = name; 43 } 44 private: 45 friend class HapResourceV2; 46 47 uint32_t offset_; 48 std::shared_ptr<MmapFile> mMapFile_; 49 ResType resType_{VALUES}; 50 uint32_t id_{0}; 51 std::string name_; 52 }; 53 54 class IdValuesV2 : public IdValues { 55 public: 56 IdValuesV2(ResType resType, uint32_t id, uint32_t offset, const std::string &name); 57 58 virtual ~IdValuesV2(); 59 60 virtual const std::vector<std::shared_ptr<ValueUnderQualifierDir>> &GetLimitPathsConst() const; 61 SetMMap(std::shared_ptr<MmapFile> mMapFile)62 inline void SetMMap(std::shared_ptr<MmapFile> mMapFile) 63 { 64 mMapFile_ = mMapFile; 65 } 66 GetMMap()67 inline std::shared_ptr<MmapFile> GetMMap() 68 { 69 return mMapFile_; 70 } 71 GetOffset()72 inline uint32_t GetOffset() const 73 { 74 return offset_; 75 } 76 GetResType()77 inline ResType GetResType() const 78 { 79 return resType_; 80 } 81 GetName()82 inline std::string GetName() const 83 { 84 return name_; 85 } 86 GetId()87 inline uint32_t GetId() const 88 { 89 return id_; 90 } 91 SetId(uint32_t id)92 inline void SetId(uint32_t id) 93 { 94 id_ = id; 95 } 96 ReserveLimitPaths(uint32_t size)97 inline void ReserveLimitPaths(uint32_t size) 98 { 99 limitPaths_.reserve(size); 100 } 101 AddLimitPath(std::shared_ptr<ValueUnderQualifierDirV2> vuqd)102 inline void AddLimitPath(std::shared_ptr<ValueUnderQualifierDirV2> vuqd) 103 { 104 limitPaths_.push_back(vuqd); 105 } 106 IsParsed()107 inline bool IsParsed() 108 { 109 return isParsed_.load(); 110 } 111 Parse()112 inline void Parse() 113 { 114 isParsed_.store(true); 115 } 116 private: 117 // resource type 118 ResType resType_; 119 120 // resource id 121 uint32_t id_; 122 123 // offset from the beginning of the index file, pointing to the resource data 124 uint32_t offset_; 125 126 // resource name 127 std::string name_; 128 129 // resource.index file inform 130 std::shared_ptr<MmapFile> mMapFile_; 131 132 // if the IdValuesV2 has been parsed flag 133 std::atomic<bool> isParsed_{false}; 134 135 // the folder desc 136 std::vector<std::shared_ptr<ValueUnderQualifierDir>> limitPaths_; 137 }; 138 139 class HapResourceV2 : public HapResource { 140 public: 141 HapResourceV2(const std::string path, time_t lastModTime, bool hasDarkRes = false); 142 143 virtual ~HapResourceV2(); 144 145 virtual bool IsSystemResource() const; 146 147 virtual bool IsOverlayResource() const; 148 149 virtual const std::shared_ptr<IdValues> GetIdValues(const uint32_t id); 150 151 virtual const std::shared_ptr<IdValues> GetIdValuesByName(const std::string name, const ResType resType); 152 153 virtual std::unordered_map<std::string, std::unordered_map<ResType, uint32_t>> BuildNameTypeIdMapping(); 154 155 virtual void GetLocales(std::set<std::string> &outValue, bool includeSystem); 156 157 bool Init(std::unordered_map<uint32_t, std::shared_ptr<ResConfigImpl>> &keys, 158 std::unordered_map<uint32_t, std::shared_ptr<IdValuesV2>> &idMap, 159 std::unordered_map<uint32_t, std::unordered_map<std::string, std::shared_ptr<IdValuesV2>>> &typeNameMap, 160 std::shared_ptr<MmapFile> mMapFile); 161 162 void InitThemeSystemRes(); 163 protected: 164 friend class HapResourceManager; 165 friend class HapParser; 166 167 virtual int32_t ParseLimitPaths(std::shared_ptr<IdValuesV2> idValue); 168 169 std::mutex idValuesMutex_; 170 171 std::shared_ptr<MmapFile> mMapFile_; 172 173 // <resconfig id, resconfig> 174 std::unordered_map<uint32_t, std::shared_ptr<ResConfigImpl>> keys_; 175 176 // <resource id, resource> 177 std::unordered_map<uint32_t, std::shared_ptr<IdValuesV2>> idMap_; 178 179 // <type, <resource name, resource>> 180 std::unordered_map<uint32_t, std::unordered_map<std::string, std::shared_ptr<IdValuesV2>>> typeNameMap_; 181 }; 182 183 class SystemResource : virtual public HapResourceV2 { 184 public: 185 SystemResource(); 186 SystemResource(const std::string path, time_t lastModTime, bool hasDarkRes = false); 187 virtual ~SystemResource(); 188 virtual bool IsSystemResource() const; 189 virtual void GetLocales(std::set<std::string> &outValue, bool includeSystem); 190 protected: 191 virtual int32_t ParseLimitPaths(std::shared_ptr<IdValuesV2> idValue); 192 }; 193 194 class OverlayResource : virtual public HapResourceV2 { 195 public: 196 OverlayResource(); 197 OverlayResource(const std::string path, time_t lastModTime, bool hasDarkRes = false); 198 virtual ~OverlayResource(); 199 virtual bool IsOverlayResource() const; 200 virtual void GetLocales(std::set<std::string> &outValue, bool includeSystem); 201 virtual void UpdateOverlayInfo(std::unordered_map<std::string, std::unordered_map<ResType, uint32_t>> &nameTypeId); 202 protected: 203 virtual int32_t ParseLimitPaths(std::shared_ptr<IdValuesV2> idValue); 204 }; 205 206 class SystemOverlayResource : public SystemResource, public OverlayResource { 207 public: 208 SystemOverlayResource(const std::string path, time_t lastModTime, bool hasDarkRes = false); 209 virtual ~SystemOverlayResource(); 210 virtual void GetLocales(std::set<std::string> &outValue, bool includeSystem); 211 protected: 212 virtual int32_t ParseLimitPaths(std::shared_ptr<IdValuesV2> idValue); 213 }; 214 } 215 } 216 } 217 #endif 218