1 /*
2 * Copyright (c) 2023-2025 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 "drv_bundle_state_callback.h"
17
18 #include <want.h>
19 #include <element_name.h>
20
21 #include "system_ability_definition.h"
22 #include "iservice_registry.h"
23 #include "bundle_constants.h"
24 #include "os_account_manager.h"
25 #include "pkg_db_helper.h"
26
27 #include "hdf_log.h"
28 #include "edm_errors.h"
29 #include "hilog_wrapper.h"
30 #include "hitrace_meter.h"
31 #include "bus_extension_core.h"
32 #include "accesstoken_kit.h"
33 #include <unordered_map>
34 #include <pthread.h>
35 #include <thread>
36 #include "driver_report_sys_event.h"
37 #include "usb_driver_info.h"
38 #include <memory.h>
39 namespace OHOS {
40 namespace ExternalDeviceManager {
41 using namespace std;
42 using namespace OHOS;
43 using namespace OHOS::AAFwk;
44 using namespace OHOS::AppExecFwk;
45 using namespace OHOS::ExternalDeviceManager;
46 using namespace OHOS::Security::AccessToken;
47
48 constexpr uint64_t LABEL = HITRACE_TAG_OHOS;
49 const string DRV_INFO_BUS = "bus";
50 const string DRV_INFO_VENDOR = "vendor";
51 const string DRV_INFO_DESC = "description";
52 const string DRV_INFO_LAUNCHONBIND = "launchonbind";
53 const string DRV_INFO_ALLOW_ACCESSED = "ohos.permission.ACCESS_DDK_ALLOWED";
54
55 static constexpr const char *BUNDLE_RESET_TASK_NAME = "DRIVER_INFO_RESET";
56 static constexpr const char *BUNDLE_UPDATE_TASK_NAME = "DRIVER_INFO_UPDATE";
57 static constexpr const char *GET_DRIVERINFO_TASK_NAME = "GET_DRIVERINFO_ASYNC";
58
GetBundleSize(const std::string & bundleName)59 std::string DrvBundleStateCallback::GetBundleSize(const std::string &bundleName)
60 {
61 std::string bundleSize = "";
62 int32_t userId = GetCurrentActiveUserId();
63 std::vector<int64_t> bundleStats;
64 std::lock_guard<std::mutex> lock(bundleMgrMutex_);
65 if (!GetBundleMgrProxy()) {
66 EDM_LOGE(MODULE_PKG_MGR, "%{public}s: failed to GetBundleMgrProxy", __func__);
67 return bundleSize;
68 }
69 if (bundleMgr_ == nullptr) {
70 EDM_LOGE(MODULE_PKG_MGR, "%{public}s: bundleMgr_ is null", __func__);
71 return bundleSize;
72 }
73 if (!bundleMgr_->GetBundleStats(bundleName, userId, bundleStats)) {
74 EDM_LOGE(MODULE_PKG_MGR, "GetBundleStats failed");
75 return bundleSize;
76 }
77 if (!bundleStats.empty()) {
78 bundleSize.append(std::to_string(bundleStats[0]));
79 }
80 return bundleSize;
81 }
82
ChangeValue(DriverInfo & tmpDrvInfo,const map<string,string> & metadata)83 void DrvBundleStateCallback::ChangeValue(DriverInfo &tmpDrvInfo, const map<string, string> &metadata)
84 {
85 for (auto data : metadata) {
86 if (data.first == DRV_INFO_BUS) {
87 tmpDrvInfo.bus_ = data.second;
88 tmpDrvInfo.busType_ = BusExtensionCore::GetBusTypeByName(tmpDrvInfo.bus_);
89 }
90 if (data.first == DRV_INFO_VENDOR) {
91 tmpDrvInfo.vendor_ = data.second;
92 }
93 if (data.first == DRV_INFO_DESC) {
94 tmpDrvInfo.description_ = data.second;
95 }
96 if (LowerStr(data.first) == DRV_INFO_LAUNCHONBIND) {
97 tmpDrvInfo.launchOnBind_ = (data.second == "true");
98 }
99 if (data.first == DRV_INFO_ALLOW_ACCESSED) {
100 tmpDrvInfo.accessAllowed_ = (data.second == "true");
101 }
102 }
103 }
104
ParseToPkgInfoTables(const std::vector<ExtensionAbilityInfo> & driverInfos,std::vector<PkgInfoTable> & pkgInfoTables)105 std::vector<DriverInfo> DrvBundleStateCallback::ParseToPkgInfoTables(
106 const std::vector<ExtensionAbilityInfo> &driverInfos, std::vector<PkgInfoTable> &pkgInfoTables)
107 {
108 std::unordered_map<std::string, std::string> bundlesSize;
109 shared_ptr<IBusExtension> extInstance = nullptr;
110 std::vector<DriverInfo> ret;
111 for (const auto &driverInfo : driverInfos) {
112 if (driverInfo.type != ExtensionAbilityType::DRIVER || driverInfo.metadata.empty()) {
113 continue;
114 }
115 DriverInfo tmpDrvInfo(driverInfo.bundleName, driverInfo.name);
116 map<string, string> metaMap;
117 for (auto meta : driverInfo.metadata) {
118 metaMap.emplace(meta.name, meta.value);
119 }
120 ChangeValue(tmpDrvInfo, metaMap);
121 extInstance = BusExtensionCore::GetInstance().GetBusExtensionByName(tmpDrvInfo.GetBusName());
122 if (extInstance == nullptr) {
123 EDM_LOGE(MODULE_PKG_MGR, "GetBusExtensionByName failed, bus:%{public}s", tmpDrvInfo.bus_.c_str());
124 continue;
125 }
126 tmpDrvInfo.driverInfoExt_ = extInstance->ParseDriverInfo(metaMap);
127 if (tmpDrvInfo.driverInfoExt_ == nullptr) {
128 EDM_LOGE(MODULE_PKG_MGR, "ParseDriverInfo null");
129 continue;
130 }
131
132 if (bundlesSize.find(driverInfo.bundleName) == bundlesSize.end()) {
133 std::string bundleSize = GetBundleSize(driverInfo.bundleName);
134 bundlesSize.emplace(driverInfo.bundleName, bundleSize);
135 }
136
137 tmpDrvInfo.driverSize_ = bundlesSize[driverInfo.bundleName];
138 tmpDrvInfo.version_ = driverInfo.applicationInfo.versionName;
139 string driverInfoStr;
140 if (tmpDrvInfo.Serialize(driverInfoStr) != EDM_OK) {
141 EDM_LOGE(MODULE_PKG_MGR, "Serialize driverInfo faild");
142 continue;
143 }
144
145 PkgInfoTable pkgInfo = CreatePkgInfoTable(driverInfo, driverInfoStr);
146 tmpDrvInfo.driverUid_ = pkgInfo.driverUid;
147 tmpDrvInfo.userId_ = pkgInfo.userId;
148 pkgInfoTables.emplace_back(pkgInfo);
149 ret.emplace_back(tmpDrvInfo);
150 }
151 return ret;
152 }
153
CreatePkgInfoTable(const ExtensionAbilityInfo & driverInfo,string driverInfoStr)154 PkgInfoTable DrvBundleStateCallback::CreatePkgInfoTable(const ExtensionAbilityInfo &driverInfo, string driverInfoStr)
155 {
156 PkgInfoTable pkgInfo = {
157 .driverUid = driverInfo.name + "-" + std::to_string(driverInfo.applicationInfo.accessTokenId),
158 .bundleAbility = driverInfo.bundleName + "-" + driverInfo.name,
159 .bundleName = driverInfo.bundleName,
160 .driverName = driverInfo.name,
161 .driverInfo = driverInfoStr
162 };
163 HapTokenInfo hapTokenInfo;
164 if (AccessTokenKit::GetHapTokenInfo(driverInfo.applicationInfo.accessTokenId, hapTokenInfo)
165 == AccessTokenKitRet::RET_SUCCESS) {
166 pkgInfo.userId = hapTokenInfo.userID;
167 pkgInfo.appIndex = hapTokenInfo.instIndex;
168 }
169 return pkgInfo;
170 }
171
DrvBundleStateCallback()172 DrvBundleStateCallback::DrvBundleStateCallback()
173 {
174 stiching.clear();
175 stiching += "-";
176 };
177
DrvBundleStateCallback(shared_future<int32_t> bmsFuture,shared_future<int32_t> accountFuture,shared_future<int32_t> commEventFuture)178 DrvBundleStateCallback::DrvBundleStateCallback(shared_future<int32_t> bmsFuture, shared_future<int32_t> accountFuture,
179 shared_future<int32_t> commEventFuture): DrvBundleStateCallback()
180 {
181 bmsFuture_ = bmsFuture;
182 accountFuture_ = accountFuture;
183 commEventFuture_ = commEventFuture;
184 }
185
~DrvBundleStateCallback()186 DrvBundleStateCallback::~DrvBundleStateCallback()
187 {
188 return;
189 };
190
PrintTest()191 void DrvBundleStateCallback::PrintTest()
192 {
193 std::vector<std::string> allBundleAbilityNames;
194 std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
195 int32_t ret = helper->QueryAllSize(allBundleAbilityNames);
196 if (ret <= 0) {
197 EDM_LOGE(MODULE_PKG_MGR, "QueryAllSize failed");
198 return;
199 }
200 cout << "allBundleAbilityNames_ size = " << allBundleAbilityNames.size() << endl;
201 }
202
OnBundleStateChanged(const uint8_t installType,const int32_t resultCode,const std::string & resultMsg,const std::string & bundleName)203 void DrvBundleStateCallback::OnBundleStateChanged(const uint8_t installType, const int32_t resultCode,
204 const std::string &resultMsg, const std::string &bundleName)
205 {
206 EDM_LOGI(MODULE_PKG_MGR, "OnBundleStateChanged");
207 return;
208 };
209
210 /**
211 * @brief Called when a new application package has been installed on the device.
212 * @param bundleName Indicates the name of the bundle whose state has been installed.
213 * @param userId Indicates the id of the bundle whose state has been installed.
214 */
OnBundleAdded(const std::string & bundleName,const int userId)215 void DrvBundleStateCallback::OnBundleAdded(const std::string &bundleName, const int userId)
216 {
217 EDM_LOGI(MODULE_PKG_MGR, "OnBundleAdded");
218 StartTrace(LABEL, "OnBundleAdded");
219 if (!IsCurrentUserId(userId)) {
220 return;
221 }
222 std::vector<ExtensionAbilityInfo> driverInfos;
223 if (!QueryDriverInfos(bundleName, userId, driverInfos)) {
224 auto extDevEvent = std::make_shared<ExtDevEvent>(__func__, DRIVER_PACKAGE_DATA_REFRESH);
225 if (extDevEvent == nullptr) {
226 return;
227 }
228 extDevEvent->bundleName = bundleName;
229 extDevEvent->userId = userId;
230 ExtDevReportSysEvent::ReportExternalDeviceEvent(extDevEvent,
231 ExtDevReportSysEvent::EventErrCode::QUERY_DRIVER_EXTENSION_FAILED);
232 return;
233 }
234
235 if (!UpdateToRdb(driverInfos, bundleName, __func__)) {
236 EDM_LOGE(MODULE_PKG_MGR, "OnBundleAdded error");
237 }
238 FinishTrace(LABEL);
239 }
240 /**
241 * @brief Called when a new application package has been Updated on the device.
242 * @param bundleName Indicates the name of the bundle whose state has been Updated.
243 * @param userId Indicates the id of the bundle whose state has been Updated.
244 */
OnBundleUpdated(const std::string & bundleName,const int userId)245 void DrvBundleStateCallback::OnBundleUpdated(const std::string &bundleName, const int userId)
246 {
247 EDM_LOGI(MODULE_PKG_MGR, "OnBundleUpdated");
248 StartTrace(LABEL, "OnBundleUpdated");
249 if (!IsCurrentUserId(userId)) {
250 return;
251 }
252 std::vector<ExtensionAbilityInfo> driverInfos;
253 if (!QueryDriverInfos(bundleName, userId, driverInfos)) {
254 auto extDevEvent = std::make_shared<ExtDevEvent>(__func__, DRIVER_PACKAGE_DATA_REFRESH);
255 if (extDevEvent == nullptr) {
256 return;
257 }
258 extDevEvent->bundleName = bundleName;
259 extDevEvent->userId = userId;
260 ExtDevReportSysEvent::ReportExternalDeviceEvent(extDevEvent,
261 ExtDevReportSysEvent::EventErrCode::QUERY_DRIVER_EXTENSION_FAILED);
262 return;
263 }
264
265 if (driverInfos.empty()) {
266 OnBundleDrvRemoved(bundleName, __func__);
267 return;
268 }
269
270 if (!UpdateToRdb(driverInfos, bundleName, __func__)) {
271 EDM_LOGE(MODULE_PKG_MGR, "OnBundleUpdated error");
272 }
273 FinishTrace(LABEL);
274 }
275
276 /**
277 * @brief Called when a new application package has been Removed on the device.
278 * @param bundleName Indicates the name of the bundle whose state has been Removed.
279 * @param userId Indicates the id of the bundle whose state has been Removed.
280 */
OnBundleRemoved(const std::string & bundleName,const int userId)281 void DrvBundleStateCallback::OnBundleRemoved(const std::string &bundleName, const int userId)
282 {
283 EDM_LOGI(MODULE_PKG_MGR, "OnBundleRemoved");
284 StartTrace(LABEL, "OnBundleRemoved");
285 if (!IsCurrentUserId(userId)) {
286 return;
287 }
288 OnBundleDrvRemoved(bundleName, __func__);
289 FinishTrace(LABEL);
290 }
291
AsObject()292 sptr<IRemoteObject> DrvBundleStateCallback::AsObject()
293 {
294 return nullptr;
295 }
296
ResetInitOnce()297 void DrvBundleStateCallback::ResetInitOnce()
298 {
299 std::lock_guard<std::mutex> lock(initOnceMutex_);
300 initOnce = false;
301 }
302
ResetMatchedBundles(const int32_t userId)303 void DrvBundleStateCallback::ResetMatchedBundles(const int32_t userId)
304 {
305 if (bundleUpdateCallback_ == nullptr) {
306 EDM_LOGE(MODULE_PKG_MGR, "DrvBundleStateCallback::ResetMatchedBundles bundleUpdateCallback_ is null");
307 return;
308 }
309 std::thread taskThread([userId, this]() {
310 bundleUpdateCallback_->OnBundlesReseted(userId);
311 });
312 pthread_setname_np(taskThread.native_handle(), BUNDLE_RESET_TASK_NAME);
313 taskThread.detach();
314 }
315
GetAllDriverInfos()316 bool DrvBundleStateCallback::GetAllDriverInfos()
317 {
318 std::lock_guard<std::mutex> lock(initOnceMutex_);
319 if (initOnce) {
320 EDM_LOGI(MODULE_PKG_MGR, "GetAllDriverInfos has inited");
321 return true;
322 }
323
324 std::vector<ExtensionAbilityInfo> driverInfos;
325 int32_t userId = GetCurrentActiveUserId();
326 if (userId == Constants::INVALID_USERID) {
327 EDM_LOGI(MODULE_PKG_MGR, "GetCurrentActiveUserId userId is invalid");
328 return false;
329 }
330 EDM_LOGI(MODULE_PKG_MGR, "QueryExtensionAbilityInfos userId:%{public}d", userId);
331 bundleMgrMutex_.lock();
332 if (!GetBundleMgrProxy()) {
333 EDM_LOGE(MODULE_PKG_MGR, "%{public}s: failed to GetBundleMgrProxy", __func__);
334 bundleMgrMutex_.unlock();
335 return false;
336 }
337 bundleMgr_->QueryExtensionAbilityInfos(ExtensionAbilityType::DRIVER, userId, driverInfos);
338 bundleMgrMutex_.unlock();
339 if (!UpdateToRdb(driverInfos, "", __func__)) {
340 EDM_LOGE(MODULE_PKG_MGR, "UpdateToRdb failed");
341 return false;
342 }
343 initOnce = true;
344 return true;
345 }
346
GetAllDriverInfosAsync()347 void DrvBundleStateCallback::GetAllDriverInfosAsync()
348 {
349 EDM_LOGI(MODULE_PKG_MGR, "GetAllDriverInfosAsync enter");
350 std::thread taskThread([this]() {
351 bmsFuture_.wait();
352 accountFuture_.wait();
353 if (!GetAllDriverInfos()) {
354 EDM_LOGE(MODULE_PKG_MGR, "GetAllDriverInfos failed");
355 }
356 });
357 pthread_setname_np(taskThread.native_handle(), GET_DRIVERINFO_TASK_NAME);
358 taskThread.detach();
359 }
360
GetStiching()361 string DrvBundleStateCallback::GetStiching()
362 {
363 return stiching;
364 }
365
CheckBundleMgrProxyPermission()366 bool DrvBundleStateCallback::CheckBundleMgrProxyPermission()
367 {
368 // check permission
369 std::lock_guard<std::mutex> lock(bundleMgrMutex_);
370 if (!GetBundleMgrProxy()) {
371 EDM_LOGE(MODULE_PKG_MGR, "%{public}s: failed to GetBundleMgrProxy", __func__);
372 return false;
373 }
374 if (!bundleMgr_->VerifySystemApi(Constants::INVALID_API_VERSION)) {
375 EDM_LOGE(MODULE_PKG_MGR, "non-system app calling system api");
376 return false;
377 }
378 if (!bundleMgr_->VerifyCallingPermission(Constants::LISTEN_BUNDLE_CHANGE)) {
379 EDM_LOGE(MODULE_PKG_MGR, "register bundle status callback failed due to lack of permission");
380 return false;
381 }
382 return true;
383 }
384
QueryDriverInfos(const std::string & bundleName,const int userId,std::vector<ExtensionAbilityInfo> & driverInfos)385 bool DrvBundleStateCallback::QueryDriverInfos(const std::string &bundleName, const int userId,
386 std::vector<ExtensionAbilityInfo> &driverInfos)
387 {
388 if (bundleName.empty()) {
389 EDM_LOGE(MODULE_PKG_MGR, "%{public}s: BundleName is empty", __func__);
390 return false;
391 }
392
393 std::lock_guard<std::mutex> lock(bundleMgrMutex_);
394 if (!GetBundleMgrProxy()) {
395 EDM_LOGE(MODULE_PKG_MGR, "%{public}s: failed to GetBundleMgrProxy", __func__);
396 return false;
397 }
398
399 BundleInfo tmpBundleInfo;
400 int32_t flags = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY) + \
401 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION) + \
402 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA);
403 if (!(bundleMgr_->GetBundleInfo(bundleName, flags, tmpBundleInfo, userId))) {
404 EDM_LOGE(MODULE_PKG_MGR, "GetBundleInfo err");
405 return false;
406 }
407
408 for (auto &extensionInfo : tmpBundleInfo.extensionInfos) {
409 if (extensionInfo.type == ExtensionAbilityType::DRIVER) {
410 extensionInfo.applicationInfo = tmpBundleInfo.applicationInfo;
411 driverInfos.emplace_back(extensionInfo);
412 }
413 }
414 return true;
415 }
416
ClearDriverInfo(DriverInfo & tmpDrvInfo)417 void DrvBundleStateCallback::ClearDriverInfo(DriverInfo &tmpDrvInfo)
418 {
419 tmpDrvInfo.bus_.clear();
420 tmpDrvInfo.vendor_.clear();
421 tmpDrvInfo.version_.clear();
422 tmpDrvInfo.driverInfoExt_ = nullptr;
423 }
424
ReportPkgsEvent(const std::vector<DriverInfo> & driverObjs,const std::string & interfaceName,const ExtDevReportSysEvent::EventErrCode errCode)425 static void ReportPkgsEvent(const std::vector<DriverInfo> &driverObjs, const std::string &interfaceName,
426 const ExtDevReportSysEvent::EventErrCode errCode)
427 {
428 EDM_LOGI(MODULE_PKG_MGR, "ReportPkgsEvent enter, interfaceName:%{public}s, %{public}zu", interfaceName.c_str(),
429 driverObjs.size());
430 auto extDevEvent = std::make_shared<ExtDevEvent>(interfaceName, DRIVER_PACKAGE_DATA_REFRESH);
431 for (const auto &driverObj : driverObjs) {
432 ExtDevReportSysEvent::ParseToExtDevEvent(std::make_shared<DriverInfo>(driverObj), extDevEvent);
433 ExtDevReportSysEvent::ReportExternalDeviceEvent(extDevEvent, errCode);
434 }
435 }
436
ReportPkgsDelEvent(const std::vector<PkgInfoTable> & pkgInfos,const std::string & interfaceName,const ExtDevReportSysEvent::EventErrCode errCode)437 static void ReportPkgsDelEvent(const std::vector<PkgInfoTable> &pkgInfos,
438 const std::string &interfaceName, const ExtDevReportSysEvent::EventErrCode errCode)
439 {
440 EDM_LOGI(MODULE_PKG_MGR, "ReportPkgsDelEvent enter, interfaceName:%{public}s, %{public}zu", interfaceName.c_str(),
441 pkgInfos.size());
442 auto extDevEvent = std::make_shared<ExtDevEvent>(interfaceName, DRIVER_PACKAGE_DATA_REFRESH);
443 for (const auto &pkgInfo : pkgInfos) {
444 std::shared_ptr<DriverInfo> driverInfo
445 = std::make_shared<DriverInfo>(pkgInfo.bundleName, pkgInfo.driverName, pkgInfo.driverUid, pkgInfo.userId);
446 if (driverInfo == nullptr) {
447 EDM_LOGE(MODULE_PKG_MGR, "ReportPkgsDelEvent driverInfo is null");
448 continue;
449 }
450 driverInfo->UnSerialize(pkgInfo.driverInfo);
451 ExtDevReportSysEvent::ParseToExtDevEvent(driverInfo, extDevEvent);
452 ExtDevReportSysEvent::ReportExternalDeviceEvent(extDevEvent, errCode);
453 }
454 }
455
UpdateToRdb(const std::vector<ExtensionAbilityInfo> & driverInfos,const std::string & bundleName,const std::string & interfaceName)456 bool DrvBundleStateCallback::UpdateToRdb(const std::vector<ExtensionAbilityInfo> &driverInfos,
457 const std::string &bundleName, const std::string &interfaceName)
458 {
459 std::vector<PkgInfoTable> pkgInfoTables;
460 auto driverObjs = ParseToPkgInfoTables(driverInfos, pkgInfoTables);
461 std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
462 if (helper->AddOrUpdatePkgInfo(pkgInfoTables, bundleName) < PKG_OK) {
463 EDM_LOGE(MODULE_PKG_MGR, "add or update failed,bundleName:%{public}s", bundleName.c_str());
464 ReportPkgsEvent(driverObjs, interfaceName, ExtDevReportSysEvent::EventErrCode::UPDATE_DATABASE_FAILED);
465 return false;
466 }
467 ReportPkgsEvent(driverObjs, interfaceName, ExtDevReportSysEvent::EventErrCode::SUCCESS);
468
469 if (bundleUpdateCallback_ != nullptr) {
470 std::thread taskThread([bundleName, this]() {
471 if (bundleUpdateCallback_ == nullptr) {
472 EDM_LOGE(MODULE_PKG_MGR, "UpdateToRdb bundleUpdateCallback_ is nullptr");
473 return;
474 }
475 bundleUpdateCallback_->OnBundlesUpdated(bundleName);
476 });
477 pthread_setname_np(taskThread.native_handle(), BUNDLE_UPDATE_TASK_NAME);
478 taskThread.detach();
479 }
480 return true;
481 }
482
GetCurrentActiveUserId()483 int32_t DrvBundleStateCallback::GetCurrentActiveUserId()
484 {
485 int32_t localId;
486 int32_t ret = AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(localId);
487 if (ret != 0) {
488 EDM_LOGE(MODULE_PKG_MGR, "GetForegroundOsAccountLocalId failed ret:%{public}d", ret);
489 return Constants::INVALID_USERID;
490 }
491 return localId;
492 }
493
IsCurrentUserId(const int userId)494 bool DrvBundleStateCallback::IsCurrentUserId(const int userId)
495 {
496 return GetCurrentActiveUserId() == userId;
497 }
498
GetBundleMgrProxy()499 bool DrvBundleStateCallback::GetBundleMgrProxy()
500 {
501 if (bundleMgr_ == nullptr) {
502 auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
503 if (systemAbilityManager == nullptr) {
504 EDM_LOGE(MODULE_PKG_MGR, "%{public}s: GetSystemAbilityManager is null", __func__);
505 return false;
506 }
507 auto remoteObj = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
508 if (remoteObj == nullptr) {
509 EDM_LOGE(MODULE_PKG_MGR, "%{public}s: GetSystemAbility is null", __func__);
510 return false;
511 }
512 bundleMgr_ = OHOS::iface_cast<IBundleMgr>(remoteObj);
513 if (bundleMgr_ == nullptr) {
514 EDM_LOGE(MODULE_PKG_MGR, "%{public}s: iface_cast get null", __func__);
515 return false;
516 }
517 bmsDeathRecipient_ = new (std::nothrow) BundleMgrDeathRecipient([this]() {
518 ResetBundleMgr();
519 });
520 if (bmsDeathRecipient_ == nullptr) {
521 EDM_LOGE(MODULE_PKG_MGR, "%{public}s: failed to create BundleMgrDeathRecipient", __func__);
522 bundleMgr_ = nullptr;
523 return false;
524 }
525 remoteObj->AddDeathRecipient(bmsDeathRecipient_);
526 }
527 return true;
528 }
529
OnBundleDrvRemoved(const std::string & bundleName,const std::string & interfaceName)530 void DrvBundleStateCallback::OnBundleDrvRemoved(const std::string &bundleName, const std::string &interfaceName)
531 {
532 std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
533 std::vector<PkgInfoTable> pkgInfos;
534 (void)helper->QueryPkgInfos(bundleName, pkgInfos);
535 int32_t ret = helper->DeleteRightRecord(bundleName);
536 if (ret < 0) {
537 EDM_LOGE(MODULE_PKG_MGR, "delete failed: %{public}s", bundleName.c_str());
538 ReportPkgsDelEvent(pkgInfos, interfaceName, ExtDevReportSysEvent::EventErrCode::UPDATE_DATABASE_FAILED);
539 return;
540 }
541 ReportPkgsDelEvent(pkgInfos, interfaceName, ExtDevReportSysEvent::EventErrCode::SUCCESS);
542 if (bundleUpdateCallback_ != nullptr) {
543 std::thread taskThread([bundleName, this]() {
544 if (bundleUpdateCallback_ == nullptr) {
545 EDM_LOGE(MODULE_PKG_MGR, "OnBundleDrvRemoved bundleUpdateCallback_ is nullptr");
546 return;
547 }
548 bundleUpdateCallback_->OnBundlesUpdated(bundleName);
549 });
550 pthread_setname_np(taskThread.native_handle(), BUNDLE_UPDATE_TASK_NAME);
551 taskThread.detach();
552 }
553 }
554
ResetBundleMgr()555 void DrvBundleStateCallback::ResetBundleMgr()
556 {
557 std::lock_guard<std::mutex> lock(bundleMgrMutex_);
558 if (bundleMgr_ != nullptr && bundleMgr_->AsObject()) {
559 bundleMgr_->AsObject()->RemoveDeathRecipient(bmsDeathRecipient_);
560 }
561 bundleMgr_ = nullptr;
562 }
563 }
564 }