diff --git a/frameworks/core/common/include/ans_inner_errors.h b/frameworks/core/common/include/ans_inner_errors.h index 3fd34d197cc2ef4c8d0d9d71e48a6d9f9b094c5f..92cfe55e020697bf8913565d5f6c2684c70c6bdd 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 2c9a2b8320493448166b10d173c069830c532fc5..c3e7b549618c2aae4f6934bd3a1b3caaf8664a2f 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 c3ed2ddedd7d1dedf6e7e8bea7935b5c3eff1d91..7f40bc2e1d2d603f28497188c4d5cb230abb2acc 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 30b357af2c96dd023351bb37e4d08aaefeb6dc93..539a12afabaf0c4b5a920cbe2c51beb6b0bf4130 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 10181436b95ed757acca323fe606a040779055f3..621f0ecb49921d268fa504a9aa464b38c2c82975 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 671e53300d98ca0df1a10300ca05ddeec58f5e03..a8cf2824f3fd2d8ec2c6f9aa4e376515c8c134d4 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 630de77cd4f7ead1464c6625ed07801106978c59..de5dfa4a449ce7fd68c00d2e9d680a911847be31 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 5c0fef0a7ab4938ed002a266149c7e3f44524074..a0309b909345b478462b2c8f2bb74a6284c4892c 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 20f46801266bc19764fd3731f805dbbb05418eb9..f8656ed158a3a82286a99aca6e53e612a1f68d75 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 837747dc72c14905cb000e63ea7574db861e688f..2fe7457c7dccaf3a861009656e94616a8b7e0377 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 0000000000000000000000000000000000000000..193d7b4840c71881bfc2045a2009b8020269bf87 --- /dev/null +++ b/frameworks/js/napi/include/manager/init_module.h @@ -0,0 +1,50 @@ +/* + * 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_publish.h b/frameworks/js/napi/include/manager/napi_publish.h new file mode 100644 index 0000000000000000000000000000000000000000..1251eaf2f5cb38d034b634bf9be67c9d6e61711c --- /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 NapiPublish(napi_env env, napi_callback_info info); +napi_value NapiShowNotification(napi_env env, napi_callback_info info); +napi_value NapiPublishAsBundle(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/publish.h b/frameworks/js/napi/include/publish.h index 44de7e95bba1a354663f70d12675d36276fd8a0a..0c8b81ed266afe417789590a941960c1e32b5dbe 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 5beb3a8cc8f5aa55bb9873dea6d9cf66a6344276..36594bcb196f53c9c6bcbbd57a8d45de1e30cacf 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 06c1286de7ca5b8a773148a639d13397e9c8f7ac..44b4a4eee3080ee652ba416644891dba63daab87 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 0000000000000000000000000000000000000000..1f7c642adeef2520f87ad356e1fd444d33c28687 --- /dev/null +++ b/frameworks/js/napi/include/subscribe/init_module.h @@ -0,0 +1,50 @@ +/* + * 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 0000000000000000000000000000000000000000..d148dc5ded45e6eb3dcdb516e314c596968dbec0 --- /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 NapiRemove(napi_env env, napi_callback_info info); +napi_value NapiRemoveAsBundle(napi_env env, napi_callback_info info); +napi_value NapiRemoveAll(napi_env env, napi_callback_info info); +napi_value NapiRemoveGroupByBundle(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 0000000000000000000000000000000000000000..5bd699e838335f5f4f39d028e5c7251bfdf0f461 --- /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 NapiSubscribe(napi_env env, napi_callback_info info); +napi_value NapiUnsubscribe(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 b40c3a2760847fabeae7dcc4934fb33852211489..d3db0dfba5023ac5f95c28c79518e020cdd90bfe 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 7110af5a7fff4fade61c5c68cba4ef5b608e8813..0321e1eaa4cf3a550093f455be76e1d52c17ff44 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 7c418bce3fbe5489b554f4de05c6fe12ba7c0f34..9121d2d12fdeef1dab0e948e2239e5e440697358 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 f8942f60904f11466770a15b86792a0aa069345c..ab665789b3dd5cc0ef27c58b726f5071a815fc5e 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,26 @@ namespace OHOS { namespace NotificationNapi { std::set> Common::wantAgent_; +namespace { +static const std::unordered_map ERROR_CODE_MESSAGE { + {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 +69,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 = ERROR_CODE_MESSAGE.find(errCode); + std::string errMsg = iter != ERROR_CODE_MESSAGE.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 +140,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 +167,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 +4710,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 b648e0bd7b22012f1d5448128fa46c1ccc864cb7..f0680657e46f0d4933ec5479fa1d09024164dfd3 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 3684872ca33a96e95f6c52895f094c183fddf7e1..6f1a492f6072fe4839725047a3a06539840dd5c4 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 e901f02856a25a8e521599ea723d8a0150d5721f..3ba80b6e7cd3324811c30a830b8cb60dbdef54bb 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 de7e99a4e2b61ce7fd8d74f088f870d29e4a4e3f..9da7ec28f7e59bdd9e734d2dd80748286eace2f8 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 1904caf65a9ae9bb4a79de332ce7d1e9961b7f2d..243f9869a163f192846cff6ad1ae82f3de349b36 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 346eea41dc008dd1d0f9b25152861d3703fedc5e..888b10577313af03c08cc64df57ff6fc0f2a2c34 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 0000000000000000000000000000000000000000..8d04207cdbdb3ab168ad6881b6ac08345be5d97e --- /dev/null +++ b/frameworks/js/napi/src/manager/BUILD.gn @@ -0,0 +1,78 @@ +# 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_publish.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 0000000000000000000000000000000000000000..14c523c89673b4fc6787955cb2c540cd0f7db4fe --- /dev/null +++ b/frameworks/js/napi/src/manager/init_module.cpp @@ -0,0 +1,66 @@ +/* + * 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_publish.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", NapiPublish), + DECLARE_NAPI_FUNCTION("publishAsBundle", NapiPublishAsBundle), + DECLARE_NAPI_FUNCTION("show", NapiShowNotification), + }; + + 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_publish.cpp b/frameworks/js/napi/src/manager/napi_publish.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c4d48f07eb1dfe09b5806b7ed1c5a799a69d302 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_publish.cpp @@ -0,0 +1,197 @@ +/* + * 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 NapiPublish(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 NapiShowNotification(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) { + 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 NapiPublishAsBundle(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/publish.cpp b/frameworks/js/napi/src/publish.cpp index 2b2602bfd2831deaac8b6cb9403b7b12cc491baf..d83738e5e6730cb34339388bc2f65831650c7d86 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 0e0e70eaaeec4c9c73c50542b34237fd95560a90..a5423b20e6bd7bb021a91b50d6be52b606f37181 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 ddf67907d521f3cf25e5b0219077a33f8a001b70..c128d88c4a3a8c864eb24a1062c52546e09f943f 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 0000000000000000000000000000000000000000..c18561fa6f9d2885996d70586731456fe40fa685 --- /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 0000000000000000000000000000000000000000..a81c51b2cc434a65b74f1ce52e71f22480fc1249 --- /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", NapiSubscribe), + DECLARE_NAPI_FUNCTION("unsubscribe", NapiUnsubscribe), + DECLARE_NAPI_FUNCTION("remove", NapiRemove), + DECLARE_NAPI_FUNCTION("removeAll", NapiRemoveAll), + DECLARE_NAPI_FUNCTION("removeGroupByBundle", NapiRemoveGroupByBundle), + }; + + 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 0000000000000000000000000000000000000000..a4838e9d9f17d962a5618decea0160566174c1c2 --- /dev/null +++ b/frameworks/js/napi/src/subscribe/napi_remove.cpp @@ -0,0 +1,211 @@ +/* + * 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 NapiRemove(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 NapiRemoveAll(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 NapiRemoveGroupByBundle(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 0000000000000000000000000000000000000000..c3b009c8971743b6d295978838427185112bac7d --- /dev/null +++ b/frameworks/js/napi/src/subscribe/napi_subscribe.cpp @@ -0,0 +1,174 @@ +/* + * 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 NapiSubscribe(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 NapiUnsubscribe(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 2f839a257f3aca8b71b73a1fa1ea4513c1a26081..e0c3258b17aaa042d41e1e99609fb84c0c2e4e1d 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");