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 23 #include "pixel_map.h" 24 #ifndef SUPPORT_SCREEN 25 #define SUPPORT_SCREEN 26 #endif 27 #include "mission_listener_interface.h" 28 #include "ws_common.h" 29 #include "wm_common.h" 30 #include "zidl/session_lifecycle_listener_interface.h" 31 #include "task_scheduler.h" 32 33 namespace OHOS { 34 namespace Rosen { 35 using ISessionListener = AAFwk::IMissionListener; 36 class SessionListenerController : public std::enable_shared_from_this<SessionListenerController> { 37 public: 38 SessionListenerController() = default; 39 SessionListenerController(const std::shared_ptr<TaskScheduler>& taskScheduler); 40 ~SessionListenerController() = default; 41 42 WSError AddSessionListener(const sptr<ISessionListener>& listener); 43 44 void DelSessionListener(const sptr<ISessionListener>& listener); 45 46 /* 47 * Window Lifecycle 48 */ 49 void NotifySessionLifecycleEvent(ISessionLifecycleListener::SessionLifecycleEvent event, 50 const SessionInfo& sessionInfo); 51 52 WMError RegisterSessionLifecycleListener(const sptr<ISessionLifecycleListener>& listener, 53 const std::vector<int32_t>& persistentIdList); 54 55 WMError RegisterSessionLifecycleListener(const sptr<ISessionLifecycleListener>& listener, 56 const std::vector<std::string>& bundleNameList); 57 58 WMError UnregisterSessionLifecycleListener(const sptr<ISessionLifecycleListener>& listener); 59 60 void NotifySessionClosed(const SessionInfo& sessionInfo); 61 62 void NotifySessionSnapshotChanged(int32_t persistentId); 63 64 void NotifySessionMovedToFront(int32_t persistentId); 65 66 void NotifySessionFocused(int32_t persistentId); 67 68 void NotifySessionUnfocused(int32_t persistentId); 69 70 void NotifySessionIconChanged(int32_t persistentId, const std::shared_ptr<OHOS::Media::PixelMap>& icon); 71 72 void NotifySessionLabelUpdated(int32_t persistentId); 73 74 void HandleUnInstallApp(const std::list<int32_t>& sessions); 75 76 private: 77 void OnListenerDied(const wptr<IRemoteObject>& remote); 78 79 void NotifySessionCreated(int32_t persistentId); 80 81 void NotifySessionDestroyed(int32_t persistentId); 82 83 void NotifyMissionEvent(ISessionLifecycleListener::SessionLifecycleEvent event, int32_t persistentId); 84 85 template<typename F, typename... Args> 86 void CallListeners(F func, Args&&... args); 87 88 class ListenerDeathRecipient : public IRemoteObject::DeathRecipient { 89 public: 90 using ListenerDiedHandler = std::function<void(const wptr<IRemoteObject>&)>; ListenerDeathRecipient(ListenerDiedHandler handler)91 explicit ListenerDeathRecipient(ListenerDiedHandler handler) : diedHandler_(std::move(handler)) {} 92 ~ListenerDeathRecipient() = default; OnRemoteDied(const wptr<IRemoteObject> & remote)93 void OnRemoteDied(const wptr<IRemoteObject>& remote) final 94 { 95 if (diedHandler_) { 96 diedHandler_(remote); 97 } 98 } 99 100 private: 101 ListenerDiedHandler diedHandler_; 102 }; 103 104 private: 105 std::mutex listenerLock_; 106 std::vector<sptr<ISessionListener>> sessionListeners_; 107 sptr<IRemoteObject::DeathRecipient> listenerDeathRecipient_; 108 109 /* 110 * Window Lifecycle 111 */ 112 void ConstructPayload(ISessionLifecycleListener::LifecycleEventPayload& payload, const SessionInfo& sessionInfo); 113 void OnSessionLifecycleListenerDied(const wptr<IRemoteObject>& remote); 114 void RemoveSessionLifecycleListener(const sptr<IRemoteObject>& target); 115 116 template <typename KeyType, typename MapType> 117 void NotifyListeners(const MapType& listenerMap, const KeyType& key, 118 const ISessionLifecycleListener::SessionLifecycleEvent event, 119 ISessionLifecycleListener::LifecycleEventPayload& payload); 120 std::shared_ptr<TaskScheduler> taskScheduler_; 121 sptr<IRemoteObject::DeathRecipient> lifecycleListenerDeathRecipient_; 122 std::map<int32_t, std::vector<sptr<ISessionLifecycleListener>>> listenerMapById_; 123 std::map<std::string, std::vector<sptr<ISessionLifecycleListener>>> listenerMapByBundle_; 124 std::vector<sptr<ISessionLifecycleListener>> listenersOfAllBundles_; 125 }; 126 } // namespace Rosen 127 } // namespace OHOS 128 #endif // OHOS_SESSION_MANAGER_SESSION_LISTENER_CONTROLLER_H 129