1 /* 2 * Copyright (c) 2021-2024 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 * @param sortingMap Indicates the sorting map used by the current subscriber to obtain notification ranking 45 * information. 46 */ 47 virtual void OnConsumed(const std::shared_ptr<OHOS::Notification::Notification> &request, 48 const std::shared_ptr<NotificationSortingMap> &sortingMap) override; 49 50 /** 51 * @brief Called back when a notification is canceled. 52 * 53 * @param sortingMap Indicates the sorting map used to obtain notification ranking information. 54 */ 55 virtual void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override; 56 57 /** 58 * @brief Called back when a notification is canceled. 59 * 60 */ 61 virtual void OnConnected() override; 62 63 /** 64 * @brief Called back when the subscriber is disconnected from the ANS. 65 * 66 */ 67 virtual void OnDisconnected() override; 68 69 /** 70 * @brief Called back when connection to the ANS has died. 71 * 72 */ 73 virtual void OnDied() override; 74 75 /** 76 * @brief Called when the Do Not Disturb mode type changes. 77 * 78 * @param date Indicates the NotificationDoNotDisturbDate object. 79 */ 80 virtual void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override; 81 82 /** 83 * @brief Called when the Do Not Disturb mode type changes. 84 * 85 * @param date Indicates the NotificationDoNotDisturbDate object. 86 */ 87 void onDoNotDisturbChanged(const std::shared_ptr<NotificationDoNotDisturbDate> &date); 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 The callback function on the badge number changed. 99 * 100 * @param badgeData Indicates the BadgeNumberCallbackData object. 101 */ 102 void OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData) override; 103 104 /** 105 * @brief The callback function on the badge enabled state changed. 106 * 107 * @param callbackData Indicates the EnabledNotificationCallbackData object. 108 */ 109 void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override; 110 111 /** 112 * @brief The callback function on the badge number changed. 113 * 114 * @param badgeData Indicates the BadgeNumberCallbackData object. 115 */ 116 virtual void OnBatchCanceled(const std::vector<std::shared_ptr<OHOS::Notification::Notification>> &requestList, 117 const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override; 118 119 /** 120 * @brief The callback function on the badge number changed. 121 * 122 * @param badgeData Indicates the BadgeNumberCallbackData object. 123 */ 124 virtual bool HasOnBatchCancelCallback() override; 125 126 /** 127 * @brief Sets the callback information by type. 128 * 129 * @param env Indicates the environment that the API is invoked under. 130 * @param type Indicates the type of callback. 131 * @param ref Indicates the napi_ref of callback. 132 */ 133 void SetCallbackInfo(const napi_env &env, const std::string &type, const napi_ref &ref); 134 135 /** 136 * @brief Sets threadsafe_function. 137 * 138 * @param tsfn Indicates the napi_threadsafe_function of callback. 139 */ 140 void SetThreadSafeFunction(const napi_threadsafe_function &tsfn); 141 142 private: 143 void SetCancelCallbackInfo(const napi_env &env, const napi_ref &ref); 144 145 void SetConsumeCallbackInfo(const napi_env &env, const napi_ref &ref); 146 147 void SetUpdateCallbackInfo(const napi_env &env, const napi_ref &ref); 148 149 void SetSubscribeCallbackInfo(const napi_env &env, const napi_ref &ref); 150 151 void SetUnsubscribeCallbackInfo(const napi_env &env, const napi_ref &ref); 152 153 void SetDieCallbackInfo(const napi_env &env, const napi_ref &ref); 154 155 void SetDisturbModeCallbackInfo(const napi_env &env, const napi_ref &ref); 156 157 void SetDisturbDateCallbackInfo(const napi_env &env, const napi_ref &ref); 158 159 void SetDisturbChangedCallbackInfo(const napi_env &env, const napi_ref &ref); 160 161 void SetEnabledNotificationCallbackInfo(const napi_env &env, const napi_ref &ref); 162 163 void SetBadgeCallbackInfo(const napi_env &env, const napi_ref &ref); 164 165 void SetBadgeEnabledCallbackInfo(const napi_env &env, const napi_ref &ref); 166 167 void SetBatchCancelCallbackInfo(const napi_env &env, const napi_ref &ref); 168 169 private: 170 struct CallbackInfo { 171 napi_env env = nullptr; 172 napi_ref ref = nullptr; 173 }; 174 napi_threadsafe_function tsfn_ = nullptr; 175 CallbackInfo canceCallbackInfo_; 176 CallbackInfo consumeCallbackInfo_; 177 CallbackInfo updateCallbackInfo_; 178 CallbackInfo subscribeCallbackInfo_; 179 CallbackInfo unsubscribeCallbackInfo_; 180 CallbackInfo dieCallbackInfo_; 181 CallbackInfo disturbModeCallbackInfo_; 182 CallbackInfo disturbDateCallbackInfo_; 183 CallbackInfo disturbChangedCallbackInfo_; 184 CallbackInfo enabledNotificationCallbackInfo_; 185 CallbackInfo setBadgeCallbackInfo_; 186 CallbackInfo setBadgeEnabledCallbackInfo_; 187 CallbackInfo batchCancelCallbackInfo_; 188 }; 189 190 struct SubscriberInstancesInfo { 191 napi_ref ref = nullptr; 192 std::shared_ptr<SubscriberInstance> subscriber = nullptr; 193 }; 194 195 struct AsyncCallbackInfoSubscribe { 196 napi_env env = nullptr; 197 napi_async_work asyncWork = nullptr; 198 std::shared_ptr<SubscriberInstance> objectInfo = nullptr; 199 NotificationSubscribeInfo subscriberInfo; 200 CallbackPromiseInfo info; 201 }; 202 203 static std::mutex mutex_; 204 static thread_local std::vector<SubscriberInstancesInfo> subscriberInstances_; 205 206 static std::mutex delMutex_; 207 static std::vector<std::shared_ptr<SubscriberInstance>> DeletingSubscriber; 208 209 bool HasNotificationSubscriber(const napi_env &env, const napi_value &value, SubscriberInstancesInfo &subscriberInfo); 210 bool AddSubscriberInstancesInfo(const napi_env &env, const SubscriberInstancesInfo &subscriberInfo); 211 bool DelSubscriberInstancesInfo(const napi_env &env, const std::shared_ptr<SubscriberInstance> subscriber); 212 213 bool AddDeletingSubscriber(std::shared_ptr<SubscriberInstance> subscriber); 214 void DelDeletingSubscriber(std::shared_ptr<SubscriberInstance> subscriber); 215 216 napi_value Subscribe(napi_env env, napi_callback_info info); 217 218 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, 219 NotificationSubscribeInfo &subscriberInfo, std::shared_ptr<SubscriberInstance> &subscriber, napi_ref &callback); 220 } // namespace NotificationNapi 221 } // namespace OHOS 222 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_SUBSCRIBE_H 223