1 /* 2 * Copyright (c) 2023-2024 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_UI_ABILITY_LIFECYCLE_MANAGER_H 17 #define OHOS_ABILITY_RUNTIME_UI_ABILITY_LIFECYCLE_MANAGER_H 18 19 #include <list> 20 #include <map> 21 #include <memory> 22 #include <queue> 23 #include <unordered_map> 24 #include "cpp/mutex.h" 25 26 #include "ability_manager_constants.h" 27 #include "ability_record.h" 28 #include "isession_handler_interface.h" 29 30 namespace OHOS { 31 namespace AAFwk { 32 class SessionInfo; 33 class StatusBarDelegateManager; 34 struct AbilityRunningInfo; 35 struct MissionValidResult; 36 37 class UIAbilityLifecycleManager : public std::enable_shared_from_this<UIAbilityLifecycleManager> { 38 public: 39 UIAbilityLifecycleManager() = default; 40 explicit UIAbilityLifecycleManager(int32_t userId); 41 virtual ~UIAbilityLifecycleManager() = default; 42 43 struct SpecifiedInfo { 44 std::string abilityName = ""; 45 std::string bundleName = ""; 46 std::string flag = ""; 47 }; 48 struct key_compare { operatorkey_compare49 bool operator()(const SpecifiedInfo &info1, const SpecifiedInfo &info2) const 50 { 51 if (info1.abilityName < info2.abilityName || info1.bundleName < info2.bundleName || 52 info1.flag < info2.flag) { 53 return true; 54 } 55 return false; 56 } 57 }; 58 59 void SignRestartAppFlag(int32_t uid, bool isAppRecovery = false); 60 61 /** 62 * StartUIAbility with request. 63 * 64 * @param abilityRequest the request of the service ability to start. 65 * @param sessionInfo the info of scene session 66 * @param isColdStart the session info of the ability is or not cold start. 67 * @return Returns ERR_OK on success, others on failure. 68 */ 69 int StartUIAbility(AbilityRequest &abilityRequest, sptr<SessionInfo> sessionInfo, uint32_t sceneFlag, 70 bool &isColdStart); 71 72 /** 73 * @brief execute after the ability schedule the lifecycle 74 * 75 * @param token the ability token 76 * @param state the ability state 77 * @param saveData the saved data 78 * @return execute error code 79 */ 80 int AbilityTransactionDone(const sptr<IRemoteObject> &token, int state, const AppExecFwk::PacMap &saveData); 81 82 /** 83 * @brief execute after the ability schedule the lifecycle 84 * 85 * @param token the ability token 86 * @param windowConfig the windowconfig 87 * @return execute error code 88 */ 89 int AbilityWindowConfigTransactionDone( 90 const sptr<IRemoteObject> &token, const AppExecFwk::WindowConfig &windowConfig); 91 92 /** 93 * attach ability thread ipc object. 94 * 95 * @param scheduler ability thread ipc object. 96 * @param token the token of ability. 97 * @return Returns ERR_OK on success, others on failure. 98 */ 99 int AttachAbilityThread(const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token); 100 101 /** 102 * app manager service call this interface after ability request done. 103 * 104 * @param token ability's token. 105 * @param state the state of ability lift cycle. 106 */ 107 void OnAbilityRequestDone(const sptr<IRemoteObject> &token, int32_t state); 108 109 /** 110 * Check whether the UIAbility is alive. 111 * 112 * @param token ability's token. 113 * @return Returns true on alive. 114 */ 115 bool IsContainsAbility(const sptr<IRemoteObject> &token) const; 116 117 /** 118 * Notify SCB to minimize UIAbility 119 * 120 * @param token ability's token 121 */ 122 int32_t NotifySCBToMinimizeUIAbility(const sptr<IRemoteObject> token); 123 124 /** 125 * MinimizeUIAbility, minimize the special ability by scb. 126 * 127 * @param abilityRecord, the ability to minimize. 128 * @param fromUser, Whether form user. 129 * @return Returns ERR_OK on success, others on failure. 130 */ 131 int MinimizeUIAbility(const std::shared_ptr<AbilityRecord> &abilityRecord, bool fromUser, uint32_t sceneFlag); 132 133 /** 134 * GetUIAbilityRecordBySessionInfo. 135 * 136 * @param sessionToken, service ability's session token. 137 * @return Returns AbilityRecord shared_ptr. 138 */ 139 std::shared_ptr<AbilityRecord> GetUIAbilityRecordBySessionInfo(const sptr<SessionInfo> &sessionInfo); 140 141 int32_t BackToCallerAbilityWithResult(std::shared_ptr<AbilityRecord> abilityRecord, 142 int resultCode, const Want *resultWant, int64_t callerRequestCode); 143 144 /** 145 * CloseUIAbility, close the special ability by scb. 146 * 147 * @param abilityRecord, the ability to close. 148 * @param resultCode, the resultCode of the ability to terminate. 149 * @param resultWant, the Want of the ability to return. 150 * @param isClearSession Indicates whether to close UIAbility because the session is cleared. 151 * @return Returns ERR_OK on success, others on failure. 152 */ 153 int CloseUIAbility(const std::shared_ptr<AbilityRecord> &abilityRecord, 154 int resultCode, const Want *resultWant, bool isClearSession); 155 156 /** 157 * Set rootSceneSession by SCB. 158 * 159 * @param rootSceneSession Indicates root scene session of SCB. 160 */ 161 void SetRootSceneSession(const sptr<IRemoteObject> &rootSceneSession); 162 163 int NotifySCBToStartUIAbility(AbilityRequest &abilityRequest); 164 165 int NotifySCBToPreStartUIAbility(const AbilityRequest &abilityRequest, 166 sptr<SessionInfo> &sessionInfo); 167 168 int32_t NotifySCBToRecoveryAfterInterception(const AbilityRequest &abilityRequest); 169 170 /** 171 * @brief handle time out event 172 * 173 * @param msgId the msg id in ability record 174 * @param abilityRecordId the id of ability record 175 * @param isHalf is half 176 */ 177 void OnTimeOut(uint32_t msgId, int64_t abilityRecordId, bool isHalf = false); 178 179 /** 180 * @brief handle when ability died 181 * 182 * @param abilityRecord the died ability 183 */ 184 void OnAbilityDied(std::shared_ptr<AbilityRecord> abilityRecord); 185 186 /** 187 * resolve the call ipc of ability for scheduling oncall. 188 * 189 * @param abilityRequest target ability request. 190 */ 191 int ResolveLocked(const AbilityRequest &abilityRequest); 192 193 /** 194 * Call UIAbility by SCB. 195 * 196 * @param sessionInfo the session info of the ability to be called. 197 * @param isColdStart the session of the ability is or not cold start. 198 */ 199 void CallUIAbilityBySCB(const sptr<SessionInfo> &sessionInfo, bool &isColdStart); 200 201 /** 202 * OnAcceptWantResponse. 203 * 204 * @param want the want of the ability to start. 205 * @param abilityRequest the flag of the ability to start. 206 * @return Returns ERR_OK on success, others on failure. 207 */ 208 void OnAcceptWantResponse(const AAFwk::Want &want, const std::string &flag, int32_t requestId = 0); 209 210 /** 211 * OnStartSpecifiedProcessResponse. 212 * 213 * @param want the want of the ability to start. 214 * @param abilityRequest target ability request. 215 */ 216 void OnStartSpecifiedProcessResponse(const AAFwk::Want &want, const std::string &flag, int32_t requestId = 0); 217 218 /** 219 * OnStartSpecifiedAbilityTimeoutResponse. 220 * 221 * @param want the want of the ability to start. 222 */ 223 void OnStartSpecifiedAbilityTimeoutResponse(const AAFwk::Want &want, int32_t requestId = 0); 224 225 /** 226 * OnStartSpecifiedProcessTimeoutResponse. 227 * 228 * @param want the want of the ability to start. 229 */ 230 void OnStartSpecifiedProcessTimeoutResponse(const AAFwk::Want &want, int32_t requestId = 0); 231 232 /** 233 * Start specified ability by SCB. 234 * 235 * @param want Want information. 236 */ 237 void StartSpecifiedAbilityBySCB(const Want &want); 238 239 /** 240 * CallRequestDone, after invoke callRequest, ability will call this interface to return callee. 241 * 242 * @param abilityRecord ability's record. 243 * @param callStub ability's callee. 244 */ 245 void CallRequestDone(const std::shared_ptr<AbilityRecord> &abilityRecord, const sptr<IRemoteObject> &callStub); 246 247 /** 248 * release the connection of this call. 249 * 250 * @param connect caller callback ipc. 251 * @param element target ability name. 252 */ 253 int ReleaseCallLocked(const sptr<IAbilityConnection> &connect, const AppExecFwk::ElementName &element); 254 255 /** 256 * @brief handle when call connection died 257 * 258 * @param callRecord the died call connection 259 */ 260 void OnCallConnectDied(const std::shared_ptr<CallRecord> &callRecord); 261 262 /** 263 * Get sessionId by ability token. 264 * 265 * @param token the ability token. 266 * @return Returns sessionId on success, zero on failure. 267 */ 268 int32_t GetSessionIdByAbilityToken(const sptr<IRemoteObject> &token); 269 270 void GetActiveAbilityList(int32_t uid, std::vector<std::string> &abilityList, int32_t pid = NO_PID); 271 272 bool PrepareTerminateAbility(const std::shared_ptr<AbilityRecord> &abilityRecord); 273 void SetSessionHandler(const sptr<ISessionHandler> &handler); 274 275 /** 276 * Get abilityRecord by session id. 277 * 278 * @param sessionId the session id. 279 * @return Returns abilityRecord on success, nullptr on failure. 280 */ 281 std::shared_ptr<AbilityRecord> GetAbilityRecordsById(int32_t sessionId) const; 282 283 /** 284 * Get check ability number. 285 * 286 * @param bundleName record ability info bundle name. 287 * @param abilityName record ability info ability name. 288 * @param moduleName recode ability info module name. 289 * @return Return find ability number. 290 */ 291 int32_t CheckAbilityNumber( 292 const std::string &bundleName, const std::string &abilityName, const std::string &moduleName) const; 293 294 /** 295 * If ability number more then one, send event info. 296 * 297 * @param userId record ability info user id. 298 * @param bundleName record ability info bundle name. 299 * @param abilityName record ability info ability name. 300 * @param moduleName recode ability info module name. 301 */ 302 void MoreAbilityNumbersSendEventInfo( 303 int32_t userId, const std::string &bundleName, const std::string &abilityName, const std::string &moduleName); 304 305 void OnAppStateChanged(const AppInfo &info); 306 307 void UninstallApp(const std::string &bundleName, int32_t uid); 308 309 void GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info, bool isPerm) const; 310 311 /** 312 * @brief dump all abilities 313 * 314 * @param info dump result. 315 */ 316 void Dump(std::vector<std::string>& info); 317 318 /** 319 * @brief dump mission list 320 * 321 * @param info dump result. 322 */ 323 void DumpMissionList(std::vector<std::string> &info, bool isClient, const std::string &args = ""); 324 325 /** 326 * @brief dump mission list by id with params 327 * 328 * @param info dump result. 329 * @param params dump params. 330 */ 331 void DumpMissionListByRecordId(std::vector<std::string>& info, bool isClient, int32_t abilityRecordId, 332 const std::vector<std::string>& params); 333 334 int MoveMissionToFront(int32_t sessionId, std::shared_ptr<StartOptions> startOptions = nullptr); 335 336 bool IsAbilityStarted(AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> &targetRecord); 337 338 /** 339 * @brief Update session info. 340 * @param sessionInfos The vector of session info. 341 */ 342 int32_t UpdateSessionInfoBySCB(std::list<SessionInfo> &sessionInfos, std::vector<int32_t> &sessionIds); 343 344 int32_t RegisterStatusBarDelegate(sptr<AbilityRuntime::IStatusBarDelegate> delegate); 345 bool IsCallerInStatusBar(); 346 347 int32_t TryPrepareTerminateByPids(const std::vector<int32_t>& pids); 348 349 int ChangeAbilityVisibility(sptr<IRemoteObject> token, bool isShow); 350 351 int ChangeUIAbilityVisibilityBySCB(sptr<SessionInfo> sessionInfo, bool isShow); 352 353 std::vector<std::shared_ptr<AbilityRecord>> GetAbilityRecordsByName(const AppExecFwk::ElementName &element); 354 355 std::shared_ptr<AbilityRecord> GetAbilityRecordByToken(const sptr<IRemoteObject> &token) const; 356 357 #ifdef SUPPORT_GRAPHICS 358 void CompleteFirstFrameDrawing(const sptr<IRemoteObject> &token); 359 #endif 360 void CompleteFirstFrameDrawing(int32_t sessionId) const; 361 362 int32_t GetAbilityStateByPersistentId(int32_t persistentId, bool &state); 363 364 void NotifySCBToHandleAtomicServiceException(sptr<SessionInfo> sessionInfo, int32_t errorCode, 365 const std::string& errorReason); 366 367 int32_t CleanUIAbility(const std::shared_ptr<AbilityRecord> &abilityRecord); 368 369 void EnableListForSCBRecovery(); 370 371 private: 372 int32_t GetPersistentIdByAbilityRequest(const AbilityRequest &abilityRequest, bool &reuse) const; 373 int32_t GetReusedSpecifiedPersistentId(const AbilityRequest &abilityRequest, bool &reuse) const; 374 int32_t GetReusedStandardPersistentId(const AbilityRequest &abilityRequest, bool &reuse) const; 375 int32_t GetReusedCollaboratorPersistentId(const AbilityRequest &abilityRequest, bool &reuse) const; 376 std::string GenerateProcessNameForNewProcessMode(const AppExecFwk::AbilityInfo& abilityInfo); 377 void PreCreateProcessName(AbilityRequest &abilityRequest); 378 void UpdateProcessName(const AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> &abilityRecord); 379 void UpdateAbilityRecordLaunchReason(const AbilityRequest &abilityRequest, 380 std::shared_ptr<AbilityRecord> &abilityRecord) const; 381 void EraseAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord); 382 int DispatchState(const std::shared_ptr<AbilityRecord> &abilityRecord, int state); 383 int DispatchTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord); 384 int DispatchBackground(const std::shared_ptr<AbilityRecord> &abilityRecord); 385 int DispatchForeground(const std::shared_ptr<AbilityRecord> &abilityRecord, bool success, 386 AbilityState state = AbilityState::INITIAL); 387 void CompleteForegroundSuccess(const std::shared_ptr<AbilityRecord> &abilityRecord); 388 void HandleLoadTimeout(const std::shared_ptr<AbilityRecord> &ability); 389 void HandleForegroundFailed(const std::shared_ptr<AbilityRecord> &ability, 390 AbilityState state = AbilityState::INITIAL); 391 void HandleForegroundTimeout(const std::shared_ptr<AbilityRecord> &ability); 392 void NotifySCBToHandleException(const std::shared_ptr<AbilityRecord> &ability, int32_t errorCode, 393 const std::string& errorReason); 394 void MoveToBackground(const std::shared_ptr<AbilityRecord> &abilityRecord); 395 void CompleteBackground(const std::shared_ptr<AbilityRecord> &abilityRecord); 396 void PrintTimeOutLog(std::shared_ptr<AbilityRecord> ability, uint32_t msgId, bool isHalf = false); 397 void DelayCompleteTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord); 398 void CompleteTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord); 399 void CompleteTerminateLocked(const std::shared_ptr<AbilityRecord> &abilityRecord); 400 bool IsContainsAbilityInner(const sptr<IRemoteObject> &token) const; 401 bool CheckProperties(const std::shared_ptr<AbilityRecord> &abilityRecord, const AbilityRequest &abilityRequest, 402 AppExecFwk::LaunchMode launchMode) const; 403 void NotifyAbilityToken(const sptr<IRemoteObject> &token, const AbilityRequest &abilityRequest) const; 404 void PrepareCloseUIAbility(std::shared_ptr<AbilityRecord> abilityRecord, 405 int resultCode, const Want *resultWant, bool isClearSession); 406 int CloseUIAbilityInner(std::shared_ptr<AbilityRecord> abilityRecord); 407 int32_t BackToCallerAbilityWithResultLocked(sptr<SessionInfo> currentSessionInfo, 408 std::shared_ptr<AbilityRecord> callerAbilityRecord); 409 410 // byCall 411 int CallAbilityLocked(const AbilityRequest &abilityRequest); 412 sptr<SessionInfo> CreateSessionInfo(const AbilityRequest &abilityRequest) const; 413 int NotifySCBPendingActivation(sptr<SessionInfo> &sessionInfo, const AbilityRequest &abilityRequest); 414 int ResolveAbility(const std::shared_ptr<AbilityRecord> &targetAbility, const AbilityRequest &abilityRequest) const; 415 std::vector<std::shared_ptr<AbilityRecord>> GetAbilityRecordsByNameInner(const AppExecFwk::ElementName &element); 416 417 void NotifyStartSpecifiedAbility(AbilityRequest &request, const AAFwk::Want &want); 418 void NotifyRestartSpecifiedAbility(AbilityRequest &request, const sptr<IRemoteObject> &token); 419 int MoveAbilityToFront(const AbilityRequest &abilityRequest, const std::shared_ptr<AbilityRecord> &abilityRecord, 420 std::shared_ptr<AbilityRecord> callerAbility, std::shared_ptr<StartOptions> startOptions = nullptr); 421 int SendSessionInfoToSCB(std::shared_ptr<AbilityRecord> &callerAbility, sptr<SessionInfo> &sessionInfo); 422 int StartAbilityBySpecifed(const AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> &callerAbility); 423 std::shared_ptr<AbilityRecord> GetReusedSpecifiedAbility(const AAFwk::Want &want, const std::string &flag); 424 void EraseSpecifiedAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord); 425 426 void SetLastExitReason(std::shared_ptr<AbilityRecord> &abilityRecord) const; 427 void SetRevicerInfo(const AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> &abilityRecord) const; 428 429 bool CheckPrepareTerminateEnable(const std::shared_ptr<AbilityRecord> &abilityRecord); 430 bool GetContentAndTypeId(uint32_t msgId, std::string &msgContent, int &typeId) const; 431 432 bool CheckSessionInfo(sptr<SessionInfo> sessionInfo) const; 433 std::shared_ptr<AbilityRecord> CreateAbilityRecord(AbilityRequest &abilityRequest, 434 sptr<SessionInfo> sessionInfo) const; 435 void AddCallerRecord(AbilityRequest &abilityRequest, sptr<SessionInfo> sessionInfo, 436 std::shared_ptr<AbilityRecord> uiAbilityRecord) const; 437 void CheckSpecified(AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> uiAbilityRecord); 438 void SendKeyEvent(AbilityRequest &abilityRequest) const; 439 bool CheckPid(const std::shared_ptr<AbilityRecord> abilityRecord, const int32_t pid) const; 440 std::shared_ptr<StatusBarDelegateManager> GetStatusBarDelegateManager(); 441 int32_t DoProcessAttachment(std::shared_ptr<AbilityRecord> abilityRecord); 442 void BatchCloseUIAbility(const std::unordered_set<std::shared_ptr<AbilityRecord>>& abilitySet); 443 void TerminateSession(std::shared_ptr<AbilityRecord> abilityRecord); 444 int StartWithPersistentIdByDistributed(const AbilityRequest &abilityRequest, int32_t persistentId); 445 void CheckCallerFromBackground(std::shared_ptr<AbilityRecord> callerAbility, sptr<SessionInfo> &sessionInfo); 446 std::shared_ptr<AbilityRecord> GenerateAbilityRecord(AbilityRequest &abilityRequest, sptr<SessionInfo> sessionInfo, 447 bool &isColdStart); 448 449 int32_t userId_ = -1; 450 mutable ffrt::mutex sessionLock_; 451 std::unordered_map<int32_t, std::shared_ptr<AbilityRecord>> sessionAbilityMap_; 452 std::unordered_map<int64_t, std::shared_ptr<AbilityRecord>> tmpAbilityMap_; 453 std::list<std::shared_ptr<AbilityRecord>> terminateAbilityList_; 454 sptr<IRemoteObject> rootSceneSession_; 455 std::map<SpecifiedInfo, std::shared_ptr<AbilityRecord>, key_compare> specifiedAbilityMap_; 456 int32_t specifiedRequestId_ = 0; 457 std::map<int32_t, AbilityRequest> specifiedRequestMap_; 458 std::queue<SpecifiedInfo> specifiedInfoQueue_; 459 sptr<ISessionHandler> handler_; 460 ffrt::mutex statusBarDelegateManagerLock_; 461 std::shared_ptr<StatusBarDelegateManager> statusBarDelegateManager_; 462 bool isSCBRecovery_ = false; 463 std::unordered_set<int32_t> coldStartInSCBRecovery_; 464 }; 465 } // namespace AAFwk 466 } // namespace OHOS 467 #endif // OHOS_ABILITY_RUNTIME_UI_ABILITY_LIFECYCLE_MANAGER_H