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 16 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_INCLUDE_NOTIFICATION_SUBSCRIBER_MANAGER_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_INCLUDE_NOTIFICATION_SUBSCRIBER_MANAGER_H 18 19 #include <list> 20 #include <memory> 21 #include <mutex> 22 23 #include "errors.h" 24 #include "event_handler.h" 25 #include "event_runner.h" 26 #include "nocopyable.h" 27 #include "refbase.h" 28 #include "singleton.h" 29 30 #include "ans_subscriber_interface.h" 31 #include "notification_bundle_option.h" 32 #include "notification_constant.h" 33 #include "notification_request.h" 34 #include "notification_sorting_map.h" 35 #include "notification_subscribe_info.h" 36 37 namespace OHOS { 38 namespace Notification { 39 class NotificationSubscriberManager : public DelayedSingleton<NotificationSubscriberManager> { 40 public: 41 /** 42 * @brief Add a subscriber. 43 * 44 * @param subscriber Indicates the AnsSubscriberInterface object. 45 * @param subscribeInfo Indicates the NotificationSubscribeInfo object. 46 * @return Indicates the result code. 47 */ 48 ErrCode AddSubscriber(const sptr<AnsSubscriberInterface> &subscriber, 49 const sptr<NotificationSubscribeInfo> &subscribeInfo); 50 51 /** 52 * @brief Remove a subscriber. 53 * 54 * @param subscriber Indicates the AnsSubscriberInterface object. 55 * @param subscribeInfo Indicates the NotificationSubscribeInfo object. 56 * @return Indicates the result code. 57 */ 58 ErrCode RemoveSubscriber( 59 const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo); 60 61 /** 62 * @brief Notify all subscribers on counsumed. 63 * 64 * @param notification Indicates the Notification object. 65 * @param notificationMap Indicates the NotificationSortingMap object. 66 */ 67 void NotifyConsumed(const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap); 68 69 /** 70 * @brief Notify all subscribers on canceled. 71 * 72 * @param notification Indicates the Notification object. 73 * @param notificationMap Indicates the NotificationSortingMap object. 74 * @param deleteReason Indicates the delete reason. 75 */ 76 void NotifyCanceled(const sptr<Notification> ¬ification, 77 const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason); 78 79 /** 80 * @brief Notify all subscribers on updated. 81 * 82 * @param notificationMap Indicates the NotificationSortingMap object. 83 */ 84 void NotifyUpdated(const sptr<NotificationSortingMap> ¬ificationMap); 85 86 /** 87 * @brief Notify all subscribers on dnd date changed. 88 * 89 * @param date Indicates the NotificationDoNotDisturbDate object. 90 */ 91 void NotifyDoNotDisturbDateChanged(const sptr<NotificationDoNotDisturbDate> &date); 92 93 void NotifyEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData); 94 95 /** 96 * @brief Obtains the death event. 97 * 98 * @param object Indicates the death object. 99 */ 100 void OnRemoteDied(const wptr<IRemoteObject> &object); 101 102 private: 103 struct SubscriberRecord; 104 105 std::shared_ptr<SubscriberRecord> FindSubscriberRecord(const wptr<IRemoteObject> &object); 106 std::shared_ptr<SubscriberRecord> FindSubscriberRecord(const sptr<AnsSubscriberInterface> &subscriber); 107 std::shared_ptr<SubscriberRecord> CreateSubscriberRecord(const sptr<AnsSubscriberInterface> &subscriber); 108 void AddRecordInfo( 109 std::shared_ptr<SubscriberRecord> &record, const sptr<NotificationSubscribeInfo> &subscribeInfo); 110 void RemoveRecordInfo( 111 std::shared_ptr<SubscriberRecord> &record, const sptr<NotificationSubscribeInfo> &subscribeInfo); 112 ErrCode AddSubscriberInner( 113 const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo); 114 ErrCode RemoveSubscriberInner( 115 const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo); 116 117 void NotifyConsumedInner( 118 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap); 119 void NotifyCanceledInner(const sptr<Notification> ¬ification, 120 const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason); 121 void NotifyUpdatedInner(const sptr<NotificationSortingMap> ¬ificationMap); 122 void NotifyDoNotDisturbDateChangedInner(const sptr<NotificationDoNotDisturbDate> &date); 123 void NotifyEnabledNotificationChangedInner(const sptr<EnabledNotificationCallbackData> &callbackData); 124 bool IsSystemUser(int32_t userId); 125 126 private: 127 std::list<std::shared_ptr<SubscriberRecord>> subscriberRecordList_ {}; 128 std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner_ {}; 129 std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ {}; 130 sptr<AnsSubscriberInterface> ansSubscriberProxy_ {}; 131 sptr<IRemoteObject::DeathRecipient> recipient_ {}; 132 133 DECLARE_DELAYED_SINGLETON(NotificationSubscriberManager); 134 DISALLOW_COPY_AND_MOVE(NotificationSubscriberManager); 135 }; 136 } // namespace Notification 137 } // namespace OHOS 138 139 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_INCLUDE_NOTIFICATION_SUBSCRIBER_MANAGER_H