1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_PLUGIN_INSTALLER_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_PLUGIN_INSTALLER_H 18 19 #include <string> 20 #include <unordered_map> 21 #include <vector> 22 23 #include "bundle_install_checker.h" 24 #include "bundle_common_event_mgr.h" 25 #include "event_report.h" 26 #include "inner_bundle_info.h" 27 #include "install_param.h" 28 #include "install_plugin_param.h" 29 #include "nocopyable.h" 30 31 namespace OHOS { 32 namespace AppExecFwk { 33 class PluginInstaller final { 34 public: 35 PluginInstaller(); 36 ~PluginInstaller(); 37 /** 38 * @brief Install or update plugin application. 39 * @param hostBundleName Indicates the bundle name of the host application. 40 * @param pluginFilePaths Indicates the paths for storing the HSP of the plugin to install or update. 41 * @param installPluginParam Indicates the install parameters. 42 * @return Returns ERR_OK if the plugin application is installed successfully; returns errcode otherwise. 43 */ 44 ErrCode InstallPlugin(const std::string &hostBundleName, const std::vector<std::string> &pluginFilePaths, 45 const InstallPluginParam &installPluginParam); 46 47 /** 48 * @brief uninstall plugin application. 49 * @param hostBundleName Indicates the bundle name of the host application. 50 * @param pluginBundleName Indicates the plugin bundle name to uninstall. 51 * @param installPluginParam Indicates the uninstall parameters. 52 * @return Returns ERR_OK if the plugin application is uninstalled successfully; returns errcode otherwise. 53 */ 54 ErrCode UninstallPlugin(const std::string &hostBundleName, const std::string &pluginBundleName, 55 const InstallPluginParam &installPluginParam); 56 57 private: 58 bool isPluginExist_ = false; 59 bool isEnterpriseBundle_ = false; 60 bool isCompressNativeLibs_ = true; 61 bool isDebug_ = false; 62 int32_t userId_ = Constants::INVALID_USERID; 63 // the real path or the parent directory of hsp files to be installed. 64 std::string bundleName_; 65 std::string bundleNameWithTime_; 66 std::string signatureFileDir_; 67 std::string nativeLibraryPath_; 68 std::string appIdentifier_; 69 std::string compileSdkType_; 70 std::string cpuAbi_; 71 std::string soPath_; 72 std::unique_ptr<BundleInstallChecker> bundleInstallChecker_ = nullptr; 73 std::vector<std::string> toDeleteTempHspPath_; 74 std::vector<std::string> pluginIds_; 75 // the key is the real path of each hsp file 76 std::unordered_map<std::string, InnerBundleInfo> parsedBundles_; 77 PluginBundleInfo oldPluginInfo_; 78 std::shared_ptr<BundleDataMgr> dataMgr_ = nullptr; 79 80 DISALLOW_COPY_AND_MOVE(PluginInstaller); 81 82 ErrCode ParseFiles(const std::vector<std::string> &pluginFilePaths, const InstallPluginParam &installPluginParam); 83 ErrCode DeliveryProfileToCodeSign(std::vector<Security::Verify::HapVerifyResult> &hapVerifyResults) const; 84 ErrCode CheckPluginId(const std::string &hostBundleName); 85 ErrCode ProcessPluginInstall(const InnerBundleInfo &hostBundleInfo); 86 ErrCode CreatePluginDir(const std::string &hostBundleName, std::string &pluginDir); 87 ErrCode ExtractPluginBundles(const std::string &bundlePath, InnerBundleInfo &newInfo, const std::string &pluginDir); 88 ErrCode CheckPluginAppLabelInfo(); 89 void MergePluginBundleInfo(InnerBundleInfo &pluginBundleInfo); 90 ErrCode SavePluginInfoToStorage(const InnerBundleInfo &pluginInfo, const InnerBundleInfo &hostBundleInfo); 91 void PluginRollBack(const std::string &hostBundleName); 92 ErrCode RemovePluginDir(const InnerBundleInfo &hostBundleInfo); 93 ErrCode CheckSupportPluginPermission(const std::string &hostBundleName); 94 ErrCode SaveHspToInstallDir(const std::string &bundlePath, const std::string &pluginBundleDir, 95 const std::string &moduleName, InnerBundleInfo &newInfo); 96 void RemoveEmptyDirs(const std::string &pluginDir) const; 97 void RemoveDir(const std::string &dir) const; 98 bool CheckAppIdentifier() const; 99 bool CheckVersionCodeForUpdate() const; 100 ErrCode ProcessPluginUninstall(const InnerBundleInfo &hostBundleInfo); 101 102 ErrCode MkdirIfNotExist(const std::string &dir); 103 ErrCode CopyHspToSecurityDir(std::vector<std::string> &bundlePaths, const InstallPluginParam &installPluginParam); 104 ErrCode ObtainHspFileAndSignatureFilePath(const std::vector<std::string> &inBundlePaths, 105 std::vector<std::string> &bundlePaths, std::string &signatureFilePath); 106 ErrCode ProcessNativeLibrary(const std::string &bundlePath, 107 const std::string &moduleDir, const std::string &moduleName, const std::string &pluginBundleDir, 108 InnerBundleInfo &newInfo); 109 ErrCode VerifyCodeSignatureForNativeFiles(const std::string &bundlePath, const std::string &cpuAbi, 110 const std::string &targetSoPath, const std::string &signatureFileDir, bool isPreInstalledBundle) const; 111 ErrCode VerifyCodeSignatureForHsp(const std::string &hspPath, const std::string &appIdentifier, 112 bool isEnterpriseBundle, bool isCompileSdkOpenHarmony) const; 113 bool ParsePluginId(const std::string &appServiceCapabilities, std::vector<std::string> &pluginIds); 114 void RemoveOldInstallDir(); 115 void UninstallRollBack(const std::string &hostBundleName); 116 bool InitDataMgr(); 117 ErrCode ParseHapPaths(const InstallPluginParam &installPluginParam, 118 const std::vector<std::string> &inBundlePaths, std::vector<std::string> &parsedPaths); 119 void NotifyPluginEvents(const NotifyType &type, int32_t uid); 120 std::string GetModuleNames(); 121 std::string JoinPluginId() const; 122 123 #define CHECK_RESULT(errcode, errmsg) \ 124 do { \ 125 if ((errcode) != ERR_OK) { \ 126 APP_LOGE(errmsg, errcode); \ 127 return errcode; \ 128 } \ 129 } while (0) 130 }; 131 } // AppExecFwk 132 } // OHOS 133 134 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_PLUGIN_INSTALLER_H