1 /* 2 * Copyright (c) 2021-2025 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_AMS_MGR_SCHEDULER_H 17 #define OHOS_ABILITY_RUNTIME_AMS_MGR_SCHEDULER_H 18 19 #include "ability_info.h" 20 #include "ability_running_record.h" 21 #include "ams_mgr_stub.h" 22 #include "app_debug_listener_interface.h" 23 #include "app_mgr_constants.h" 24 #include "app_mgr_service_inner.h" 25 #include "app_record_id.h" 26 #include "app_running_record.h" 27 #include "app_scheduler_proxy.h" 28 #include "appexecfwk_errors.h" 29 #include "application_info.h" 30 #include "if_system_ability_manager.h" 31 #include "nocopyable.h" 32 #include "system_ability.h" 33 #include "task_handler_wrap.h" 34 35 namespace OHOS { 36 namespace AppExecFwk { 37 class AmsMgrScheduler : public AmsMgrStub { 38 public: 39 AmsMgrScheduler( 40 const std::shared_ptr<AppMgrServiceInner> &MgrServiceInner_, 41 const std::shared_ptr<AAFwk::TaskHandlerWrap> &Handler_); 42 virtual ~AmsMgrScheduler() override; 43 44 /** 45 * LoadAbility, call LoadAbility() through proxy project, load the ability that needed to be started. 46 * 47 * @param token, the unique identification to start the ability. 48 * @param preToken, the unique identification to call the ability. 49 * @param abilityInfo, the ability information. 50 * @param appInfo, the app information. 51 * @param want, the starting information. 52 */ 53 virtual void LoadAbility(const std::shared_ptr<AbilityInfo> &abilityInfo, 54 const std::shared_ptr<ApplicationInfo> &appInfo, 55 const std::shared_ptr<AAFwk::Want> &want, std::shared_ptr<AbilityRuntime::LoadParam> loadParam) override; 56 57 /** 58 * TerminateAbility, call TerminateAbility() through the proxy object, terminate the token ability. 59 * 60 * @param token, token, he unique identification to terminate the ability. 61 * @param clearMissionFlag, indicates whether terminate the ability when clearMission. 62 * @return 63 */ 64 virtual void TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag) override; 65 66 /** 67 * UpdateAbilityState, call UpdateAbilityState() through the proxy object, update the ability status. 68 * 69 * @param token, the unique identification to update the ability. 70 * @param state, ability status that needs to be updated. 71 * @return 72 */ 73 virtual void UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state) override; 74 75 /** 76 * UpdateExtensionState, call UpdateExtensionState() through the proxy object, update the extension status. 77 * 78 * @param token, the unique identification to update the extension. 79 * @param state, extension status that needs to be updated. 80 * @return 81 */ 82 virtual void UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state) override; 83 84 /** 85 * RegisterAppStateCallback, call RegisterAppStateCallback() through the proxy object, register the callback. 86 * 87 * @param callback, Ams register the callback. 88 * @return 89 */ 90 virtual void RegisterAppStateCallback(const sptr<IAppStateCallback> &callback) override; 91 92 /** 93 * KillProcessByAbilityToken, call KillProcessByAbilityToken() through proxy object, 94 * kill the process by ability token. 95 * 96 * @param token, the unique identification to the ability. 97 * @return 98 */ 99 virtual void KillProcessByAbilityToken(const sptr<IRemoteObject> &token) override; 100 101 /** 102 * KillProcessesByUserId, call KillProcessesByUserId() through proxy object, 103 * kill the processes by userId. 104 * Send appSpawn uninstall debug hap message. 105 * 106 * @param userId, the user id. 107 * @param isNeedSendAppSpawnMsg, true send appSpawn message otherwise not send. 108 * @return 109 */ 110 virtual void KillProcessesByUserId(int32_t userId, bool isNeedSendAppSpawnMsg = false, 111 sptr<AAFwk::IUserCallback> callback = nullptr) override; 112 113 /** 114 * KillProcessesByPids, only in process call is allowed, 115 * kill the processes by pid list given. 116 * 117 * @param pids, the pid list of processes are going to be killed. 118 * @param reason, the reason to kill the processes. 119 * @param subProcess, kill SubProcess or not. 120 * @return ERR_OK, return back success, others fail. 121 */ 122 virtual int32_t KillProcessesByPids(const std::vector<int32_t> &pids, 123 const std::string &reason = "KillProcessesByPids", bool subProcess = false, 124 bool isKillPrecedeStart = false) override; 125 126 /** 127 * Set child and parent relationship 128 * @param token child process 129 * @param callerToken parent process 130 */ 131 virtual void AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken) override; 132 133 /** 134 * KillProcessWithAccount, call KillProcessWithAccount() through proxy object, kill the 135 * process. 136 * 137 * @param bundleName, bundle name in Application record. 138 * @param accountId, account ID. 139 * @return ERR_OK, return back success, others fail. 140 */ 141 virtual int32_t KillProcessWithAccount(const std::string &bundleName, const int accountId, 142 const bool clearPageStack = false, int32_t appIndex = 0) override; 143 144 /** 145 * KillProcessesInBatch, kill processes in batch, call KillProcessesInBatch() through proxy object; 146 * the killed bundle won't be started by the watcher. 147 * 148 * @param pids, the pid list of processes are going to be killed. 149 * @return ERR_OK, return back success, others fail. 150 */ 151 virtual int32_t KillProcessesInBatch(const std::vector<int32_t> &pids) override; 152 153 /** 154 * UpdateApplicationInfoInstalled, call UpdateApplicationInfoInstalled() through proxy object, 155 * update the application info after new module installed. 156 * 157 * @param bundleName, bundle name in Application record. 158 * @param uid, uid. 159 * @return ERR_OK, return back success, others fail. 160 */ 161 virtual int32_t UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid, 162 const std::string &moduleName, bool isPlugin) override; 163 164 /** 165 * KillApplication, call KillApplication() through proxy object, kill the application. 166 * 167 * @param bundleName, bundle name in Application record. 168 * @return ERR_OK, return back success, others fail. 169 */ 170 virtual int32_t KillApplication(const std::string &bundleName, bool clearPageStack = false, 171 int32_t appIndex = 0) override; 172 173 /** 174 * ForceKillApplication, force kill the application. 175 * 176 * @param bundleName, bundle name in Application record. 177 * @param userId, userId. 178 * @param appIndex, appIndex. 179 * @return ERR_OK, return back success, others fail. 180 */ 181 virtual int32_t ForceKillApplication(const std::string &bundleName, const int userId = -1, 182 const int appIndex = 0) override; 183 184 /** 185 * KillProcessesByAccessTokenId. 186 * 187 * @param accessTokenId, accessTokenId. 188 * @return ERR_OK, return back success, others fail. 189 */ 190 virtual int32_t KillProcessesByAccessTokenId(const uint32_t accessTokenId) override; 191 192 /** 193 * KillApplicationByUid, call KillApplicationByUid() through proxy object, kill the application. 194 * 195 * @param bundleName, bundle name in Application record. 196 * @param uid, uid. 197 * @param reason, caller function name. 198 * @return ERR_OK, return back success, others fail. 199 */ 200 virtual int KillApplicationByUid(const std::string &bundleName, const int uid, 201 const std::string& reason = "KillApplicationByUid") override; 202 203 virtual int32_t NotifyUninstallOrUpgradeApp(const std::string &bundleName, int32_t uid, bool isUpgrade) override; 204 205 virtual void NotifyUninstallOrUpgradeAppEnd(int32_t uid) override; 206 207 /** 208 * KillApplicationSelf, this allows app to terminate itself. 209 * 210 * @param clearPageStack, the flag indicates if ClearPageStack lifecycle should be scheduled. 211 * @return ERR_OK for success call, others for failure. 212 */ 213 virtual int KillApplicationSelf(const bool clearPageStack = false, 214 const std::string& reason = "KillApplicationSelf") override; 215 216 /** 217 * Get application info by process id. 218 * 219 * @param pid Process id. 220 * @param application Application information got. 221 * @param debug Whether IsDebugApp. 222 * @return Returns ERR_OK on success, others on failure. 223 */ 224 int GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug) override; 225 226 /** 227 * Record process exit reason to appRunningRecord 228 * @param pid pid 229 * @param reason reason enum 230 * @param exitMsg exitMsg 231 * @return Returns ERR_OK on success, others on failure. 232 */ 233 virtual int32_t NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg) override; 234 235 /** 236 * AbilityAttachTimeOut, called by ability manager service when an ability is loaded timeout. 237 * 238 * @param token indicates the token of the ability which is timeout. 239 */ 240 virtual void AbilityAttachTimeOut(const sptr<IRemoteObject> &token) override; 241 242 /** 243 * PrepareTerminate, called before terminating one ability by ability manager service to notify application. 244 * 245 * @param token indicates the token of the ability to be terminated. 246 * @param clearMissionFlag indicates whether it is caused by cleaning mission. 247 */ 248 virtual void PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag = false) override; 249 250 /** 251 * GetRunningProcessInfoByToken, get process info for one ability. 252 * 253 * @param token indicates the token of the ability requested. 254 * @param info output of the information. 255 */ 256 virtual void GetRunningProcessInfoByToken( 257 const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info) override; 258 259 /** 260 * Set AbilityForegroundingFlag of an app-record to true. 261 * 262 * @param pid, pid. 263 * 264 */ 265 void SetAbilityForegroundingFlagToAppRecord(const pid_t pid) override; 266 267 /** 268 * Start specified ability. 269 * 270 * @param want Want contains information of the ability to start. 271 * @param abilityInfo Ability information. 272 * @param requestId request id to callback 273 */ 274 virtual void StartSpecifiedAbility( 275 const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo, int32_t requestId = 0) override; 276 277 /** 278 * Register response of start specified ability. 279 * 280 * @param response Response of start specified ability. 281 */ 282 virtual void RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response) override; 283 284 virtual void PrepareTerminateApp(const pid_t pid, const std::string &moduleName) override; 285 286 /** 287 * Start specified process. 288 * 289 * @param want Want contains information wish to start. 290 * @param abilityInfo Ability information. 291 * @param requestId for callback 292 */ 293 virtual void StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo, 294 int32_t requestId = 0, const std::string &customProcess = "") override; 295 296 /** 297 * SetCurrentUserId, set the userid to app mgr by ability mgr when switching to another user. 298 * 299 * @param userId the new user. 300 */ 301 virtual void SetCurrentUserId(const int32_t userId) override; 302 303 virtual void SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess) override; 304 305 /** 306 * GetBundleNameByPid, get bundleName and uid of a process. 307 * 308 * @param pid input param indicates the pid of the process. 309 * @param bundleName output for bundleName. 310 * @param uid output for uid. 311 * @return ERR_OK for success call, others for failure. 312 */ 313 virtual int32_t GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid) override; 314 315 /** 316 * @brief Register app debug listener. 317 * @param listener App debug listener. 318 * @return ERR_OK, return back success, others fail. 319 */ 320 int32_t RegisterAppDebugListener(const sptr<IAppDebugListener> &listener) override; 321 322 /** 323 * @brief Unregister app debug listener. 324 * @param listener App debug listener. 325 * @return ERR_OK, return back success, others fail. 326 */ 327 int32_t UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener) override; 328 329 /** 330 * @brief Attach app debug. 331 * @param bundleName The application bundle name. 332 * @return Returns ERR_OK on success, others on failure. 333 */ 334 int32_t AttachAppDebug(const std::string &bundleName, bool isDebugFromLocal) override; 335 336 /** 337 * @brief Detach app debug. 338 * @param bundleName The application bundle name. 339 * @return Returns ERR_OK on success, others on failure. 340 */ 341 int32_t DetachAppDebug(const std::string &bundleName) override; 342 343 /** 344 * @brief Set app waiting debug mode. 345 * @param bundleName The application bundle name. 346 * @param isPersist The persist flag. 347 * @return Returns ERR_OK on success, others on failure. 348 */ 349 int32_t SetAppWaitingDebug(const std::string &bundleName, bool isPersist) override; 350 351 /** 352 * @brief Cancel app waiting debug mode. 353 * @return Returns ERR_OK on success, others on failure. 354 */ 355 int32_t CancelAppWaitingDebug() override; 356 357 /** 358 * @brief Get waiting debug mode application. 359 * @param debugInfoList The debug info list, including bundle name and persist flag. 360 * @return Returns ERR_OK on success, others on failure. 361 */ 362 int32_t GetWaitingDebugApp(std::vector<std::string> &debugInfoList) override; 363 364 /** 365 * @brief Determine whether it is a waiting debug application based on the bundle name. 366 * @return Returns true if it is a waiting debug application, otherwise it returns false. 367 */ 368 bool IsWaitingDebugApp(const std::string &bundleName) override; 369 370 /** 371 * @brief Clear non persist waiting debug flag. 372 */ 373 void ClearNonPersistWaitingDebugFlag() override; 374 375 /** 376 * @brief Registering ability debug mode response. 377 * @param response Response for ability debug object. 378 * @return Returns ERR_OK on success, others on failure. 379 */ 380 int32_t RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response) override; 381 382 /** 383 * @brief Determine whether it is an attachment debug application based on the bundle name. 384 * @param bundleName The application bundle name. 385 * @return Returns true if it is an attach debug application, otherwise it returns false. 386 */ 387 bool IsAttachDebug(const std::string &bundleName) override; 388 389 /** 390 * @brief Set resident process enable status. 391 * @param bundleName The application bundle name. 392 * @param enable The current updated enable status. 393 * @param uid indicates user, 0 for all users 394 */ 395 void SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid) override; 396 397 /** 398 * @brief Set non-resident keep-alive process status. 399 * @param bundleName The application bundle name. 400 * @param enable The current updated enable status. 401 * @param uid indicates user, 0 for all users 402 */ 403 void SetKeepAliveDkv(const std::string &bundleName, bool enable, int32_t uid) override; 404 405 void SetKeepAliveAppService(const std::string &bundleName, bool enable, int32_t uid) override; 406 407 /** 408 * To clear the process by ability token. 409 * 410 * @param token the unique identification to the ability. 411 */ 412 virtual void ClearProcessByToken(sptr<IRemoteObject> token) override; 413 414 /** 415 * whether memory size is sufficient. 416 * @return Returns true is sufficient memory size, others return false. 417 */ 418 virtual bool IsMemorySizeSufficent() override; 419 420 /** 421 * whether or not requier a big memory 422 * @return Returens true is no big memory, others return false. 423 */ 424 virtual bool IsNoRequireBigMemory() override; 425 426 /** 427 * Notifies that one ability is attached to status bar. 428 * 429 * @param token the token of the abilityRecord that is attached to status bar. 430 */ 431 void AttachedToStatusBar(const sptr<IRemoteObject> &token) override; 432 433 virtual void BlockProcessCacheByPids(const std::vector<int32_t> &pids) override; 434 435 /** 436 * whether killed for upgrade web. 437 * 438 * @param bundleName the bundle name is killed for upgrade web. 439 * @return Returns true is killed for upgrade web, others return false. 440 */ 441 virtual bool IsKilledForUpgradeWeb(const std::string &bundleName) override; 442 443 /** 444 * Request to clean uiability from user. 445 * 446 * @param token the token of ability. 447 * @return Returns true if clean success, others return false. 448 */ 449 virtual bool CleanAbilityByUserRequest(const sptr<IRemoteObject> &token) override; 450 451 /** 452 * whether the abilities of process specified by pid type only UIAbility. 453 * @return Returns true is only UIAbility, otherwise return false 454 */ 455 virtual bool IsProcessContainsOnlyUIAbility(const pid_t pid) override; 456 457 /** 458 * Whether a process is attached, refer to AttachApplication 459 */ 460 virtual bool IsProcessAttached(sptr<IRemoteObject> token) override; 461 462 virtual bool IsCallerKilling(const std::string& callerKey) override; 463 464 virtual int32_t PreloadApplicationByPhase(const std::string &bundleName, int32_t userId, int32_t appIndex, 465 AppExecFwk::PreloadPhase preloadPhase) override; 466 467 virtual int32_t NotifyPreloadAbilityStateChanged(sptr<IRemoteObject> token, bool isPreForeground) override; 468 469 virtual int32_t CheckPreloadAppRecordExist(const std::string &bundleName, int32_t userId, int32_t appIndex, 470 bool &isExist) override; 471 472 virtual int32_t VerifyKillProcessPermission(const std::string &bundleName) override; 473 474 private: 475 /** 476 * @brief Judge whether the application service is ready. 477 * 478 * @return Returns true means service is ready, otherwise service is not ready. 479 */ 480 bool IsReady() const; 481 482 private: 483 std::shared_ptr<AppMgrServiceInner> amsMgrServiceInner_; 484 std::shared_ptr<AAFwk::TaskHandlerWrap> amsHandler_; 485 sptr<ISystemAbilityManager> systemAbilityMgr_; 486 487 DISALLOW_COPY_AND_MOVE(AmsMgrScheduler); 488 }; 489 } // namespace AppExecFwk 490 } // namespace OHOS 491 #endif // OHOS_ABILITY_RUNTIME_AMS_MGR_SCHEDULER_H 492