1 /* 2 * Copyright (c) 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_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_record.h" 27 #include "isession_handler_interface.h" 28 #include "session/host/include/zidl/session_interface.h" 29 30 namespace OHOS { 31 namespace AAFwk { 32 class SessionInfo; 33 struct AbilityRunningInfo; 34 struct MissionValidResult; 35 36 class UIAbilityLifecycleManager : public std::enable_shared_from_this<UIAbilityLifecycleManager> { 37 public: 38 UIAbilityLifecycleManager() = default; 39 virtual ~UIAbilityLifecycleManager() = default; 40 41 struct SpecifiedInfo { 42 std::string abilityName = ""; 43 std::string bundleName = ""; 44 std::string flag = ""; 45 }; 46 struct key_compare { operatorkey_compare47 bool operator()(const SpecifiedInfo &info1, const SpecifiedInfo &info2) const 48 { 49 if (info1.abilityName < info2.abilityName || info1.bundleName < info2.bundleName || 50 info1.flag < info2.flag) { 51 return true; 52 } 53 return false; 54 } 55 }; 56 57 /** 58 * StartUIAbility with request. 59 * 60 * @param abilityRequest the request of the service ability to start. 61 * @param sessionInfo the info of scene session 62 * @return Returns ERR_OK on success, others on failure. 63 */ 64 int StartUIAbility(AbilityRequest &abilityRequest, sptr<SessionInfo> sessionInfo); 65 66 /** 67 * @brief execute after the ability schedule the lifecycle 68 * 69 * @param token the ability token 70 * @param state the ability state 71 * @param saveData the saved data 72 * @return execute error code 73 */ 74 int AbilityTransactionDone(const sptr<IRemoteObject> &token, int state, const AppExecFwk::PacMap &saveData); 75 76 /** 77 * attach ability thread ipc object. 78 * 79 * @param scheduler ability thread ipc object. 80 * @param token the token of ability. 81 * @return Returns ERR_OK on success, others on failure. 82 */ 83 int AttachAbilityThread(const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token); 84 85 /** 86 * app manager service call this interface after ability request done. 87 * 88 * @param token ability's token. 89 * @param state the state of ability lift cycle. 90 */ 91 void OnAbilityRequestDone(const sptr<IRemoteObject> &token, int32_t state) const; 92 93 /** 94 * Check whether the UIAbility is alive. 95 * 96 * @param token ability's token. 97 * @return Returns true on alive. 98 */ 99 bool IsContainsAbility(const sptr<IRemoteObject> &token) const; 100 101 /** 102 * MinimizeUIAbility, minimize the special ability by scb. 103 * 104 * @param abilityRecord, the ability to minimize. 105 * @param fromUser, Whether form user. 106 * @return Returns ERR_OK on success, others on failure. 107 */ 108 int MinimizeUIAbility(const std::shared_ptr<AbilityRecord> &abilityRecord, bool fromUser = false); 109 110 /** 111 * GetUIAbilityRecordBySessionInfo. 112 * 113 * @param sessionToken, service ability's session token. 114 * @return Returns AbilityRecord shared_ptr. 115 */ 116 std::shared_ptr<AbilityRecord> GetUIAbilityRecordBySessionInfo(const sptr<SessionInfo> &sessionInfo); 117 118 /** 119 * CloseUIAbility, close the special ability by scb. 120 * 121 * @param abilityRecord, the ability to close. 122 * @param resultCode, the resultCode of the ability to terminate. 123 * @param resultWant, the Want of the ability to return. 124 * @param isClearSession Indicates whether to close UIAbility because the session is cleared. 125 * @return Returns ERR_OK on success, others on failure. 126 */ 127 int CloseUIAbility(const std::shared_ptr<AbilityRecord> &abilityRecord, 128 int resultCode, const Want *resultWant, bool isClearSession); 129 130 /** 131 * Set rootSceneSession by SCB. 132 * 133 * @param rootSceneSession Indicates root scene session of SCB. 134 */ 135 void SetRootSceneSession(const sptr<IRemoteObject> &rootSceneSession); 136 137 int NotifySCBToStartUIAbility(const AbilityRequest &abilityRequest, int32_t userId); 138 139 /** 140 * @brief handle time out event 141 * 142 * @param msgId the msg id in ability record 143 * @param abilityRecordId the id of ability record 144 * @param isHalf is half 145 */ 146 void OnTimeOut(uint32_t msgId, int64_t abilityRecordId, bool isHalf = false); 147 148 /** 149 * @brief handle when ability died 150 * 151 * @param abilityRecord the died ability 152 */ 153 void OnAbilityDied(std::shared_ptr<AbilityRecord> abilityRecord); 154 155 /** 156 * resolve the call ipc of ability for scheduling oncall. 157 * 158 * @param abilityRequest target ability request. 159 */ 160 int ResolveLocked(const AbilityRequest &abilityRequest, int32_t userId); 161 162 /** 163 * Call UIAbility by SCB. 164 * 165 * @param sessionInfo the session info of the ability to be called. 166 */ 167 void CallUIAbilityBySCB(const sptr<SessionInfo> &sessionInfo); 168 169 /** 170 * OnAcceptWantResponse. 171 * 172 * @param want the want of the ability to start. 173 * @param abilityRequest the flag of the ability to start. 174 * @return Returns ERR_OK on success, others on failure. 175 */ 176 void OnAcceptWantResponse(const AAFwk::Want &want, const std::string &flag); 177 178 /** 179 * OnStartSpecifiedProcessResponse. 180 * 181 * @param want the want of the ability to start. 182 * @param abilityRequest target ability request. 183 */ 184 void OnStartSpecifiedProcessResponse(const AAFwk::Want &want, const std::string &flag); 185 186 /** 187 * OnStartSpecifiedAbilityTimeoutResponse. 188 * 189 * @param want the want of the ability to start. 190 */ 191 void OnStartSpecifiedAbilityTimeoutResponse(const AAFwk::Want &want); 192 193 /** 194 * OnStartSpecifiedProcessTimeoutResponse. 195 * 196 * @param want the want of the ability to start. 197 */ 198 void OnStartSpecifiedProcessTimeoutResponse(const AAFwk::Want &want); 199 200 /** 201 * Start specified ability by SCB. 202 * 203 * @param want Want information. 204 * @param userId Use id. 205 */ 206 void StartSpecifiedAbilityBySCB(const Want &want, int32_t userId); 207 208 /** 209 * CallRequestDone, after invoke callRequest, ability will call this interface to return callee. 210 * 211 * @param abilityRecord ability's record. 212 * @param callStub ability's callee. 213 */ 214 void CallRequestDone(const std::shared_ptr<AbilityRecord> &abilityRecord, const sptr<IRemoteObject> &callStub); 215 216 /** 217 * release the connection of this call. 218 * 219 * @param connect caller callback ipc. 220 * @param element target ability name. 221 */ 222 int ReleaseCallLocked(const sptr<IAbilityConnection> &connect, const AppExecFwk::ElementName &element); 223 224 /** 225 * @brief handle when call connection died 226 * 227 * @param callRecord the died call connection 228 */ 229 void OnCallConnectDied(const std::shared_ptr<CallRecord> &callRecord); 230 231 /** 232 * Get sessionId by ability token. 233 * 234 * @param token the ability token. 235 * @return Returns sessionId on success, zero on failure. 236 */ 237 int32_t GetSessionIdByAbilityToken(const sptr<IRemoteObject> &token); 238 239 void GetActiveAbilityList(const std::string &bundleName, std::vector<std::string> &abilityList); 240 241 bool PrepareTerminateAbility(const std::shared_ptr<AbilityRecord> &abilityRecord); 242 void SetSessionHandler(const sptr<ISessionHandler> &handler); 243 244 /** 245 * Get abilityRecord by session id. 246 * 247 * @param sessionId the session id. 248 * @return Returns abilityRecord on success, nullptr on failure. 249 */ 250 std::shared_ptr<AbilityRecord> GetAbilityRecordsById(int32_t sessionId) const; 251 252 void GetActiveAbilityList(const std::string &bundleName, std::vector<std::string> &abilityList, 253 int32_t targetUserId) const; 254 255 void OnAppStateChanged(const AppInfo &info, int32_t targetUserId); 256 257 void UninstallApp(const std::string &bundleName, int32_t uid, int32_t targetUserId); 258 259 void GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info, bool isPerm, int32_t userId) const; 260 261 #ifdef ABILITY_COMMAND_FOR_TEST 262 /** 263 * Block ability. 264 * 265 * @param abilityRecordId The Ability Record Id. 266 * @return Returns ERR_OK on success, others on failure. 267 */ 268 int BlockAbility(int abilityRecordId, int32_t targetUserId) const; 269 #endif 270 271 /** 272 * @brief dump all abilities 273 * 274 * @param info dump result. 275 */ 276 void Dump(std::vector<std::string>& info); 277 278 /** 279 * @brief dump mission list 280 * 281 * @param info dump result. 282 */ 283 void DumpMissionList(std::vector<std::string> &info, bool isClient, int userId, const std::string &args = ""); 284 285 /** 286 * @brief dump mission list by id with params 287 * 288 * @param info dump result. 289 * @param params dump params. 290 */ 291 void DumpMissionListByRecordId(std::vector<std::string>& info, bool isClient, int32_t abilityRecordId, 292 const std::vector<std::string>& params, int userId); 293 294 int MoveMissionToFront(int32_t sessionId, std::shared_ptr<StartOptions> startOptions = nullptr); 295 296 void SetDevice(std::string deviceType); 297 298 bool IsAbilityStarted(AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> &targetRecord, 299 const int32_t oriValidUserId); 300 301 /** 302 * @brief Update session info. 303 * @param sessionInfos The vector of session info. 304 */ 305 void UpdateSessionInfoBySCB(const std::vector<SessionInfo> &sessionInfos, int32_t userId); 306 307 private: 308 std::shared_ptr<AbilityRecord> GetAbilityRecordByToken(const sptr<IRemoteObject> &token) const; 309 int32_t GetPersistentIdByAbilityRequest(const AbilityRequest &abilityRequest, bool &reuse, int32_t userId) const; 310 int32_t GetReusedSpecifiedPersistentId(const AbilityRequest &abilityRequest, bool &reuse, int32_t userId) const; 311 int32_t GetReusedStandardPersistentId(const AbilityRequest &abilityRequest, bool &reuse, int32_t userId) const; 312 int32_t GetReusedCollaboratorPersistentId(const AbilityRequest &abilityRequest, bool &reuse) const; 313 void UpdateAbilityRecordLaunchReason(const AbilityRequest &abilityRequest, 314 std::shared_ptr<AbilityRecord> &abilityRecord) const; 315 void EraseAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord); 316 int DispatchState(const std::shared_ptr<AbilityRecord> &abilityRecord, int state); 317 int DispatchTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord); 318 int DispatchBackground(const std::shared_ptr<AbilityRecord> &abilityRecord); 319 int DispatchForeground(const std::shared_ptr<AbilityRecord> &abilityRecord, bool success, 320 AbilityState state = AbilityState::INITIAL); 321 void CompleteForegroundSuccess(const std::shared_ptr<AbilityRecord> &abilityRecord); 322 void HandleLoadTimeout(const std::shared_ptr<AbilityRecord> &ability); 323 void HandleForegroundFailed(const std::shared_ptr<AbilityRecord> &ability, 324 AbilityState state = AbilityState::INITIAL); 325 void HandleForegroundTimeout(const std::shared_ptr<AbilityRecord> &ability); 326 void NotifySCBToHandleException(const std::shared_ptr<AbilityRecord> &ability, int32_t errorCode, 327 std::string errorReason); 328 void MoveToBackground(const std::shared_ptr<AbilityRecord> &abilityRecord); 329 void CompleteBackground(const std::shared_ptr<AbilityRecord> &abilityRecord); 330 void PrintTimeOutLog(std::shared_ptr<AbilityRecord> ability, uint32_t msgId, bool isHalf = false); 331 void DelayCompleteTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord); 332 void CompleteTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord); 333 bool IsContainsAbilityInner(const sptr<IRemoteObject> &token) const; 334 bool CheckProperties(const std::shared_ptr<AbilityRecord> &abilityRecord, const AbilityRequest &abilityRequest, 335 AppExecFwk::LaunchMode launchMode, int32_t userId) const; 336 void NotifyAbilityToken(const sptr<IRemoteObject> &token, const AbilityRequest &abilityRequest) const; 337 int CloseUIAbilityInner(std::shared_ptr<AbilityRecord> abilityRecord, 338 int resultCode, const Want *resultWant, bool isClearSession); 339 340 // byCall 341 int CallAbilityLocked(const AbilityRequest &abilityRequest, int32_t userId); 342 sptr<SessionInfo> CreateSessionInfo(const AbilityRequest &abilityRequest) const; 343 int NotifySCBPendingActivation(sptr<SessionInfo> &sessionInfo, const AbilityRequest &abilityRequest) const; 344 int ResolveAbility(const std::shared_ptr<AbilityRecord> &targetAbility, const AbilityRequest &abilityRequest) const; 345 std::vector<std::shared_ptr<AbilityRecord>> GetAbilityRecordsByName(const AppExecFwk::ElementName &element); 346 347 void EnqueueAbilityToFront(const AbilityRequest &abilityRequest); 348 void NotifyStartSpecifiedAbility(AbilityRequest &request, const AAFwk::Want &want); 349 void NotifyRestartSpecifiedAbility(AbilityRequest &request, const sptr<IRemoteObject> &token); 350 int MoveAbilityToFront(const AbilityRequest &abilityRequest, const std::shared_ptr<AbilityRecord> &abilityRecord, 351 std::shared_ptr<AbilityRecord> callerAbility, std::shared_ptr<StartOptions> startOptions = nullptr); 352 int SendSessionInfoToSCB(std::shared_ptr<AbilityRecord> &callerAbility, sptr<SessionInfo> &sessionInfo); 353 int StartAbilityBySpecifed(const AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> &callerAbility); 354 std::shared_ptr<AbilityRecord> GetReusedSpecifiedAbility(const AAFwk::Want &want, const std::string &flag); 355 void EraseSpecifiedAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord); 356 357 void SetLastExitReason(std::shared_ptr<AbilityRecord> &abilityRecord) const; 358 LastExitReason CovertAppExitReasonToLastReason(const Reason exitReason) const; 359 void SetRevicerInfo(const AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> &abilityRecord) const; 360 361 bool CheckPrepareTerminateEnable(const std::shared_ptr<AbilityRecord> &abilityRecord); 362 bool GetContentAndTypeId(uint32_t msgId, std::string &msgContent, int &typeId) const; 363 364 bool CheckSessionInfo(sptr<SessionInfo> sessionInfo) const; 365 std::shared_ptr<AbilityRecord> CreateAbilityRecord(AbilityRequest &abilityRequest, 366 sptr<SessionInfo> sessionInfo) const; 367 void AddCallerRecord(AbilityRequest &abilityRequest, sptr<SessionInfo> sessionInfo, 368 std::shared_ptr<AbilityRecord> uiAbilityRecord) const; 369 void CheckSpecified(AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> uiAbilityRecord); 370 void SendKeyEvent(AbilityRequest &abilityRequest) const; 371 372 mutable ffrt::mutex sessionLock_; 373 std::unordered_map<int32_t, std::shared_ptr<AbilityRecord>> sessionAbilityMap_; 374 std::unordered_map<int64_t, std::shared_ptr<AbilityRecord>> tmpAbilityMap_; 375 std::list<std::shared_ptr<AbilityRecord>> terminateAbilityList_; 376 sptr<Rosen::ISession> rootSceneSession_; 377 std::map<SpecifiedInfo, std::shared_ptr<AbilityRecord>, key_compare> specifiedAbilityMap_; 378 std::queue<AbilityRequest> abilityQueue_; 379 std::queue<SpecifiedInfo> specifiedInfoQueue_; 380 sptr<ISessionHandler> handler_; 381 bool isPcDevice_ = false; 382 }; 383 } // namespace AAFwk 384 } // namespace OHOS 385 #endif // OHOS_ABILITY_RUNTIME_UI_ABILITY_LIFECYCLE_MANAGER_H