diff --git a/services/ans/include/bundle_manager_helper.h b/services/ans/include/bundle_manager_helper.h index 9a060facae6ec7dcb7d1bdab33e06a77af3423df..1ddaf79187f5f1425ad4ea59f700b659af2da745 100644 --- a/services/ans/include/bundle_manager_helper.h +++ b/services/ans/include/bundle_manager_helper.h @@ -33,7 +33,7 @@ class BundleManagerHelper : public DelayedSingleton { public: std::string GetBundleNameByUid(int uid); bool IsSystemApp(int uid); - int GetDefaultUidByBundleName(const std::string &bundle); + int GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId); bool GetBundleInfoByBundleName(const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED bool GetDistributedNotificationEnabled(const std::string &bundleName, const int userId); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 0a68398f59dd4fb29f08658b449a6f444ee9d17b..897ec9b92472f4b920b837d0baedc1de8644a620 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -302,7 +302,12 @@ sptr AdvancedNotificationService::GenerateValidBundleO if (bundleOption->GetUid() <= 0) { std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); if (bundleManager != nullptr) { - int uid = bundleManager->GetDefaultUidByBundleName(bundleOption->GetBundleName()); + int activeUserId = -1; + if (!GetActiveUserId(activeUserId)) { + ANS_LOGE("Failed to get active user id!"); + return validBundleOption; + } + int uid = bundleManager->GetDefaultUidByBundleName(bundleOption->GetBundleName(), activeUserId); if (uid > 0) { validBundleOption = new NotificationBundleOption(bundleOption->GetBundleName(), uid); } @@ -1576,7 +1581,7 @@ ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify( result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(targetBundle, allowed); if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { result = ERR_OK; - allowed = CheckApiCompatibility(bundleOption); + allowed = CheckApiCompatibility(targetBundle); } } })); @@ -2737,8 +2742,12 @@ void AdvancedNotificationService::OnDistributedPublish( const std::string &deviceId, const std::string &bundleName, sptr &request) { ANS_LOGD("%{public}s", __FUNCTION__); - - request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName)); + int activeUserId = -1; + if (!GetActiveUserId(activeUserId)) { + ANS_LOGE("Failed to get active user id!"); + return; + } + request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, activeUserId)); handler_->PostTask(std::bind([this, deviceId, bundleName, request]() { if (!CheckDistributedNotificationType(request)) { @@ -2788,8 +2797,12 @@ void AdvancedNotificationService::OnDistributedUpdate( const std::string &deviceId, const std::string &bundleName, sptr &request) { ANS_LOGD("%{public}s", __FUNCTION__); - - request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName)); + int activeUserId = -1; + if (!GetActiveUserId(activeUserId)) { + ANS_LOGE("Failed to get active user id!"); + return; + } + request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, activeUserId)); handler_->PostTask(std::bind([this, deviceId, bundleName, request]() { if (!CheckDistributedNotificationType(request)) { @@ -3177,6 +3190,8 @@ bool AdvancedNotificationService::CheckApiCompatibility(const sptrGetUid(), callingUserId); if (bundleManager != nullptr) { if (!bundleManager->GetBundleInfoByBundleName(bundleOption->GetBundleName(), callingUserId, bundleInfo)) { + ANS_LOGW("Failed to GetBundleInfoByBundleName, bundlename = %{public}s", + bundleOption->GetBundleName().c_str()); return false; } } diff --git a/services/ans/src/bundle_manager_helper.cpp b/services/ans/src/bundle_manager_helper.cpp index 7afa5a38f68c2b2184681f66ba420df836aaa17a..9e965edcb73798f9fe07f06e0f98ee0b2983315e 100644 --- a/services/ans/src/bundle_manager_helper.cpp +++ b/services/ans/src/bundle_manager_helper.cpp @@ -111,7 +111,7 @@ void BundleManagerHelper::Disconnect() } } -int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle) +int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId) { int uid = -1; @@ -121,7 +121,7 @@ int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle) if (bundleMgr_ != nullptr) { AppExecFwk::BundleInfo bundleInfo; - if (bundleMgr_->GetBundleInfo(bundle, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo)) { + if (bundleMgr_->GetBundleInfo(bundle, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, userId)) { uid = bundleInfo.uid; } } diff --git a/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp b/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp index 7528a5c370598c4f3198c81625b6103ae9dd515e..6eba48ba6f8b9a28b9b77136e946362d3b88ca52 100644 --- a/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp +++ b/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp @@ -36,7 +36,7 @@ std::string BundleManagerHelper::GetBundleNameByUid(int uid) return (uid == NON_BUNDLE_NAME_UID) ? "" : "bundleName"; } -int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle) +int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId) { return NON_SYSTEM_APP_UID; }