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_MGR_SERVICE_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_MGR_SERVICE_H
18 
19 #include <atomic>
20 #include <memory>
21 
22 #include "aot/charge_idle_listener.h"
23 #include "singleton.h"
24 #include "system_ability.h"
25 
26 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
27 #include "app_control_manager_host_impl.h"
28 #endif
29 #include "bms_param.h"
30 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
31 #include "aging/bundle_aging_mgr.h"
32 #include "bundle_connect_ability_mgr.h"
33 #include "bundle_distributed_manager.h"
34 #endif
35 #include "bundle_constants.h"
36 #include "bundle_data_mgr.h"
37 #include "bundle_installer_host.h"
38 #include "bundle_mgr_host_impl.h"
39 #include "bundle_mgr_service_event_handler.h"
40 #include "bundle_user_mgr_host_impl.h"
41 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
42 #include "default_app_host_impl.h"
43 #endif
44 #include "extend_resource_manager_host_impl.h"
45 #include "hidump_helper.h"
46 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
47 #include "quick_fix_manager_host_impl.h"
48 #endif
49 #include "pre_install_exception_mgr.h"
50 #ifdef BUNDLE_FRAMEWORK_OVERLAY_INSTALLATION
51 #include "bundle_overlay_manager_host_impl.h"
52 #endif
53 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
54 #include "bundle_resource_host_impl.h"
55 #endif
56 #include "verify_manager_host_impl.h"
57 namespace OHOS {
58 namespace AppExecFwk {
59 class BundleMgrService : public SystemAbility {
60     DECLARE_DELAYED_SINGLETON(BundleMgrService);
61     DECLEAR_SYSTEM_ABILITY(BundleMgrService);
62 
63 public:
64     /**
65      * @brief Start the bundle manager service.
66      * @return
67      */
68     virtual void OnStart() override;
69     /**
70      * @brief Stop the bundle manager service.
71      * @return
72      */
73     virtual void OnStop() override;
74     /**
75      * @brief Check whether if the bundle manager service is ready.
76      * @return Returns true if the bundle manager service is ready; returns false otherwise.
77      */
78     bool IsServiceReady() const;
79 
80     const std::shared_ptr<BundleDataMgr> GetDataMgr() const;
81 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
82     const std::shared_ptr<BundleAgingMgr> GetAgingMgr() const;
83     /**
84      * @brief Get a util object for FA  Distribution center
85      * @return Returns the pointer of BundleConnectAbility object.
86      */
87     const std::shared_ptr<BundleConnectAbilityMgr> GetConnectAbility(int32_t userId = Constants::UNSPECIFIED_USERID);
88 
89     const std::shared_ptr<BundleDistributedManager> GetBundleDistributedManager() const;
90 #endif
91     /**
92      * @brief Get a IBundleInstaller object for IPC
93      * @return Returns the pointer of IBundleInstaller object.
94      */
95     sptr<BundleInstallerHost> GetBundleInstaller() const;
96     /**
97      * @brief Get a IBundleUserMgr object for IPC
98      * @return Returns the pointer of IBundleUserMgr object.
99      */
100     sptr<BundleUserMgrHostImpl> GetBundleUserMgr() const;
101     /**
102      * @brief Get a IVerifyManager object for IPC
103      * @return Returns the pointer of IVerifyManager object.
104      */
105     sptr<IVerifyManager> GetVerifyManager() const;
106 
107     sptr<IExtendResourceManager> GetExtendResourceManager() const;
108 
109 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
110     sptr<IDefaultApp> GetDefaultAppProxy() const;
111 #endif
112 
113 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
114     sptr<IAppControlMgr> GetAppControlProxy() const;
115 #endif
116 
117 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
118     sptr<QuickFixManagerHostImpl> GetQuickFixManagerProxy() const;
119 #endif
120 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
121     sptr<IBundleResource> GetBundleResourceProxy() const;
122 #endif
123     /**
124      * @brief Check all user.
125      */
126     void CheckAllUser();
127     /**
128      * @brief register bms sa to samgr
129      */
130     void RegisterService();
131     /**
132      * @brief send bundle scan finished common event
133      */
134     void NotifyBundleScanStatus();
135     /**
136      * @brief Process hidump.
137      * @param args Indicates the args.
138      * @param result Indicates the result.
139      * @return Returns whether the interface is called successfully.
140      */
141     bool Hidump(const std::vector<std::string> &args, std::string& result) const;
142 
143     void RegisterDataMgr(std::shared_ptr<BundleDataMgr> dataMgrImpl);
144 
145     const std::shared_ptr<BmsParam> GetBmsParam() const;
146 
147     const std::shared_ptr<PreInstallExceptionMgr> GetPreInstallExceptionMgr() const;
148 
149 #ifdef BUNDLE_FRAMEWORK_OVERLAY_INSTALLATION
150     sptr<IOverlayManager> GetOverlayManagerProxy() const;
151 #endif
152 
153     bool IsBrokerServiceStarted() const;
154 
155     void RegisterChargeIdleListener();
156 
157 protected:
158     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
159 
160 private:
161     bool Init();
162     void SelfClean();
163 
164     void InitBmsParam();
165     void InitPreInstallExceptionMgr();
166     bool InitBundleMgrHost();
167     bool InitBundleInstaller();
168     void InitBundleDataMgr();
169     bool InitBundleUserMgr();
170     bool InitVerifyManager();
171     bool InitExtendResourceManager();
172     bool InitBundleEventHandler();
173     void InitHidumpHelper();
174     void InitFreeInstall();
175     bool InitDefaultApp();
176     bool InitAppControl();
177     bool InitQuickFixManager();
178     bool InitOverlayManager();
179     void CreateBmsServiceDir();
180     bool InitBundleResourceMgr();
181 
182 private:
183     bool ready_ = false;
184     bool registerToService_ = false;
185     bool notifyBundleScanStatus = false;
186     std::atomic<bool> isBrokerServiceStarted_ = false;
187     std::shared_ptr<BMSEventHandler> handler_;
188     std::shared_ptr<BundleDataMgr> dataMgr_;
189     std::shared_ptr<HidumpHelper> hidumpHelper_;
190     std::shared_ptr<ChargeIdleListener> chargeIdleListener_;
191 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
192     mutable std::mutex bundleConnectMutex_;
193     std::shared_ptr<BundleAgingMgr> agingMgr_;
194     // key is userId
195     std::map<int32_t, std::shared_ptr<BundleConnectAbilityMgr>> connectAbilityMgr_;
196     std::shared_ptr<BundleDistributedManager> bundleDistributedManager_;
197 #endif
198     sptr<BundleMgrHostImpl> host_;
199     sptr<BundleInstallerHost> installer_;
200     sptr<BundleUserMgrHostImpl> userMgrHost_;
201     sptr<IVerifyManager> verifyManager_;
202     sptr<IExtendResourceManager> extendResourceManager_;
203     std::shared_ptr<BmsParam> bmsParam_;
204     std::shared_ptr<PreInstallExceptionMgr> preInstallExceptionMgr_;
205 
206 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
207     sptr<DefaultAppHostImpl> defaultAppHostImpl_;
208 #endif
209 
210 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
211     sptr<AppControlManagerHostImpl> appControlManagerHostImpl_;
212 #endif
213 
214 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
215     sptr<QuickFixManagerHostImpl> quickFixManagerHostImpl_;
216 #endif
217 
218 #ifdef BUNDLE_FRAMEWORK_OVERLAY_INSTALLATION
219     sptr<OverlayManagerHostImpl>  overlayManagerHostImpl_;
220 #endif
221 
222 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
223     sptr<BundleResourceHostImpl> bundleResourceHostImpl_;
224 #endif
225 
226 #define CHECK_INIT_RESULT(result, errmsg)                                         \
227     do {                                                                          \
228         if (!(result)) {                                                          \
229             APP_LOGE(errmsg);                                                     \
230             return result;                                                        \
231         }                                                                         \
232     } while (0)
233 
234     DISALLOW_COPY_AND_MOVE(BundleMgrService);
235 };
236 }  // namespace AppExecFwk
237 }  // namespace OHOS
238 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_MGR_SERVICE_H
239