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_INFO_MGR_H 17 #define OHOS_ABILITY_RUNTIME_MISSION_INFO_MGR_H 18 19 #include <condition_variable> 20 #include <list> 21 #include <mutex> 22 #include <string> 23 24 #include "inner_mission_info.h" 25 #include "mission_listener_controller.h" 26 #include "mission_snapshot.h" 27 #include "snapshot.h" 28 #include "task_data_persistence_mgr.h" 29 30 namespace OHOS { 31 namespace AAFwk { 32 const int MIN_MISSION_ID = 1; 33 const int MAX_MISSION_ID = INT_MAX; 34 35 class MissionInfoMgr : public std::enable_shared_from_this<MissionInfoMgr> { 36 DECLARE_DELAYED_SINGLETON(MissionInfoMgr) 37 public: 38 /** 39 * @brief generate mission id of mission info object. 40 * @param missionId Indicates the missionInfo object of user to operate. 41 * @return Returns true if the missionId is successfully generated; returns false otherwise. 42 */ 43 bool GenerateMissionId(int32_t &missionId); 44 45 /** 46 * @brief initialization of mission info manager. 47 * @param userId Indicates the missionInfo object of user to operate. 48 * @return Returns true if init successfully, returns false otherwise. 49 */ 50 bool Init(int userId); 51 52 /** 53 * @brief Add the mission info. 54 * @param missionInfo Indicates the missionInfo object to be Added. 55 * @return Returns true if the data is successfully saved; returns false otherwise. 56 */ 57 bool AddMissionInfo(const InnerMissionInfo &missionInfo); 58 59 /** 60 * @brief Update the mission info. 61 * @param missionInfo Indicates the missionInfo object to be updated. 62 * @return Returns true if the data is successfully saved; returns false otherwise. 63 */ 64 bool UpdateMissionInfo(const InnerMissionInfo &missionInfo); 65 66 /** 67 * @brief Delete the mission info corresponding to the mission Id. 68 * @param missionId Indicates this mission id. 69 * @return Returns true if the data is successfully deleted; returns false otherwise. 70 */ 71 bool DeleteMissionInfo(int missionId); 72 73 /** 74 * @brief Get all mission infos,sorted by time stamp. 75 * @param numMax max num of missions. 76 * @return ERR_OK if get success. 77 */ 78 int GetMissionInfos(int32_t numMax, std::vector<MissionInfo> &missionInfos); 79 80 /** 81 * @brief Get mission info by mission id. 82 * @param missionId indicates this mission id. 83 * @param missionInfo indicates the missionInfo object related to the missionId. 84 * @return ERR_OK if get success; return else otherwise. 85 */ 86 int GetMissionInfoById(int32_t missionId, MissionInfo &missionInfo); 87 88 /** 89 * @brief Get inner mission info by mission id. 90 * @param missionId indicates this mission id. 91 * @param innerMissionInfo indicates the inner missionInfo object related to the missionId. 92 * @return ERR_OK if get success; return else otherwise. 93 */ 94 int GetInnerMissionInfoById(int32_t missionId, InnerMissionInfo &innerMissionInfo); 95 96 /** 97 * @brief Try find reused mission info. 98 * 99 * @param missionName name of mission. 100 * @param flag name of specified mission flag. 101 * @param isFindRecentStandard find recent standard mission. 102 * @param info found mission info. 103 * @return true if success. 104 */ 105 bool FindReusedMissionInfo(const std::string &missionName, const std::string &flag, 106 bool isFindRecentStandard, InnerMissionInfo &info); 107 108 /** 109 * @brief Delete all the mission info. 110 * 111 * @param listenerController The mission listener controller. 112 */ 113 bool DeleteAllMissionInfos(const std::shared_ptr<MissionListenerController> &listenerController); 114 115 /** 116 * @brief Update mission label. 117 * 118 * @param missionId indicates this mission id. 119 * @param label indicates this mission label. 120 * @return 0 if success. 121 */ 122 int UpdateMissionLabel(int32_t missionId, const std::string& label); 123 124 /** 125 * @brief dump mission info 126 * 127 * @param info dump result. 128 */ 129 void Dump(std::vector<std::string> &info); 130 131 /** 132 * @brief update mission snapshot 133 * @param missionId mission id 134 * @param abilityToken abilityToken to get current mission snapshot 135 * @param missionSnapshot result of snapshot 136 * @param isLowResolution low resolution snapshot. 137 * @return return true if update mission snapshot success, else false 138 */ 139 bool UpdateMissionSnapshot(int32_t missionId, const sptr<IRemoteObject>& abilityToken, 140 MissionSnapshot& missionSnapshot, bool isLowResolution = false); 141 142 #ifdef SUPPORT_GRAPHICS 143 /** 144 * @brief Get the Snapshot object 145 * @param missionId Indicates this mission id. 146 * @return Returns PixelMap of snapshot. 147 */ 148 std::shared_ptr<Media::PixelMap> GetSnapshot(int32_t missionId) const; 149 #endif 150 151 /** 152 * @brief get the mission snapshot object 153 * @param missionId mission id 154 * @param abilityToken abilityToken to get current mission snapshot 155 * @param missionSnapshot result of snapshot 156 # @param isLowResolution low resolution. 157 * @param force force get snapshot from window manager service. 158 * @return true return true if get mission snapshot success, else false 159 */ 160 bool GetMissionSnapshot(int32_t missionId, const sptr<IRemoteObject>& abilityToken, 161 MissionSnapshot& missionSnapshot, bool isLowResolution, bool force = false); 162 163 /** 164 * @brief register snapshotHandler 165 * @param handler the snapshotHandler 166 */ 167 void RegisterSnapshotHandler(const sptr<ISnapshotHandler>& handler); 168 169 void HandleUnInstallApp(const std::string &bundleName, int32_t uid, std::list<int32_t> &missions); 170 171 void CompleteSaveSnapshot(int32_t missionId); 172 private: 173 /** 174 * @brief Boot query mission info. 175 * @return Returns true if this function is successfully called; returns false otherwise. 176 */ 177 bool LoadAllMissionInfo(); 178 179 void GetMatchedMission(const std::string &bundleName, int32_t uid, std::list<int32_t> &missions); 180 #ifdef SUPPORT_GRAPHICS 181 void CreateWhitePixelMap(Snapshot &snapshot) const; 182 #endif 183 184 private: 185 int32_t currentMissionId_ = MIN_MISSION_ID; 186 std::unordered_map<int32_t, bool> missionIdMap_; // key:distributed missionid, vaule: has been saved 187 std::list<InnerMissionInfo> missionInfoList_; 188 std::shared_ptr<TaskDataPersistenceMgr> taskDataPersistenceMgr_; 189 sptr<ISnapshotHandler> snapshotHandler_; 190 mutable std::recursive_mutex mutex_; 191 std::unordered_map<int32_t, uint32_t> savingSnapshot_; 192 std::mutex savingSnapshotLock_; 193 std::condition_variable waitSavingCondition_; 194 }; 195 } // namespace AAFwk 196 } // namespace OHOS 197 #endif // OHOS_ABILITY_RUNTIME_MISSION_INFO_MGR_H 198