From 6bbfb6a5bad577dde43686a17abc98b7b96eefc3 Mon Sep 17 00:00:00 2001 From: zengsiyu Date: Sat, 12 Mar 2022 20:50:57 +0800 Subject: [PATCH] fix: fix api check Signed-off-by: zengsiyu Change-Id: Id8b5e0a6348612fb81a9084cd3d42d74f366e336 Signed-off-by: zengsiyu --- services/ans/include/bundle_manager_helper.h | 4 +++- .../ans/include/notification_preferences.h | 3 ++- .../ans/src/advanced_notification_service.cpp | 21 +++++------------- services/ans/src/bundle_manager_helper.cpp | 22 ++++++++++++++++++- services/ans/src/notification_preferences.cpp | 14 ++++++++++++ services/ans/src/permission_filter.cpp | 8 +++++-- .../mock/mock_bundle_manager_helper.cpp | 7 +++++- .../mock/mock_bundle_manager_helper.cpp | 7 +++++- 8 files changed, 63 insertions(+), 23 deletions(-) diff --git a/services/ans/include/bundle_manager_helper.h b/services/ans/include/bundle_manager_helper.h index 1ddaf7918..2e5fbd57a 100644 --- a/services/ans/include/bundle_manager_helper.h +++ b/services/ans/include/bundle_manager_helper.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -23,6 +23,7 @@ #include "bundle_mgr_interface.h" #include "ipc_skeleton.h" #include "iremote_object.h" +#include "notification_bundle_option.h" #include "refbase.h" #include "remote_death_recipient.h" #include "singleton.h" @@ -33,6 +34,7 @@ class BundleManagerHelper : public DelayedSingleton { public: std::string GetBundleNameByUid(int uid); bool IsSystemApp(int uid); + bool CheckApiCompatibility(const sptr &bundleOption); 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 diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 63d605377..df5bf7901 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -101,6 +101,7 @@ private: ErrCode GetBundleProperty( const sptr &bundleOption, const BundleType &type, T &value) const; std::string GenerateBundleKey(const sptr &bundleOption) const; + bool CheckApiCompatibility(const sptr &bundleOption) const; private: NotificationPreferencesInfo preferencesInfo_ {}; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 2cb610c11..9678c5f8d 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1591,6 +1591,7 @@ ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify( if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { result = ERR_OK; allowed = CheckApiCompatibility(targetBundle); + SetNotificationsEnabledForSpecialBundle("", bundleOption, allowed); } } })); @@ -3225,24 +3226,12 @@ ErrCode AdvancedNotificationService::GetHasPoppedDialog( bool AdvancedNotificationService::CheckApiCompatibility(const sptr &bundleOption) { - AppExecFwk::BundleInfo bundleInfo; + ANS_LOGD("%{public}s", __FUNCTION__); std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); - int32_t callingUserId; - AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), 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; - } - } - - for (auto abilityInfo : bundleInfo.abilityInfos) { - if (abilityInfo.isStageBasedModel) { - return false; - } + if (bundleManager == nullptr) { + return false; } - return true; + return bundleManager->CheckApiCompatibility(bundleOption); } void AdvancedNotificationService::OnResourceRemove(int32_t userId) diff --git a/services/ans/src/bundle_manager_helper.cpp b/services/ans/src/bundle_manager_helper.cpp index 9e965edcb..037d4bb0a 100644 --- a/services/ans/src/bundle_manager_helper.cpp +++ b/services/ans/src/bundle_manager_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,6 +17,7 @@ #include "if_system_ability_manager.h" #include "iservice_registry.h" +#include "os_account_manager.h" #include "system_ability_definition.h" #include "ans_const_define.h" @@ -71,6 +72,25 @@ bool BundleManagerHelper::IsSystemApp(int uid) return isSystemApp; } +bool BundleManagerHelper::CheckApiCompatibility(const sptr &bundleOption) +{ + AppExecFwk::BundleInfo bundleInfo; + int32_t callingUserId; + AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), callingUserId); + if (!GetBundleInfoByBundleName(bundleOption->GetBundleName(), callingUserId, bundleInfo)) { + ANS_LOGW("Failed to GetBundleInfoByBundleName, bundlename = %{public}s", + bundleOption->GetBundleName().c_str()); + return false; + } + + for (auto abilityInfo : bundleInfo.abilityInfos) { + if (abilityInfo.isStageBasedModel) { + return false; + } + } + return true; +} + bool BundleManagerHelper::GetBundleInfoByBundleName( const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo) { diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index a0613efa3..f3367710e 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -20,6 +20,7 @@ #include "ans_const_define.h" #include "ans_inner_errors.h" #include "ans_log_wrapper.h" +#include "bundle_manager_helper.h" #include "nlohmann/json.hpp" #include "os_account_manager.h" @@ -611,6 +612,7 @@ ErrCode NotificationPreferences::CheckSlotForCreateSlot(const sptrGetBundleName()); bundleInfo.SetBundleUid(bundleOption->GetUid()); + bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption)); } bundleInfo.SetSlot(slot); preferencesInfo.SetBundleInfo(bundleInfo); @@ -635,6 +637,7 @@ ErrCode NotificationPreferences::CheckGroupForCreateSlotGroup(const sptrGetBundleName()); bundleInfo.SetBundleUid(bundleOption->GetUid()); + bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption)); } else { if (bundleInfo.GetGroupSize() >= MAX_SLOT_GROUP_NUM) { return ERR_ANS_PREFERENCES_NOTIFICATION_SLOTGROUP_EXCEED_MAX_NUM; @@ -745,6 +748,7 @@ ErrCode NotificationPreferences::SetBundleProperty(NotificationPreferencesInfo & if (!preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) { bundleInfo.SetBundleName(bundleOption->GetBundleName()); bundleInfo.SetBundleUid(bundleOption->GetUid()); + bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption)); } result = SaveBundleProperty(bundleInfo, bundleOption, type, value); @@ -890,5 +894,15 @@ void NotificationPreferences::RemoveSettings(int32_t userId) preferncesDB_->RemoveDoNotDisturbDate(userId); } } + +bool NotificationPreferences::CheckApiCompatibility(const sptr &bundleOption) const +{ + ANS_LOGD("%{public}s", __FUNCTION__); + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager == nullptr) { + return false; + } + return bundleManager->CheckApiCompatibility(bundleOption); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/permission_filter.cpp b/services/ans/src/permission_filter.cpp index 9c4a86870..70ef64d60 100644 --- a/services/ans/src/permission_filter.cpp +++ b/services/ans/src/permission_filter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,6 +17,7 @@ #include "ans_inner_errors.h" #include "ans_log_wrapper.h" +#include "bundle_manager_helper.h" #include "notification_preferences.h" namespace OHOS { @@ -34,7 +35,10 @@ ErrCode PermissionFilter::OnPublish(const std::shared_ptr &r NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(record->bundleOption, enable); if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { result = ERR_OK; - enable = true; + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager == nullptr) { + enable = bundleManager->CheckApiCompatibility(record->bundleOption); + } } if (result == ERR_OK) { if (!enable) { 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 6eba48ba6..fa0d3eb61 100644 --- a/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp +++ b/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -46,6 +46,11 @@ bool BundleManagerHelper::IsSystemApp(int uid) return (uid == SYSTEM_APP_UID); } +bool BundleManagerHelper::CheckApiCompatibility(const sptr &bundleOption) +{ + return true; +} + bool BundleManagerHelper::GetBundleInfoByBundleName( const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo) { diff --git a/services/test/moduletest/mock/mock_bundle_manager_helper.cpp b/services/test/moduletest/mock/mock_bundle_manager_helper.cpp index fdeac6723..38de76c01 100644 --- a/services/test/moduletest/mock/mock_bundle_manager_helper.cpp +++ b/services/test/moduletest/mock/mock_bundle_manager_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -40,6 +40,11 @@ bool BundleManagerHelper::IsSystemApp(int uid) return true; } +bool BundleManagerHelper::CheckApiCompatibility(const sptr &bundleOption) +{ + return true; +} + int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId) { return 1000; -- Gitee