• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int32_t userId_ = Constants::INVALID_USERID;
62     // the real path or the parent directory of hsp files to be installed.
63     std::string bundleName_;
64     std::string bundleNameWithTime_;
65     std::string signatureFileDir_;
66     std::string nativeLibraryPath_;
67     std::string appIdentifier_;
68     std::string compileSdkType_;
69     std::string cpuAbi_;
70     std::string soPath_;
71     std::unique_ptr<BundleInstallChecker> bundleInstallChecker_ = nullptr;
72     std::vector<std::string> toDeleteTempHspPath_;
73     std::vector<std::string> pluginIds_;
74     // the key is the real path of each hsp file
75     std::unordered_map<std::string, InnerBundleInfo> parsedBundles_;
76     std::unordered_set<int32_t> hasInstalledUsers_;
77     PluginBundleInfo oldPluginInfo_;
78 
79     DISALLOW_COPY_AND_MOVE(PluginInstaller);
80 
81     ErrCode ParseFiles(const std::vector<std::string> &pluginFilePaths, const InstallPluginParam &installPluginParam);
82     ErrCode DeliveryProfileToCodeSign(std::vector<Security::Verify::HapVerifyResult> &hapVerifyResults) const;
83     ErrCode CheckPluginId(const std::string &hostBundleName);
84     ErrCode ProcessPluginInstall(const InnerBundleInfo &hostBundleInfo);
85     ErrCode CheckPluginDir(const std::string &hostBundleName, std::string &pluginDir);
86     ErrCode ExtractPluginBundles(const std::string &bundlePath, InnerBundleInfo &newInfo, const std::string &pluginDir);
87     ErrCode CheckPluginAppLabelInfo();
88     void MergePluginBundleInfo(InnerBundleInfo &pluginBundleInfo);
89     ErrCode SavePluginInfoToStorage(const InnerBundleInfo &pluginInfo, const InnerBundleInfo &hostBundleInfo);
90     void PluginRollBack(const InnerBundleInfo &hostBundleInfo);
91     ErrCode RemovePluginDir(const InnerBundleInfo &hostBundleInfo);
92     ErrCode RemovePluginInfo(const InnerBundleInfo &hostBundleInfo);
93     ErrCode CheckSupportPluginPermission(const InnerBundleInfo &hostBundleInfo);
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 InnerBundleInfo &hostBundleInfo);
116     std::string GetPluginBundleDir();
117 
118 #define CHECK_RESULT(errcode, errmsg)                                              \
119     do {                                                                           \
120         if ((errcode) != ERR_OK) {                                                   \
121             APP_LOGE(errmsg, errcode);                                             \
122             return errcode;                                                        \
123         }                                                                          \
124     } while (0)
125 };
126 } // AppExecFwk
127 } // OHOS
128 
129 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_PLUGIN_INSTALLER_H