From 7a27f06ca993279330035ec9fe3afe11d364abea Mon Sep 17 00:00:00 2001 From: fangJinliang1 Date: Sat, 8 Oct 2022 22:22:58 +0800 Subject: [PATCH] add new interface Signed-off-by: fangJinliang1 Change-Id: I40a28d17274ed0dabbec27fc97eb8ccca7f726ad Signed-off-by: fangJinliang1 --- .../core/common/include/ans_inner_errors.h | 19 + frameworks/js/napi/BUILD.gn | 6 +- frameworks/js/napi/include/ans_template.h | 14 + frameworks/js/napi/include/cancel.h | 52 +- frameworks/js/napi/include/common.h | 12 + frameworks/js/napi/include/display_badge.h | 30 + frameworks/js/napi/include/distributed.h | 84 ++ frameworks/js/napi/include/disturb_mode.h | 39 + .../js/napi/include/enable_notification.h | 32 + frameworks/js/napi/include/get_active.h | 9 + .../js/napi/include/manager/init_module.h | 51 ++ .../js/napi/include/manager/napi_cancel.h | 32 + .../napi/include/manager/napi_display_badge.h | 28 + .../napi/include/manager/napi_distributed.h | 36 + .../napi/include/manager/napi_disturb_mode.h | 29 + .../manager/napi_enable_notification.h | 30 + .../js/napi/include/manager/napi_get_active.h | 31 + .../js/napi/include/manager/napi_publish.h | 29 + .../js/napi/include/manager/napi_slot.h | 188 +++++ .../js/napi/include/manager/napi_template.h | 29 + frameworks/js/napi/include/publish.h | 18 + frameworks/js/napi/include/remove.h | 42 + frameworks/js/napi/include/subscribe.h | 11 + .../js/napi/include/subscribe/init_module.h | 51 ++ .../js/napi/include/subscribe/napi_remove.h | 31 + .../napi/include/subscribe/napi_subscribe.h | 28 + frameworks/js/napi/include/unsubscribe.h | 21 +- frameworks/js/napi/src/ans_template.cpp | 14 - frameworks/js/napi/src/cancel.cpp | 42 - frameworks/js/napi/src/common.cpp | 122 ++- frameworks/js/napi/src/display_badge.cpp | 29 +- frameworks/js/napi/src/distributed.cpp | 78 -- frameworks/js/napi/src/disturb_mode.cpp | 36 - .../js/napi/src/enable_notification.cpp | 29 - frameworks/js/napi/src/get_active.cpp | 9 - frameworks/js/napi/src/init.cpp | 2 + frameworks/js/napi/src/manager/BUILD.gn | 86 +++ .../js/napi/src/manager/init_module.cpp | 109 +++ .../js/napi/src/manager/napi_cancel.cpp | 257 +++++++ .../napi/src/manager/napi_display_badge.cpp | 160 ++++ .../js/napi/src/manager/napi_distributed.cpp | 545 +++++++++++++ .../js/napi/src/manager/napi_disturb_mode.cpp | 221 ++++++ .../src/manager/napi_enable_notification.cpp | 256 +++++++ .../js/napi/src/manager/napi_get_active.cpp | 274 +++++++ .../js/napi/src/manager/napi_publish.cpp | 205 +++++ frameworks/js/napi/src/manager/napi_slot.cpp | 723 ++++++++++++++++++ .../js/napi/src/manager/napi_template.cpp | 83 ++ frameworks/js/napi/src/publish.cpp | 12 - frameworks/js/napi/src/remove.cpp | 34 - frameworks/js/napi/src/subscribe.cpp | 8 - frameworks/js/napi/src/subscribe/BUILD.gn | 73 ++ .../js/napi/src/subscribe/init_module.cpp | 68 ++ .../js/napi/src/subscribe/napi_remove.cpp | 215 ++++++ .../js/napi/src/subscribe/napi_subscribe.cpp | 178 +++++ frameworks/js/napi/src/unsubscribe.cpp | 13 - 55 files changed, 4551 insertions(+), 312 deletions(-) create mode 100644 frameworks/js/napi/include/manager/init_module.h create mode 100644 frameworks/js/napi/include/manager/napi_cancel.h create mode 100644 frameworks/js/napi/include/manager/napi_display_badge.h create mode 100644 frameworks/js/napi/include/manager/napi_distributed.h create mode 100644 frameworks/js/napi/include/manager/napi_disturb_mode.h create mode 100644 frameworks/js/napi/include/manager/napi_enable_notification.h create mode 100644 frameworks/js/napi/include/manager/napi_get_active.h create mode 100644 frameworks/js/napi/include/manager/napi_publish.h create mode 100644 frameworks/js/napi/include/manager/napi_slot.h create mode 100644 frameworks/js/napi/include/manager/napi_template.h create mode 100644 frameworks/js/napi/include/subscribe/init_module.h create mode 100644 frameworks/js/napi/include/subscribe/napi_remove.h create mode 100644 frameworks/js/napi/include/subscribe/napi_subscribe.h create mode 100644 frameworks/js/napi/src/manager/BUILD.gn create mode 100644 frameworks/js/napi/src/manager/init_module.cpp create mode 100644 frameworks/js/napi/src/manager/napi_cancel.cpp create mode 100644 frameworks/js/napi/src/manager/napi_display_badge.cpp create mode 100644 frameworks/js/napi/src/manager/napi_distributed.cpp create mode 100644 frameworks/js/napi/src/manager/napi_disturb_mode.cpp create mode 100644 frameworks/js/napi/src/manager/napi_enable_notification.cpp create mode 100644 frameworks/js/napi/src/manager/napi_get_active.cpp create mode 100644 frameworks/js/napi/src/manager/napi_publish.cpp create mode 100644 frameworks/js/napi/src/manager/napi_slot.cpp create mode 100644 frameworks/js/napi/src/manager/napi_template.cpp create mode 100644 frameworks/js/napi/src/subscribe/BUILD.gn create mode 100644 frameworks/js/napi/src/subscribe/init_module.cpp create mode 100644 frameworks/js/napi/src/subscribe/napi_remove.cpp create mode 100644 frameworks/js/napi/src/subscribe/napi_subscribe.cpp diff --git a/frameworks/core/common/include/ans_inner_errors.h b/frameworks/core/common/include/ans_inner_errors.h index 3fd34d197..92cfe55e0 100644 --- a/frameworks/core/common/include/ans_inner_errors.h +++ b/frameworks/core/common/include/ans_inner_errors.h @@ -78,6 +78,25 @@ enum ErrorCode : uint32_t { ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED, ERR_ANS_DLP_HAP, }; + +// Common error code +const uint32_t ERROR_PERMISSION_DENIED = 201; // No permission to call the interface. +const uint32_t ERROR_PARAM_INVALID = 401; // Invalid input parameter. +const uint32_t ERROR_SYSTEM_CAP_ERROR = 801; // The specified SystemCapability names was not found. + +// Notification error code +const int32_t ERROR_INTERNAL_ERROR = 1600001; // Internal error. +const int32_t ERROR_IPC_ERROR = 1600002; // marshalling or unmarshalling error. +const int32_t ERROR_SERVICE_CONNECT_ERROR = 1600003; // Failed to connect service. +const int32_t ERROR_NOTIFICATION_CLOSED = 1600004; // Notification is not enabled. +const int32_t ERROR_SLOT_CLOSED = 1600005; // Notification slot is not enabled. +const int32_t ERROR_NOTIFICATION_UNREMOVABLE = 1600006; // Notification is not allowed to remove. +const int32_t ERROR_NOTIFICATION_NOT_EXIST = 1600007; // The notification is not exist. +const int32_t ERROR_USER_NOT_EXIST = 1600008; // The user is not exist. +const int32_t ERROR_OVER_MAX_NUM_PER_SECOND = 1600009; // Over max number notifications per second. +const int32_t ERROR_DISTRIBUTED_OPERATION_FAILED = 1600010; // Distributed operation failed. +const int32_t ERROR_READ_TEMPLATE_CONFIG_FAILED = 1600011; // Read template config failed. +const int32_t ERROR_BUNDLE_NOT_FOUND = 17700001; // The specified bundle name was not found. } // namespace Notification } // namespace OHOS diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 2c9a2b832..c3e7b5496 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -19,7 +19,11 @@ group("napi_reminder") { } group("napi_notification") { - deps = [ ":notification" ] + deps = [ + ":notification", + "src/manager:notificationmanager", + "src/subscribe:notificationsubscribe", + ] } cflags = [] diff --git a/frameworks/js/napi/include/ans_template.h b/frameworks/js/napi/include/ans_template.h index c3ed2dded..7f40bc2e1 100644 --- a/frameworks/js/napi/include/ans_template.h +++ b/frameworks/js/napi/include/ans_template.h @@ -22,7 +22,21 @@ namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; +struct TemplateName { + std::string templateName = ""; + bool support = false; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoTemplate { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + TemplateName params; + CallbackPromiseInfo info; +}; + napi_value IsSupportTemplate(napi_env env, napi_callback_info info); +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, TemplateName& params); } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/include/cancel.h b/frameworks/js/napi/include/cancel.h index 30b357af2..539a12afa 100644 --- a/frameworks/js/napi/include/cancel.h +++ b/frameworks/js/napi/include/cancel.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_CANCEL_H -#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_CANCEL_H +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_CANCEL_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_CANCEL_H #include "common.h" @@ -22,11 +22,57 @@ namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; +struct ParametersInfoCancel { + int32_t id = 0; + std::string label = ""; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoCancel { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + int32_t id = 0; + std::string label; + CallbackPromiseInfo info; +}; + +struct ParametersInfoCancelGroup { + std::string groupName = ""; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoCancelGroup { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + ParametersInfoCancelGroup params {}; +}; + +struct ParametersInfoCancelAsBundle { + int32_t id = 0; + std::string representativeBundle = ""; + int32_t userId = 0; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoCancelAsBundle { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + int32_t id = 0; + std::string representativeBundle = ""; + int32_t userId = 0; + CallbackPromiseInfo info; +}; + napi_value Cancel(napi_env env, napi_callback_info info); napi_value CancelAll(napi_env env, napi_callback_info info); napi_value CancelGroup(napi_env env, napi_callback_info info); napi_value CancelAsBundle(napi_env env, napi_callback_info info); + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, ParametersInfoCancelAsBundle ¶s); +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, ParametersInfoCancelGroup ¶s); +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, ParametersInfoCancel ¶s); } // namespace NotificationNapi } // namespace OHOS -#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_CANCEL_H \ No newline at end of file +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_CANCEL_H \ No newline at end of file diff --git a/frameworks/js/napi/include/common.h b/frameworks/js/napi/include/common.h index 10181436b..621f0ecb4 100644 --- a/frameworks/js/napi/include/common.h +++ b/frameworks/js/napi/include/common.h @@ -1539,7 +1539,19 @@ public: static napi_value GetNotificationBadgeNumber( const napi_env &env, const napi_value &value, NotificationRequest &request); + /** + * @brief Create a napi value with specified error object for callback + * + * @param env Indicates the environment that the API is invoked under + * @param errCode Indicates specified err code + * @return Returns a napi value with specified error object for callback + */ + static napi_value CreateErrorValue(napi_env env, int32_t errCode); + static bool IsValidRemoveReason(int32_t reasonType); + static void NapiThrow(napi_env env, int32_t errCode); + static int32_t ErrorToExternal(uint32_t errCode); + static void CreateReturnValue(const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result); private: static const int32_t ARGS_ONE = 1; diff --git a/frameworks/js/napi/include/display_badge.h b/frameworks/js/napi/include/display_badge.h index 671e53300..a8cf2824f 100644 --- a/frameworks/js/napi/include/display_badge.h +++ b/frameworks/js/napi/include/display_badge.h @@ -21,8 +21,38 @@ namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; +struct EnableBadgeParams { + NotificationBundleOption option; + bool enable = false; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoEnableBadge { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + EnableBadgeParams params; + CallbackPromiseInfo info; +}; + +struct IsDisplayBadgeParams { + NotificationBundleOption option; + napi_ref callback = nullptr; + bool hasBundleOption = false; +}; + +struct AsyncCallbackInfoIsDisplayBadge { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + IsDisplayBadgeParams params; + CallbackPromiseInfo info; + bool enabled = false; +}; + napi_value DisplayBadge(napi_env env, napi_callback_info info); napi_value IsBadgeDisplayed(napi_env env, napi_callback_info info); + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnableBadgeParams ¶ms); +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, IsDisplayBadgeParams ¶ms); } // namespace NotificationNapi } // namespace OHOS #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_DISPLAY_BADGE_H \ No newline at end of file diff --git a/frameworks/js/napi/include/distributed.h b/frameworks/js/napi/include/distributed.h index 630de77cd..de5dfa4a4 100644 --- a/frameworks/js/napi/include/distributed.h +++ b/frameworks/js/napi/include/distributed.h @@ -22,6 +22,84 @@ namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; +struct AsyncCallbackInfoIsEnabled { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + bool enable = false; +}; + +struct EnabledParams { + napi_ref callback = nullptr; + bool enable = false; +}; + +struct AsyncCallbackInfoEnabled { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + EnabledParams params; +}; + +struct EnabledByBundleParams { + NotificationBundleOption option; + napi_ref callback = nullptr; + bool enable = false; +}; + +struct AsyncCallbackInfoEnabledByBundle { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + EnabledByBundleParams params; +}; + +struct IsEnabledByBundleParams { + NotificationBundleOption option; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoIsEnabledByBundle { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + IsEnabledByBundleParams params; + bool enable = false; +}; + +struct AsyncCallbackInfoGetRemindType { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + NotificationConstant::RemindType remindType = NotificationConstant::RemindType::NONE; +}; + +struct EnabledWithoutAppParams { + int32_t userId = SUBSCRIBE_USER_INIT; + bool enable = false; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoEnabledWithoutApp { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + EnabledWithoutAppParams params; +}; + +struct GetEnabledWithoutAppParams { + int32_t userId = SUBSCRIBE_USER_INIT; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoGetEnabledWithoutApp { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + GetEnabledWithoutAppParams params; + bool enable = false; +}; + napi_value IsDistributedEnabled(napi_env env, napi_callback_info info); napi_value EnableDistributed(napi_env env, napi_callback_info info); napi_value EnableDistributedByBundle(napi_env env, napi_callback_info info); @@ -30,6 +108,12 @@ napi_value IsDistributedEnableByBundle(napi_env env, napi_callback_info info); napi_value GetDeviceRemindType(napi_env env, napi_callback_info info); napi_value SetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info); napi_value GetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info); + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnabledParams ¶ms); +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnabledByBundleParams ¶ms); +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, IsEnabledByBundleParams ¶ms); +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnabledWithoutAppParams ¶ms); +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetEnabledWithoutAppParams ¶ms); } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/include/disturb_mode.h b/frameworks/js/napi/include/disturb_mode.h index 5c0fef0a7..a0309b909 100644 --- a/frameworks/js/napi/include/disturb_mode.h +++ b/frameworks/js/napi/include/disturb_mode.h @@ -21,9 +21,48 @@ namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; +struct SetDoNotDisturbDateParams { + NotificationDoNotDisturbDate date; + bool hasUserId = false; + int32_t userId = SUBSCRIBE_USER_INIT; + napi_ref callback = nullptr; +}; + +struct GetDoNotDisturbDateParams { + bool hasUserId = false; + int32_t userId = SUBSCRIBE_USER_INIT; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoSetDoNotDisturb { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + SetDoNotDisturbDateParams params; + CallbackPromiseInfo info; +}; + +struct AsyncCallbackInfoGetDoNotDisturb { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + GetDoNotDisturbDateParams params; + NotificationDoNotDisturbDate date; + CallbackPromiseInfo info; +}; + +struct AsyncCallbackInfoSupportDoNotDisturb { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + napi_ref callback = nullptr; + bool isSupported = false; + CallbackPromiseInfo info; +}; + napi_value SetDoNotDisturbDate(napi_env env, napi_callback_info info); napi_value GetDoNotDisturbDate(napi_env env, napi_callback_info info); napi_value SupportDoNotDisturbMode(napi_env env, napi_callback_info info); + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, SetDoNotDisturbDateParams ¶ms); +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetDoNotDisturbDateParams ¶ms); } // namespace NotificationNapi } // namespace OHOS #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_DISTURB_MODE_H \ No newline at end of file diff --git a/frameworks/js/napi/include/enable_notification.h b/frameworks/js/napi/include/enable_notification.h index 20f468012..f8656ed15 100644 --- a/frameworks/js/napi/include/enable_notification.h +++ b/frameworks/js/napi/include/enable_notification.h @@ -21,10 +21,42 @@ namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; +struct EnableParams { + NotificationBundleOption option; + bool enable = false; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoEnable { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + EnableParams params; + CallbackPromiseInfo info; +}; + +struct IsEnableParams { + NotificationBundleOption option; + napi_ref callback = nullptr; + bool hasBundleOption = false; + int32_t userId = SUBSCRIBE_USER_INIT; + bool hasUserId = false; +}; + +struct AsyncCallbackInfoIsEnable { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + IsEnableParams params; + CallbackPromiseInfo info; + bool allowed = false; +}; + napi_value EnableNotification(napi_env env, napi_callback_info info); napi_value IsNotificationEnabled(napi_env env, napi_callback_info info); napi_value IsNotificationEnabledSelf(napi_env env, napi_callback_info info); napi_value RequestEnableNotification(napi_env env, napi_callback_info info); + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnableParams ¶ms); +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, IsEnableParams ¶ms); } // namespace NotificationNapi } // namespace OHOS #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_ENABLE_NOTIFICATION_H \ No newline at end of file diff --git a/frameworks/js/napi/include/get_active.h b/frameworks/js/napi/include/get_active.h index 837747dc7..2fe7457c7 100644 --- a/frameworks/js/napi/include/get_active.h +++ b/frameworks/js/napi/include/get_active.h @@ -22,6 +22,15 @@ namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; +struct AsyncCallbackInfoActive { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + std::vector> notifications; + std::vector> requests; + uint64_t num = 0; +}; + napi_value GetAllActiveNotifications(napi_env env, napi_callback_info info); napi_value GetActiveNotifications(napi_env env, napi_callback_info info); napi_value GetActiveNotificationCount(napi_env env, napi_callback_info info); diff --git a/frameworks/js/napi/include/manager/init_module.h b/frameworks/js/napi/include/manager/init_module.h new file mode 100644 index 000000000..05cc3935e --- /dev/null +++ b/frameworks/js/napi/include/manager/init_module.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 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. + */ +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_MANAGER_INIT_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_MANAGER_INIT_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace NotificationNapi { +#ifdef __cplusplus +extern "C" { +#endif + +__attribute__((constructor)) void RegisterModule(void); +napi_value NotificationManagerInit(napi_env env, napi_value exports); +static napi_value Init(napi_env env, napi_value exports); + +#ifdef __cplusplus +} +#endif + +/* + * Module define + */ +napi_module _module_manager = { + + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "notificationManager", + .nm_priv = ((void *)0), + .reserved = {0} + +}; +} // namespace NotificationNapi +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_MANAGER_INIT_H diff --git a/frameworks/js/napi/include/manager/napi_cancel.h b/frameworks/js/napi/include/manager/napi_cancel.h new file mode 100644 index 000000000..c416bbc3d --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_cancel.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_CANCEL_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_CANCEL_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value Napi_Cancel(napi_env env, napi_callback_info info); +napi_value Napi_CancelAll(napi_env env, napi_callback_info info); +napi_value Napi_CancelGroup(napi_env env, napi_callback_info info); +napi_value Napi_CancelAsBundle(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_CANCEL_H \ No newline at end of file diff --git a/frameworks/js/napi/include/manager/napi_display_badge.h b/frameworks/js/napi/include/manager/napi_display_badge.h new file mode 100644 index 000000000..2c9ac573a --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_display_badge.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022 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. + */ +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISPLAY_BADGE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISPLAY_BADGE_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value Napi_DisplayBadge(napi_env env, napi_callback_info info); +napi_value Napi_IsBadgeDisplayed(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISPLAY_BADGE_H \ No newline at end of file diff --git a/frameworks/js/napi/include/manager/napi_distributed.h b/frameworks/js/napi/include/manager/napi_distributed.h new file mode 100644 index 000000000..a8e6f0be5 --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_distributed.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 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. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISTRIBUTED_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISTRIBUTED_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value Napi_IsDistributedEnabled(napi_env env, napi_callback_info info); +napi_value Napi_EnableDistributed(napi_env env, napi_callback_info info); +napi_value Napi_EnableDistributedByBundle(napi_env env, napi_callback_info info); +napi_value Napi_EnableDistributedSelf(napi_env env, napi_callback_info info); +napi_value Napi_IsDistributedEnableByBundle(napi_env env, napi_callback_info info); +napi_value Napi_GetDeviceRemindType(napi_env env, napi_callback_info info); +napi_value Napi_SetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info); +napi_value Napi_GetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISTRIBUTED_H \ No newline at end of file diff --git a/frameworks/js/napi/include/manager/napi_disturb_mode.h b/frameworks/js/napi/include/manager/napi_disturb_mode.h new file mode 100644 index 000000000..57e236947 --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_disturb_mode.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 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. + */ +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISTURB_MODE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISTURB_MODE_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value Napi_SetDoNotDisturbDate(napi_env env, napi_callback_info info); +napi_value Napi_GetDoNotDisturbDate(napi_env env, napi_callback_info info); +napi_value Napi_SupportDoNotDisturbMode(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISTURB_MODE_H \ No newline at end of file diff --git a/frameworks/js/napi/include/manager/napi_enable_notification.h b/frameworks/js/napi/include/manager/napi_enable_notification.h new file mode 100644 index 000000000..c28c86a9a --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_enable_notification.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022 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. + */ +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_ENABLE_NOTIFICATION_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_ENABLE_NOTIFICATION_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value Napi_EnableNotification(napi_env env, napi_callback_info info); +napi_value Napi_IsNotificationEnabled(napi_env env, napi_callback_info info); +napi_value Napi_IsNotificationEnabledSelf(napi_env env, napi_callback_info info); +napi_value Napi_RequestEnableNotification(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_ENABLE_NOTIFICATION_H \ No newline at end of file diff --git a/frameworks/js/napi/include/manager/napi_get_active.h b/frameworks/js/napi/include/manager/napi_get_active.h new file mode 100644 index 000000000..5f950bdfb --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_get_active.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 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. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_GET_ACTIVE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_GET_ACTIVE_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value Napi_GetAllActiveNotifications(napi_env env, napi_callback_info info); +napi_value Napi_GetActiveNotifications(napi_env env, napi_callback_info info); +napi_value Napi_GetActiveNotificationCount(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_GET_ACTIVE_H \ No newline at end of file diff --git a/frameworks/js/napi/include/manager/napi_publish.h b/frameworks/js/napi/include/manager/napi_publish.h new file mode 100644 index 000000000..51d995a2a --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_publish.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 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. + */ +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_PUBLISH_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_PUBLISH_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value Napi_Publish(napi_env env, napi_callback_info info); +napi_value Napi_ShowNotification(napi_env env, napi_callback_info info); +napi_value Napi_PublishAsBundle(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_PUBLISH_H \ No newline at end of file diff --git a/frameworks/js/napi/include/manager/napi_slot.h b/frameworks/js/napi/include/manager/napi_slot.h new file mode 100644 index 000000000..e48814b6c --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_slot.h @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2022 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. + */ +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_SLOT_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_SLOT_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +struct ParametersInfoAddSlot { + NotificationSlot slot; + NotificationConstant::SlotType inType = NotificationConstant::SlotType::OTHER; + bool isAddSlotByType = false; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoAddSlot { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + NotificationSlot slot; + NotificationConstant::SlotType inType = NotificationConstant::SlotType::OTHER; + bool isAddSlotByType = false; + CallbackPromiseInfo info; +}; + +struct ParametersInfoAddSlots { + std::vector slots; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoAddSlots { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + std::vector slots; + CallbackPromiseInfo info; +}; + +struct ParametersInfoSetSlotByBundle { + NotificationBundleOption option; + std::vector> slots; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoSetSlotByBundle { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + ParametersInfoSetSlotByBundle params; + CallbackPromiseInfo info; +}; + +struct ParametersInfoGetSlot { + NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoGetSlot { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER; + CallbackPromiseInfo info; + sptr slot = nullptr; +}; + +struct ParametersInfoGetSlotNumByBundle { + NotificationBundleOption option; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoGetSlotNumByBundle { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + ParametersInfoGetSlotNumByBundle params; + CallbackPromiseInfo info; + uint64_t num = 0; +}; + +struct AsyncCallbackInfoGetSlots { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + std::vector> slots; +}; + +struct ParametersInfoGetSlotsByBundle { + NotificationBundleOption option; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoGetSlotsByBundle { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + ParametersInfoGetSlotsByBundle params; + CallbackPromiseInfo info; + std::vector> slots; +}; + +struct ParametersInfoRemoveSlot { + NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoRemoveSlot { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER; + CallbackPromiseInfo info; +}; + +struct AsyncCallbackInfoRemoveAllSlots { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; +}; + +struct ParametersInfoEnableSlot { + NotificationBundleOption option; + NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER; + bool enable = false; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoInfoEnableSlot { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + ParametersInfoEnableSlot params; + CallbackPromiseInfo info; +}; + +struct ParametersInfoIsEnableSlot { + NotificationBundleOption option; + NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoInfoIsEnableSlot { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + ParametersInfoIsEnableSlot params; + bool isEnable = false; + CallbackPromiseInfo info; +}; + +napi_value Napi_AddSlot(napi_env env, napi_callback_info info); +napi_value Napi_AddSlots(napi_env env, napi_callback_info info); +napi_value Napi_SetSlotByBundle(napi_env env, napi_callback_info info); +napi_value Napi_GetSlot(napi_env env, napi_callback_info info); +napi_value Napi_GetSlotNumByBundle(napi_env env, napi_callback_info info); +napi_value Napi_GetSlots(napi_env env, napi_callback_info info); +napi_value Napi_GetSlotsByBundle(napi_env env, napi_callback_info info); +napi_value Napi_RemoveSlot(napi_env env, napi_callback_info info); +napi_value Napi_RemoveAllSlots(napi_env env, napi_callback_info info); +napi_value Napi_EnableNotificationSlot(napi_env env, napi_callback_info info); +napi_value Napi_IsEnableNotificationSlot(napi_env env, napi_callback_info info); + +napi_value ParseParametersByAddSlot(const napi_env &env, const napi_callback_info &info, ParametersInfoAddSlot ¶s); +napi_value ParseParametersByAddSlots( + const napi_env &env, const napi_callback_info &info, ParametersInfoAddSlots ¶s); +napi_value ParseParametersSetSlotByBundle( + const napi_env &env, const napi_callback_info &info, ParametersInfoSetSlotByBundle ¶ms); +napi_value ParseParametersByGetSlot(const napi_env &env, const napi_callback_info &info, ParametersInfoGetSlot ¶s); +napi_value ParseParametersGetSlotNumByBundle( + const napi_env &env, const napi_callback_info &info, ParametersInfoGetSlotNumByBundle ¶ms); +napi_value ParseParametersGetSlotsByBundle( + const napi_env &env, const napi_callback_info &info, ParametersInfoGetSlotsByBundle ¶ms); +napi_value ParseParametersByRemoveSlot( + const napi_env &env, const napi_callback_info &info, ParametersInfoRemoveSlot ¶s); +napi_value ParseParametersEnableSlot( + const napi_env &env, const napi_callback_info &info, ParametersInfoEnableSlot ¶ms); +napi_value ParseParametersIsEnableSlot( + const napi_env &env, const napi_callback_info &info, ParametersInfoIsEnableSlot ¶ms); +} // namespace NotificationNapi +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_SLOT_H \ No newline at end of file diff --git a/frameworks/js/napi/include/manager/napi_template.h b/frameworks/js/napi/include/manager/napi_template.h new file mode 100644 index 000000000..00aa09bba --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_template.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 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. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_TEMPLATE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_TEMPLATE_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value Napi_IsSupportTemplate(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_TEMPLATE_H \ No newline at end of file diff --git a/frameworks/js/napi/include/publish.h b/frameworks/js/napi/include/publish.h index 44de7e95b..0c8b81ed2 100644 --- a/frameworks/js/napi/include/publish.h +++ b/frameworks/js/napi/include/publish.h @@ -20,9 +20,27 @@ namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; + +struct AsyncCallbackInfoPublish { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + NotificationRequest request; + CallbackPromiseInfo info; +}; + +struct ParametersInfoPublish { + NotificationRequest request; + napi_ref callback = nullptr; +}; + napi_value Publish(napi_env env, napi_callback_info info); napi_value ShowNotification(napi_env env, napi_callback_info info); napi_value PublishAsBundle(napi_env env, napi_callback_info info); + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, ParametersInfoPublish ¶ms); +napi_value ParseShowOptions(const napi_env &env, const napi_callback_info &info, ParametersInfoPublish ¶ms); +napi_value ParsePublishAsBundleParameters( + const napi_env &env, const napi_callback_info &info, ParametersInfoPublish ¶ms); } // namespace NotificationNapi } // namespace OHOS #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_PUBLISH_H \ No newline at end of file diff --git a/frameworks/js/napi/include/remove.h b/frameworks/js/napi/include/remove.h index 5beb3a8cc..36594bcb1 100644 --- a/frameworks/js/napi/include/remove.h +++ b/frameworks/js/napi/include/remove.h @@ -22,10 +22,52 @@ namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; +struct BundleAndKeyInfo { + NotificationBundleOption option; + NotificationKey key; +}; + +struct RemoveParams { + std::optional hashcode {}; + std::optional bundleAndKeyInfo {}; + int32_t userId = SUBSCRIBE_USER_INIT; + int32_t removeReason = NotificationConstant::CANCEL_REASON_DELETE; + bool hasUserId = false; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoRemove { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + RemoveParams params {}; + CallbackPromiseInfo info; +}; + +struct RemoveParamsGroupByBundle { + NotificationBundleOption option; + std::string groupName = ""; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoRemoveGroupByBundle { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + RemoveParamsGroupByBundle params {}; + CallbackPromiseInfo info; +}; + napi_value Remove(napi_env env, napi_callback_info info); napi_value RemoveAsBundle(napi_env env, napi_callback_info info); napi_value RemoveAll(napi_env env, napi_callback_info info); napi_value RemoveGroupByBundle(napi_env env, napi_callback_info info); + +bool ParseBundleOptionTypeParams(const napi_env &env, napi_value* argv, size_t argc, RemoveParams ¶ms); +bool ParseHashcodeTypeParams(const napi_env &env, napi_value* argv, size_t argc, RemoveParams ¶ms); +bool ParseCallbackFunc(const napi_env &env, const napi_value &value, RemoveParams ¶ms); +bool ParseRemoveReason(const napi_env &env, const napi_value &value, RemoveParams ¶ms); +bool ParseParameters(const napi_env &env, const napi_callback_info &info, RemoveParams ¶ms); +napi_value ParseParametersByRemoveAll(const napi_env &env, const napi_callback_info &info, RemoveParams ¶ms); +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, RemoveParamsGroupByBundle ¶ms); } // namespace NotificationNapi } // namespace OHOS #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_REMOVE_H \ No newline at end of file diff --git a/frameworks/js/napi/include/subscribe.h b/frameworks/js/napi/include/subscribe.h index 06c1286de..44b4a4eee 100644 --- a/frameworks/js/napi/include/subscribe.h +++ b/frameworks/js/napi/include/subscribe.h @@ -151,6 +151,14 @@ struct SubscriberInstancesInfo { SubscriberInstance *subscriber = nullptr; }; +struct AsyncCallbackInfoSubscribe { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + SubscriberInstance *objectInfo = nullptr; + NotificationSubscribeInfo subscriberInfo; + CallbackPromiseInfo info; +}; + static std::mutex mutex_; static thread_local std::vector subscriberInstances_; @@ -165,6 +173,9 @@ bool AddDeletingSubscriber(SubscriberInstance *subscriber); void DelDeletingSubscriber(SubscriberInstance *subscriber); napi_value Subscribe(napi_env env, napi_callback_info info); + +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 diff --git a/frameworks/js/napi/include/subscribe/init_module.h b/frameworks/js/napi/include/subscribe/init_module.h new file mode 100644 index 000000000..a6d33b947 --- /dev/null +++ b/frameworks/js/napi/include/subscribe/init_module.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021 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. + */ +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_INIT_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_INIT_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace NotificationNapi { +#ifdef __cplusplus +extern "C" { +#endif + +__attribute__((constructor)) void RegisterModule(void); +napi_value NotificationSubscribeInit(napi_env env, napi_value exports); +static napi_value Init(napi_env env, napi_value exports); + +#ifdef __cplusplus +} +#endif + +/* + * Module define + */ +napi_module _module_subscribe = { + + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "notificationSubscribe", + .nm_priv = ((void *)0), + .reserved = {0} + +}; +} // namespace NotificationNapi +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_INIT_H diff --git a/frameworks/js/napi/include/subscribe/napi_remove.h b/frameworks/js/napi/include/subscribe/napi_remove.h new file mode 100644 index 000000000..4cb069b2c --- /dev/null +++ b/frameworks/js/napi/include/subscribe/napi_remove.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 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. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_REMOVE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_REMOVE_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value Napi_Remove(napi_env env, napi_callback_info info); +napi_value Napi_RemoveAsBundle(napi_env env, napi_callback_info info); +napi_value Napi_RemoveAll(napi_env env, napi_callback_info info); +napi_value Napi_RemoveGroupByBundle(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_REMOVE_H \ No newline at end of file diff --git a/frameworks/js/napi/include/subscribe/napi_subscribe.h b/frameworks/js/napi/include/subscribe/napi_subscribe.h new file mode 100644 index 000000000..d47116921 --- /dev/null +++ b/frameworks/js/napi/include/subscribe/napi_subscribe.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 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. + */ +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_SUBSCRIBE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_SUBSCRIBE_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value Napi_Subscribe(napi_env env, napi_callback_info info); +napi_value Napi_Unsubscribe(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_SUBSCRIBE_H \ No newline at end of file diff --git a/frameworks/js/napi/include/unsubscribe.h b/frameworks/js/napi/include/unsubscribe.h index b40c3a276..d3db0dfba 100644 --- a/frameworks/js/napi/include/unsubscribe.h +++ b/frameworks/js/napi/include/unsubscribe.h @@ -13,16 +13,31 @@ * limitations under the License. */ -#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_UNSUBSCRIBE_H -#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_UNSUBSCRIBE_H +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_UNSUBSCRIBE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_UNSUBSCRIBE_H #include "common.h" +#include "subscribe.h" namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; +struct AsyncCallbackInfoUnsubscribe { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + SubscriberInstance *objectInfo = nullptr; + CallbackPromiseInfo info; +}; + +struct ParametersInfoUnsubscribe { + SubscriberInstance *objectInfo = nullptr; + napi_ref callback = nullptr; +}; + napi_value Unsubscribe(napi_env env, napi_callback_info info); + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, ParametersInfoUnsubscribe ¶s); } // namespace NotificationNapi } // namespace OHOS -#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_UNSUBSCRIBE_H \ No newline at end of file +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_UNSUBSCRIBE_H \ No newline at end of file diff --git a/frameworks/js/napi/src/ans_template.cpp b/frameworks/js/napi/src/ans_template.cpp index 7110af5a7..0321e1eaa 100644 --- a/frameworks/js/napi/src/ans_template.cpp +++ b/frameworks/js/napi/src/ans_template.cpp @@ -14,25 +14,11 @@ */ #include "ans_template.h" -#include "cancel.h" namespace OHOS { namespace NotificationNapi { const int IS_TEMPLATE_MAX_PARA = 2; -struct TemplateName { - std::string templateName = ""; - bool support = false; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoTemplate { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - TemplateName params; - CallbackPromiseInfo info; -}; - napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, TemplateName& params) { ANS_LOGI("enter"); diff --git a/frameworks/js/napi/src/cancel.cpp b/frameworks/js/napi/src/cancel.cpp index 7c418bce3..9121d2d12 100644 --- a/frameworks/js/napi/src/cancel.cpp +++ b/frameworks/js/napi/src/cancel.cpp @@ -22,48 +22,6 @@ constexpr int8_t CANCEL_GROUP_MAX_PARA = 2; constexpr int8_t CANCEL_GROUP_MIN_PARA = 1; constexpr int8_t CANCEL_AS_BUNDLE_MAX_PARA = 4; -struct ParametersInfoCancel { - int32_t id = 0; - std::string label = ""; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoCancel { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - int32_t id = 0; - std::string label; - CallbackPromiseInfo info; -}; - -struct ParametersInfoCancelGroup { - std::string groupName = ""; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoCancelGroup { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - CallbackPromiseInfo info; - ParametersInfoCancelGroup params {}; -}; - -struct ParametersInfoCancelAsBundle { - int32_t id = 0; - std::string representativeBundle = ""; - int32_t userId = 0; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoCancelAsBundle { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - int32_t id = 0; - std::string representativeBundle = ""; - int32_t userId = 0; - CallbackPromiseInfo info; -}; - napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, ParametersInfoCancel ¶s) { ANS_LOGI("enter"); diff --git a/frameworks/js/napi/src/common.cpp b/frameworks/js/napi/src/common.cpp index f8942f609..3b888761e 100644 --- a/frameworks/js/napi/src/common.cpp +++ b/frameworks/js/napi/src/common.cpp @@ -14,6 +14,7 @@ */ #include "common.h" +#include "ans_inner_errors.h" #include "napi_common.h" #include "pixel_map_napi.h" @@ -21,6 +22,23 @@ namespace OHOS { namespace NotificationNapi { std::set> Common::wantAgent_; +static const std::unordered_map ErrorCodeToMsg { + {ERROR_PERMISSION_DENIED, "Permission denied"}, + {ERROR_PARAM_INVALID, "Invalid parameter"}, + {ERROR_SYSTEM_CAP_ERROR, "SystemCapability not found"}, + {ERROR_INTERNAL_ERROR, "Internal error"}, + {ERROR_IPC_ERROR, "marshalling or unmarshalling error"}, + {ERROR_SERVICE_CONNECT_ERROR, "Failed to connect service"}, + {ERROR_NOTIFICATION_CLOSED, "Notification is not enabled"}, + {ERROR_SLOT_CLOSED, "Notification slot is not enabled"}, + {ERROR_NOTIFICATION_UNREMOVABLE, "Notification is not allowed to remove"}, + {ERROR_NOTIFICATION_NOT_EXIST, "The notification is not exist"}, + {ERROR_USER_NOT_EXIST, "The user is not exist"}, + {ERROR_OVER_MAX_NUM_PER_SECOND, "Over max number notifications per second"}, + {ERROR_DISTRIBUTED_OPERATION_FAILED, "Distributed operation failed"}, + {ERROR_READ_TEMPLATE_CONFIG_FAILED, "Read template config failed"}, + {ERROR_BUNDLE_NOT_FOUND, "The specified bundle name was not found"}, +}; Common::Common() {} @@ -48,6 +66,29 @@ napi_value Common::NapiGetUndefined(napi_env env) return result; } +napi_value Common::CreateErrorValue(napi_env env, int32_t errCode) +{ + napi_value code = nullptr; + napi_create_int32(env, errCode, &code); + + auto iter = ErrorCodeToMsg.find(errCode); + std::string errMsg = iter != ErrorCodeToMsg.end() ? iter->second : ""; + napi_value message = nullptr; + napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message); + + napi_value error = nullptr; + napi_create_error(env, nullptr, message, &error); + napi_set_named_property(env, error, "code", code); + return error; +} + +void Common::NapiThrow(napi_env env, int32_t errCode) +{ + ANS_LOGI("enter"); + + napi_throw(env, CreateErrorValue(env, errCode)); +} + napi_value Common::GetCallbackErrorValue(napi_env env, int32_t errCode) { napi_value result = nullptr; @@ -96,7 +137,7 @@ void Common::SetCallback( napi_value resultout = nullptr; napi_get_reference_value(env, callbackIn, &callback); napi_value results[ARGS_TWO] = {nullptr}; - results[PARAM0] = GetCallbackErrorValue(env, errorCode); + results[PARAM0] = CreateErrorValue(env, errorCode); results[PARAM1] = result; NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &results[PARAM0], &resultout)); ANS_LOGI("end"); @@ -123,7 +164,7 @@ void Common::SetPromise( if (errorCode == ERR_OK) { napi_resolve_deferred(env, deferred, result); } else { - napi_reject_deferred(env, deferred, GetCallbackErrorValue(env, errorCode)); + napi_reject_deferred(env, deferred, CreateErrorValue(env, errorCode)); } ANS_LOGI("end"); } @@ -4666,5 +4707,82 @@ napi_value Common::GetNotificationBadgeNumber( return NapiGetNull(env); } + +void Common::CreateReturnValue(const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result) +{ + ANS_LOGI("enter errorCode=%{public}d", info.errorCode); + int32_t errorCode = info.errorCode == ERR_OK ? ERR_OK : ErrorToExternal(info.errorCode); + if (info.isCallback) { + SetCallback(env, info.callback, errorCode, result); + } else { + SetPromise(env, info.deferred, errorCode, result); + } + ANS_LOGI("end"); +} + +int32_t Common::ErrorToExternal(uint32_t errCode) +{ + int32_t ExternalCode = ERROR_INTERNAL_ERROR; + switch (errCode) { + case ERR_ANS_PERMISSION_DENIED: + case ERR_ANS_NON_SYSTEM_APP: + ExternalCode = ERROR_PERMISSION_DENIED; + break; + case ERR_ANS_INVALID_PARAM: + case ERR_ANS_INVALID_UID: + case ERR_ANS_ICON_OVER_SIZE: + case ERR_ANS_PICTURE_OVER_SIZE: + ExternalCode = ERROR_PARAM_INVALID; + break; + case ERR_ANS_NO_MEMORY: + case ERR_ANS_TASK_ERR: + ExternalCode = ERROR_INTERNAL_ERROR; + break; + case ERR_ANS_PARCELABLE_FAILED: + case ERR_ANS_TRANSACT_FAILED: + case ERR_ANS_REMOTE_DEAD: + ExternalCode = ERROR_IPC_ERROR; + break; + case ERR_ANS_SERVICE_NOT_READY: + case ERR_ANS_SERVICE_NOT_CONNECTED: + ExternalCode = ERROR_SERVICE_CONNECT_ERROR; + break; + case ERR_ANS_NOT_ALLOWED: + ExternalCode = ERROR_NOTIFICATION_CLOSED; + break; + case ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED: + ExternalCode = ERROR_SLOT_CLOSED; + break; + case ERR_ANS_NOTIFICATION_IS_UNREMOVABLE: + ExternalCode = ERROR_NOTIFICATION_UNREMOVABLE; + break; + case ERR_ANS_NOTIFICATION_NOT_EXISTS: + ExternalCode = ERROR_NOTIFICATION_NOT_EXIST; + break; + case ERR_ANS_GET_ACTIVE_USER_FAILED: + ExternalCode = ERROR_USER_NOT_EXIST; + break; + case ERR_ANS_INVALID_PID: + case ERR_ANS_INVALID_BUNDLE: + ExternalCode = ERROR_BUNDLE_NOT_FOUND; + break; + case ERR_ANS_OVER_MAX_ACTIVE_PERSECOND: + ExternalCode = ERROR_OVER_MAX_NUM_PER_SECOND; + break; + case ERR_ANS_DISTRIBUTED_OPERATION_FAILED: + case ERR_ANS_DISTRIBUTED_GET_INFO_FAILED: + ExternalCode = ERROR_DISTRIBUTED_OPERATION_FAILED; + break; + case ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED: + ExternalCode = ERROR_READ_TEMPLATE_CONFIG_FAILED; + break; + default: + ExternalCode = ERROR_INTERNAL_ERROR; + break; + } + + ANS_LOGI("internal errorCode[%{public}u] to external errorCode[%{public}d]", errCode, ExternalCode); + return ExternalCode; +} } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/src/display_badge.cpp b/frameworks/js/napi/src/display_badge.cpp index b648e0bd7..f0680657e 100644 --- a/frameworks/js/napi/src/display_badge.cpp +++ b/frameworks/js/napi/src/display_badge.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "enable_notification.h" +#include "display_badge.h" namespace OHOS { namespace NotificationNapi { @@ -22,33 +22,6 @@ const int ENABLE_BADGE_DISPLAYED_MIN_PARA = 2; const int IS_DISPLAY_BADGE_MAX_PARA = 2; const int IS_DISPLAY_BADGE_MIN_PARA = 1; -struct EnableBadgeParams { - NotificationBundleOption option; - bool enable = false; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoEnableBadge { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - EnableBadgeParams params; - CallbackPromiseInfo info; -}; - -struct IsDisplayBadgeParams { - NotificationBundleOption option; - napi_ref callback = nullptr; - bool hasBundleOption = false; -}; - -struct AsyncCallbackInfoIsDisplayBadge { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - IsDisplayBadgeParams params; - CallbackPromiseInfo info; - bool enabled = false; -}; - napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnableBadgeParams ¶ms) { ANS_LOGI("enter"); diff --git a/frameworks/js/napi/src/distributed.cpp b/frameworks/js/napi/src/distributed.cpp index 3684872ca..6f1a492f6 100644 --- a/frameworks/js/napi/src/distributed.cpp +++ b/frameworks/js/napi/src/distributed.cpp @@ -26,84 +26,6 @@ const int IS_ENABLED_BUNDLE_MIN_PARA = 1; const int ENABLED_SYNC_MAX_PARA = 3; const int ENABLED_SYNC_MIN_PARA = 2; -struct AsyncCallbackInfoIsEnabled { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - CallbackPromiseInfo info; - bool enable = false; -}; - -struct EnabledParams { - napi_ref callback = nullptr; - bool enable = false; -}; - -struct AsyncCallbackInfoEnabled { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - CallbackPromiseInfo info; - EnabledParams params; -}; - -struct EnabledByBundleParams { - NotificationBundleOption option; - napi_ref callback = nullptr; - bool enable = false; -}; - -struct AsyncCallbackInfoEnabledByBundle { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - CallbackPromiseInfo info; - EnabledByBundleParams params; -}; - -struct IsEnabledByBundleParams { - NotificationBundleOption option; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoIsEnabledByBundle { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - CallbackPromiseInfo info; - IsEnabledByBundleParams params; - bool enable = false; -}; - -struct AsyncCallbackInfoGetRemindType { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - CallbackPromiseInfo info; - NotificationConstant::RemindType remindType = NotificationConstant::RemindType::NONE; -}; - -struct EnabledWithoutAppParams { - int32_t userId = SUBSCRIBE_USER_INIT; - bool enable = false; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoEnabledWithoutApp { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - CallbackPromiseInfo info; - EnabledWithoutAppParams params; -}; - -struct GetEnabledWithoutAppParams { - int32_t userId = SUBSCRIBE_USER_INIT; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoGetEnabledWithoutApp { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - CallbackPromiseInfo info; - GetEnabledWithoutAppParams params; - bool enable = false; -}; - napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnabledParams ¶ms) { ANS_LOGI("enter"); diff --git a/frameworks/js/napi/src/disturb_mode.cpp b/frameworks/js/napi/src/disturb_mode.cpp index e901f0285..3ba80b6e7 100644 --- a/frameworks/js/napi/src/disturb_mode.cpp +++ b/frameworks/js/napi/src/disturb_mode.cpp @@ -21,42 +21,6 @@ const int SET_DISTURB_MAX_PARA = 3; const int SET_DISTURB_MIN_PARA = 1; const int GET_DISTURB_MAX_PARA = 2; -struct SetDoNotDisturbDateParams { - NotificationDoNotDisturbDate date; - bool hasUserId = false; - int32_t userId = SUBSCRIBE_USER_INIT; - napi_ref callback = nullptr; -}; - -struct GetDoNotDisturbDateParams { - bool hasUserId = false; - int32_t userId = SUBSCRIBE_USER_INIT; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoSetDoNotDisturb { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - SetDoNotDisturbDateParams params; - CallbackPromiseInfo info; -}; - -struct AsyncCallbackInfoGetDoNotDisturb { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - GetDoNotDisturbDateParams params; - NotificationDoNotDisturbDate date; - CallbackPromiseInfo info; -}; - -struct AsyncCallbackInfoSupportDoNotDisturb { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - napi_ref callback = nullptr; - bool isSupported = false; - CallbackPromiseInfo info; -}; - napi_value GetDoNotDisturbDate(const napi_env &env, const napi_value &argv, SetDoNotDisturbDateParams ¶ms) { ANS_LOGI("enter"); diff --git a/frameworks/js/napi/src/enable_notification.cpp b/frameworks/js/napi/src/enable_notification.cpp index de7e99a4e..9da7ec28f 100644 --- a/frameworks/js/napi/src/enable_notification.cpp +++ b/frameworks/js/napi/src/enable_notification.cpp @@ -21,35 +21,6 @@ const int ENABLE_NOTIFICATION_MAX_PARA = 3; const int ENABLE_NOTIFICATION_MIN_PARA = 2; const int IS_NOTIFICATION_ENABLE_MAX_PARA = 2; -struct EnableParams { - NotificationBundleOption option; - bool enable = false; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoEnable { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - EnableParams params; - CallbackPromiseInfo info; -}; - -struct IsEnableParams { - NotificationBundleOption option; - napi_ref callback = nullptr; - bool hasBundleOption = false; - int32_t userId = SUBSCRIBE_USER_INIT; - bool hasUserId = false; -}; - -struct AsyncCallbackInfoIsEnable { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - IsEnableParams params; - CallbackPromiseInfo info; - bool allowed = false; -}; - napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnableParams ¶ms) { ANS_LOGI("enter"); diff --git a/frameworks/js/napi/src/get_active.cpp b/frameworks/js/napi/src/get_active.cpp index 1904caf65..243f9869a 100644 --- a/frameworks/js/napi/src/get_active.cpp +++ b/frameworks/js/napi/src/get_active.cpp @@ -17,15 +17,6 @@ namespace OHOS { namespace NotificationNapi { -struct AsyncCallbackInfoActive { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - CallbackPromiseInfo info; - std::vector> notifications; - std::vector> requests; - uint64_t num = 0; -}; - void AsyncCompleteCallbackGetAllActiveNotifications(napi_env env, napi_status status, void *data) { ANS_LOGI("GetAllActiveNotifications napi_create_async_work end"); diff --git a/frameworks/js/napi/src/init.cpp b/frameworks/js/napi/src/init.cpp index 346eea41d..888b10577 100644 --- a/frameworks/js/napi/src/init.cpp +++ b/frameworks/js/napi/src/init.cpp @@ -98,6 +98,7 @@ static napi_value Init(napi_env env, napi_value exports) /* * Propertise define */ + ANS_LOGI("libnotification Init start"); NotificationInit(env, exports); ConstantInit(env, exports); OHOS::Media::PixelMapNapi::Init(env, exports); @@ -110,6 +111,7 @@ static napi_value Init(napi_env env, napi_value exports) */ __attribute__((constructor)) void RegisterModule(void) { + ANS_LOGI("libnotification register start"); napi_module_register(&_module); } EXTERN_C_END diff --git a/frameworks/js/napi/src/manager/BUILD.gn b/frameworks/js/napi/src/manager/BUILD.gn new file mode 100644 index 000000000..be9ad9625 --- /dev/null +++ b/frameworks/js/napi/src/manager/BUILD.gn @@ -0,0 +1,86 @@ +# Copyright (c) 2022 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. + +import("//base/notification/distributed_notification_service/notification.gni") +import("//build/ohos.gni") + +cflags = [] + +config("native_module_config") { + visibility = [ ":*" ] + + include_dirs = [] + + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } +} + +ohos_shared_library("notificationmanager") { + include_dirs = [ + "${ability_runtime_path}/interfaces/kits/native/ability/native", + "${ability_runtime_napi_path}/inner/napi_common", + "//foundation/multimedia/image_framework/interfaces/kits/js/common/include/", + "../../include", + "../../include/manager", + "//third_party/node/src", + ] + + configs = [ ":native_module_config" ] + + sources = [ + "../ans_template.cpp", + "../cancel.cpp", + "../common.cpp", + "../constant.cpp", + "../display_badge.cpp", + "../distributed.cpp", + "../disturb_mode.cpp", + "../enable_notification.cpp", + "../get_active.cpp", + "../publish.cpp", + "../slot.cpp", + "init_module.cpp", + "napi_cancel.cpp", + "napi_display_badge.cpp", + "napi_distributed.cpp", + "napi_disturb_mode.cpp", + "napi_enable_notification.cpp", + "napi_get_active.cpp", + "napi_publish.cpp", + "napi_slot.cpp", + "napi_template.cpp", + ] + + deps = [ + "${ability_runtime_napi_path}/inner/napi_common:napi_common", + "${ability_runtime_path}/frameworks/native/appkit:app_context", + "${core_path}:ans_core", + "//foundation/multimedia/image_framework/interfaces/innerkits:image", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:abilitykit_native", + "ability_runtime:wantagent_innerkits", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "napi:ace_napi", + "relational_store:native_rdb", + ] + + relative_install_dir = "module" + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} diff --git a/frameworks/js/napi/src/manager/init_module.cpp b/frameworks/js/napi/src/manager/init_module.cpp new file mode 100644 index 000000000..020a2bbf9 --- /dev/null +++ b/frameworks/js/napi/src/manager/init_module.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2022 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 "init_module.h" +#include "constant.h" +#include "napi_cancel.h" +#include "napi_display_badge.h" +#include "napi_distributed.h" +#include "napi_disturb_mode.h" +#include "napi_enable_notification.h" +#include "napi_get_active.h" +#include "napi_publish.h" +#include "napi_slot.h" +#include "napi_template.h" +#include "pixel_map_napi.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +EXTERN_C_START + +napi_value NotificationManagerInit(napi_env env, napi_value exports) +{ + ANS_LOGI("NotificationManagerInit start"); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("publish", Napi_Publish), + DECLARE_NAPI_FUNCTION("publishAsBundle", Napi_PublishAsBundle), + DECLARE_NAPI_FUNCTION("show", Napi_ShowNotification), + DECLARE_NAPI_FUNCTION("cancel", Napi_Cancel), + DECLARE_NAPI_FUNCTION("cancelAll", Napi_CancelAll), + DECLARE_NAPI_FUNCTION("cancelGroup", Napi_CancelGroup), + DECLARE_NAPI_FUNCTION("cancelAsBundle", Napi_CancelAsBundle), + DECLARE_NAPI_FUNCTION("addSlot", Napi_AddSlot), + DECLARE_NAPI_FUNCTION("addSlots", Napi_AddSlots), + DECLARE_NAPI_FUNCTION("setSlotByBundle", Napi_SetSlotByBundle), + DECLARE_NAPI_FUNCTION("getSlot", Napi_GetSlot), + DECLARE_NAPI_FUNCTION("getSlotNumByBundle", Napi_GetSlotNumByBundle), + DECLARE_NAPI_FUNCTION("getSlots", Napi_GetSlots), + DECLARE_NAPI_FUNCTION("getSlotsByBundle", Napi_GetSlotsByBundle), + DECLARE_NAPI_FUNCTION("removeSlot", Napi_RemoveSlot), + DECLARE_NAPI_FUNCTION("removeAllSlots", Napi_RemoveAllSlots), + DECLARE_NAPI_FUNCTION("setNotificationEnableSlot", Napi_EnableNotificationSlot), + DECLARE_NAPI_FUNCTION("isNotificationSlotEnabled", Napi_IsEnableNotificationSlot), + DECLARE_NAPI_FUNCTION("setNotificationEnable", Napi_EnableNotification), + DECLARE_NAPI_FUNCTION("isNotificationEnabled", Napi_IsNotificationEnabled), + DECLARE_NAPI_FUNCTION("requestEnableNotification", Napi_RequestEnableNotification), + DECLARE_NAPI_FUNCTION("getAllActiveNotifications", Napi_GetAllActiveNotifications), + DECLARE_NAPI_FUNCTION("getActiveNotifications", Napi_GetActiveNotifications), + DECLARE_NAPI_FUNCTION("getActiveNotificationCount", Napi_GetActiveNotificationCount), + DECLARE_NAPI_FUNCTION("displayBadge", Napi_DisplayBadge), + DECLARE_NAPI_FUNCTION("isBadgeDisplayed", Napi_IsBadgeDisplayed), + DECLARE_NAPI_FUNCTION("setDoNotDisturbDate", Napi_SetDoNotDisturbDate), + DECLARE_NAPI_FUNCTION("getDoNotDisturbDate", Napi_GetDoNotDisturbDate), + DECLARE_NAPI_FUNCTION("supportDoNotDisturbMode", Napi_SupportDoNotDisturbMode), + DECLARE_NAPI_FUNCTION("isSupportTemplate", Napi_IsSupportTemplate), + DECLARE_NAPI_FUNCTION("isDistributedEnabled", Napi_IsDistributedEnabled), + DECLARE_NAPI_FUNCTION("setDistributedEnable", Napi_EnableDistributed), + DECLARE_NAPI_FUNCTION("setDistributedEnableByBundle", Napi_EnableDistributedByBundle), + DECLARE_NAPI_FUNCTION("enableDistributedSelf", Napi_EnableDistributedSelf), + DECLARE_NAPI_FUNCTION("isDistributedEnableByBundle", Napi_IsDistributedEnableByBundle), + DECLARE_NAPI_FUNCTION("getDeviceRemindType", Napi_GetDeviceRemindType), + DECLARE_NAPI_FUNCTION("setSyncNotificationEnabledWithoutApp", Napi_SetSyncNotificationEnabledWithoutApp), + DECLARE_NAPI_FUNCTION("getSyncNotificationEnabledWithoutApp", Napi_GetSyncNotificationEnabledWithoutApp), + }; + + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + + return exports; +} + +/* + * Module export function + */ +static napi_value Init(napi_env env, napi_value exports) +{ + /* + * Propertise define + */ + NotificationManagerInit(env, exports); + ConstantInit(env, exports); + OHOS::Media::PixelMapNapi::Init(env, exports); + + return exports; +} + +/* + * Module register function + */ +__attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&_module_manager); +} +EXTERN_C_END +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/manager/napi_cancel.cpp b/frameworks/js/napi/src/manager/napi_cancel.cpp new file mode 100644 index 000000000..f05062f87 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_cancel.cpp @@ -0,0 +1,257 @@ +/* + * Copyright (c) 2022 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 "napi_cancel.h" + +#include "ans_inner_errors.h" +#include "cancel.h" + +namespace OHOS { +namespace NotificationNapi { +napi_value Napi_Cancel(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoCancel paras; + if (ParseParameters(env, info, paras) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoCancel *asynccallbackinfo = new (std::nothrow) + AsyncCallbackInfoCancel {.env = env, .asyncWork = nullptr, .id = paras.id, .label = paras.label}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, paras.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "cancel", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("Cancel napi_create_async_work start"); + AsyncCallbackInfoCancel *asynccallbackinfo = static_cast(data); + + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::CancelNotification(asynccallbackinfo->label, asynccallbackinfo->id); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("Cancel napi_create_async_work end"); + AsyncCallbackInfoCancel *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_CancelAll(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoCancel {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "cancelAll", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("CancelAll napi_create_async_work start"); + AsyncCallbackInfoCancel *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::CancelAllNotifications(); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("CancelAll napi_create_async_work end"); + AsyncCallbackInfoCancel *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_CancelGroup(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoCancelGroup params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoCancelGroup *asynccallbackinfo = new (std::nothrow) + AsyncCallbackInfoCancelGroup {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "cancelGroup", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("CancelGroup napi_create_async_work start"); + AsyncCallbackInfoCancelGroup *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + ANS_LOGI("asynccallbackinfo->params.groupName = %{public}s", + asynccallbackinfo->params.groupName.c_str()); + asynccallbackinfo->info.errorCode = + NotificationHelper::CancelGroup(asynccallbackinfo->params.groupName); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("CancelGroup napi_create_async_work end"); + AsyncCallbackInfoCancelGroup *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_CancelAsBundle(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoCancelAsBundle paras; + if (ParseParameters(env, info, paras) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoCancelAsBundle *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoCancelAsBundle { + .env = env, .asyncWork = nullptr, + .id = paras.id, + .representativeBundle = paras.representativeBundle, + .userId = paras.userId + }; + if (!asynccallbackinfo) { + return Common::JSParaError(env, paras.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "cancelasbundle", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("Cancel napi_create_async_work start"); + AsyncCallbackInfoCancelAsBundle *asynccallbackinfo = static_cast(data); + + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::CancelAsBundle( + asynccallbackinfo->id, asynccallbackinfo->representativeBundle, asynccallbackinfo->userId); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("Cancel napi_create_async_work end"); + AsyncCallbackInfoCancelAsBundle *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/manager/napi_display_badge.cpp b/frameworks/js/napi/src/manager/napi_display_badge.cpp new file mode 100644 index 000000000..d1ded7b95 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_display_badge.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2021-2022 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 "napi_display_badge.h" + +#include "ans_inner_errors.h" +#include "display_badge.h" + +namespace OHOS { +namespace NotificationNapi { +napi_value Napi_DisplayBadge(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + EnableBadgeParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoEnableBadge *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnableBadge {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "DisplayBadge", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("DisplayBadge napi_create_async_work start"); + AsyncCallbackInfoEnableBadge *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + ANS_LOGI("option.bundle = %{public}s option.uid = %{public}d enable = %{public}d", + asynccallbackinfo->params.option.GetBundleName().c_str(), + asynccallbackinfo->params.option.GetUid(), + asynccallbackinfo->params.enable); + asynccallbackinfo->info.errorCode = NotificationHelper::SetShowBadgeEnabledForBundle( + asynccallbackinfo->params.option, asynccallbackinfo->params.enable); + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("DisplayBadge napi_create_async_work end"); + AsyncCallbackInfoEnableBadge *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +void AsyncCompleteCallbackNapiIsBadgeDisplayed(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoIsDisplayBadge *asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + napi_get_boolean(env, asynccallbackinfo->enabled, &result); + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_IsBadgeDisplayed(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + IsDisplayBadgeParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + ANS_LOGE("Failed to parse params!"); + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoIsDisplayBadge *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoIsDisplayBadge {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "IsBadgeDisplayed", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsBadgeDisplayed napi_create_async_work start"); + AsyncCallbackInfoIsDisplayBadge *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + if (asynccallbackinfo->params.hasBundleOption) { + ANS_LOGI("option.bundle = %{public}s option.uid = %{public}d", + asynccallbackinfo->params.option.GetBundleName().c_str(), + asynccallbackinfo->params.option.GetUid()); + asynccallbackinfo->info.errorCode = NotificationHelper::GetShowBadgeEnabledForBundle( + asynccallbackinfo->params.option, asynccallbackinfo->enabled); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::GetShowBadgeEnabled( + asynccallbackinfo->enabled); + } + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d, enabled = %{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->enabled); + } + }, + AsyncCompleteCallbackNapiIsBadgeDisplayed, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/manager/napi_distributed.cpp b/frameworks/js/napi/src/manager/napi_distributed.cpp new file mode 100644 index 000000000..6281fe0aa --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_distributed.cpp @@ -0,0 +1,545 @@ +/* + * Copyright (c) 2022 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 "napi_distributed.h" + +#include "ans_inner_errors.h" +#include "distributed.h" + +namespace OHOS { +namespace NotificationNapi { +void AsyncCompleteCallbackNapiIsDistributedEnabled(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + ANS_LOGI("IsDistributedEnabled napi_create_async_work end"); + AsyncCallbackInfoIsEnabled *asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_get_boolean(env, asynccallbackinfo->enable, &result); + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_IsDistributedEnabled(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoIsEnabled {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "isDistributedEnabled", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsDistributedEnabled napi_create_async_work start"); + AsyncCallbackInfoIsEnabled *asynccallbackinfo = static_cast(data); + + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::IsDistributedEnabled(asynccallbackinfo->enable); + ANS_LOGI("IsDistributedEnabled enable = %{public}d", asynccallbackinfo->enable); + } + }, + AsyncCompleteCallbackNapiIsDistributedEnabled, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_EnableDistributed(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + EnabledParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoEnabled *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnabled {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "enableDistributed", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("EnableDistributed napi_create_async_work start"); + AsyncCallbackInfoEnabled *asynccallbackinfo = static_cast(data); + + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::EnableDistributed(asynccallbackinfo->params.enable); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("EnableDistributed napi_create_async_work end"); + AsyncCallbackInfoEnabled *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_EnableDistributedByBundle(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + EnabledByBundleParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoEnabledByBundle *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnabledByBundle {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "enableDistributedByBundle", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("EnableDistributedByBundle napi_create_async_work start"); + AsyncCallbackInfoEnabledByBundle *asynccallbackinfo = static_cast(data); + + asynccallbackinfo->info.errorCode = NotificationHelper::EnableDistributedByBundle( + asynccallbackinfo->params.option, asynccallbackinfo->params.enable); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("EnableDistributedByBundle napi_create_async_work end"); + AsyncCallbackInfoEnabledByBundle *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_EnableDistributedSelf(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + EnabledParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoEnabled *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnabled {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "enableDistributedSelf", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("EnableDistributedSelf napi_create_async_work start"); + auto asynccallbackinfo = reinterpret_cast(data); + asynccallbackinfo->info.errorCode = + NotificationHelper::EnableDistributedSelf(asynccallbackinfo->params.enable); + ANS_LOGI("EnableDistributedSelf enable = %{public}d", asynccallbackinfo->params.enable); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("EnableDistributedSelf napi_create_async_work end"); + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +void AsyncCompleteCallbackNapiIsDistributedEnableByBundle(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + ANS_LOGI("IsDistributedEnableByBundle napi_create_async_work end"); + AsyncCallbackInfoIsEnabledByBundle *asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_get_boolean(env, asynccallbackinfo->enable, &result); + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_IsDistributedEnableByBundle(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + IsEnabledByBundleParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoIsEnabledByBundle *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoIsEnabledByBundle {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "isDistributedEnableByBundle", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsDistributedEnableByBundle napi_create_async_work start"); + auto asynccallbackinfo = reinterpret_cast(data); + + asynccallbackinfo->info.errorCode = NotificationHelper::IsDistributedEnableByBundle( + asynccallbackinfo->params.option, asynccallbackinfo->enable); + }, + AsyncCompleteCallbackNapiIsDistributedEnableByBundle, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +void AsyncCompleteCallbackNapiGetDeviceRemindType(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + ANS_LOGI("GetDeviceRemindType napi_create_async_work end"); + AsyncCallbackInfoGetRemindType *asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + DeviceRemindType outType = DeviceRemindType::IDLE_DONOT_REMIND; + if (!Common::DeviceRemindTypeCToJS(asynccallbackinfo->remindType, outType)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + napi_create_int32(env, (int32_t)outType, &result); + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_GetDeviceRemindType(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoGetRemindType {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getDeviceRemindType", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetDeviceRemindType napi_create_async_work start"); + auto asynccallbackinfo = reinterpret_cast(data); + asynccallbackinfo->info.errorCode = + NotificationHelper::GetDeviceRemindType(asynccallbackinfo->remindType); + }, + AsyncCompleteCallbackNapiGetDeviceRemindType, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_SetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + EnabledWithoutAppParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::JSParaError(env, params.callback); + } + + AsyncCallbackInfoEnabledWithoutApp *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnabledWithoutApp {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "SetSyncNotificationEnabledWithoutApp", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("SetSyncNotificationEnabledWithoutApp napi_create_async_work start"); + AsyncCallbackInfoEnabledWithoutApp *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::SetSyncNotificationEnabledWithoutApp( + asynccallbackinfo->params.userId, asynccallbackinfo->params.enable); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("SetSyncNotificationEnabledWithoutApp napi_create_async_work end"); + AsyncCallbackInfoEnabledWithoutApp *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + if (napi_queue_async_work(env, asynccallbackinfo->asyncWork) != napi_ok) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + return Common::JSParaError(env, params.callback); + } + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } + return promise; +} + +napi_value Napi_GetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + GetEnabledWithoutAppParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::JSParaError(env, params.callback); + } + + AsyncCallbackInfoGetEnabledWithoutApp *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoGetEnabledWithoutApp { + .env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "GetSyncNotificationEnabledWithoutApp", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetSyncNotificationEnabledWithoutApp napi_create_async_work start"); + AsyncCallbackInfoGetEnabledWithoutApp *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::GetSyncNotificationEnabledWithoutApp( + asynccallbackinfo->params.userId, asynccallbackinfo->enable); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("GetSyncNotificationEnabledWithoutApp napi_create_async_work end"); + AsyncCallbackInfoGetEnabledWithoutApp *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_get_boolean(env, asynccallbackinfo->enable, &result); + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + if (napi_queue_async_work(env, asynccallbackinfo->asyncWork) != napi_ok) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + return Common::JSParaError(env, params.callback); + } + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } + return promise; +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/manager/napi_disturb_mode.cpp b/frameworks/js/napi/src/manager/napi_disturb_mode.cpp new file mode 100644 index 000000000..bccd1256c --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_disturb_mode.cpp @@ -0,0 +1,221 @@ +/* + * Copyright (c) 2022 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 "napi_disturb_mode.h" + +#include "ans_inner_errors.h" +#include "disturb_mode.h" + +namespace OHOS { +namespace NotificationNapi { +napi_value Napi_SetDoNotDisturbDate(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + SetDoNotDisturbDateParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoSetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "setDoNotDisturbDate", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, resourceName, [](napi_env env, void *data) { + ANS_LOGI("SetDoNotDisturbDate napi_create_async_work start"); + AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo->params.hasUserId) { + asynccallbackinfo->info.errorCode = NotificationHelper::SetDoNotDisturbDate( + asynccallbackinfo->params.userId, asynccallbackinfo->params.date); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::SetDoNotDisturbDate( + asynccallbackinfo->params.date); + } + + ANS_LOGI("SetDoNotDisturbDate date=%{public}s errorCode=%{public}d, hasUserId=%{public}d", + asynccallbackinfo->params.date.Dump().c_str(), asynccallbackinfo->info.errorCode, + asynccallbackinfo->params.hasUserId); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("SetDoNotDisturbDate napi_create_async_work end"); + AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +void AsyncCompleteCallbackNapiGetDoNotDisturbDate(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo = static_cast(data); + napi_value result = Common::NapiGetNull(env); + if (asynccallbackinfo->info.errorCode == ERR_OK) { + napi_create_object(env, &result); + if (!Common::SetDoNotDisturbDate(env, asynccallbackinfo->date, result)) { + asynccallbackinfo->info.errorCode = ERROR; + } + } + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_GetDoNotDisturbDate(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + GetDoNotDisturbDateParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoGetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getDoNotDisturbDate", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetDoNotDisturbDate napi_create_async_work start"); + AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo->params.hasUserId) { + asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate( + asynccallbackinfo->params.userId, asynccallbackinfo->date); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate(asynccallbackinfo->date); + } + + ANS_LOGI("GetDoNotDisturbDate errorCode=%{public}d date=%{public}s, hasUserId=%{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->date.Dump().c_str(), + asynccallbackinfo->params.hasUserId); + }, + AsyncCompleteCallbackNapiGetDoNotDisturbDate, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_SupportDoNotDisturbMode(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoSupportDoNotDisturb { + .env = env, .asyncWork = nullptr, .callback = callback}; + + if (!asynccallbackinfo) { + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "supportDoNotDisturbMode", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("SupportDoNotDisturbMode napi_create_async_work start"); + AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo = + static_cast(data); + asynccallbackinfo->info.errorCode = + NotificationHelper::DoesSupportDoNotDisturbMode(asynccallbackinfo->isSupported); + ANS_LOGI("SupportDoNotDisturbMode errorCode=%{public}d isSupported=%{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->isSupported); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("SupportDoNotDisturbMode napi_create_async_work end"); + AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + napi_value result = nullptr; + napi_get_boolean(env, asynccallbackinfo->isSupported, &result); + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/manager/napi_enable_notification.cpp b/frameworks/js/napi/src/manager/napi_enable_notification.cpp new file mode 100644 index 000000000..a71122281 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_enable_notification.cpp @@ -0,0 +1,256 @@ +/* + * Copyright (c) 2022 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 "napi_enable_notification.h" + +#include "ans_inner_errors.h" +#include "enable_notification.h" + +namespace OHOS { +namespace NotificationNapi { +void AsyncCompleteCallbackNapiEnableNotification(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoEnable *asynccallbackinfo = static_cast(data); + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_EnableNotification(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + EnableParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoEnable *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnable {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "enableNotification", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("EnableNotification napi_create_async_work start"); + AsyncCallbackInfoEnable *asynccallbackinfo = static_cast(data); + std::string deviceId {""}; + asynccallbackinfo->info.errorCode = NotificationHelper::SetNotificationsEnabledForSpecifiedBundle( + asynccallbackinfo->params.option, deviceId, asynccallbackinfo->params.enable); + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode); + }, + AsyncCompleteCallbackNapiEnableNotification, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +void AsyncCompleteCallbackNapiIsNotificationEnabled(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + napi_get_boolean(env, asynccallbackinfo->allowed, &result); + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_IsNotificationEnabled(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + IsEnableParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoIsEnable *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoIsEnable {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "isNotificationEnabled", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsNotificationEnabled napi_create_async_work start"); + AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); + + if (asynccallbackinfo->params.hasBundleOption) { + ANS_LOGI("option.bundle = %{public}s option.uid = %{public}d", + asynccallbackinfo->params.option.GetBundleName().c_str(), + asynccallbackinfo->params.option.GetUid()); + asynccallbackinfo->info.errorCode = + NotificationHelper::IsAllowedNotify(asynccallbackinfo->params.option, asynccallbackinfo->allowed); + } else if (asynccallbackinfo->params.hasUserId) { + ANS_LOGI("userId = %{public}d", asynccallbackinfo->params.userId); + asynccallbackinfo->info.errorCode = + NotificationHelper::IsAllowedNotify(asynccallbackinfo->params.userId, asynccallbackinfo->allowed); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::IsAllowedNotify(asynccallbackinfo->allowed); + } + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d, allowed = %{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->allowed); + }, + AsyncCompleteCallbackNapiIsNotificationEnabled, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_IsNotificationEnabledSelf(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + IsEnableParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoIsEnable *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoIsEnable {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "IsNotificationEnabledSelf", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsNotificationEnabledSelf napi_create_async_work start"); + AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); + + if (asynccallbackinfo->params.hasBundleOption) { + ANS_LOGE("Not allowed to query another application"); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::IsAllowedNotifySelf(asynccallbackinfo->allowed); + } + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d, allowed = %{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->allowed); + }, + AsyncCompleteCallbackNapiIsNotificationEnabled, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_RequestEnableNotification(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + IsEnableParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoIsEnable *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoIsEnable {.env = env, .asyncWork = nullptr, .params = params}; + + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "RequestEnableNotification", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("RequestEnableNotification napi_create_async_work start"); + AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); + + std::string deviceId {""}; + asynccallbackinfo->info.errorCode = NotificationHelper::RequestEnableNotification(deviceId); + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode); + }, + AsyncCompleteCallbackNapiIsNotificationEnabled, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/manager/napi_get_active.cpp b/frameworks/js/napi/src/manager/napi_get_active.cpp new file mode 100644 index 000000000..6d281aec0 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_get_active.cpp @@ -0,0 +1,274 @@ +/* + * Copyright (c) 2022 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 "napi_get_active.h" + +#include "ans_inner_errors.h" +#include "get_active.h" + +namespace OHOS { +namespace NotificationNapi { +void AsyncCompleteCallbackNapiGetAllActiveNotifications(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("GetAllActiveNotifications napi_create_async_work end"); + + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + + auto asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_value arr = nullptr; + int32_t count = 0; + napi_create_array(env, &arr); + for (auto vec : asynccallbackinfo->notifications) { + if (!vec) { + ANS_LOGW("Invalid Notification object ptr"); + continue; + } + napi_value notificationResult = nullptr; + napi_create_object(env, ¬ificationResult); + if (!Common::SetNotification(env, vec.GetRefPtr(), notificationResult)) { + ANS_LOGW("Set Notification object failed"); + continue; + } + napi_set_element(env, arr, count, notificationResult); + count++; + } + ANS_LOGI("GetAllActiveNotifications count = %{public}d", count); + result = arr; + if ((count == 0) && (asynccallbackinfo->notifications.size() > 0)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_GetAllActiveNotifications(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoActive {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getAllActiveNotifications", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetAllActiveNotifications napi_create_async_work start"); + auto asynccallbackinfo = static_cast(data); + + asynccallbackinfo->info.errorCode = + NotificationHelper::GetAllActiveNotifications(asynccallbackinfo->notifications); + }, + AsyncCompleteCallbackNapiGetAllActiveNotifications, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +void AsyncCompleteCallbackNapiGetActiveNotifications(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("GetActiveNotifications napi_create_async_work end"); + + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + + auto asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_value arr = nullptr; + int32_t count = 0; + napi_create_array(env, &arr); + for (auto vec : asynccallbackinfo->requests) { + if (!vec) { + ANS_LOGW("Invalid NotificationRequest object ptr"); + continue; + } + napi_value requestResult = nullptr; + napi_create_object(env, &requestResult); + if (!Common::SetNotificationRequest(env, vec.GetRefPtr(), requestResult)) { + ANS_LOGW("Set NotificationRequest object failed"); + continue; + } + napi_set_element(env, arr, count, requestResult); + count++; + } + ANS_LOGI("GetActiveNotifications count = %{public}d", count); + result = arr; + if ((count == 0) && (asynccallbackinfo->requests.size() > 0)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_GetActiveNotifications(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoActive {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getActiveNotifications", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetActiveNotifications napi_create_async_work start"); + auto asynccallbackinfo = static_cast(data); + + asynccallbackinfo->info.errorCode = + NotificationHelper::GetActiveNotifications(asynccallbackinfo->requests); + }, + AsyncCompleteCallbackNapiGetActiveNotifications, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +void AsyncCompleteCallbackNapiGetActiveNotificationCount(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("GetActiveNotificationCount napi_create_async_work end"); + + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + + auto asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_create_uint32(env, asynccallbackinfo->num, &result); + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_GetActiveNotificationCount(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoActive {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getActiveNotificationCount", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetActiveNotificationCount napi_create_async_work start"); + auto asynccallbackinfo = static_cast(data); + + asynccallbackinfo->info.errorCode = NotificationHelper::GetActiveNotificationNums(asynccallbackinfo->num); + ANS_LOGI("GetActiveNotificationCount count = %{public}" PRIu64 "", asynccallbackinfo->num); + }, + AsyncCompleteCallbackNapiGetActiveNotificationCount, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/manager/napi_publish.cpp b/frameworks/js/napi/src/manager/napi_publish.cpp new file mode 100644 index 000000000..3e751036f --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_publish.cpp @@ -0,0 +1,205 @@ +/* + * Copyright (c) 2022 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 "napi_publish.h" + +#include "ans_inner_errors.h" +#include "publish.h" + +namespace OHOS { +namespace NotificationNapi { + +napi_value Napi_Publish(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoPublish params; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + napi_value promise = nullptr; + auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoPublish {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + + asynccallbackinfo->request = params.request; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "publish", NAPI_AUTO_LENGTH, &resourceName); + + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("Publish napi_create_async_work start"); + AsyncCallbackInfoPublish *asynccallbackinfo = static_cast(data); + ANS_LOGI("Publish napi_create_async_work start notificationId = %{public}d, contentType = " + "%{public}d", + asynccallbackinfo->request.GetNotificationId(), + asynccallbackinfo->request.GetContent()->GetContentType()); + + asynccallbackinfo->info.errorCode = + NotificationHelper::PublishNotification(asynccallbackinfo->request); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("Publish napi_create_async_work complete start"); + AsyncCallbackInfoPublish *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + ANS_LOGI("Publish napi_create_async_work complete end"); + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_ShowNotification(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoPublish params; + if (ParseShowOptions(env, info, params) == nullptr) { + ANS_LOGW("parse showOptions failed"); + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoPublish {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + ANS_LOGW("failed to create asynccallbackinfo"); + return Common::JSParaError(env, params.callback); + } + + asynccallbackinfo->request = params.request; + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "show", NAPI_AUTO_LENGTH, &resourceName); + + ANS_LOGI("before napi_create_async_work"); + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("Show napi_create_async_work start"); + AsyncCallbackInfoPublish *asynccallbackinfo = static_cast(data); + ANS_LOGI("Show napi_create_async_work start notificationId = %{public}d, contentType = " + "%{public}d", + asynccallbackinfo->request.GetNotificationId(), + asynccallbackinfo->request.GetContent()->GetContentType()); + + asynccallbackinfo->info.errorCode = + NotificationHelper::PublishNotification(asynccallbackinfo->request); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("Show napi_create_async_work complete start"); + AsyncCallbackInfoPublish *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + ANS_LOGI("Show napi_create_async_work complete end"); + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + return nullptr; +} + +napi_value Napi_PublishAsBundle(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoPublish params; + if (ParsePublishAsBundleParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + napi_value promise = nullptr; + auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoPublish {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + + asynccallbackinfo->request = params.request; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "publishasbundle", NAPI_AUTO_LENGTH, &resourceName); + + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("PublishAsBundle napi_create_async_work start"); + AsyncCallbackInfoPublish *asynccallbackinfo = static_cast(data); + ANS_LOGI("PublishAsBundle napi_create_async_work start notificationId = %{public}d, contentType = " + "%{public}d", + asynccallbackinfo->request.GetNotificationId(), + asynccallbackinfo->request.GetContent()->GetContentType()); + + asynccallbackinfo->info.errorCode = + NotificationHelper::PublishNotification(asynccallbackinfo->request); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("PublishAsBundle napi_create_async_work complete start"); + AsyncCallbackInfoPublish *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + ANS_LOGI("PublishAsBundle napi_create_async_work complete end"); + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/manager/napi_slot.cpp b/frameworks/js/napi/src/manager/napi_slot.cpp new file mode 100644 index 000000000..794d26589 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_slot.cpp @@ -0,0 +1,723 @@ +/* + * Copyright (c) 2022 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 "napi_slot.h" + +#include "ans_inner_errors.h" +#include "slot.h" + +namespace OHOS { +namespace NotificationNapi { +napi_value Napi_AddSlot(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoAddSlot paras; + if (ParseParametersByAddSlot(env, info, paras) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoAddSlot *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoAddSlot { + .env = env, + .asyncWork = nullptr, + .slot = paras.slot, + .inType = paras.inType, + .isAddSlotByType = paras.isAddSlotByType + }; + if (!asynccallbackinfo) { + return Common::JSParaError(env, paras.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "addSlot", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("AddSlot napi_create_async_work start"); + auto asynccallbackinfo = static_cast(data); + if (asynccallbackinfo->isAddSlotByType) { + asynccallbackinfo->info.errorCode = NotificationHelper::AddSlotByType(asynccallbackinfo->inType); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::AddNotificationSlot(asynccallbackinfo->slot); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("AddSlot napi_create_async_work end"); + auto asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_AddSlots(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoAddSlots paras; + if (ParseParametersByAddSlots(env, info, paras) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoAddSlots *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoAddSlots {.env = env, .asyncWork = nullptr, .slots = paras.slots}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, paras.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "addSlots", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("AddSlots napi_create_async_work start"); + auto asynccallbackinfo = static_cast(data); + asynccallbackinfo->info.errorCode = NotificationHelper::AddNotificationSlots(asynccallbackinfo->slots); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("AddSlots napi_create_async_work end"); + auto asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_SetSlotByBundle(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoSetSlotByBundle params {}; + if (ParseParametersSetSlotByBundle(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoSetSlotByBundle *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoSetSlotByBundle {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "setSlotByBundle", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("SetSlotByBundle napi_create_async_work start"); + auto asynccallbackinfo = static_cast(data); + + asynccallbackinfo->info.errorCode = NotificationHelper::UpdateNotificationSlots( + asynccallbackinfo->params.option, asynccallbackinfo->params.slots); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("SetSlotByBundle napi_create_async_work end"); + auto asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + + +void AsyncCompleteCallbackNapiGetSlot(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("GetSlot napi_create_async_work end"); + + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + + auto asynccallbackinfo = static_cast(data); + napi_value result = Common::NapiGetNull(env); + if (asynccallbackinfo->info.errorCode == ERR_OK) { + if (asynccallbackinfo->slot == nullptr) { + asynccallbackinfo->info.errorCode = ERROR; + } else { + napi_create_object(env, &result); + if (!Common::SetNotificationSlot(env, *asynccallbackinfo->slot, result)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_GetSlot(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoGetSlot paras; + if (ParseParametersByGetSlot(env, info, paras) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoGetSlot *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoGetSlot {.env = env, .asyncWork = nullptr, .outType = paras.outType}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, paras.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getSlot", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetSlot napi_create_async_work start"); + auto asynccallbackinfo = static_cast(data); + + asynccallbackinfo->info.errorCode = + NotificationHelper::GetNotificationSlot(asynccallbackinfo->outType, asynccallbackinfo->slot); + }, + AsyncCompleteCallbackNapiGetSlot, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_GetSlotNumByBundle(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoGetSlotNumByBundle params {}; + if (ParseParametersGetSlotNumByBundle(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoGetSlotNumByBundle *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoGetSlotNumByBundle {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getSlotNumByBundle", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetSlotNumByBundle napi_create_async_work start"); + auto asynccallbackinfo = reinterpret_cast(data); + + asynccallbackinfo->info.errorCode = NotificationHelper::GetNotificationSlotNumAsBundle( + asynccallbackinfo->params.option, asynccallbackinfo->num); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("GetSlotNumByBundle napi_create_async_work end"); + auto asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + napi_value result = nullptr; + napi_create_uint32(env, asynccallbackinfo->num, &result); + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +void AsyncCompleteCallbackNapiGetSlots(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + napi_value result = nullptr; + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_value arr = nullptr; + napi_create_array(env, &arr); + size_t count = 0; + for (auto vec : asynccallbackinfo->slots) { + if (!vec) { + ANS_LOGW("Invalid NotificationSlot object ptr"); + continue; + } + napi_value nSlot = nullptr; + napi_create_object(env, &nSlot); + if (!Common::SetNotificationSlot(env, *vec, nSlot)) { + continue; + } + napi_set_element(env, arr, count, nSlot); + count++; + } + ANS_LOGI("getSlots count = %{public}zu", count); + result = arr; + if ((count == 0) && (asynccallbackinfo->slots.size() > 0)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_GetSlots(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoGetSlots {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getSlots", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetSlots napi_create_async_work start"); + auto asynccallbackinfo = reinterpret_cast(data); + asynccallbackinfo->info.errorCode = NotificationHelper::GetNotificationSlots(asynccallbackinfo->slots); + }, + AsyncCompleteCallbackNapiGetSlots, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +void AsyncCompleteCallbackNapiGetSlotsByBundle(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + napi_value result = nullptr; + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_value arr = nullptr; + napi_create_array(env, &arr); + size_t count = 0; + for (auto vec : asynccallbackinfo->slots) { + if (!vec) { + ANS_LOGW("Invalid NotificationSlot object ptr"); + continue; + } + napi_value nSlot = nullptr; + napi_create_object(env, &nSlot); + if (!Common::SetNotificationSlot(env, *vec, nSlot)) { + continue; + } + napi_set_element(env, arr, count, nSlot); + count++; + } + ANS_LOGI("getSlots count = %{public}zu", count); + result = arr; + if ((count == 0) && (asynccallbackinfo->slots.size() > 0)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_GetSlotsByBundle(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoGetSlotsByBundle params {}; + if (ParseParametersGetSlotsByBundle(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoGetSlotsByBundle *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoGetSlotsByBundle {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getSlotsByBundle", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetSlotsByBundle napi_create_async_work start"); + auto asynccallbackinfo = reinterpret_cast(data); + asynccallbackinfo->info.errorCode = NotificationHelper::GetNotificationSlotsForBundle( + asynccallbackinfo->params.option, asynccallbackinfo->slots); + }, + AsyncCompleteCallbackNapiGetSlotsByBundle, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_RemoveSlot(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoRemoveSlot paras; + if (ParseParametersByRemoveSlot(env, info, paras) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoRemoveSlot *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoRemoveSlot {.env = env, .asyncWork = nullptr, .outType = paras.outType}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, paras.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "removeSlot", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("removeSlot napi_create_async_work start"); + auto asynccallbackinfo = reinterpret_cast(data); + asynccallbackinfo->info.errorCode = NotificationHelper::RemoveNotificationSlot(asynccallbackinfo->outType); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("removeSlot napi_create_async_work end"); + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_RemoveAllSlots(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + auto *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoRemoveAllSlots {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "removeAll", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("RemoveAllSlots napi_create_async_work start"); + auto asynccallbackinfo = reinterpret_cast(data); + asynccallbackinfo->info.errorCode = NotificationHelper::RemoveAllSlots(); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("RemoveAllSlots napi_create_async_work end"); + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_EnableNotificationSlot(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoEnableSlot params {}; + if (ParseParametersEnableSlot(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoInfoEnableSlot *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoInfoEnableSlot {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "EnableNotificationSlot", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("EnableNotificationSlot napi_create_async_work start"); + auto asynccallbackinfo = static_cast(data); + + asynccallbackinfo->info.errorCode = NotificationHelper::SetEnabledForBundleSlot( + asynccallbackinfo->params.option, asynccallbackinfo->params.outType, asynccallbackinfo->params.enable); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("EnableNotificationSlot napi_create_async_work end"); + auto asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_IsEnableNotificationSlot(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoIsEnableSlot params {}; + if (ParseParametersIsEnableSlot(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoInfoIsEnableSlot *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoInfoIsEnableSlot {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "IsEnableNotificationSlot", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsEnableNotificationSlot napi_create_async_work start"); + auto asynccallbackinfo = static_cast(data); + + asynccallbackinfo->info.errorCode = NotificationHelper::GetEnabledForBundleSlot( + asynccallbackinfo->params.option, asynccallbackinfo->params.outType, asynccallbackinfo->isEnable); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("IsEnableNotificationSlot napi_create_async_work end"); + auto asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + napi_value result = nullptr; + napi_get_boolean(env, asynccallbackinfo->isEnable, &result); + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/manager/napi_template.cpp b/frameworks/js/napi/src/manager/napi_template.cpp new file mode 100644 index 000000000..4ddc90841 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_template.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2021-2022 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 "napi_template.h" + +#include "ans_inner_errors.h" +#include "ans_template.h" + +namespace OHOS { +namespace NotificationNapi { +napi_value Napi_IsSupportTemplate(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + TemplateName params; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoTemplate *asyncCallbackinfo = new (std::nothrow) + AsyncCallbackInfoTemplate {.env = env, .asyncWork = nullptr, .params = params}; + if (!asyncCallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asyncCallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "IsSupportTemplate", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsSupportTemplate napi_create_async_work start"); + AsyncCallbackInfoTemplate *asyncCallbackinfo = static_cast(data); + + if (asyncCallbackinfo) { + asyncCallbackinfo->info.errorCode = NotificationHelper::IsSupportTemplate( + asyncCallbackinfo->params.templateName, asyncCallbackinfo->params.support); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("IsSupportTemplate napi_create_async_work end"); + AsyncCallbackInfoTemplate *asyncCallbackinfo = static_cast(data); + if (asyncCallbackinfo) { + napi_value result = nullptr; + napi_get_boolean(env, asyncCallbackinfo->params.support, &result); + Common::CreateReturnValue(env, asyncCallbackinfo->info, result); + if (asyncCallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asyncCallbackinfo->info.callback); + } + napi_delete_async_work(env, asyncCallbackinfo->asyncWork); + delete asyncCallbackinfo; + asyncCallbackinfo = nullptr; + } + }, + (void *)asyncCallbackinfo, + &asyncCallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackinfo->asyncWork)); + + if (asyncCallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/publish.cpp b/frameworks/js/napi/src/publish.cpp index 2b2602bfd..d83738e5e 100644 --- a/frameworks/js/napi/src/publish.cpp +++ b/frameworks/js/napi/src/publish.cpp @@ -27,18 +27,6 @@ constexpr int8_t SHOW_NOTIFICATION_MAX = 1; constexpr int8_t PUBLISH_AS_BUNDLE_MAX = 4; } -struct AsyncCallbackInfoPublish { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - NotificationRequest request; - CallbackPromiseInfo info; -}; - -struct ParametersInfoPublish { - NotificationRequest request; - napi_ref callback = nullptr; -}; - napi_value GetCallback(const napi_env &env, const napi_value &value, ParametersInfoPublish ¶ms) { ANS_LOGI("enter"); diff --git a/frameworks/js/napi/src/remove.cpp b/frameworks/js/napi/src/remove.cpp index 0e0e70eaa..a5423b20e 100644 --- a/frameworks/js/napi/src/remove.cpp +++ b/frameworks/js/napi/src/remove.cpp @@ -29,40 +29,6 @@ const int REMOVE_BY_BUNDLE_AND_KEY_MAX_PARA = 4; const int REMOVE_GROUP_BY_BUNDLE_MIN_PARA = 2; const int REMOVE_GROUP_BY_BUNDLE_MAX_PARA = 3; -struct BundleAndKeyInfo { - NotificationBundleOption option; - NotificationKey key; -}; - -struct RemoveParams { - std::optional hashcode {}; - std::optional bundleAndKeyInfo {}; - int32_t userId = SUBSCRIBE_USER_INIT; - int32_t removeReason = NotificationConstant::CANCEL_REASON_DELETE; - bool hasUserId = false; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoRemove { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - RemoveParams params {}; - CallbackPromiseInfo info; -}; - -struct RemoveParamsGroupByBundle { - NotificationBundleOption option; - std::string groupName = ""; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoRemoveGroupByBundle { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - RemoveParamsGroupByBundle params {}; - CallbackPromiseInfo info; -}; - bool ParseRemoveReason(const napi_env &env, const napi_value &value, RemoveParams ¶ms) { napi_valuetype valueType = napi_undefined; diff --git a/frameworks/js/napi/src/subscribe.cpp b/frameworks/js/napi/src/subscribe.cpp index ddf67907d..c128d88c4 100644 --- a/frameworks/js/napi/src/subscribe.cpp +++ b/frameworks/js/napi/src/subscribe.cpp @@ -866,14 +866,6 @@ void SubscriberInstance::SetCallbackInfo(const napi_env &env, const std::string } } -struct AsyncCallbackInfoSubscribe { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - SubscriberInstance *objectInfo = nullptr; - NotificationSubscribeInfo subscriberInfo; - CallbackPromiseInfo info; -}; - bool HasNotificationSubscriber(const napi_env &env, const napi_value &value, SubscriberInstancesInfo &subscriberInfo) { std::lock_guard lock(mutex_); diff --git a/frameworks/js/napi/src/subscribe/BUILD.gn b/frameworks/js/napi/src/subscribe/BUILD.gn new file mode 100644 index 000000000..c18561fa6 --- /dev/null +++ b/frameworks/js/napi/src/subscribe/BUILD.gn @@ -0,0 +1,73 @@ +# Copyright (c) 2021 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. + +import("//base/notification/distributed_notification_service/notification.gni") +import("//build/ohos.gni") + +cflags = [] + +config("native_module_config") { + visibility = [ ":*" ] + + include_dirs = [] + + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } +} + +ohos_shared_library("notificationsubscribe") { + include_dirs = [ + "${ability_runtime_path}/interfaces/kits/native/ability/native", + "${ability_runtime_napi_path}/inner/napi_common", + "//foundation/multimedia/image_framework/interfaces/kits/js/common/include/", + "../../include", + "../../include/subscribe", + "//third_party/node/src", + "//third_party/libuv/include", + ] + + configs = [ ":native_module_config" ] + + sources = [ + "../common.cpp", + "../constant.cpp", + "../remove.cpp", + "../subscribe.cpp", + "../unsubscribe.cpp", + "init_module.cpp", + "napi_remove.cpp", + "napi_subscribe.cpp", + ] + + deps = [ + "${ability_runtime_napi_path}/inner/napi_common:napi_common", + "${core_path}:ans_core", + "//foundation/multimedia/image_framework/interfaces/innerkits:image", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:abilitykit_native", + "ability_runtime:wantagent_innerkits", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "napi:ace_napi", + "relational_store:native_rdb", + ] + + relative_install_dir = "module" + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} diff --git a/frameworks/js/napi/src/subscribe/init_module.cpp b/frameworks/js/napi/src/subscribe/init_module.cpp new file mode 100644 index 000000000..d3a5d077d --- /dev/null +++ b/frameworks/js/napi/src/subscribe/init_module.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021-2022 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 "init_module.h" +#include "constant.h" +#include "napi_remove.h" +#include "napi_subscribe.h" +#include "pixel_map_napi.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +EXTERN_C_START + +napi_value NotificationSubscribeInit(napi_env env, napi_value exports) +{ + ANS_LOGI("NotificationSubscribeInit start"); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("subscribe", Napi_Subscribe), + DECLARE_NAPI_FUNCTION("unsubscribe", Napi_Unsubscribe), + DECLARE_NAPI_FUNCTION("remove", Napi_Remove), + DECLARE_NAPI_FUNCTION("removeAll", Napi_RemoveAll), + DECLARE_NAPI_FUNCTION("removeGroupByBundle", Napi_RemoveGroupByBundle), + }; + + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + + return exports; +} +/* + * Module export function + */ +static napi_value Init(napi_env env, napi_value exports) +{ + /* + * Propertise define + */ + NotificationSubscribeInit(env, exports); + ConstantInit(env, exports); + OHOS::Media::PixelMapNapi::Init(env, exports); + + return exports; +} + +/* + * Module register function + */ +__attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&_module_subscribe); +} +EXTERN_C_END +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/subscribe/napi_remove.cpp b/frameworks/js/napi/src/subscribe/napi_remove.cpp new file mode 100644 index 000000000..cf94db4a2 --- /dev/null +++ b/frameworks/js/napi/src/subscribe/napi_remove.cpp @@ -0,0 +1,215 @@ +/* + * Copyright (c) 2022 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 "napi_remove.h" + +#include "ans_inner_errors.h" +#include +#include "remove.h" + +namespace OHOS { +namespace NotificationNapi { +void NapiRemoveExecuteCallback(napi_env env, void *data) +{ + ANS_LOGI("Remove napi_create_async_work start"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + auto removeInfo = static_cast(data); + if (removeInfo->params.hashcode.has_value()) { + removeInfo->info.errorCode = NotificationHelper::RemoveNotification(removeInfo->params.hashcode.value(), + removeInfo->params.removeReason); + } else if (removeInfo->params.bundleAndKeyInfo.has_value()) { + auto &infos = removeInfo->params.bundleAndKeyInfo.value(); + removeInfo->info.errorCode = NotificationHelper::RemoveNotification(infos.option, + infos.key.id, infos.key.label, removeInfo->params.removeReason); + } +} + +void NapiRemoveCompleteCallback(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("Remove napi_create_async_work end"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + auto removeInfo = static_cast(data); + Common::CreateReturnValue(env, removeInfo->info, Common::NapiGetNull(env)); + if (removeInfo->info.callback != nullptr) { + napi_delete_reference(env, removeInfo->info.callback); + } + napi_delete_async_work(env, removeInfo->asyncWork); + delete removeInfo; + removeInfo = nullptr; +} + +napi_value Napi_Remove(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + RemoveParams params {}; + if (!ParseParameters(env, info, params)) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + auto removeInfo = new (std::nothrow) AsyncCallbackInfoRemove {.env = env, .asyncWork = nullptr, .params = params}; + if (!removeInfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, removeInfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "remove", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, nullptr, resourceName, NapiRemoveExecuteCallback, NapiRemoveCompleteCallback, + (void *)removeInfo, &removeInfo->asyncWork); + NAPI_CALL(env, napi_queue_async_work(env, removeInfo->asyncWork)); + if (removeInfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_RemoveAll(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + RemoveParams params {}; + if (ParseParametersByRemoveAll(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoRemove *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoRemove {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "removeAll", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("RemoveAll napi_create_async_work start"); + AsyncCallbackInfoRemove *asynccallbackinfo = static_cast(data); + + if (asynccallbackinfo->params.bundleAndKeyInfo.has_value()) { + auto &infos = asynccallbackinfo->params.bundleAndKeyInfo.value(); + + asynccallbackinfo->info.errorCode = NotificationHelper::RemoveAllNotifications(infos.option); + } else if (asynccallbackinfo->params.hasUserId) { + asynccallbackinfo->info.errorCode = NotificationHelper::RemoveNotifications( + asynccallbackinfo->params.userId); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::RemoveNotifications(); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("RemoveAll napi_create_async_work end"); + AsyncCallbackInfoRemove *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +void AsyncCompleteCallbackNapiRemoveGroupByBundle(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoRemoveGroupByBundle *asynccallbackinfo = static_cast(data); + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value Napi_RemoveGroupByBundle(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + RemoveParamsGroupByBundle params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoRemoveGroupByBundle *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoRemoveGroupByBundle {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "removeGroupByBundle", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("RemoveGroupByBundle napi_create_async_work start"); + AsyncCallbackInfoRemoveGroupByBundle *asynccallbackinfo = + static_cast(data); + ANS_LOGI("option.bundle = %{public}s, option.uid = %{public}d, groupName = %{public}s", + asynccallbackinfo->params.option.GetBundleName().c_str(), + asynccallbackinfo->params.option.GetUid(), + asynccallbackinfo->params.groupName.c_str()); + asynccallbackinfo->info.errorCode = NotificationHelper::RemoveGroupByBundle( + asynccallbackinfo->params.option, asynccallbackinfo->params.groupName); + }, + AsyncCompleteCallbackNapiRemoveGroupByBundle, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/subscribe/napi_subscribe.cpp b/frameworks/js/napi/src/subscribe/napi_subscribe.cpp new file mode 100644 index 000000000..965a8ef28 --- /dev/null +++ b/frameworks/js/napi/src/subscribe/napi_subscribe.cpp @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2022 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 "napi_subscribe.h" + +#include "ans_inner_errors.h" +#include "subscribe.h" +#include "unsubscribe.h" + +namespace OHOS { +namespace NotificationNapi { +napi_value Napi_Subscribe(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + SubscriberInstance *objectInfo = nullptr; + NotificationSubscribeInfo subscriberInfo; + if (ParseParameters(env, info, subscriberInfo, objectInfo, callback) == nullptr) { + if (objectInfo) { + delete objectInfo; + objectInfo = nullptr; + } + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoSubscribe *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoSubscribe { + .env = env, .asyncWork = nullptr, .objectInfo = objectInfo, .subscriberInfo = subscriberInfo + }; + if (!asynccallbackinfo) { + if (objectInfo) { + delete objectInfo; + objectInfo = nullptr; + } + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "subscribeNotification", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("Subscribe napi_create_async_work start"); + if (!data) { + ANS_LOGE("Invalid asynccallbackinfo!"); + return; + } + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo->subscriberInfo.hasSubscribeInfo) { + ANS_LOGI("Subscribe with NotificationSubscribeInfo"); + OHOS::Notification::NotificationSubscribeInfo subscribeInfo; + subscribeInfo.AddAppNames(asynccallbackinfo->subscriberInfo.bundleNames); + subscribeInfo.AddAppUserId(asynccallbackinfo->subscriberInfo.userId); + asynccallbackinfo->info.errorCode = + NotificationHelper::SubscribeNotification(*(asynccallbackinfo->objectInfo), subscribeInfo); + } else { + asynccallbackinfo->info.errorCode = + NotificationHelper::SubscribeNotification(*(asynccallbackinfo->objectInfo)); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("Subscribe napi_create_async_work end"); + if (!data) { + ANS_LOGE("Invalid asynccallbackinfo!"); + return; + } + auto asynccallbackinfo = reinterpret_cast(data); + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value Napi_Unsubscribe(napi_env env, napi_callback_info info) +{ + ANS_LOGI("Unsubscribe start"); + + ParametersInfoUnsubscribe paras; + if (ParseParameters(env, info, paras) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoUnsubscribe *asynccallbackinfo = new (std::nothrow) + AsyncCallbackInfoUnsubscribe {.env = env, .asyncWork = nullptr, .objectInfo = paras.objectInfo}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, paras.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "unsubscribe", NAPI_AUTO_LENGTH, &resourceName); + + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("Unsubscribe napi_create_async_work start"); + auto asynccallbackinfo = reinterpret_cast(data); + + if (asynccallbackinfo->objectInfo == nullptr) { + ANS_LOGE("invalid object info"); + asynccallbackinfo->info.errorCode = ERR_ANS_INVALID_PARAM; + return; + } + + bool ret = AddDeletingSubscriber(asynccallbackinfo->objectInfo); + if (ret) { + asynccallbackinfo->info.errorCode = + NotificationHelper::UnSubscribeNotification(*(asynccallbackinfo->objectInfo)); + if (asynccallbackinfo->info.errorCode != ERR_OK) { + DelDeletingSubscriber(asynccallbackinfo->objectInfo); + } + } else { + asynccallbackinfo->info.errorCode = ERR_ANS_SUBSCRIBER_IS_DELETING; + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("Unsubscribe napi_create_async_work end"); + AsyncCallbackInfoUnsubscribe *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/unsubscribe.cpp b/frameworks/js/napi/src/unsubscribe.cpp index 2f839a257..e0c3258b1 100644 --- a/frameworks/js/napi/src/unsubscribe.cpp +++ b/frameworks/js/napi/src/unsubscribe.cpp @@ -14,7 +14,6 @@ */ #include "unsubscribe.h" -#include "subscribe.h" #include "ans_inner_errors.h" @@ -22,18 +21,6 @@ namespace OHOS { namespace NotificationNapi { const int UNSUBSCRIBE_MAX_PARA = 2; -struct AsyncCallbackInfoUnsubscribe { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - SubscriberInstance *objectInfo = nullptr; - CallbackPromiseInfo info; -}; - -struct ParametersInfoUnsubscribe { - SubscriberInstance *objectInfo = nullptr; - napi_ref callback = nullptr; -}; - napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, ParametersInfoUnsubscribe ¶s) { ANS_LOGI("enter"); -- Gitee