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