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 "app_log_wrapper.h"
19 #include "bundle_constants.h"
20 #include "datetime_ex.h"
21 #include "os_account_manager.h"
22 #include "perf_profile.h"
23 #include "system_ability_definition.h"
24 #include "system_ability_helper.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 const bool REGISTER_RESULT =
29 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<BundleMgrService>::GetInstance().get());
30
BundleMgrService()31 BundleMgrService::BundleMgrService() : SystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, true)
32 {
33 APP_LOGI("instance is created");
34 PerfProfile::GetInstance().SetBmsLoadStartTime(GetTickCount());
35 }
36
~BundleMgrService()37 BundleMgrService::~BundleMgrService()
38 {
39 if (host_ != nullptr) {
40 host_ = nullptr;
41 }
42 if (installer_ != nullptr) {
43 installer_ = nullptr;
44 }
45 if (handler_) {
46 handler_.reset();
47 }
48 if (runner_) {
49 runner_.reset();
50 }
51 if (dataMgr_) {
52 dataMgr_.reset();
53 }
54 if (cloneMgr_) {
55 cloneMgr_.reset();
56 }
57 if (perChangeSub_) {
58 perChangeSub_.reset();
59 }
60 APP_LOGI("instance is destroyed");
61 }
62
OnStart()63 void BundleMgrService::OnStart()
64 {
65 APP_LOGD("start is triggered");
66 if (!Init()) {
67 APP_LOGE("init fail");
68 return;
69 }
70
71 PerfProfile::GetInstance().SetBmsLoadEndTime(GetTickCount());
72 if (!needToScan_) {
73 PerfProfile::GetInstance().Dump();
74 }
75 AddSystemAbilityListener(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
76 AddSystemAbilityListener(DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
77 }
78
AfterRegisterToService()79 void BundleMgrService::AfterRegisterToService()
80 {
81 if (!perChangeSub_) {
82 EventFwk::MatchingSkills matchingSkills;
83 matchingSkills.AddEvent("PERMISSIONS_CHANGED_EVENT");
84 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
85 perChangeSub_ = std::make_shared<BundlePermissionsChangedMonitor>(dataMgr_, subscribeInfo);
86 EventFwk::CommonEventManager::SubscribeCommonEvent(perChangeSub_);
87 }
88 }
89
OnStop()90 void BundleMgrService::OnStop()
91 {
92 APP_LOGI("OnStop is called");
93 SelfClean();
94 if (perChangeSub_) {
95 EventFwk::CommonEventManager::UnSubscribeCommonEvent(perChangeSub_);
96 }
97 }
98
IsServiceReady() const99 bool BundleMgrService::IsServiceReady() const
100 {
101 return ready_;
102 }
103
Init()104 bool BundleMgrService::Init()
105 {
106 if (ready_) {
107 APP_LOGW("init more than one time");
108 return false;
109 }
110
111 if (host_ == nullptr) {
112 host_ = new (std::nothrow) BundleMgrHostImpl();
113 if (!host_) {
114 APP_LOGE("create host instance fail");
115 return false;
116 }
117 }
118
119 APP_LOGI("init begin");
120 if (!runner_) {
121 runner_ = EventRunner::Create(Constants::BMS_SERVICE_NAME);
122 if (!runner_) {
123 APP_LOGE("create runner fail");
124 return false;
125 }
126 }
127 APP_LOGD("create runner success");
128
129 if (!handler_) {
130 handler_ = std::make_shared<BMSEventHandler>(runner_);
131 if (!handler_) {
132 APP_LOGE("create bms event handler fail");
133 return false;
134 }
135 }
136 APP_LOGD("create handler success");
137
138 if (!installer_) {
139 installer_ = new (std::nothrow) BundleInstallerHost();
140 if (!installer_ || !installer_->Init()) {
141 APP_LOGE("init installer fail");
142 return false;
143 }
144 }
145 APP_LOGD("create installer host success");
146
147 if (!dataMgr_) {
148 APP_LOGI("Create BundledataMgr");
149 dataMgr_ = std::make_shared<BundleDataMgr>();
150 if (!dataMgr_) {
151 APP_LOGE("create data manager fail");
152 return false;
153 }
154 }
155 APP_LOGD("create dataManager success");
156
157 if (userMgrHost_ == nullptr) {
158 userMgrHost_ = new (std::nothrow) BundleUserMgrHostImpl();
159 if (!userMgrHost_) {
160 APP_LOGE("create userMgrHost instance fail");
161 return false;
162 }
163 }
164 APP_LOGD("create userMgrHost success");
165
166 if (!(dataMgr_->LoadDataFromPersistentStorage())) {
167 APP_LOGW("load data from persistent storage fail");
168 dataMgr_->AddUserId(Constants::DEFAULT_USERID);
169 handler_->SendEvent(BMSEventHandler::BUNDLE_SCAN_START);
170 needToScan_ = true;
171 } else {
172 APP_LOGD("Reboot start scan");
173 handler_->SendEvent(BMSEventHandler::BUNDLE_REBOOT_SCAN_START);
174 }
175
176 if (!cloneMgr_) {
177 APP_LOGI("Create BundleCloneMgr");
178 cloneMgr_ = std::make_shared<BundleCloneMgr>();
179 }
180 APP_LOGI("create BundleCloneMgr success");
181
182 if (!deviceManager_) {
183 APP_LOGI("Create device manager");
184 deviceManager_ = std::make_shared<BmsDeviceManager>();
185 }
186
187 CheckAllUser();
188 ready_ = true;
189 APP_LOGI("init end success");
190 return true;
191 }
192
GetBundleInstaller() const193 sptr<IBundleInstaller> BundleMgrService::GetBundleInstaller() const
194 {
195 return installer_;
196 }
197
GetDataMgr() const198 const std::shared_ptr<BundleDataMgr> BundleMgrService::GetDataMgr() const
199 {
200 return dataMgr_;
201 }
202
GetCloneMgr() const203 const std::shared_ptr<BundleCloneMgr> BundleMgrService::GetCloneMgr() const
204 {
205 return cloneMgr_;
206 }
207
SelfClean()208 void BundleMgrService::SelfClean()
209 {
210 if (ready_) {
211 ready_ = false;
212 if (registerToService_) {
213 registerToService_ = false;
214 }
215 if (needToScan_) {
216 needToScan_ = false;
217 }
218 }
219 }
220
GetBundleUserMgr() const221 sptr<BundleUserMgrHostImpl> BundleMgrService::GetBundleUserMgr() const
222 {
223 return userMgrHost_;
224 }
225
CheckAllUser()226 void BundleMgrService::CheckAllUser()
227 {
228 if (!dataMgr_) {
229 return;
230 }
231
232 APP_LOGD("Check all user start.");
233 std::set<int32_t> userIds = dataMgr_->GetAllUser();
234 for (auto userId : userIds) {
235 bool isExists = false;
236 if (AccountSA::OsAccountManager::IsOsAccountExists(userId, isExists) != ERR_OK) {
237 APP_LOGE("Failed to query whether the user(%{public}d) exists.", userId);
238 continue;
239 }
240
241 if (!isExists) {
242 userMgrHost_->RemoveUser(userId);
243 }
244 }
245 APP_LOGD("Check all user end");
246 }
247
RegisterService()248 void BundleMgrService::RegisterService()
249 {
250 if (!registerToService_) {
251 if (!SystemAbilityHelper::AddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, host_)) {
252 APP_LOGE("fail to register to system ability manager");
253 return;
254 }
255 APP_LOGI("register to sam success");
256 registerToService_ = true;
257 }
258 AfterRegisterToService();
259 }
260
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)261 void BundleMgrService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
262 {
263 APP_LOGD("OnAddSystemAbility systemAbilityId:%{public}d added!", systemAbilityId);
264 if (deviceManager_) {
265 deviceManager_->OnAddSystemAbility(systemAbilityId, deviceId);
266 }
267 }
268
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)269 void BundleMgrService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
270 {
271 APP_LOGD("OnRemoveSystemAbility systemAbilityId:%{public}d removed!", systemAbilityId);
272 if (deviceManager_) {
273 deviceManager_->OnRemoveSystemAbility(systemAbilityId, deviceId);
274 }
275 }
276 } // namespace AppExecFwk
277 } // namespace OHOS
278