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_LIST_H
17 #define OHOS_ABILITY_RUNTIME_MISSION_LIST_H
18 
19 #include <list>
20 #include <memory>
21 
22 #include "ability_manager_constants.h"
23 #include "ability_record.h"
24 #include "iremote_object.h"
25 #include "mission.h"
26 
27 using IRemoteObject = OHOS::IRemoteObject;
28 
29 namespace OHOS {
30 namespace AAFwk {
31 enum MissionListType {
32     CURRENT = 0,
33     DEFAULT_STANDARD,
34     DEFAULT_SINGLE,
35     LAUNCHER
36 };
37 
38 class MissionList : public std::enable_shared_from_this<MissionList> {
39 public:
40     explicit MissionList(MissionListType type = MissionListType::CURRENT);
MissionList(const MissionList & missionList)41     explicit MissionList(const MissionList& missionList) : type_(missionList.type_), missions_(missionList.missions_) {}
42     virtual ~MissionList();
43 
44     /**
45      * Add mission to top of this mission list.
46      *
47      * @param mission target mission
48      */
49     void AddMissionToTop(const std::shared_ptr<Mission> &mission);
50 
51     /**
52      * Remove mission from this mission list.
53      *
54      * @param mission target mission
55      */
56     void RemoveMission(const std::shared_ptr<Mission> &mission);
57 
58     /**
59      * Get singleton mission by name.
60      *
61      * @param missionName target mission name.
62      * @return founded mission.
63      */
64     std::shared_ptr<Mission> GetSingletonMissionByName(const std::string& missionName) const;
65 
66     /**
67      * Get specified mission by name and flag.
68      *
69      * @param missionName target mission name.
70      * @param flag target mission specified flag.
71      * @return founded mission.
72      */
73     std::shared_ptr<Mission> GetSpecifiedMission(const std::string& missionName, const std::string& flag) const;
74 
75     /**
76      * Get recent standard mission by name.
77      *
78      * @param missionName target mission name.
79      * @return founded mission.
80      */
81     std::shared_ptr<Mission> GetRecentStandardMission(const std::string& missionName) const;
82 
83     /**
84      * Get top mission of this mission list.
85      *
86      * @return founded mission.
87      */
88     std::shared_ptr<Mission> GetTopMission() const;
89 
90     /**
91      * @brief Get the Ability Record By Token object
92      *
93      * @param token the ability to search
94      * @return std::shared_ptr<AbilityRecord> the ability
95      */
96     std::shared_ptr<AbilityRecord> GetAbilityRecordByToken(const sptr<IRemoteObject> &token) const;
97 
98     /**
99      * @brief remove mission by ability record
100      *
101      * @param abilityRecord the ability need to remove
102      */
103     void RemoveMissionByAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord);
104 
105     /**
106      * whether the missionList contains mission.
107      *
108      * @return finded mission.
109      */
110     bool IsEmpty();
111 
112     /**
113      * @brief Get the Top Ability object
114      *
115      * @return std::shared_ptr<AbilityRecord> the top ability
116      */
117     std::shared_ptr<AbilityRecord> GetTopAbility() const;
118 
119     /**
120      * @brief Get the Mission By Id object
121      *
122      * @param missionId the given missionId
123      * @return the mission of the given id
124      */
125     std::shared_ptr<Mission> GetMissionById(int missionId) const;
126 
127     /**
128      * @brief Get the Mission By Id object
129      *
130      * @param missionId the given missionId
131      * @return the mission of the given id
132      */
133     std::list<std::shared_ptr<Mission>>& GetAllMissions();
134 
135     /**
136      * @brief Get the type of the missionList
137      *
138      * @return the mission list type
139      */
140     MissionListType GetType() const;
141 
142     /**
143      * @brief Get the launcher root
144      *
145      * @return launcher root
146      */
147     std::shared_ptr<AbilityRecord> GetLauncherRoot() const;
148 
149     /**
150      * @brief get ability record by id
151      *
152      * @param abilityRecordId ability record id
153      * @return std::shared_ptr<AbilityRecord> return ability record
154      */
155     std::shared_ptr<AbilityRecord> GetAbilityRecordById(int64_t abilityRecordId) const;
156 
157     /**
158      * @brief Get the Ability Record By Caller object
159      *
160      * @param caller the ability which call terminateAbility
161      * @param requestCode startAbilityWithRequstCode
162      * @return std::shared_ptr<AbilityRecord> the ability record which find
163      */
164     std::shared_ptr<AbilityRecord> GetAbilityRecordByCaller(
165         const std::shared_ptr<AbilityRecord> &caller, int requestCode);
166 
167     /**
168      * @brief Get the Ability Record By elementName
169      *
170      * @param element
171      * @return std::shared_ptr<AbilityRecord> the ability record which find
172      */
173     std::shared_ptr<AbilityRecord> GetAbilityRecordByName(const AppExecFwk::ElementName &element);
174 
175     /**
176      * @brief Get the Ability Records By elementName
177      *
178      * @param element conditions for search
179      * @param records out of parameter, list of returned records
180      * @return void
181      */
182     void GetAbilityRecordsByName(
183         const AppExecFwk::ElementName &element, std::vector<std::shared_ptr<AbilityRecord>> &records);
184 
185     /**
186      * Get ability token by target mission id.
187      *
188      * @param missionId target missionId.
189      * @return the ability token of target mission.
190      */
191     sptr<IRemoteObject> GetAbilityTokenByMissionId(int32_t missionId);
192 
193     /**
194      * Handle uninstall bundle.
195      *
196      * @param bundleName name of bundle.
197      * @param uid the uid of bundle.
198      */
199     void HandleUnInstallApp(const std::string &bundleName, int32_t uid);
200 
201     /**
202      * @brief dump mission
203      *
204      * @param info dump result.
205      */
206     void Dump(std::vector<std::string> &info);
207 
208     /**
209      * @brief dump mission list info
210      *
211      * @param info dump result.
212      */
213     void DumpList(std::vector<std::string> &info, bool isClient);
214 
215     void DumpStateByRecordId(
216         std::vector<std::string> &info, bool isClient, int32_t abilityRecordId, const std::vector<std::string> &params);
217 
218     std::shared_ptr<Mission> GetMissionBySpecifiedFlag(const AAFwk::Want &want, const std::string &flag) const;
219 
220     int32_t GetMissionCountByUid(int32_t targetUid) const;
221     void FindEarliestMission(std::shared_ptr<Mission>& targetMission) const;
222     int32_t GetMissionCount() const;
223     void GetActiveAbilityList(int32_t uid, std::vector<std::string> &abilityList, int32_t pid = NO_PID);
224 
225     void SignRestartAppFlag(int32_t uid);
226 
227 private:
228     std::string GetTypeName();
229     bool MatchedInitialMission(const std::shared_ptr<Mission>& mission, const std::string &bundleName, int32_t uid);
230 
231     MissionListType type_;
232     std::list<std::shared_ptr<Mission>> missions_ {};
233 };
234 }  // namespace AAFwk
235 }  // namespace OHOS
236 #endif  // OHOS_ABILITY_RUNTIME_MISSION_LIST_H
237