1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_SUBSCRIBE_H 16 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_SUBSCRIBE_H 17 18 #include "common.h" 19 20 namespace OHOS { 21 namespace NotificationNapi { 22 using namespace OHOS::Notification; 23 24 class SubscriberInstance : public NotificationSubscriber { 25 public: 26 SubscriberInstance(); 27 virtual ~SubscriberInstance(); 28 29 /** 30 * @brief Called back when a notification is canceled. 31 * 32 * @param request Indicates the canceled NotificationRequest object. 33 * @param sortingMap Indicates the sorting map used by the current subscriber to obtain notification ranking 34 * information. 35 * @param deleteReason Indicates the reason for the deletion. For details, see NotificationConstant. 36 */ 37 virtual void OnCanceled(const std::shared_ptr<OHOS::Notification::Notification> &request, 38 const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override; 39 40 /** 41 * @brief Called back when a notification is canceled. 42 * 43 * @param request Indicates the received NotificationRequest object. 44 */ 45 virtual void OnConsumed(const std::shared_ptr<OHOS::Notification::Notification> &request) override; 46 47 /** 48 * @brief Called back when a notification is canceled. 49 * 50 * @param request Indicates the received NotificationRequest object. 51 * @param sortingMap Indicates the sorting map used by the current subscriber to obtain notification ranking 52 * information. 53 */ 54 virtual void OnConsumed(const std::shared_ptr<OHOS::Notification::Notification> &request, 55 const std::shared_ptr<NotificationSortingMap> &sortingMap) override; 56 57 /** 58 * @brief Called back when a notification is canceled. 59 * 60 * @param sortingMap Indicates the sorting map used to obtain notification ranking information. 61 */ 62 virtual void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override; 63 64 /** 65 * @brief Called back when a notification is canceled. 66 * 67 */ 68 virtual void OnConnected() override; 69 70 /** 71 * @brief Called back when the subscriber is disconnected from the ANS. 72 * 73 */ 74 virtual void OnDisconnected() override; 75 76 /** 77 * @brief Called back when connection to the ANS has died. 78 * 79 */ 80 virtual void OnDied() override; 81 82 /** 83 * @brief Called when the Do Not Disturb mode type changes. 84 * 85 * @param date Indicates the NotificationDoNotDisturbDate object. 86 */ 87 virtual void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override; 88 89 /** 90 * @brief Called when the enabled notification changes. 91 * 92 * @param callbackData Indicates the EnabledNotificationCallbackData object. 93 */ 94 virtual void OnEnabledNotificationChanged( 95 const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override; 96 97 /** 98 * @brief Sets the callback information by type. 99 * 100 * @param env Indicates the environment that the API is invoked under. 101 * @param type Indicates the type of callback. 102 * @param ref Indicates the napi_ref of callback. 103 */ 104 void SetCallbackInfo(const napi_env &env, const std::string &type, const napi_ref &ref); 105 106 private: 107 void SetCancelCallbackInfo(const napi_env &env, const napi_ref &ref); 108 109 void SetConsumeCallbackInfo(const napi_env &env, const napi_ref &ref); 110 111 void SetUpdateCallbackInfo(const napi_env &env, const napi_ref &ref); 112 113 void SetSubscribeCallbackInfo(const napi_env &env, const napi_ref &ref); 114 115 void SetUnsubscribeCallbackInfo(const napi_env &env, const napi_ref &ref); 116 117 void SetDieCallbackInfo(const napi_env &env, const napi_ref &ref); 118 119 void SetDisturbModeCallbackInfo(const napi_env &env, const napi_ref &ref); 120 121 void SetDisturbDateCallbackInfo(const napi_env &env, const napi_ref &ref); 122 123 void SetEnabledNotificationCallbackInfo(const napi_env &env, const napi_ref &ref); 124 125 private: 126 struct CallbackInfo { 127 napi_env env = nullptr; 128 napi_ref ref = nullptr; 129 }; 130 131 CallbackInfo canceCallbackInfo_; 132 CallbackInfo consumeCallbackInfo_; 133 CallbackInfo updateCallbackInfo_; 134 CallbackInfo subscribeCallbackInfo_; 135 CallbackInfo unsubscribeCallbackInfo_; 136 CallbackInfo dieCallbackInfo_; 137 CallbackInfo disturbModeCallbackInfo_; 138 CallbackInfo disturbDateCallbackInfo_; 139 CallbackInfo enabledNotificationCallbackInfo_; 140 }; 141 142 struct SubscriberInstancesInfo { 143 napi_ref ref = nullptr; 144 SubscriberInstance *subscriber = nullptr; 145 }; 146 147 struct AsyncCallbackInfoSubscribe { 148 napi_env env = nullptr; 149 napi_async_work asyncWork = nullptr; 150 SubscriberInstance *objectInfo = nullptr; 151 NotificationSubscribeInfo subscriberInfo; 152 CallbackPromiseInfo info; 153 }; 154 155 static std::mutex mutex_; 156 static thread_local std::vector<SubscriberInstancesInfo> subscriberInstances_; 157 158 static std::mutex delMutex_; 159 static std::vector<SubscriberInstance*> DeletingSubscriber; 160 161 bool HasNotificationSubscriber(const napi_env &env, const napi_value &value, SubscriberInstancesInfo &subscriberInfo); 162 bool AddSubscriberInstancesInfo(const napi_env &env, const SubscriberInstancesInfo &subscriberInfo); 163 bool DelSubscriberInstancesInfo(const napi_env &env, const SubscriberInstance *subscriber); 164 165 bool AddDeletingSubscriber(SubscriberInstance *subscriber); 166 void DelDeletingSubscriber(SubscriberInstance *subscriber); 167 168 napi_value Subscribe(napi_env env, napi_callback_info info); 169 170 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, 171 NotificationSubscribeInfo &subscriberInfo, SubscriberInstance *&subscriber, napi_ref &callback); 172 } // namespace NotificationNapi 173 } // namespace OHOS 174 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_SUBSCRIBE_H