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 OHOS_SESSION_MANAGER_SESSION_LISTENER_CONTROLLER_H
17 #define OHOS_SESSION_MANAGER_SESSION_LISTENER_CONTROLLER_H
18 
19 #include <mutex>
20 #include <vector>
21 #include <list>
22 #include "cpp/mutex.h"
23 
24 #include "common/include/task_scheduler.h"
25 #include "mission_listener_interface.h"
26 #include "window_manager_hilog.h"
27 #include "ws_common.h"
28 
29 namespace OHOS {
30 namespace Rosen {
31 using ISessionListener = AAFwk::IMissionListener;
32 class SessionListenerController : public std::enable_shared_from_this<SessionListenerController> {
33     public:
34     SessionListenerController();
35 
36     ~SessionListenerController();
37 
38     void Init();
39 
40     WSError AddSessionListener(const sptr<ISessionListener>& listener);
41 
42     void DelSessionListener(const sptr<ISessionListener>& listener);
43 
44     void NotifySessionCreated(int32_t persistentId);
45 
46     void NotifySessionDestroyed(int32_t persistentId);
47 
48     void NotifySessionSnapshotChanged(int32_t persistentId);
49 
50     void NotifySessionMovedToFront(int32_t persistentId);
51 
52     void NotifySessionFocused(int32_t persistentId);
53 
54     void NotifySessionUnfocused(int32_t persistentId);
55 
56     void NotifySessionIconChanged(int32_t persistentId, const std::shared_ptr<OHOS::Media::PixelMap>& icon);
57 
58     void NotifySessionClosed(int32_t persistentId);
59 
60     void NotifySessionLabelUpdated(int32_t persistentId);
61 
62     void HandleUnInstallApp(const std::list<int32_t>& sessions);
63 
64 private:
65     void OnListenerDied(const wptr<IRemoteObject>& remote);
66 
67     template<typename F, typename... Args>
68     void CallListeners(F func, Args&& ... args);
69 
70     class ListenerDeathRecipient : public IRemoteObject::DeathRecipient {
71     public:
72         using ListenerDiedHandler = std::function<void(const wptr<IRemoteObject>&)>;
73         explicit ListenerDeathRecipient(ListenerDiedHandler handler);
74         ~ListenerDeathRecipient() = default;
75         void OnRemoteDied(const wptr<IRemoteObject>& remote) final;
76 
77     private:
78         ListenerDiedHandler diedHandler_;
79     };
80 
81 private:
82     ffrt::mutex listenerLock_;
83     std::shared_ptr<TaskScheduler> taskScheduler_;
84     std::vector<sptr<ISessionListener>> sessionListeners_;
85     sptr<IRemoteObject::DeathRecipient> listenerDeathRecipient_;
86 };
87 }  // namespace Rosen
88 }  // namespace OHOS
89 #endif  // OHOS_SESSION_MANAGER_SESSION_LISTENER_CONTROLLER_H
90