• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 missionAffinity uni-key of mission from broker.
103      * @param flag name of specified mission flag.
104      * @param isFindRecentStandard find recent standard mission.
105      * @param isFromCollaborator request from collaborator.
106      * @param info found mission info.
107      * @return true if success.
108      */
109     bool FindReusedMissionInfo(const std::string &missionName, const std::string &missionAffinity,
110         const std::string &flag, bool isFindRecentStandard, bool isFromCollaborator, InnerMissionInfo &info);
111 
112     /**
113      * @brief Delete all the mission info.
114      *
115      * @param listenerController The mission listener controller.
116      */
117     bool DeleteAllMissionInfos(const std::shared_ptr<MissionListenerController> &listenerController);
118 
119     /**
120      * @brief Update mission continue state.
121      *
122      * @param missionId indicates this mission id.
123      * @param state indicates this mission label.
124      * @return 0 if success.
125      */
126     int UpdateMissionContinueState(int32_t missionId, const AAFwk::ContinueState &state);
127 
128     /**
129      * @brief Update mission label.
130      *
131      * @param missionId indicates this mission id.
132      * @param label indicates this mission label.
133      * @return 0 if success.
134      */
135     int UpdateMissionLabel(int32_t missionId, const std::string& label);
136 
137     /**
138      * @brief Set mission abilityState.
139      *
140      * @param missionId indicates this mission id.
141      * @param abilityState indicates this mission abilityState.
142      * @return 0 if success.
143      */
144     void SetMissionAbilityState(int32_t missionId, AbilityState state);
145 
146     /**
147      * @brief dump mission info
148      *
149      * @param info dump result.
150      */
151     void Dump(std::vector<std::string> &info);
152 
153     /**
154      * @brief update mission snapshot
155      * @param missionId mission id
156      * @param abilityToken abilityToken to get current mission snapshot
157      * @param missionSnapshot result of snapshot
158      * @param isLowResolution low resolution snapshot.
159      * @return return true if update mission snapshot success, else false
160      */
161     bool UpdateMissionSnapshot(int32_t missionId, const sptr<IRemoteObject>& abilityToken,
162         MissionSnapshot& missionSnapshot, bool isLowResolution = false);
163 
164     /**
165      * @brief update mission snapshot
166      * @param missionId mission id
167      * @param pixelMap The snapshot.
168      * @param isPrivate Indicates whether the window is private window.
169      */
170     void UpdateMissionSnapshot(int32_t missionId, const std::shared_ptr<Media::PixelMap> &pixelMap, bool isPrivate);
171 
172 #ifdef SUPPORT_GRAPHICS
173     /**
174      * @brief Get the Snapshot object
175      * @param missionId Indicates this mission id.
176      * @return Returns PixelMap of snapshot.
177      */
178     std::shared_ptr<Media::PixelMap> GetSnapshot(int32_t missionId) const;
179 #endif
180 
181     /**
182      * @brief get the mission snapshot object
183      * @param missionId mission id
184      * @param abilityToken abilityToken to get current mission snapshot
185      * @param missionSnapshot result of snapshot
186      # @param isLowResolution low resolution.
187      * @param force force get snapshot from window manager service.
188      * @return true return true if get mission snapshot success, else false
189      */
190     bool GetMissionSnapshot(int32_t missionId, const sptr<IRemoteObject>& abilityToken,
191         MissionSnapshot& missionSnapshot, bool isLowResolution, bool force = false);
192 
193     /**
194      * @brief register snapshotHandler
195      * @param handler the snapshotHandler
196      */
197     void RegisterSnapshotHandler(const sptr<ISnapshotHandler>& handler);
198 
199     void HandleUnInstallApp(const std::string &bundleName, int32_t uid, std::list<int32_t> &missions);
200 
201     void CompleteSaveSnapshot(int32_t missionId);
202 private:
203     /**
204      * @brief Boot query mission info.
205      * @return Returns true if this function is successfully called; returns false otherwise.
206      */
207     bool LoadAllMissionInfo();
208     bool AddMissionInfoInner(const InnerMissionInfo &missionInfo);
209     void GetMatchedMission(const std::string &bundleName, int32_t uid, std::list<int32_t> &missions);
210 #ifdef SUPPORT_GRAPHICS
211     void CreateWhitePixelMap(Snapshot &snapshot) const;
212 #endif
213 
214 private:
215     int32_t currentMissionId_ = MIN_MISSION_ID;
216     std::unordered_map<int32_t, bool> missionIdMap_; // key:distributed missionid, vaule: has been saved
217     std::list<InnerMissionInfo> missionInfoList_;
218     std::shared_ptr<TaskDataPersistenceMgr> taskDataPersistenceMgr_;
219     sptr<ISnapshotHandler> snapshotHandler_;
220     mutable ffrt::mutex mutex_;
221     std::unordered_map<int32_t, uint32_t> savingSnapshot_;
222     ffrt::mutex savingSnapshotLock_;
223     ffrt::condition_variable waitSavingCondition_;
224 };
225 }  // namespace AAFwk
226 }  // namespace OHOS
227 #endif  // OHOS_ABILITY_RUNTIME_MISSION_INFO_MGR_H
228