diff --git a/services/bundlemgr/include/bundle_common_event_mgr.h b/services/bundlemgr/include/bundle_common_event_mgr.h index ab7806f524cc62050621c27b325abcb333fa8ba5..70f20680e08fc33c8f4ceb037be3853a91ba64e5 100755 --- a/services/bundlemgr/include/bundle_common_event_mgr.h +++ b/services/bundlemgr/include/bundle_common_event_mgr.h @@ -73,6 +73,7 @@ struct NotifyBundleEvents { bool isBundleExist = false; bool crossAppSharedConfig = false; bool isRecover = false; + bool isInstallByBundleName = false; }; class BundleCommonEventMgr { @@ -100,7 +101,7 @@ private: bool PublishCommonEvent(const std::string &bundleName, const std::string &action, const int32_t publishUserId, const EventFwk::CommonEventData &commonData); bool ProcessBundleChangedEventForOtherUsers(const std::shared_ptr &dataMgr, - const std::string &bundleName, const std::string &action, const int32_t publishUserId, + const NotifyBundleEvents &event, const int32_t publishUserId, const EventFwk::CommonEventData &commonData); void Init(); diff --git a/services/bundlemgr/src/base_bundle_installer.cpp b/services/bundlemgr/src/base_bundle_installer.cpp index 2d83c03167e5fec17cf4884593f690a5beafa84b..f326bb5a66bb0307b66b9aef6610b815cbd8efe6 100644 --- a/services/bundlemgr/src/base_bundle_installer.cpp +++ b/services/bundlemgr/src/base_bundle_installer.cpp @@ -269,7 +269,8 @@ ErrCode BaseBundleInstaller::InstallBundleByBundleName( .atomicServiceModuleUpgrade = atomicServiceModuleUpgrade_, .bundleName = bundleName, .appDistributionType = appDistributionType_, - .crossAppSharedConfig = isBundleCrossAppSharedConfig_ + .crossAppSharedConfig = isBundleCrossAppSharedConfig_, + .isInstallByBundleName = true }; if (installParam.concentrateSendEvent) { AddNotifyBundleEvents(installRes); diff --git a/services/bundlemgr/src/bundle_common_event_mgr.cpp b/services/bundlemgr/src/bundle_common_event_mgr.cpp index 3ae5656599d2de954c1f79708d8f99767c450d20..0d3aa1217b4f6ac558ffa8809d13d4eaea4d0897 100755 --- a/services/bundlemgr/src/bundle_common_event_mgr.cpp +++ b/services/bundlemgr/src/bundle_common_event_mgr.cpp @@ -144,8 +144,7 @@ void BundleCommonEventMgr::NotifyBundleStatus(const NotifyBundleEvents &installR std::string identity = IPCSkeleton::ResetCallingIdentity(); (void)PublishCommonEvent(installResult.bundleName, want.GetAction(), publishUserId, commonData); - (void)ProcessBundleChangedEventForOtherUsers(dataMgr, installResult.bundleName, - want.GetAction(), publishUserId, commonData); + (void)ProcessBundleChangedEventForOtherUsers(dataMgr, installResult, publishUserId, commonData); IPCSkeleton::SetCallingIdentity(identity); } @@ -174,23 +173,27 @@ bool BundleCommonEventMgr::PublishCommonEvent( } return true; } + bool BundleCommonEventMgr::ProcessBundleChangedEventForOtherUsers( const std::shared_ptr &dataMgr, - const std::string &bundleName, - const std::string &action, + const NotifyBundleEvents &event, const int32_t publishUserId, const EventFwk::CommonEventData &commonData) { - if (action != EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) { + if ((event.type != NotifyType::INSTALL) && (event.type != NotifyType::UPDATE)) { + return false; + } + if ((event.type == NotifyType::INSTALL) && + (event.isInstallByBundleName || event.isRecover || (event.appIndex != 0))) { return false; } if (dataMgr == nullptr) { - APP_LOGE("dataMgr is nullptr -n %{public}s", bundleName.c_str()); + APP_LOGE("dataMgr is nullptr -n %{public}s", event.bundleName.c_str()); return false; } - auto userIds = dataMgr->GetUserIds(bundleName); + auto userIds = dataMgr->GetUserIds(event.bundleName); if (userIds.size() <= 1) { - APP_LOGD("-n %{public}s only has one user", bundleName.c_str()); + APP_LOGD("-n %{public}s only has one user", event.bundleName.c_str()); return false; } EventFwk::CommonEventPublishInfo publishInfo; @@ -200,8 +203,24 @@ bool BundleCommonEventMgr::ProcessBundleChangedEventForOtherUsers( if (userId == publishUserId) { continue; } - APP_LOGI("-n %{public}s publish change event for -u %{public}d", bundleName.c_str(), userId); - if (!EventFwk::CommonEventManager::PublishCommonEventAsUser(commonData, publishInfo, userId)) { + OHOS::AAFwk::Want want = commonData.GetWant(); + if (event.type == NotifyType::INSTALL) { + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); + } + ApplicationInfo appInfo; + if (dataMgr->GetApplicationInfoV9(event.bundleName, + static_cast(GetApplicationFlag::GET_APPLICATION_INFO_WITH_DISABLE), userId, appInfo) != ERR_OK) { + APP_LOGE("-n %{public}s -u %{public}d get appInfo failed when publish event", + event.bundleName.c_str(), userId); + continue; + } + // need modify uid, userId, tokenId + want.SetParam(Constants::UID, appInfo.uid); + want.SetParam(Constants::USER_ID, userId); + want.SetParam(ACCESS_TOKEN_ID, static_cast(appInfo.accessTokenId)); + APP_LOGI("-n %{public}s publish change event for -u %{public}d", event.bundleName.c_str(), userId); + EventFwk::CommonEventData newCommonData { want }; + if (!EventFwk::CommonEventManager::PublishCommonEventAsUser(newCommonData, publishInfo, userId)) { APP_LOGE("PublishCommonEventAsUser failed, userId:%{public}d", userId); } } diff --git a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test.cpp index e7b012fd5945f8b55138a4f814f2d3f0ea30b8a7..ef7381924757f0d806e2ed42426e710d7f6233ba 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test.cpp @@ -3640,16 +3640,18 @@ HWTEST_F(BmsBundleDataMgrTest, ProcessBundleChangedEventForOtherUsers_0100, Func { EventFwk::CommonEventData commonData; std::shared_ptr commonEventMgr = std::make_shared(); - bool ret = commonEventMgr->ProcessBundleChangedEventForOtherUsers(nullptr, "notExist", "", USERID, commonData); + NotifyBundleEvents installResult; + installResult.bundleName = "notExist"; + installResult.type = NotifyType::UNINSTALL_BUNDLE; + bool ret = commonEventMgr->ProcessBundleChangedEventForOtherUsers(nullptr, installResult, USERID, commonData); EXPECT_FALSE(ret); - ret = commonEventMgr->ProcessBundleChangedEventForOtherUsers(nullptr, "notExist", - EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED, USERID, commonData); + installResult.type = NotifyType::UPDATE; + ret = commonEventMgr->ProcessBundleChangedEventForOtherUsers(nullptr, installResult, USERID, commonData); EXPECT_FALSE(ret); auto dataMgr = GetBundleDataMgr(); - ret = commonEventMgr->ProcessBundleChangedEventForOtherUsers(dataMgr, "notExist", - EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED, USERID, commonData); + ret = commonEventMgr->ProcessBundleChangedEventForOtherUsers(dataMgr, installResult, USERID, commonData); EXPECT_FALSE(ret); } @@ -3671,8 +3673,10 @@ HWTEST_F(BmsBundleDataMgrTest, ProcessBundleChangedEventForOtherUsers_0200, Func dataMgr->bundleInfos_["bundleName"] = innerBundleInfo; EventFwk::CommonEventData commonData; std::shared_ptr commonEventMgr = std::make_shared(); - bool ret = commonEventMgr->ProcessBundleChangedEventForOtherUsers(dataMgr, "bundleName", - EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED, USERID, commonData); + NotifyBundleEvents installResult; + installResult.bundleName = "bundleName"; + installResult.type = NotifyType::UPDATE; + bool ret = commonEventMgr->ProcessBundleChangedEventForOtherUsers(dataMgr, installResult, USERID, commonData); EXPECT_TRUE(ret); auto iter = dataMgr->bundleInfos_.find("bundleName"); if (iter != dataMgr->bundleInfos_.end()) { @@ -3680,6 +3684,56 @@ HWTEST_F(BmsBundleDataMgrTest, ProcessBundleChangedEventForOtherUsers_0200, Func } } +/** + * @tc.number: ProcessBundleChangedEventForOtherUsers_0300 + * @tc.name: test ProcessBundleChangedEventForOtherUsers + * @tc.desc: 1.ProcessBundleChangedEventForOtherUsers + */ +HWTEST_F(BmsBundleDataMgrTest, ProcessBundleChangedEventForOtherUsers_0300, Function | MediumTest | Level1) +{ + InnerBundleUserInfo userInfo; + userInfo.bundleUserInfo.userId = USERID; + InnerBundleInfo innerBundleInfo; + innerBundleInfo.innerBundleUserInfos_["_100"] = userInfo; + userInfo.bundleUserInfo.userId = 200; + innerBundleInfo.innerBundleUserInfos_["_200"] = userInfo; + + auto dataMgr = GetBundleDataMgr(); + dataMgr->AddUserId(200); + dataMgr->bundleInfos_["bundleName"] = innerBundleInfo; + EventFwk::CommonEventData commonData; + std::shared_ptr commonEventMgr = std::make_shared(); + NotifyBundleEvents installResult; + installResult.bundleName = "bundleName"; + installResult.type = NotifyType::INSTALL; + installResult.isInstallByBundleName = false; + installResult.isRecover = false; + bool ret = commonEventMgr->ProcessBundleChangedEventForOtherUsers(dataMgr, installResult, USERID, commonData); + EXPECT_TRUE(ret); + installResult.isInstallByBundleName = true; + installResult.isRecover = false; + ret = commonEventMgr->ProcessBundleChangedEventForOtherUsers(dataMgr, installResult, USERID, commonData); + EXPECT_FALSE(ret); + installResult.isInstallByBundleName = false; + installResult.isRecover = true; + ret = commonEventMgr->ProcessBundleChangedEventForOtherUsers(dataMgr, installResult, USERID, commonData); + EXPECT_FALSE(ret); + installResult.isInstallByBundleName = true; + installResult.isRecover = true; + ret = commonEventMgr->ProcessBundleChangedEventForOtherUsers(dataMgr, installResult, USERID, commonData); + EXPECT_FALSE(ret); + installResult.isInstallByBundleName = false; + installResult.isRecover = false; + installResult.appIndex = 1; + ret = commonEventMgr->ProcessBundleChangedEventForOtherUsers(dataMgr, installResult, USERID, commonData); + EXPECT_FALSE(ret); + auto iter = dataMgr->bundleInfos_.find("bundleName"); + if (iter != dataMgr->bundleInfos_.end()) { + dataMgr->bundleInfos_.erase(iter); + } + dataMgr->RemoveUserId(200); +} + /** * @tc.number: PublishCommonEvent_0100 * @tc.name: test PublishCommonEvent