1 /*
2  * Copyright (c) 2021-2023 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_MANAGER_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_INSTALLER_MANAGER_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <unordered_map>
23 
24 #include "nocopyable.h"
25 
26 #include "bundle_installer.h"
27 #include "status_receiver_interface.h"
28 #include "thread_pool.h"
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 using ThreadPoolTask = std::function<void()>;
33 class BundleInstallerManager : public std::enable_shared_from_this<BundleInstallerManager> {
34 public:
35     BundleInstallerManager();
36     ~BundleInstallerManager();
37     /**
38      * @brief Create a bundle installer object for installing a bundle.
39      * @param bundleFilePath Indicates the path for storing the HAP of the bundle to install or update.
40      * @param installParam Indicates the install parameters.
41      * @param statusReceiver Indicates the callback object that using for notifing the install result.
42      * @return
43      */
44     void CreateInstallTask(const std::string &bundleFilePath, const InstallParam &installParam,
45         const sptr<IStatusReceiver> &statusReceiver);
46     /**
47      * @brief Create a bundle installer object for installing a bundle by bundleName.
48      * @param bundleName Indicates the bundleName.
49      * @param installParam Indicates the install parameters.
50      * @param statusReceiver Indicates the callback object that using for notifing the install result.
51      * @return
52      */
53     void CreateInstallByBundleNameTask(const std::string &bundleName, const InstallParam &installParam,
54         const sptr<IStatusReceiver> &statusReceiver);
55     /**
56      * @brief Create a bundle installer object for installing a bundle by bundleName.
57      * @param bundleFilePath Indicates the path for storing the HAP of the bundle to install or update.
58      * @param installParam Indicates the install parameters.
59      * @param statusReceiver Indicates the callback object that using for notifing the install result.
60      * @return
61      */
62     void CreateRecoverTask(const std::string &bundleName, const InstallParam &installParam,
63         const sptr<IStatusReceiver> &statusReceiver);
64     /**
65      * @brief Create a bundle installer object for installing multiple haps of a bundle.
66      * @param bundleFilePaths Indicates the paths for storing the HAPs of the bundle to install or update.
67      * @param installParam Indicates the install parameters.
68      * @param statusReceiver Indicates the callback object that using for notifing the install result.
69      * @return
70      */
71     void CreateInstallTask(const std::vector<std::string> &bundleFilePaths, const InstallParam &installParam,
72         const sptr<IStatusReceiver> &statusReceiver);
73     /**
74      * @brief Create a bundle installer object for uninstalling an bundle.
75      * @param bundleName Indicates the bundle name of the application to uninstall.
76      * @param installParam Indicates the uninstall parameters.
77      * @param statusReceiver Indicates the callback object that using for notifing the uninstall result.
78      * @return
79      */
80     void CreateUninstallTask(
81         const std::string &bundleName, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver);
82     /**
83      * @brief Create a bundle installer object for uninstalling a module.
84      * @param bundleName Indicates the bundle name of the module to uninstall.
85      * @param modulePackage Indicates the module package of the module to uninstall.
86      * @param installParam Indicates the uninstall parameters.
87      * @param statusReceiver Indicates the callback object that using for notifing the uninstall result.
88      * @return
89      */
90     void CreateUninstallTask(const std::string &bundleName, const std::string &modulePackage,
91         const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver);
92     /**
93      * @brief Create a bundle installer object for uninstalling a module.
94      * @param uninstallParam Indicates the input of uninstall param.
95      * @param statusReceiver Indicates the callback object that using for notifing the uninstall result.
96      * @return
97      */
98     void CreateUninstallTask(const UninstallParam &uninstallParam, const sptr<IStatusReceiver> &statusReceive);
99 
100     void CreateUninstallAndRecoverTask(const std::string &bundleName, const InstallParam &installParam,
101         const sptr<IStatusReceiver> &statusReceiver);
102 
103     void AddTask(const ThreadPoolTask &task, const std::string &taskName);
104     size_t GetCurTaskNum();
GetThreadsNum()105     int32_t GetThreadsNum()
106     {
107         return THREAD_NUMBER;
108     }
109 
110 private:
111     /**
112      * @brief Create a bundle installer object internal.
113      * @param statusReceiver Indicates the callback object for this installer.
114      * @return Returns a pointers to BundleInstaller object.
115      */
116     std::shared_ptr<BundleInstaller> CreateInstaller(const sptr<IStatusReceiver> &statusReceiver);
117 
118     void DelayStopThreadPool();
119 
120     DISALLOW_COPY_AND_MOVE(BundleInstallerManager);
121 
122     std::shared_ptr<ThreadPool> threadPool_ = nullptr;
123     const int32_t THREAD_NUMBER = std::thread::hardware_concurrency();
124     std::mutex mutex_;
125 };
126 }  // namespace AppExecFwk
127 }  // namespace OHOS
128 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_INSTALLER_MANAGER_H
129