1 /* 2 * Copyright (c) 2021-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_APP_SCHEDULER_H 17 #define OHOS_ABILITY_RUNTIME_APP_SCHEDULER_H 18 19 #include <memory> 20 #include <unordered_set> 21 22 #include "ability_debug_response_interface.h" 23 #include "ability_info.h" 24 #include "ability_manager_client.h" 25 #include "app_debug_listener_interface.h" 26 #include "application_info.h" 27 #include "appmgr/app_mgr_client.h" 28 #include "appmgr/app_state_callback_host.h" 29 #include "appmgr/start_specified_ability_response_stub.h" 30 #include "bundle_info.h" 31 #include "fault_data.h" 32 #include "iremote_object.h" 33 #include "refbase.h" 34 #include "running_process_info.h" 35 #include "singleton.h" 36 #include "system_memory_attr.h" 37 #include "want.h" 38 39 namespace OHOS { 40 namespace AppExecFwk { 41 class Configuration; 42 } 43 namespace AAFwk { 44 /** 45 * @enum AppAbilityState 46 * AppAbilityState defines the life cycle state of app ability. 47 */ 48 enum class AppAbilityState { 49 ABILITY_STATE_UNDEFINED = 0, 50 ABILITY_STATE_FOREGROUND, 51 ABILITY_STATE_BACKGROUND, 52 ABILITY_STATE_END, 53 }; 54 55 enum class AppState { 56 BEGIN = 0, 57 READY, 58 FOREGROUND, 59 FOCUS, 60 BACKGROUND, 61 TERMINATED, 62 END, 63 SUSPENDED, 64 COLD_START = 99, 65 }; 66 67 struct AppData { 68 std::string appName; 69 int32_t uid; 70 }; 71 72 struct AppInfo { 73 std::vector<AppData> appData; 74 std::string processName; 75 AppState state; 76 pid_t pid = 0; 77 }; 78 /** 79 * @class AppStateCallback 80 * AppStateCallback. 81 */ 82 class AppStateCallback { 83 public: AppStateCallback()84 AppStateCallback() 85 {} ~AppStateCallback()86 virtual ~AppStateCallback() 87 {} 88 89 virtual void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state) = 0; 90 91 virtual void OnAppStateChanged(const AppInfo &info) = 0; 92 NotifyConfigurationChange(const AppExecFwk::Configuration &config, int32_t userId)93 virtual void NotifyConfigurationChange(const AppExecFwk::Configuration &config, int32_t userId) {} 94 NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos)95 virtual void NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos) {} 96 97 /** 98 * @brief Notify abilityms app process pre cache 99 * @param pid process pid. 100 * @param userId userId Designation User ID. 101 */ NotifyAppPreCache(int32_t pid, int32_t userId)102 virtual void NotifyAppPreCache(int32_t pid, int32_t userId) {} 103 104 /** 105 * @brief Notify abilityms app process OnRemoteDied 106 * @param abilityTokens abilities in died process. 107 */ OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens)108 virtual void OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens) {} 109 }; 110 111 class StartSpecifiedAbilityResponse : public AppExecFwk::StartSpecifiedAbilityResponseStub { 112 public: 113 StartSpecifiedAbilityResponse() = default; 114 virtual ~StartSpecifiedAbilityResponse() = default; 115 116 virtual void OnAcceptWantResponse(const AAFwk::Want &want, const std::string &flag, 117 int32_t requestId) override; 118 virtual void OnTimeoutResponse(const AAFwk::Want &want, int32_t requestId) override; 119 120 virtual void OnNewProcessRequestResponse(const AAFwk::Want &want, const std::string &flag, 121 int32_t requestId) override; 122 virtual void OnNewProcessRequestTimeoutResponse(const AAFwk::Want &want, int32_t requestId) override; 123 }; 124 125 /** 126 * @class AppScheduler 127 * AppScheduler , access app manager service. 128 */ 129 class AppScheduler : virtual RefBase, public AppExecFwk::AppStateCallbackHost { 130 DECLARE_DELAYED_SINGLETON(AppScheduler) 131 public: 132 /** 133 * init app scheduler. 134 * @param callback, app state call back. 135 * @return true on success ,false on failure. 136 */ 137 bool Init(const std::weak_ptr<AppStateCallback> &callback); 138 139 /** 140 * load ability with token, ability info and application info. 141 * 142 * @param token, the token of ability. 143 * @param preToken, the token of ability's caller. 144 * @param abilityInfo, ability info. 145 * @param applicationInfo, application info. 146 * @param want ability want 147 * @return true on success ,false on failure. 148 */ 149 int LoadAbility(sptr<IRemoteObject> token, sptr<IRemoteObject> preToken, 150 const AppExecFwk::AbilityInfo &abilityInfo, const AppExecFwk::ApplicationInfo &applicationInfo, 151 const Want &want, int32_t abilityRecordId, const std::string &instanceKey); 152 153 /** 154 * terminate ability with token. 155 * 156 * @param token, the token of ability. 157 * @param clearMissionFlag, indicates whether terminate the ability when clearMission. 158 * @return true on success ,false on failure. 159 */ 160 int TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag); 161 162 /** 163 * move ability to foreground. 164 * 165 * @param token, the token of ability. 166 */ 167 void MoveToForeground(const sptr<IRemoteObject> &token); 168 169 /** 170 * move ability to background. 171 * 172 * @param token, the token of ability. 173 */ 174 void MoveToBackground(const sptr<IRemoteObject> &token); 175 176 /** 177 * Update ability state. 178 * 179 * @param token, the token of ability. 180 * @param state, ability state. 181 */ 182 void UpdateAbilityState(const sptr<IRemoteObject> &token, const AppExecFwk::AbilityState state); 183 184 /** 185 * UpdateExtensionState, call UpdateExtensionState() through the proxy object, update the extension status. 186 * 187 * @param token, the unique identification to update the extension. 188 * @param state, extension status that needs to be updated. 189 * @return 190 */ 191 void UpdateExtensionState(const sptr<IRemoteObject> &token, const AppExecFwk::ExtensionState state); 192 193 /** 194 * KillProcessByAbilityToken, call KillProcessByAbilityToken() through proxy object, 195 * kill the process by ability token. 196 * 197 * @param token, the unique identification to the ability. 198 */ 199 void KillProcessByAbilityToken(const sptr<IRemoteObject> &token); 200 201 /** 202 * KillProcessesByUserId, call KillProcessesByUserId() through proxy object, 203 * kill the process by user id. 204 * 205 * @param userId, the user id. 206 */ 207 void KillProcessesByUserId(int32_t userId); 208 209 /** 210 * KillProcessesByPids, only in process call is allowed, 211 * kill the processes by pid list given. 212 * 213 * @param pids, the pid list of processes are going to be killed. 214 */ 215 void KillProcessesByPids(std::vector<int32_t> &pids); 216 217 /** 218 * Set child and parent relationship 219 * @param token child process 220 * @param callerToken parent process 221 */ 222 void AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken); 223 224 /** 225 * convert ability state to app ability state. 226 * 227 * @param state, the state of ability. 228 */ 229 AppAbilityState ConvertToAppAbilityState(const int32_t state); 230 231 /** 232 * get ability state. 233 * 234 * @return state, the state of app ability. 235 */ 236 AppAbilityState GetAbilityState() const; 237 238 /** 239 * kill the application 240 * 241 * @param bundleName. 242 */ 243 int KillApplication(const std::string &bundleName, const bool clearPageStack = false); 244 245 /** 246 * ForceKillApplication, force kill the application. 247 * 248 * @param bundleName, bundle name in Application record. 249 * @param userId, userId. 250 * @param appIndex, appIndex. 251 * @return ERR_OK, return back success, others fail. 252 */ 253 int ForceKillApplication(const std::string &bundleName, const int userId = -1, 254 const int appIndex = 0); 255 256 /** 257 * KillProcessesByAccessTokenId. 258 * 259 * @param accessTokenId, accessTokenId. 260 * @return ERR_OK, return back success, others fail. 261 */ 262 int KillProcessesByAccessTokenId(const uint32_t accessTokenId); 263 264 /** 265 * kill the application by uid 266 * 267 * @param bundleName name of bundle. 268 * @param uid uid of bundle. 269 * @param reason, caller function name. 270 * @return 0 if success. 271 */ 272 int KillApplicationByUid(const std::string &bundleName, int32_t uid, 273 const std::string& reason = "KillApplicationByUid"); 274 275 /** 276 * update the application info after new module installed. 277 * 278 * @param bundleName, bundle name in Application record. 279 * @param uid, uid. 280 * @return 0 if success. 281 */ 282 int UpdateApplicationInfoInstalled(const std::string &bundleName, const int32_t uid); 283 284 /** 285 * Ability attach timeout. If start ability encounter failure, attach timeout to terminate. 286 * 287 * @param token Ability identify. 288 */ 289 void AttachTimeOut(const sptr<IRemoteObject> &token); 290 291 /** 292 * Prepare terminate. 293 * 294 * @param token Ability identify. 295 * @param clearMissionFlag Clear mission flag. 296 */ 297 void PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag = false); 298 299 /** 300 * Get running process information by ability token. 301 * 302 * @param token Ability identify. 303 * @param info Running process info. 304 */ 305 void GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info); 306 307 /** 308 * Get running process information by pid. 309 * 310 * @param pid process id. 311 * @param info Output parameters, return runningProcessInfo. 312 * @return Returns ERR_OK on success, others on failure. 313 */ 314 void GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info) const; 315 316 /** 317 * Set AbilityForegroundingFlag of an app-record to true. 318 * 319 * @param pid, pid. 320 * 321 */ 322 void SetAbilityForegroundingFlagToAppRecord(const pid_t pid) const; 323 324 /** 325 * Start a resident process 326 */ 327 void StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos); 328 329 /** 330 * Start specified ability. 331 * 332 * @param want Want contains information of the ability to start. 333 * @param abilityInfo Ability information. 334 * @param requestId request id to callback 335 */ 336 void StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo, 337 int32_t requestId = 0); 338 339 /** 340 * @brief Get running process information. 341 * 342 * @param info Running process information. 343 * @return Returns ERR_OK on success, others on failure. 344 */ 345 int GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> &info); 346 347 /** 348 * Start specified process. 349 * 350 * @param want Want contains information wish to start. 351 * @param abilityInfo Ability information. 352 * @param requestId for callback 353 */ 354 void StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo, 355 int32_t requestId = 0); 356 357 /** 358 * Start a user test 359 */ 360 int StartUserTest(const Want &want, const sptr<IRemoteObject> &observer, const AppExecFwk::BundleInfo &bundleInfo, 361 int32_t userId); 362 363 /** 364 * @brief Finish user test. 365 * @param msg user test message. 366 * @param resultCode user test result Code. 367 * @param bundleName user test bundleName. 368 * 369 * @return Returns ERR_OK on success, others on failure. 370 */ 371 int FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName); 372 373 /** 374 * GetProcessRunningInfosByUserId, call GetProcessRunningInfosByUserId() through proxy project. 375 * Obtains information about application processes that are running on the device. 376 * 377 * @param info, app name in Application record. 378 * @param userId, user Id in Application record. 379 * @return ERR_OK ,return back success,others fail. 380 */ 381 int GetProcessRunningInfosByUserId(std::vector<AppExecFwk::RunningProcessInfo> &info, int32_t userId); 382 std::string ConvertAppState(const AppState &state); 383 384 /** 385 * ANotify application update system environment changes. 386 * 387 * @param config System environment change parameters. 388 * @return Returns ERR_OK on success, others on failure. 389 */ 390 int UpdateConfiguration(const AppExecFwk::Configuration &config); 391 392 /** 393 * GetConfiguration 394 * 395 * @param info to retrieve configuration data. 396 * @return ERR_OK ,return back success,others fail. 397 */ 398 int GetConfiguration(AppExecFwk::Configuration &config); 399 400 /** 401 * Get the token of ability records by process ID. 402 * 403 * @param pid The process id. 404 * @param tokens The token of ability records. 405 * @return Returns ERR_OK on success, others on failure. 406 */ 407 int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens); 408 409 /** 410 * Get the application info by process ID. 411 * 412 * @param pid The process id. 413 * @param application The application info. 414 * @param debug The app is or not debug. 415 * @return Returns ERR_OK on success, others on failure. 416 */ 417 int GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug); 418 419 /** 420 * Set the process cache status by process ID. 421 * 422 * @param pid The process id. 423 * @param isSupport The process is support cache. 424 * @return Returns ERR_OK on success, others on failure. 425 */ 426 void SetProcessCacheStatus(int32_t pid, bool isSupport); 427 428 /** 429 * Record process exit reason to appRunningRecord 430 * @param pid pid 431 * @param reason reason enum 432 * @param exitMsg exitMsg 433 * @return Returns ERR_OK on success, others on failure. 434 */ 435 virtual int32_t NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg); 436 437 /** 438 * Set the current userId of appMgr, only used by abilityMgr. 439 * 440 * @param userId the user id. 441 * 442 * @return 443 */ 444 void SetCurrentUserId(int32_t userId); 445 446 /** 447 * Set enable start process flag by userId 448 * @param userId the user id. 449 * @param enableStartProcess enable start process. 450 * @return 451 */ 452 void SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess); 453 454 /** 455 * Get bundleName by pid. 456 * 457 * @param pid process id. 458 * @param bundleName Output parameters, return bundleName. 459 * @param uid Output parameters, return userId. 460 * @return Returns ERR_OK on success, others on failure. 461 */ 462 int32_t GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid); 463 464 /** 465 * Notify Fault Data 466 * 467 * @param faultData the fault data. 468 * @return Returns ERR_OK on success, others on failure. 469 */ 470 int32_t NotifyFault(const AppExecFwk::FaultData &faultData); 471 472 /** 473 * @brief Register app debug listener. 474 * @param listener App debug listener. 475 * @return Returns ERR_OK on success, others on failure. 476 */ 477 int32_t RegisterAppDebugListener(const sptr<AppExecFwk::IAppDebugListener> &listener); 478 479 /** 480 * @brief Unregister app debug listener. 481 * @param listener App debug listener. 482 * @return Returns ERR_OK on success, others on failure. 483 */ 484 int32_t UnregisterAppDebugListener(const sptr<AppExecFwk::IAppDebugListener> &listener); 485 486 /** 487 * @brief Attach app debug. 488 * @param bundleName The application bundle name. 489 * @return Returns ERR_OK on success, others on failure. 490 */ 491 int32_t AttachAppDebug(const std::string &bundleName); 492 493 /** 494 * @brief Detach app debug. 495 * @param bundleName The application bundle name. 496 * @return Returns ERR_OK on success, others on failure. 497 */ 498 int32_t DetachAppDebug(const std::string &bundleName); 499 500 /** 501 * @brief Register ability debug response to set debug mode. 502 * @param bundleName The application bundle name. 503 * @return Returns ERR_OK on success, others on failure. 504 */ 505 int32_t RegisterAbilityDebugResponse(const sptr<AppExecFwk::IAbilityDebugResponse> &response); 506 507 /** 508 * @brief Determine whether it is an attachment debug application based on the bundle name. 509 * @param bundleName The application bundle name. 510 * @return Returns true if it is an attach debug application, otherwise it returns false. 511 */ 512 bool IsAttachDebug(const std::string &bundleName); 513 514 /** 515 * To clear the process by ability token. 516 * 517 * @param token the unique identification to the ability. 518 */ 519 void ClearProcessByToken(sptr<IRemoteObject> token) const; 520 521 /** 522 * whether memory size is sufficient. 523 * @return Returns true is sufficient memory size, others return false. 524 */ 525 virtual bool IsMemorySizeSufficent() const; 526 527 /** 528 * Notifies that one ability is attached to status bar. 529 * 530 * @param token the token of the abilityRecord that is attached to status bar. 531 */ 532 void AttachedToStatusBar(const sptr<IRemoteObject> &token); 533 534 /** 535 * Temporarily block the process cache feature. 536 * 537 * @param pids the pids of the processes that should be blocked. 538 */ 539 void BlockProcessCacheByPids(const std::vector<int32_t>& pids); 540 541 /** 542 * Request to clean uiability from user. 543 * 544 * @param token the token of ability. 545 * @return Returns true if clean success, others return false. 546 */ 547 bool CleanAbilityByUserRequest(const sptr<IRemoteObject> &token); 548 549 /** 550 * whether killed for upgrade web. 551 * 552 * @param bundleName the bundle name is killed for upgrade web. 553 * @return Returns true is killed for upgrade web, others return false. 554 */ 555 bool IsKilledForUpgradeWeb(const std::string &bundleName); 556 557 /** 558 * whether the abilities of process specified by pid type only UIAbility. 559 * @return Returns true is only UIAbility, otherwise return false 560 */ 561 bool IsProcessContainsOnlyUIAbility(const pid_t pid); 562 563 bool IsProcessAttached(sptr<IRemoteObject> token) const; 564 565 bool IsAppKilling(sptr<IRemoteObject> token) const; 566 567 protected: 568 /** 569 * OnAbilityRequestDone, app manager service call this interface after ability request done. 570 * 571 * @param token,ability's token. 572 * @param state,the state of ability lift cycle. 573 */ 574 virtual void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const AppExecFwk::AbilityState state) override; 575 576 /** 577 * Application state changed callback. 578 * 579 * @param appProcessData Process data 580 */ 581 virtual void OnAppStateChanged(const AppExecFwk::AppProcessData &appData) override; 582 583 /** 584 * @brief Notify application update system environment changes. 585 * @param config System environment change parameters. 586 * @param userId userId Designation User ID. 587 */ 588 virtual void NotifyConfigurationChange(const AppExecFwk::Configuration &config, int32_t userId) override; 589 590 /** 591 * @brief Notify abilityms start resident process. 592 * @param bundleInfos resident process bundle infos. 593 */ 594 virtual void NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos) override; 595 596 /** 597 * @brief Notify abilityms app process OnRemoteDied 598 * @param abilityTokens abilities in died process. 599 */ 600 virtual void OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens) override; 601 602 /** 603 * @brief Notify abilityms app process pre cache 604 * @param pid process pid. 605 * @param userId userId Designation User ID. 606 */ 607 virtual void NotifyAppPreCache(int32_t pid, int32_t userId) override; 608 609 private: 610 std::mutex lock_; 611 bool isInit_ {false}; 612 std::weak_ptr<AppStateCallback> callback_; 613 std::unique_ptr<AppExecFwk::AppMgrClient> appMgrClient_; 614 AppAbilityState appAbilityState_ = AppAbilityState::ABILITY_STATE_UNDEFINED; 615 sptr<StartSpecifiedAbilityResponse> startSpecifiedAbilityResponse_; 616 }; 617 } // namespace AAFwk 618 } // namespace OHOS 619 #endif // OHOS_ABILITY_RUNTIME_APP_SCHEDULER_H 620