• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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     virtual sptr<IBundleStreamInstaller> CreateStreamInstaller(const InstallParam &installParam,
122         const sptr<IStatusReceiver> &statusReceiver, const std::vector<std::string> &originHapPaths) override;
123     virtual bool DestoryBundleStreamInstaller(uint32_t streamInstallerId) override;
124     virtual ErrCode StreamInstall(const std::vector<std::string> &bundleFilePaths, const InstallParam &installParam,
125         const sptr<IStatusReceiver> &statusReceiver) override;
126     bool UpdateBundleForSelf(const std::vector<std::string> &bundleFilePaths, const InstallParam &installParam,
127         const sptr<IStatusReceiver> &statusReceiver) override;
128     bool UninstallAndRecover(const std::string &bundleName, const InstallParam &installParam,
129         const sptr<IStatusReceiver> &statusReceiver) override;
130     void AddTask(const ThreadPoolTask &task, const std::string &taskName);
131     size_t GetCurTaskNum();
132     int32_t GetThreadsNum();
133 
134     virtual ErrCode InstallCloneApp(const std::string &bundleName, int32_t userId, int32_t& appIndex) override;
135 
136     virtual ErrCode UninstallCloneApp(const std::string &bundleName, int32_t userId, int32_t appIndex,
137                                       const DestroyAppCloneParam &destroyAppCloneParam) override;
138 
139     virtual ErrCode InstallExisted(const std::string &bundleName, int32_t userId) override;
140 private:
141     /**
142      * @brief Handles the Install function called from a IBundleInstaller proxy object.
143      * @param data Indicates the data to be read.
144      * @param reply Indicates the reply to be sent;
145      * @return
146      */
147     void HandleInstallMessage(MessageParcel &data);
148     /**
149      * @brief Handles the Install by bundleName function called from a IBundleInstaller proxy object.
150      * @param data Indicates the data to be read.
151      * @return
152      */
153     void HandleRecoverMessage(MessageParcel &data);
154     /**
155      * @brief Handles the Install multiple haps function called from a IBundleInstaller proxy object.
156      * @param data Indicates the data to be read.
157      * @param reply Indicates the reply to be sent;
158      * @return
159      */
160     void HandleInstallMultipleHapsMessage(MessageParcel &data);
161     /**
162      * @brief Handles the Uninstall bundle 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 HandleUninstallMessage(MessageParcel &data);
168     /**
169      * @brief Handles the Uninstall module function called from a IBundleInstaller proxy object.
170      * @param data Indicates the data to be read.
171      * @param reply Indicates the reply to be sent;
172      * @return
173      */
174     void HandleUninstallModuleMessage(MessageParcel &data);
175     /**
176      * @brief Handles the uninstall by input uninstall param.
177      * @param data Indicates the data to be read.
178      * @return Returns true if the application is uninstall successfully; returns false otherwise.
179      */
180     void HandleUninstallByUninstallParam(MessageParcel &data);
181     /**
182      * @brief Handles the InstallSandboxApp 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 Returns true if the sandbox application is installed successfully; returns false otherwise.
186      */
187     void HandleInstallSandboxApp(MessageParcel &data, MessageParcel &reply);
188     /**
189      * @brief Handles the UninstallSandboxApp 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 Returns true if the sandbox application is installed successfully; returns false otherwise.
193      */
194     void HandleUninstallSandboxApp(MessageParcel &data, MessageParcel &reply);
195     /**
196      * @brief Check whether the statusReceiver object is valid.
197      * @param statusReceiver Indicates the IStatusReceiver object.
198      * @return Returns true if the object is valid; returns false otherwise.
199      */
200     bool CheckBundleInstallerManager(const sptr<IStatusReceiver> &statusReceiver) const;
201 
202     void HandleCreateStreamInstaller(MessageParcel &data, MessageParcel &reply);
203     void HandleDestoryBundleStreamInstaller(MessageParcel &data, MessageParcel &reply);
204     void HandleUninstallAndRecoverMessage(MessageParcel &data);
205 
206     void HandleInstallCloneApp(MessageParcel &data, MessageParcel &reply);
207     void HandleUninstallCloneApp(MessageParcel &data, MessageParcel &reply);
208     void HandleInstallExisted(MessageParcel &data, MessageParcel &reply);
209 private:
210     InstallParam CheckInstallParam(const InstallParam &installParam);
211     bool IsPermissionVaild(const InstallParam &installParam, InstallParam &installParam2);
212     bool CheckUninstallDisposedRule(const std::string &bundleName, int32_t userId, int32_t appIndex, bool isKeepData,
213         const std::string &modulePackage = "");
214     std::shared_ptr<BundleInstallerManager> manager_;
215     std::vector<sptr<IBundleStreamInstaller>> streamInstallers_;
216     std::atomic<uint32_t> streamInstallerIds_ = 0;
217     std::mutex streamInstallMutex_;
218 
219     DISALLOW_COPY_AND_MOVE(BundleInstallerHost);
220 };
221 }  // namespace AppExecFwk
222 }  // namespace OHOS
223 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_INSTALLER_HOST_H