diff --git a/frameworks/ans/core/common/include/ans_inner_errors.h b/frameworks/ans/core/common/include/ans_inner_errors.h index 44f0f61998592ca174d7ed7df513d5dda8e63910..15d3d89c135a6e27f193089aacf8297cfb5c39ae 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 193de4582d67a284e65dcf57e9b00e5da27ac466..26babfc735cb4341b005438c7b76b07b63cb4a82 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 585679aa45cb3bfc39c724ece21a2ce4efb6cc95..3f53310214599a431d07fe26309f5fc56c71f735 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 0ba592149706e507b5cb76e40ac5a162f87e05a0..9bd006a38a31464aa1e1b08da902f1e3d9b660ee 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");