• 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 #include "bundle_mgr_service.h"
17 
18 #include "account_helper.h"
19 #include "app_log_wrapper.h"
20 #include "bundle_constants.h"
21 #include "bundle_distributed_manager.h"
22 #include "bundle_permission_mgr.h"
23 #include "common_event_data.h"
24 #include "common_event_manager.h"
25 #include "common_event_support.h"
26 #include "datetime_ex.h"
27 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
28 #include "app_control_manager_host_impl.h"
29 #endif
30 #include "perf_profile.h"
31 #include "system_ability_definition.h"
32 #include "system_ability_helper.h"
33 #include "want.h"
34 #include "xcollie/watchdog.h"
35 
36 namespace OHOS {
37 namespace AppExecFwk {
38 const bool REGISTER_RESULT =
39     SystemAbility::MakeAndRegisterAbility(DelayedSingleton<BundleMgrService>::GetInstance().get());
40 
BundleMgrService()41 BundleMgrService::BundleMgrService() : SystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, true)
42 {
43     APP_LOGI("instance is created");
44     PerfProfile::GetInstance().SetBmsLoadStartTime(GetTickCount());
45 }
46 
~BundleMgrService()47 BundleMgrService::~BundleMgrService()
48 {
49     host_ = nullptr;
50     installer_ = nullptr;
51     if (handler_) {
52         handler_.reset();
53     }
54     if (runner_) {
55         runner_.reset();
56     }
57     if (dataMgr_) {
58         dataMgr_.reset();
59     }
60 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
61     if (connectAbilityMgr_ != nullptr) {
62         connectAbilityMgr_.reset();
63     }
64 #endif
65     if (hidumpHelper_) {
66         hidumpHelper_.reset();
67     }
68 
69     bmsThreadPool_.Stop();
70 
71     APP_LOGI("instance is destroyed");
72 }
73 
OnStart()74 void BundleMgrService::OnStart()
75 {
76     APP_LOGD("start is triggered");
77     if (!Init()) {
78         APP_LOGE("init fail");
79         return;
80     }
81 
82     AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
83 #ifdef DEVICE_MANAGER_ENABLE
84     AddSystemAbilityListener(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
85     AddSystemAbilityListener(DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
86 #endif
87 }
88 
OnStop()89 void BundleMgrService::OnStop()
90 {
91     APP_LOGI("OnStop is called");
92     SelfClean();
93 }
94 
IsServiceReady() const95 bool BundleMgrService::IsServiceReady() const
96 {
97     return ready_;
98 }
99 
Init()100 bool BundleMgrService::Init()
101 {
102     if (ready_) {
103         APP_LOGW("init more than one time");
104         return false;
105     }
106 
107     APP_LOGI("Init begin");
108     InitThreadPool();
109     InitBmsParam();
110     CHECK_INIT_RESULT(InitBundleMgrHost(), "Init bundleMgr fail");
111     CHECK_INIT_RESULT(InitBundleInstaller(), "Init bundleInstaller fail");
112     InitBundleDataMgr();
113     CHECK_INIT_RESULT(InitBundleUserMgr(), "Init bundleUserMgr fail");
114     CHECK_INIT_RESULT(InitBundleEventHandler(), "Init bundleEventHandler fail");
115     InitDeviceManager();
116     InitHidumpHelper();
117     InitFreeInstall();
118     CHECK_INIT_RESULT(InitDefaultApp(), "Init defaultApp fail");
119     CHECK_INIT_RESULT(InitAppControl(), "Init appControl fail");
120     CHECK_INIT_RESULT(InitQuickFixManager(), "Init quickFixManager fail");
121     ready_ = true;
122     APP_LOGI("Init success");
123     return true;
124 }
125 
InitThreadPool()126 void BundleMgrService::InitThreadPool()
127 {
128     bmsThreadPool_.Start(THREAD_NUMBER);
129     bmsThreadPool_.SetMaxTaskNum(Constants::MAX_TASK_NUMBER);
130 }
131 
InitBmsParam()132 void BundleMgrService::InitBmsParam()
133 {
134     bmsParam_ = std::make_shared<BmsParam>();
135 }
136 
InitBundleMgrHost()137 bool BundleMgrService::InitBundleMgrHost()
138 {
139     if (host_ == nullptr) {
140         host_ = new (std::nothrow) BundleMgrHostImpl();
141     }
142 
143     return host_ != nullptr;
144 }
145 
InitBundleInstaller()146 bool BundleMgrService::InitBundleInstaller()
147 {
148     if (installer_ == nullptr) {
149         installer_ = new (std::nothrow) BundleInstallerHost();
150         if (installer_ == nullptr || !installer_->Init()) {
151             APP_LOGE("init installer fail");
152             return false;
153         }
154     }
155 
156     return true;
157 }
158 
InitBundleDataMgr()159 void BundleMgrService::InitBundleDataMgr()
160 {
161     if (dataMgr_ == nullptr) {
162         APP_LOGI("Create BundledataMgr");
163         dataMgr_ = std::make_shared<BundleDataMgr>();
164         dataMgr_->AddUserId(Constants::DEFAULT_USERID);
165     }
166 }
167 
InitBundleUserMgr()168 bool BundleMgrService::InitBundleUserMgr()
169 {
170     if (userMgrHost_ == nullptr) {
171         userMgrHost_ = new (std::nothrow) BundleUserMgrHostImpl();
172     }
173 
174     return userMgrHost_ != nullptr;
175 }
176 
InitBundleEventHandler()177 bool BundleMgrService::InitBundleEventHandler()
178 {
179     if (runner_ == nullptr) {
180         runner_ = EventRunner::Create(Constants::BMS_SERVICE_NAME);
181         if (runner_ == nullptr) {
182             APP_LOGE("create runner fail");
183             return false;
184         }
185     }
186 
187     if (handler_ == nullptr) {
188         handler_ = std::make_shared<BMSEventHandler>(runner_);
189         int32_t timeout = 10 * 60 * 1000; // 10min
190         if (HiviewDFX::Watchdog::GetInstance().AddThread(Constants::BMS_SERVICE_NAME, handler_, timeout) != 0) {
191             APP_LOGE("watchdog addThread failed");
192         }
193     }
194 
195     handler_->SendEvent(BMSEventHandler::BMS_START);
196     return true;
197 }
198 
InitDeviceManager()199 void BundleMgrService::InitDeviceManager()
200 {
201 #ifdef DEVICE_MANAGER_ENABLE
202     if (deviceManager_ == nullptr) {
203         APP_LOGI("Create device manager");
204         deviceManager_ = std::make_shared<BmsDeviceManager>();
205     }
206 #endif
207 }
208 
InitHidumpHelper()209 void BundleMgrService::InitHidumpHelper()
210 {
211     if (hidumpHelper_ == nullptr) {
212         APP_LOGI("Create hidump helper");
213         hidumpHelper_ = std::make_shared<HidumpHelper>(dataMgr_);
214     }
215 }
216 
InitFreeInstall()217 void BundleMgrService::InitFreeInstall()
218 {
219 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
220     if (agingMgr_ == nullptr) {
221         APP_LOGI("Create aging manager");
222         agingMgr_ = DelayedSingleton<BundleAgingMgr>::GetInstance();
223         if (agingMgr_ != nullptr) {
224             agingMgr_->InitAgingRunner();
225             agingMgr_->InitAgingtTimer();
226         }
227     }
228     if (connectAbilityMgr_ == nullptr) {
229         APP_LOGI("Create BundleConnectAbility");
230         connectAbilityMgr_ = std::make_shared<BundleConnectAbilityMgr>();
231     }
232     if (bundleDistributedManager_ == nullptr) {
233         APP_LOGI("Create bundleDistributedManager");
234         bundleDistributedManager_ = std::make_shared<BundleDistributedManager>();
235     }
236 #endif
237 }
238 
InitDefaultApp()239 bool BundleMgrService::InitDefaultApp()
240 {
241 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
242     if (defaultAppHostImpl_ == nullptr) {
243         defaultAppHostImpl_ = new (std::nothrow) DefaultAppHostImpl();
244         if (defaultAppHostImpl_ == nullptr) {
245             APP_LOGE("create DefaultAppHostImpl failed.");
246             return false;
247         }
248     }
249 #endif
250     return true;
251 }
252 
InitAppControl()253 bool BundleMgrService::InitAppControl()
254 {
255 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
256     if (appControlManagerHostImpl_ == nullptr) {
257         appControlManagerHostImpl_ = new (std::nothrow) AppControlManagerHostImpl();
258         if (appControlManagerHostImpl_ == nullptr) {
259             APP_LOGE("create appControlManagerHostImpl failed.");
260             return false;
261         }
262     }
263 #endif
264     return true;
265 }
266 
InitQuickFixManager()267 bool BundleMgrService::InitQuickFixManager()
268 {
269 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
270     if (quickFixManagerHostImpl_ == nullptr) {
271         quickFixManagerHostImpl_ = new (std::nothrow) QuickFixManagerHostImpl();
272         if (quickFixManagerHostImpl_ == nullptr) {
273             APP_LOGE("create QuickFixManagerHostImpl failed.");
274             return false;
275         }
276     }
277 #endif
278     return true;
279 }
280 
GetBundleInstaller() const281 sptr<IBundleInstaller> BundleMgrService::GetBundleInstaller() const
282 {
283     return installer_;
284 }
285 
RegisterDataMgr(std::shared_ptr<BundleDataMgr> dataMgrImpl)286 void BundleMgrService::RegisterDataMgr(std::shared_ptr<BundleDataMgr> dataMgrImpl)
287 {
288     dataMgr_ = dataMgrImpl;
289     if (dataMgr_ != nullptr) {
290         dataMgr_->AddUserId(Constants::DEFAULT_USERID);
291     }
292 }
293 
GetDataMgr() const294 const std::shared_ptr<BundleDataMgr> BundleMgrService::GetDataMgr() const
295 {
296     return dataMgr_;
297 }
298 
299 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
GetAgingMgr() const300 const std::shared_ptr<BundleAgingMgr> BundleMgrService::GetAgingMgr() const
301 {
302     return agingMgr_;
303 }
304 
GetConnectAbility() const305 const std::shared_ptr<BundleConnectAbilityMgr> BundleMgrService::GetConnectAbility() const
306 {
307     return connectAbilityMgr_;
308 }
309 
GetBundleDistributedManager() const310 const std::shared_ptr<BundleDistributedManager> BundleMgrService::GetBundleDistributedManager() const
311 {
312     return bundleDistributedManager_;
313 }
314 #endif
315 
316 #ifdef DEVICE_MANAGER_ENABLE
GetDeviceManager() const317 const std::shared_ptr<BmsDeviceManager> BundleMgrService::GetDeviceManager() const
318 {
319     return deviceManager_;
320 }
321 #endif
322 
SelfClean()323 void BundleMgrService::SelfClean()
324 {
325     if (ready_) {
326         ready_ = false;
327         if (registerToService_) {
328             registerToService_ = false;
329         }
330     }
331 }
332 
GetBundleUserMgr() const333 sptr<BundleUserMgrHostImpl> BundleMgrService::GetBundleUserMgr() const
334 {
335     return userMgrHost_;
336 }
337 
GetBmsParam() const338 const std::shared_ptr<BmsParam> BundleMgrService::GetBmsParam() const
339 {
340     return bmsParam_;
341 }
342 
343 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
GetDefaultAppProxy() const344 sptr<IDefaultApp> BundleMgrService::GetDefaultAppProxy() const
345 {
346     return defaultAppHostImpl_;
347 }
348 #endif
349 
350 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
GetAppControlProxy() const351 sptr<IAppControlMgr> BundleMgrService::GetAppControlProxy() const
352 {
353     return appControlManagerHostImpl_;
354 }
355 #endif
356 
357 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
GetQuickFixManagerProxy() const358 sptr<QuickFixManagerHostImpl> BundleMgrService::GetQuickFixManagerProxy() const
359 {
360     return quickFixManagerHostImpl_;
361 }
362 #endif
363 
CheckAllUser()364 void BundleMgrService::CheckAllUser()
365 {
366     if (dataMgr_ == nullptr) {
367         return;
368     }
369 
370     APP_LOGD("Check all user start.");
371     std::set<int32_t> userIds = dataMgr_->GetAllUser();
372     for (auto userId : userIds) {
373         bool isExists = false;
374         if (AccountHelper::IsOsAccountExists(userId, isExists) != ERR_OK) {
375             APP_LOGE("Failed to query whether the user(%{public}d) exists.", userId);
376             continue;
377         }
378 
379         if (!isExists) {
380             userMgrHost_->RemoveUser(userId);
381         }
382     }
383     APP_LOGD("Check all user end");
384 }
385 
RegisterService()386 void BundleMgrService::RegisterService()
387 {
388     if (!registerToService_) {
389         if (!SystemAbilityHelper::AddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, host_)) {
390             APP_LOGE("fail to register to system ability manager");
391             return;
392         }
393         APP_LOGI("register to sam success");
394         registerToService_ = true;
395     }
396 
397     PerfProfile::GetInstance().SetBmsLoadEndTime(GetTickCount());
398     PerfProfile::GetInstance().Dump();
399 }
400 
NotifyBundleScanStatus()401 void BundleMgrService::NotifyBundleScanStatus()
402 {
403     APP_LOGD("PublishCommonEvent for bundle scan finished");
404     AAFwk::Want want;
405     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_BUNDLE_SCAN_FINISHED);
406     EventFwk::CommonEventData commonEventData { want };
407     if (!EventFwk::CommonEventManager::PublishCommonEvent(commonEventData)) {
408         notifyBundleScanStatus = true;
409         APP_LOGE("PublishCommonEvent for bundle scan finished failed.");
410     } else {
411         APP_LOGD("PublishCommonEvent for bundle scan finished succeed.");
412     }
413 }
414 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)415 void BundleMgrService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
416 {
417     APP_LOGD("OnAddSystemAbility systemAbilityId:%{public}d added!", systemAbilityId);
418 #ifdef DEVICE_MANAGER_ENABLE
419     if (deviceManager_) {
420         deviceManager_->OnAddSystemAbility(systemAbilityId, deviceId);
421     }
422 #endif
423     if (COMMON_EVENT_SERVICE_ID == systemAbilityId && notifyBundleScanStatus) {
424         NotifyBundleScanStatus();
425     }
426 }
427 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)428 void BundleMgrService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
429 {
430     APP_LOGD("OnRemoveSystemAbility systemAbilityId:%{public}d removed!", systemAbilityId);
431 #ifdef DEVICE_MANAGER_ENABLE
432     if (deviceManager_) {
433         deviceManager_->OnRemoveSystemAbility(systemAbilityId, deviceId);
434     }
435 #endif
436 }
437 
Hidump(const std::vector<std::string> & args,std::string & result) const438 bool BundleMgrService::Hidump(const std::vector<std::string> &args, std::string& result) const
439 {
440     if (hidumpHelper_ && hidumpHelper_->Dump(args, result)) {
441         return true;
442     }
443 
444     APP_LOGD("HidumpHelper failed");
445     return false;
446 }
447 
GetThreadPool()448 ThreadPool &BundleMgrService::GetThreadPool()
449 {
450     return bmsThreadPool_;
451 }
452 }  // namespace AppExecFwk
453 }  // namespace OHOS
454