1 /* 2 * Copyright (c) 2021-2022 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_MANAGER_H 17 #define OHOS_ABILITY_RUNTIME_MISSION_LIST_MANAGER_H 18 19 #include <list> 20 #include <queue> 21 #include <memory> 22 23 #include "ability_running_info.h" 24 #include "foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include/device_manager.h" 25 #include "mission_list.h" 26 #include "mission_listener_controller.h" 27 #include "mission_info.h" 28 #include "mission_snapshot.h" 29 #include "snapshot.h" 30 #include "start_options.h" 31 #include "want.h" 32 #include "iability_info_callback.h" 33 34 namespace OHOS { 35 namespace AAFwk { 36 class MissionListManager : public std::enable_shared_from_this<MissionListManager> { 37 public: 38 explicit MissionListManager(int userId); 39 ~MissionListManager(); 40 41 /** 42 * init ability mission manager. 43 * 44 */ 45 void Init(); 46 47 /** 48 * StartAbility with request. 49 * 50 * @param abilityRequest, the request of the service ability to start. 51 * @return Returns ERR_OK on success, others on failure. 52 */ 53 int StartAbility(AbilityRequest &abilityRequest); 54 55 /** 56 * MinimizeAbility, minimize the special ability. 57 * 58 * @param token, ability token. 59 * @param fromUser mark the minimize operation source. 60 * @return Returns ERR_OK on success, others on failure. 61 */ 62 int MinimizeAbility(const sptr<IRemoteObject> &token, bool fromUser); 63 64 int RegisterMissionListener(const sptr<IMissionListener> &listener); 65 66 int UnRegisterMissionListener(const sptr<IMissionListener> &listener); 67 68 int GetMissionInfos(int32_t numMax, std::vector<MissionInfo> &missionInfos); 69 70 int GetMissionInfo(int32_t missionId, MissionInfo &missionInfo); 71 72 int MoveMissionToFront(int32_t missionId, std::shared_ptr<StartOptions> startOptions = nullptr); 73 74 int MoveMissionToFront(int32_t missionId, bool isCallerFromLauncher, 75 std::shared_ptr<StartOptions> startOptions = nullptr); 76 77 /** 78 * OnAbilityRequestDone, app manager service call this interface after ability request done. 79 * 80 * @param token,ability's token. 81 * @param state,the state of ability lift cycle. 82 */ 83 void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state); 84 85 void OnAppStateChanged(const AppInfo &info); 86 87 /** 88 * attach ability thread ipc object. 89 * 90 * @param scheduler, ability thread ipc object. 91 * @param token, the token of ability. 92 * @return Returns ERR_OK on success, others on failure. 93 */ 94 int AttachAbilityThread(const sptr<AAFwk::IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token); 95 96 /** 97 * push waiting ability to queue. 98 * 99 * @param abilityRequest, the request of ability. 100 */ 101 void EnqueueWaitingAbility(const AbilityRequest &abilityRequest); 102 103 /** 104 * push front waiting ability to queue. 105 * 106 * @param abilityRequest, the request of ability. 107 */ 108 void EnqueueWaitingAbilityToFront(const AbilityRequest &abilityRequest); 109 110 /** 111 * start waiting ability. 112 */ 113 void StartWaitingAbility(); 114 115 /** 116 * @brief Get the Ability Record By Token object 117 * 118 * @param token the search token 119 * @return std::shared_ptr<AbilityRecord> the AbilityRecord of the token 120 */ 121 std::shared_ptr<AbilityRecord> GetAbilityRecordByToken(const sptr<IRemoteObject> &token) const; 122 123 /** 124 * @brief Get the Mission By Id object 125 * 126 * @param missionId the given missionId 127 * @return the mission of the given id 128 */ 129 std::shared_ptr<Mission> GetMissionById(int missionId) const; 130 131 /** 132 * @brief Terminate ability with the given abilityRecord 133 * 134 * @param abilityRecord the ability to terminate 135 * @param resultCode the terminate data 136 * @param resultWant the terminate data 137 * @param flag mark terminate flag 138 * @return int error code 139 */ 140 int TerminateAbility(const std::shared_ptr<AbilityRecord> &abilityRecord, 141 int resultCode, const Want *resultWant, bool flag); 142 143 /** 144 * @brief Terminate ability with caller 145 * 146 * @param caller the ability which start the ability 147 * @param requestCode which ability to terminate 148 * @return int error code 149 */ 150 int TerminateAbility(const std::shared_ptr<AbilityRecord> &caller, int requestCode); 151 152 /** 153 * @brief remove the mission from the mission list 154 * 155 * @param abilityRecord the ability need to remove 156 * @param flag mark is terminate or close 157 */ 158 void RemoveTerminatingAbility(const std::shared_ptr<AbilityRecord> &abilityRecord, bool flag); 159 160 /** 161 * @brief remove the mission list from the mission list manager 162 * 163 * @param MissionList the mission list need to remove 164 */ 165 void RemoveMissionList(const std::shared_ptr<MissionList> &MissionList); 166 167 /** 168 * @brief execute after the ability schedule the lifecycle 169 * 170 * @param token the ability token 171 * @param state the ability state 172 * @param saveData the saved data 173 * @return execute error code 174 */ 175 int AbilityTransactionDone(const sptr<IRemoteObject> &token, int state, const PacMap &saveData); 176 177 /** 178 * @brief search the ability from terminating list 179 * 180 * @param token the ability token 181 * @return the ability need to terminate 182 */ 183 std::shared_ptr<AbilityRecord> GetAbilityFromTerminateList(const sptr<IRemoteObject> &token); 184 185 /** 186 * @brief clear the mission with the given id 187 * 188 * @param missionId the mission need to delete 189 * @return int error code 190 */ 191 int ClearMission(int missionId); 192 193 /** 194 * @brief clear all the missions 195 * 196 * @return int error code 197 */ 198 int ClearAllMissions(); 199 200 void ClearAllMissionsLocked(std::list<std::shared_ptr<Mission>> &missionList, 201 std::list<std::shared_ptr<Mission>> &foregroundAbilities, bool searchActive); 202 203 /** 204 * @brief Set the Mission Locked State object 205 * 206 * @param missionId the id of the mission 207 * @return int error code 208 */ 209 int SetMissionLockedState(int missionId, bool lockedState); 210 211 /** 212 * @brief schedule to background 213 * 214 * @param abilityRecord the ability to move 215 */ 216 void MoveToBackgroundTask(const std::shared_ptr<AbilityRecord> &abilityRecord); 217 218 /** 219 * @brief handle time out event 220 * 221 * @param msgId the msg id in ability record 222 * @param eventId the event id in ability record 223 */ 224 void OnTimeOut(uint32_t msgId, int64_t eventId); 225 226 /** 227 * @brief handle when ability died 228 * 229 * @param abilityRecord the died ability 230 */ 231 void OnAbilityDied(std::shared_ptr<AbilityRecord> abilityRecord, int32_t currentUserId); 232 233 /** 234 * @brief handle when call connection died 235 * 236 * @param callRecord the died call connection 237 */ 238 void OnCallConnectDied(const std::shared_ptr<CallRecord> &callRecord); 239 240 /** 241 * Get mission id by target ability token. 242 * 243 * @param token target ability token. 244 * @return the missionId of target mission. 245 */ 246 int32_t GetMissionIdByAbilityToken(const sptr<IRemoteObject> &token); 247 248 /** 249 * Get ability token by target mission id. 250 * 251 * @param missionId target missionId. 252 * @return the ability token of target mission. 253 */ 254 sptr<IRemoteObject> GetAbilityTokenByMissionId(int32_t missionId); 255 256 /** 257 * @brief dump all abilities 258 * 259 * @param info dump result. 260 */ 261 void Dump(std::vector<std::string>& info); 262 263 /** 264 * @brief dump mission list 265 * 266 * @param info dump result. 267 */ 268 void DumpMissionList(std::vector<std::string> &info, bool isClient, const std::string &args = ""); 269 270 /** 271 * @brief dump mission list by id with params 272 * 273 * @param info dump result. 274 * @param params dump params. 275 */ 276 void DumpMissionListByRecordId( 277 std::vector<std::string>& info, bool isClient, int32_t abilityRecordId, const std::vector<std::string>& params); 278 279 /** 280 * @brief dump mission by id 281 * 282 * @param info dump result. 283 */ 284 void DumpMission(int missionId, std::vector<std::string> &info); 285 286 /** 287 * @brief dump mission infos 288 * 289 * @param info dump result. 290 */ 291 void DumpMissionInfos(std::vector<std::string> &info); 292 293 void OnAcceptWantResponse(const AAFwk::Want &want, const std::string &flag); 294 295 void OnStartSpecifiedAbilityTimeoutResponse(const AAFwk::Want &want); 296 /** 297 * resolve the call ipc of ability for scheduling oncall. 298 * 299 * @param abilityRequest, target ability request. 300 */ 301 int ResolveLocked(const AbilityRequest &abilityRequest); 302 303 /** 304 * release the connection of this call. 305 * 306 * @param connect, caller callback ipc. 307 * @param element, target ability name. 308 */ 309 int ReleaseCallLocked(const sptr<IAbilityConnection> &connect, const AppExecFwk::ElementName &element); 310 /** 311 * @brief register snapshotHandler 312 * @param handler the snapshotHandler 313 */ 314 void RegisterSnapshotHandler(const sptr<ISnapshotHandler>& handler); 315 316 /** 317 * @brief Get the Mission Snapshot object 318 * @param missionId mission id 319 * @param abilityToken abilityToken to get current mission snapshot 320 * @param missionSnapshot result of snapshot 321 * @param isLowResolution low resolution. 322 * @return Returns true on success, false on failure. 323 */ 324 bool GetMissionSnapshot(int32_t missionId, const sptr<IRemoteObject>& abilityToken, 325 MissionSnapshot& missionSnapshot, bool isLowResolution); 326 void GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info, bool isPerm); 327 328 /** 329 * Called to update mission snapshot. 330 * @param token The target ability. 331 */ 332 void UpdateSnapShot(const sptr<IRemoteObject>& token); 333 334 #ifdef ABILITY_COMMAND_FOR_TEST 335 /** 336 * Block ability. 337 * 338 * @param abilityRecordId The Ability Record Id. 339 * @return Returns ERR_OK on success, others on failure. 340 */ 341 int BlockAbility(int abilityRecordId); 342 #endif 343 344 void UninstallApp(const std::string &bundleName, int32_t uid); 345 346 bool IsStarted(); 347 void PauseManager(); 348 void ResumeManager(); 349 350 void SetMissionANRStateByTokens(const std::vector<sptr<IRemoteObject>> &tokens); 351 352 #ifdef SUPPORT_GRAPHICS 353 public: 354 /** 355 * Set mission label of this ability. 356 * 357 * @param abilityToken target ability token. 358 * @param label target label. 359 * @return Return 0 if success. 360 */ 361 int SetMissionLabel(const sptr<IRemoteObject> &abilityToken, const std::string &label); 362 363 /** 364 * Set mission icon of this ability. 365 * 366 * @param token target ability token. 367 * @param icon target label. 368 * @return Return 0 if success. 369 */ 370 int SetMissionIcon(const sptr<IRemoteObject> &token, const std::shared_ptr<Media::PixelMap> &icon); 371 372 void CompleteFirstFrameDrawing(const sptr<IRemoteObject> &abilityToken) const; 373 374 void PostMissionLabelUpdateTask(int missionId) const; 375 376 private: 377 Closure GetCancelStartingWindowTask(const std::shared_ptr<AbilityRecord> &abilityRecord) const; 378 void PostCancelStartingWindowTask(const std::shared_ptr<AbilityRecord> &abilityRecord) const; 379 #endif 380 381 private: 382 int StartAbilityLocked(const std::shared_ptr<AbilityRecord> ¤tTopAbility, 383 const std::shared_ptr<AbilityRecord> &callerAbility, const AbilityRequest &abilityRequest); 384 int StartAbility(const std::shared_ptr<AbilityRecord> ¤tTopAbility, 385 const std::shared_ptr<AbilityRecord> &callerAbility, const AbilityRequest &abilityRequest); 386 int MinimizeAbilityLocked(const std::shared_ptr<AbilityRecord> &abilityRecord, bool fromUser); 387 std::shared_ptr<AbilityRecord> GetCurrentTopAbilityLocked() const; 388 std::shared_ptr<MissionList> GetTargetMissionList( 389 const std::shared_ptr<AbilityRecord> &callerAbility, const AbilityRequest &abilityRequest); 390 std::shared_ptr<MissionList> GetTargetMissionListByLauncher(const AbilityRequest &abilityRequest); 391 std::shared_ptr<MissionList> GetTargetMissionListByDefault( 392 const std::shared_ptr<AbilityRecord> &callerAbility, const AbilityRequest &abilityRequest); 393 std::shared_ptr<Mission> GetReusedMission(const AbilityRequest &abilityRequest); 394 std::shared_ptr<Mission> GetReusedSpecifiedMission(const AbilityRequest &abilityRequest); 395 std::shared_ptr<Mission> GetReusedStandardMission(const AbilityRequest &abilityRequest); 396 void GetTargetMissionAndAbility(const AbilityRequest &abilityRequest, std::shared_ptr<Mission> &targetMission, 397 std::shared_ptr<AbilityRecord> &targetRecord); 398 bool HandleReusedMissionAndAbility(const AbilityRequest &abilityRequest, std::shared_ptr<Mission> &targetMission, 399 std::shared_ptr<AbilityRecord> &targetRecord); 400 std::string GetMissionName(const AbilityRequest &abilityRequest) const; 401 bool CreateOrReusedMissionInfo(const AbilityRequest &abilityRequest, InnerMissionInfo &info) const; 402 void MoveMissionToTargetList(bool isCallFromLauncher, 403 const std::shared_ptr<MissionList> &targetMissionList, 404 const std::shared_ptr<Mission> &mission); 405 void MoveMissionListToTop(const std::shared_ptr<MissionList> &missionList); 406 void MoveNoneTopMissionToDefaultList(const std::shared_ptr<Mission> &mission); 407 void PrintTimeOutLog(const std::shared_ptr<AbilityRecord> &ability, uint32_t msgId); 408 409 int DispatchState(const std::shared_ptr<AbilityRecord> &abilityRecord, int state); 410 int DispatchForeground(const std::shared_ptr<AbilityRecord> &abilityRecord, bool success, 411 AbilityState state = AbilityState::INITIAL); 412 int DispatchTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord); 413 int DispatchBackground(const std::shared_ptr<AbilityRecord> &abilityRecord); 414 void CompleteForegroundSuccess(const std::shared_ptr<AbilityRecord> &abilityRecord); 415 void CompleteTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord); 416 void DelayCompleteTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord); 417 void CompleteBackground(const std::shared_ptr<AbilityRecord> &abilityRecord); 418 void CompleteTerminateAndUpdateMission(const std::shared_ptr<AbilityRecord> &abilityRecord); 419 bool RemoveMissionList(const std::list<std::shared_ptr<MissionList>> lists, 420 const std::shared_ptr<MissionList> &list); 421 int ClearMissionLocked(int missionId, const std::shared_ptr<Mission> &mission); 422 int TerminateAbilityLocked(const std::shared_ptr<AbilityRecord> &abilityRecord, bool flag); 423 std::shared_ptr<AbilityRecord> GetAbilityRecordByEventId(int64_t eventId) const; 424 std::shared_ptr<AbilityRecord> GetAbilityRecordByCaller( 425 const std::shared_ptr<AbilityRecord> &caller, int requestCode); 426 std::shared_ptr<MissionList> GetTargetMissionList(int missionId, std::shared_ptr<Mission> &mission); 427 void PostStartWaitingAbility(); 428 void HandleAbilityDied(std::shared_ptr<AbilityRecord> abilityRecord); 429 void HandleLauncherDied(std::shared_ptr<AbilityRecord> ability); 430 void HandleAbilityDiedByDefault(std::shared_ptr<AbilityRecord> abilityRecord); 431 void DelayedStartLauncher(); 432 void BackToLauncher(); 433 void GetAllForegroundAbilities(std::list<std::shared_ptr<AbilityRecord>>& foregroundList); 434 void GetForegroundAbilities(const std::shared_ptr<MissionList>& missionList, 435 std::list<std::shared_ptr<AbilityRecord>>& foregroundList); 436 std::shared_ptr<Mission> GetMissionBySpecifiedFlag(const AAFwk::Want &want, const std::string &flag) const; 437 bool IsReachToLimitLocked(const AbilityRequest &abilityRequest); 438 439 // handle timeout event 440 void HandleLoadTimeout(const std::shared_ptr<AbilityRecord> &ability); 441 void HandleForegroundTimeout(const std::shared_ptr<AbilityRecord> &ability, 442 AbilityState state = AbilityState::INITIAL); 443 void HandleTimeoutAndResumeAbility(const std::shared_ptr<AbilityRecord> &ability, 444 AbilityState state = AbilityState::INITIAL); 445 void MoveToTerminateList(const std::shared_ptr<AbilityRecord> &ability); 446 void DelayedResumeTimeout(const std::shared_ptr<AbilityRecord> &callerAbility); 447 void BackToCaller(const std::shared_ptr<AbilityRecord> &callerAbility); 448 449 // new version for call inner function. 450 void CompleteForegroundFailed(const std::shared_ptr<AbilityRecord> &abilityRecord, AbilityState state); 451 int ResolveAbility(const std::shared_ptr<AbilityRecord> &targetAbility, const AbilityRequest &abilityRequest); 452 std::shared_ptr<AbilityRecord> GetAbilityRecordByName(const AppExecFwk::ElementName &element); 453 int CallAbilityLocked(const AbilityRequest &abilityRequest); 454 void UpdateMissionSnapshot(const std::shared_ptr<AbilityRecord> &abilityRecord) const; 455 void AddUninstallTags(const std::string &bundleName, int32_t uid); 456 void EraseWaitingAbility(const std::string &bundleName, int32_t uid); 457 void RemoveMissionLocked(int32_t missionId, bool excludeFromMissions); 458 void TerminatePreviousAbility(const std::shared_ptr<AbilityRecord> &abilityRecord); 459 void NotifyMissionCreated(const std::shared_ptr<AbilityRecord> &abilityRecord) const; 460 bool IsExcludeFromMissions(const std::shared_ptr<Mission> &mission); 461 void BuildInnerMissionInfo(InnerMissionInfo &info, const std::string &missionName, 462 const AbilityRequest &abilityRequest) const; 463 void NotifyStartSpecifiedAbility(AbilityRequest &request, const AAFwk::Want &want); 464 void NotifyRestartSpecifiedAbility(AbilityRequest &request, const sptr<IRemoteObject> &token); 465 void ProcessPreload(const std::shared_ptr<AbilityRecord> &record) const; 466 std::shared_ptr<AbilityRecord> GetAliveAbilityRecordByToken(const sptr<IRemoteObject> &token) const; 467 void NotifyAbilityToken(const sptr<IRemoteObject> &token, const AbilityRequest &abilityRequest); 468 void NotifyStartAbilityResult(const AbilityRequest &abilityRequest, int result); 469 470 int userId_; 471 mutable std::recursive_mutex managerLock_; 472 // launcher list is also in currentMissionLists_ 473 std::list<std::shared_ptr<MissionList>> currentMissionLists_; 474 // only manager the ability of standard in the default list 475 std::shared_ptr<MissionList> defaultStandardList_; 476 // only manager the ability of singleton in the default list for the fast search 477 std::shared_ptr<MissionList> defaultSingleList_; 478 std::shared_ptr<MissionList> launcherList_; 479 std::list<std::shared_ptr<AbilityRecord>> terminateAbilityList_; 480 481 std::queue<AbilityRequest> waitingAbilityQueue_; 482 std::shared_ptr<MissionListenerController> listenerController_; 483 484 class MissionDmInitCallback : public DistributedHardware::DmInitCallback { 485 public: 486 void OnRemoteDied() override; 487 488 static bool isInit_; 489 }; 490 }; 491 } // namespace AAFwk 492 } // namespace OHOS 493 #endif // OHOS_ABILITY_RUNTIME_MISSION_LIST_MANAGER_H 494