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_MGR_SERVICE_INNER_H 17 #define OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_INNER_H 18 19 #include <list> 20 #include <map> 21 #include <mutex> 22 #include <regex> 23 #include <unordered_map> 24 #include <unordered_set> 25 #include <vector> 26 27 #include "ability_debug_response_interface.h" 28 #include "ability_foreground_state_observer_interface.h" 29 #include "ability_info.h" 30 #include "advanced_security_mode_manager.h" 31 #include "app_death_recipient.h" 32 #include "app_debug_listener_interface.h" 33 #include "app_debug_manager.h" 34 #include "app_foreground_state_observer_interface.h" 35 #include "app_malloc_info.h" 36 #include "app_mgr_constants.h" 37 #include "app_mgr_event.h" 38 #include "app_preloader.h" 39 #include "app_record_id.h" 40 #include "app_running_manager.h" 41 #include "app_running_record.h" 42 #include "app_running_status_listener_interface.h" 43 #include "app_running_status_module.h" 44 #include "app_scheduler_interface.h" 45 #include "app_spawn_client.h" 46 #include "appexecfwk_errors.h" 47 #include "bundle_info.h" 48 #include "bundle_mgr_helper.h" 49 #include "child_process_info.h" 50 #include "child_process_request.h" 51 #include "cpp/mutex.h" 52 #include "event_report.h" 53 #include "fault_data.h" 54 #include "hisysevent.h" 55 #include "iapp_state_callback.h" 56 #include "iapplication_state_observer.h" 57 #include "iconfiguration_observer.h" 58 #include "iremote_object.h" 59 #include "irender_state_observer.h" 60 #include "istart_specified_ability_response.h" 61 #include "record_query_result.h" 62 #include "refbase.h" 63 #include "remote_client_manager.h" 64 #include "render_process_info.h" 65 #include "running_process_info.h" 66 #include "shared/base_shared_bundle_info.h" 67 #include "task_handler_wrap.h" 68 #include "want.h" 69 #include "app_jsheap_mem_info.h" 70 #include "running_multi_info.h" 71 #include "multi_user_config_mgr.h" 72 73 namespace OHOS { 74 namespace AbilityRuntime { 75 struct LoadParam; 76 } 77 namespace Rosen { 78 class WindowVisibilityInfo; 79 class FocusChangeInfo; 80 } 81 namespace AppExecFwk { 82 using OHOS::AAFwk::Want; 83 class WindowFocusChangedListener; 84 class WindowVisibilityChangedListener; 85 using LoadAbilityTaskFunc = std::function<void()>; 86 constexpr int32_t BASE_USER_RANGE = 200000; 87 88 class AppMgrServiceInner : public std::enable_shared_from_this<AppMgrServiceInner> { 89 public: 90 struct ConfigurationObserverWithUserId { 91 sptr<IConfigurationObserver> observer; 92 int32_t userId = -1; 93 }; 94 struct AppStateCallbackWithUserId { 95 sptr<IAppStateCallback> callback; 96 int32_t userId = -1; 97 }; 98 99 AppMgrServiceInner(); 100 virtual ~AppMgrServiceInner(); 101 102 /** 103 * Initialize the object. 104 */ 105 void Init(); 106 /** 107 * LoadAbility, load the ability that needed to be started. 108 * 109 * @param token, the unique identification to start the ability. 110 * @param preToken, the unique identification to call the ability. 111 * @param abilityInfo, the ability information. 112 * @param appInfo, the app information. 113 * @param want the ability want. 114 * 115 * @return 116 */ 117 virtual void LoadAbility(std::shared_ptr<AbilityInfo> abilityInfo, std::shared_ptr<ApplicationInfo> appInfo, 118 std::shared_ptr<AAFwk::Want> want, std::shared_ptr<AbilityRuntime::LoadParam> loadParam); 119 120 /** 121 * TerminateAbility, terminate the token ability. 122 * 123 * @param token, he unique identification to terminate the ability. 124 * @param clearMissionFlag, indicates whether terminate the ability when clearMission. 125 * @return 126 */ 127 virtual void TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag); 128 129 /** 130 * UpdateAbilityState, update the ability status. 131 * 132 * @param token, the unique identification to update the ability. 133 * @param state, ability status that needs to be updated. 134 * 135 * @return 136 */ 137 virtual void UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state); 138 139 /** 140 * UpdateExtensionState, call UpdateExtensionState() through the proxy object, update the extension status. 141 * 142 * @param token, the unique identification to update the extension. 143 * @param state, extension status that needs to be updated. 144 */ 145 virtual void UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state); 146 147 /** 148 * StateChangedNotifyObserver, Call ability state change. 149 * 150 * @param ability, the ability info. 151 * @param state, the ability state. 152 * 153 * @return 154 */ 155 void StateChangedNotifyObserver( 156 const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged); 157 158 /** 159 * RegisterAppStateCallback, register the callback. 160 * 161 * @param callback, Ams register the callback. 162 * 163 * @return 164 */ 165 virtual void RegisterAppStateCallback(const sptr<IAppStateCallback> &callback); 166 void RemoveDeadAppStateCallback(const wptr<IRemoteObject> &remote); 167 168 /** 169 * AbilityBehaviorAnalysis, ability behavior analysis assistant process optimization. 170 * 171 * @param token, the unique identification to start the ability. 172 * @param preToken, the unique identification to call the ability. 173 * @param visibility, the visibility information about windows info. 174 * @param perceptibility, the Perceptibility information about windows info. 175 * @param connectionState, the service ability connection state. 176 * @return 177 */ 178 virtual void AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken, 179 const int32_t visibility, const int32_t perceptibility, const int32_t connectionState); 180 181 /** 182 * KillProcessByAbilityToken, kill the process by ability token. 183 * 184 * @param token, the unique identification to the ability. 185 * @return 186 */ 187 virtual void KillProcessByAbilityToken(const sptr<IRemoteObject> &token); 188 189 /** 190 * KillProcessesByUserId, kill the processes by userId. 191 * 192 * @param userId, the user id. 193 * @return 194 */ 195 virtual void KillProcessesByUserId(int32_t userId); 196 197 virtual void KillProcessesByPids(std::vector<int32_t> &pids); 198 199 /** 200 * KillProcessesInBatch, kill processes in batch; 201 * the killed bundle won't be started by the watcher. 202 * 203 * @param pids, the pid list of processes are going to be killed. 204 * @return ERR_OK, return back success, others fail. 205 */ 206 virtual int32_t KillProcessesInBatch(const std::vector<int32_t> &pids); 207 208 virtual void AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken); 209 210 /** 211 * AttachApplication, get all the information needed to start the Application 212 * (data related to the Application ). 213 * 214 * @param appScheduler, information needed to start the Application. 215 * 216 * @return 217 */ 218 virtual void AttachApplication(const pid_t pid, const sptr<IAppScheduler> &appScheduler); 219 220 /** 221 * Preload application. 222 * 223 * @param bundleName The bundle name of the application to preload. 224 * @param userId Indicates the user identification. 225 * @param preloadMode Preload application mode. 226 * @param appIndex The index of application clone. 227 * @return Returns ERR_OK on success, others on failure. 228 */ 229 virtual int32_t PreloadApplication(const std::string &bundleName, int32_t userId, 230 AppExecFwk::PreloadMode preloadMode, int32_t appIndex); 231 232 /** 233 * ApplicationForegrounded, set the application to Foreground State. 234 * 235 * @param recordId, a unique record that identifies this Application from others. 236 * 237 * @return 238 */ 239 virtual void ApplicationForegrounded(const int32_t recordId); 240 241 /** 242 * ApplicationBackgrounded, set the application to Backgrounded State. 243 * 244 * @param recordId, a unique record that identifies this Application from others. 245 * 246 * @return 247 */ 248 virtual void ApplicationBackgrounded(const int32_t recordId); 249 250 /** 251 * ApplicationTerminated, terminate the application. 252 * 253 * @param recordId, a unique record that identifies this Application from others. 254 * 255 * @return 256 */ 257 virtual void ApplicationTerminated(const int32_t recordId); 258 259 /** 260 * AbilityTerminated, terminate the ability. 261 * 262 * @param token, the unique identification to terminated the ability. 263 * 264 * @return 265 */ 266 virtual void AbilityTerminated(const sptr<IRemoteObject> &token); 267 268 /** 269 * UpdateApplicationInfoInstalled, update the application info after new module installed. 270 * 271 * @param bundleName, bundle name in Application record. 272 * @param uid, uid. 273 * @return ERR_OK, return back success, others fail. 274 */ 275 virtual int32_t UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid); 276 277 /** 278 * KillApplication, kill the application. 279 * 280 * @param bundleName, bundle name in Application record. 281 * 282 * @return ERR_OK, return back success, others fail. 283 */ 284 virtual int32_t KillApplication(const std::string &bundleName, const bool clearPageStack = true); 285 286 /** 287 * ForceKillApplication, force kill the application. 288 * 289 * @param bundleName, bundle name in Application record. 290 * @param userId, userId. 291 * @param appIndex, appIndex. 292 * @return ERR_OK, return back success, others fail. 293 */ 294 virtual int32_t ForceKillApplication(const std::string &bundleName, const int userId = -1, 295 const int appIndex = 0); 296 297 /** 298 * KillProcessesByAccessTokenId. 299 * 300 * @param accessTokenId, accessTokenId. 301 * @return ERR_OK, return back success, others fail. 302 */ 303 virtual int32_t KillProcessesByAccessTokenId(const uint32_t accessTokenId); 304 305 /** 306 * KillApplicationByUid, call KillApplicationByUid() through proxy object, kill the application. 307 * 308 * @param bundleName, bundle name in Application record. 309 * @param uid, uid. 310 * @param reason, caller function name. 311 * @return ERR_OK, return back success, others fail. 312 */ 313 virtual int32_t KillApplicationByUid(const std::string &bundleName, const int uid, 314 const std::string& reason = "KillApplicationByUid"); 315 316 virtual int32_t KillApplicationSelf(const bool clearPageStack = false, 317 const std::string& reason = "KillApplicationSelf"); 318 319 /** 320 * KillApplicationByUserId, kill the application by user ID. 321 * 322 * @param bundleName, bundle name in Application record. 323 * @param appCloneIndex the app clone id. 324 * @param userId, user ID. 325 * @param reason, caller function name. 326 * 327 * @return ERR_OK, return back success, others fail. 328 */ 329 virtual int32_t KillApplicationByUserId( 330 const std::string &bundleName, int32_t appCloneIndex, int userId, const bool clearPageStack = false, 331 const std::string& reason = "KillApplicationByUserId"); 332 333 /** 334 * ClearUpApplicationData, clear the application data. 335 * 336 * @param bundleName, bundle name in Application record. 337 * @param callerUid, app uid in Application record. 338 * @param callerPid, app pid in Application record. 339 * @param appCloneIndex the app clone id. 340 * @param userId the user id 341 * @return ERR_OK, return back success, others fail. 342 */ 343 virtual int32_t ClearUpApplicationData(const std::string &bundleName, 344 int32_t callerUid, pid_t callerPid, int32_t appCloneIndex, int32_t userId = -1); 345 346 /** 347 * ClearUpApplicationDataBySelf, clear the application data. 348 * 349 * @param callerUid, app uid in Application record. 350 * @param callerPid, app pid in Application record. 351 * @param userId, user ID. 352 * 353 * @return ERR_OK, return back success, others fail. 354 */ 355 virtual int32_t ClearUpApplicationDataBySelf(int32_t callerUid, pid_t callerPid, int32_t userId = -1); 356 357 /** 358 * GetAllRunningProcesses, Obtains information about application processes that are running on the device. 359 * 360 * @param info, app name in Application record. 361 * 362 * @return ERR_OK ,return back success,others fail. 363 */ 364 virtual int32_t GetAllRunningProcesses(std::vector<RunningProcessInfo> &info); 365 366 /** 367 * GetRunningMultiAppInfoByBundleName, call GetRunningMultiAppInfoByBundleName through proxy project. 368 * Obtains information about TwinApp that are running on the device. 369 * 370 * @param bundleName, input. 371 * @param info, output multiapp information. 372 * @return void. 373 */ 374 virtual int32_t GetRunningMultiAppInfoByBundleName(const std::string &bundleName, 375 RunningMultiAppInfo &info); 376 377 /** 378 * GetAllRunningInstanceKeysBySelf, call GetAllRunningInstanceKeysBySelf() through proxy project. 379 * Obtains running instance keys of multi-instance app that are running on the device. 380 * 381 * @param instanceKeys, output instance keys of the multi-instance app. 382 * @return ERR_OK ,return back success,others fail. 383 */ 384 virtual int32_t GetAllRunningInstanceKeysBySelf(std::vector<std::string> &instanceKeys); 385 386 /** 387 * GetAllRunningInstanceKeysByBundleName, call GetAllRunningInstanceKeysByBundleName() through proxy project. 388 * Obtains running isntance keys of multi-instance app that are running on the device. 389 * 390 * @param bundlename, bundle name in Application record. 391 * @param instanceKeys, output instance keys of the multi-insatnce app. 392 * @param userId, user id. 393 * @return ERR_OK ,return back success,others fail. 394 */ 395 virtual int32_t GetAllRunningInstanceKeysByBundleName(const std::string &bundleName, 396 std::vector<std::string> &instanceKeys, int32_t userId = -1); 397 398 /** 399 * GetRunningProcessesByBundleType, Obtains information about application processes by bundle type. 400 * 401 * @param bundleType, the bundle type of the application process 402 * @param info, app name in Application record. 403 * 404 * @return ERR_OK ,return back success,others fail. 405 */ 406 virtual int32_t GetRunningProcessesByBundleType(BundleType bundleType, std::vector<RunningProcessInfo> &info); 407 408 /** 409 * GetProcessRunningInfosByUserId, Obtains information about application processes that are running on the device. 410 * 411 * @param info, app name in Application record. 412 * @param userId, userId. 413 * 414 * @return ERR_OK ,return back success,others fail. 415 */ 416 virtual int32_t GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId); 417 418 /** 419 * GetProcessRunningInformation, Obtains information about current application process 420 * which is running on the device. 421 * @param info, app name in Application record. 422 * 423 * @return ERR_OK ,return back success,others fail. 424 */ 425 virtual int32_t GetProcessRunningInformation(RunningProcessInfo &info); 426 427 /** 428 * GetAllRenderProcesses, Obtains information about render processes that are running on the device. 429 * 430 * @param info, render process record. 431 * 432 * @return ERR_OK, return back success, others fail. 433 */ 434 virtual int32_t GetAllRenderProcesses(std::vector<RenderProcessInfo> &info); 435 436 /** 437 * GetAllChildrenProcesses, call GetAllChildrenProcesses() through proxy project. 438 * Obtains information about children processes that are running on the device. 439 * 440 * @param info, child process info. 441 * @return ERR_OK, return back success, others fail. 442 */ 443 virtual int GetAllChildrenProcesses(std::vector<ChildProcessInfo> &info); 444 445 /** 446 * NotifyMemoryLevel, Notify applications background the current memory level. 447 * 448 * @param level, current memory level. 449 * 450 * @return ERR_OK ,return back success,others fail. 451 */ 452 virtual int32_t NotifyMemoryLevel(int32_t level); 453 454 /** 455 * NotifyProcMemoryLevel, Notify applications background the current memory level. 456 * 457 * @param procLevelMap , <pid_t, MemoryLevel>. 458 * 459 * @return ERR_OK ,return back success,others fail. 460 */ 461 virtual int32_t NotifyProcMemoryLevel(const std::map<pid_t, MemoryLevel> &procLevelMap); 462 463 /** 464 * DumpHeapMemory, get the application's memory info. 465 * Get the application's memory allocation info. 466 * 467 * @param pid, pid input. 468 * @param mallocInfo, dynamic storage information output. 469 * 470 * @return ERR_OK, return back success,others fail. 471 */ 472 virtual int32_t DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo); 473 474 /** 475 * DumpJsHeapMemory, call DumpJsHeapMemory() through proxy project. 476 * triggerGC and dump the application's jsheap memory info. 477 * 478 * @param info, pid, tid, needGc, needSnapshot 479 * @return Returns ERR_OK on success, others on failure. 480 */ 481 virtual int32_t DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info); 482 483 /** 484 * @brief Check whether the shared bundle is running. 485 * 486 * @param bundleName Shared bundle name. 487 * @param versionCode Shared bundle version code. 488 * @return Returns the shared bundle running result. The result is true if running, false otherwise. 489 */ 490 virtual bool IsSharedBundleRunning(const std::string &bundleName, uint32_t versionCode); 491 492 /** 493 * Check whether the bundle is running. 494 * 495 * @param bundleName Indicates the bundle name of the bundle. 496 * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise. 497 * @return Return ERR_OK if success, others fail. 498 */ 499 int32_t IsApplicationRunning(const std::string &bundleName, bool &isRunning); 500 501 /** 502 * Check whether the bundle is running. 503 * 504 * @param bundleName Indicates the bundle name of the bundle. 505 * @param appCloneIndex the appindex of the bundle. 506 * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise. 507 * @return Return ERR_OK if success, others fail. 508 */ 509 int32_t IsAppRunning(const std::string &bundleName, int32_t appCloneIndex, bool &isRunning); 510 511 int32_t StartNativeProcessForDebugger(const AAFwk::Want &want); 512 513 std::shared_ptr<AppRunningRecord> CreateAppRunningRecord( 514 sptr<IRemoteObject> token, 515 sptr<IRemoteObject> preToken, 516 std::shared_ptr<ApplicationInfo> appInfo, 517 std::shared_ptr<AbilityInfo> abilityInfo, 518 const std::string &processName, 519 const BundleInfo &bundleInfo, 520 const HapModuleInfo &hapModuleInfo, 521 std::shared_ptr<AAFwk::Want> want, 522 int32_t abilityRecordId); 523 524 /** 525 * OnStop, Application management service stopped. 526 * 527 * @return 528 */ 529 void OnStop(); 530 531 /** 532 * OpenAppSpawnConnection, Open connection with appspawn. 533 * 534 * @return ERR_OK ,return back success,others fail. 535 */ 536 virtual ErrCode OpenAppSpawnConnection(); 537 538 /** 539 * CloseAppSpawnConnection, Close connection with appspawn. 540 * 541 * @return 542 */ 543 virtual void CloseAppSpawnConnection() const; 544 545 /** 546 * QueryAppSpawnConnectionState, Query the connection status with appspawn. 547 * 548 * @return Returns the connection status with appspawn. 549 */ 550 virtual SpawnConnectionState QueryAppSpawnConnectionState() const; 551 552 /** 553 * SetAppSpawnClient, Setting the client to connect with appspawn. 554 * 555 * @param spawnClient, the client to connect with appspawn. 556 * 557 * @return 558 */ 559 void SetAppSpawnClient(std::shared_ptr<AppSpawnClient> spawnClient); 560 561 // Schedule launch application with specified |appRecord| 562 563 /** 564 * LaunchApplication, Notify application to launch application. 565 * 566 * @param appRecord, the application record. 567 * 568 * @return 569 */ 570 void LaunchApplication(const std::shared_ptr<AppRunningRecord> &appRecord); 571 572 /** 573 * LaunchApplicationExt, to solve the nbnc of method over 50. 574 * 575 * @param appRecord, the application record. 576 * 577 * @return 578 */ 579 void LaunchApplicationExt(const std::shared_ptr<AppRunningRecord> &appRecord); 580 581 /** 582 * Notice of AddAbilityStageInfo() 583 * 584 * @param recordId, the application record. 585 */ 586 virtual void AddAbilityStageDone(const int32_t recordId); 587 588 /** 589 * GetAppRunningRecordByPid, Get process record by application pid. 590 * 591 * @param pid, the application pid. 592 * 593 * @return process record. 594 */ 595 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByPid(const pid_t pid) const; 596 597 /** 598 * GetAppRunningRecordByAbilityToken, Get process record by ability token. 599 * 600 * @param abilityToken, the ability token. 601 * 602 * @return process record. 603 */ 604 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAbilityToken(const sptr<IRemoteObject> &abilityToken) const; 605 606 /** 607 * GetTerminatingAppRunningRecord, Get process record by ability token. 608 * 609 * @param abilityToken, the ability token. 610 * 611 * @return process record. 612 */ 613 std::shared_ptr<AppRunningRecord> GetTerminatingAppRunningRecord(const sptr<IRemoteObject> &token) const; 614 615 /** 616 * GetAppRunningRecordByAppRecordId, Get process record by application id. 617 * 618 * @param recordId, the application id. 619 * 620 * @return process record. 621 */ 622 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAppRecordId(const int32_t recordId) const; 623 624 /** 625 * OnAbilityStateChanged, Call ability state change. 626 * 627 * @param ability, the ability info. 628 * @param state, the ability state. 629 * 630 * @return 631 */ 632 void OnAbilityStateChanged(const std::shared_ptr<AbilityRunningRecord> &ability, const AbilityState state); 633 634 /** 635 * OnRemoteDied, Equipment death notification. 636 * 637 * @param remote, Death client. 638 * @param isRenderProcess is render process died. 639 * @param isChildProcess is child process died. 640 * @return 641 */ 642 void OnRemoteDied(const wptr<IRemoteObject> &remote, bool isRenderProcess = false, bool isChildProcess = false); 643 644 void HandleTimeOut(const AAFwk::EventWrap &event); 645 DecreaseWillKillPidsNum()646 void DecreaseWillKillPidsNum() 647 { 648 willKillPidsNum_ -= 1; 649 } 650 SetTaskHandler(std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler)651 void SetTaskHandler(std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler) 652 { 653 taskHandler_ = taskHandler; 654 } 655 SetEventHandler(const std::shared_ptr<AMSEventHandler> & eventHandler)656 void SetEventHandler(const std::shared_ptr<AMSEventHandler> &eventHandler) 657 { 658 eventHandler_ = eventHandler; 659 } 660 661 void HandleAbilityAttachTimeOut(const sptr<IRemoteObject> &token); 662 663 void PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag = false); 664 665 void OnAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, const ApplicationState state, 666 bool needNotifyApp, bool isFromWindowFocusChanged); 667 668 void OnAppStarted(const std::shared_ptr<AppRunningRecord> &appRecord); 669 670 void OnAppStopped(const std::shared_ptr<AppRunningRecord> &appRecord); 671 672 void GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info); 673 674 int32_t GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info) const; 675 676 /** 677 * Set AbilityForegroundingFlag of an app-record to true. 678 * 679 * @param pid, pid. 680 * 681 */ 682 void SetAbilityForegroundingFlagToAppRecord(const pid_t pid) const; 683 684 /** 685 * UpdateConfiguration, ANotify application update system environment changes. 686 * 687 * @param config, System environment change parameters. 688 * @return Returns ERR_OK on success, others on failure. 689 */ 690 int32_t UpdateConfiguration(const Configuration &config, const int32_t userId = -1); 691 692 int32_t UpdateConfigurationByBundleName(const Configuration &config, const std::string &name); 693 694 std::shared_ptr<AppExecFwk::Configuration> GetConfiguration(); 695 696 int32_t RegisterConfigurationObserver(const sptr<IConfigurationObserver>& observer); 697 698 int32_t UnregisterConfigurationObserver(const sptr<IConfigurationObserver>& observer); 699 700 /** 701 * Start empty process 702 */ 703 void LoadResidentProcess(const std::vector<BundleInfo> &infos); 704 705 void StartResidentProcess(const std::vector<BundleInfo> &infos, int restartCount, bool isEmptyKeepAliveApp); 706 707 bool CheckRemoteClient(); 708 709 /** 710 * Register application or process state observer. 711 * @param observer, ability token. 712 * @return Returns ERR_OK on success, others on failure. 713 */ 714 int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer, 715 const std::vector<std::string> &bundleNameList = {}); 716 717 /** 718 * Unregister application or process state observer. 719 * @param observer, ability token. 720 * @return Returns ERR_OK on success, others on failure. 721 */ 722 int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer); 723 724 /** 725 * Register application or process state observer. 726 * @param observer, Is ability foreground state observer 727 * @return Returns ERR_OK on success, others on failure. 728 */ 729 int32_t RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer); 730 731 /** 732 * Unregister application or process state observer. 733 * @param observer, Is ability foreground state observer 734 * @return Returns ERR_OK on success, others on failure. 735 */ 736 int32_t UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer); 737 738 /** 739 * Get Foreground Applications. 740 * 741 * @return Foreground Applications. 742 */ 743 int32_t GetForegroundApplications(std::vector<AppStateData> &list); 744 745 /** 746 * Start user test process. 747 * @param want, want object. 748 * @param observer, test observer remote object. 749 * @param bundleInfo, bundle info. 750 * @param userId the user id. 751 * @return Returns ERR_OK on success, others on failure. 752 */ 753 int StartUserTestProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer, 754 const AppExecFwk::BundleInfo &bundleInfo, int32_t userId); 755 756 /** 757 * @brief Finish user test. 758 * @param msg user test message. 759 * @param resultCode user test result Code. 760 * @param bundleName user test bundleName. 761 * @param pid the user test process id. 762 * 763 * @return Returns ERR_OK on success, others on failure. 764 */ 765 int FinishUserTest( 766 const std::string &msg, const int64_t &resultCode, const std::string &bundleName, const pid_t &pid); 767 768 void StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo, 769 int32_t requestId = 0); 770 771 void StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo, 772 int32_t requestId = 0); 773 774 void RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response); 775 776 void ScheduleAcceptWantDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag); 777 778 void ScheduleNewProcessRequestDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag); 779 780 /** 781 * Get the token of ability records by process ID. 782 * 783 * @param pid The process id. 784 * @param tokens The token of ability records. 785 * @return Returns true on success, others on failure. 786 */ 787 int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens); 788 789 virtual int32_t PreStartNWebSpawnProcess(const pid_t hostPid); 790 791 virtual int32_t StartRenderProcess(const pid_t hostPid, 792 const std::string &renderParam, 793 int32_t ipcFd, int32_t sharedFd, 794 int32_t crashFd, pid_t &renderPid, bool isGPU = false); 795 796 virtual void AttachRenderProcess(const pid_t pid, const sptr<IRenderScheduler> &scheduler); 797 798 virtual int GetRenderProcessTerminationStatus(pid_t renderPid, int &status); 799 800 int VerifyKillProcessPermission(const sptr<IRemoteObject> &token) const; 801 802 int VerifyAccountPermission(const std::string &permissionName, const int userId) const; 803 804 int VerifyRequestPermission() const; 805 806 void ClearAppRunningData(const std::shared_ptr<AppRunningRecord> &appRecord, bool containsApp); 807 808 void TerminateApplication(const std::shared_ptr<AppRunningRecord> &appRecord); 809 810 int GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug); 811 812 int32_t NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg); 813 814 /** 815 * Notify application status. 816 * 817 * @param bundleName Indicates the name of the bundle. 818 * @param eventData Indicates the event defined by CommonEventSupport 819 * 820 * @return 821 */ 822 void NotifyAppStatus(const std::string &bundleName, const std::string &eventData); 823 824 int32_t KillProcessByPid(const pid_t pid, const std::string& reason = "foundation"); 825 826 bool GetAppRunningStateByBundleName(const std::string &bundleName); 827 828 int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback); 829 830 int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback); 831 832 int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback); 833 834 void HandleFocused(const sptr<OHOS::Rosen::FocusChangeInfo> &focusChangeInfo); 835 void HandleUnfocused(const sptr<OHOS::Rosen::FocusChangeInfo> &focusChangeInfo); 836 837 /** 838 * Handle window visibility changed. 839 */ 840 void HandleWindowVisibilityChanged( 841 const std::vector<sptr<OHOS::Rosen::WindowVisibilityInfo>> &windowVisibilityInfos); 842 843 /** 844 * Set the current userId, only used by abilityMgr. 845 * 846 * @param userId the user id. 847 * 848 * @return 849 */ 850 void SetCurrentUserId(const int32_t userId); 851 852 /** 853 * Set enable start process flag by userId 854 * @param userId the user id. 855 * @param enableStartProcess enable start process. 856 * @return 857 */ 858 void SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess); 859 860 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE 861 int32_t SetContinuousTaskProcess(int32_t pid, bool isContinuousTask); 862 #endif 863 864 /** 865 * Get bundleName by pid. 866 * 867 * @param pid process id. 868 * @param bundleName Output parameters, return bundleName. 869 * @param uid Output parameters, return userId. 870 * @return Returns ERR_OK on success, others on failure. 871 */ 872 int32_t GetBundleNameByPid(const int32_t pid, std::string &bundleName, int32_t &uid); 873 874 /** 875 * Notify Fault Data 876 * 877 * @param faultData the fault data. 878 * @return Returns ERR_OK on success, others on failure. 879 */ 880 int32_t NotifyAppFault(const FaultData &faultData); 881 882 /** 883 * Transformed Notify Fault Data 884 * 885 * @param faultData Transformed the fault data. 886 * @return Returns ERR_OK on success, others on failure. 887 */ 888 889 int32_t TransformedNotifyAppFault(const AppFaultDataBySA &faultData); 890 891 /** 892 * Notify Fault Data By SA 893 * 894 * @param faultData the fault data notified by SA. 895 * @return Returns ERR_OK on success, others on failure. 896 */ 897 int32_t NotifyAppFaultBySA(const AppFaultDataBySA &faultData); 898 899 /** 900 * Set Appfreeze Detect Filter 901 * 902 * @param pid the process pid. 903 * @return Returns true on success, others on failure. 904 */ 905 bool SetAppFreezeFilter(int32_t pid); 906 907 /** 908 * get memorySize by pid. 909 * 910 * @param pid process id. 911 * @param memorySize Output parameters, return memorySize in KB. 912 * @return Returns ERR_OK on success, others on failure. 913 */ 914 virtual int32_t GetProcessMemoryByPid(const int32_t pid, int32_t &memorySize); 915 916 /** 917 * get application processes information list by bundleName. 918 * 919 * @param bundleName Bundle name. 920 * @param userId user Id in Application record. 921 * @param info Output parameters, return running process info list. 922 * @return Returns ERR_OK on success, others on failure. 923 */ 924 virtual int32_t GetRunningProcessInformation( 925 const std::string &bundleName, int32_t userId, std::vector<RunningProcessInfo> &info); 926 927 /** 928 * init focusListener. 929 * 930 * @return 931 */ 932 void InitFocusListener(); 933 934 /** 935 * free focusListener. 936 * 937 * @return 938 */ 939 void FreeFocusListener(); 940 941 /** 942 * Init window visibility changed listener. 943 */ 944 void InitWindowVisibilityChangedListener(); 945 946 /** 947 * Free window visibility changed listener. 948 */ 949 void FreeWindowVisibilityChangedListener(); 950 951 /* 952 * @brief Notify NativeEngine GC of status change. 953 * 954 * @param state GC state 955 * @param pid pid 956 * 957 * @return Is the status change completed. 958 */ 959 int32_t ChangeAppGcState(pid_t pid, int32_t state); 960 961 /** 962 * @brief Register app debug listener. 963 * @param listener App debug listener. 964 * @return Returns ERR_OK on success, others on failure. 965 */ 966 int32_t RegisterAppDebugListener(const sptr<IAppDebugListener> &listener); 967 968 /** 969 * @brief Unregister app debug listener. 970 * @param listener App debug listener. 971 * @return Returns ERR_OK on success, others on failure. 972 */ 973 int32_t UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener); 974 975 /** 976 * @brief Attach app debug. 977 * @param bundleName The application bundle name. 978 * @return Returns ERR_OK on success, others on failure. 979 */ 980 int32_t AttachAppDebug(const std::string &bundleName); 981 982 /** 983 * @brief Detach app debug. 984 * @param bundleName The application bundle name. 985 * @return Returns ERR_OK on success, others on failure. 986 */ 987 int32_t DetachAppDebug(const std::string &bundleName); 988 989 /** 990 * @brief Set app waiting debug mode. 991 * @param bundleName The application bundle name. 992 * @param isPersist The persist flag. 993 * @return Returns ERR_OK on success, others on failure. 994 */ 995 int32_t SetAppWaitingDebug(const std::string &bundleName, bool isPersist); 996 997 /** 998 * @brief Cancel app waiting debug mode. 999 * @return Returns ERR_OK on success, others on failure. 1000 */ 1001 int32_t CancelAppWaitingDebug(); 1002 1003 /** 1004 * @brief Get waiting debug mode application. 1005 * @param bundleNameList The application bundle name list. 1006 * @return Returns ERR_OK on success, others on failure. 1007 */ 1008 int32_t GetWaitingDebugApp(std::vector<std::string> &bundleNameList); 1009 1010 /** 1011 * @brief Determine whether it is a waiting debug application based on the bundle name. 1012 * @return Returns true if it is a waiting debug application, otherwise it returns false. 1013 */ 1014 bool IsWaitingDebugApp(const std::string &bundleName); 1015 1016 /** 1017 * @brief Clear non persist waiting debug flag. 1018 */ 1019 void ClearNonPersistWaitingDebugFlag(); 1020 1021 /** 1022 * @brief Registering ability debug mode response. 1023 * @param response Response for ability debug object. 1024 * @return Returns ERR_OK on success, others on failure. 1025 */ 1026 int32_t RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response); 1027 1028 /** 1029 * @brief Determine whether it is an attachment debug application based on the bundle name. 1030 * @param bundleName The application bundle name. 1031 * @return Returns true if it is an attach debug application, otherwise it returns false. 1032 */ 1033 bool IsAttachDebug(const std::string &bundleName); 1034 1035 /** 1036 * @brief Notify AbilityManagerService the page show. 1037 * @param token Ability identify. 1038 * @param pageStateData The data of ability's page state. 1039 * @return Returns ERR_OK on success, others on failure. 1040 */ 1041 virtual int32_t NotifyPageShow(const sptr<IRemoteObject> &token, const PageStateData &pageStateData); 1042 1043 /** 1044 * @brief Notify AbilityManagerService the page hide. 1045 * @param token Ability identify. 1046 * @param pageStateData The data of ability's page state. 1047 * @return Returns ERR_OK on success, others on failure. 1048 */ 1049 virtual int32_t NotifyPageHide(const sptr<IRemoteObject> &token, const PageStateData &pageStateData); 1050 1051 /** 1052 * Register appRunning status listener. 1053 * 1054 * @param listener Running status listener. 1055 * @return Returns ERR_OK on success, others on failure. 1056 */ 1057 int32_t RegisterAppRunningStatusListener(const sptr<IRemoteObject> &listener); 1058 1059 /** 1060 * Unregister appRunning status listener. 1061 * 1062 * @param listener Running status listener. 1063 * @return Returns ERR_OK on success, others on failure. 1064 */ 1065 int32_t UnregisterAppRunningStatusListener(const sptr<IRemoteObject> &listener); 1066 1067 /** 1068 * Register application foreground state observer. 1069 * @param observer Is app foreground statue observer 1070 * @return Returns ERR_OK on success, others on failure. 1071 */ 1072 int32_t RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer); 1073 1074 /** 1075 * Unregister application foreground state observer. 1076 * @param observer Is app foreground statue observer 1077 * @return Returns ERR_OK on success, others on failure. 1078 */ 1079 int32_t UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer); 1080 1081 /** 1082 * Start child process, called by ChildProcessManager. 1083 * 1084 * @param callingPid Calling process pid. 1085 * @param childPid Created child process pid. 1086 * @param request Child process start request params. 1087 * @return Returns ERR_OK on success, others on failure. 1088 */ 1089 virtual int32_t StartChildProcess(const pid_t callingPid, pid_t &childPid, const ChildProcessRequest &request); 1090 1091 /** 1092 * Get child process record for self. 1093 * 1094 * @return child process record. 1095 */ 1096 virtual int32_t GetChildProcessInfoForSelf(ChildProcessInfo &info); 1097 1098 /** 1099 * Attach child process scheduler to app manager service. 1100 * 1101 * @param pid the child process pid to exit. 1102 * @param childScheduler scheduler of child process. 1103 */ 1104 virtual void AttachChildProcess(const pid_t pid, const sptr<IChildScheduler> &childScheduler); 1105 1106 /** 1107 * Exit child process safely by child process pid. 1108 * 1109 * @param pid child process pid. 1110 */ 1111 virtual void ExitChildProcessSafelyByChildPid(const pid_t pid); 1112 1113 /** 1114 * Start native child process, callde by ChildProcessManager. 1115 * @param hostPid Host process pid. 1116 * @param childProcessCount current started child process count 1117 * @param libName lib file name to be load in child process 1118 * @param callback callback for notify start result 1119 * @return Returns ERR_OK on success, others on failure. 1120 */ 1121 virtual int32_t StartNativeChildProcess(const pid_t hostPid, 1122 const std::string &libName, int32_t childProcessCount, const sptr<IRemoteObject> &callback); 1123 1124 /** 1125 * To clear the process by ability token. 1126 * 1127 * @param token the unique identification to the ability. 1128 */ 1129 void ClearProcessByToken(sptr<IRemoteObject> token); 1130 1131 /** 1132 * Whether the current application process is the last surviving process. 1133 * @param bundleName To query the bundle name of a process. 1134 * @return Returns true is final application process, others return false. 1135 */ 1136 bool IsFinalAppProcessByBundleName(const std::string &bundleName); 1137 1138 /** 1139 * @brief Notify memory size state changed to sufficient or insufficent. 1140 * @param isMemorySizeSufficent Indicates the memory size state. 1141 * @return Returns ERR_OK on success, others on failure. 1142 */ 1143 int32_t NotifyMemorySizeStateChanged(bool isMemorySizeSufficent); 1144 1145 /** 1146 * whether memory size is sufficent. 1147 * @return Returns true is sufficent memory size, others return false. 1148 */ 1149 bool IsMemorySizeSufficent(); 1150 1151 int32_t RegisterRenderStateObserver(const sptr<IRenderStateObserver> &observer); 1152 1153 int32_t UnregisterRenderStateObserver(const sptr<IRenderStateObserver> &observer); 1154 1155 int32_t UpdateRenderState(pid_t renderPid, int32_t state); 1156 1157 int32_t SignRestartAppFlag(int32_t uid); 1158 1159 int32_t GetAppIndexByPid(pid_t pid, int32_t &appIndex) const; 1160 1161 void SetAppAssertionPauseState(bool flag); 1162 1163 void SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid); 1164 1165 int32_t GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId); 1166 1167 int32_t GetAllUIExtensionRootHostPid(pid_t pid, std::vector<pid_t> &hostPids); 1168 1169 int32_t GetAllUIExtensionProviderPid(pid_t hostPid, std::vector<pid_t> &providerPids); 1170 1171 virtual int DumpIpcAllStart(std::string& result); 1172 1173 virtual int DumpIpcAllStop(std::string& result); 1174 1175 virtual int DumpIpcAllStat(std::string& result); 1176 1177 virtual int DumpIpcStart(const int32_t pid, std::string& result); 1178 1179 virtual int DumpIpcStop(const int32_t pid, std::string& result); 1180 1181 virtual int DumpIpcStat(const int32_t pid, std::string& result); 1182 1183 virtual int DumpFfrt(const std::vector<int32_t>& pids, std::string& result); 1184 1185 int32_t SetSupportedProcessCacheSelf(bool isSupport); 1186 1187 int32_t SetSupportedProcessCache(int32_t pid, bool isSupport); 1188 1189 void OnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, ApplicationState state); 1190 1191 virtual void SaveBrowserChannel(const pid_t hostPid, sptr<IRemoteObject> browser); 1192 1193 bool IsAppProcessesAllCached(const std::string &bundleName, int32_t uid, 1194 const std::set<std::shared_ptr<AppRunningRecord>> &cachedSet); 1195 1196 /** 1197 * Check caller is test ability 1198 * 1199 * @param pid, the pid of ability. 1200 * @return Returns ERR_OK is test ability, others is not test ability. 1201 */ 1202 int32_t CheckCallingIsUserTestModeInner(const pid_t pid, bool &isUserTest); 1203 1204 bool GetSceneBoardAttachFlag() const; 1205 1206 void SetSceneBoardAttachFlag(bool flag); 1207 1208 void CacheLoadAbilityTask(const LoadAbilityTaskFunc& func); 1209 1210 void SubmitCacheLoadAbilityTask(); 1211 /** 1212 * Notifies that one ability is attached to status bar. 1213 * 1214 * @param token the token of the abilityRecord that is attached to status bar. 1215 */ 1216 void AttachedToStatusBar(const sptr<IRemoteObject> &token); 1217 void KillApplicationByRecord(const std::shared_ptr<AppRunningRecord> &appRecord); 1218 1219 int32_t NotifyProcessDependedOnWeb(); 1220 1221 void KillProcessDependedOnWeb(); 1222 1223 void RestartResidentProcessDependedOnWeb(); 1224 1225 void BlockProcessCacheByPids(const std::vector<int32_t>& pids); 1226 1227 bool IsKilledForUpgradeWeb(const std::string &bundleName) const; 1228 1229 bool CleanAbilityByUserRequest(const sptr<IRemoteObject> &token); 1230 1231 bool IsProcessContainsOnlyUIAbility(const pid_t pid); 1232 1233 bool IsProcessAttached(sptr<IRemoteObject> token) const; 1234 1235 void NotifyAppPreCache(int32_t pid, int32_t userId); 1236 1237 /** 1238 * Get pids of processes which belong to specific bundle name and support process cache feature. 1239 * 1240 * @param bundleName bundle name. 1241 * @param pidList pid list of processes that support process cache. 1242 * @return Returns ERR_OK on success, others on failure. 1243 */ 1244 virtual int32_t GetSupportedProcessCachePids(const std::string &bundleName, std::vector<int32_t> &pidList); 1245 1246 bool IsSpecifiedModuleLoaded(const AAFwk::Want &want, const AbilityInfo &abilityInfo); 1247 1248 /** 1249 * Send appSpawn uninstall debug hap message. 1250 * 1251 * @param userId, the user id. 1252 */ 1253 void SendAppSpawnUninstallDebugHapMsg(int32_t userId); 1254 1255 private: 1256 int32_t ForceKillApplicationInner(const std::string &bundleName, const int userId = -1, 1257 const int appIndex = 0); 1258 1259 std::string FaultTypeToString(FaultDataType type); 1260 1261 FaultData ConvertDataTypes(const AppFaultDataBySA &faultData); 1262 1263 void StartEmptyResidentProcess(const BundleInfo &info, const std::string &processName, int restartCount, 1264 bool isEmptyKeepAliveApp); 1265 1266 void RestartResidentProcess(std::shared_ptr<AppRunningRecord> appRecord); 1267 1268 bool CheckLoadAbilityConditions(const sptr<IRemoteObject> &token, 1269 const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo); 1270 1271 bool GetBundleInfo(const std::string &bundleName, BundleInfo &bundleInfo); 1272 1273 bool GenerateRenderUid(int32_t &renderUid); 1274 1275 void MakeServiceExtProcessName(const std::shared_ptr<AbilityInfo> &abilityInfo, 1276 const std::shared_ptr<ApplicationInfo> &appInfo, std::string &processName) const; 1277 1278 void MakeProcessName(const std::shared_ptr<AbilityInfo> &abilityInfo, 1279 const std::shared_ptr<ApplicationInfo> &appInfo, const HapModuleInfo &hapModuleInfo, int32_t appIndex, 1280 const std::string &specifiedProcessFlag, std::string &processName) const; 1281 1282 void MakeProcessName(const std::shared_ptr<ApplicationInfo> &appInfo, const HapModuleInfo &hapModuleInfo, 1283 std::string &processName) const; 1284 1285 bool CheckIsolationMode(const HapModuleInfo &hapModuleInfo) const; 1286 1287 bool IsMainProcess(const std::shared_ptr<ApplicationInfo> &appInfo, const std::string &processName) const; 1288 1289 /** 1290 * StartAbility, load the ability that needed to be started(Start on the basis of the original process). 1291 * Start on a new boot process 1292 * @param token, the unique identification to start the ability. 1293 * @param preToken, the unique identification to call the ability. 1294 * @param abilityInfo, the ability information. 1295 * @param appInfo, the app information. 1296 * 1297 * @return 1298 */ 1299 void StartAbility(sptr<IRemoteObject> token, sptr<IRemoteObject> preToken, 1300 std::shared_ptr<AbilityInfo> abilityInfo, std::shared_ptr<AppRunningRecord> appRecord, 1301 const HapModuleInfo &hapModuleInfo, std::shared_ptr<AAFwk::Want> want, int32_t abilityRecordId); 1302 1303 int32_t StartPerfProcess(const std::shared_ptr<AppRunningRecord> &appRecord, const std::string& perfCmd, 1304 const std::string& debugCmd, bool isSandboxApp); 1305 1306 void StartProcessVerifyPermission(const BundleInfo &bundleInfo, bool &hasAccessBundleDirReq, 1307 uint8_t &setAllowInternet, uint8_t &allowInternet, std::vector<int32_t> &gids); 1308 1309 void AddMountPermission(uint32_t accessTokenId, std::set<std::string> &permissions); 1310 1311 /** 1312 * StartProcess, load the ability that needed to be started(Start on a new boot process). 1313 * 1314 * @param appName, the app name. 1315 * @param processName, the process name. 1316 * @param appRecord, the app information. 1317 * @param uid, the process uid. 1318 * @param bundleName, the app bundleName. 1319 * 1320 * @return 1321 */ 1322 int32_t StartProcess(const std::string &appName, const std::string &processName, uint32_t startFlags, 1323 std::shared_ptr<AppRunningRecord> appRecord, const int uid, const BundleInfo &bundleInfo, 1324 const std::string &bundleName, const int32_t bundleIndex, bool appExistFlag = true, 1325 bool isPreload = false, AppExecFwk::PreloadMode preloadMode = AppExecFwk::PreloadMode::PRE_MAKE, 1326 const std::string &moduleName = "", const std::string &abilityName = "", 1327 bool strictMode = false, int32_t maxChildProcess = 0, sptr<IRemoteObject> token = nullptr, 1328 std::shared_ptr<AAFwk::Want> want = nullptr, 1329 ExtensionAbilityType ExtensionAbilityType = ExtensionAbilityType::UNSPECIFIED); 1330 1331 /** 1332 * KillApplicationByUserId, kill the application by user ID. 1333 * 1334 * @param bundleName, bundle name in Application record. 1335 * @param appCloneIndex the app clone id. 1336 * @param userId, user ID. 1337 * @param reason, caller function name. 1338 * 1339 * @return ERR_OK, return back success, others fail. 1340 */ 1341 int32_t KillApplicationByUserIdLocked(const std::string &bundleName, int32_t appCloneIndex, int32_t userId, 1342 const bool clearPageStack = false, const std::string& reason = "KillApplicationByUserIdLocked"); 1343 1344 /** 1345 * WaitForRemoteProcessExit, Wait for the process to exit normally. 1346 * 1347 * @param pids, process number collection to exit. 1348 * @param startTime, execution process security exit start time. 1349 * 1350 * @return true, return back success,others fail. 1351 */ 1352 bool WaitForRemoteProcessExit(std::list<pid_t> &pids, const int64_t startTime); 1353 1354 /** 1355 * WaitProcessesExitAndKill, Wait for the process to exit normally, and kill it if time out. 1356 * 1357 * @param pids, process number collection to exit. 1358 * @param startTime, execution process security exit start time. 1359 * @param reason caller function name 1360 * 1361 * @return ERR_OK, return back success, others fail. 1362 */ 1363 int32_t WaitProcessesExitAndKill(std::list<pid_t> &pids, const int64_t startTime, const std::string& reason); 1364 1365 /** 1366 * ProcessExist, Judge whether the process exists. 1367 * 1368 * @param pids, process number collection to exit. 1369 * 1370 * @return true, return back existed,others non-existent. 1371 */ 1372 bool ProcessExist(pid_t pid); 1373 1374 /** 1375 * CheckAllProcessExit, Determine whether all processes exits . 1376 * 1377 * @param pids, process number collection to exit. 1378 * 1379 * @return true, Returns that no process exist in the list. 1380 */ 1381 bool CheckAllProcessExit(std::list<pid_t> &pids); 1382 1383 /** 1384 * SystemTimeMillisecond, Get system time. 1385 * 1386 * @return the system time. 1387 */ 1388 int64_t SystemTimeMillisecond(); 1389 1390 // Test add the bundle manager instance. 1391 void SetBundleManagerHelper(const std::shared_ptr<BundleMgrHelper> &bundleMgrHelper); 1392 1393 void HandleTerminateApplicationTimeOut(const int64_t eventId); 1394 1395 void HandleAddAbilityStageTimeOut(const int64_t eventId); 1396 1397 void ClipStringContent(const std::regex &re, const std::string &source, std::string &afterCutStr); 1398 1399 bool GetBundleAndHapInfo(const AbilityInfo &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo, 1400 BundleInfo &bundleInfo, HapModuleInfo &hapModuleInfo, int32_t appIndex = 0) const; 1401 AppProcessData WrapAppProcessData(const std::shared_ptr<AppRunningRecord> &appRecord, 1402 const ApplicationState state); 1403 1404 int UserTestAbnormalFinish(const sptr<IRemoteObject> &observer, const std::string &msg); 1405 int GetHapModuleInfoForTestRunner(const AAFwk::Want &want, const sptr<IRemoteObject> &observer, 1406 const BundleInfo &bundleInfo, HapModuleInfo &hapModuleInfo); 1407 int StartEmptyProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer, const BundleInfo &info, 1408 const std::string &processName, const int userId); 1409 1410 void HandleStartSpecifiedAbilityTimeOut(const int64_t eventId); 1411 1412 void HandleStartSpecifiedProcessTimeout(const int64_t eventId); 1413 1414 void InitGlobalConfiguration(); 1415 1416 void GetRunningProcesses(const std::shared_ptr<AppRunningRecord> &appRecord, std::vector<RunningProcessInfo> &info); 1417 void GetRunningProcess(const std::shared_ptr<AppRunningRecord> &appRecord, RunningProcessInfo &info); 1418 1419 void GetRenderProcesses(const std::shared_ptr<AppRunningRecord> &appRecord, std::vector<RenderProcessInfo> &info); 1420 1421 void GetChildrenProcesses(const std::shared_ptr<AppRunningRecord> &appRecord, std::vector<ChildProcessInfo> &info); 1422 1423 int StartRenderProcessImpl(const std::shared_ptr<RenderRecord> &renderRecord, 1424 const std::shared_ptr<AppRunningRecord> appRecord, pid_t &renderPid, bool isGPU = false); 1425 1426 void SetRenderStartMsg(AppSpawnStartMsg &startMsg, std::shared_ptr<RenderRecord> renderRecord, 1427 const int32_t renderUid, const bool isGPU); 1428 1429 void OnRenderRemoteDied(const wptr<IRemoteObject> &remote); 1430 1431 void AddWatchParameter(); 1432 1433 bool VerifyAPL() const; 1434 1435 void RegisterFocusListener(); 1436 1437 static void PointerDeviceEventCallback(const char *key, const char *value, void *context); 1438 1439 int VerifyKillProcessPermission(const std::string &bundleName) const; 1440 1441 int32_t VerifyKillProcessPermissionCommon() const; 1442 1443 bool CheckCallerIsAppGallery(); 1444 1445 void ApplicationTerminatedSendProcessEvent(const std::shared_ptr<AppRunningRecord> &appRecord); 1446 void ClearAppRunningDataForKeepAlive(const std::shared_ptr<AppRunningRecord> &appRecord); 1447 1448 int32_t StartChildProcessPreCheck(pid_t callingPid, int32_t childProcessType); 1449 1450 int32_t StartChildProcessImpl(const std::shared_ptr<ChildProcessRecord> childProcessRecord, 1451 const std::shared_ptr<AppRunningRecord> appRecord, pid_t &childPid, const ChildProcessArgs &args, 1452 const ChildProcessOptions &options); 1453 1454 int32_t GetChildProcessInfo(const std::shared_ptr<ChildProcessRecord> childProcessRecord, 1455 const std::shared_ptr<AppRunningRecord> appRecord, ChildProcessInfo &info, 1456 bool isCallFromGetChildrenProcesses = false); 1457 1458 void OnChildProcessRemoteDied(const wptr<IRemoteObject> &remote); 1459 1460 void KillChildProcess(const std::shared_ptr<AppRunningRecord> &appRecord); 1461 1462 void KillAttachedChildProcess(const std::shared_ptr<AppRunningRecord> &appRecord); 1463 1464 void PresetMaxChildProcess(const std::shared_ptr<AbilityInfo> &abilityInfo, int32_t &maxChildProcess); 1465 1466 void AfterLoadAbility(std::shared_ptr<AppRunningRecord> appRecord, std::shared_ptr<AbilityInfo> abilityInfo, 1467 std::shared_ptr<AbilityRuntime::LoadParam> loadParam); 1468 1469 void RemoveRenderRecordNoAttach(const std::shared_ptr<AppRunningRecord> &hostRecord, int32_t renderPid); 1470 1471 private: 1472 /** 1473 * ClearUpApplicationData, clear the application data. 1474 * 1475 * @param bundleName, bundle name in Application record. 1476 * @param uid, app uid in Application record. 1477 * @param pid, app pid in Application record. 1478 * @param appCloneIndex the app clone id. 1479 * @param userId, userId. 1480 * @param isBySelf, clear data by application self. 1481 * @param reason, caller function. 1482 * 1483 * @return Returns ERR_OK on success, others on failure. 1484 */ 1485 int32_t ClearUpApplicationDataByUserId(const std::string &bundleName, 1486 int32_t callerUid, pid_t callerPid, int32_t appCloneIndex, int32_t userId, bool isBySelf = false, 1487 const std::string& reason = "ClearUpApplicationDataByUserId"); 1488 1489 bool CheckGetRunningInfoPermission() const; 1490 1491 /** 1492 * kill all processes of a bundleName 1493 * @param bundleName bundleName of which to be killed 1494 * @param clearPageStack clear Page Stack 1495 * @param reason caller function name 1496 */ 1497 int32_t KillApplicationByBundleName(const std::string &bundleName, const bool clearPageStack = false, 1498 const std::string& reason = "KillApplicationByBundleName"); 1499 1500 bool SendProcessStartEvent(const std::shared_ptr<AppRunningRecord> &appRecord, bool isPreload, 1501 AppExecFwk::PreloadMode preloadMode); 1502 1503 bool SendProcessStartFailedEvent(std::shared_ptr<AppRunningRecord> appRecord, ProcessStartFailedReason reason, 1504 int32_t subReason); 1505 1506 void SendAppStartupTypeEvent(const std::shared_ptr<AppRunningRecord> &appRecord, 1507 const std::shared_ptr<AbilityInfo> &abilityInfo, const AppStartType startType); 1508 1509 bool SendCreateAtomicServiceProcessEvent(const std::shared_ptr<AppRunningRecord> &appRecord, 1510 const BundleType &bundleType, const std::string &moduleName = "", const std::string &abilityName = ""); 1511 1512 void SendProcessExitEvent(const std::shared_ptr<AppRunningRecord> &appRecord); 1513 1514 void SendProcessExitEventTask(const std::shared_ptr<AppRunningRecord> &appRecord, time_t exitTime, int32_t count); 1515 1516 void SetRunningSharedBundleList(const std::string &bundleName, 1517 const std::vector<BaseSharedBundleInfo> baseSharedBundleInfoList); 1518 1519 void RemoveRunningSharedBundleList(const std::string &bundleName); 1520 1521 void KillRenderProcess(const std::shared_ptr<AppRunningRecord> &appRecord); 1522 1523 void SetOverlayInfo(const std::string& bundleName, const int32_t userId, AppSpawnStartMsg& startMsg); 1524 void SetAppEnvInfo(const BundleInfo &bundleInfo, AppSpawnStartMsg& startMsg); 1525 1526 void TimeoutNotifyApp(int32_t pid, int32_t uid, const std::string& bundleName, const std::string& processName, 1527 const FaultData &faultData); 1528 1529 void AppRecoveryNotifyApp(int32_t pid, const std::string& bundleName, 1530 FaultDataType faultType, const std::string& markers); 1531 1532 void ProcessAppDebug(const std::shared_ptr<AppRunningRecord> &appRecord, const bool &isDebugStart); 1533 AppDebugInfo MakeAppDebugInfo(const std::shared_ptr<AppRunningRecord> &appRecord, const bool &isDebugStart); 1534 int32_t NotifyAbilitysDebugChange(const std::string &bundleName, const bool &isAppDebug); 1535 int32_t NotifyAbilitysAssertDebugChange(const std::shared_ptr<AppRunningRecord> &appRecord, bool isAssertDebug); 1536 1537 void SetProcessJITState(const std::shared_ptr<AppRunningRecord> appRecord); 1538 1539 bool JudgeSelfCalledByToken(const sptr<IRemoteObject> &token, const PageStateData &pageStateData); 1540 1541 void ParseServiceExtMultiProcessWhiteList(); 1542 void ClearData(std::shared_ptr<AppRunningRecord> appRecord); 1543 1544 /** 1545 * Notify the app running status. 1546 * 1547 * @param bundle Bundle name in application record. 1548 * @param uid Uid of bundle. 1549 * @param runningStatus The app running status. 1550 * 1551 * @return 1552 */ 1553 void NotifyAppRunningStatusEvent( 1554 const std::string &bundle, int32_t uid, AbilityRuntime::RunningStatus runningStatus); 1555 1556 void GetRunningCloneAppInfo(const std::shared_ptr<AppRunningRecord> &appRecord, 1557 RunningMultiAppInfo &info); 1558 1559 void GetRunningMultiInstanceKeys(const std::shared_ptr<AppRunningRecord> &appRecord, 1560 std::vector<std::string> &instanceKeys); 1561 /** 1562 * To Prevent process being killed when ability is starting in an existing process, 1563 * we need notify memmgr to increase process priority. 1564 * 1565 * @param appRecord Current app running record. 1566 * 1567 * @return Whether improve priority succeed. 1568 */ 1569 bool NotifyMemMgrPriorityChanged(const std::shared_ptr<AppRunningRecord> appRecord); 1570 1571 void HandlePreloadApplication(const PreloadRequest &request); 1572 1573 void reportpreLoadTask(const std::shared_ptr<AppRunningRecord> appRecord); 1574 1575 std::string GetSpecifiedProcessFlag(std::shared_ptr<AbilityInfo> abilityInfo, std::shared_ptr<AAFwk::Want> want); 1576 std::string GetSpecifiedProcessFlag(const AbilityInfo &abilityInfo, const AAFwk::Want &want); 1577 1578 void LoadAbilityNoAppRecord(const std::shared_ptr<AppRunningRecord> appRecord, 1579 bool isShellCall, std::shared_ptr<ApplicationInfo> appInfo, 1580 std::shared_ptr<AbilityInfo> abilityInfo, const std::string &processName, 1581 const std::string &specifiedProcessFlag, const BundleInfo &bundleInfo, 1582 const HapModuleInfo &hapModuleInfo, std::shared_ptr<AAFwk::Want> want, 1583 bool appExistFlag, bool isPreload, AppExecFwk::PreloadMode preloadMode, 1584 sptr<IRemoteObject> token = nullptr); 1585 1586 int32_t CreatNewStartMsg(const Want &want, const AbilityInfo &abilityInfo, 1587 const std::shared_ptr<ApplicationInfo> &appInfo, const std::string &processName, 1588 AppSpawnStartMsg &startMsg); 1589 1590 int32_t CreateStartMsg(const std::string &processName, uint32_t startFlags, const int uid, 1591 const BundleInfo &bundleInfo, const int32_t bundleIndex, BundleType bundleType, AppSpawnStartMsg &startMsg, 1592 std::shared_ptr<AAFwk::Want> want, const std::string &moduleName = "", const std::string &abilityName = "", 1593 bool strictMode = false); 1594 1595 void QueryExtensionSandBox(const std::string &moduleName, const std::string &abilityName, 1596 const BundleInfo &bundleInfo, AppSpawnStartMsg &startMsg, DataGroupInfoList& dataGroupInfoList, 1597 bool strictMode, std::shared_ptr<AAFwk::Want> want); 1598 1599 int32_t StartPerfProcessByStartMsg(AppSpawnStartMsg &startMsg, const std::string& perfCmd, 1600 const std::string& debugCmd, bool isSandboxApp); 1601 1602 void SetAtomicServiceInfo(BundleType bundleType, AppSpawnStartMsg &startMsg); 1603 1604 void SetAppInfo(const BundleInfo &bundleInfo, AppSpawnStartMsg &startMsg); 1605 1606 bool CreateAbilityInfo(const AAFwk::Want &want, AbilityInfo &abilityInfo); 1607 1608 AAFwk::EventInfo BuildEventInfo(std::shared_ptr<AppRunningRecord> appRecord) const; 1609 1610 void NotifyAppAttachFailed(std::shared_ptr<AppRunningRecord> appRecord); 1611 1612 void NotifyStartProcessFailed(sptr<IRemoteObject> token); 1613 private: 1614 /** 1615 * Notify application status. 1616 * 1617 * @param bundleName Indicates the name of the bundle. 1618 * @param bundleName Indicates the name of the bundle. 1619 * @param eventData Indicates the event defined by CommonEventSupport 1620 * 1621 * @return 1622 */ 1623 void NotifyAppStatusByCallerUid(const std::string &bundleName, const int32_t tokenId, const int32_t userId, 1624 const int32_t callerUid, const int32_t targetUid, const std::string &eventData); 1625 void SendHiSysEvent(const int32_t innerEventId, const int64_t eventId); 1626 int FinishUserTestLocked( 1627 const std::string &msg, const int64_t &resultCode, const std::shared_ptr<AppRunningRecord> &appRecord); 1628 int32_t GetCurrentAccountId() const; 1629 void SendReStartProcessEvent(AAFwk::EventInfo &eventInfo, int32_t appUid); 1630 void SendAppLaunchEvent(const std::shared_ptr<AppRunningRecord> &appRecord); 1631 void InitAppWaitingDebugList(); 1632 void HandleConfigurationChange(const Configuration &config, const int32_t userId = -1); 1633 bool CheckIsThreadInFoundation(pid_t pid); 1634 bool CheckAppFault(const std::shared_ptr<AppRunningRecord> &appRecord, const FaultData &faultData); 1635 int32_t KillFaultApp(int32_t pid, const std::string &bundleName, const FaultData &faultData, 1636 bool isNeedExit = false); 1637 void AddUIExtensionLauncherItem(std::shared_ptr<AAFwk::Want> want, std::shared_ptr<AppRunningRecord> appRecord, 1638 sptr<IRemoteObject> token); 1639 void NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos); 1640 void RemoveUIExtensionLauncherItem(std::shared_ptr<AppRunningRecord> appRecord, sptr<IRemoteObject> token); 1641 bool IsSceneBoardCall(); 1642 void CheckCleanAbilityByUserRequest(const std::shared_ptr<AppRunningRecord> &appRecord, 1643 const std::shared_ptr<AbilityRunningRecord> &abilityRecord, const AbilityState state); 1644 void GetPidsByAccessTokenId(const uint32_t accessTokenId, std::vector<pid_t> &pids); 1645 int32_t DealWithUserConfiguration(const Configuration &config, const int32_t userId, int32_t ¬ifyUserId); 1646 int32_t KillProcessByPidInner(const pid_t pid, const std::string& reason, 1647 const std::string& killReason, std::shared_ptr<AppRunningRecord> appRecord); 1648 bool IsAllowedNWebPreload(const std::string &processName); 1649 bool CheckAppRecordAndPriorityObject(const std::shared_ptr<AppRunningRecord> &appRecord); 1650 void GetAppCloneInfo(const std::shared_ptr<AppRunningRecord> &appRecord, 1651 RunningMultiAppInfo &info); 1652 void GetMultiInstanceInfo(const std::shared_ptr<AppRunningRecord> &appRecord, 1653 RunningMultiAppInfo &info); 1654 int32_t GetAllRunningInstanceKeysByBundleNameInner(const std::string &bundleName, 1655 std::vector<std::string> &instanceKeys, int32_t userId); 1656 void AddAbilityStageForSpecified(std::shared_ptr<AppRunningRecord> appRecord); 1657 1658 const std::string TASK_ON_CALLBACK_DIED = "OnCallbackDiedTask"; 1659 std::vector<AppStateCallbackWithUserId> appStateCallbacks_; 1660 std::shared_ptr<RemoteClientManager> remoteClientManager_; 1661 std::shared_ptr<AppRunningManager> appRunningManager_; 1662 std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_; 1663 std::shared_ptr<AMSEventHandler> eventHandler_; 1664 ffrt::mutex userTestLock_; 1665 ffrt::mutex appStateCallbacksLock_; 1666 ffrt::mutex renderUidSetLock_; 1667 ffrt::mutex exceptionLock_; 1668 ffrt::mutex browserHostLock_; 1669 sptr<IStartSpecifiedAbilityResponse> startSpecifiedAbilityResponse_; 1670 ffrt::mutex configurationObserverLock_; 1671 std::vector<ConfigurationObserverWithUserId> configurationObservers_; 1672 sptr<WindowFocusChangedListener> focusListener_; 1673 sptr<WindowVisibilityChangedListener> windowVisibilityChangedListener_; 1674 std::vector<std::shared_ptr<AppRunningRecord>> restartResedentTaskList_; 1675 ffrt::mutex runningSharedBundleListMutex_; 1676 std::map<std::string, std::vector<BaseSharedBundleInfo>> runningSharedBundleList_; 1677 std::map<std::string, bool> waitingDebugBundleList_; 1678 ffrt::mutex waitingDebugLock_; 1679 bool isInitAppWaitingDebugListExecuted_ = false; 1680 std::unordered_set<int32_t> renderUidSet_; 1681 std::string supportIsolationMode_ {"false"}; 1682 std::string supportServiceExtMultiProcess_ {"false"}; 1683 int32_t currentUserId_ = 0; 1684 int32_t lastRenderUid_ = Constants::START_UID_FOR_RENDER_PROCESS; 1685 sptr<IAbilityDebugResponse> abilityDebugResponse_; 1686 std::shared_ptr<AppDebugManager> appDebugManager_; 1687 ffrt::mutex killpedProcessMapLock_; 1688 mutable std::map<int64_t, std::string> killedProcessMap_; 1689 ffrt::mutex startChildProcessLock_; 1690 std::vector<std::string> serviceExtensionWhiteList_; 1691 std::shared_ptr<AbilityRuntime::AppRunningStatusModule> appRunningStatusModule_; 1692 std::shared_ptr<AdvancedSecurityModeManager> securityModeManager_; 1693 std::shared_ptr<AAFwk::TaskHandlerWrap> dfxTaskHandler_; 1694 std::shared_ptr<AAFwk::TaskHandlerWrap> otherTaskHandler_; 1695 std::shared_ptr<AppPreloader> appPreloader_; 1696 std::atomic<bool> sceneBoardAttachFlag_ = true; 1697 1698 std::mutex loadTaskListMutex_; 1699 std::vector<LoadAbilityTaskFunc> loadAbilityTaskFuncList_; 1700 std::atomic<int32_t> willKillPidsNum_ = 0; 1701 std::shared_ptr<AAFwk::TaskHandlerWrap> delayKillTaskHandler_; 1702 1703 std::shared_ptr<MultiUserConfigurationMgr> multiUserConfigurationMgr_; 1704 std::unordered_set<std::string> nwebPreloadSet_ {}; 1705 ffrt::mutex killedBundleSetMutex_; 1706 std::set<std::string> killedBundleSet_; 1707 1708 std::mutex childProcessRecordMapMutex_; 1709 }; 1710 } // namespace AppExecFwk 1711 } // namespace OHOS 1712 #endif // OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_INNER_H 1713