From 7a691b9bd011ca163106fa17434e6277b4653993 Mon Sep 17 00:00:00 2001 From: "yangjun (CI)" Date: Thu, 22 May 2025 17:00:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=E9=80=9A=E7=9F=A5=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangjun --- services/ans/BUILD.gn | 1 + .../advanced_notification_query.cpp | 281 ++++++++++++++++++ .../ans/src/advanced_notification_service.cpp | 200 ------------- .../ans/src/advanced_notification_utils.cpp | 54 ---- 4 files changed, 282 insertions(+), 254 deletions(-) create mode 100644 services/ans/src/advanced_notification_manager/advanced_notification_query.cpp diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index aa1d209c1..e534c802d 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -72,6 +72,7 @@ ohos_source_set("ans_service_sources") { "src/advanced_notification_flow_control_service.cpp", "src/advanced_notification_inline.cpp", "src/advanced_notification_live_view_service.cpp", + "src/advanced_notification_manager/advanced_notification_query.cpp", "src/advanced_notification_publish/base_publish_process.cpp", "src/advanced_notification_publish/common_notification_publish_process.cpp", "src/advanced_notification_publish/live_publish_process.cpp", diff --git a/services/ans/src/advanced_notification_manager/advanced_notification_query.cpp b/services/ans/src/advanced_notification_manager/advanced_notification_query.cpp new file mode 100644 index 000000000..1bee13e13 --- /dev/null +++ b/services/ans/src/advanced_notification_manager/advanced_notification_query.cpp @@ -0,0 +1,281 @@ +/* + * Copyright (c) 2025 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "advanced_notification_service.h" + +#include "ans_log_wrapper.h" +#include "ans_permission_def.h" +#include "access_token_helper.h" + +#include "ipc_skeleton.h" + +namespace OHOS { +namespace Notification { +constexpr int32_t RSS_UID = 3051; + +inline bool IsContained(const std::vector &vec, const std::string &target) +{ + bool isContained = false; + + auto iter = vec.begin(); + while (iter != vec.end()) { + if (*iter == target) { + isContained = true; + break; + } + iter++; + } + + return isContained; +} + +ErrCode AdvancedNotificationService::GetActiveNotifications( + std::vector> ¬ifications, const std::string &instanceKey) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + bundleOption->SetAppInstanceKey(instanceKey); + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalidated."); + return ERR_ANS_INVALID_PARAM; + } + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + notifications.clear(); + for (auto record : notificationList_) { + if ((record->bundleOption->GetBundleName() == bundleOption->GetBundleName()) && + (record->bundleOption->GetUid() == bundleOption->GetUid()) && + (record->notification->GetInstanceKey() == bundleOption->GetAppInstanceKey())) { + notifications.push_back(record->request); + } + } + })); + notificationSvrQueue_->wait(handler); + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetActiveNotificationNums(uint64_t &num) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + ANS_LOGE("BundleOption is nullptr."); + return ERR_ANS_INVALID_BUNDLE; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + size_t count = 0; + for (auto record : notificationList_) { + if ((record->bundleOption->GetBundleName() == bundleOption->GetBundleName()) && + (record->bundleOption->GetUid() == bundleOption->GetUid())) { + count += 1; + } + } + num = static_cast(count); + })); + notificationSvrQueue_->wait(handler); + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetAllActiveNotifications(std::vector> ¬ifications) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + int32_t callingUid = IPCSkeleton::GetCallingUid(); + if (callingUid != RSS_UID && !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGE("AccessTokenHelper::CheckPermission failed."); + return ERR_ANS_PERMISSION_DENIED; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalidity."); + return ERR_ANS_INVALID_PARAM; + } + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + notifications.clear(); + for (auto record : notificationList_) { + if (record->notification != nullptr && record->notification->request_ != nullptr) { + notifications.push_back(record->notification); + } + } + })); + notificationSvrQueue_->wait(handler); + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetAllNotificationsBySlotType(std::vector> ¬ifications, + int32_t slotTypeInt) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + NotificationConstant::SlotType slotType = static_cast(slotTypeInt); + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGE("AccessTokenHelper::CheckPermission failed."); + return ERR_ANS_PERMISSION_DENIED; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalidity."); + return ERR_ANS_INVALID_PARAM; + } + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + notifications.clear(); + for (auto record : notificationList_) { + if (record->notification != nullptr && record->notification->request_ != nullptr && + record->notification->request_->GetSlotType() == slotType) { + notifications.push_back(record->notification); + } + } + })); + notificationSvrQueue_->wait(handler); + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetSpecialActiveNotifications( + const std::vector &key, std::vector> ¬ifications) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGE("Check permission is false."); + return ERR_ANS_PERMISSION_DENIED; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + for (auto record : notificationList_) { + if (IsContained(key, record->notification->GetKey())) { + notifications.push_back(record->notification); + } + } + })); + notificationSvrQueue_->wait(handler); + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetActiveNotificationByFilter( + const sptr &bundleOption, int32_t notificationId, const std::string &label, + const std::vector &extraInfoKeys, sptr &request) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + sptr bundle = GenerateValidBundleOption(bundleOption); + if (bundle == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + // get other bundle notification need controller permission + if (bundle->GetUid() == IPCSkeleton::GetCallingUid()) { + ANS_LOGI("Get self notification uid: %{public}d, curUid: %{public}d.", + bundle->GetUid(), IPCSkeleton::GetCallingUid()); + } else { + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGE("Get live view by filter failed because check permission is false."); + return ERR_ANS_PERMISSION_DENIED; + } + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalidity."); + return ERR_ANS_INVALID_PARAM; + } + + ErrCode result = ERR_ANS_NOTIFICATION_NOT_EXISTS; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + + auto record = GetRecordFromNotificationList(notificationId, bundle->GetUid(), label, bundle->GetBundleName()); + if ((record == nullptr) || (!record->request->IsCommonLiveView())) { + return; + } + result = IsAllowedGetNotificationByFilter(record, bundle); + if (result != ERR_OK) { + return; + } + + if (extraInfoKeys.empty()) { + // return all liveViewExtraInfo because no extraInfoKeys + request = record->request; + return; + } + // obtain extraInfo by extraInfoKeys + if (FillRequestByKeys(record->request, extraInfoKeys, request) != ERR_OK) { + return; + } + })); + notificationSvrQueue_->wait(handler); + + return result; +} + +ErrCode AdvancedNotificationService::GetNotificationRequestByHashCode( + const std::string& hashCode, sptr& notificationRequest) +{ + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGE("Check permission is false."); + return ERR_ANS_PERMISSION_DENIED; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + auto record = GetFromNotificationList(hashCode); + if (record != nullptr) { + notificationRequest = record->request; + } + })); + notificationSvrQueue_->wait(handler); + return ERR_OK; +} +} // Notification +} // OHOS \ No newline at end of file diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index fb0ffd5cc..3adf48d22 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1119,34 +1119,6 @@ void AdvancedNotificationService::AddToNotificationList(const std::shared_ptr& notificationRequest) -{ - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - ANS_LOGD("Check permission is false."); - return ERR_ANS_PERMISSION_DENIED; - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalid."); - return ERR_ANS_INVALID_PARAM; - } - - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - auto record = GetFromNotificationList(hashCode); - if (record != nullptr) { - notificationRequest = record->request; - } - })); - notificationSvrQueue_->wait(handler); - return ERR_OK; -} - ErrCode AdvancedNotificationService::UpdateInNotificationList(const std::shared_ptr &record) { auto iter = notificationList_.begin(); @@ -1205,65 +1177,6 @@ void AdvancedNotificationService::StopFilters() } } -ErrCode AdvancedNotificationService::GetActiveNotifications( - std::vector> ¬ifications, const std::string &instanceKey) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - sptr bundleOption = GenerateBundleOption(); - if (bundleOption == nullptr) { - return ERR_ANS_INVALID_BUNDLE; - } - bundleOption->SetAppInstanceKey(instanceKey); - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalidated."); - return ERR_ANS_INVALID_PARAM; - } - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - notifications.clear(); - for (auto record : notificationList_) { - if ((record->bundleOption->GetBundleName() == bundleOption->GetBundleName()) && - (record->bundleOption->GetUid() == bundleOption->GetUid()) && - (record->notification->GetInstanceKey() == bundleOption->GetAppInstanceKey())) { - notifications.push_back(record->request); - } - } - })); - notificationSvrQueue_->wait(handler); - return ERR_OK; -} - -ErrCode AdvancedNotificationService::GetActiveNotificationNums(uint64_t &num) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - sptr bundleOption = GenerateBundleOption(); - if (bundleOption == nullptr) { - ANS_LOGD("BundleOption is nullptr."); - return ERR_ANS_INVALID_BUNDLE; - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalid."); - return ERR_ANS_INVALID_PARAM; - } - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - size_t count = 0; - for (auto record : notificationList_) { - if ((record->bundleOption->GetBundleName() == bundleOption->GetBundleName()) && - (record->bundleOption->GetUid() == bundleOption->GetUid())) { - count += 1; - } - } - num = static_cast(count); - })); - notificationSvrQueue_->wait(handler); - return ERR_OK; -} - ErrCode AdvancedNotificationService::GetBundleImportance(int32_t &importance) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -1556,119 +1469,6 @@ std::shared_ptr AdvancedNotificationService::GetFromDelayedN return nullptr; } -ErrCode AdvancedNotificationService::GetAllActiveNotifications(std::vector> ¬ifications) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - return ERR_ANS_NON_SYSTEM_APP; - } - - int32_t callingUid = IPCSkeleton::GetCallingUid(); - if (callingUid != RSS_UID && !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - ANS_LOGW("AccessTokenHelper::CheckPermission failed."); - return ERR_ANS_PERMISSION_DENIED; - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalidity."); - return ERR_ANS_INVALID_PARAM; - } - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - notifications.clear(); - for (auto record : notificationList_) { - if (record->notification != nullptr && record->notification->request_ != nullptr) { - notifications.push_back(record->notification); - } - } - })); - notificationSvrQueue_->wait(handler); - return ERR_OK; -} - -ErrCode AdvancedNotificationService::GetAllNotificationsBySlotType(std::vector> ¬ifications, - int32_t slotTypeInt) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - NotificationConstant::SlotType slotType = static_cast(slotTypeInt); - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - ANS_LOGW("AccessTokenHelper::CheckPermission failed."); - return ERR_ANS_PERMISSION_DENIED; - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalidity."); - return ERR_ANS_INVALID_PARAM; - } - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - notifications.clear(); - for (auto record : notificationList_) { - if (record->notification != nullptr && record->notification->request_ != nullptr && - record->notification->request_->GetSlotType() == slotType) { - notifications.push_back(record->notification); - } - } - })); - notificationSvrQueue_->wait(handler); - return ERR_OK; -} - -inline bool IsContained(const std::vector &vec, const std::string &target) -{ - bool isContained = false; - - auto iter = vec.begin(); - while (iter != vec.end()) { - if (*iter == target) { - isContained = true; - break; - } - iter++; - } - - return isContained; -} - -ErrCode AdvancedNotificationService::GetSpecialActiveNotifications( - const std::vector &key, std::vector> ¬ifications) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - ANS_LOGD("Check permission is false."); - return ERR_ANS_PERMISSION_DENIED; - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalid."); - return ERR_ANS_INVALID_PARAM; - } - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - for (auto record : notificationList_) { - if (IsContained(key, record->notification->GetKey())) { - notifications.push_back(record->notification); - } - } - })); - notificationSvrQueue_->wait(handler); - return ERR_OK; -} - std::shared_ptr AdvancedNotificationService::GetRecordFromNotificationList( int32_t notificationId, int32_t uid, const std::string &label, const std::string &bundleName) { diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index 327c53182..3119041cc 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -278,60 +278,6 @@ ErrCode AdvancedNotificationService::IsAllowedGetNotificationByFilter( return ERR_ANS_PERMISSION_DENIED; } -ErrCode AdvancedNotificationService::GetActiveNotificationByFilter( - const sptr &bundleOption, int32_t notificationId, const std::string &label, - const std::vector &extraInfoKeys, sptr &request) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - ANS_LOGD("%{public}s", __FUNCTION__); - sptr bundle = GenerateValidBundleOption(bundleOption); - if (bundle == nullptr) { - return ERR_ANS_INVALID_BUNDLE; - } - // get other bundle notification need controller permission - if (bundle->GetUid() == IPCSkeleton::GetCallingUid()) { - ANS_LOGI("Get self notification uid: %{public}d, curUid: %{public}d.", - bundle->GetUid(), IPCSkeleton::GetCallingUid()); - } else { - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - ANS_LOGE("Get live view by filter failed because check permission is false."); - return ERR_ANS_PERMISSION_DENIED; - } - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalidity."); - return ERR_ANS_INVALID_PARAM; - } - - ErrCode result = ERR_ANS_NOTIFICATION_NOT_EXISTS; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - - auto record = GetRecordFromNotificationList(notificationId, bundle->GetUid(), label, bundle->GetBundleName()); - if ((record == nullptr) || (!record->request->IsCommonLiveView())) { - return; - } - result = IsAllowedGetNotificationByFilter(record, bundle); - if (result != ERR_OK) { - return; - } - - if (extraInfoKeys.empty()) { - // return all liveViewExtraInfo because no extraInfoKeys - request = record->request; - return; - } - // obtain extraInfo by extraInfoKeys - if (FillRequestByKeys(record->request, extraInfoKeys, request) != ERR_OK) { - return; - } - })); - notificationSvrQueue_->wait(handler); - - return result; -} - void AdvancedNotificationService::SetAgentNotification(sptr& notificationRequest, std::string& bundleName) { -- Gitee