1 /* 2 * Copyright (c) 2021 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 FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_MGR_SERVICE_INNER_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_MGR_SERVICE_INNER_H 18 19 #include <list> 20 #include <map> 21 #include <vector> 22 23 #include "iremote_object.h" 24 #include "refbase.h" 25 26 #include "ability_info.h" 27 #include "appexecfwk_errors.h" 28 #include "app_death_recipient.h" 29 #include "app_mgr_constants.h" 30 #include "app_record_id.h" 31 #include "app_running_record.h" 32 #include "app_scheduler_interface.h" 33 #include "app_spawn_client.h" 34 #include "app_task_info.h" 35 #include "iapp_state_callback.h" 36 #include "app_process_manager.h" 37 #include "remote_client_manager.h" 38 #include "app_running_manager.h" 39 #include "record_query_result.h" 40 #include "running_process_info.h" 41 #include "bundle_info.h" 42 43 #include "process_optimizer_uba.h" 44 45 #include "ohos/aafwk/content/want.h" 46 47 namespace OHOS { 48 namespace AppExecFwk { 49 using OHOS::AAFwk::Want; 50 51 class AppMgrServiceInner : public std::enable_shared_from_this<AppMgrServiceInner> { 52 public: 53 AppMgrServiceInner(); 54 virtual ~AppMgrServiceInner(); 55 56 /** 57 * LoadAbility, load the ability that needed to be started. 58 * 59 * @param token, the unique identification to start the ability. 60 * @param preToken, the unique identification to call the ability. 61 * @param abilityInfo, the ability information. 62 * @param appInfo, the app information. 63 * 64 * @return 65 */ 66 virtual void LoadAbility(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken, 67 const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo); 68 69 /** 70 * TerminateAbility, terminate the token ability. 71 * 72 * @param token, he unique identification to terminate the ability. 73 * 74 * @return 75 */ 76 virtual void TerminateAbility(const sptr<IRemoteObject> &token); 77 78 /** 79 * UpdateAbilityState, update the ability status. 80 * 81 * @param token, the unique identification to update the ability. 82 * @param state, ability status that needs to be updated. 83 * 84 * @return 85 */ 86 virtual void UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state); 87 88 /** 89 * RegisterAppStateCallback, register the callback. 90 * 91 * @param callback, Ams register the callback. 92 * 93 * @return 94 */ 95 virtual void RegisterAppStateCallback(const sptr<IAppStateCallback> &callback); 96 97 /** 98 * StopAllProcess, Terminate all processes. 99 * 100 * @return 101 */ 102 virtual void StopAllProcess(); 103 104 /** 105 * AbilityBehaviorAnalysis, ability behavior analysis assistant process optimization. 106 * 107 * @param token, the unique identification to start the ability. 108 * @param preToken, the unique identification to call the ability. 109 * @param visibility, the visibility information about windows info. 110 * @param perceptibility, the Perceptibility information about windows info. 111 * @param connectionState, the service ability connection state. 112 * @return 113 */ 114 virtual void AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken, 115 const int32_t visibility, const int32_t perceptibility, const int32_t connectionState); 116 117 /** 118 * KillProcessByAbilityToken, kill the process by ability token. 119 * 120 * @param token, the unique identification to the ability. 121 * @return 122 */ 123 virtual void KillProcessByAbilityToken(const sptr<IRemoteObject> &token); 124 125 /** 126 * AttachApplication, get all the information needed to start the Application 127 * (data related to the Application ). 128 * 129 * @param app, information needed to start the Application. 130 * 131 * @return 132 */ 133 virtual void AttachApplication(const pid_t pid, const sptr<IAppScheduler> &app); 134 135 /** 136 * ApplicationForegrounded, set the application to Foreground State. 137 * 138 * @param recordId, a unique record that identifies this Application from others. 139 * 140 * @return 141 */ 142 virtual void ApplicationForegrounded(const int32_t recordId); 143 144 /** 145 * ApplicationBackgrounded, set the application to Backgrounded State. 146 * 147 * @param recordId, a unique record that identifies this Application from others. 148 * 149 * @return 150 */ 151 virtual void ApplicationBackgrounded(const int32_t recordId); 152 153 /** 154 * ApplicationTerminated, terminate the application. 155 * 156 * @param recordId, a unique record that identifies this Application from others. 157 * 158 * @return 159 */ 160 virtual void ApplicationTerminated(const int32_t recordId); 161 162 /** 163 * AbilityTerminated, terminate the ability. 164 * 165 * @param token, the unique identification to terminated the ability. 166 * 167 * @return 168 */ 169 virtual void AbilityTerminated(const sptr<IRemoteObject> &token); 170 171 /** 172 * KillApplication, kill the application. 173 * 174 * @param bundleName, bundle name in Application record. 175 * 176 * @return ERR_OK, return back success, others fail. 177 */ 178 virtual int32_t KillApplication(const std::string &bundleName); 179 180 /** 181 * ClearUpApplicationData, clear the application data. 182 * 183 * @param bundleName, bundle name in Application record. 184 * @param uid, app uid in Application record. 185 * @param pid, app pid in Application record. 186 * 187 * @return 188 */ 189 virtual void ClearUpApplicationData(const std::string &bundleName, const int32_t uid, const pid_t pid); 190 191 /** 192 * IsBackgroundRunningRestricted, Checks whether the process of this application is forbidden 193 * to run in the background. 194 * 195 * @param bundleName, bundle name in Application record. 196 * 197 * @return ERR_OK, return back success, others fail. 198 */ 199 virtual int32_t IsBackgroundRunningRestricted(const std::string &bundleName); 200 201 /** 202 * GetAllRunningProcesses, Obtains information about application processes that are running on the device. 203 * 204 * @param info, app name in Application record. 205 * 206 * @return ERR_OK ,return back success,others fail. 207 */ 208 virtual int32_t GetAllRunningProcesses(std::vector<RunningProcessInfo> &info); 209 210 // Get AppRunningRecord according to appInfo. Create if not exists. 211 // Create ability record if not exists and abilityInfo not null. 212 // Return AppRunningRecord pointer if success get or create. 213 // If error occurs, error code is in |result| 214 215 /** 216 * GetOrCreateAppRunningRecord, Get or create application record information. 217 * 218 * @param token, the unique identification to the ability. 219 * @param abilityInfo, ability information. 220 * @param appInfo, app information. 221 * @param processName, the app process name. 222 * @param uid, app uid in Application record. 223 * @param result, If error occurs, error code is in |result|. 224 * 225 * @return AppRunningRecord pointer if success get or create. 226 */ 227 std::shared_ptr<AppRunningRecord> GetOrCreateAppRunningRecord(const sptr<IRemoteObject> &token, 228 const std::shared_ptr<ApplicationInfo> &appInfo, const std::shared_ptr<AbilityInfo> &abilityInfo, 229 const std::string &processName, const int32_t uid, RecordQueryResult &result); 230 231 /** 232 * OnStop, Application management service stopped. 233 * 234 * @return 235 */ 236 void OnStop(); 237 238 // functions about appspawn 239 240 /** 241 * OpenAppSpawnConnection, Open connection with appspwan. 242 * 243 * @return ERR_OK ,return back success,others fail. 244 */ 245 virtual ErrCode OpenAppSpawnConnection(); 246 247 /** 248 * CloseAppSpawnConnection, Close connection with appspwan. 249 * 250 * @return 251 */ 252 virtual void CloseAppSpawnConnection() const; 253 254 /** 255 * QueryAppSpawnConnectionState, Query the connection status with appspwan. 256 * 257 * @return Returns the connection status with appspwan. 258 */ 259 virtual SpawnConnectionState QueryAppSpawnConnectionState() const; 260 261 /** 262 * SetAppSpawnClient, Setting the client to connect with appspwan. 263 * 264 * @param spawnClient, the client to connect with appspwan. 265 * 266 * @return 267 */ 268 void SetAppSpawnClient(std::shared_ptr<AppSpawnClient> spawnClient); 269 270 // Schedule launch application with specified |appRecord| 271 272 /** 273 * LaunchApplication, Notify application to launch application. 274 * 275 * @param appRecord, the application record. 276 * 277 * @return 278 */ 279 void LaunchApplication(const std::shared_ptr<AppRunningRecord> &appRecord); 280 281 /** 282 * GetRecordMap, Get all the ability information in the application record. 283 * 284 * @return all the ability information in the application record. 285 */ 286 const std::map<const int32_t, const std::shared_ptr<AppRunningRecord>> &GetRecordMap() const; 287 288 // functions to get special AppRunningRecord 289 /** 290 * GetAppRunningRecordByAppName, Get process record by application name. 291 * 292 * @param appName, the application name. 293 * 294 * @return process record. 295 */ 296 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAppName(const std::string &appName) const; 297 298 /** 299 * GetAppRunningRecordByProcessName, Get process record by application name and process name. 300 * 301 * @param appName, the application name. 302 * @param processName, the process name. 303 * 304 * @return process record. 305 */ 306 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByProcessName( 307 const std::string &appName, const std::string &processName) const; 308 309 /** 310 * GetAppRunningRecordByPid, Get process record by application pid. 311 * 312 * @param pid, the application pid. 313 * 314 * @return process record. 315 */ 316 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByPid(const pid_t pid) const; 317 318 /** 319 * GetAppRunningRecordByAbilityToken, Get process record by ability token. 320 * 321 * @param abilityToken, the ability token. 322 * 323 * @return process record. 324 */ 325 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAbilityToken(const sptr<IRemoteObject> &abilityToken) const; 326 327 /** 328 * GetAppRunningRecordByAppRecordId, Get process record by application id. 329 * 330 * @param recordId, the application id. 331 * 332 * @return process record. 333 */ 334 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAppRecordId(const int32_t recordId) const; 335 336 /** 337 * OnAbilityStateChanged, Call ability state change. 338 * 339 * @param ability, the ability info. 340 * @param state, the ability state. 341 * 342 * @return 343 */ 344 void OnAbilityStateChanged(const std::shared_ptr<AbilityRunningRecord> &ability, const AbilityState state); 345 346 /** 347 * GetRecentAppList, Get a list of recent applications. 348 * 349 * @return a list of recent applications. 350 */ 351 const std::list<const std::shared_ptr<AppTaskInfo>> &GetRecentAppList() const; 352 353 /** 354 * GetRecentAppList, Remove the corresponding latest application list data by applying the name. 355 * 356 * @param appName, the application name. 357 * @param processName, the process name. 358 * 359 * @return 360 */ 361 void RemoveAppFromRecentList(const std::string &appName, const std::string &processName); 362 363 /** 364 * GetRecentAppList, Clear recent application list. 365 * 366 * @return 367 */ 368 void ClearRecentAppList(); 369 370 /** 371 * OnRemoteDied, Equipment death notification. 372 * 373 * @param remote, Death client. 374 * @return 375 */ 376 void OnRemoteDied(const wptr<IRemoteObject> &remote); 377 378 /** 379 * AddAppDeathRecipient, Add monitoring death application record. 380 * 381 * @param pid, the application pid. 382 * @param appDeathRecipient, Application death recipient list. 383 * 384 * @return 385 */ 386 387 virtual void AddAppDeathRecipient(const pid_t pid, const sptr<AppDeathRecipient> &appDeathRecipient) const; 388 /** 389 * ProcessOptimizerInit, Process Optimizer init. 390 * 391 * @param 392 * @return ERR_OK, return back success, others fail. 393 */ 394 virtual int32_t ProcessOptimizerInit(); 395 396 /** 397 * OptimizerAbilityStateChanged, Optimizer processing ability state changes. 398 * 399 * @param ability, the ability info. 400 * @param state, the ability state before change. 401 * 402 * @return 403 */ 404 virtual void OptimizerAbilityStateChanged( 405 const std::shared_ptr<AbilityRunningRecord> &ability, const AbilityState state); 406 407 /** 408 * OptimizerAppStateChanged, Optimizer processing app state changes. 409 * 410 * @param appRecord, the app information. 411 * @param state, the app before change. 412 * @return 413 */ 414 virtual void OptimizerAppStateChanged( 415 const std::shared_ptr<AppRunningRecord> &appRecord, const ApplicationState state); 416 417 /** 418 * SetAppSuspendTimes, Setting the Freezing Time of APP Background. 419 * 420 * @param time, The timeout(second) recorded when the application enters the background . 421 * 422 * @return Success or Failure . 423 */ 424 void SetAppFreezingTime(int time); 425 426 /** 427 * GetAppFreezingTime, Getting the Freezing Time of APP Background. 428 * 429 * @param time, The timeout(second) recorded when the application enters the background . 430 * 431 * @return Success or Failure . 432 */ 433 void GetAppFreezingTime(int &time); 434 void HandleTimeOut(const InnerEvent::Pointer &event); 435 436 void SetEventHandler(const std::shared_ptr<AMSEventHandler> &handler); 437 438 void HandleAbilityAttachTimeOut(const sptr<IRemoteObject> &token); 439 440 /** 441 * Checks whether a specified permission has been granted to the process identified by pid and uid 442 * 443 * @param permission Indicates the permission to check. 444 * @param pid Indicates the ID of the process to check. 445 * @param uid Indicates the UID of the process to check. 446 * @param message Describe success or failure 447 * 448 * @return Returns ERR_OK on success, others on failure. 449 */ 450 int CompelVerifyPermission(const std::string &permission, int pid, int uid, std::string &message); 451 452 /** 453 * SuspendApplication, Application state changed. 454 * 455 * @param appRecord, the app information. 456 * @param state, the app state. 457 */ 458 void OnAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, const ApplicationState state); 459 460 private: 461 /** 462 * StartAbility, load the ability that needed to be started(Start on the basis of the original process). 463 * Start on a new boot process 464 * @param token, the unique identification to start the ability. 465 * @param preToken, the unique identification to call the ability. 466 * @param abilityInfo, the ability information. 467 * @param appInfo, the app information. 468 * 469 * @return 470 */ 471 void StartAbility(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken, 472 const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<AppRunningRecord> &appRecord); 473 474 /** 475 * UnsuspendApplication, Application process state switch to unsuspend. 476 * 477 * @param appRecord, the app information. 478 * 479 * @return 480 */ 481 void UnsuspendApplication(const std::shared_ptr<AppRunningRecord> &appRecord); 482 483 /** 484 * SuspendApplication, Application process state switch to suspend. 485 * 486 * @param appRecord, the app information. 487 * 488 * @return 489 */ 490 void SuspendApplication(const std::shared_ptr<AppRunningRecord> &appRecord); 491 492 /** 493 * LowMemoryApplicationAlert, Application low memory alert. 494 * 495 * @param appRecord, the app information. 496 * @param level, the app low memory level. 497 * 498 * @return 499 */ 500 void LowMemoryApplicationAlert( 501 const std::shared_ptr<AppRunningRecord> &appRecord, const CgroupManager::LowMemoryLevel level); 502 503 /** 504 * GetAbilityOwnerApp, Get the process record of ability. 505 * 506 * @param abilityRecord, the ability information. 507 * 508 * @return process record. 509 */ 510 std::shared_ptr<AppRunningRecord> GetAbilityOwnerApp( 511 const std::shared_ptr<AbilityRunningRecord> &abilityRecord) const; 512 513 /** 514 * GetAbilityRunningRecordByAbilityToken, Get the ability record by token. 515 * 516 * @param abilityToken, the ability token. 517 * 518 * @return ability record. 519 */ 520 std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecordByAbilityToken( 521 const sptr<IRemoteObject> &abilityToken) const; 522 523 /** 524 * StartProcess, load the ability that needed to be started(Start on a new boot process). 525 * 526 * @param appName, the app name. 527 * @param processName, the process name. 528 * @param appRecord, the app information. 529 * 530 * @return 531 */ 532 void StartProcess( 533 const std::string &appName, const std::string &processName, const std::shared_ptr<AppRunningRecord> &appRecord); 534 535 /** 536 * PushAppFront, Adjust the latest application record to the top level. 537 * 538 * @param recordId, the app record id. 539 * 540 * @return 541 */ 542 void PushAppFront(const int32_t recordId); 543 544 /** 545 * RemoveAppFromRecentListById, Remove the specified recent application record by application record id. 546 * 547 * @param recordId, the app record id. 548 * 549 * @return 550 */ 551 void RemoveAppFromRecentListById(const int32_t recordId); 552 553 /** 554 * AddAppToRecentList, Add application to recent list. 555 * 556 * @param appName, the app name. 557 * @param processName, the process name. 558 * @param pid, the app pid. 559 * @param recordId, the app record id. 560 * 561 * @return 562 */ 563 void AddAppToRecentList( 564 const std::string &appName, const std::string &processName, const pid_t pid, const int32_t recordId); 565 566 /** 567 * AddAppToRecentList, Get application task information through ID. 568 * 569 * @param recordId, the app record id. 570 * 571 * @return application task information. 572 */ 573 const std::shared_ptr<AppTaskInfo> GetAppTaskInfoById(const int32_t recordId) const; 574 575 /** 576 * KillProcessByPid, Kill process by PID. 577 * 578 * @param pid_t, the app record pid. 579 * 580 * @return ERR_OK, return back success,others fail. 581 */ 582 int32_t KillProcessByPid(const pid_t pid) const; 583 584 /** 585 * WaitForRemoteProcessExit, Wait for the process to exit normally. 586 * 587 * @param pids, process number collection to exit. 588 * @param startTime, execution process security exit start time. 589 * 590 * @return true, return back success,others fail. 591 */ 592 bool WaitForRemoteProcessExit(std::list<pid_t> &pids, const int64_t startTime); 593 594 /** 595 * GetAllPids, Get the corresponding pid collection. 596 * 597 * @param pids, process number collection to exit. 598 * 599 * @return true, return back success,others fail. 600 */ 601 bool GetAllPids(std::list<pid_t> &pids); 602 603 /** 604 * process_exist, Judge whether the process exists. 605 * 606 * @param pids, process number collection to exit. 607 * 608 * @return true, return back existed,others non-existent. 609 */ 610 bool process_exist(pid_t &pid); 611 612 /** 613 * CheckALLProcessExist, Determine whether all processes exist . 614 * 615 * @param pids, process number collection to exit. 616 * 617 * @return true, Returns that a process exists and all other processes do not exist. 618 */ 619 bool CheckALLProcessExist(std::list<pid_t> &pids); 620 621 /** 622 * SystemTimeMillis, Get system time. 623 * 624 * @return the system time. 625 */ 626 int64_t SystemTimeMillis(); 627 628 // Test add the bundle manager instance. 629 void SetBundleManager(sptr<IBundleMgr> bundleManager); 630 631 void HandleTerminateApplicationTimeOut(const int64_t eventId); 632 633 private: 634 std::vector<const sptr<IAppStateCallback>> appStateCallbacks_; 635 std::shared_ptr<AppProcessManager> appProcessManager_; 636 std::shared_ptr<RemoteClientManager> remoteClientManager_; 637 std::shared_ptr<AppRunningManager> appRunningManager_; 638 std::shared_ptr<ProcessOptimizerUBA> processOptimizerUBA_; 639 std::shared_ptr<AMSEventHandler> eventHandler_; 640 std::mutex serviceLock_; 641 }; 642 } // namespace AppExecFwk 643 } // namespace OHOS 644 #endif // FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_MGR_SERVICE_INNER_H 645