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 MissionVaildResult; 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 * @return Returns ERR_OK on success, others on failure. 125 */ 126 int CloseUIAbility(const std::shared_ptr<AbilityRecord> &abilityRecord, 127 int resultCode, const Want *resultWant); 128 129 /** 130 * Set rootSceneSession by SCB. 131 * 132 * @param rootSceneSession Indicates root scene session of SCB. 133 */ 134 void SetRootSceneSession(const sptr<IRemoteObject> &rootSceneSession); 135 136 int NotifySCBToStartUIAbility(const AbilityRequest &abilityRequest, int32_t userId); 137 138 /** 139 * @brief handle time out event 140 * 141 * @param msgId the msg id in ability record 142 * @param abilityRecordId the id of ability record 143 * @param isHalf is half 144 */ 145 void OnTimeOut(uint32_t msgId, int64_t abilityRecordId, bool isHalf = false); 146 147 /** 148 * @brief handle when ability died 149 * 150 * @param abilityRecord the died ability 151 */ 152 void OnAbilityDied(std::shared_ptr<AbilityRecord> abilityRecord); 153 154 /** 155 * resolve the call ipc of ability for scheduling oncall. 156 * 157 * @param abilityRequest target ability request. 158 */ 159 int ResolveLocked(const AbilityRequest &abilityRequest, int32_t userId); 160 161 /** 162 * Call UIAbility by SCB. 163 * 164 * @param sessionInfo the session info of the ability to be called. 165 */ 166 void CallUIAbilityBySCB(const sptr<SessionInfo> &sessionInfo); 167 168 /** 169 * OnAcceptWantResponse. 170 * 171 * @param want the want of the ability to start. 172 * @param abilityRequest the flag of the ability to start. 173 * @return Returns ERR_OK on success, others on failure. 174 */ 175 void OnAcceptWantResponse(const AAFwk::Want &want, const std::string &flag); 176 177 /** 178 * Start specified ability by SCB. 179 * 180 * @param want Want information. 181 * @param userId Use id. 182 */ 183 void StartSpecifiedAbilityBySCB(const Want &want, int32_t userId); 184 185 /** 186 * CallRequestDone, after invoke callRequest, ability will call this interface to return callee. 187 * 188 * @param abilityRecord ability's record. 189 * @param callStub ability's callee. 190 */ 191 void CallRequestDone(const std::shared_ptr<AbilityRecord> &abilityRecord, const sptr<IRemoteObject> &callStub); 192 193 /** 194 * release the connection of this call. 195 * 196 * @param connect caller callback ipc. 197 * @param element target ability name. 198 */ 199 int ReleaseCallLocked(const sptr<IAbilityConnection> &connect, const AppExecFwk::ElementName &element); 200 201 /** 202 * @brief handle when call connection died 203 * 204 * @param callRecord the died call connection 205 */ 206 void OnCallConnectDied(const std::shared_ptr<CallRecord> &callRecord); 207 208 /** 209 * Get sessionId by ability token. 210 * 211 * @param token the ability token. 212 * @return Returns sessionId on success, zero on failure. 213 */ 214 int32_t GetSessionIdByAbilityToken(const sptr<IRemoteObject> &token); 215 216 void GetActiveAbilityList(const std::string &bundleName, std::vector<std::string> &abilityList); 217 218 bool PrepareTerminateAbility(const std::shared_ptr<AbilityRecord> &abilityRecord); 219 void SetSessionHandler(const sptr<ISessionHandler> &handler); 220 221 /** 222 * Get abilityRecord by session id. 223 * 224 * @param sessionId the session id. 225 * @return Returns abilityRecord on success, nullptr on failure. 226 */ 227 std::shared_ptr<AbilityRecord> GetAbilityRecordsById(int32_t sessionId) const; 228 229 void GetActiveAbilityList(const std::string &bundleName, std::vector<std::string> &abilityList, 230 int32_t targetUserId) const; 231 232 void OnAppStateChanged(const AppInfo &info, int32_t targetUserId); 233 234 void UninstallApp(const std::string &bundleName, int32_t uid, int32_t targetUserId); 235 236 void GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info, bool isPerm, int32_t userId) const; 237 238 #ifdef ABILITY_COMMAND_FOR_TEST 239 /** 240 * Block ability. 241 * 242 * @param abilityRecordId The Ability Record Id. 243 * @return Returns ERR_OK on success, others on failure. 244 */ 245 int BlockAbility(int abilityRecordId, int32_t targetUserId) const; 246 #endif 247 248 private: 249 std::shared_ptr<AbilityRecord> GetAbilityRecordByToken(const sptr<IRemoteObject> &token) const; 250 int32_t GetPersistentIdByAbilityRequest(const AbilityRequest &abilityRequest, bool &reuse, int32_t userId) const; 251 int32_t GetReusedSpecifiedPersistentId(const AbilityRequest &abilityRequest, bool &reuse, int32_t userId) const; 252 int32_t GetReusedStandardPersistentId(const AbilityRequest &abilityRequest, bool &reuse, int32_t userId) const; 253 void UpdateAbilityRecordLaunchReason(const AbilityRequest &abilityRequest, 254 std::shared_ptr<AbilityRecord> &abilityRecord) const; 255 void EraseAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord); 256 int DispatchState(const std::shared_ptr<AbilityRecord> &abilityRecord, int state); 257 int DispatchTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord); 258 int DispatchBackground(const std::shared_ptr<AbilityRecord> &abilityRecord); 259 int DispatchForeground(const std::shared_ptr<AbilityRecord> &abilityRecord, bool success, 260 AbilityState state = AbilityState::INITIAL); 261 void CompleteForegroundSuccess(const std::shared_ptr<AbilityRecord> &abilityRecord); 262 void HandleLoadTimeout(const std::shared_ptr<AbilityRecord> &ability); 263 void HandleForegroundFailed(const std::shared_ptr<AbilityRecord> &ability, 264 AbilityState state = AbilityState::INITIAL); 265 void HandleForegroundTimeout(const std::shared_ptr<AbilityRecord> &ability); 266 void NotifySCBToHandleException(const std::shared_ptr<AbilityRecord> &ability, int32_t errorCode, 267 std::string errorReason); 268 void MoveToBackground(const std::shared_ptr<AbilityRecord> &abilityRecord); 269 void CompleteBackground(const std::shared_ptr<AbilityRecord> &abilityRecord); 270 void PrintTimeOutLog(const std::shared_ptr<AbilityRecord> &ability, uint32_t msgId, bool isHalf = false); 271 void DelayCompleteTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord); 272 void CompleteTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord); 273 bool IsContainsAbilityInner(const sptr<IRemoteObject> &token) const; 274 void ReportEventToSuspendManager(const AppExecFwk::AbilityInfo &abilityInfo) const; 275 bool CheckProperties(const std::shared_ptr<AbilityRecord> &abilityRecord, const AbilityRequest &abilityRequest, 276 AppExecFwk::LaunchMode launchMode, int32_t userId) const; 277 void NotifyAbilityToken(const sptr<IRemoteObject> &token, const AbilityRequest &abilityRequest) const; 278 279 // byCall 280 int CallAbilityLocked(const AbilityRequest &abilityRequest, int32_t userId); 281 sptr<SessionInfo> CreateSessionInfo(const AbilityRequest &abilityRequest) const; 282 int NotifySCBPendingActivation(sptr<SessionInfo> &sessionInfo, const AbilityRequest &abilityRequest) const; 283 int ResolveAbility(const std::shared_ptr<AbilityRecord> &targetAbility, const AbilityRequest &abilityRequest) const; 284 std::vector<std::shared_ptr<AbilityRecord>> GetAbilityRecordsByName(const AppExecFwk::ElementName &element); 285 286 void EnqueueAbilityToFront(const AbilityRequest &abilityRequest); 287 void NotifyStartSpecifiedAbility(AbilityRequest &request, const AAFwk::Want &want); 288 void NotifyRestartSpecifiedAbility(AbilityRequest &request, const sptr<IRemoteObject> &token); 289 int MoveAbilityToFront(const AbilityRequest &abilityRequest, const std::shared_ptr<AbilityRecord> &abilityRecord, 290 std::shared_ptr<AbilityRecord> callerAbility, std::shared_ptr<StartOptions> startOptions = nullptr); 291 int SendSessionInfoToSCB(std::shared_ptr<AbilityRecord> &callerAbility, sptr<SessionInfo> &sessionInfo); 292 int StartAbilityBySpecifed(const AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> &callerAbility); 293 std::shared_ptr<AbilityRecord> GetReusedSpecifiedAbility(const AAFwk::Want &want, const std::string &flag); 294 void EraseSpecifiedAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord); 295 296 void SetLastExitReason(std::shared_ptr<AbilityRecord> &abilityRecord) const; 297 LastExitReason CovertAppExitReasonToLastReason(const Reason exitReason) const; 298 void SetRevicerInfo(const AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> &abilityRecord) const; 299 300 bool CheckPrepareTerminateEnable(const std::shared_ptr<AbilityRecord> &abilityRecord); 301 302 mutable ffrt::mutex sessionLock_; 303 std::unordered_map<int32_t, std::shared_ptr<AbilityRecord>> sessionAbilityMap_; 304 std::unordered_map<int64_t, std::shared_ptr<AbilityRecord>> tmpAbilityMap_; 305 std::list<std::shared_ptr<AbilityRecord>> terminateAbilityList_; 306 sptr<Rosen::ISession> rootSceneSession_; 307 std::map<SpecifiedInfo, std::shared_ptr<AbilityRecord>, key_compare> specifiedAbilityMap_; 308 std::queue<AbilityRequest> abilityQueue_; 309 std::queue<SpecifiedInfo> specifiedInfoQueue_; 310 sptr<ISessionHandler> handler_; 311 }; 312 } // namespace AAFwk 313 } // namespace OHOS 314 #endif // OHOS_ABILITY_RUNTIME_UI_ABILITY_LIFECYCLE_MANAGER_H