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 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 <string> 21 22 #include "nocopyable.h" 23 24 #include "appexecfwk_errors.h" 25 #include "bundle_clone_mgr.h" 26 #include "bundle_data_mgr.h" 27 #include "bundle_verify_mgr.h" 28 #include "inner_bundle_info.h" 29 #include "install_param.h" 30 31 namespace OHOS { 32 namespace AppExecFwk { 33 class BaseBundleInstaller { 34 public: 35 BaseBundleInstaller(); 36 virtual ~BaseBundleInstaller(); 37 38 protected: 39 enum class InstallerState { 40 INSTALL_START, 41 INSTALL_BUNDLE_CHECKED = 5, 42 INSTALL_SYSCAP_CHECKED = 10, 43 INSTALL_SIGNATURE_CHECKED = 15, 44 INSTALL_PARSED = 20, 45 INSTALL_PERMS_REQ = 25, 46 INSTALL_VERSION_AND_BUNDLENAME_CHECKED = 30, 47 INSTALL_CREATDIR = 40, 48 INSTALL_EXTRACTED = 60, 49 INSTALL_INFO_SAVED = 80, 50 INSTALL_RENAMED = 90, 51 INSTALL_SUCCESS = 100, 52 INSTALL_FAILED, 53 }; 54 55 /** 56 * @brief The main function for system and normal bundle install. 57 * @param bundlePath Indicates the path for storing the HAP file of the application 58 * to install or update. 59 * @param installParam Indicates the install parameters. 60 * @param appType Indicates the application type. 61 * @return Returns ERR_OK if the application install successfully; returns error code otherwise. 62 */ 63 ErrCode InstallBundle( 64 const std::string &bundlePath, const InstallParam &installParam, const Constants::AppType appType); 65 /** 66 * @brief The main function for system and normal bundle install. 67 * @param bundlePaths Indicates the paths for storing the HAP file sof the application 68 * to install or update. 69 * @param installParam Indicates the install parameters. 70 * @param appType Indicates the application type. 71 * @return Returns ERR_OK if the application install successfully; returns error code otherwise. 72 */ 73 ErrCode InstallBundle(const std::vector<std::string> &bundlePaths, const InstallParam &installParam, 74 const Constants::AppType appType); 75 /** 76 * @brief The main function for uninstall a bundle. 77 * @param bundleName Indicates the bundle name of the application to uninstall. 78 * @param installParam Indicates the uninstall parameters. 79 * @return Returns ERR_OK if the application uninstall successfully; returns error code otherwise. 80 */ 81 ErrCode UninstallBundle(const std::string &bundleName, const InstallParam &installParam); 82 /** 83 * @brief The main function for uninstall a module in a specific bundle. 84 * @param bundleName Indicates the bundle name of the application to uninstall. 85 * @param modulePackage Indicates the module package of the module to uninstall. 86 * @param installParam Indicates the uninstall parameters. 87 * @return Returns ERR_OK if the application uninstall successfully; returns error code otherwise. 88 */ 89 ErrCode UninstallBundle( 90 const std::string &bundleName, const std::string &modulePackage, const InstallParam &installParam); 91 /** 92 * @brief Update the installer state. 93 * @attention This function changes the base class state only. 94 * @param state Indicates the state to be updated to. 95 * @return 96 */ 97 virtual void UpdateInstallerState(const InstallerState state); 98 /** 99 * @brief Get the installer state. 100 * @return The current state of the installer object. 101 */ GetInstallerState()102 inline InstallerState GetInstallerState() 103 { 104 return state_; 105 } 106 /** 107 * @brief Get the installer state. 108 * @param state Indicates the state to be updated to. 109 * @return 110 */ SetInstallerState(InstallerState state)111 inline void SetInstallerState(InstallerState state) 112 { 113 state_ = state; 114 } 115 /** 116 * @brief The main function for bundle install by bundleName. 117 * @param bundleName Indicates the bundleName of the application to install. 118 * @param installParam Indicates the install parameters. 119 * @return Returns ERR_OK if the application install successfully; returns error code otherwise. 120 */ 121 ErrCode Recover(const std::string &bundleName, const InstallParam &installParam); 122 /** 123 * @brief Reset install properties. 124 */ 125 void ResetInstallProperties(); 126 private: 127 /** 128 * @brief The real procedure for system and normal bundle install. 129 * @param bundlePath Indicates the path for storing the HAP file of the application 130 * to install or update. 131 * @param installParam Indicates the install parameters. 132 * @param appType Indicates the application type. 133 * @param uid Indicates the uid of the application. 134 * @return Returns ERR_OK if the bundle install successfully; returns error code otherwise. 135 */ 136 ErrCode ProcessBundleInstall(const std::vector<std::string> &bundlePaths, const InstallParam &installParam, 137 const Constants::AppType appType, int32_t &uid); 138 139 ErrCode InnerProcessBundleInstall(std::unordered_map<std::string, InnerBundleInfo> &newInfos, 140 InnerBundleInfo &oldInfo, const InstallParam &installParam, int32_t &uid); 141 /** 142 * @brief The real procedure function for uninstall a bundle. 143 * @param bundleName Indicates the bundle name of the application to uninstall. 144 * @param installParam Indicates the uninstall parameters. 145 * @param uid Indicates the uid of the application. 146 * @return Returns ERR_OK if the bundle uninstall successfully; returns error code otherwise. 147 */ 148 ErrCode ProcessBundleUninstall(const std::string &bundleName, const InstallParam &installParam, int32_t &uid); 149 /** 150 * @brief The real procedure for uninstall a module in a specific bundle. 151 * @param bundleName Indicates the bundle name of the application to uninstall. 152 * @param modulePackage Indicates the module package of the module to uninstall. 153 * @param installParam Indicates the uninstall parameters. 154 * @param uid Indicates the uid of the application. 155 * @return Returns ERR_OK if the module uninstall successfully; returns error code otherwise. 156 */ 157 ErrCode ProcessBundleUninstall(const std::string &bundleName, const std::string &modulePackage, 158 const InstallParam &installParam, int32_t &uid); 159 /** 160 * @brief The process of installing a new bundle. 161 * @param info Indicates the InnerBundleInfo parsed from the config.json in the HAP package. 162 * @param uid Indicates the uid of the application. 163 * @return Returns ERR_OK if the new bundle install successfully; returns error code otherwise. 164 */ 165 ErrCode ProcessBundleInstallStatus(InnerBundleInfo &info, int32_t &uid); 166 /** 167 * @brief The process of updating an exist bundle. 168 * @param oldInfo Indicates the exist InnerBundleInfo object get from the database. 169 * @param newInfo Indicates the InnerBundleInfo object parsed from the config.json in the HAP package. 170 * @param isReplace Indicates whether there is the replace flag in the install flag. 171 * @return Returns ERR_OK if the bundle updating successfully; returns error code otherwise. 172 */ 173 ErrCode ProcessBundleUpdateStatus(InnerBundleInfo &oldInfo, 174 InnerBundleInfo &newInfo, bool isReplace, bool noSkipsKill = true); 175 /** 176 * @brief Remove a whole bundle. 177 * @param info Indicates the InnerBundleInfo object of a bundle. 178 * @return Returns ERR_OK if the bundle removed successfully; returns error code otherwise. 179 */ 180 ErrCode RemoveBundle(InnerBundleInfo &info); 181 /** 182 * @brief Create the code and data directories of a bundle. 183 * @param info Indicates the InnerBundleInfo object of a bundle. 184 * @return Returns ERR_OK if the bundle directories created successfully; returns error code otherwise. 185 */ 186 ErrCode CreateBundleAndDataDir(InnerBundleInfo &info) const; 187 /** 188 * @brief Extract the code to temporilay directory and rename it. 189 * @param info Indicates the InnerBundleInfo object of a bundle. 190 * @param modulePath normal files decompression path. 191 * @return Returns ERR_OK if the bundle extract and renamed successfully; returns error code otherwise. 192 */ 193 ErrCode ExtractModule(InnerBundleInfo &info, const std::string &modulePath); 194 /** 195 * @brief Remove the code and data directories of a bundle. 196 * @param info Indicates the InnerBundleInfo object of a bundle. 197 * @param isUninstall Indicates that whether the remove is in an uninstall process. 198 * @return Returns ERR_OK if the bundle directories removed successfully; returns error code otherwise. 199 */ 200 ErrCode RemoveBundleAndDataDir(const InnerBundleInfo &info, bool isUninstall) const; 201 /** 202 * @brief Remove the code and data directories of a module in a bundle. 203 * @param info Indicates the InnerBundleInfo object of a bundle. 204 * @param modulePackage Indicates the module to be removed. 205 * @param userId Indicates the userId. 206 * @return Returns ERR_OK if the bundle directories removed successfully; returns error code otherwise. 207 */ 208 ErrCode RemoveModuleAndDataDir(const InnerBundleInfo &info, 209 const std::string &modulePackage, int32_t userId = Constants::UNSPECIFIED_USERID) const; 210 /** 211 * @brief Parse the bundle config.json file. 212 * @param bundleFilePath Indicates the HAP file path. 213 * @param InnerBundleInfo Indicates the InnerBundleInfo object of a bundle. 214 * @return Returns ERR_OK if the bundle parsed successfully; returns error code otherwise. 215 */ 216 ErrCode ParseBundleInfo(const std::string &bundleFilePath, InnerBundleInfo &info) const; 217 /** 218 * @brief Remove the current installing module directory. 219 * @param info Indicates the InnerBundleInfo object of a bundle under installing. 220 * @return Returns ERR_OK if the module directory removed successfully; returns error code otherwise. 221 */ 222 ErrCode RemoveModuleDir(const std::string &modulePath) const; 223 /** 224 * @brief Extract files of the current installing module package. 225 * @param info Indicates the InnerBundleInfo object of a bundle under installing. 226 * @param modulePath normal files decompression path. 227 * @param targetSoPath so files decompression path. 228 * @param cpuAbi cpuAbi. 229 * @return Returns ERR_OK if the module files extraced successfully; returns error code otherwise. 230 */ 231 ErrCode ExtractModuleFiles(const InnerBundleInfo &info, const std::string &modulePath, 232 const std::string &targetSoPath, const std::string &cpuAbi); 233 /** 234 * @brief Create the data directories of current installing module package. 235 * @param info Indicates the InnerBundleInfo object of a bundle under installing. 236 * @return Returns ERR_OK if the module directory created successfully; returns error code otherwise. 237 */ 238 ErrCode CreateModuleDataDir(InnerBundleInfo &info) const; 239 /** 240 * @brief Rename the directory of current installing module package. 241 * @param info Indicates the InnerBundleInfo object of a bundle under installing. 242 * @return Returns ERR_OK if the module directory renamed successfully; returns error code otherwise. 243 */ 244 ErrCode RenameModuleDir(const InnerBundleInfo &info) const; 245 /** 246 * @brief Modify the install directory path for different install type. 247 * @param info Indicates the InnerBundleInfo object of a bundle under installing. 248 * @return Returns true if the path set successfully; returns false otherwise. 249 */ 250 ErrCode ModifyInstallDirByHapType(const InstallParam &installParam, const Constants::AppType appType); 251 /** 252 * @brief Update the bundle paths in the InnerBundleInfo object. 253 * @param info Indicates the InnerBundleInfo object of a bundle under installing. 254 * @param baseDataPath Indicates the data file paths. 255 * @return Returns true if the path set successfully; returns false otherwise. 256 */ 257 bool UpdateBundlePaths(InnerBundleInfo &info, const std::string baseDataPath) const; 258 /** 259 * @brief The process of install a new module package. 260 * @param newInfo Indicates the InnerBundleInfo object parsed from the config.json in the HAP package. 261 * @param oldInfo Indicates the exist InnerBundleInfo object get from the database. 262 * @return Returns ERR_OK if the new module install successfully; returns error code otherwise. 263 */ 264 ErrCode ProcessNewModuleInstall(InnerBundleInfo &newInfo, InnerBundleInfo &oldInfo); 265 /** 266 * @brief The process of updating an exist module package. 267 * @param newInfo Indicates the InnerBundleInfo object parsed from the config.json in the HAP package. 268 * @param oldInfo Indicates the exist InnerBundleInfo object get from the database. 269 * @return Returns ERR_OK if the module updating successfully; returns error code otherwise. 270 */ 271 ErrCode ProcessModuleUpdate(InnerBundleInfo &newInfo, 272 InnerBundleInfo &oldInfo, bool isReplace, bool noSkipsKill = true); 273 /** 274 * @brief try to get the bundle info to decide use install or update. 275 * @param newInfo Indicates the InnerBundleInfo object parsed from the config.json in the HAP package. 276 * @param uid Indicates the uid of the application. 277 * @param installFlag Indicates install Flag. 278 * @return Returns ERR_OK if the bundle install successfully; returns error code otherwise. 279 */ 280 ErrCode ProcessBundleStatus(InnerBundleInfo &newInfo, int32_t &uid, const InstallFlag &installFlag); 281 /** 282 * @brief The real procedure for bundle install by bundleName. 283 * @param bundleName Indicates the bundleName the application to install. 284 * @param installParam Indicates the install parameters. 285 * @param uid Indicates the uid of the application. 286 * @return Returns ERR_OK if the bundle install successfully; returns error code otherwise. 287 */ 288 ErrCode ProcessRecover( 289 const std::string &bundleName, const InstallParam &installParam, int32_t &uid); 290 /** 291 * @brief Check syscap. 292 * @param bundlePaths Indicates the file paths of all HAP packages. 293 * @return Returns ERR_OK if the syscap satisfy; returns error code otherwise. 294 */ 295 ErrCode CheckSysCap(const std::vector<std::string> &bundlePaths); 296 /** 297 * @brief Check signature info of multiple haps. 298 * @param bundlePaths Indicates the file paths of all HAP packages. 299 * @param installParam Indicates the install parameters. 300 * @param hapVerifyRes Indicates the signature info. 301 * @return Returns ERR_OK if the every hap has signature info and all haps have same signature info. 302 */ 303 ErrCode CheckMultipleHapsSignInfo(const std::vector<std::string> &bundlePaths, const InstallParam &installParam, 304 std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes) const; 305 /** 306 * @brief To parse hap files and to obtain innerBundleInfo of each hap. 307 * @param bundlePaths Indicates the file paths of all HAP packages. 308 * @param installParam Indicates the install parameters. 309 * @param appType Indicates the app type of the hap. 310 * @param hapVerifyRes Indicates all signature info of all haps. 311 * @param infos Indicates the innerBundleinfo of each hap. 312 * @return Returns ERR_OK if each hap is parsed successfully; returns error code otherwise. 313 */ 314 ErrCode ParseHapFiles(const std::vector<std::string> &bundlePaths, const InstallParam &installParam, 315 const Constants::AppType appType, std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes, 316 std::unordered_map<std::string, InnerBundleInfo> &infos); 317 /** 318 * @brief To check the version code and bundleName in all haps. 319 * @param infos .Indicates all innerBundleInfo for all haps need to be installed. 320 * @return Returns ERR_OK if haps checking successfully; returns error code otherwise. 321 */ 322 ErrCode CheckAppLabelInfo(const std::unordered_map<std::string, InnerBundleInfo> &infos); 323 /** 324 * @brief To roll back when the installation is failed. 325 * @param infos .Indicates the innerBundleInfo needs to be roll back. 326 * @param oldInfo Indicates the original innerBundleInfo of the bundle. 327 * @return Returns ERR_OK if roll back successfully; returns error code otherwise. 328 */ 329 void RollBack(const InnerBundleInfo &info, InnerBundleInfo &oldInfo); 330 /** 331 * @brief To check the version code and bundleName in all haps. 332 * @param newInfos .Indicates all innerBundleInfo for all haps need to be rolled back. 333 * @param oldInfo Indicates the original innerBundleInfo of the bundle. 334 * @return Returns ERR_OK if roll back successfully; returns error code otherwise. 335 */ 336 void RollBack(const std::unordered_map<std::string, InnerBundleInfo> &newInfos, InnerBundleInfo &oldInfo); 337 /** 338 * @brief To remove innerBundleInfo or moduleInfo of the corresponding haps. 339 * @param bundleName Indicates the bundle name of the bundle which needs to be removed innerBundleInfo or 340 * moudleInfo. 341 * @param packageName Indicates the package name of the hap which needs to be removed the moduleInfo. 342 * @return Returns ERR_OK if the removing is successful; returns error code otherwise. 343 */ 344 void RemoveInfo(const std::string &bundleName, const std::string &packageName); 345 /** 346 * @brief To roll back the moduleInfo of the corresponding haps. 347 * @param bundleName Indicates the bundle name of the bundle which needs to be rolled back the moudleInfo. 348 * @param oldInfo Indicates the original innerBundleInfo of the bundle. 349 * @return Returns ERR_OK if the rollback is successful; returns error code otherwise. 350 */ 351 void RollBackMoudleInfo(const std::string &bundleName, InnerBundleInfo &oldInfo); 352 /** 353 * @brief To obtain the innerBundleInfo of the corresponding hap. 354 * @param info Indicates the innerBundleInfo obtained. 355 * @param isAppExist Indicates if the innerBundleInfo is existed or not. 356 * @return Returns ERR_OK if the innerBundleInfo is obtained successfully; returns error code otherwise. 357 */ 358 bool GetInnerBundleInfo(InnerBundleInfo &info, bool &isAppExist); 359 /** 360 * @brief To check whether the version code is compatible or not. 361 * @param oldInfo Indicates the original innerBundleInfo of the bundle. 362 * @param newInfo Indicates the innerBundleInfo of the hap needs to be installed or updated. 363 * @return Returns ERR_OK if version code is checked successfully; returns error code otherwise. 364 */ 365 ErrCode CheckVersionCompatibility(const InnerBundleInfo &oldInfo); 366 /** 367 * @brief To uninstall lower version feature haps. 368 * @param info Indicates the innerBundleInfo of the bundle. 369 * @param packageVec Indicates the array of package names of the high version entry or feature hap. 370 * @return Returns ERR_OK if uninstall successfully; returns error code otherwise. 371 */ 372 ErrCode UninstallLowerVersionFeature(const std::vector<std::string> &packageVec); 373 /** 374 * @brief Check whether the disk path memory is available for installing the hap. 375 * @param bundlePath Indicates the file path. 376 * @param appType Indicates the bundle type of the application. 377 * @return Returns ERR_OK if system memory is adequate; returns error code otherwise. 378 */ 379 ErrCode CheckSystemSize(const std::string &bundlePath, const Constants::AppType appType) const; 380 /** 381 * @brief To get userId. 382 * @param installParam Indicates the installParam of the bundle. 383 * @return Returns userId. 384 */ 385 int32_t GetUserId(const InstallParam& installParam) const; 386 /** 387 * @brief Remove bundle user data. 388 * @param innerBundleInfo Indicates the innerBundleInfo of the bundle. 389 * @return Returns BundleUserMgr. 390 */ 391 ErrCode RemoveBundleUserData(InnerBundleInfo &innerBundleInfo); 392 /** 393 * @brief Create bundle user data. 394 * @param innerBundleInfo Indicates the bundle type of the application. 395 * @param needResetInstallState Indicates need update InstallState or not. 396 * @return Returns ERR_OK if result is ok; returns error code otherwise. 397 */ 398 ErrCode CreateBundleUserData( 399 InnerBundleInfo &innerBundleInfo, bool needResetInstallState = true); 400 /** 401 * @brief Update UserInfo to Db. 402 * @param innerBundleInfo Indicates the bundle type of the application. 403 * @param needResetInstallState Indicates need update InstallState or not. 404 * @return Returns ERR_OK if result is ok; returns error code otherwise. 405 */ 406 ErrCode UpdateUserInfoToDb( 407 InnerBundleInfo &innerBundleInfo, bool needResetInstallState = true); 408 409 bool verifyUriPrefix(const InnerBundleInfo &info, int32_t userId, bool isUpdate = false) const; 410 private: 411 ErrCode CreateBundleCodeDir(InnerBundleInfo &info) const; 412 ErrCode CreateBundleDataDir(InnerBundleInfo &info, bool onlyOneUser = true) const; 413 ErrCode RemoveModuleDataDir(const InnerBundleInfo &info, const std::string &modulePackage) const; 414 ErrCode RemoveHapModuleDataDir(const InnerBundleInfo &info, 415 const std::string &modulePackage, int32_t userId = Constants::UNSPECIFIED_USERID) const; 416 ErrCode RemoveBundleCodeDir(const InnerBundleInfo &info) const; 417 ErrCode RemoveBundleDataDir(const InnerBundleInfo &info) const; 418 uint32_t CreateAccessTokenId(const InnerBundleInfo &info); 419 ErrCode GrantRequestPermissions(const InnerBundleInfo &info, const uint32_t tokenId); 420 ErrCode UpdateDefineAndRequestPermissions(const InnerBundleInfo &oldInfo, const InnerBundleInfo &newInfo); 421 ErrCode SetDirApl(const InnerBundleInfo &info); 422 ErrCode CheckModuleNameForMulitHaps(const std::unordered_map<std::string, InnerBundleInfo> &infos) const; 423 bool IsExistedDistroModule(const InnerBundleInfo &newInfo, const InnerBundleInfo &info) const; 424 bool IsContainModuleName(const InnerBundleInfo &newInfo, const InnerBundleInfo &info) const; 425 426 InstallerState state_ = InstallerState::INSTALL_START; 427 std::shared_ptr<BundleDataMgr> dataMgr_ = nullptr; // this pointer will get when public functions called 428 std::shared_ptr<BundleCloneMgr> cloneMgr_ = nullptr; 429 std::string bundleName_; 430 std::string moduleTmpDir_; 431 std::string modulePath_; 432 std::string baseDataPath_; 433 std::string modulePackage_; 434 std::string mainAbility_; 435 // key is package name, value is boolean 436 std::unordered_map<std::string, bool> installedModules_; 437 bool isAppExist_ = false; 438 bool isContainEntry_ = false; 439 uint32_t versionCode_ = 0; 440 // value is packageName for uninstalling 441 bool isFeatureNeedUninstall_ = false; 442 std::vector<std::string> uninstallModuleVec_; 443 444 int32_t userId_ = Constants::INVALID_USERID; 445 bool hasInstalledInUser_ = false; 446 447 DISALLOW_COPY_AND_MOVE(BaseBundleInstaller); 448 449 #define CHECK_RESULT_WITHOUT_ROLLBACK(errcode, errmsg) \ 450 do { \ 451 if (errcode != ERR_OK) { \ 452 APP_LOGE(errmsg, errcode); \ 453 return errcode; \ 454 } \ 455 } while (0) 456 457 #define CHECK_RESULT_WITH_ROLLBACK(errcode, errmsg, newInfos, oldInfo) \ 458 do { \ 459 if (errcode != ERR_OK) { \ 460 APP_LOGE(errmsg, errcode); \ 461 RollBack(newInfos, oldInfo); \ 462 return errcode; \ 463 } \ 464 } while (0) 465 }; 466 } // namespace AppExecFwk 467 } // namespace OHOS 468 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BASE_BUNDLE_INSTALLER_H