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