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