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