From 0784b163e8d3b0d4b5c10457e19a8aecb8bc5e00 Mon Sep 17 00:00:00 2001 From: fangJinliang1 Date: Mon, 28 Mar 2022 16:55:26 +0800 Subject: [PATCH] unsubscribe modify Signed-off-by: fangJinliang1 Change-Id: Ibf0c796e7dcd746fb93b8f1ce0ebdd5b4d676f02 Signed-off-by: fangJinliang1 --- .../ans/core/common/include/ans_inner_errors.h | 1 + interfaces/kits/napi/ans/include/subscribe.h | 5 +++++ interfaces/kits/napi/ans/src/subscribe.cpp | 11 +++++++++++ interfaces/kits/napi/ans/src/unsubscribe.cpp | 12 ++++++++++-- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/frameworks/ans/core/common/include/ans_inner_errors.h b/frameworks/ans/core/common/include/ans_inner_errors.h index 44f0f6199..15d3d89c1 100644 --- a/frameworks/ans/core/common/include/ans_inner_errors.h +++ b/frameworks/ans/core/common/include/ans_inner_errors.h @@ -74,6 +74,7 @@ enum ErrorCode : uint32_t { ERR_ANS_DISTRIBUTED_GET_INFO_FAILED, ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED, ERR_ANS_GET_ACTIVE_USER_FAILED, + ERR_ANS_SUBSCRIBER_ALREADY_DELETE, }; } // namespace Notification } // namespace OHOS diff --git a/interfaces/kits/napi/ans/include/subscribe.h b/interfaces/kits/napi/ans/include/subscribe.h index 193de4582..26babfc73 100644 --- a/interfaces/kits/napi/ans/include/subscribe.h +++ b/interfaces/kits/napi/ans/include/subscribe.h @@ -52,6 +52,8 @@ public: void SetCallbackInfo(const napi_env &env, const std::string &type, const napi_ref &ref); + bool SetObjectDeleting(bool status); + private: void SetCancelCallbackInfo(const napi_env &env, const napi_ref &ref); @@ -86,6 +88,9 @@ private: CallbackInfo disturbModeCallbackInfo_; CallbackInfo disturbDateCallbackInfo_; CallbackInfo enabledNotificationCallbackInfo_; + + std::mutex delMutex_; + bool isDelete_ = false; }; struct SubscriberInstancesInfo { diff --git a/interfaces/kits/napi/ans/src/subscribe.cpp b/interfaces/kits/napi/ans/src/subscribe.cpp index 585679aa4..3f5331021 100644 --- a/interfaces/kits/napi/ans/src/subscribe.cpp +++ b/interfaces/kits/napi/ans/src/subscribe.cpp @@ -851,6 +851,17 @@ void SubscriberInstance::SetDisturbDateCallbackInfo(const napi_env &env, const n disturbDateCallbackInfo_.ref = ref; } +bool SubscriberInstance::SetObjectDeleting(bool status) +{ + std::lock_guard lock(delMutex_); + if (isDelete_ && isDelete_ == status) { + return false; + } else { + isDelete_ = status; + return true; + } +} + void SubscriberInstance::SetCallbackInfo(const napi_env &env, const std::string &type, const napi_ref &ref) { if (type == CONSUME) { diff --git a/interfaces/kits/napi/ans/src/unsubscribe.cpp b/interfaces/kits/napi/ans/src/unsubscribe.cpp index 0ba592149..9bd006a38 100644 --- a/interfaces/kits/napi/ans/src/unsubscribe.cpp +++ b/interfaces/kits/napi/ans/src/unsubscribe.cpp @@ -100,8 +100,16 @@ napi_value Unsubscribe(napi_env env, napi_callback_info info) return; } - asynccallbackinfo->info.errorCode = - NotificationHelper::UnSubscribeNotification(*(asynccallbackinfo->objectInfo)); + bool ret = asynccallbackinfo->objectInfo->SetObjectDeleting(true); + if (ret) { + asynccallbackinfo->info.errorCode = + NotificationHelper::UnSubscribeNotification(*(asynccallbackinfo->objectInfo)); + if (asynccallbackinfo->info.errorCode != ERR_OK) { + asynccallbackinfo->objectInfo->SetObjectDeleting(false); + } + } else { + asynccallbackinfo->info.errorCode = ERR_ANS_SUBSCRIBER_ALREADY_DELETE; + } }, [](napi_env env, napi_status status, void *data) { ANS_LOGI("Unsubscribe napi_create_async_work end"); -- Gitee