1 /* 2 * Copyright (c) 2021-2023 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_RESOURCEMANAGER_H 16 #define OHOS_RESOURCE_MANAGER_RESOURCEMANAGER_H 17 18 #include <map> 19 #include <string> 20 #include <vector> 21 #include <memory> 22 #include <tuple> 23 #include "res_config.h" 24 25 namespace OHOS { 26 namespace Global { 27 namespace Resource { 28 enum FunctionType { 29 SYNC = 0, 30 ASYNC = 1 31 }; 32 class ResourceManager { 33 public: 34 std::pair<std::string, std::string> bundleInfo; 35 36 int32_t userId = 0; 37 38 typedef struct { 39 /** the raw file fd */ 40 int fd; 41 42 /** the offset from where the raw file starts in the HAP */ 43 int64_t offset; 44 45 /** the length of the raw file in the HAP. */ 46 int64_t length; 47 } RawFileDescriptor; 48 49 struct Resource { 50 /** the hap bundle name */ 51 std::string bundleName; 52 53 /** the hap module name */ 54 std::string moduleName; 55 56 /** the resource id in hap */ 57 uint32_t id; 58 }; 59 60 struct ResData { 61 /** the resource type */ 62 ResType resType; 63 64 /** the resource value */ 65 std::string value; 66 }; 67 68 struct Quantity { 69 /** the indication of whether quantity is an integer. */ 70 bool isInteger = false; 71 72 /** the integer representation of the quantity value. */ 73 int intValue = 0; 74 75 /** the double representation of the quantity value. */ 76 double doubleValue = 0.0; 77 }; 78 79 enum class NapiValueType { 80 NAPI_NUMBER = 0, 81 NAPI_STRING = 1 82 }; 83 84 virtual ~ResourceManager() = 0; 85 86 /** 87 * Add resource of hap. 88 * 89 * @param path The path of hap. 90 * @param selectedTypes If this param is setted, will only add resource of types specified by this param, 91 * it will be faster than add all resource, you can set this param by combining flags 92 * named SELECT_XXX defined in res_common.h. What's more, if you call UpdateResConfig() 93 * before calling this method, will only add resource that matching the config, for example, 94 * only add resource of current language, which means it will be further faster. 95 * @return true if init success, else false 96 */ 97 virtual bool AddResource(const char *path, const uint32_t &selectedTypes = SELECT_ALL, 98 bool forceReload = false) = 0; 99 100 virtual RState UpdateResConfig(ResConfig &resConfig, bool isUpdateTheme = false) = 0; 101 102 virtual void GetResConfig(ResConfig &resConfig) = 0; 103 104 virtual RState GetResConfigById(uint32_t resId, ResConfig &resConfig, uint32_t density = 0) = 0; 105 106 virtual RState GetResConfigByName(const std::string &name, const ResType type, 107 ResConfig &resConfig, uint32_t density = 0) = 0; 108 109 virtual RState GetStringById(uint32_t id, std::string &outValue) = 0; 110 111 virtual RState GetStringByName(const char *name, std::string &outValue) = 0; 112 113 virtual RState GetStringFormatById(std::string &outValue, uint32_t id, ...) = 0; 114 115 virtual RState GetStringFormatByName(std::string &outValue, const char *name, ...) = 0; 116 117 virtual RState GetStringArrayById(uint32_t id, std::vector<std::string> &outValue) = 0; 118 119 virtual RState GetStringArrayByName(const char *name, std::vector<std::string> &outValue) = 0; 120 121 virtual RState GetPatternById(uint32_t id, std::map<std::string, std::string> &outValue) = 0; 122 123 virtual RState GetPatternByName(const char *name, std::map<std::string, std::string> &outValue) = 0; 124 125 virtual RState GetPluralStringById(uint32_t id, int quantity, std::string &outValue) = 0; 126 127 virtual RState GetPluralStringByName(const char *name, int quantity, std::string &outValue) = 0; 128 129 virtual RState GetPluralStringByIdFormat(std::string &outValue, uint32_t id, int quantity, ...) = 0; 130 131 virtual RState GetPluralStringByNameFormat(std::string &outValue, const char *name, int quantity, ...) = 0; 132 133 virtual RState GetThemeById(uint32_t id, std::map<std::string, std::string> &outValue) = 0; 134 135 virtual RState GetThemeByName(const char *name, std::map<std::string, std::string> &outValue) = 0; 136 137 virtual RState GetBooleanById(uint32_t id, bool &outValue) = 0; 138 139 virtual RState GetBooleanByName(const char *name, bool &outValue) = 0; 140 141 virtual RState GetIntegerById(uint32_t id, int &outValue) = 0; 142 143 virtual RState GetIntegerByName(const char *name, int &outValue) = 0; 144 145 virtual RState GetFloatById(uint32_t id, float &outValue) = 0; 146 147 virtual RState GetFloatById(uint32_t id, float &outValue, std::string &unit) = 0; 148 149 virtual RState GetFloatByName(const char *name, float &outValue) = 0; 150 151 virtual RState GetFloatByName(const char *name, float &outValue, std::string &unit) = 0; 152 153 virtual RState GetIntArrayById(uint32_t id, std::vector<int> &outValue) = 0; 154 155 virtual RState GetIntArrayByName(const char *name, std::vector<int> &outValue) = 0; 156 157 virtual RState GetColorById(uint32_t id, uint32_t &outValue) = 0; 158 159 virtual RState GetColorByName(const char *name, uint32_t &outValue) = 0; 160 161 virtual RState GetProfileById(uint32_t id, std::string &outValue) = 0; 162 163 virtual RState GetProfileByName(const char *name, std::string &outValue) = 0; 164 165 virtual RState GetMediaById(uint32_t id, std::string &outValue, uint32_t density = 0) = 0; 166 167 virtual RState GetMediaByName(const char *name, std::string &outValue, uint32_t density = 0) = 0; 168 169 virtual RState GetRawFilePathByName(const std::string &name, std::string &outValue) = 0; 170 171 virtual RState GetRawFileDescriptor(const std::string &name, RawFileDescriptor &descriptor) = 0; 172 173 virtual RState CloseRawFileDescriptor(const std::string &name) = 0; 174 175 virtual RState GetMediaDataById(uint32_t id, size_t& len, std::unique_ptr<uint8_t[]> &outValue, 176 uint32_t density = 0) = 0; 177 178 virtual RState GetMediaDataByName(const char *name, size_t& len, std::unique_ptr<uint8_t[]> &outValue, 179 uint32_t density = 0) = 0; 180 181 virtual RState GetMediaBase64DataById(uint32_t id, std::string &outValue, uint32_t density = 0) = 0; 182 183 virtual RState GetMediaBase64DataByName(const char *name, std::string &outValue, uint32_t density = 0) = 0; 184 185 virtual RState GetProfileDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue) = 0; 186 187 virtual RState GetProfileDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue) = 0; 188 189 virtual RState GetRawFileFromHap(const std::string &rawFileName, size_t &len, 190 std::unique_ptr<uint8_t[]> &outValue) = 0; 191 192 virtual RState GetRawFileDescriptorFromHap(const std::string &rawFileName, RawFileDescriptor &descriptor) = 0; 193 194 virtual RState IsLoadHap(std::string &hapPath) = 0; 195 196 virtual RState GetRawFileList(const std::string &rawDirPath, std::vector<std::string> &rawfileList) = 0; 197 198 virtual RState GetDrawableInfoById(uint32_t id, std::string &type, size_t &len, 199 std::unique_ptr<uint8_t[]> &outValue, uint32_t density = 0) = 0; 200 201 virtual RState GetDrawableInfoByName(const char *name, std::string &type, size_t &len, 202 std::unique_ptr<uint8_t[]> &outValue, uint32_t density = 0) = 0; 203 204 virtual bool AddResource(const std::string &path, const std::vector<std::string> &overlayPaths) = 0; 205 206 virtual bool RemoveResource(const std::string &path, const std::vector<std::string> &overlayPaths) = 0; 207 208 virtual RState GetStringFormatById(uint32_t id, std::string &outValue, 209 std::vector<std::tuple<NapiValueType, std::string>> &jsParams) = 0; 210 211 virtual RState GetStringFormatByName(const char *name, std::string &outValue, 212 std::vector<std::tuple<NapiValueType, std::string>> &jsParams) = 0; 213 214 virtual uint32_t GetResourceLimitKeys() = 0; 215 216 virtual bool AddAppOverlay(const std::string &path) = 0; 217 218 virtual bool RemoveAppOverlay(const std::string &path) = 0; 219 220 virtual RState GetRawFdNdkFromHap(const std::string &rawFileName, RawFileDescriptor &descriptor) = 0; 221 222 virtual RState GetResId(const std::string &resTypeName, uint32_t &resId) = 0; 223 224 virtual void GetLocales(std::vector<std::string> &outValue, bool includeSystem = false) = 0; 225 226 virtual RState GetDrawableInfoById(uint32_t id, 227 std::tuple<std::string, size_t, std::string> &drawableInfo, 228 std::unique_ptr<uint8_t[]> &outValue, uint32_t iconType, uint32_t density = 0) = 0; 229 230 virtual RState GetDrawableInfoByName(const char *name, 231 std::tuple<std::string, size_t, std::string> &drawableInfo, 232 std::unique_ptr<uint8_t[]> &outValue, uint32_t iconType, uint32_t density = 0) = 0; 233 234 virtual RState GetSymbolById(uint32_t id, uint32_t &outValue) = 0; 235 236 virtual RState GetSymbolByName(const char *name, uint32_t &outValue) = 0; 237 238 virtual RState GetThemeIcons(uint32_t resId, std::pair<std::unique_ptr<uint8_t[]>, size_t> &foregroundInfo, 239 std::pair<std::unique_ptr<uint8_t[]>, size_t> &backgroundInfo, uint32_t density = 0, 240 const std::string &abilityName = "") = 0; 241 242 virtual std::string GetThemeMask() = 0; 243 244 virtual bool HasIconInTheme(const std::string &bundleName) = 0; 245 246 virtual RState GetOtherIconsInfo(const std::string &iconName, 247 std::unique_ptr<uint8_t[]> &outValue, size_t &len, bool isGlobalMask) = 0; 248 249 virtual RState IsRawDirFromHap(const std::string &pathName, bool &outValue) = 0; 250 251 virtual std::shared_ptr<ResourceManager> GetOverrideResourceManager( 252 std::shared_ptr<ResConfig> overrideResConfig) = 0; 253 254 virtual RState UpdateOverrideResConfig(ResConfig &resConfig) = 0; 255 256 virtual void GetOverrideResConfig(ResConfig &resConfig) = 0; 257 258 virtual RState GetDynamicIcon(const std::string &resName, std::pair<std::unique_ptr<uint8_t[]>, size_t> &iconInfo, 259 uint32_t density = 0) = 0; 260 261 virtual RState GetStringFormatById(std::string &outValue, uint32_t id, va_list args) = 0; 262 263 virtual RState GetStringFormatByName(std::string &outValue, const char *name, va_list args) = 0; 264 265 virtual RState GetFormatPluralStringById(std::string &outValue, uint32_t id, int quantity, 266 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams) = 0; 267 268 virtual RState GetFormatPluralStringByName(std::string &outValue, const char *name, int quantity, 269 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams) = 0; 270 271 virtual bool AddPatchResource(const char *path, const char *patchPath) = 0; 272 273 virtual RState GetThemeDataByName(const char *name, std::map<std::string, ResData> &outValue) = 0; 274 275 virtual RState GetThemeDataById(uint32_t id, std::map<std::string, ResData> &outValue) = 0; 276 277 virtual RState GetPatternDataById(uint32_t id, std::map<std::string, ResData> &outValue) = 0; 278 279 virtual RState GetPatternDataByName(const char *name, std::map<std::string, ResData> &outValue) = 0; 280 281 virtual RState GetFormatPluralStringById(std::string &outValue, uint32_t id, Quantity quantity, 282 va_list args) = 0; 283 284 virtual RState GetFormatPluralStringById(std::string &outValue, uint32_t id, Quantity quantity, 285 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams) = 0; 286 287 virtual RState GetFormatPluralStringByName(std::string &outValue, const char *name, Quantity quantity, 288 va_list args) = 0; 289 290 virtual RState GetFormatPluralStringByName(std::string &outValue, const char *name, Quantity quantity, 291 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams) = 0; 292 }; 293 294 EXPORT_FUNC ResourceManager *CreateResourceManager(bool includeSystemRes = true); 295 296 /** 297 * Get system resource manager, the added system resource is sandbox path. This method should call 298 * after the sandbox mount. 299 * 300 * @return pointer of system resource manager 301 * @deprecated since 14 302 * @useinstead CreateResourceManager 303 */ 304 EXPORT_FUNC ResourceManager *GetSystemResourceManager(); 305 306 /** 307 * Get system resource manager, the added system resource is no sandbox path. This method should call 308 * before the sandbox mount, for example appspawn. 309 * 310 * @return pointer of system resource manager 311 * @deprecated since 14 312 * @useinstead CreateResourceManager 313 */ 314 EXPORT_FUNC ResourceManager *GetSystemResourceManagerNoSandBox(); 315 316 /** 317 * Create app resource manager. 318 * 319 * @param bundleName the hap bundleName 320 * @param moduleName the hap moduleName 321 * @param hapPath the hap resource path 322 * @param overlayPath the hap overlay resource path 323 * @param resConfig the device resConfig 324 * @param appType the app type 325 * @return pointer of app resource manager 326 */ 327 EXPORT_FUNC std::shared_ptr<ResourceManager> CreateResourceManager(const std::string &bundleName, 328 const std::string &moduleName, const std::string &hapPath, const std::vector<std::string> &overlayPath, 329 ResConfig &resConfig, int32_t appType = 0, int32_t userId = 100); 330 331 /** 332 * Release system resource manager. This object may be held by multiple objects, and once released, the 333 * ResourceManagers of all holders are released. When using this interface, pay attention to the impact on all holders. 334 */ 335 EXPORT_FUNC void ReleaseSystemResourceManager(); 336 } // namespace Resource 337 } // namespace Global 338 } // namespace OHOS 339 #endif