1 /* 2 * Copyright (c) 2025 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 16 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_ETS_ANI_INCLUDE_STS_SUBSCRIBE_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_ETS_ANI_INCLUDE_STS_SUBSCRIBE_H 18 #include "ani.h" 19 #include "notification_request.h" 20 #include "notification_operation_info.h" 21 #include "ans_operation_callback_stub.h" 22 #include "notification_subscriber.h" 23 #include "notification_do_not_disturb_date.h" 24 25 #include "sts_notification_manager.h" 26 #include "sts_subscriber.h" 27 28 namespace OHOS { 29 namespace NotificationSts { 30 using NotificationKey = OHOS::Notification::NotificationKey; 31 using StsNotificationOperationInfo = OHOS::Notification::NotificationOperationInfo; 32 33 class StsDistributedOperationCallback : public OHOS::Notification::AnsOperationCallbackStub { 34 public: 35 explicit StsDistributedOperationCallback(ani_object promise, ani_resolver resolver); ~StsDistributedOperationCallback()36 ~StsDistributedOperationCallback() override {}; 37 ErrCode OnOperationCallback(const int32_t operationResult) override; 38 void OnStsOperationCallback(ani_env *env, const int32_t operationResult); 39 void SetVm(ani_vm *vm); 40 private: 41 ani_vm *etsVm_; 42 ani_resolver resolver_; 43 bool isCall_ = false; 44 std::mutex lock_; 45 }; 46 47 class StsSubscriberInstance : public OHOS::Notification::NotificationSubscriber { 48 public: 49 StsSubscriberInstance(); 50 virtual ~StsSubscriberInstance(); 51 52 virtual void OnCanceled(const std::shared_ptr<OHOS::Notification::Notification> &request, 53 const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override; 54 55 virtual void OnConsumed(const std::shared_ptr<OHOS::Notification::Notification> &request, 56 const std::shared_ptr<NotificationSortingMap> &sortingMap) override; 57 58 virtual void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override; 59 60 virtual void OnConnected() override; 61 62 virtual void OnDisconnected() override; 63 64 virtual void OnDied() override; 65 66 virtual void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override; 67 68 void onDoNotDisturbChanged(const std::shared_ptr<NotificationDoNotDisturbDate> &date); 69 70 virtual void OnEnabledNotificationChanged( 71 const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override; 72 73 void OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData) override; 74 75 void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override; 76 77 virtual void OnBatchCanceled(const std::vector<std::shared_ptr<OHOS::Notification::Notification>> &requestList, 78 const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override; 79 80 virtual bool HasOnBatchCancelCallback() override; 81 82 bool SetObject(ani_env *env, ani_object obj); 83 bool IsInit(); 84 bool Compare(ani_env *env, ani_object obj); 85 bool Compare(std::shared_ptr<StsSubscriberInstance> instance); 86 87 private: 88 bool CallFunction(ani_env *env, const char* func, std::vector<ani_ref> &parm); 89 90 private: 91 ani_ref ref_ = nullptr; 92 ani_object obj_ = nullptr; 93 ani_vm *vm_ = nullptr; 94 std::mutex lock_; 95 }; 96 97 class SubscriberInstanceManager { 98 public: GetInstance()99 static SubscriberInstanceManager* GetInstance() 100 { 101 static SubscriberInstanceManager instance; 102 return &instance; 103 } 104 ~SubscriberInstanceManager() = default; 105 106 bool HasNotificationSubscriber( 107 ani_env *env, ani_object value, std::shared_ptr<StsSubscriberInstance> &subscriberInfo); 108 bool AddSubscriberInstancesInfo(ani_env *env, std::shared_ptr<StsSubscriberInstance> &subscriberInfo); 109 bool DelSubscriberInstancesInfo(ani_env *env, ani_object obj); 110 111 bool AddDeletingSubscriber(std::shared_ptr<StsSubscriberInstance> subscriber); 112 void DelDeletingSubscriber(std::shared_ptr<StsSubscriberInstance> subscriber); 113 114 bool Subscribe(ani_env *env, ani_object subscriber, ani_object info); 115 bool SubscribeSelf(ani_env *env, ani_object subscriber); 116 bool UnSubscribe(ani_env *env, ani_object subscriber); 117 private: SubscriberInstanceManager()118 SubscriberInstanceManager() {} 119 120 bool GetNotificationSubscriber( 121 ani_env *env, ani_object value, std::shared_ptr<StsSubscriberInstance> &subscriberInfo); 122 123 private: 124 std::mutex mutex_; 125 std::vector<std::shared_ptr<StsSubscriberInstance>> subscriberInstances_; 126 std::mutex delMutex_; 127 std::vector<std::shared_ptr<StsSubscriberInstance>> DeletingSubscriber; 128 }; 129 130 bool IsValidRemoveReason(int32_t reasonType); 131 bool UnWarpReasonEnum(ani_env *env, const ani_object enumItem, int32_t &outEnum); 132 bool UnWarpNotificationKey(ani_env *env, const ani_object obj, NotificationKey &OutObj); 133 bool UnwarpOperationInfo(ani_env *env, const ani_object obj, StsNotificationOperationInfo &outObj); 134 sptr<StsNotificationOperationInfo> GetOperationInfoForDistributeOperation( 135 ani_env *env, ani_string hashcode, ani_object operationInfo, bool &noWithOperationInfo); 136 } // namespace NotificationSts 137 } // OHOS 138 #endif