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 16 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BASE_BUNDLE_INSTALLER_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BASE_BUNDLE_INSTALLER_H 18 19 #include <map> 20 #include <unordered_map> 21 #include <string> 22 23 #include "nocopyable.h" 24 25 #include "access_token.h" 26 #include "bundle_common_event_mgr.h" 27 #include "bundle_data_mgr.h" 28 #include "bundle_install_checker.h" 29 #include "event_report.h" 30 #include "install_param.h" 31 #include "quick_fix/appqf_info.h" 32 #include "shared_bundle_installer.h" 33 34 #ifdef APP_DOMAIN_VERIFY_ENABLED 35 #include "app_domain_verify_mgr_client.h" 36 #endif 37 38 namespace OHOS { 39 namespace AppExecFwk { 40 class BaseBundleInstaller { 41 public: 42 BaseBundleInstaller(); 43 virtual ~BaseBundleInstaller(); 44 void SetCallingUid(int32_t callingUid); 45 46 protected: 47 bool otaInstall_ = false; 48 enum class InstallerState { 49 INSTALL_START, 50 INSTALL_BUNDLE_CHECKED = 5, 51 INSTALL_SYSCAP_CHECKED = 10, 52 INSTALL_SIGNATURE_CHECKED = 15, 53 INSTALL_PARSED = 20, 54 INSTALL_HAP_HASH_PARAM_CHECKED = 25, 55 INSTALL_OVERLAY_CHECKED = 30, 56 INSTALL_VERSION_AND_BUNDLENAME_CHECKED = 35, 57 INSTALL_NATIVE_SO_CHECKED = 40, 58 INSTALL_PROXY_DATA_CHECKED = 45, 59 INSTALL_REMOVE_SANDBOX_APP = 50, 60 INSTALL_EXTRACTED = 60, 61 INSTALL_INFO_SAVED = 80, 62 INSTALL_RENAMED = 90, 63 INSTALL_SUCCESS = 100, 64 INSTALL_FAILED, 65 }; 66 67 enum SingletonState { 68 DEFAULT, 69 SINGLETON_TO_NON = 1, 70 NON_TO_SINGLETON = 2, 71 }; 72 73 struct SharedBundleRollBackInfo { 74 std::vector<std::string> newDirs; // record newly created directories, delete when rollback 75 std::vector<std::string> newBundles; // record newly installed bundle, uninstall when rollback 76 std::unordered_map<std::string, InnerBundleInfo> backupBundles; // record initial InnerBundleInfo 77 }; 78 79 /** 80 * @brief The main function for system and normal bundle install. 81 * @param bundlePath Indicates the path for storing the HAP file of the application 82 * to install or update. 83 * @param installParam Indicates the install parameters. 84 * @param appType Indicates the application type. 85 * @return Returns ERR_OK if the application install successfully; returns error code otherwise. 86 */ 87 ErrCode InstallBundle( 88 const std::string &bundlePath, const InstallParam &installParam, const Constants::AppType appType); 89 /** 90 * @brief The main function for system and normal bundle install. 91 * @param bundlePaths Indicates the paths for storing the HAP file sof the application 92 * to install or update. 93 * @param installParam Indicates the install parameters. 94 * @param appType Indicates the application type. 95 * @return Returns ERR_OK if the application install successfully; returns error code otherwise. 96 */ 97 ErrCode InstallBundle(const std::vector<std::string> &bundlePaths, const InstallParam &installParam, 98 const Constants::AppType appType); 99 /** 100 * @brief The main function for uninstall a bundle. 101 * @param bundleName Indicates the bundle name of the application to uninstall. 102 * @param installParam Indicates the uninstall parameters. 103 * @return Returns ERR_OK if the application uninstall successfully; returns error code otherwise. 104 */ 105 ErrCode UninstallBundle(const std::string &bundleName, const InstallParam &installParam); 106 /** 107 * @brief The main function for uninstall a module in a specific bundle. 108 * @param bundleName Indicates the bundle name of the application to uninstall. 109 * @param modulePackage Indicates the module package of the module to uninstall. 110 * @param installParam Indicates the uninstall parameters. 111 * @return Returns ERR_OK if the application uninstall successfully; returns error code otherwise. 112 */ 113 ErrCode UninstallBundle( 114 const std::string &bundleName, const std::string &modulePackage, const InstallParam &installParam); 115 /** 116 * @brief The main function for uninstall a bundle by uninstallParam. 117 * @param uninstallParam Indicates the input of uninstallParam. 118 * @return Returns ERR_OK if the application uninstall successfully; returns error code otherwise. 119 */ 120 ErrCode CheckUninstallInnerBundleInfo(const InnerBundleInfo &info, const std::string &bundleName); 121 ErrCode UninstallBundleByUninstallParam(const UninstallParam &uninstallParam); 122 /** 123 * @brief Update the installer state. 124 * @attention This function changes the base class state only. 125 * @param state Indicates the state to be updated to. 126 * @return 127 */ 128 virtual void UpdateInstallerState(const InstallerState state); 129 /** 130 * @brief Get the installer state. 131 * @return The current state of the installer object. 132 */ GetInstallerState()133 inline InstallerState GetInstallerState() 134 { 135 return state_; 136 } 137 /** 138 * @brief Get the installer state. 139 * @param state Indicates the state to be updated to. 140 * @return 141 */ SetInstallerState(InstallerState state)142 inline void SetInstallerState(InstallerState state) 143 { 144 state_ = state; 145 } 146 /** 147 * @brief The main function for bundle install by bundleName. 148 * @param bundleName Indicates the bundleName of the application to install. 149 * @param installParam Indicates the install parameters. 150 * @return Returns ERR_OK if the application install successfully; returns error code otherwise. 151 */ 152 ErrCode Recover(const std::string &bundleName, const InstallParam &installParam); 153 /** 154 * @brief The main function for bundle install by bundleName. 155 * @param bundleName Indicates the bundleName of the application to install. 156 * @param installParam Indicates the install parameters. 157 * @return Returns ERR_OK if the application install successfully; returns error code otherwise. 158 */ 159 ErrCode InstallBundleByBundleName(const std::string &bundleName, const InstallParam &installParam); 160 /** 161 * @brief Reset install properties. 162 */ 163 void ResetInstallProperties(); 164 /** 165 * @brief Reset install properties. 166 * @param isBootScene Indicates the event occurs in the boot phase. 167 */ MarkPreBundleSyeEventBootTag(bool isBootScene)168 void MarkPreBundleSyeEventBootTag(bool isBootScene) 169 { 170 sysEventInfo_.preBundleScene = 171 isBootScene ? InstallScene::BOOT : InstallScene::REBOOT; 172 } 173 174 bool NotifyAllBundleStatus(); 175 176 std::string GetCheckResultMsg() const; 177 178 void SetCheckResultMsg(const std::string checkResultMsg) const; 179 180 private: 181 /** 182 * @brief The real procedure for system and normal bundle install. 183 * @param bundlePath Indicates the path for storing the HAP file of the application 184 * to install or update. 185 * @param installParam Indicates the install parameters. 186 * @param appType Indicates the application type. 187 * @param uid Indicates the uid of the application. 188 * @return Returns ERR_OK if the bundle install successfully; returns error code otherwise. 189 */ 190 ErrCode ProcessBundleInstall(const std::vector<std::string> &bundlePaths, const InstallParam &installParam, 191 const Constants::AppType appType, int32_t &uid); 192 193 ErrCode InnerProcessBundleInstall(std::unordered_map<std::string, InnerBundleInfo> &newInfos, 194 InnerBundleInfo &oldInfo, const InstallParam &installParam, int32_t &uid); 195 196 /** 197 * @brief The real procedure function for uninstall a bundle. 198 * @param bundleName Indicates the bundle name of the application to uninstall. 199 * @param installParam Indicates the uninstall parameters. 200 * @param uid Indicates the uid of the application. 201 * @return Returns ERR_OK if the bundle uninstall successfully; returns error code otherwise. 202 */ 203 ErrCode ProcessBundleUninstall(const std::string &bundleName, const InstallParam &installParam, int32_t &uid); 204 /** 205 * @brief The real procedure for uninstall a module in a specific bundle. 206 * @param bundleName Indicates the bundle name of the application to uninstall. 207 * @param modulePackage Indicates the module package of the module to uninstall. 208 * @param installParam Indicates the uninstall parameters. 209 * @param uid Indicates the uid of the application. 210 * @return Returns ERR_OK if the module uninstall successfully; returns error code otherwise. 211 */ 212 ErrCode ProcessBundleUninstall(const std::string &bundleName, const std::string &modulePackage, 213 const InstallParam &installParam, int32_t &uid); 214 /** 215 * @brief The process of installing a new bundle. 216 * @param info Indicates the InnerBundleInfo parsed from the config.json in the HAP package. 217 * @param uid Indicates the uid of the application. 218 * @return Returns ERR_OK if the new bundle install successfully; returns error code otherwise. 219 */ 220 ErrCode ProcessBundleInstallStatus(InnerBundleInfo &info, int32_t &uid); 221 /** 222 * @brief The process of installing a native bundle. 223 * @param info Indicates the InnerBundleInfo parsed from the config.json in the HAP package. 224 * @param uid Indicates the uid of the application. 225 * @return Returns ERR_OK if the native bundle install successfully; returns error code otherwise. 226 */ 227 ErrCode ProcessBundleInstallNative(InnerBundleInfo &info, int32_t &userId); 228 /** 229 * @brief The process of uninstalling a native bundle. 230 * @param info Indicates the InnerBundleInfo parsed from the config.json in the HAP package. 231 * @param uid Indicates the uid of the application. 232 * @param bundleName Indicates the bundleName of the application. 233 * @return Returns ERR_OK if the native bundle uninstall successfully; returns error code otherwise. 234 */ 235 ErrCode ProcessBundleUnInstallNative(InnerBundleInfo &info, int32_t &userId, std::string bundleName); 236 /** 237 * @brief The process of updating an exist bundle. 238 * @param oldInfo Indicates the exist InnerBundleInfo object get from the database. 239 * @param newInfo Indicates the InnerBundleInfo object parsed from the config.json in the HAP package. 240 * @param isReplace Indicates whether there is the replace flag in the install flag. 241 * @return Returns ERR_OK if the bundle updating successfully; returns error code otherwise. 242 */ 243 ErrCode ProcessBundleUpdateStatus(InnerBundleInfo &oldInfo, 244 InnerBundleInfo &newInfo, bool isReplace, bool killProcess = true); 245 /** 246 * @brief Remove a whole bundle. 247 * @param info Indicates the InnerBundleInfo object of a bundle. 248 * @param isKeepData Indicates that whether to save data. 249 * @return Returns ERR_OK if the bundle removed successfully; returns error code otherwise. 250 */ 251 ErrCode RemoveBundle(InnerBundleInfo &info, bool isKeepData); 252 /** 253 * @brief Create the code and data directories of a bundle. 254 * @param info Indicates the InnerBundleInfo object of a bundle. 255 * @return Returns ERR_OK if the bundle directories created successfully; returns error code otherwise. 256 */ 257 ErrCode CreateBundleAndDataDir(InnerBundleInfo &info) const; 258 /** 259 * @brief Extract the code to temporilay directory and rename it. 260 * @param info Indicates the InnerBundleInfo object of a bundle. 261 * @param modulePath normal files decompression path. 262 * @return Returns ERR_OK if the bundle extract and renamed successfully; returns error code otherwise. 263 */ 264 ErrCode ExtractModule(InnerBundleInfo &info, const std::string &modulePath); 265 /** 266 * @brief Remove the code and data directories of a bundle. 267 * @param info Indicates the InnerBundleInfo object of a bundle. 268 * @param isKeepData Indicates that whether to save data. 269 * @return Returns ERR_OK if the bundle directories removed successfully; returns error code otherwise. 270 */ 271 ErrCode RemoveBundleAndDataDir(const InnerBundleInfo &info, bool isKeepData); 272 /** 273 * @brief Remove the code and data directories of a module in a bundle. 274 * @param info Indicates the InnerBundleInfo object of a bundle. 275 * @param modulePackage Indicates the module to be removed. 276 * @param userId Indicates the userId. 277 * @param isKeepData Indicates that whether to save data. 278 * @return Returns ERR_OK if the bundle directories removed successfully; returns error code otherwise. 279 */ 280 ErrCode RemoveModuleAndDataDir(const InnerBundleInfo &info, 281 const std::string &modulePackage, int32_t userId, bool isKeepData) const; 282 /** 283 * @brief Remove the current installing module directory. 284 * @param info Indicates the InnerBundleInfo object of a bundle under installing. 285 * @return Returns ERR_OK if the module directory removed successfully; returns error code otherwise. 286 */ 287 ErrCode RemoveModuleDir(const std::string &modulePath) const; 288 /** 289 * @brief Extract files of the current installing module package. 290 * @param info Indicates the InnerBundleInfo object of a bundle under installing. 291 * @param modulePath normal files decompression path. 292 * @param targetSoPath so files decompression path. 293 * @param cpuAbi cpuAbi. 294 * @return Returns ERR_OK if the module files extraced successfully; returns error code otherwise. 295 */ 296 ErrCode ExtractModuleFiles(const InnerBundleInfo &info, const std::string &modulePath, 297 const std::string &targetSoPath, const std::string &cpuAbi); 298 /** 299 * @brief Rename the directory of current installing module package. 300 * @param info Indicates the InnerBundleInfo object of a bundle under installing. 301 * @return Returns ERR_OK if the module directory renamed successfully; returns error code otherwise. 302 */ 303 ErrCode RenameModuleDir(const InnerBundleInfo &info) const; 304 /** 305 * @brief The process of install a new module package. 306 * @param newInfo Indicates the InnerBundleInfo object parsed from the config.json in the HAP package. 307 * @param oldInfo Indicates the exist InnerBundleInfo object get from the database. 308 * @return Returns ERR_OK if the new module install successfully; returns error code otherwise. 309 */ 310 ErrCode ProcessNewModuleInstall(InnerBundleInfo &newInfo, InnerBundleInfo &oldInfo); 311 /** 312 * @brief The process of updating an exist module package. 313 * @param newInfo Indicates the InnerBundleInfo object parsed from the config.json in the HAP package. 314 * @param oldInfo Indicates the exist InnerBundleInfo object get from the database. 315 * @return Returns ERR_OK if the module updating successfully; returns error code otherwise. 316 */ 317 ErrCode ProcessModuleUpdate(InnerBundleInfo &newInfo, 318 InnerBundleInfo &oldInfo, bool isReplace, bool killProcess = true); 319 /** 320 * @brief The real procedure for bundle install by bundleName. 321 * @param bundleName Indicates the bundleName the application to install. 322 * @param installParam Indicates the install parameters. 323 * @param uid Indicates the uid of the application. 324 * @return Returns ERR_OK if the bundle install successfully; returns error code otherwise. 325 */ 326 ErrCode ProcessRecover( 327 const std::string &bundleName, const InstallParam &installParam, int32_t &uid); 328 /** 329 * @brief The real procedure for bundle install by bundleName. 330 * @param bundleName Indicates the bundleName the application to install. 331 * @param installParam Indicates the install parameters. 332 * @param uid Indicates the uid of the application. 333 * @return Returns ERR_OK if the bundle install successfully; returns error code otherwise. 334 */ 335 ErrCode ProcessInstallBundleByBundleName( 336 const std::string &bundleName, const InstallParam &installParam, int32_t &uid); 337 /** 338 * @brief The real procedure for bundle install by bundleName. 339 * @param bundleName Indicates the bundleName the application to install. 340 * @param installParam Indicates the install parameters. 341 * @param uid Indicates the uid of the application. 342 * @return Returns ERR_OK if the bundle install successfully; returns error code otherwise. 343 */ 344 ErrCode InnerProcessInstallByPreInstallInfo( 345 const std::string &bundleName, const InstallParam &installParam, int32_t &uid); 346 /** 347 * @brief Check syscap. 348 * @param bundlePaths Indicates the file paths of all HAP packages. 349 * @return Returns ERR_OK if the syscap satisfy; returns error code otherwise. 350 */ 351 ErrCode CheckSysCap(const std::vector<std::string> &bundlePaths); 352 /** 353 * @brief Check signature info of multiple haps. 354 * @param bundlePaths Indicates the file paths of all HAP packages. 355 * @param installParam Indicates the install parameters. 356 * @param hapVerifyRes Indicates the signature info. 357 * @return Returns ERR_OK if the every hap has signature info and all haps have same signature info. 358 */ 359 ErrCode CheckMultipleHapsSignInfo( 360 const std::vector<std::string> &bundlePaths, 361 const InstallParam &installParam, 362 std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes); 363 /** 364 * @brief To parse hap files and to obtain innerBundleInfo of each hap. 365 * @param bundlePaths Indicates the file paths of all HAP packages. 366 * @param installParam Indicates the install parameters. 367 * @param appType Indicates the app type of the hap. 368 * @param hapVerifyRes Indicates all signature info of all haps. 369 * @param infos Indicates the innerBundleinfo of each hap. 370 * @return Returns ERR_OK if each hap is parsed successfully; returns error code otherwise. 371 */ 372 ErrCode ParseHapFiles( 373 const std::vector<std::string> &bundlePaths, 374 const InstallParam &installParam, 375 const Constants::AppType appType, 376 std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes, 377 std::unordered_map<std::string, InnerBundleInfo> &infos); 378 379 ErrCode CheckShellInstall(std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes); 380 381 ErrCode CheckInstallCondition(std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes, 382 std::unordered_map<std::string, InnerBundleInfo> &infos); 383 384 ErrCode CheckInstallPermission(const InstallParam &installParam, 385 std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes); 386 /** 387 * @brief To check dependency whether or not exists. 388 * @param infos Indicates all innerBundleInfo for all haps need to be installed. 389 * @param sharedBundleInstaller Cross-app shared bundle installer 390 * @return Returns ERR_OK if haps checking successfully; returns error code otherwise. 391 */ 392 ErrCode CheckDependency(std::unordered_map<std::string, InnerBundleInfo> &infos, 393 const SharedBundleInstaller &sharedBundleInstaller); 394 395 /** 396 * @brief To check the hap hash param. 397 * @param infos .Indicates all innerBundleInfo for all haps need to be installed. 398 * @param hashParams .Indicates all hashParams in installParam. 399 * @return Returns ERR_OK if haps checking successfully; returns error code otherwise. 400 */ 401 ErrCode CheckHapHashParams( 402 std::unordered_map<std::string, InnerBundleInfo> &infos, 403 std::map<std::string, std::string> hashParams); 404 /** 405 * @brief To check the version code and bundleName in all haps. 406 * @param infos .Indicates all innerBundleInfo for all haps need to be installed. 407 * @return Returns ERR_OK if haps checking successfully; returns error code otherwise. 408 */ 409 ErrCode CheckAppLabelInfo(const std::unordered_map<std::string, InnerBundleInfo> &infos); 410 411 /** 412 * @brief send notify to start install applicaiton 413 * @param installParam Indicates the install parameters. 414 * @param infos .Indicates all innerBundleInfo for all haps need to be installed. 415 */ 416 void SendStartInstallNotify(const InstallParam &installParam, 417 const std::unordered_map<std::string, InnerBundleInfo> &infos); 418 419 ErrCode CheckSharedBundleLabelInfo(std::unordered_map<std::string, InnerBundleInfo> &infos); 420 /** 421 * @brief To check native file in all haps. 422 * @param infos .Indicates all innerBundleInfo for all haps need to be installed. 423 * @return Returns ERR_OK if haps checking successfully; returns error code otherwise. 424 */ 425 ErrCode CheckMultiNativeFile( 426 std::unordered_map<std::string, InnerBundleInfo> &infos); 427 /** 428 * @brief To roll back when the installation is failed. 429 * @param infos .Indicates the innerBundleInfo needs to be roll back. 430 * @param oldInfo Indicates the original innerBundleInfo of the bundle. 431 * @return Returns ERR_OK if roll back successfully; returns error code otherwise. 432 */ 433 void RollBack(const InnerBundleInfo &info, InnerBundleInfo &oldInfo); 434 /** 435 * @brief To check the version code and bundleName in all haps. 436 * @param newInfos .Indicates all innerBundleInfo for all haps need to be rolled back. 437 * @param oldInfo Indicates the original innerBundleInfo of the bundle. 438 * @return Returns ERR_OK if roll back successfully; returns error code otherwise. 439 */ 440 void RollBack(const std::unordered_map<std::string, InnerBundleInfo> &newInfos, InnerBundleInfo &oldInfo); 441 /** 442 * @brief To remove innerBundleInfo or moduleInfo of the corresponding haps. 443 * @param bundleName Indicates the bundle name of the bundle which needs to be removed innerBundleInfo or 444 * moudleInfo. 445 * @param packageName Indicates the package name of the hap which needs to be removed the moduleInfo. 446 * @return Returns ERR_OK if the removing is successful; returns error code otherwise. 447 */ 448 void RemoveInfo(const std::string &bundleName, const std::string &packageName); 449 /** 450 * @brief To roll back the moduleInfo of the corresponding haps. 451 * @param bundleName Indicates the bundle name of the bundle which needs to be rolled back the moudleInfo. 452 * @param oldInfo Indicates the original innerBundleInfo of the bundle. 453 * @return Returns ERR_OK if the rollback is successful; returns error code otherwise. 454 */ 455 void RollBackModuleInfo(const std::string &bundleName, InnerBundleInfo &oldInfo); 456 /** 457 * @brief To obtain the innerBundleInfo of the corresponding hap. 458 * @param info Indicates the innerBundleInfo obtained. 459 * @param isAppExist Indicates if the innerBundleInfo is existed or not. 460 * @return Returns ERR_OK if the innerBundleInfo is obtained successfully; returns error code otherwise. 461 */ 462 bool GetInnerBundleInfo(InnerBundleInfo &info, bool &isAppExist); 463 /** 464 * @brief To check whether the version code is compatible for application or not. 465 * @param oldInfo Indicates the original innerBundleInfo of the bundle. 466 * @return Returns ERR_OK if version code is checked successfully; returns error code otherwise. 467 */ 468 ErrCode CheckVersionCompatibility(const InnerBundleInfo &oldInfo); 469 /** 470 * @brief To check whether the version code is compatible for application or not. 471 * @param oldInfo Indicates the original innerBundleInfo of the bundle. 472 * @return Returns ERR_OK if version code is checked successfully; returns error code otherwise. 473 */ 474 ErrCode CheckVersionCompatibilityForApplication(const InnerBundleInfo &oldInfo); 475 /** 476 * @brief To check whether the version code is compatible for openharmony service or not. 477 * @param info Indicates the original innerBundleInfo of the bundle. 478 * @return Returns ERR_OK if version code is checked successfully; returns error code otherwise. 479 */ 480 ErrCode CheckVersionCompatibilityForHmService(const InnerBundleInfo &oldInfo); 481 /** 482 * @brief To uninstall lower version feature haps. 483 * @param info Indicates the innerBundleInfo of the bundle. 484 * @param packageVec Indicates the array of package names of the high version entry or feature hap. 485 * @return Returns ERR_OK if uninstall successfully; returns error code otherwise. 486 */ 487 ErrCode UninstallLowerVersionFeature(const std::vector<std::string> &packageVec, bool killProcess = false); 488 /** 489 * @brief To get userId. 490 * @param installParam Indicates the installParam of the bundle. 491 * @return Returns userId. 492 */ 493 int32_t GetUserId(const int32_t &userId) const; 494 /** 495 * @brief Remove bundle user data. 496 * @param innerBundleInfo Indicates the innerBundleInfo of the bundle. 497 * @param needRemoveData Indicates need remove data or not. 498 * @return Returns BundleUserMgr. 499 */ 500 ErrCode RemoveBundleUserData(InnerBundleInfo &innerBundleInfo, bool needRemoveData = true); 501 /** 502 * @brief Create bundle user data. 503 * @param innerBundleInfo Indicates the bundle type of the application. 504 * @return Returns ERR_OK if result is ok; returns error code otherwise. 505 */ 506 ErrCode CreateBundleUserData(InnerBundleInfo &innerBundleInfo); 507 void AddBundleStatus(const NotifyBundleEvents &installRes); 508 ErrCode CheckInstallationFree(const InnerBundleInfo &innerBundleInfo, 509 const std::unordered_map<std::string, InnerBundleInfo> &infos) const; 510 511 bool UninstallAppControl(const std::string &appId, int32_t userId); 512 513 ErrCode InstallNormalAppControl(const std::string &installAppId, int32_t userId, bool isPreInstallApp = false); 514 515 ErrCode CreateBundleCodeDir(InnerBundleInfo &info) const; 516 ErrCode CreateBundleDataDir(InnerBundleInfo &info) const; 517 ErrCode RemoveBundleCodeDir(const InnerBundleInfo &info) const; 518 ErrCode RemoveBundleDataDir(const InnerBundleInfo &info, bool forException = false); 519 void RemoveEmptyDirs(const std::unordered_map<std::string, InnerBundleInfo> &infos) const; 520 std::string GetModuleNames(const std::unordered_map<std::string, InnerBundleInfo> &infos) const; 521 ErrCode UpdateHapToken(bool needUpdate, InnerBundleInfo &newInfo); 522 ErrCode SetDirApl(const InnerBundleInfo &info); 523 ErrCode SetDirApl( 524 int32_t userId, const std::string &bundleName, const std::string &CloneBundleName, 525 const std::string &appPrivilegeLevel, bool isPreInstallApp, const std::string &appProvisionType); 526 /** 527 * @brief Check to set isRemovable true when install. 528 * @param newInfos Indicates all innerBundleInfo for all haps need to be installed. 529 * @param oldInfo Indicates the original innerBundleInfo of the bundle. 530 * @param userId Indicates the userId. 531 * @param isFreeInstallFlag Indicates whether is FREE_INSTALL flag. 532 * @param isAppExist Indicates whether app is exist. 533 * @return 534 */ 535 void CheckEnableRemovable(std::unordered_map<std::string, InnerBundleInfo> &newInfos, 536 InnerBundleInfo &oldInfo, int32_t &userId, bool isFreeInstallFlag, bool isAppExist); 537 /** 538 * @brief Save oldInfo isRemovable to newInfo isRemovable. 539 * @param newModuleInfo Indicates the old innerModuleInfo of the bundle.. 540 * @param oldInfo Indicates the old innerBundleInfo of the bundle. 541 * @param existModule Indicates whether module is exist. 542 * @return 543 */ 544 void SaveOldRemovableInfo(InnerModuleInfo &newModuleInfo, InnerBundleInfo &oldInfo, bool existModule); 545 /** 546 * @brief Save hap path to records. 547 * @param isPreInstallApp Indicates isPreInstallApp or not. 548 * @param infos Indicates all innerBundleInfo for all haps need to be installed. 549 * @return 550 */ 551 void SaveHapPathToRecords( 552 bool isPreInstallApp, const std::unordered_map<std::string, InnerBundleInfo> &infos); 553 void OnSingletonChange(bool killProcess); 554 bool AllowSingletonChange(const std::string &bundleName); 555 void MarkPreInstallState(const std::string &bundleName, bool isUninstalled); 556 ErrCode UninstallAllSandboxApps(const std::string &bundleName, int32_t userId = Constants::INVALID_USERID); 557 ErrCode CheckAppLabel(const InnerBundleInfo &oldInfo, const InnerBundleInfo &newInfo) const; 558 bool CheckReleaseTypeIsCompatible(const InnerBundleInfo &oldInfo, const InnerBundleInfo &newInfo) const; 559 void SendBundleSystemEvent(const std::string &bundleName, BundleEventType bundleEventType, 560 const InstallParam &installParam, InstallScene preBundleScene, ErrCode errCode); 561 ErrCode CheckNativeFileWithOldInfo( 562 const InnerBundleInfo &oldInfo, std::unordered_map<std::string, InnerBundleInfo> &newInfos); 563 bool HasAllOldModuleUpdate( 564 const InnerBundleInfo &oldInfo, std::unordered_map<std::string, InnerBundleInfo> &newInfos); 565 ErrCode CheckArkNativeFileWithOldInfo( 566 const InnerBundleInfo &oldInfo, std::unordered_map<std::string, InnerBundleInfo> &newInfos); 567 ErrCode CheckNativeSoWithOldInfo( 568 const InnerBundleInfo &oldInfo, std::unordered_map<std::string, InnerBundleInfo> &newInfos); 569 ErrCode NotifyBundleStatus(const NotifyBundleEvents &installRes); 570 void AddNotifyBundleEvents(const NotifyBundleEvents ¬ifyBundleEvents); 571 void ProcessHqfInfo(const InnerBundleInfo &oldInfo, const InnerBundleInfo &newInfo) const; 572 ErrCode ProcessDiffFiles(const AppqfInfo &appQfInfo, const std::string &nativeLibraryPath, 573 const std::string &cpuAbi) const; 574 ErrCode ProcessDeployedHqfInfo(const std::string &nativeLibraryPath, 575 const std::string &cpuAbi, const InnerBundleInfo &newInfo, const AppQuickFix &appQuickFix) const; 576 ErrCode ProcessDeployingHqfInfo( 577 const std::string &nativeLibraryPath, const std::string &cpuAbi, const InnerBundleInfo &newInfo) const; 578 ErrCode UpdateLibAttrs(const InnerBundleInfo &newInfo, 579 const std::string &cpuAbi, const std::string &nativeLibraryPath, AppqfInfo &appQfInfo) const; 580 bool CheckHapLibsWithPatchLibs( 581 const std::string &nativeLibraryPath, const std::string &hqfLibraryPath) const; 582 ErrCode ExtractArkNativeFile(InnerBundleInfo &info, const std::string &modulePath); 583 ErrCode DeleteOldArkNativeFile(const InnerBundleInfo &oldInfo); 584 int32_t GetConfirmUserId( 585 const int32_t &userId, std::unordered_map<std::string, InnerBundleInfo> &newInfos); 586 ErrCode CheckUserId(const int32_t &userId) const; 587 ErrCode CreateArkProfile( 588 const std::string &bundleName, int32_t userId, int32_t uid, int32_t gid) const; 589 ErrCode DeleteArkProfile(const std::string &bundleName, int32_t userId) const; 590 ErrCode ExtractArkProfileFile(const std::string &modulePath, const std::string &bundleName, 591 int32_t userId) const; 592 ErrCode ExtractAllArkProfileFile(const InnerBundleInfo &oldInfo, bool checkRepeat = false) const; 593 ErrCode CopyPgoFileToArkProfileDir(const std::string &moduleName, const std::string &modulePath, 594 const std::string &bundleName, int32_t userId) const; 595 ErrCode CopyPgoFile(const std::string &moduleName, const std::string &pgoPath, 596 const std::string &bundleName, int32_t userId) const; 597 ErrCode CheckOverlayInstallation(std::unordered_map<std::string, InnerBundleInfo> &newInfos, int32_t userId); 598 ErrCode CheckOverlayUpdate(const InnerBundleInfo &oldInfo, const InnerBundleInfo &newInfo, int32_t userId) const; 599 NotifyType GetNotifyType(); 600 void KillRelatedProcessIfArkWeb(const std::string &bundleName, bool isAppExist, bool isOta); 601 ErrCode CheckAppService( 602 const InnerBundleInfo &newInfo, const InnerBundleInfo &oldInfo, bool isAppExist); 603 ErrCode CheckSingleton(const InnerBundleInfo &newInfo, const int32_t userId); 604 void GetCallingEventInfo(EventInfo &eventInfo); 605 void GetInstallEventInfo(EventInfo &eventInfo); 606 void GetInstallEventInfo(const InnerBundleInfo &bundleInfo, EventInfo &eventInfo); 607 ErrCode CheckArkProfileDir(const InnerBundleInfo &newInfo, const InnerBundleInfo &oldInfo) const; 608 ErrCode ProcessAsanDirectory(InnerBundleInfo &info) const; 609 ErrCode CleanAsanDirectory(InnerBundleInfo &info) const; 610 void AddAppProvisionInfo(const std::string &bundleName, 611 const Security::Verify::ProvisionInfo &provisionInfo, const InstallParam &installParam) const; 612 ErrCode UninstallHspBundle(std::string &uninstallDir, const std::string &bundleName); 613 ErrCode UninstallHspVersion(std::string &uninstallDir, int32_t versionCode, InnerBundleInfo &info); 614 ErrCode CheckProxyDatas(const std::unordered_map<std::string, InnerBundleInfo> &newInfos); 615 bool CheckDuplicateProxyData(const std::unordered_map<std::string, InnerBundleInfo> &newInfos); 616 bool CheckDuplicateProxyData(const InnerBundleInfo &newInfo, const InnerBundleInfo &oldInfo); 617 bool CheckDuplicateProxyData(const std::vector<ProxyData> &proxyDatas); 618 bool CheckApiInfo(const std::unordered_map<std::string, InnerBundleInfo> &infos); 619 ErrCode InnerProcessNativeLibs(InnerBundleInfo &info, const std::string &modulePath); 620 ErrCode CheckSoEncryption(InnerBundleInfo &info, const std::string &cpuAbi, const std::string &targetSoPath); 621 bool ExtractSoFiles(const std::string &soPath, const std::string &cpuAbi) const; 622 void ProcessOldNativeLibraryPath(const std::unordered_map<std::string, InnerBundleInfo> &newInfos, 623 uint32_t oldVersionCode, const std::string &oldNativeLibraryPath) const; 624 void ProcessAOT(bool isOTA, const std::unordered_map<std::string, InnerBundleInfo> &infos) const; 625 void RemoveOldHapIfOTA(const InstallParam &installParam, 626 const std::unordered_map<std::string, InnerBundleInfo> &newInfos, const InnerBundleInfo &oldInfo) const; 627 ErrCode CopyHapsToSecurityDir(const InstallParam &installParam, std::vector<std::string> &bundlePaths); 628 ErrCode RenameAllTempDir(const std::unordered_map<std::string, InnerBundleInfo> &newInfos) const; 629 ErrCode FindSignatureFileDir(const std::string &moduleName, std::string &signatureFileDir); 630 ErrCode MoveFileToRealInstallationDir(const std::unordered_map<std::string, InnerBundleInfo> &infos); 631 std::string GetTempHapPath(const InnerBundleInfo &info); 632 ErrCode SaveHapToInstallPath(const std::unordered_map<std::string, InnerBundleInfo> &infos); 633 ErrCode CheckHapEncryption(const std::unordered_map<std::string, InnerBundleInfo> &infos); 634 void UpdateAppInstallControlled(int32_t userId); 635 ErrCode MoveSoFileToRealInstallationDir(const std::unordered_map<std::string, InnerBundleInfo> &infos); 636 void ProcessDataGroupInfo(const std::vector<std::string> &bundlePaths, 637 std::unordered_map<std::string, InnerBundleInfo> &infos, 638 int32_t userId, const std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes); 639 ErrCode GetGroupDirsChange(const InnerBundleInfo &info, const InnerBundleInfo &oldInfo, bool oldInfoExisted); 640 ErrCode GetRemoveDataGroupDirs(const InnerBundleInfo &oldInfo, const InnerBundleInfo &newInfo); 641 ErrCode RemoveOldGroupDirs() const; 642 ErrCode CreateGroupDirs() const; 643 void CreateDataGroupDir(InnerBundleInfo &info) const; 644 ErrCode GetDataGroupCreateInfos(const InnerBundleInfo &newInfo); 645 ErrCode RemoveDataGroupDirs(const std::string &bundleName, int32_t userId, bool isKeepData = false) const; 646 void DeleteGroupDirsForException() const; 647 ErrCode CreateDataGroupDirs( 648 const std::unordered_map<std::string, InnerBundleInfo> &newInfos, const InnerBundleInfo &oldInfo); 649 bool NeedDeleteOldNativeLib( 650 std::unordered_map<std::string, InnerBundleInfo> &newInfos, 651 const InnerBundleInfo &oldInfo); 652 ErrCode UninstallBundleFromBmsExtension(const std::string &bundleName); 653 ErrCode CheckBundleInBmsExtension(const std::string &bundleName, int32_t userId); 654 ErrCode CheckMDMUpdateBundleForSelf(const InstallParam &installParam, InnerBundleInfo &oldInfo, 655 const std::unordered_map<std::string, InnerBundleInfo> &newInfos, bool isAppExist); 656 void ExtractResourceFiles(const InnerBundleInfo &info, const std::string &targetPath) const; 657 void RemoveTempSoDir(const std::string &tempSoDir); 658 bool CheckAppIdentifier(InnerBundleInfo &oldInfo, InnerBundleInfo &newInfo); 659 ErrCode InstallEntryMoudleFirst(std::unordered_map<std::string, InnerBundleInfo> &newInfos, 660 InnerBundleInfo &bundleInfo, const InnerBundleUserInfo &innerBundleUserInfo, const InstallParam &installParam); 661 void ProcessQuickFixWhenInstallNewModule(const InstallParam &installParam, 662 const std::unordered_map<std::string, InnerBundleInfo> &newInfos); 663 bool ExtractEncryptedSoFiles(const InnerBundleInfo &info, const std::string &tmpSoPath, int32_t uid) const; 664 ErrCode VerifyCodeSignatureForNativeFiles(InnerBundleInfo &info, const std::string &cpuAbi, 665 const std::string &targetSoPath, const std::string &signatureFileDir) const; 666 ErrCode VerifyCodeSignatureForHap(const std::unordered_map<std::string, InnerBundleInfo> &infos, 667 const std::string &srcHapPath, const std::string &realHapPath); 668 ErrCode DeliveryProfileToCodeSign() const; 669 ErrCode RemoveProfileFromCodeSign(const std::string &bundleName) const; 670 ErrCode ExtractResFileDir(const std::string &modulePath) const; 671 ErrCode ExtractHnpFileDir(const std::string &cpuAbi, const std::string &hnpPackageInfoString, 672 const std::string &modulePath) const; 673 void DeleteOldNativeLibraryPath() const; 674 void RemoveTempPathOnlyUsedForSo(const InnerBundleInfo &innerBundleInfo) const; 675 void GenerateOdid(std::unordered_map<std::string, InnerBundleInfo> &infos, 676 const std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes) const; 677 void SetAppDistributionType(const std::unordered_map<std::string, InnerBundleInfo> &infos); 678 ErrCode CreateShaderCache(const std::string &bundleName, int32_t uid, int32_t gid) const; 679 ErrCode DeleteShaderCache(const std::string &bundleName) const; 680 ErrCode CleanShaderCache(const std::string &bundleName) const; 681 void CreateCloudShader(const std::string &bundleName, int32_t uid, int32_t gid) const; 682 bool VerifyActivationLock() const; 683 std::vector<std::string> GenerateScreenLockProtectionDir(const std::string &bundleName) const; 684 void CreateScreenLockProtectionDir(); 685 void DeleteScreenLockProtectionDir(const std::string bundleName) const; 686 bool SetEncryptionDirPolicy(InnerBundleInfo &info); 687 void DeleteEncryptionKeyId(const InnerBundleInfo &oldInfo) const; 688 void CreateScreenLockProtectionExistDirs(const InnerBundleInfo &info, const std::string &dir); 689 #ifdef APP_DOMAIN_VERIFY_ENABLED 690 void PrepareSkillUri(const std::vector<Skill> &skills, std::vector<AppDomainVerify::SkillUri> &skillUris) const; 691 #endif 692 void PrepareBundleDirQuota(const std::string &bundleName, const int32_t uid, 693 const std::string &bundleDataDirPath, const int32_t limitSize) const; 694 void VerifyDomain(); 695 void GetUninstallBundleInfo(bool isKeepData, int32_t userId, 696 const InnerBundleInfo &oldInfo, UninstallBundleInfo &uninstallBundleInfo); 697 bool CheckInstallOnKeepData(const std::string &bundleName, bool isOTA, 698 const std::unordered_map<std::string, InnerBundleInfo> &infos); 699 void SaveUninstallBundleInfo(const std::string bundleName, bool isKeepData, 700 const UninstallBundleInfo &uninstallBundleInfo); 701 void DeleteUninstallBundleInfo(const std::string &bundleName); 702 void ClearDomainVerifyStatus(const std::string &appIdentifier, const std::string &bundleName) const; 703 bool IsRdDevice() const; 704 void SetAtomicServiceModuleUpgrade(const InnerBundleInfo &oldInfo); 705 void UpdateExtensionSandboxInfo(std::unordered_map<std::string, InnerBundleInfo> &newInfos, 706 const std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes); 707 void GetValidDataGroupIds(const std::vector<std::string> &extensionDataGroupIds, 708 const std::vector<std::string> &bundleDataGroupIds, std::vector<std::string> &validGroupIds) const; 709 void GetExtensionDirsChange(std::unordered_map<std::string, InnerBundleInfo> &newInfos, 710 const InnerBundleInfo &oldInfo); 711 void GetCreateExtensionDirs(std::unordered_map<std::string, InnerBundleInfo> &newInfos); 712 void GetRemoveExtensionDirs( 713 std::unordered_map<std::string, InnerBundleInfo> &newInfos, const InnerBundleInfo &oldInfo); 714 void CreateExtensionDataDir(InnerBundleInfo &info) const; 715 void RemoveCreatedExtensionDirsForException() const; 716 void RemoveOldExtensionDirs() const; 717 ErrCode InnerProcessUpdateHapToken(const bool isOldSystemApp); 718 bool InitDataMgr(); 719 std::string GetInstallSource(const InstallParam &installParam) const; 720 void SetInstallSourceToAppInfo(std::unordered_map<std::string, InnerBundleInfo> &infos, 721 const InstallParam &installParam) const; 722 void SetApplicationFlagsForPreinstallSource(std::unordered_map<std::string, InnerBundleInfo> &infos, 723 const InstallParam &installParam) const; 724 bool IsAppInBlocklist(const std::string &bundleName, const int32_t userId) const; 725 bool CheckWhetherCanBeUninstalled(const std::string &bundleName) const; 726 void CheckBundleNameAndStratAbility(const std::string &bundleName, const std::string &appIdentifier) const; 727 void MarkInstallFinish(); 728 729 bool SetDisposedRuleWhenBundleUpdateStart(const std::unordered_map<std::string, InnerBundleInfo> &infos, 730 const InnerBundleInfo &oldBundleInfo, bool isPreInstallApp); 731 732 bool DeleteDisposedRuleWhenBundleUpdateEnd(const InnerBundleInfo &oldBundleInfo); 733 bool IsArkWeb(const std::string &bundleName) const; 734 #ifdef WEBVIEW_ENABLE 735 ErrCode VerifyArkWebInstall(const std::string &bundleName); 736 #endif 737 738 InstallerState state_ = InstallerState::INSTALL_START; 739 std::shared_ptr<BundleDataMgr> dataMgr_ = nullptr; // this pointer will get when public functions called 740 std::string bundleName_; 741 std::string moduleTmpDir_; 742 std::string modulePath_; 743 std::string baseDataPath_; 744 std::string modulePackage_; 745 std::string mainAbility_; 746 // key is package name, value is boolean 747 std::unordered_map<std::string, bool> installedModules_; 748 bool isAppExist_ = false; 749 bool isContainEntry_ = false; 750 uint32_t versionCode_ = 0; 751 uint32_t accessTokenId_ = 0; 752 bool isAppService_ = false; 753 // value is packageName for uninstalling 754 bool isFeatureNeedUninstall_ = false; 755 std::vector<std::string> uninstallModuleVec_; 756 // for quick fix 757 bool needDeleteQuickFixInfo_ = false; 758 759 int32_t userId_ = Constants::INVALID_USERID; 760 bool hasInstalledInUser_ = false; 761 SingletonState singletonState_ = SingletonState::DEFAULT; 762 std::map<std::string, std::string> hapPathRecords_; 763 // used to record system event infos 764 EventInfo sysEventInfo_; 765 std::unique_ptr<BundleInstallChecker> bundleInstallChecker_ = nullptr; 766 int32_t overlayType_ = NON_OVERLAY_TYPE; 767 std::string moduleName_; 768 // utilizing for code-signature 769 std::map<std::string, std::string> verifyCodeParams_; 770 std::vector<std::string> toDeleteTempHapPath_; 771 std::vector<NotifyBundleEvents> bundleEvents_; 772 // key is the temp path of hap or hsp 773 // value is the signature file path 774 std::map<std::string, std::string> signatureFileMap_; 775 std::vector<DataGroupInfo> createGroupDirs_; 776 std::vector<std::string> removeGroupDirs_; 777 std::vector<std::string> bundlePaths_; 778 std::unordered_map<std::string, std::string> signatureFileTmpMap_; 779 std::string uninstallBundleAppId_; 780 bool isModuleUpdate_ = false; 781 BundleType bundleType_ = BundleType::APP; 782 int32_t atomicServiceModuleUpgrade_ = 0; 783 // utilize for install entry firstly from multi-installation 784 bool isEntryInstalled_ = false; 785 std::string entryModuleName_ = ""; 786 std::map<std::string, std::string> pgoParams_; 787 bool isEnterpriseBundle_ = false; 788 std::string appIdentifier_ = ""; 789 // When it is true, it means that the same bundleName and same userId was uninstalled with keepData before 790 bool existBeforeKeepDataApp_ = false; 791 Security::Verify::HapVerifyResult verifyRes_; 792 std::map<std::string, std::string> targetSoPathMap_; 793 bool copyHapToInstallPath_ = false; 794 std::string appDistributionType_; 795 // indicates sandboxd dirs need to create by extension 796 std::vector<std::string> newExtensionDirs_; 797 // indicates sandboxd dirs need to create by extension 798 std::vector<std::string> createExtensionDirs_; 799 // indicates sandboxd dirs need to remove by extension 800 std::vector<std::string> removeExtensionDirs_; 801 bool needSetDisposeRule_ = false; 802 803 DISALLOW_COPY_AND_MOVE(BaseBundleInstaller); 804 805 #define CHECK_RESULT(errcode, errmsg) \ 806 do { \ 807 if ((errcode) != ERR_OK) { \ 808 APP_LOGE(errmsg, errcode); \ 809 return errcode; \ 810 } \ 811 } while (0) 812 813 #define CHECK_RESULT_WITH_ROLLBACK(errcode, errmsg, newInfos, oldInfo) \ 814 do { \ 815 if ((errcode) == ERR_APPEXECFWK_INSTALL_SINGLETON_NOT_SAME || \ 816 (errcode) == ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON) { \ 817 APP_LOGE(errmsg, errcode); \ 818 return errcode; \ 819 } \ 820 \ 821 if ((errcode) != ERR_OK) { \ 822 APP_LOGE(errmsg, errcode); \ 823 RollBack(newInfos, oldInfo); \ 824 return errcode; \ 825 } \ 826 } while (0) 827 }; 828 } // namespace AppExecFwk 829 } // namespace OHOS 830 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BASE_BUNDLE_INSTALLER_H