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) override;
137 
138     virtual ErrCode InstallExisted(const std::string &bundleName, int32_t userId) override;
139 private:
140     /**
141      * @brief Handles the Install function called from a IBundleInstaller proxy object.
142      * @param data Indicates the data to be read.
143      * @param reply Indicates the reply to be sent;
144      * @return
145      */
146     void HandleInstallMessage(MessageParcel &data);
147     /**
148      * @brief Handles the Install by bundleName function called from a IBundleInstaller proxy object.
149      * @param data Indicates the data to be read.
150      * @return
151      */
152     void HandleRecoverMessage(MessageParcel &data);
153     /**
154      * @brief Handles the Install multiple haps function called from a IBundleInstaller proxy object.
155      * @param data Indicates the data to be read.
156      * @param reply Indicates the reply to be sent;
157      * @return
158      */
159     void HandleInstallMultipleHapsMessage(MessageParcel &data);
160     /**
161      * @brief Handles the Uninstall bundle function called from a IBundleInstaller proxy object.
162      * @param data Indicates the data to be read.
163      * @param reply Indicates the reply to be sent;
164      * @return
165      */
166     void HandleUninstallMessage(MessageParcel &data);
167     /**
168      * @brief Handles the Uninstall module function called from a IBundleInstaller proxy object.
169      * @param data Indicates the data to be read.
170      * @param reply Indicates the reply to be sent;
171      * @return
172      */
173     void HandleUninstallModuleMessage(MessageParcel &data);
174     /**
175      * @brief Handles the uninstall by input uninstall param.
176      * @param data Indicates the data to be read.
177      * @return Returns true if the application is uninstall successfully; returns false otherwise.
178      */
179     void HandleUninstallByUninstallParam(MessageParcel &data);
180     /**
181      * @brief Handles the InstallSandboxApp function called from a IBundleInstaller proxy object.
182      * @param data Indicates the data to be read.
183      * @param reply Indicates the reply to be sent.
184      * @return Returns true if the sandbox application is installed successfully; returns false otherwise.
185      */
186     void HandleInstallSandboxApp(MessageParcel &data, MessageParcel &reply);
187     /**
188      * @brief Handles the UninstallSandboxApp function called from a IBundleInstaller proxy object.
189      * @param data Indicates the data to be read.
190      * @param reply Indicates the reply to be sent.
191      * @return Returns true if the sandbox application is installed successfully; returns false otherwise.
192      */
193     void HandleUninstallSandboxApp(MessageParcel &data, MessageParcel &reply);
194     /**
195      * @brief Check whether the statusReceiver object is valid.
196      * @param statusReceiver Indicates the IStatusReceiver object.
197      * @return Returns true if the object is valid; returns false otherwise.
198      */
199     bool CheckBundleInstallerManager(const sptr<IStatusReceiver> &statusReceiver) const;
200 
201     void HandleCreateStreamInstaller(MessageParcel &data, MessageParcel &reply);
202     void HandleDestoryBundleStreamInstaller(MessageParcel &data, MessageParcel &reply);
203     void HandleUninstallAndRecoverMessage(MessageParcel &data);
204 
205     void HandleInstallCloneApp(MessageParcel &data, MessageParcel &reply);
206     void HandleUninstallCloneApp(MessageParcel &data, MessageParcel &reply);
207     void HandleInstallExisted(MessageParcel &data, MessageParcel &reply);
208 private:
209     InstallParam CheckInstallParam(const InstallParam &installParam);
210     bool IsPermissionVaild(const InstallParam &installParam, InstallParam &installParam2);
211     std::shared_ptr<BundleInstallerManager> manager_;
212     std::vector<sptr<IBundleStreamInstaller>> streamInstallers_;
213     std::atomic<uint32_t> streamInstallerIds_ = 0;
214     std::mutex streamInstallMutex_;
215 
216     DISALLOW_COPY_AND_MOVE(BundleInstallerHost);
217 };
218 }  // namespace AppExecFwk
219 }  // namespace OHOS
220 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_INSTALLER_HOST_H