1 /*
2  * Copyright (c) 2021-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 OHOS_ABILITY_RUNTIME_MISSION_LISTENER_CONTROLLER_H
17 #define OHOS_ABILITY_RUNTIME_MISSION_LISTENER_CONTROLLER_H
18 
19 #include <mutex>
20 #include <vector>
21 #include <list>
22 #include "cpp/mutex.h"
23 
24 #include "task_handler_wrap.h"
25 #include "mission_listener_interface.h"
26 
27 namespace OHOS {
28 namespace AAFwk {
29 /**
30  * @class MissionListenerController
31  * MissionListenerController manage mission listeners.
32  */
33 class MissionListenerController : public std::enable_shared_from_this<MissionListenerController> {
34 public:
35     MissionListenerController();
36     ~MissionListenerController();
37 
38     /**
39      * init mission listener controller.
40      *
41      */
42     void Init();
43 
44     /**
45      * add mission listener callback.
46      *
47      * @param listener the listener callback.
48      * @return Returns ERR_OK on success, others on failure.
49      */
50     int AddMissionListener(const sptr<IMissionListener> &listener);
51 
52     /**
53      * delete a mission listener callback.
54      *
55      * @param listener the listener callback.
56      */
57     void DelMissionListener(const sptr<IMissionListener> &listener);
58 
59     /**
60      * notify listeners that mission was created.
61      *
62      * @param missionId target mission id.
63      */
64     void NotifyMissionCreated(int32_t missionId);
65 
66     /**
67      * notify listeners that mission was created.
68      *
69      * @param missionId target mission id.
70      */
71     void NotifyMissionDestroyed(int32_t missionId);
72 
73     /**
74      * notify listeners that mission was created.
75      *
76      * @param missionId target mission id.
77      */
78     void NotifyMissionSnapshotChanged(int32_t missionId);
79 
80     /**
81      * notify listeners that mission was created.
82      *
83      * @param missionId target mission id.
84      */
85     void NotifyMissionMovedToFront(int32_t missionId);
86 
87     /**
88      * notify listeners that mission was focused.
89      *
90      * @param missionId target mission id.
91      */
92     void NotifyMissionFocused(int32_t missionId);
93 
94     /**
95      * notify listeners that mission was unfocused.
96      *
97      * @param missionId target mission id.
98      */
99     void NotifyMissionUnfocused(int32_t missionId);
100 
101 #ifdef SUPPORT_GRAPHICS
102     /**
103      * notify listeners that mission icon has changed.
104      *
105      * @param missionId target mission id.
106      * @param icon mission icon.
107      */
108     void NotifyMissionIconChanged(int32_t missionId, const std::shared_ptr<OHOS::Media::PixelMap> &icon);
109 #endif
110 
111     /**
112      * notify listeners that mission was closed.
113      *
114      * @param missionId target mission id.
115      */
116     void NotifyMissionClosed(int32_t missionId);
117 
118     /**
119      * notify listeners that mission label was updated.
120      *
121      * @param missionId target mission id.
122      */
123     void NotifyMissionLabelUpdated(int32_t missionId);
124 
125     void HandleUnInstallApp(const std::list<int32_t> &missions);
126 
127 private:
128     void OnListenerDied(const wptr<IRemoteObject> &remote);
129 
130     template<typename F, typename... Args>
CallListeners(F func,Args &&...args)131     void CallListeners(F func, Args&&... args)
132     {
133         std::lock_guard<ffrt::mutex> guard(listenerLock_);
134         for (auto listener : missionListeners_) {
135             if (listener) {
136                 (listener->*func)(std::forward<Args>(args)...);
137             }
138         }
139     }
140 
141     class ListenerDeathRecipient : public IRemoteObject::DeathRecipient {
142     public:
143         using ListenerDiedHandler = std::function<void(const wptr<IRemoteObject> &)>;
144         explicit ListenerDeathRecipient(ListenerDiedHandler handler);
145         ~ListenerDeathRecipient() = default;
146         void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
147 
148     private:
149         ListenerDiedHandler diedHandler_;
150     };
151 
152 private:
153     ffrt::mutex listenerLock_;
154     std::shared_ptr<TaskHandlerWrap> handler_;
155     std::vector<sptr<IMissionListener>> missionListeners_;
156     sptr<IRemoteObject::DeathRecipient> listenerDeathRecipient_;
157 };
158 }  // namespace AAFwk
159 }  // namespace OHOS
160 #endif  // OHOS_ABILITY_RUNTIME_MISSION_LISTENER_CONTROLLER_H
161