1 /* 2 * Copyright (c) 2024-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 DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_JSON_H 17 #define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_JSON_H 18 19 #include <filesystem> 20 #include <list> 21 #include <memory> 22 #include <string> 23 24 #include "hap_verify_info.h" 25 #include "module_api_version.h" 26 #include "module_metadata_info.h" 27 #include "normalize_version.h" 28 #include "pt_json.h" 29 #include "version.h" 30 31 namespace OHOS { 32 namespace AppPackingTool { 33 class ModuleJson { 34 public: ModuleJson()35 ModuleJson() {}; ~ModuleJson()36 virtual ~ModuleJson() {}; 37 38 bool ParseFromString(const std::string& jsonString); 39 bool ParseFromFile(const std::string& jsonFile); 40 std::string ToString(); 41 void Release(); 42 43 bool IsValid(); 44 45 // object funcs 46 bool GetAppObject(std::unique_ptr<PtJson>& appObj); 47 bool GetDeviceConfigObject(std::unique_ptr<PtJson>& deviceConfigObj); 48 bool GetVersionObject(std::unique_ptr<PtJson>& versionObj); 49 bool GetModuleObject(std::unique_ptr<PtJson>& moduleObj); 50 bool GetDistroObject(std::unique_ptr<PtJson>& distroObj); 51 bool GetDistroObjectByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::unique_ptr<PtJson>& distroObj); 52 bool GetApiVersionObject(std::unique_ptr<PtJson>& apiVersionObj); 53 54 // stage funcs, module.json 55 bool SetStageBundleName(const std::string& bundleName); 56 bool SetStageMinCompatibleVersionCode(const int32_t& minCompatibleVersionCode); 57 bool SetStageMinAPIVersion(const int32_t& minAPIVersion); 58 bool SetStageTargetAPIVersion(const int32_t& targetAPIVersion); 59 bool SetStageApiReleaseType(const std::string& apiReleaseType); 60 bool SetStageBundleType(const std::string& bundleType); 61 bool SetStageInstallationFree(const bool& installationFree); 62 bool SetStageDeliveryWithInstall(const bool& deliveryWithInstall); 63 bool SetStageDeviceTypes(const std::list<std::string>& deviceType); 64 65 // fa funcs, module.json 66 bool SetFaBundleName(const std::string& bundleName); 67 bool SetFaMinCompatibleVersionCode(const int32_t& minCompatibleVersionCode); 68 bool SetFaMinAPIVersion(const int32_t& minAPIVersion); 69 bool SetFaTargetAPIVersion(const int32_t& targetAPIVersion); 70 bool SetFaApiReleaseType(const std::string& apiReleaseType); 71 bool SetFaBundleType(const std::string& bundleType); 72 bool SetFaInstallationFree(const bool& installationFree); 73 bool SetFaDeliveryWithInstall(const bool& deliveryWithInstall); 74 bool SetFaDeviceTypes(const std::list<std::string>& deviceType); 75 76 bool GetStageVersion(Version& version); 77 bool GetStageVersionByAppObj(std::unique_ptr<PtJson>& appObj, Version& version); 78 bool SetStageVersionCode(const int32_t& versionCode); 79 bool SetStageVersionName(const std::string& versionName); 80 bool GetStageModuleApiVersion(ModuleApiVersion& moduleApiVersion); 81 bool GetStageModuleApiVersionByAppObj(std::unique_ptr<PtJson>& appObj, ModuleApiVersion& moduleApiVersion); 82 bool GetStageModuleName(std::string& moduleName); 83 bool GetStageModuleNameByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& moduleName); 84 bool GetStageEntry(std::list<std::string>& deviceTypes); 85 bool GetStageDeviceTypes(std::list<std::string>& deviceTypes); 86 bool GetStageDeviceTypesByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::list<std::string>& deviceTypes); 87 bool GetStageHapVerifyInfo(HapVerifyInfo& hapVerifyInfo); 88 bool GetStageDistroFilter(DistroFilter& distroFilter, 89 const std::map<std::string, std::string>& resourceMap); 90 bool GetStageDistroFilterByModuleObj(std::unique_ptr<PtJson>& moduleObj, 91 const std::map<std::string, std::string>& resourceMap, DistroFilter& distroFilter); 92 bool GetStageInstallationFree(bool& installationFree); 93 bool GetStageInstallationFreeByModuleObj(std::unique_ptr<PtJson>& moduleObj, bool& installationFree); 94 bool GetStageBundleType(std::string& bundleType); 95 bool GetStageCompileSdkType(std::string& compileSdkType); 96 bool GetStageCompileSdkTypeByAppObj(std::unique_ptr<PtJson>& appObj, std::string& compileSdkType); 97 bool GetStageCompileSdkVersion(std::string& compileSdkVersion); 98 bool GetStageCompileSdkVersionByAppObj(std::unique_ptr<PtJson>& appObj, std::string& compileSdkVersion); 99 bool GetStageModuleType(std::string& moduleType); 100 bool GetStageModuleTypeByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& moduleType); 101 bool GetStageDebug(bool& debug); 102 bool GetStageDebugByAppObj(std::unique_ptr<PtJson>& appObj, bool& debug); 103 bool GetStageAsanEnabled(bool& asanEnabled); 104 bool GetStageAsanEnabledByAppObj(std::unique_ptr<PtJson>& appObj, bool& asanEnabled); 105 bool GetStageTsanEnabled(bool& tsanEnabled); 106 bool GetStageTsanEnabledByAppObj(std::unique_ptr<PtJson>& appObj, bool& tsanEnabled); 107 bool GetStageApiReleaseType(std::string& apiReleaseType); 108 bool GetStageApiReleaseTypeByAppObj(std::unique_ptr<PtJson>& appObj, std::string& apiReleaseType); 109 bool GetStageCompressNativeLibs(bool& compressNativeLibs); 110 bool GetStageCompressNativeLibsByAppObj(std::unique_ptr<PtJson>& appObj, bool& compressNativeLibs); 111 bool GetAbilityContinueTypeMap(std::map<std::string, std::list<std::string>>& abilityContinueTypeMap); 112 bool GetAbilityContinueTypeMapByModuleObj(std::unique_ptr<PtJson>& moduleObj, 113 std::map<std::string, std::list<std::string>>& abilityContinueTypeMap); 114 bool GetExtensionAbilityNames(std::list<std::string>& extensionAbilityNames); 115 bool GetExtensionAbilityNamesByModuleObj(std::unique_ptr<PtJson>& moduleObj, 116 std::list<std::string>& extensionAbilityNames); 117 bool GetModuleMetadatas(std::list<ModuleMetadataInfo>& moduleMetadataInfos, 118 const std::map<std::string, std::string>& resourceMap); 119 bool GetModuleMetadatasByModuleObj(std::unique_ptr<PtJson>& moduleObj, 120 const std::map<std::string, std::string>& resourceMap, std::list<ModuleMetadataInfo>& moduleMetadataInfos); 121 bool ParseModuleMetadatasToDistroFilter(const std::list<ModuleMetadataInfo>& moduleMetadataInfos, 122 DistroFilter& distroFilter); 123 124 bool IsExistedStageRequestPermissions(); 125 bool IsExistedStageModuleTargetPriority(); 126 bool IsExistedStageAppTargetPriority(); 127 128 // fa funcs config.json 129 bool GetFaVersion(Version& version); 130 bool GetFaVersionByAppObj(std::unique_ptr<PtJson>& appObj, Version& version); 131 bool GetFaVersionByVersionObj(std::unique_ptr<PtJson>& versionObj, Version& version); 132 bool SetFaVersionCode(const int32_t& versionCode); 133 bool SetFaVersionName(const std::string& versionName); 134 bool GetFaInstallationFree(bool& installationFree); 135 bool GetFaInstallationFreeByModuleObj(std::unique_ptr<PtJson>& moduleObj, bool& installationFree); 136 bool GetFaInstallationFreeByDistroObj(std::unique_ptr<PtJson>& distroObj, bool& installationFree); 137 bool GetFaBundleType(std::string& bundleType); 138 bool GetFaModuleApiVersion(ModuleApiVersion& moduleApiVersion); 139 bool GetFaModuleApiVersionByAppObj(std::unique_ptr<PtJson>& appObj, ModuleApiVersion& moduleApiVersion); 140 bool GetFaModuleApiVersionByApiVersionObj(std::unique_ptr<PtJson>& apiVersionObj, 141 ModuleApiVersion& moduleApiVersion); 142 bool GetFaModuleName(std::string& faModuleName); 143 bool GetFaModuleNameByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& faModuleName); 144 bool GetFaModuleNameByDistroObj(std::unique_ptr<PtJson>& distroObj, std::string& faModuleName); 145 bool GetFaPackageStr(std::string& packageStr); 146 bool GetFaPackageStrByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& packageStr); 147 bool GetFaEntry(std::list<std::string>& deviceTypes); 148 bool GetFaDeviceTypes(std::list<std::string>& deviceTypes); 149 bool GetFaDeviceTypesByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::list<std::string>& deviceTypes); 150 bool GetFaHapVerifyInfo(HapVerifyInfo& hapVerifyInfo); 151 bool GetFaDistroFilter(DistroFilter& distroFilter); 152 bool GetFaDistroFilterByModuleObj(std::unique_ptr<PtJson>& moduleObj, DistroFilter& distroFilter); 153 bool GetFaCompileSdkType(std::string& compileSdkType); 154 bool GetFaCompileSdkTypeByAppObj(std::unique_ptr<PtJson>& appObj, std::string& compileSdkType); 155 bool GetFaCompileSdkVersion(std::string& compileSdkVersion); 156 bool GetFaCompileSdkVersionByAppObj(std::unique_ptr<PtJson>& appObj, std::string& compileSdkVersion); 157 bool GetFaModuleType(std::string& moduleType); 158 bool GetFaModuleTypeByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& moduleType); 159 bool GetFaDebug(bool& debug); 160 bool GetFaDebugByDeviceConfigObj(std::unique_ptr<PtJson>& deviceConfigObj, bool& debug); 161 bool GetFaAsanEnabled(bool& asanEnabled); 162 bool GetFaAsanEnabledByAppObj(std::unique_ptr<PtJson>& appObj, bool& asanEnabled); 163 bool GetFaReleaseType(std::string& releaseType); 164 bool GetFaReleaseTypeByAppObj(std::unique_ptr<PtJson>& appObj, std::string& releaseType); 165 bool GetFaDeliveryWithInstall(bool& deliveryWithInstall); 166 167 // common funcs 168 bool GetBundleName(std::string& bundleName); 169 bool GetBundleNameByAppObj(std::unique_ptr<PtJson>& appObj, std::string& bundleName); 170 bool SetBundleName(const std::string& bundleName); 171 bool GetModuleName(std::string& moduleName); 172 bool GetModuleNameByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& moduleName); 173 bool GetVendor(std::string& vendor); 174 bool GetVendorByAppObj(std::unique_ptr<PtJson>& appObj, std::string& vendor); 175 bool GetTargetBundleName(std::string& targetBundleName); 176 bool GetTargetBundleNameByAppObj(std::unique_ptr<PtJson>& appObj, std::string& targetBundleName); 177 bool GetTargetModuleName(std::string& targetModuleName); 178 bool GetTargetModuleNameByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::string& targetModuleName); 179 bool GetTargetPriority(int32_t& targetPriority); 180 bool GetTargetPriorityByAppObj(std::unique_ptr<PtJson>& appObj, int32_t& targetPriority); 181 bool GetTargetModulePriority(int32_t& targetModulePriority); 182 bool GetTargetModulePriorityByModuleObj(std::unique_ptr<PtJson>& moduleObj, int32_t& targetModulePriority); 183 184 // config.json or module.json 185 bool GetAbilityNames(std::list<std::string>& abilityNames); 186 bool GetAbilityNamesByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::list<std::string>& abilityNames); 187 // unknown 188 bool GetDependencyItems(std::list<DependencyItem>& dependencyItems, const std::string& defaultBundleName); 189 bool GetDependencyItemsByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::list<DependencyItem>& dependencyItems, 190 const std::string& defaultBundleName); 191 bool GetAtomicServicePreloads(std::list<PreloadItem>& preloadItems); 192 bool GetAtomicServicePreloadsByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::list<PreloadItem>& preloadItems); 193 bool GetProxyDataUris(std::list<std::string>& proxyDataUris); 194 bool GetProxyDataUrisByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::list<std::string>& proxyDataUris); 195 bool GetProxyDataUrisByProxyDatasObj(std::unique_ptr<PtJson>& proxyDatasObj, 196 std::list<std::string>& proxyDataUris); 197 198 bool GetAssetAccessGroups(std::list<std::string>& assetAccessGroups); 199 bool GetAssetAccessGroupsByModuleObj(std::unique_ptr<PtJson>& moduleObj, std::list<std::string>& assetAccessGroups); 200 201 bool GetMultiAppMode(MultiAppMode& multiAppMode); 202 bool GetMultiAppModeByAppObj(std::unique_ptr<PtJson>& appObj, MultiAppMode& multiAppMode); 203 bool GetMinApiVersion(int32_t& minAPIVersion); 204 bool GetTargetApiVersion(int32_t& targetAPIVersion); 205 bool GetDeliveryWithInstall(bool& deliveryWithInstall); 206 207 bool IsModuleAtomicServiceValid(); 208 bool CheckEntryInAtomicService(); 209 bool CheckAtomicServiceInstallationFree(); 210 bool CheckStageAsanTsanEnabledValid(); 211 bool CheckStageAtomicService(); 212 bool CheckStageOverlayCfg(); 213 214 bool GetGenerateBuildHash(bool& generateBuildHash); 215 bool RemoveGenerateBuildHash(); 216 bool RemoveGenerateBuildHashFromAppObj(); 217 bool RemoveGenerateBuildHashFromModuleObj(); 218 219 bool GetNormalizeVersion(NormalizeVersion& normalizeVersion, const bool& isStage = true); 220 bool SetVersionCodeAndName(const int32_t& versionCode, const std::string& versionName, const bool& isStage = true); 221 222 bool SetBuildHash(const std::string& buildHash); 223 224 // json function for hqf 225 bool GetPatchModuleName(std::string& patchModuleName); 226 227 bool SetBundleName(const std::string& bundleName, const bool& isStage); 228 bool SetMinCompatibleVersionCode(const int32_t& minCompatibleVersionCode, const bool& isStage); 229 bool SetMinAPIVersion(const int32_t& minAPIVersion, const bool& isStage); 230 bool SetTargetAPIVersion(const int32_t& targetAPIVersion, const bool& isStage); 231 bool SetApiReleaseType(const std::string& apiReleaseType, const bool& isStage); 232 bool SetBundleType(const std::string& bundleType, const bool& isStage); 233 bool SetInstallationFree(const bool& installationFree, const bool& isStage); 234 bool SetDeliveryWithInstall(const bool& deliveryWithInstall, const bool& isStage); 235 bool SetVersionCode(const int32_t& versionCode, const bool& isStage); 236 bool SetVersionName(const std::string& versionName, const bool& isStage); 237 bool SetDeviceTypes(const std::list<std::string>& deviceTypes, const bool& isStage); 238 239 protected: 240 bool CheckStageBundleType(const std::string& moduleName, const std::string& moduleType, 241 const std::string& bundleType, const bool& installationFree); 242 bool GetAbilityNameByAbilityObj(std::unique_ptr<PtJson>& abilityObj, std::string& abilityName); 243 bool GetContinueTypesByAbilityObj(std::unique_ptr<PtJson>& abilityObj, std::list<std::string>& continueTypes); 244 bool GetDependencyBundleNameByDependencyItemObj(std::unique_ptr<PtJson>& dependencyItemObj, 245 std::string& bundleName, const std::string& defaultBundleName); 246 bool GetDependencyModuleNameByDependencyItemObj(std::unique_ptr<PtJson>& dependencyItemObj, 247 std::string& moduleName); 248 bool GetModuleMetadataInfoByModuleMetadataInfoObj(std::unique_ptr<PtJson>& moduleMetadataInfoObj, 249 const std::map<std::string, std::string>& resourceMap, ModuleMetadataInfo& moduleMetadataInfo); 250 bool GetExtensionAbilityNamesByExtensionAbilityObj(std::unique_ptr<PtJson>& extensionAbilitiesObj, 251 std::list<std::string>& extensionAbilityNames); 252 bool GetAbilityNamesByAbilitiesObj(std::unique_ptr<PtJson>& abilitiesObj, std::list<std::string>& abilityNames); 253 bool SetStageHapVerifyInfoByAppObj(std::unique_ptr<PtJson>& appObj, HapVerifyInfo& hapVerifyInfo); 254 bool SetStageHapVerifyInfoByModuleObj(std::unique_ptr<PtJson>& moduleObj, HapVerifyInfo& hapVerifyInfo); 255 bool SetStageHapVerifyInfoExtByModuleObj(std::unique_ptr<PtJson>& moduleObj, HapVerifyInfo& hapVerifyInfo); 256 bool SetFaHapVerifyInfoByAppObj(std::unique_ptr<PtJson>& appObj, HapVerifyInfo& hapVerifyInfo); 257 bool SetFaHapVerifyInfoByModuleObj(std::unique_ptr<PtJson>& moduleObj, HapVerifyInfo& hapVerifyInfo); 258 259 private: 260 std::unique_ptr<PtJson> root_ = nullptr; 261 }; 262 } // namespace AppPackingTool 263 } // namespace OHOS 264 #endif // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_MODULE_JSON_H 265