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 void NotifySessionTransferToTargetScreenEvent(const SessionInfo& sessionInfo, 53 const uint32_t resultCode, const uint64_t fromScreenId, const uint64_t toScreenId); 54 55 WMError RegisterSessionLifecycleListener(const sptr<ISessionLifecycleListener>& listener, 56 const std::vector<int32_t>& persistentIdList); 57 58 WMError RegisterSessionLifecycleListener(const sptr<ISessionLifecycleListener>& listener, 59 const std::vector<std::string>& bundleNameList); 60 61 WMError UnregisterSessionLifecycleListener(const sptr<ISessionLifecycleListener>& listener); 62 63 bool IsListenerMapByIdSizeReachLimit() const; 64 65 bool IsListenerMapByBundleSizeReachLimit(bool isBundleNameListEmpty) const; 66 67 void NotifySessionClosed(const SessionInfo& sessionInfo); 68 69 void NotifySessionSnapshotChanged(int32_t persistentId); 70 71 void NotifySessionMovedToFront(int32_t persistentId); 72 73 void NotifySessionFocused(int32_t persistentId); 74 75 void NotifySessionUnfocused(int32_t persistentId); 76 77 void NotifySessionIconChanged(int32_t persistentId, const std::shared_ptr<OHOS::Media::PixelMap>& icon); 78 79 void NotifySessionLabelUpdated(int32_t persistentId); 80 81 void HandleUnInstallApp(const std::list<int32_t>& sessions); 82 83 private: 84 void OnListenerDied(const wptr<IRemoteObject>& remote); 85 86 void NotifySessionCreated(int32_t persistentId); 87 88 void NotifySessionDestroyed(int32_t persistentId); 89 90 void NotifySessionBackground(int32_t persistentId); 91 92 void NotifyMissionEvent(ISessionLifecycleListener::SessionLifecycleEvent event, int32_t persistentId); 93 94 template<typename F, typename... Args> 95 void CallListeners(F func, Args&&... args); 96 97 class ListenerDeathRecipient : public IRemoteObject::DeathRecipient { 98 public: 99 using ListenerDiedHandler = std::function<void(const wptr<IRemoteObject>&)>; ListenerDeathRecipient(ListenerDiedHandler handler)100 explicit ListenerDeathRecipient(ListenerDiedHandler handler) : diedHandler_(std::move(handler)) {} 101 ~ListenerDeathRecipient() = default; OnRemoteDied(const wptr<IRemoteObject> & remote)102 void OnRemoteDied(const wptr<IRemoteObject>& remote) final 103 { 104 if (diedHandler_) { 105 diedHandler_(remote); 106 } 107 } 108 109 private: 110 ListenerDiedHandler diedHandler_; 111 }; 112 113 private: 114 std::mutex listenerLock_; 115 std::vector<sptr<ISessionListener>> sessionListeners_; 116 sptr<IRemoteObject::DeathRecipient> listenerDeathRecipient_; 117 118 /* 119 * Window Lifecycle 120 */ 121 void ConstructPayload(ISessionLifecycleListener::LifecycleEventPayload& payload, const SessionInfo& sessionInfo, 122 const uint32_t resultCode = 0, const uint64_t fromScreenId = 0, const uint64_t toScreenId = 0); 123 void OnSessionLifecycleListenerDied(const wptr<IRemoteObject>& remote); 124 void RemoveSessionLifecycleListener(const sptr<IRemoteObject>& target); 125 126 template <typename KeyType, typename MapType> 127 void NotifyListeners(const MapType& listenerMap, const KeyType& key, 128 const ISessionLifecycleListener::SessionLifecycleEvent event, 129 const ISessionLifecycleListener::LifecycleEventPayload& payload); 130 std::shared_ptr<TaskScheduler> taskScheduler_; 131 sptr<IRemoteObject::DeathRecipient> lifecycleListenerDeathRecipient_; 132 std::map<int32_t, std::vector<sptr<ISessionLifecycleListener>>> listenerMapById_; 133 std::map<std::string, std::vector<sptr<ISessionLifecycleListener>>> listenerMapByBundle_; 134 std::vector<sptr<ISessionLifecycleListener>> listenersOfAllBundles_; 135 }; 136 } // namespace Rosen 137 } // namespace OHOS 138 #endif // OHOS_SESSION_MANAGER_SESSION_LISTENER_CONTROLLER_H 139