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