1 /*
2  * Copyright (c) 2023 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 BND_NOTIFICATION_SERVICE_SERVICES_INCLUDE_NOTIFICATION_LOCAL_LIVE_VIEW_SUBSCRIBER_LOCAL_LIVE_VIEW_MANAGER_H
17 #define BND_NOTIFICATION_SERVICE_SERVICES_INCLUDE_NOTIFICATION_LOCAL_LIVE_VIEW_SUBSCRIBER_LOCAL_LIVE_VIEW_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 "ffrt.h"
27 #include "nocopyable.h"
28 #include "refbase.h"
29 #include "singleton.h"
30 
31 #include "ans_subscriber_local_live_view_interface.h"
32 #include "notification_bundle_option.h"
33 #include "notification_constant.h"
34 #include "notification_request.h"
35 #include "notification_sorting_map.h"
36 #include "notification_subscribe_info.h"
37 
38 namespace OHOS {
39 namespace Notification {
40 class NotificationLocalLiveViewSubscriberManager : public DelayedSingleton<NotificationLocalLiveViewSubscriberManager> {
41 public:
42     /**
43      * @brief Add a subscriber.
44      *
45      * @param subscriber Indicates the AnsSubscriberInterface object.
46      * @param subscribeInfo Indicates the NotificationSubscribeInfo object.
47      * @return Indicates the result code.
48      */
49     ErrCode AddLocalLiveViewSubscriber(const sptr<AnsSubscriberLocalLiveViewInterface> &subscriber,
50         const sptr<NotificationSubscribeInfo> &subscribeInfo);
51 
52     /**
53      * @brief Remove a subscriber.
54      *
55      * @param subscriber Indicates the AnsSubscriberInterface object.
56      * @param subscribeInfo Indicates the NotificationSubscribeInfo object.
57      * @return Indicates the result code.
58      */
59     ErrCode RemoveLocalLiveViewSubscriber(const sptr<AnsSubscriberLocalLiveViewInterface> &subscriber,
60         const sptr<NotificationSubscribeInfo> &subscribeInfo);
61 
62     /**
63      * @brief Notify all subscribers on canceled.
64      *
65      * @param notification Indicates the Notification object.
66      * @param buttonOption Indicates the buttonOption object.
67      */
68     void NotifyTriggerResponse(const sptr<Notification> &notification,
69         const sptr<NotificationButtonOption> &buttonOption);
70 
71     /**
72      * @brief Obtains the death event.
73      *
74      * @param object Indicates the death object.
75      */
76     void OnRemoteDied(const wptr<IRemoteObject> &object);
77 
78     /**
79      * @brief Reset ffrt queue
80      */
81     void ResetFfrtQueue();
82 
83 private:
84     struct LocalLiveViewSubscriberRecord;
85 
86     std::shared_ptr<LocalLiveViewSubscriberRecord> FindSubscriberRecord(const wptr<IRemoteObject> &object);
87     std::shared_ptr<LocalLiveViewSubscriberRecord> FindSubscriberRecord(
88         const sptr<AnsSubscriberLocalLiveViewInterface> &subscriber);
89     std::shared_ptr<LocalLiveViewSubscriberRecord> CreateSubscriberRecord(
90         const sptr<AnsSubscriberLocalLiveViewInterface> &subscriber,
91         const sptr<NotificationBundleOption> &bundleOption);
92 
93     ErrCode AddSubscriberInner(const sptr<AnsSubscriberLocalLiveViewInterface> &subscriber,
94         const sptr<NotificationBundleOption> &bundleOption);
95     ErrCode RemoveSubscriberInner(const sptr<AnsSubscriberLocalLiveViewInterface> &subscriber,
96         const sptr<NotificationSubscribeInfo> &subscribeInfo);
97 
98     void NotifyTriggerResponseInner(const sptr<Notification> &notification,
99         sptr<NotificationButtonOption> buttonOption);
100     bool IsSystemUser(int32_t userId);
101 
102 private:
103     std::list<std::shared_ptr<LocalLiveViewSubscriberRecord>> buttonRecordList_ {};
104     std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner_ {};
105     std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ {};
106     sptr<AnsSubscriberLocalLiveViewInterface> ansSubscriberProxy_ {};
107     sptr<IRemoteObject::DeathRecipient> recipient_ {};
108     std::shared_ptr<ffrt::queue> notificationButtonQueue_ = nullptr;
109 
110     DECLARE_DELAYED_SINGLETON(NotificationLocalLiveViewSubscriberManager);
111     DISALLOW_COPY_AND_MOVE(NotificationLocalLiveViewSubscriberManager);
112 };
113 }  // namespace Notification
114 }  // namespace OHOS
115 
116 #endif  // BND_NOTIFICATION_SERVICE_SERVICES_INCLUDE_NOTIFICATION_LOCAL_LIVE_VIEW_SUBSCRIBER_LOCAL_LIVE_VIEW_MANAGER_H