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