diff --git a/frameworks/ans/src/notification_subscriber.cpp b/frameworks/ans/src/notification_subscriber.cpp index 8fc4bd693097a69334db7790770b2f9a7a675bb9..b1718bf9f09663a4979707197e4abc474f6c1ea1 100644 --- a/frameworks/ans/src/notification_subscriber.cpp +++ b/frameworks/ans/src/notification_subscriber.cpp @@ -15,9 +15,13 @@ #include "notification_subscriber.h" +#include "errors.h" +#include "hitrace_meter.h" #include "hitrace_meter_adapter.h" #include "iservice_registry.h" +#include "notification_sorting_map.h" #include "system_ability_definition.h" +#include namespace OHOS { namespace Notification { @@ -85,6 +89,15 @@ void NotificationSubscriber::SubscriberImpl::OnCanceledList(const std::vector ¬ificationMap, int32_t deleteReason) { HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + std::vector> notificationList; + for (auto notification : notifications) { + notificationList.emplace_back(std::make_shared(*notification)); + } + if (notificationMap == nullptr && subscriber_.OnBatchCancel(notificationList, std::make_shared(), deleteReason) == ERR_OK) { + return; + } else if (notificationMap != nullptr && subscriber_.OnBatchCancel(notificationList, std::make_shared(*notificationMap), deleteReason) == ERR_OK) { + return; + } for (auto notification : notifications) { OnCanceled(notification, notificationMap, deleteReason); } diff --git a/frameworks/js/napi/include/subscribe.h b/frameworks/js/napi/include/subscribe.h index b096727d4b055c22fe578ffb44d45364d65e7ec0..4992176a55440db8dff38a0a91e53ecdf37906c6 100644 --- a/frameworks/js/napi/include/subscribe.h +++ b/frameworks/js/napi/include/subscribe.h @@ -101,6 +101,13 @@ public: */ void OnBadgeChanged(const std::shared_ptr &badgeData) override; + /** + * @brief The callback function on the badge number changed. + * + * @param badgeData Indicates the BadgeNumberCallbackData object. + */ + virtual ErrCode OnBatchCancel(const std::vector> &requestList, + const std::shared_ptr &sortingMap, int32_t deleteReason) override; /** * @brief Sets the callback information by type. * @@ -133,6 +140,8 @@ private: void SetBadgeCallbackInfo(const napi_env &env, const napi_ref &ref); + void SetBatchCancelCallbackInfo(const napi_env &env, const napi_ref &ref); + private: struct CallbackInfo { napi_env env = nullptr; @@ -150,6 +159,7 @@ private: CallbackInfo disturbChangedCallbackInfo_; CallbackInfo enabledNotificationCallbackInfo_; CallbackInfo setBadgeCallbackInfo_; + CallbackInfo batchCancelCallbackInfo_; }; struct SubscriberInstancesInfo { @@ -184,4 +194,4 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, NotificationSubscribeInfo &subscriberInfo, SubscriberInstance *&subscriber, napi_ref &callback); } // namespace NotificationNapi } // namespace OHOS -#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_SUBSCRIBE_H \ No newline at end of file +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_SUBSCRIBE_H diff --git a/frameworks/js/napi/src/subscribe.cpp b/frameworks/js/napi/src/subscribe.cpp index a65074fc87a999ee68647ec97b2d750e4ead21c3..8d24a603f339c800997dc66df72bf383dadc3726 100644 --- a/frameworks/js/napi/src/subscribe.cpp +++ b/frameworks/js/napi/src/subscribe.cpp @@ -14,9 +14,14 @@ */ #include "subscribe.h" +#include "ans_inner_errors.h" +#include "errors.h" +#include "js_native_api.h" +#include #include #include +#include namespace OHOS { namespace NotificationNapi { @@ -33,11 +38,13 @@ const std::string DISTURB_DATE_CHANGE = "onDoNotDisturbDateChange"; const std::string DISTURB_CHANGED = "onDoNotDisturbChanged"; const std::string ENABLE_NOTIFICATION_CHANGED = "OnEnabledNotificationChanged"; const std::string BADGE_CHANGED = "OnBadgeChanged"; +const std::string BATCH_CANCEL = "onBatchCancel"; struct NotificationReceiveDataWorker { napi_env env = nullptr; napi_ref ref = nullptr; std::shared_ptr request; + std::vector> requestList; std::shared_ptr sortingMap; NotificationDoNotDisturbDate date; EnabledNotificationCallbackData callbackData; @@ -147,6 +154,9 @@ SubscriberInstance::~SubscriberInstance() if (enabledNotificationCallbackInfo_.ref != nullptr) { napi_delete_reference(enabledNotificationCallbackInfo_.env, enabledNotificationCallbackInfo_.ref); } + if (batchCancelCallbackInfo_.ref != nullptr) { + napi_delete_reference(batchCancelCallbackInfo_.env, batchCancelCallbackInfo_.ref); + } } void UvQueueWorkOnCanceled(uv_work_t *work, int status) @@ -248,6 +258,112 @@ void SubscriberInstance::OnCanceled(const std::shared_ptr(work->data); + if (dataWorkerData == nullptr) { + ANS_LOGE("Create dataWorkerData failed."); + delete work; + work = nullptr; + return; + } + + napi_value resultArray = nullptr; + napi_handle_scope scope; + napi_open_handle_scope(dataWorkerData->env, &scope); + napi_create_array(dataWorkerData->env, &resultArray); + int index = 0; + for (auto request : dataWorkerData->requestList) { + napi_value result = nullptr; + napi_create_object(dataWorkerData->env, &result); + if (SetSubscribeCallbackData(dataWorkerData->env, request, + dataWorkerData->sortingMap, dataWorkerData->deleteReason, result)) { + napi_set_element(dataWorkerData->env, resultArray, index, result); + } + } + uint32_t elementCount = 0; + napi_get_array_length(dataWorkerData->env, resultArray, &elementCount); + if (elementCount > 0) { + Common::SetCallback(dataWorkerData->env, dataWorkerData->ref, resultArray); + } + + napi_close_handle_scope(dataWorkerData->env, scope); + + delete dataWorkerData; + dataWorkerData = nullptr; + delete work; +} + +ErrCode SubscriberInstance::OnBatchCancel(const std::vector> &requestList, + const std::shared_ptr &sortingMap, int32_t deleteReason) +{ + ANS_LOGI("OnBatchCancel"); + + if (batchCancelCallbackInfo_.ref == nullptr) { + ANS_LOGI("cancel callback unset"); + return ERR_ANS_INVALID_PARAM; + } + + if (requestList.empty()) { + ANS_LOGE("requestList is empty"); + return ERR_ANS_INVALID_PARAM; + } + + if (sortingMap == nullptr) { + ANS_LOGE("sortingMap is null"); + return ERR_ANS_INVALID_PARAM; + } + ANS_LOGI("OnCanceled notification number = %{public}d", requestList.size()); + ANS_LOGI("OnCanceled sortingMap size = %{public}zu", sortingMap->GetKey().size()); + ANS_LOGI("OnCanceled deleteReason = %{public}d", deleteReason); + + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(batchCancelCallbackInfo_.env, &loop); + if (loop == nullptr) { + ANS_LOGE("loop instance is nullptr"); + return ERR_ANS_INVALID_PARAM; + } + + NotificationReceiveDataWorker *dataWorker = new (std::nothrow) NotificationReceiveDataWorker(); + if (dataWorker == nullptr) { + ANS_LOGE("DataWorker is nullptr."); + return ERR_ANS_INVALID_PARAM; + } + + dataWorker->requestList = requestList; + dataWorker->sortingMap = sortingMap; + dataWorker->deleteReason = deleteReason; + dataWorker->env = canceCallbackInfo_.env; + dataWorker->ref = canceCallbackInfo_.ref; + + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + ANS_LOGE("new work failed"); + delete dataWorker; + dataWorker = nullptr; + return ERR_ANS_INVALID_PARAM; + } + + work->data = reinterpret_cast(dataWorker); + + int ret = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {}, + UvQueueWorkOnBatchCancel, uv_qos_user_initiated); + if (ret != 0) { + delete dataWorker; + dataWorker = nullptr; + delete work; + work = nullptr; + } + return ERR_OK; +} + void UvQueueWorkOnConsumed(uv_work_t *work, int status) { ANS_LOGI("OnConsumed uv_work_t start"); @@ -945,6 +1061,7 @@ void SubscriberInstance::OnBadgeChanged( } } + void SubscriberInstance::SetCancelCallbackInfo(const napi_env &env, const napi_ref &ref) { canceCallbackInfo_.env = env; @@ -1011,6 +1128,13 @@ void SubscriberInstance::SetBadgeCallbackInfo(const napi_env &env, const napi_re setBadgeCallbackInfo_.ref = ref; } +void SubscriberInstance::SetBatchCancelCallbackInfo(const napi_env &env, const napi_ref &ref) +{ + ANS_LOGE("SetBatchCancelCallbackInfo"); + batchCancelCallbackInfo_.env = env; + batchCancelCallbackInfo_.ref = ref; +} + void SubscriberInstance::SetCallbackInfo(const napi_env &env, const std::string &type, const napi_ref &ref) { if (type == CONSUME) { @@ -1035,6 +1159,8 @@ void SubscriberInstance::SetCallbackInfo(const napi_env &env, const std::string SetEnabledNotificationCallbackInfo(env, ref); } else if (type == BADGE_CHANGED) { SetBadgeCallbackInfo(env, ref); + } else if (type == BATCH_CANCEL) { + SetBatchCancelCallbackInfo(env, ref); } else { ANS_LOGW("type is error"); } @@ -1219,6 +1345,19 @@ napi_value GetNotificationSubscriber( napi_create_reference(env, nOnBadgeChanged, 1, &result); subscriberInfo.subscriber->SetCallbackInfo(env, BADGE_CHANGED, result); } + // onBatchCancel?:(data: Array) => void + NAPI_CALL(env, napi_has_named_property(env, value, "onBatchCancel", &hasProperty)); + if (hasProperty) { + napi_value nOnBadgeChanged = nullptr; + napi_get_named_property(env, value, "onBatchCancel", &nOnBadgeChanged); + NAPI_CALL(env, napi_typeof(env, nOnBadgeChanged, &valuetype)); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } + napi_create_reference(env, nOnBadgeChanged, 1, &result); + subscriberInfo.subscriber->SetCallbackInfo(env, BADGE_CHANGED, result); + } return Common::NapiGetNull(env); } @@ -1460,4 +1599,4 @@ void DelDeletingSubscriber(SubscriberInstance *subscriber) } } } // namespace NotificationNapi -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/interfaces/inner_api/notification_subscriber.h b/interfaces/inner_api/notification_subscriber.h index eec196e3ce36fb05c53793048f94caef3d0c7877..d4ae9cf01d91be3bf80a26b7c1e8de14bf358ec5 100644 --- a/interfaces/inner_api/notification_subscriber.h +++ b/interfaces/inner_api/notification_subscriber.h @@ -18,6 +18,7 @@ #include "ans_manager_interface.h" #include "ans_subscriber_stub.h" +#include "errors.h" #include "notification_request.h" #include "notification_sorting.h" #include "notification_sorting_map.h" @@ -94,6 +95,14 @@ public: */ virtual void OnBadgeChanged(const std::shared_ptr &badgeData) = 0; + /** + * @brief The callback function on the badge number changed. + * + * @param badgeData Indicates the BadgeNumberCallbackData object. + */ + virtual ErrCode OnBatchCancel(const std::vector> &requestList, + const std::shared_ptr &sortingMap, int32_t deleteReason) = 0; + private: class SubscriberImpl final : public AnsSubscriberStub { public: @@ -154,4 +163,4 @@ private: } // namespace Notification } // namespace OHOS -#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SUBSCRIBER_H \ No newline at end of file +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SUBSCRIBER_H