• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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_BUNDLE_INSTALLER_HOST_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_INSTALLER_HOST_H
18 
19 #include <atomic>
20 #include <memory>
21 #include <string>
22 
23 #include "iremote_stub.h"
24 #include "nocopyable.h"
25 
26 #include "bundle_installer_interface.h"
27 #include "bundle_installer_manager.h"
28 #include "bundle_stream_installer_host_impl.h"
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 class BundleInstallerHost : public IRemoteStub<IBundleInstaller> {
33 public:
34     BundleInstallerHost();
35     virtual ~BundleInstallerHost() override;
36 
37     void Init();
38     virtual int OnRemoteRequest(
39         uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
40     /**
41      * @brief Installs an application, the final result will be notified from the statusReceiver object.
42      * @attention Notice that the bundleFilePath should be an absolute path.
43      * @param bundleFilePath Indicates the path for storing the ohos Ability Package (HAP) of the application
44      *                       to install or update.
45      * @param installParam Indicates the install parameters.
46      * @param statusReceiver Indicates the callback object that using for notifing the install result.
47      * @return Returns true if this function is successfully called; returns false otherwise.
48      */
49     virtual bool Install(const std::string &bundleFilePath, const InstallParam &installParam,
50         const sptr<IStatusReceiver> &statusReceiver) override;
51     /**
52      * @brief Installs an application by bundleName, the final result will be notified from the statusReceiver object.
53      * @param bundleName Indicates the bundleName of the application to install.
54      * @param installParam Indicates the install parameters.
55      * @param statusReceiver Indicates the callback object that using for notifing the install result.
56      * @return Returns true if this function is successfully called; returns false otherwise.
57      */
58     virtual bool Recover(const std::string &bundleName, const InstallParam &installParam,
59         const sptr<IStatusReceiver> &statusReceiver) override;
60     /**
61      * @brief Installs multiple haps, the final result will be notified from the statusReceiver object.
62      * @attention Notice that the bundleFilePath should be an string vector of absolute paths.
63      * @param bundleFilePaths Indicates the paths for storing the ohos Ability Packages (HAP) of the application
64      *                       to install or update.
65      * @param installParam Indicates the install parameters.
66      * @param statusReceiver Indicates the callback object that using for notifing the install result.
67      * @return Returns true if this function is successfully called; returns false otherwise.
68      */
69     virtual bool Install(const std::vector<std::string> &bundleFilePaths, const InstallParam &installParam,
70         const sptr<IStatusReceiver> &statusReceiver) override;
71     /**
72      * @brief Uninstalls an application, the result will be notified from the statusReceiver object.
73      * @param bundleName Indicates the bundle name of the application to uninstall.
74      * @param installParam Indicates the uninstall parameters.
75      * @param statusReceiver Indicates the callback object that using for notifing the uninstall result.
76      * @return Returns true if this function is successfully called; returns false otherwise.
77      */
78     virtual bool Uninstall(const std::string &bundleName, const InstallParam &installParam,
79         const sptr<IStatusReceiver> &statusReceiver) override;
80     /**
81      * @brief Uninstalls a module in an application, the result will be notified from the statusReceiver object.
82      * @param bundleName Indicates the bundle name of the module to uninstall.
83      * @param modulePackage Indicates the module package of the module to uninstall.
84      * @param installParam Indicates the uninstall parameters.
85      * @param statusReceiver Indicates the callback object that using for notifing the uninstall result.
86      * @return Returns true if this function is successfully called; returns false otherwise.
87      */
88     virtual bool Uninstall(const std::string &bundleName, const std::string &modulePackage,
89         const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver) override;
90 
91     virtual bool Uninstall(const UninstallParam &uninstallParam,
92         const sptr<IStatusReceiver> &statusReceiver) override;
93     /**
94      * @brief Installs an app by bundleName, only used in preInstall app.
95      * @param bundleName Indicates the bundleName of the application to install.
96      * @param installParam Indicates the install parameters.
97      * @param statusReceiver Indicates the callback object that using for notifing the install result.
98      * @return Returns true if this function is successfully called; returns false otherwise.
99      */
100     virtual bool InstallByBundleName(const std::string &bundleName, const InstallParam &installParam,
101         const sptr<IStatusReceiver> &statusReceiver) override;
102     /**
103      * @brief Install sandbox application.
104      * @param bundleName Indicates the bundle name of the sandbox application to be install.
105      * @param dlpType Indicates type of the sandbox application.
106      * @param userId Indicates the sandbox application will be installed under which user id.
107      * @param appIndex Indicates the appIndex of the sandbox application installed under which user id.
108      * @return Returns ERR_OK if the sandbox application is installed successfully; returns errcode otherwise.
109      */
110     virtual ErrCode InstallSandboxApp(const std::string &bundleName, int32_t dplType, int32_t userId,
111         int32_t &appIndex) override;
112     /**
113      * @brief Uninstall sandbox application.
114      * @param bundleName Indicates the bundle name of the sandbox application to be install.
115      * @param appIndex Indicates application index of the sandbox application.
116      * @param userId Indicates the sandbox application will be uninstall under which user id.
117      * @return Returns ERR_OK if the sandbox application is installed successfully; returns errcode otherwise.
118      */
119     virtual ErrCode UninstallSandboxApp(const std::string &bundleName, int32_t appIndex, int32_t userId) override;
120 
121     /**
122      * @brief Install or update plugin application.
123      * @param hostBundleName Indicates the bundle name of the host application.
124      * @param pluginFilePaths Indicates the paths for storing the HSP of the plugin to install or update.
125      * @param installPluginParam Indicates the install parameters.
126      * @return Returns ERR_OK if the plugin application is installed successfully; returns errcode otherwise.
127      */
128     virtual ErrCode InstallPlugin(const std::string &hostBundleName, const std::vector<std::string> &pluginFilePaths,
129         const InstallPluginParam &installPluginParam) override;
130 
131     /**
132      * @brief uninstall plugin application.
133      * @param hostBundleName Indicates the bundle name of the host application.
134      * @param pluginBundleName Indicates the plugin bundle name to uninstall.
135      * @param installPluginParam Indicates the uninstall parameters.
136      * @return Returns ERR_OK if the plugin application is uninstalled successfully; returns errcode otherwise.
137      */
138     virtual ErrCode UninstallPlugin(const std::string &hostBundleName, const std::string &pluginBundleName,
139         const InstallPluginParam &installPluginParam) override;
140 
141     virtual sptr<IBundleStreamInstaller> CreateStreamInstaller(const InstallParam &installParam,
142         const sptr<IStatusReceiver> &statusReceiver, const std::vector<std::string> &originHapPaths) override;
143     virtual bool DestoryBundleStreamInstaller(uint32_t streamInstallerId) override;
144     virtual ErrCode StreamInstall(const std::vector<std::string> &bundleFilePaths, const InstallParam &installParam,
145         const sptr<IStatusReceiver> &statusReceiver) override;
146     bool UpdateBundleForSelf(const std::vector<std::string> &bundleFilePaths, const InstallParam &installParam,
147         const sptr<IStatusReceiver> &statusReceiver) override;
148     bool UninstallAndRecover(const std::string &bundleName, const InstallParam &installParam,
149         const sptr<IStatusReceiver> &statusReceiver) override;
150     void AddTask(const ThreadPoolTask &task, const std::string &taskName);
151     size_t GetCurTaskNum();
152     int32_t GetThreadsNum();
153 
154     virtual ErrCode InstallCloneApp(const std::string &bundleName, int32_t userId, int32_t& appIndex) override;
155 
156     virtual ErrCode UninstallCloneApp(const std::string &bundleName, int32_t userId, int32_t appIndex,
157                                       const DestroyAppCloneParam &destroyAppCloneParam) override;
158 
159     virtual ErrCode InstallExisted(const std::string &bundleName, int32_t userId) override;
160 private:
161     /**
162      * @brief Handles the Install function called from a IBundleInstaller proxy object.
163      * @param data Indicates the data to be read.
164      * @param reply Indicates the reply to be sent;
165      * @return
166      */
167     void HandleInstallMessage(MessageParcel &data);
168     /**
169      * @brief Handles the Install by bundleName function called from a IBundleInstaller proxy object.
170      * @param data Indicates the data to be read.
171      * @return
172      */
173     void HandleRecoverMessage(MessageParcel &data);
174     /**
175      * @brief Handles the Install multiple haps function called from a IBundleInstaller proxy object.
176      * @param data Indicates the data to be read.
177      * @param reply Indicates the reply to be sent;
178      * @return
179      */
180     void HandleInstallMultipleHapsMessage(MessageParcel &data);
181     /**
182      * @brief Handles the Uninstall bundle function called from a IBundleInstaller proxy object.
183      * @param data Indicates the data to be read.
184      * @param reply Indicates the reply to be sent;
185      * @return
186      */
187     void HandleUninstallMessage(MessageParcel &data);
188     /**
189      * @brief Handles the Uninstall module function called from a IBundleInstaller proxy object.
190      * @param data Indicates the data to be read.
191      * @param reply Indicates the reply to be sent;
192      * @return
193      */
194     void HandleUninstallModuleMessage(MessageParcel &data);
195     /**
196      * @brief Handles the uninstall by input uninstall param.
197      * @param data Indicates the data to be read.
198      * @return Returns true if the application is uninstall successfully; returns false otherwise.
199      */
200     void HandleUninstallByUninstallParam(MessageParcel &data);
201     /**
202      * @brief Handles the InstallSandboxApp function called from a IBundleInstaller proxy object.
203      * @param data Indicates the data to be read.
204      * @param reply Indicates the reply to be sent.
205      * @return Returns true if the sandbox application is installed successfully; returns false otherwise.
206      */
207     void HandleInstallSandboxApp(MessageParcel &data, MessageParcel &reply);
208     /**
209      * @brief Handles the UninstallSandboxApp function called from a IBundleInstaller proxy object.
210      * @param data Indicates the data to be read.
211      * @param reply Indicates the reply to be sent.
212      * @return Returns true if the sandbox application is installed successfully; returns false otherwise.
213      */
214     void HandleUninstallSandboxApp(MessageParcel &data, MessageParcel &reply);
215     /**
216      * @brief Check whether the statusReceiver object is valid.
217      * @param statusReceiver Indicates the IStatusReceiver object.
218      * @return Returns true if the object is valid; returns false otherwise.
219      */
220     bool CheckBundleInstallerManager(const sptr<IStatusReceiver> &statusReceiver) const;
221     /**
222      * @brief Handles the InstallPlugin function called from a IBundleInstaller proxy object.
223      * @param data Indicates the data to be read.
224      * @param reply Indicates the reply to be sent.
225      * @return
226      */
227     void HandleInstallPlugin(MessageParcel &data, MessageParcel &reply);
228 
229     /**
230      * @brief Handles the UninstallPlugin function called from a IBundleInstaller proxy object.
231      * @param data Indicates the data to be read.
232      * @param reply Indicates the reply to be sent.
233      * @return
234      */
235     void HandleUninstallPlugin(MessageParcel &data, MessageParcel &reply);
236 
237     void HandleCreateStreamInstaller(MessageParcel &data, MessageParcel &reply);
238     void HandleDestoryBundleStreamInstaller(MessageParcel &data, MessageParcel &reply);
239     void HandleUninstallAndRecoverMessage(MessageParcel &data);
240 
241     void HandleInstallCloneApp(MessageParcel &data, MessageParcel &reply);
242     void HandleUninstallCloneApp(MessageParcel &data, MessageParcel &reply);
243     void HandleInstallExisted(MessageParcel &data, MessageParcel &reply);
244 private:
245     InstallParam CheckInstallParam(const InstallParam &installParam);
246     bool IsPermissionVaild(const InstallParam &installParam, InstallParam &installParam2);
247     bool CheckUninstallDisposedRule(const std::string &bundleName, int32_t userId, int32_t appIndex, bool isKeepData,
248         const std::string &modulePackage = "");
249     std::atomic<uint32_t> streamInstallerIds_ = 0;
250     std::mutex streamInstallMutex_;
251     std::shared_ptr<BundleInstallerManager> manager_;
252     std::vector<sptr<IBundleStreamInstaller>> streamInstallers_;
253 
254     DISALLOW_COPY_AND_MOVE(BundleInstallerHost);
255 };
256 }  // namespace AppExecFwk
257 }  // namespace OHOS
258 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_INSTALLER_HOST_H