From 032817961e0de49250ecc397e13255023e6d3fae Mon Sep 17 00:00:00 2001 From: guxiang Date: Tue, 27 May 2025 20:19:02 +0800 Subject: [PATCH] split service|add local_live_view_service Signed-off-by: guxiang --- interfaces/inner_api/notification_constant.h | 2 + services/ans/BUILD.gn | 2 + .../include/advanced_notification_service.h | 13 + .../advanced_notification_cancel.cpp | 916 ++++++++++++++ .../advanced_notification_publish_service.cpp | 1117 +---------------- ..._notification_system_live_view_service.cpp | 199 +++ 6 files changed, 1191 insertions(+), 1058 deletions(-) create mode 100644 services/ans/src/advanced_notification_manager/advanced_notification_cancel.cpp create mode 100644 services/ans/src/system_live_view/advanced_notification_system_live_view_service.cpp diff --git a/interfaces/inner_api/notification_constant.h b/interfaces/inner_api/notification_constant.h index 909b908e4..a33fcc3c8 100644 --- a/interfaces/inner_api/notification_constant.h +++ b/interfaces/inner_api/notification_constant.h @@ -349,6 +349,8 @@ public: static constexpr uint64_t INVALID_TIMER_ID = 0ULL; + static constexpr int32_t ANS_UID = 5523; + // live view max size is 512KB(extra size) + 8KB(base size) = 520KB static constexpr uint64_t NOTIFICATION_MAX_LIVE_VIEW_SIZE = 520ULL * 1024ULL; diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index 59ff0def9..b7248f129 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -67,6 +67,7 @@ ohos_source_set("ans_service_sources") { "src/advanced_datashare_helper.cpp", "src/advanced_datashare_helper_ext.cpp", "src/advanced_datashare_observer.cpp", + "src/advanced_notification_manager/advanced_notification_cancel.cpp", "src/advanced_notification_clone_service.cpp", "src/advanced_notification_event_service.cpp", "src/advanced_notification_flow_control_service.cpp", @@ -133,6 +134,7 @@ ohos_source_set("ans_service_sources") { "src/report_time_info.cpp", "src/system_dialog_connect_stb.cpp", "src/system_event_observer.cpp", + "src/system_live_view/advanced_notification_system_live_view_service.cpp", "src/telephony_extension_wrapper.cpp", "src/utils/notifictaion_load_utils.cpp", ] diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index a36a3dcf7..e8003e2db 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1523,6 +1523,19 @@ private: void RemoveDoNotDisturbProfileTrustList(const sptr &bundleOption); ErrCode DeleteAllByUserInner(const int32_t &userId, int32_t reason, bool isAsync = false, bool removeAll = false); ErrCode RemoveAllNotificationsInner(const sptr &bundleOption, int32_t reason); + void ExcuteRemoveAllNotificationsInner(const sptr &bundleOption, + const sptr &bundle, int32_t &reason); + void GetRemoveListForRemoveAll(const sptr &bundleOption, + const sptr &bundle, std::vector> &removeList); + ErrCode ValidRightsForCancelAsBundle(int32_t notificationId, int32_t &reason); + ErrCode ExcuteRemoveNotification(const sptr &bundle, + int32_t notificationId, const std::string &label, int32_t &removeReason); + void ExcuteRemoveNotifications(const std::vector &keys, int32_t removeReason); + void GetRemoveListForRemoveNtfBySlot(const sptr &bundle, + const sptr &slot, std::vector> &removeList); + void ExcuteDeleteAll(ErrCode &result, const int32_t reason); + ErrCode GetNotificationById(const sptr &bundle, + const int32_t notificationId, sptr ¬ification); ErrCode AssignValidNotificationSlot(const std::shared_ptr &record, const sptr &bundleOption); ErrCode UpdateSlotReminderModeBySlotFlags(const sptr &bundle, uint32_t slotFlags); diff --git a/services/ans/src/advanced_notification_manager/advanced_notification_cancel.cpp b/services/ans/src/advanced_notification_manager/advanced_notification_cancel.cpp new file mode 100644 index 000000000..9b3d35d32 --- /dev/null +++ b/services/ans/src/advanced_notification_manager/advanced_notification_cancel.cpp @@ -0,0 +1,916 @@ +/* + * Copyright (c) 2025-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 "access_token_helper.h" +#include "ans_permission_def.h" +#include "bundle_manager_helper.h" +#include "hitrace_meter_adapter.h" +#include "ipc_skeleton.h" +#include "notification_analytics_util.h" +#include "notification_bundle_option.h" +#include "notification_constant.h" +#include "notification_local_live_view_content.h" +#include "notification_subscriber_manager.h" +#include "os_account_manager.h" + +#include "../advanced_notification_inline.cpp" + +namespace OHOS { +namespace Notification { +ErrCode AdvancedNotificationService::Cancel(int32_t notificationId, + const std::string &label, const std::string &instanceKey) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + std::string message = "get bundleOption is null."; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(1, 1) + .ErrorCode(ERR_ANS_INVALID_BUNDLE).NotificationId(notificationId); + ReportDeleteFailedEventPush(haMetaMessage, NotificationConstant::APP_CANCEL_REASON_DELETE, + message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_INVALID_BUNDLE; + } + bundleOption->SetAppInstanceKey(instanceKey); + return CancelPreparedNotification(notificationId, label, bundleOption, + NotificationConstant::APP_CANCEL_REASON_DELETE); +} + +ErrCode AdvancedNotificationService::CancelAll(const std::string &instanceKey) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + const int reason = NotificationConstant::APP_CANCEL_ALL_REASON_DELETE; + 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; + } + ErrCode result = ExcuteCancelAll(bundleOption, reason); + return result; +} + +ErrCode AdvancedNotificationService::ExcuteCancelAll( + const sptr& bundleOption, const int32_t reason) +{ + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + sptr notification = nullptr; + + std::vector keys = GetNotificationKeysByBundle(bundleOption); + std::vector> notifications; + std::vector timerIds; + for (auto key : keys) { +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + std::string deviceId; + std::string bundleName; + GetDistributedInfo(key, deviceId, bundleName); +#endif + result = RemoveFromNotificationList(key, notification, true, reason); + if (result != ERR_OK) { + continue; + } + + if (notification != nullptr) { + UpdateRecentNotification(notification, true, reason); + notifications.emplace_back(notification); + timerIds.emplace_back(notification->GetAutoDeletedTimer()); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + DoDistributedDelete(deviceId, bundleName, notification); +#endif + } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + std::vector> currNotificationList = notifications; + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + currNotificationList, nullptr, reason); + notifications.clear(); + } + } + + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + notifications, nullptr, reason); + } + BatchCancelTimer(timerIds); + result = ERR_OK; + })); + notificationSvrQueue_->wait(handler); + return result; +} + +ErrCode AdvancedNotificationService::CancelAsBundle( + const sptr &bundleOption, int32_t notificationId, int32_t userId) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + int32_t reason = NotificationConstant::APP_CANCEL_AS_BUNELE_REASON_DELETE; + if (bundleOption == nullptr) { + ANS_LOGE("bundleOption is invalid"); + return ERR_ANS_INVALID_PARAM; + } + int32_t errCode = ValidRightsForCancelAsBundle(notificationId, reason); + if (errCode != ERR_OK) { + return errCode; + } + errCode = CheckUserIdParams(userId); + if (errCode != ERR_OK) { + std::string message = "userId:" + std::to_string(userId); + HaMetaMessage haMateMessage = HaMetaMessage(EventSceneId::SCENE_13, EventBranchId::BRANCH_14) + .ErrorCode(errCode).NotificationId(notificationId); + ReportDeleteFailedEventPush(haMateMessage, reason, message); + return errCode; + } + + int32_t uid = -1; + if (bundleOption->GetUid() == DEFAULT_UID) { + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager != nullptr) { + uid = BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleOption->GetBundleName(), userId); + } + } else { + uid = bundleOption->GetUid(); + } + if (uid < 0) { + std::string message = "uid error"; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(2, 3) + .ErrorCode(ERR_ANS_INVALID_UID).NotificationId(notificationId); + ReportDeleteFailedEventPush(haMetaMessage, reason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_INVALID_UID; + } + sptr bundle = new (std::nothrow) NotificationBundleOption( + bundleOption->GetBundleName(), uid); + return CancelPreparedNotification(notificationId, "", bundle, reason); +} + +ErrCode AdvancedNotificationService::ValidRightsForCancelAsBundle(int32_t notificationId, int32_t &reason) +{ + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + std::string message = "not systemApp"; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(2, 1) + .ErrorCode(ERR_ANS_NON_SYSTEM_APP).NotificationId(notificationId); + ReportDeleteFailedEventPush(haMetaMessage, reason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER) || + !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) { + std::string message = "no acl permission"; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(2, 2) + .ErrorCode(ERR_ANS_PERMISSION_DENIED).NotificationId(notificationId); + ReportDeleteFailedEventPush(haMetaMessage, reason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_PERMISSION_DENIED; + } + return ERR_OK; +} + +ErrCode AdvancedNotificationService::CancelAsBundle( + const sptr &bundleOption, int32_t notificationId) +{ + ANS_LOGD("%{public}s, uid = %{public}d", __FUNCTION__, bundleOption->GetUid()); + int32_t userId = -1; + if (bundleOption->GetUid() != 0) { + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), userId); + } else { + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(IPCSkeleton::GetCallingUid(), userId); + } + return CancelAsBundle(bundleOption, notificationId, userId); +} + +ErrCode AdvancedNotificationService::CancelAsBundle( + int32_t notificationId, const std::string &representativeBundle, int32_t userId) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + sptr bundleOption = new (std::nothrow) NotificationBundleOption( + representativeBundle, DEFAULT_UID); + if (bundleOption == nullptr) { + ANS_LOGE("bundleOption is nullptr."); + return ERR_ANS_TASK_ERR; + } + return CancelAsBundle(bundleOption, notificationId, userId); +} + +ErrCode AdvancedNotificationService::CancelAsBundleWithAgent( + const sptr &bundleOption, const int32_t id) +{ + ANS_LOGD("Called."); + int32_t reason = NotificationConstant::APP_CANCEL_AS_BUNELE_WITH_AGENT_REASON_DELETE; + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + std::string message = "not systemApp"; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(2, 4) + .ErrorCode(ERR_ANS_NON_SYSTEM_APP).NotificationId(id); + ReportDeleteFailedEventPush(haMetaMessage, reason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_NON_SYSTEM_APP; + } + + if (IsAgentRelationship(GetClientBundleName(), bundleOption->GetBundleName())) { + int32_t userId = -1; + if (bundleOption->GetUid() != 0) { + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), userId); + } else { + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(IPCSkeleton::GetCallingUid(), userId); + } + int32_t uid = -1; + if (bundleOption->GetUid() == DEFAULT_UID) { + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager != nullptr) { + uid = BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName( + bundleOption->GetBundleName(), userId); + } + } else { + uid = bundleOption->GetUid(); + } + if (uid < 0) { + std::string message = "uid error"; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(2, 5) + .ErrorCode(ERR_ANS_INVALID_UID).NotificationId(id); + ReportDeleteFailedEventPush(haMetaMessage, reason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_INVALID_UID; + } + sptr bundle = new (std::nothrow) NotificationBundleOption( + bundleOption->GetBundleName(), uid); + return CancelPreparedNotification(id, "", bundle, reason); + } + std::string message = "no agent setting"; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(2, 6) + .ErrorCode(ERR_ANS_NO_AGENT_SETTING).NotificationId(id); + ReportDeleteFailedEventPush(haMetaMessage, reason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_NO_AGENT_SETTING; +} + +ErrCode AdvancedNotificationService::CancelContinuousTaskNotification(const std::string &label, int32_t notificationId) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem) { + return ERR_ANS_NOT_SYSTEM_SERVICE; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + int32_t uid = IPCSkeleton::GetCallingUid(); + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + sptr notification = nullptr; + for (auto record : notificationList_) { + if ((record->bundleOption->GetBundleName().empty()) && (record->bundleOption->GetUid() == uid) && + (record->notification->GetId() == notificationId) && (record->notification->GetLabel() == label)) { + notification = record->notification; + notificationList_.remove(record); + result = ERR_OK; + break; + } + } + if (notification != nullptr) { + int32_t reason = NotificationConstant::APP_CANCEL_REASON_DELETE; + UpdateRecentNotification(notification, true, reason); + CancelTimer(notification->GetAutoDeletedTimer()); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, reason); + } + })); + notificationSvrQueue_->wait(handler); + return result; +} + +ErrCode AdvancedNotificationService::RemoveNotification(const sptr &bundleOption, + int32_t notificationId, const std::string &label, int32_t removeReason) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + std::string message = "not systemApp."; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(4, 4) + .ErrorCode(ERR_ANS_NON_SYSTEM_APP).NotificationId(notificationId); + ReportDeleteFailedEventPush(haMetaMessage, removeReason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + std::string message = "no acl controller permission."; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(4, 5) + .ErrorCode(ERR_ANS_PERMISSION_DENIED).NotificationId(notificationId); + ReportDeleteFailedEventPush(haMetaMessage, removeReason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_PERMISSION_DENIED; + } + + sptr bundle = GenerateValidBundleOption(bundleOption); + if (bundle == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + + if (notificationSvrQueue_ == nullptr) { + std::string message = "NotificationSvrQueue_ is null."; + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_INVALID_PARAM; + } + ErrCode result = ERR_ANS_NOTIFICATION_NOT_EXISTS; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + result = ExcuteRemoveNotification(bundle, notificationId, label, removeReason); + })); + notificationSvrQueue_->wait(handler); + if (result != ERR_OK) { + std::string message = "remove notificaiton error"; + ANS_LOGE("%{public}s %{public}d", message.c_str(), result); + } + SendRemoveHiSysEvent(notificationId, label, bundleOption, result); + return result; +} + +ErrCode AdvancedNotificationService::ExcuteRemoveNotification(const sptr &bundle, + int32_t notificationId, const std::string &label, int32_t &removeReason) +{ + ErrCode result = ERR_ANS_NOTIFICATION_NOT_EXISTS; + ANS_LOGD("ffrt enter!"); + bool isThirdParty = true; + sptr notification = nullptr; + sptr notificationRequest = nullptr; + +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + std::string deviceId; + std::string bundleName; +#endif + for (auto record : notificationList_) { + if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) && + (record->bundleOption->GetUid() == bundle->GetUid()) && +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + (record->deviceId.empty()) && +#endif + (record->notification->GetId() == notificationId) && (record->notification->GetLabel() == label)) { + if (!record->notification->IsRemoveAllowed()) { + result = ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED; + break; + } +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + deviceId = record->deviceId; + bundleName = record->bundleName; +#endif + notification = record->notification; + notificationRequest = record->request; + isThirdParty = record->isThirdparty; + + if (removeReason != NotificationConstant::CLICK_REASON_DELETE) { + ProcForDeleteLiveView(record); + } + + notificationList_.remove(record); + result = ERR_OK; + break; + } + } + + if (notification != nullptr) { + UpdateRecentNotification(notification, true, removeReason); + CancelTimer(notification->GetAutoDeletedTimer()); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, removeReason); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + DoDistributedDelete(deviceId, bundleName, notification); +#endif + } + if (removeReason != NotificationConstant::CLICK_REASON_DELETE) { + TriggerRemoveWantAgent(notificationRequest, removeReason, isThirdParty); + } + return result; +} + +ErrCode AdvancedNotificationService::RemoveAllNotificationsForDisable( + const sptr &bundleOption) +{ + return RemoveAllNotificationsInner(bundleOption, NotificationConstant::DISABLE_NOTIFICATION_REASON_DELETE); +} + +ErrCode AdvancedNotificationService::RemoveAllNotifications(const sptr &bundleOption) +{ + return RemoveAllNotificationsInner(bundleOption, NotificationConstant::APP_REMOVE_ALL_REASON_DELETE); +} + +ErrCode AdvancedNotificationService::RemoveAllNotificationsInner(const sptr &bundleOption, + int32_t reason) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + std::string message = "not system app."; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(6, 1) + .ErrorCode(ERR_ANS_NON_SYSTEM_APP); + ReportDeleteFailedEventPush(haMetaMessage, reason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_NON_SYSTEM_APP; + } + + int32_t callingUid = IPCSkeleton::GetCallingUid(); + if (callingUid != NotificationConstant::ANS_UID && + !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + std::string message = "no acl permission."; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(6, 2) + .ErrorCode(ERR_ANS_PERMISSION_DENIED); + ReportDeleteFailedEventPush(haMetaMessage, reason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_PERMISSION_DENIED; + } + + sptr bundle = GenerateValidBundleOption(bundleOption); + if (bundle == nullptr) { + std::string message = "budle is nullptr."; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(6, 3) + .ErrorCode(ERR_ANS_INVALID_BUNDLE); + ReportDeleteFailedEventPush(haMetaMessage, reason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_INVALID_BUNDLE; + } + + if (notificationSvrQueue_ == nullptr) { + std::string message = "Serial queue is nullptr."; + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_INVALID_PARAM; + } + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ExcuteRemoveAllNotificationsInner(bundleOption, bundle, reason); + })); + notificationSvrQueue_->wait(handler); + + return ERR_OK; +} + +void AdvancedNotificationService::ExcuteRemoveAllNotificationsInner(const sptr &bundleOption, + const sptr &bundle, int32_t &reason) +{ + std::vector> removeList; + ANS_LOGD("ffrt enter!"); + GetRemoveListForRemoveAll(bundleOption, bundle, removeList); + std::vector> notifications; + std::vector timerIds; + for (auto record : removeList) { + notificationList_.remove(record); + if (record->notification != nullptr) { + ANS_LOGD("record->notification is not nullptr."); + UpdateRecentNotification(record->notification, true, reason); + notifications.emplace_back(record->notification); + timerIds.emplace_back(record->notification->GetAutoDeletedTimer()); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + DoDistributedDelete(record->deviceId, record->bundleName, record->notification); +#endif + } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + SendNotificationsOnCanceled(notifications, nullptr, reason); + } + + TriggerRemoveWantAgent(record->request, reason, record->isThirdparty); + } + + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(notifications, nullptr, reason); + } + BatchCancelTimer(timerIds); +} + +void AdvancedNotificationService::GetRemoveListForRemoveAll(const sptr &bundleOption, + const sptr &bundle, std::vector> &removeList) +{ + for (auto record : notificationList_) { + bool isAllowedNotification = true; + if (IsAllowedNotifyForBundle(bundleOption, isAllowedNotification) != ERR_OK) { + ANSR_LOGW("The application does not request enable notification."); + } + if (!record->notification->IsRemoveAllowed() && isAllowedNotification) { + ANS_LOGI("BatchRemove-FILTER-RemoveNotAllowed-%{public}s", record->notification->GetKey().c_str()); + continue; + } + if (record->slot != nullptr && record->slot->GetForceControl() && record->slot->GetEnable()) { + ANS_LOGI("BatchRemove-FILTER-ForceControl-%{public}s", record->notification->GetKey().c_str()); + continue; + } + if (record->bundleOption->GetBundleName() != bundle->GetBundleName() || + record->bundleOption->GetUid() != bundle->GetUid() +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + || !record->deviceId.empty() +#endif + ) { + continue; + } + auto notificationRequest = record->request; + if (!BundleManagerHelper::GetInstance()->IsSystemApp(bundle->GetUid()) && + notificationRequest->IsSystemLiveView()) { + auto localLiveviewContent = std::static_pointer_cast( + notificationRequest->GetContent()->GetNotificationContent()); + if (localLiveviewContent->GetType() == 0) { + continue; + } + } + ProcForDeleteLiveView(record); + removeList.push_back(record); + } +} + +ErrCode AdvancedNotificationService::RemoveNotifications( + const std::vector &keys, int32_t removeReason) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + ANS_LOGD("enter"); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("NotificationSvrQueue is nullptr."); + return ERR_ANS_INVALID_PARAM; + } + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ExcuteRemoveNotifications(keys, removeReason); + })); + notificationSvrQueue_->wait(handler); + + return ERR_OK; +} + +void AdvancedNotificationService::ExcuteRemoveNotifications(const std::vector &keys, int32_t removeReason) +{ + std::vector> notifications; + std::vector timerIds; + for (auto key : keys) { + sptr notification = nullptr; +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + std::string deviceId; + std::string bundleName; + GetDistributedInfo(key, deviceId, bundleName); +#endif + ErrCode result = RemoveFromNotificationList(key, notification, false, removeReason); + if (result != ERR_OK) { + continue; + } + if (notification != nullptr) { + UpdateRecentNotification(notification, true, removeReason); + notifications.emplace_back(notification); + timerIds.emplace_back(notification->GetAutoDeletedTimer()); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + DoDistributedDelete(deviceId, bundleName, notification); +#endif + } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + std::vector> currNotificationList = notifications; + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + currNotificationList, nullptr, removeReason); + notifications.clear(); + } + } + + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(notifications, nullptr, removeReason); + } + BatchCancelTimer(timerIds); +} + +ErrCode AdvancedNotificationService::RemoveNotificationBySlot(const sptr &bundleOption, + const sptr &slot, const int reason) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + sptr bundle = GenerateValidBundleOption(bundleOption); + if (bundle == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + ErrCode result = ERR_ANS_NOTIFICATION_NOT_EXISTS; + std::vector> removeList; + GetRemoveListForRemoveNtfBySlot(bundle, slot, removeList); + + std::vector> notifications; + std::vector timerIds; + for (auto record : removeList) { + if (record == nullptr) { + ANS_LOGE("record is nullptr"); + continue; + } + notificationList_.remove(record); + if (record->notification != nullptr) { + ANS_LOGD("record->notification is not nullptr."); + UpdateRecentNotification(record->notification, true, reason); + notifications.emplace_back(record->notification); + timerIds.emplace_back(record->notification->GetAutoDeletedTimer()); + } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + SendNotificationsOnCanceled(notifications, nullptr, reason); + } + + TriggerRemoveWantAgent(record->request, reason, record->isThirdparty); + result = ERR_OK; + } + + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(notifications, nullptr, reason); + } + BatchCancelTimer(timerIds); + return result; +} + +void AdvancedNotificationService::GetRemoveListForRemoveNtfBySlot(const sptr &bundle, + const sptr &slot, std::vector> &removeList) +{ + for (auto record : notificationList_) { + if (record == nullptr) { + ANS_LOGE("record is nullptr"); + continue; + } + if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) && + (record->bundleOption->GetUid() == bundle->GetUid()) && + (record->request->GetSlotType() == slot->GetType())) { + if ((record->request->GetAgentBundle() != nullptr && record->request->IsSystemLiveView())) { + ANS_LOGW("Agent systemliveview no need remove."); + continue; + } + ProcForDeleteLiveView(record); + removeList.push_back(record); + } + } +} + +ErrCode AdvancedNotificationService::RemoveNotificationFromRecordList( + const std::vector>& recordList) +{ + ErrCode result = ERR_OK; + std::vector> notifications; + std::vector timerIds; + for (auto& record : recordList) { + std::string key = record->notification->GetKey(); + sptr notification = nullptr; +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + std::string deviceId; + std::string bundleName; + GetDistributedInfo(key, deviceId, bundleName); +#endif + result = RemoveFromNotificationList(key, notification, true, + NotificationConstant::USER_STOPPED_REASON_DELETE); + if (result != ERR_OK) { + continue; + } + if (notification != nullptr) { + int32_t reason = NotificationConstant::USER_STOPPED_REASON_DELETE; + UpdateRecentNotification(notification, true, reason); + notifications.emplace_back(notification); + timerIds.emplace_back(notification->GetAutoDeletedTimer()); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + DoDistributedDelete(deviceId, bundleName, notification); +#endif + } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + std::vector> currNotificationList = notifications; + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + currNotificationList, nullptr, NotificationConstant::USER_STOPPED_REASON_DELETE); + notifications.clear(); + } + } + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + notifications, nullptr, NotificationConstant::USER_STOPPED_REASON_DELETE); + } + BatchCancelTimer(timerIds); + return result; +} + +ErrCode AdvancedNotificationService::Delete(const std::string &key, int32_t removeReason) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + std::string message = "not systemApp. key:" + key + "."; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(4, 1) + .ErrorCode(ERR_ANS_NON_SYSTEM_APP); + ReportDeleteFailedEventPush(haMetaMessage, removeReason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + std::string message = "no acl permission. key:" + key + "."; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(4, 2) + .ErrorCode(ERR_ANS_PERMISSION_DENIED); + ReportDeleteFailedEventPush(haMetaMessage, removeReason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_PERMISSION_DENIED; + } + + if (notificationSvrQueue_ == nullptr) { + std::string message = "Serial queue is invalidated. key:" + key + "."; + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_INVALID_PARAM; + } + + return ExcuteDelete(key, removeReason); +} + +ErrCode AdvancedNotificationService::ExcuteDelete(const std::string &key, const int32_t removeReason) +{ + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + sptr notification = nullptr; +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + std::string deviceId; + std::string bundleName; + GetDistributedInfo(key, deviceId, bundleName); +#endif + result = RemoveFromNotificationList(key, notification, false, removeReason); + if (result != ERR_OK) { + return; + } + + if (notification != nullptr) { + UpdateRecentNotification(notification, true, removeReason); + CancelTimer(notification->GetAutoDeletedTimer()); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, removeReason); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + DoDistributedDelete(deviceId, bundleName, notification); +#endif + } + })); + notificationSvrQueue_->wait(handler); + + return result; +} + +ErrCode AdvancedNotificationService::DeleteByBundle(const sptr &bundleOption) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGD("VerifyNativeToken is false."); + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + + sptr bundle = GenerateValidBundleOption(bundleOption); + if (bundle == nullptr) { + ANS_LOGD("bundle is false."); + return ERR_ANS_INVALID_BUNDLE; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + std::vector keys = GetNotificationKeys(bundle); + for (auto key : keys) { +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + std::string deviceId; + std::string bundleName; + GetDistributedInfo(key, deviceId, bundleName); +#endif + sptr notification = nullptr; + + result = RemoveFromNotificationList(key, notification, false, NotificationConstant::CANCEL_REASON_DELETE); + if (result != ERR_OK) { + continue; + } + + if (notification != nullptr) { + int32_t reason = NotificationConstant::CANCEL_REASON_DELETE; + UpdateRecentNotification(notification, true, reason); + CancelTimer(notification->GetAutoDeletedTimer()); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, reason); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + DoDistributedDelete(deviceId, bundleName, notification); +#endif + } + } + + result = ERR_OK; + })); + notificationSvrQueue_->wait(handler); + + return result; +} + +ErrCode AdvancedNotificationService::DeleteAll() +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + const int32_t reason = NotificationConstant::CANCEL_ALL_REASON_DELETE; + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + std::string message = "not system app."; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(6, 8) + .ErrorCode(ERR_ANS_NON_SYSTEM_APP); + ReportDeleteFailedEventPush(haMetaMessage, reason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + std::string message = "no acl permission."; + OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(6, 9) + .ErrorCode(ERR_ANS_PERMISSION_DENIED); + ReportDeleteFailedEventPush(haMetaMessage, reason, message); + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_PERMISSION_DENIED; + } + + if (notificationSvrQueue_ == nullptr) { + std::string message = "Serial queue is invalidity."; + ANS_LOGE("%{public}s", message.c_str()); + return ERR_ANS_INVALID_PARAM; + } + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ExcuteDeleteAll(result, reason); + })); + notificationSvrQueue_->wait(handler); + + return result; +} + +void AdvancedNotificationService::ExcuteDeleteAll(ErrCode &result, const int32_t reason) +{ + ANS_LOGD("ffrt enter!"); + int32_t activeUserId = SUBSCRIBE_USER_INIT; + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(activeUserId) != ERR_OK) { + return; + } + std::vector keys = GetNotificationKeys(nullptr); + std::vector> notifications; + std::vector timerIds; + for (auto key : keys) { +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + std::string deviceId; + std::string bundleName; + GetDistributedInfo(key, deviceId, bundleName); +#endif + sptr notification = nullptr; + + result = RemoveFromNotificationListForDeleteAll(key, activeUserId, notification); + if ((result != ERR_OK) || (notification == nullptr)) { + continue; + } + + if (notification->GetUserId() == activeUserId) { + UpdateRecentNotification(notification, true, reason); + notifications.emplace_back(notification); + timerIds.emplace_back(notification->GetAutoDeletedTimer()); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + DoDistributedDelete(deviceId, bundleName, notification); +#endif + } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + ANS_LOGD("Notifications size greater than or equal to MAX_CANCELED_PARCELABLE_VECTOR_NUM."); + SendNotificationsOnCanceled(notifications, nullptr, reason); + } + } + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + notifications, nullptr, reason); + } + BatchCancelTimer(timerIds); + result = ERR_OK; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index 3c209722c..e15e38b4e 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -67,7 +67,6 @@ constexpr uint32_t SECONDS_IN_ONE_DAY = 24 * 60 * 60; const static std::string NOTIFICATION_EVENT_PUSH_AGENT = "notification.event.PUSH_AGENT"; const static std::string NOTIFICATION_EVENT_SUBSCRIBER_STATUS = "notification.event.SUBSCRIBER_STATUS"; constexpr int32_t RSS_PID = 3051; -constexpr int32_t ANS_UID = 5523; constexpr int32_t AVSEESAION_PID = 6700; constexpr int32_t TYPE_CODE_DOWNLOAD = 8; constexpr const char *FOCUS_MODE_REPEAT_CALLERS_ENABLE = "1"; @@ -460,231 +459,6 @@ bool AdvancedNotificationService::InitPublishProcess() return true; } -ErrCode AdvancedNotificationService::Cancel(int32_t notificationId, - const std::string &label, const std::string &instanceKey) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - sptr bundleOption = GenerateBundleOption(); - if (bundleOption == nullptr) { - std::string message = "get bundleOption is null."; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(1, 1) - .ErrorCode(ERR_ANS_INVALID_BUNDLE).NotificationId(notificationId); - ReportDeleteFailedEventPush(haMetaMessage, NotificationConstant::APP_CANCEL_REASON_DELETE, - message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_INVALID_BUNDLE; - } - bundleOption->SetAppInstanceKey(instanceKey); - return CancelPreparedNotification(notificationId, label, bundleOption, - NotificationConstant::APP_CANCEL_REASON_DELETE); -} - -ErrCode AdvancedNotificationService::CancelAll(const std::string &instanceKey) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - const int reason = NotificationConstant::APP_CANCEL_ALL_REASON_DELETE; - 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; - } - ErrCode result = ExcuteCancelAll(bundleOption, reason); - return result; -} - -ErrCode AdvancedNotificationService::ExcuteCancelAll( - const sptr& bundleOption, const int32_t reason) -{ - ErrCode result = ERR_OK; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - sptr notification = nullptr; - - std::vector keys = GetNotificationKeysByBundle(bundleOption); - std::vector> notifications; - std::vector timerIds; - for (auto key : keys) { -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - std::string deviceId; - std::string bundleName; - GetDistributedInfo(key, deviceId, bundleName); -#endif - result = RemoveFromNotificationList(key, notification, true, reason); - if (result != ERR_OK) { - continue; - } - - if (notification != nullptr) { - UpdateRecentNotification(notification, true, reason); - notifications.emplace_back(notification); - timerIds.emplace_back(notification->GetAutoDeletedTimer()); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - DoDistributedDelete(deviceId, bundleName, notification); -#endif - } - if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { - std::vector> currNotificationList = notifications; - NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( - currNotificationList, nullptr, reason); - notifications.clear(); - } - } - - if (!notifications.empty()) { - NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( - notifications, nullptr, reason); - } - BatchCancelTimer(timerIds); - result = ERR_OK; - })); - notificationSvrQueue_->wait(handler); - return result; -} - -ErrCode AdvancedNotificationService::CancelAsBundle( - const sptr &bundleOption, int32_t notificationId, int32_t userId) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - int32_t reason = NotificationConstant::APP_CANCEL_AS_BUNELE_REASON_DELETE; - if (bundleOption == nullptr) { - ANS_LOGE("bundleOption is invalid"); - return ERR_ANS_INVALID_PARAM; - } - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - std::string message = "not systemApp"; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(2, 1) - .ErrorCode(ERR_ANS_NON_SYSTEM_APP).NotificationId(notificationId); - ReportDeleteFailedEventPush(haMetaMessage, reason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER) || - !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) { - std::string message = "no acl permission"; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(2, 2) - .ErrorCode(ERR_ANS_PERMISSION_DENIED).NotificationId(notificationId); - ReportDeleteFailedEventPush(haMetaMessage, reason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_PERMISSION_DENIED; - } - - int32_t errCode = CheckUserIdParams(userId); - if (errCode != ERR_OK) { - std::string message = "userId:" + std::to_string(userId); - HaMetaMessage haMateMessage = HaMetaMessage(EventSceneId::SCENE_13, EventBranchId::BRANCH_14) - .ErrorCode(errCode).NotificationId(notificationId); - ReportDeleteFailedEventPush(haMateMessage, reason, message); - return errCode; - } - - int32_t uid = -1; - if (bundleOption->GetUid() == DEFAULT_UID) { - std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); - if (bundleManager != nullptr) { - uid = BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleOption->GetBundleName(), userId); - } - } else { - uid = bundleOption->GetUid(); - } - if (uid < 0) { - std::string message = "uid error"; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(2, 3) - .ErrorCode(ERR_ANS_INVALID_UID).NotificationId(notificationId); - ReportDeleteFailedEventPush(haMetaMessage, reason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_INVALID_UID; - } - sptr bundle = new (std::nothrow) NotificationBundleOption( - bundleOption->GetBundleName(), uid); - return CancelPreparedNotification(notificationId, "", bundle, reason); -} - -ErrCode AdvancedNotificationService::CancelAsBundle( - const sptr &bundleOption, int32_t notificationId) -{ - ANS_LOGD("%{public}s, uid = %{public}d", __FUNCTION__, bundleOption->GetUid()); - int32_t userId = -1; - if (bundleOption->GetUid() != 0) { - OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), userId); - } else { - OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(IPCSkeleton::GetCallingUid(), userId); - } - return CancelAsBundle(bundleOption, notificationId, userId); -} - -ErrCode AdvancedNotificationService::CancelAsBundle( - int32_t notificationId, const std::string &representativeBundle, int32_t userId) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - sptr bundleOption = new (std::nothrow) NotificationBundleOption( - representativeBundle, DEFAULT_UID); - if (bundleOption == nullptr) { - ANS_LOGE("bundleOption is nullptr."); - return ERR_ANS_TASK_ERR; - } - return CancelAsBundle(bundleOption, notificationId, userId); -} - -ErrCode AdvancedNotificationService::CancelAsBundleWithAgent( - const sptr &bundleOption, const int32_t id) -{ - ANS_LOGD("Called."); - int32_t reason = NotificationConstant::APP_CANCEL_AS_BUNELE_WITH_AGENT_REASON_DELETE; - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - std::string message = "not systemApp"; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(2, 4) - .ErrorCode(ERR_ANS_NON_SYSTEM_APP).NotificationId(id); - ReportDeleteFailedEventPush(haMetaMessage, reason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_NON_SYSTEM_APP; - } - - if (IsAgentRelationship(GetClientBundleName(), bundleOption->GetBundleName())) { - int32_t userId = -1; - if (bundleOption->GetUid() != 0) { - OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), userId); - } else { - OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(IPCSkeleton::GetCallingUid(), userId); - } - int32_t uid = -1; - if (bundleOption->GetUid() == DEFAULT_UID) { - std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); - if (bundleManager != nullptr) { - uid = BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName( - bundleOption->GetBundleName(), userId); - } - } else { - uid = bundleOption->GetUid(); - } - if (uid < 0) { - std::string message = "uid error"; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(2, 5) - .ErrorCode(ERR_ANS_INVALID_UID).NotificationId(id); - ReportDeleteFailedEventPush(haMetaMessage, reason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_INVALID_UID; - } - sptr bundle = new (std::nothrow) NotificationBundleOption( - bundleOption->GetBundleName(), uid); - return CancelPreparedNotification(id, "", bundle, reason); - } - std::string message = "no agent setting"; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(2, 6) - .ErrorCode(ERR_ANS_NO_AGENT_SETTING).NotificationId(id); - ReportDeleteFailedEventPush(haMetaMessage, reason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_NO_AGENT_SETTING; -} - ErrCode AdvancedNotificationService::PublishAsBundle( const sptr& notification, const std::string &representativeBundle) { @@ -697,785 +471,100 @@ ErrCode AdvancedNotificationService::PublishAsBundleWithMaxCapacity( return PublishAsBundle(notification, representativeBundle); } -ErrCode AdvancedNotificationService::Delete(const std::string &key, int32_t removeReason) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - std::string message = "not systemApp. key:" + key + "."; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(4, 1) - .ErrorCode(ERR_ANS_NON_SYSTEM_APP); - ReportDeleteFailedEventPush(haMetaMessage, removeReason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - std::string message = "no acl permission. key:" + key + "."; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(4, 2) - .ErrorCode(ERR_ANS_PERMISSION_DENIED); - ReportDeleteFailedEventPush(haMetaMessage, removeReason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_PERMISSION_DENIED; - } - - if (notificationSvrQueue_ == nullptr) { - std::string message = "Serial queue is invalidated. key:" + key + "."; - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_INVALID_PARAM; - } - - return ExcuteDelete(key, removeReason); -} - -ErrCode AdvancedNotificationService::ExcuteDelete(const std::string &key, const int32_t removeReason) -{ - ErrCode result = ERR_OK; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - sptr notification = nullptr; -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - std::string deviceId; - std::string bundleName; - GetDistributedInfo(key, deviceId, bundleName); -#endif - result = RemoveFromNotificationList(key, notification, false, removeReason); - if (result != ERR_OK) { - return; - } - - if (notification != nullptr) { - UpdateRecentNotification(notification, true, removeReason); - CancelTimer(notification->GetAutoDeletedTimer()); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, removeReason); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - DoDistributedDelete(deviceId, bundleName, notification); -#endif - } - })); - notificationSvrQueue_->wait(handler); - - return result; -} - -ErrCode AdvancedNotificationService::DeleteByBundle(const sptr &bundleOption) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - ANS_LOGD("VerifyNativeToken is false."); - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - return ERR_ANS_PERMISSION_DENIED; - } - - sptr bundle = GenerateValidBundleOption(bundleOption); - if (bundle == nullptr) { - ANS_LOGD("bundle is false."); - return ERR_ANS_INVALID_BUNDLE; - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalid."); - return ERR_ANS_INVALID_PARAM; - } - ErrCode result = ERR_OK; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - std::vector keys = GetNotificationKeys(bundle); - for (auto key : keys) { -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - std::string deviceId; - std::string bundleName; - GetDistributedInfo(key, deviceId, bundleName); -#endif - sptr notification = nullptr; - - result = RemoveFromNotificationList(key, notification, false, NotificationConstant::CANCEL_REASON_DELETE); - if (result != ERR_OK) { - continue; - } - - if (notification != nullptr) { - int32_t reason = NotificationConstant::CANCEL_REASON_DELETE; - UpdateRecentNotification(notification, true, reason); - CancelTimer(notification->GetAutoDeletedTimer()); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, reason); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - DoDistributedDelete(deviceId, bundleName, notification); -#endif - } - } - - result = ERR_OK; - })); - notificationSvrQueue_->wait(handler); - - return result; -} - -ErrCode AdvancedNotificationService::DeleteAll() -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - const int32_t reason = NotificationConstant::CANCEL_ALL_REASON_DELETE; - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - std::string message = "not system app."; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(6, 8) - .ErrorCode(ERR_ANS_NON_SYSTEM_APP); - ReportDeleteFailedEventPush(haMetaMessage, reason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - std::string message = "no acl permission."; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(6, 9) - .ErrorCode(ERR_ANS_PERMISSION_DENIED); - ReportDeleteFailedEventPush(haMetaMessage, reason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_PERMISSION_DENIED; - } - - if (notificationSvrQueue_ == nullptr) { - std::string message = "Serial queue is invalidity."; - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_INVALID_PARAM; - } - ErrCode result = ERR_OK; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - int32_t activeUserId = SUBSCRIBE_USER_INIT; - if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(activeUserId) != ERR_OK) { - return; - } - std::vector keys = GetNotificationKeys(nullptr); - std::vector> notifications; - std::vector timerIds; - for (auto key : keys) { -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - std::string deviceId; - std::string bundleName; - GetDistributedInfo(key, deviceId, bundleName); -#endif - sptr notification = nullptr; - - result = RemoveFromNotificationListForDeleteAll(key, activeUserId, notification); - if ((result != ERR_OK) || (notification == nullptr)) { - continue; - } - - if (notification->GetUserId() == activeUserId) { - UpdateRecentNotification(notification, true, reason); - notifications.emplace_back(notification); - timerIds.emplace_back(notification->GetAutoDeletedTimer()); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - DoDistributedDelete(deviceId, bundleName, notification); -#endif - } - if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { - ANS_LOGD("Notifications size greater than or equal to MAX_CANCELED_PARCELABLE_VECTOR_NUM."); - SendNotificationsOnCanceled(notifications, nullptr, reason); - } - } - if (!notifications.empty()) { - NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( - notifications, nullptr, reason); - } - BatchCancelTimer(timerIds); - result = ERR_OK; - })); - notificationSvrQueue_->wait(handler); - - return result; -} - ErrCode AdvancedNotificationService::IsAllowedNotifyForBundle(const sptr &bundleOption, bool &allowed) { - ANS_LOGD("%{public}s", __FUNCTION__); - if (bundleOption == nullptr) { - return ERR_ANS_INVALID_BUNDLE; - } - - int32_t userId = SUBSCRIBE_USER_INIT; - if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { - ANS_LOGD("GetActiveUserId is false"); - return ERR_ANS_GET_ACTIVE_USER_FAILED; - } - - ErrCode result = ERR_OK; - allowed = false; - result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed); - if (result == ERR_OK && allowed) { - result = NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundleOption, allowed); - if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { - result = ERR_OK; - // FA model app can publish notification without user confirm - allowed = CheckApiCompatibility(bundleOption); - } - } - return result; -} - -ErrCode AdvancedNotificationService::PublishContinuousTaskNotification(const sptr &request) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem) { - return ERR_ANS_NOT_SYSTEM_SERVICE; - } - - int32_t uid = IPCSkeleton::GetCallingUid(); - int32_t userId = SUBSCRIBE_USER_INIT; - OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId); - request->SetCreatorUserId(userId); - ANS_LOGD("%{public}s, uid=%{public}d userId=%{public}d", __FUNCTION__, uid, userId); - - if (request->GetCreatorBundleName().empty()) { - request->SetCreatorBundleName(FOUNDATION_BUNDLE_NAME); - } - - if (request->GetOwnerBundleName().empty()) { - request->SetOwnerBundleName(FOUNDATION_BUNDLE_NAME); - } - - sptr bundleOption = nullptr; - bundleOption = new (std::nothrow) NotificationBundleOption(std::string(), uid); - if (bundleOption == nullptr) { - ANS_LOGE("Failed to create NotificationBundleOption instance"); - return ERR_NO_MEMORY; - } - - ErrCode result = PrepareContinuousTaskNotificationRequest(request, uid); - if (result != ERR_OK) { - return result; - } - request->SetUnremovable(true); - std::shared_ptr record = std::make_shared(); - record->request = request; - record->bundleOption = bundleOption; - record->notification = new (std::nothrow) Notification(request); - if (record->notification == nullptr) { - ANS_LOGE("Failed to create Notification instance"); - return ERR_NO_MEMORY; - } - record->notification->SetSourceType(NotificationConstant::SourceType::TYPE_CONTINUOUS); - - 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!"); - if (!IsNotificationExists(record->notification->GetKey())) { - AddToNotificationList(record); - } else { - if (record->request->IsAlertOneTime()) { - CloseAlert(record); - } - UpdateInNotificationList(record); - } - - UpdateRecentNotification(record->notification, false, 0); - sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyConsumed(record->notification, sortingMap); - })); - notificationSvrQueue_->wait(handler); - - return result; -} - -ErrCode AdvancedNotificationService::CancelContinuousTaskNotification(const std::string &label, int32_t notificationId) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem) { - return ERR_ANS_NOT_SYSTEM_SERVICE; - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalid."); - return ERR_ANS_INVALID_PARAM; - } - int32_t uid = IPCSkeleton::GetCallingUid(); - ErrCode result = ERR_OK; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - sptr notification = nullptr; - for (auto record : notificationList_) { - if ((record->bundleOption->GetBundleName().empty()) && (record->bundleOption->GetUid() == uid) && - (record->notification->GetId() == notificationId) && (record->notification->GetLabel() == label)) { - notification = record->notification; - notificationList_.remove(record); - result = ERR_OK; - break; - } - } - if (notification != nullptr) { - int32_t reason = NotificationConstant::APP_CANCEL_REASON_DELETE; - UpdateRecentNotification(notification, true, reason); - CancelTimer(notification->GetAutoDeletedTimer()); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, reason); - } - })); - notificationSvrQueue_->wait(handler); - return result; -} - -ErrCode AdvancedNotificationService::RemoveSystemLiveViewNotifications( - const std::string& bundleName, const int32_t uid) -{ - std::vector> recordList; - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("NotificationSvrQueue is nullptr"); - return ERR_ANS_INVALID_PARAM; - } - ErrCode result = ERR_OK; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - LivePublishProcess::GetInstance()->EraseLiveViewSubsciber(uid); - GetTargetRecordList(uid, NotificationConstant::SlotType::LIVE_VIEW, - NotificationContent::Type::LOCAL_LIVE_VIEW, recordList); - GetCommonTargetRecordList(uid, NotificationConstant::SlotType::LIVE_VIEW, - NotificationContent::Type::LIVE_VIEW, recordList); - if (recordList.size() == 0) { - ANS_LOGE("Get Target record list fail."); - result = ERR_ANS_NOTIFICATION_NOT_EXISTS; - return; - } - result = RemoveNotificationFromRecordList(recordList); - })); - notificationSvrQueue_->wait(handler); - return result; -} - -ErrCode AdvancedNotificationService::RemoveSystemLiveViewNotificationsOfSa(int32_t uid) -{ - { - std::lock_guard lock(delayNotificationMutext_); - for (auto iter = delayNotificationList_.begin(); iter != delayNotificationList_.end();) { - if ((*iter).first->notification->GetNotificationRequest().GetCreatorUid() == uid && - (*iter).first->notification->GetNotificationRequest().IsInProgress()) { - CancelTimer((*iter).second); - iter = delayNotificationList_.erase(iter); - } else { - iter++; - } - } - } - - ErrCode result = ERR_OK; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - LivePublishProcess::GetInstance()->EraseLiveViewSubsciber(uid); - std::vector> recordList; - for (auto item : notificationList_) { - if (item->notification->GetNotificationRequest().GetCreatorUid() == uid && - item->notification->GetNotificationRequest().IsInProgress()) { - recordList.emplace_back(item); - } - } - if (!recordList.empty()) { - result = RemoveNotificationFromRecordList(recordList); - } - })); - notificationSvrQueue_->wait(handler); - return result; -} - -ErrCode AdvancedNotificationService::TriggerLocalLiveView(const sptr &bundleOption, - const int32_t notificationId, const sptr &buttonOption) -{ - HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); - 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("AccessTokenHelper::CheckPermission is bogus."); - return ERR_ANS_PERMISSION_DENIED; - } - - sptr bundle = GenerateValidBundleOption(bundleOption); - if (bundle == nullptr) { - return ERR_ANS_INVALID_BUNDLE; - } - - ErrCode result = ERR_ANS_NOTIFICATION_NOT_EXISTS; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - sptr notification = nullptr; - - for (auto record : notificationList_) { - if (record->request->GetAgentBundle() != nullptr) { - if ((record->request->GetAgentBundle()->GetBundleName() == bundle->GetBundleName()) && - (record->request->GetAgentBundle()->GetUid() == bundle->GetUid()) && - (record->notification->GetId() == notificationId)) { - notification = record->notification; - result = ERR_OK; - break; - } - } else { - if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) && - (record->bundleOption->GetUid() == bundle->GetUid()) && - (record->notification->GetId() == notificationId)) { - notification = record->notification; - result = ERR_OK; - break; - } - } - } - - if (notification != nullptr) { - NotificationLocalLiveViewSubscriberManager::GetInstance()->NotifyTriggerResponse(notification, - buttonOption); - } - })); - notificationSvrQueue_->wait(handler); - return result; -} - -ErrCode AdvancedNotificationService::RemoveNotification(const sptr &bundleOption, - int32_t notificationId, const std::string &label, int32_t removeReason) -{ - HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); - ANS_LOGD("%{public}s", __FUNCTION__); - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - std::string message = "not systemApp."; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(4, 4) - .ErrorCode(ERR_ANS_NON_SYSTEM_APP).NotificationId(notificationId); - ReportDeleteFailedEventPush(haMetaMessage, removeReason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - std::string message = "no acl controller permission."; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(4, 5) - .ErrorCode(ERR_ANS_PERMISSION_DENIED).NotificationId(notificationId); - ReportDeleteFailedEventPush(haMetaMessage, removeReason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_PERMISSION_DENIED; - } - - sptr bundle = GenerateValidBundleOption(bundleOption); - if (bundle == nullptr) { + ANS_LOGD("%{public}s", __FUNCTION__); + if (bundleOption == nullptr) { return ERR_ANS_INVALID_BUNDLE; } - if (notificationSvrQueue_ == nullptr) { - std::string message = "NotificationSvrQueue_ is null."; - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_INVALID_PARAM; + int32_t userId = SUBSCRIBE_USER_INIT; + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { + ANS_LOGD("GetActiveUserId is false"); + return ERR_ANS_GET_ACTIVE_USER_FAILED; } - ErrCode result = ERR_ANS_NOTIFICATION_NOT_EXISTS; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - bool isThirdParty = true; - sptr notification = nullptr; - sptr notificationRequest = nullptr; - -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - std::string deviceId; - std::string bundleName; -#endif - for (auto record : notificationList_) { - if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) && - (record->bundleOption->GetUid() == bundle->GetUid()) && -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - (record->deviceId.empty()) && -#endif - (record->notification->GetId() == notificationId) && (record->notification->GetLabel() == label)) { - if (!record->notification->IsRemoveAllowed()) { - result = ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED; - break; - } -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - deviceId = record->deviceId; - bundleName = record->bundleName; -#endif - notification = record->notification; - notificationRequest = record->request; - isThirdParty = record->isThirdparty; - - if (removeReason != NotificationConstant::CLICK_REASON_DELETE) { - ProcForDeleteLiveView(record); - } - - notificationList_.remove(record); - result = ERR_OK; - break; - } - } - if (notification != nullptr) { - UpdateRecentNotification(notification, true, removeReason); - CancelTimer(notification->GetAutoDeletedTimer()); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, removeReason); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - DoDistributedDelete(deviceId, bundleName, notification); -#endif - } - if (removeReason != NotificationConstant::CLICK_REASON_DELETE) { - TriggerRemoveWantAgent(notificationRequest, removeReason, isThirdParty); + ErrCode result = ERR_OK; + allowed = false; + result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed); + if (result == ERR_OK && allowed) { + result = NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundleOption, allowed); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + // FA model app can publish notification without user confirm + allowed = CheckApiCompatibility(bundleOption); } - })); - notificationSvrQueue_->wait(handler); - if (result != ERR_OK) { - std::string message = "remove notificaiton error"; - ANS_LOGE("%{public}s %{public}d", message.c_str(), result); } - SendRemoveHiSysEvent(notificationId, label, bundleOption, result); return result; } -ErrCode AdvancedNotificationService::RemoveAllNotificationsForDisable( - const sptr &bundleOption) -{ - return RemoveAllNotificationsInner(bundleOption, NotificationConstant::DISABLE_NOTIFICATION_REASON_DELETE); -} - -ErrCode AdvancedNotificationService::RemoveAllNotifications(const sptr &bundleOption) -{ - return RemoveAllNotificationsInner(bundleOption, NotificationConstant::APP_REMOVE_ALL_REASON_DELETE); -} - -ErrCode AdvancedNotificationService::RemoveAllNotificationsInner(const sptr &bundleOption, - int32_t reason) +ErrCode AdvancedNotificationService::PublishContinuousTaskNotification(const sptr &request) { - HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s", __FUNCTION__); bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - std::string message = "not system app."; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(6, 1) - .ErrorCode(ERR_ANS_NON_SYSTEM_APP); - ReportDeleteFailedEventPush(haMetaMessage, reason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_NON_SYSTEM_APP; + if (!isSubsystem) { + return ERR_ANS_NOT_SYSTEM_SERVICE; } - int32_t callingUid = IPCSkeleton::GetCallingUid(); - if (callingUid != ANS_UID && !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - std::string message = "no acl permission."; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(6, 2) - .ErrorCode(ERR_ANS_PERMISSION_DENIED); - ReportDeleteFailedEventPush(haMetaMessage, reason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_PERMISSION_DENIED; - } + int32_t uid = IPCSkeleton::GetCallingUid(); + int32_t userId = SUBSCRIBE_USER_INIT; + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId); + request->SetCreatorUserId(userId); + ANS_LOGD("%{public}s, uid=%{public}d userId=%{public}d", __FUNCTION__, uid, userId); - sptr bundle = GenerateValidBundleOption(bundleOption); - if (bundle == nullptr) { - std::string message = "budle is nullptr."; - OHOS::Notification::HaMetaMessage haMetaMessage = HaMetaMessage(6, 3) - .ErrorCode(ERR_ANS_INVALID_BUNDLE); - ReportDeleteFailedEventPush(haMetaMessage, reason, message); - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_INVALID_BUNDLE; + if (request->GetCreatorBundleName().empty()) { + request->SetCreatorBundleName(FOUNDATION_BUNDLE_NAME); } - if (notificationSvrQueue_ == nullptr) { - std::string message = "Serial queue is nullptr."; - ANS_LOGE("%{public}s", message.c_str()); - return ERR_ANS_INVALID_PARAM; + if (request->GetOwnerBundleName().empty()) { + request->SetOwnerBundleName(FOUNDATION_BUNDLE_NAME); } - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - std::vector> removeList; - ANS_LOGD("ffrt enter!"); - for (auto record : notificationList_) { - bool isAllowedNotification = true; - if (IsAllowedNotifyForBundle(bundleOption, isAllowedNotification) != ERR_OK) { - ANSR_LOGW("The application does not request enable notification."); - } - if (!record->notification->IsRemoveAllowed() && isAllowedNotification) { - ANS_LOGI("BatchRemove-FILTER-RemoveNotAllowed-%{public}s", record->notification->GetKey().c_str()); - continue; - } - if (record->slot != nullptr) { - if (record->slot->GetForceControl() && record->slot->GetEnable()) { - ANS_LOGI("BatchRemove-FILTER-ForceControl-%{public}s", record->notification->GetKey().c_str()); - continue; - } - } - if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) && - (record->bundleOption->GetUid() == bundle->GetUid()) -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - && record->deviceId.empty() -#endif - ) { - auto notificationRequest = record->request; - if (!BundleManagerHelper::GetInstance()->IsSystemApp(bundle->GetUid()) && - notificationRequest->IsSystemLiveView()) { - auto localLiveviewContent = std::static_pointer_cast( - notificationRequest->GetContent()->GetNotificationContent()); - if (localLiveviewContent->GetType() == 0) { - continue; - } - } - ProcForDeleteLiveView(record); - removeList.push_back(record); - } - } - - std::vector> notifications; - std::vector timerIds; - for (auto record : removeList) { - notificationList_.remove(record); - if (record->notification != nullptr) { - ANS_LOGD("record->notification is not nullptr."); - UpdateRecentNotification(record->notification, true, reason); - notifications.emplace_back(record->notification); - timerIds.emplace_back(record->notification->GetAutoDeletedTimer()); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - DoDistributedDelete(record->deviceId, record->bundleName, record->notification); -#endif - } - if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { - SendNotificationsOnCanceled(notifications, nullptr, reason); - } - - TriggerRemoveWantAgent(record->request, reason, record->isThirdparty); - } - - if (!notifications.empty()) { - NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(notifications, nullptr, reason); - } - BatchCancelTimer(timerIds); - })); - notificationSvrQueue_->wait(handler); - - return ERR_OK; -} -ErrCode AdvancedNotificationService::RemoveNotifications( - const std::vector &keys, int32_t removeReason) -{ - HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); - ANS_LOGD("enter"); + sptr bundleOption = nullptr; + bundleOption = new (std::nothrow) NotificationBundleOption(std::string(), uid); + if (bundleOption == nullptr) { + ANS_LOGE("Failed to create NotificationBundleOption instance"); + return ERR_NO_MEMORY; + } - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - return ERR_ANS_NON_SYSTEM_APP; + ErrCode result = PrepareContinuousTaskNotificationRequest(request, uid); + if (result != ERR_OK) { + return result; } - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - return ERR_ANS_PERMISSION_DENIED; + request->SetUnremovable(true); + std::shared_ptr record = std::make_shared(); + record->request = request; + record->bundleOption = bundleOption; + record->notification = new (std::nothrow) Notification(request); + if (record->notification == nullptr) { + ANS_LOGE("Failed to create Notification instance"); + return ERR_NO_MEMORY; } + record->notification->SetSourceType(NotificationConstant::SourceType::TYPE_CONTINUOUS); + if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("NotificationSvrQueue is nullptr."); + ANS_LOGE("Serial queue is invalid."); return ERR_ANS_INVALID_PARAM; } ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - std::vector> notifications; - std::vector timerIds; - for (auto key : keys) { - sptr notification = nullptr; -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - std::string deviceId; - std::string bundleName; - GetDistributedInfo(key, deviceId, bundleName); -#endif - ErrCode result = RemoveFromNotificationList(key, notification, false, removeReason); - if (result != ERR_OK) { - continue; - } - if (notification != nullptr) { - UpdateRecentNotification(notification, true, removeReason); - notifications.emplace_back(notification); - timerIds.emplace_back(notification->GetAutoDeletedTimer()); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - DoDistributedDelete(deviceId, bundleName, notification); -#endif - } - if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { - std::vector> currNotificationList = notifications; - NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( - currNotificationList, nullptr, removeReason); - notifications.clear(); + ANS_LOGD("ffrt enter!"); + if (!IsNotificationExists(record->notification->GetKey())) { + AddToNotificationList(record); + } else { + if (record->request->IsAlertOneTime()) { + CloseAlert(record); } + UpdateInNotificationList(record); } - if (!notifications.empty()) { - NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(notifications, nullptr, removeReason); - } - BatchCancelTimer(timerIds); + UpdateRecentNotification(record->notification, false, 0); + sptr sortingMap = GenerateSortingMap(); + NotificationSubscriberManager::GetInstance()->NotifyConsumed(record->notification, sortingMap); })); notificationSvrQueue_->wait(handler); - return ERR_OK; -} - -ErrCode AdvancedNotificationService::RemoveNotificationBySlot(const sptr &bundleOption, - const sptr &slot, const int reason) -{ - HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); - ANS_LOGD("%{public}s", __FUNCTION__); - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - return ERR_ANS_NON_SYSTEM_APP; - } - - sptr bundle = GenerateValidBundleOption(bundleOption); - if (bundle == nullptr) { - return ERR_ANS_INVALID_BUNDLE; - } - ErrCode result = ERR_ANS_NOTIFICATION_NOT_EXISTS; - std::vector> removeList; - for (auto record : notificationList_) { - if (record == nullptr) { - ANS_LOGE("record is nullptr"); - continue; - } - if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) && - (record->bundleOption->GetUid() == bundle->GetUid()) && - (record->request->GetSlotType() == slot->GetType())) { - if ((record->request->GetAgentBundle() != nullptr && record->request->IsSystemLiveView())) { - ANS_LOGW("Agent systemliveview no need remove."); - continue; - } - ProcForDeleteLiveView(record); - removeList.push_back(record); - } - } - - std::vector> notifications; - std::vector timerIds; - for (auto record : removeList) { - if (record == nullptr) { - ANS_LOGE("record is nullptr"); - continue; - } - notificationList_.remove(record); - if (record->notification != nullptr) { - ANS_LOGD("record->notification is not nullptr."); - UpdateRecentNotification(record->notification, true, reason); - notifications.emplace_back(record->notification); - timerIds.emplace_back(record->notification->GetAutoDeletedTimer()); - } - if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { - SendNotificationsOnCanceled(notifications, nullptr, reason); - } - - TriggerRemoveWantAgent(record->request, reason, record->isThirdparty); - result = ERR_OK; - } - - if (!notifications.empty()) { - NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(notifications, nullptr, reason); - } - BatchCancelTimer(timerIds); return result; } @@ -1485,7 +574,8 @@ ErrCode AdvancedNotificationService::IsNeedSilentInDoNotDisturbMode( ANS_LOGD("%{public}s", __FUNCTION__); int32_t callingUid = IPCSkeleton::GetCallingUid(); - if (callingUid != ANS_UID && !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + if (callingUid != NotificationConstant::ANS_UID && + !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { ANS_LOGD("IsNeedSilentInDoNotDisturbMode CheckPermission failed."); return ERR_ANS_PERMISSION_DENIED; } @@ -1769,49 +859,6 @@ ErrCode AdvancedNotificationService::RemoveGroupByBundle( return ERR_OK; } -ErrCode AdvancedNotificationService::RemoveNotificationFromRecordList( - const std::vector>& recordList) -{ - ErrCode result = ERR_OK; - std::vector> notifications; - std::vector timerIds; - for (auto& record : recordList) { - std::string key = record->notification->GetKey(); - sptr notification = nullptr; -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - std::string deviceId; - std::string bundleName; - GetDistributedInfo(key, deviceId, bundleName); -#endif - result = RemoveFromNotificationList(key, notification, true, - NotificationConstant::USER_STOPPED_REASON_DELETE); - if (result != ERR_OK) { - continue; - } - if (notification != nullptr) { - int32_t reason = NotificationConstant::USER_STOPPED_REASON_DELETE; - UpdateRecentNotification(notification, true, reason); - notifications.emplace_back(notification); - timerIds.emplace_back(notification->GetAutoDeletedTimer()); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - DoDistributedDelete(deviceId, bundleName, notification); -#endif - } - if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { - std::vector> currNotificationList = notifications; - NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( - currNotificationList, nullptr, NotificationConstant::USER_STOPPED_REASON_DELETE); - notifications.clear(); - } - } - if (!notifications.empty()) { - NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( - notifications, nullptr, NotificationConstant::USER_STOPPED_REASON_DELETE); - } - BatchCancelTimer(timerIds); - return result; -} - void AdvancedNotificationService::UpdateUnifiedGroupInfo(const std::string &key, std::shared_ptr &groupInfo) { @@ -2026,52 +1073,6 @@ ErrCode AdvancedNotificationService::PublishNotificationBySa(const sptr &subscriber, const bool isNative) -{ - return SubscribeLocalLiveView(subscriber, nullptr, isNative); -} - -ErrCode AdvancedNotificationService::SubscribeLocalLiveView( - const sptr &subscriber, - const sptr &info, const bool isNative) -{ - HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); - ANS_LOGD("%{public}s, isNative: %{public}d", __FUNCTION__, isNative); - - ErrCode errCode = ERR_OK; - do { - if (!isNative) { - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - ANS_LOGE("Client is not a system app or subsystem."); - errCode = ERR_ANS_NON_SYSTEM_APP; - break; - } - } - - if (subscriber == nullptr) { - errCode = ERR_ANS_INVALID_PARAM; - break; - } - - errCode = NotificationLocalLiveViewSubscriberManager::GetInstance()->AddLocalLiveViewSubscriber( - subscriber, info); - if (errCode != ERR_OK) { - break; - } - } while (0); - if (errCode == ERR_OK) { - int32_t callingUid = IPCSkeleton::GetCallingUid(); - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - LivePublishProcess::GetInstance()->AddLiveViewSubscriber(callingUid); - })); - notificationSvrQueue_->wait(handler); - } - SendSubscribeHiSysEvent(IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid(), info, errCode); - return errCode; -} - ErrCode AdvancedNotificationService::DuplicateMsgControl(const sptr &request) { if (request->IsCommonLiveView() || request->GetAppMessageId().empty()) { diff --git a/services/ans/src/system_live_view/advanced_notification_system_live_view_service.cpp b/services/ans/src/system_live_view/advanced_notification_system_live_view_service.cpp new file mode 100644 index 000000000..9ee386ecb --- /dev/null +++ b/services/ans/src/system_live_view/advanced_notification_system_live_view_service.cpp @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2025-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_permission_def.h" +#include "access_token_helper.h" +#include "ans_log_wrapper.h" +#include "ans_inner_errors.h" +#include "errors.h" +#include "hitrace_meter_adapter.h" +#include "ipc_skeleton.h" + +#include "notification_bundle_option.h" +#include "notification_constant.h" +#include "notification_local_live_view_subscriber_manager.h" +#include "live_publish_process.h" + +namespace OHOS { +namespace Notification { +ErrCode AdvancedNotificationService::TriggerLocalLiveView(const sptr &bundleOption, + const int32_t notificationId, const sptr &buttonOption) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + 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("AccessTokenHelper::CheckPermission is bogus."); + return ERR_ANS_PERMISSION_DENIED; + } + + sptr bundle = GenerateValidBundleOption(bundleOption); + if (bundle == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + + ErrCode result = ERR_ANS_NOTIFICATION_NOT_EXISTS; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + sptr notification = nullptr; + result = GetNotificationById(bundle, notificationId, notification); + if (notification != nullptr) { + NotificationLocalLiveViewSubscriberManager::GetInstance()->NotifyTriggerResponse(notification, + buttonOption); + } + })); + notificationSvrQueue_->wait(handler); + return result; +} + +ErrCode AdvancedNotificationService::GetNotificationById(const sptr &bundle, + const int32_t notificationId, sptr ¬ification) +{ + if (bundle == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + for (auto record : notificationList_) { + if (record->request->GetAgentBundle() != nullptr) { + if ((record->request->GetAgentBundle()->GetBundleName() == bundle->GetBundleName()) && + (record->request->GetAgentBundle()->GetUid() == bundle->GetUid()) && + (record->notification->GetId() == notificationId)) { + notification = record->notification; + return ERR_OK; + } + } else { + if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) && + (record->bundleOption->GetUid() == bundle->GetUid()) && + (record->notification->GetId() == notificationId)) { + notification = record->notification; + return ERR_OK; + } + } + } + return ERR_ANS_NOTIFICATION_NOT_EXISTS; +} + +ErrCode AdvancedNotificationService::SubscribeLocalLiveView( + const sptr &subscriber, const bool isNative) +{ + return SubscribeLocalLiveView(subscriber, nullptr, isNative); +} + +ErrCode AdvancedNotificationService::SubscribeLocalLiveView( + const sptr &subscriber, + const sptr &info, const bool isNative) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + ANS_LOGD("%{public}s, isNative: %{public}d", __FUNCTION__, isNative); + + ErrCode errCode = ERR_OK; + do { + if (!isNative) { + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGE("Client is not a system app or subsystem."); + errCode = ERR_ANS_NON_SYSTEM_APP; + break; + } + } + + if (subscriber == nullptr) { + errCode = ERR_ANS_INVALID_PARAM; + break; + } + + errCode = NotificationLocalLiveViewSubscriberManager::GetInstance()->AddLocalLiveViewSubscriber( + subscriber, info); + if (errCode != ERR_OK) { + break; + } + } while (0); + if (errCode == ERR_OK) { + int32_t callingUid = IPCSkeleton::GetCallingUid(); + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + LivePublishProcess::GetInstance()->AddLiveViewSubscriber(callingUid); + })); + notificationSvrQueue_->wait(handler); + } + SendSubscribeHiSysEvent(IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid(), info, errCode); + return errCode; +} + +ErrCode AdvancedNotificationService::RemoveSystemLiveViewNotifications( + const std::string& bundleName, const int32_t uid) +{ + std::vector> recordList; + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("NotificationSvrQueue is nullptr"); + return ERR_ANS_INVALID_PARAM; + } + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + LivePublishProcess::GetInstance()->EraseLiveViewSubsciber(uid); + GetTargetRecordList(uid, NotificationConstant::SlotType::LIVE_VIEW, + NotificationContent::Type::LOCAL_LIVE_VIEW, recordList); + GetCommonTargetRecordList(uid, NotificationConstant::SlotType::LIVE_VIEW, + NotificationContent::Type::LIVE_VIEW, recordList); + if (recordList.size() == 0) { + ANS_LOGE("Get Target record list fail."); + result = ERR_ANS_NOTIFICATION_NOT_EXISTS; + return; + } + result = RemoveNotificationFromRecordList(recordList); + })); + notificationSvrQueue_->wait(handler); + return result; +} + +ErrCode AdvancedNotificationService::RemoveSystemLiveViewNotificationsOfSa(int32_t uid) +{ + { + std::lock_guard lock(delayNotificationMutext_); + for (auto iter = delayNotificationList_.begin(); iter != delayNotificationList_.end();) { + if ((*iter).first->notification->GetNotificationRequest().GetCreatorUid() == uid && + (*iter).first->notification->GetNotificationRequest().IsInProgress()) { + CancelTimer((*iter).second); + iter = delayNotificationList_.erase(iter); + } else { + iter++; + } + } + } + + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + LivePublishProcess::GetInstance()->EraseLiveViewSubsciber(uid); + std::vector> recordList; + for (auto item : notificationList_) { + if (item->notification->GetNotificationRequest().GetCreatorUid() == uid && + item->notification->GetNotificationRequest().IsInProgress()) { + recordList.emplace_back(item); + } + } + if (!recordList.empty()) { + result = RemoveNotificationFromRecordList(recordList); + } + })); + notificationSvrQueue_->wait(handler); + return result; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file -- Gitee