1 /* 2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H 17 #define OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H 18 19 #include <list> 20 #include <string> 21 #include <vector> 22 23 #include "ability_info.h" 24 #include "ability_running_record.h" 25 #include "ams_mgr_scheduler.h" 26 #include "app_malloc_info.h" 27 #include "app_mgr_constants.h" 28 #include "app_mgr_service_event_handler.h" 29 #include "app_mgr_service_inner.h" 30 #include "app_mgr_stub.h" 31 #include "app_record_id.h" 32 #include "app_running_record.h" 33 #include "app_running_status_listener_interface.h" 34 #include "app_scheduler_proxy.h" 35 #include "appexecfwk_errors.h" 36 #include "application_info.h" 37 #include "if_system_ability_manager.h" 38 #include "nocopyable.h" 39 #include "system_ability.h" 40 #include "task_handler_wrap.h" 41 #include "app_jsheap_mem_info.h" 42 #include "app_cjheap_mem_info.h" 43 44 namespace OHOS { 45 namespace AppExecFwk { 46 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 47 48 struct AppMgrServiceState { 49 ServiceRunningState serviceRunningState = ServiceRunningState::STATE_NOT_START; 50 SpawnConnectionState connectionState = SpawnConnectionState::STATE_NOT_CONNECT; 51 }; 52 53 class AMSEventHandler; 54 55 class AppMgrService : public SystemAbility, public AppMgrStub { 56 public: 57 DECLEAR_SYSTEM_ABILITY(AppMgrService); 58 59 AppMgrService(); 60 explicit AppMgrService(const int32_t serviceId, bool runOnCreate = false); 61 virtual ~AppMgrService() override; 62 63 // the function about application 64 // attach the application to ability mgr, then ability mgr can control it. 65 /** 66 * AttachApplication, call AttachApplication() through proxy object, 67 * get all the information needed to start the Application (data related to the Application ). 68 * 69 * @param app, information needed to start the Application. 70 * @return 71 */ 72 virtual void AttachApplication(const sptr<IRemoteObject> &app) override; 73 74 /** 75 * Preload application. 76 * 77 * @param bundleName The bundle name of the application to preload. 78 * @param userId Indicates the user identification. 79 * @param preloadMode Preload application mode. 80 * @param appIndex The index of application clone. 81 * @return Returns ERR_OK on success, others on failure. 82 */ 83 virtual int32_t PreloadApplication(const std::string &bundleName, int32_t userId, 84 AppExecFwk::PreloadMode preloadMode, int32_t appIndex) override; 85 86 /** 87 * ApplicationForegrounded, call ApplicationForegrounded() through proxy object, 88 * set the application to Foreground State. 89 * 90 * @param recordId, a unique record that identifies this Application from others. 91 * @return 92 */ 93 virtual void ApplicationForegrounded(const int32_t recordId) override; 94 95 /** 96 * ApplicationBackgrounded, call ApplicationBackgrounded() through proxy object, 97 * set the application to Backgrounded State. 98 * 99 * @param recordId, a unique record that identifies this Application from others. 100 * @return 101 */ 102 virtual void ApplicationBackgrounded(const int32_t recordId) override; 103 104 /** 105 * ApplicationTerminated, call ApplicationTerminated() through proxy object, 106 * terminate the application. 107 * 108 * @param recordId, a unique record that identifies this Application from others. 109 * @return 110 */ 111 virtual void ApplicationTerminated(const int32_t recordId) override; 112 113 /** 114 * AbilityCleaned,call through AbilityCleaned() proxy project, clean Ability record. 115 * 116 * @param token, a unique record that identifies AbilityCleaned from others. 117 * @return 118 */ 119 virtual void AbilityCleaned(const sptr<IRemoteObject> &token) override; 120 121 /** 122 * ClearUpApplicationData, call ClearUpApplicationData() through proxy project, 123 * clear the application data. 124 * 125 * @param bundleName, bundle name in Application record. 126 * @param appCloneIndex the app clone id. 127 * @param userId the user id. 128 * @return Returns ERR_OK on success, others on failure. 129 */ 130 virtual int32_t ClearUpApplicationData(const std::string &bundleName, int32_t appCloneIndex, 131 int32_t userId = -1) override; 132 133 /** 134 * ClearUpApplicationDataBySelf, call ClearUpApplicationDataBySelf() through proxy project, 135 * clear the application data by self. 136 * 137 * @param userId, user ID. 138 * @return Returns ERR_OK on success, others on failure. 139 */ 140 virtual int32_t ClearUpApplicationDataBySelf(int32_t userId = -1) override; 141 142 /** 143 * GetAllRunningProcesses, call GetAllRunningProcesses() through proxy project. 144 * Obtains information about application processes that are running on the device. 145 * 146 * @param info, app name in Application record. 147 * @return Returns ERR_OK on success, others on failure. 148 */ 149 virtual int32_t GetAllRunningProcesses(std::vector<RunningProcessInfo> &info) override; 150 151 /** 152 * GetRunningMultiAppInfoByBundleName, call GetRunningMultiAppInfoByBundleName() through proxy project. 153 * Obtains information about multiapp that are running on the device. 154 * 155 * @param bundlename, input. 156 * @param info, output multiapp information. 157 * @return ERR_OK ,return back success,others fail. 158 */ 159 virtual int32_t GetRunningMultiAppInfoByBundleName(const std::string &bundleName, 160 RunningMultiAppInfo &info) override; 161 162 /** 163 * GetAllRunningInstanceKeysBySelf, call GetAllRunningInstanceKeysBySelf() through proxy project. 164 * Obtains running instance keys of multi-instance app that are running on the device. 165 * 166 * @param instanceKeys, output instance keys of the multi-instance app. 167 * @return ERR_OK ,return back success,others fail. 168 */ 169 virtual int32_t GetAllRunningInstanceKeysBySelf(std::vector<std::string> &instanceKeys) override; 170 171 /** 172 * GetAllRunningInstanceKeysByBundleName, call GetAllRunningInstanceKeysByBundleName() through proxy project. 173 * Obtains running instance keys of multi-instance app that are running on the device. 174 * 175 * @param bundlename, bundle name in Application record. 176 * @param instanceKeys, output instance keys of the multi-instance app. 177 * @param userId, user id. 178 * @return ERR_OK ,return back success,others fail. 179 */ 180 virtual int32_t GetAllRunningInstanceKeysByBundleName(const std::string &bundleName, 181 std::vector<std::string> &instanceKeys, int32_t userId = -1) override; 182 183 /** 184 * GetRunningProcessesByBundleType, call GetRunningProcessesByBundleType() through proxy project. 185 * Obtains information about application processes by bundle type that are running on the device. 186 * 187 * @param bundleType, bundle type of the processes 188 * @param info, app name in Application record. 189 * @return ERR_OK ,return back success,others fail. 190 */ 191 virtual int GetRunningProcessesByBundleType(const BundleType bundleType, 192 std::vector<RunningProcessInfo> &info) override; 193 194 /** 195 * GetAllRenderProcesses, call GetAllRenderProcesses() through proxy project. 196 * Obtains information about render processes that are running on the device. 197 * 198 * @param info, render process info. 199 * @return ERR_OK, return back success, others fail. 200 */ 201 virtual int32_t GetAllRenderProcesses(std::vector<RenderProcessInfo> &info) override; 202 203 /** 204 * GetAllChildrenProcesses, call GetAllChildrenProcesses() through proxy project. 205 * Obtains information about children processes that are running on the device. 206 * 207 * @param info, child process info. 208 * @return ERR_OK, return back success, others fail. 209 */ 210 virtual int GetAllChildrenProcesses(std::vector<ChildProcessInfo> &info) override; 211 212 /** 213 * JudgeSandboxByPid, call JudgeSandboxByPid() through proxy project. 214 * Obtains information about application processes that are running on the device. 215 * 216 * @param pid, the pid of current app running record. 217 * @param isSandbox, current app is or not a sandbox. 218 * @return Returns ERR_OK on success, others on failure. 219 */ 220 virtual int32_t JudgeSandboxByPid(pid_t pid, bool &isSandbox) override; 221 222 /** 223 * IsTerminatingByPid, call IsTerminatingByPid() through proxy project. 224 * Obtains information about application processes that are running on the device. 225 * 226 * @param pid, the pid of current app running record. 227 * @param isTerminating, current app is or not terminating. 228 * @return Returns ERR_OK on success, others on failure. 229 */ 230 virtual int32_t IsTerminatingByPid(pid_t pid, bool &isTerminating) override; 231 232 /** 233 * GetProcessRunningInfosByUserId, call GetProcessRunningInfosByUserId() through proxy project. 234 * Obtains information about application processes that are running on the device. 235 * 236 * @param info, app name in Application record. 237 * @param userId, userId. 238 * 239 * @return Returns ERR_OK on success, others on failure. 240 */ 241 virtual int32_t GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId) override; 242 243 /** 244 * GetProcessRunningInformation, call GetProcessRunningInformation() through proxy project. 245 * Obtains information about current application process which is running on the device. 246 * 247 * @param info, app name in Application record. 248 * 249 * @return Returns ERR_OK on success, others on failure. 250 */ 251 virtual int32_t GetProcessRunningInformation(RunningProcessInfo &info) override; 252 253 /** 254 * NotifyMemoryLevel, call NotifyMemoryLevel() through proxy project. 255 * Notify applications background the current memory level. 256 * 257 * @param level, current memory level. 258 * @return Returns ERR_OK on success, others on failure. 259 */ 260 virtual int32_t NotifyMemoryLevel(int32_t level) override; 261 262 /** 263 * NotifyProcMemoryLevel, call NotifyMemoryLevel() through proxy project. 264 * Notify applications the current memory level. 265 * 266 * @param procLevelMap , <pid_t, MemoryLevel> map. 267 * @return Returns ERR_OK on success, others on failure. 268 */ 269 virtual int32_t NotifyProcMemoryLevel(const std::map<pid_t, MemoryLevel> &procLevelMap) override; 270 271 /** 272 * DumpHeapMemory, call DumpHeapMemory() through proxy project. 273 * Get the application's memory allocation info. 274 * 275 * @param pid, pid input. 276 * @param mallocInfo, dynamic storage information output. 277 * @return Returns ERR_OK on success, others on failure. 278 */ 279 virtual int32_t DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo) override; 280 281 /** 282 * Authenticate dump permissions 283 * 284 * @return Returns true on permission, others on false 285 */ 286 bool HasDumpPermission() const; 287 /** 288 * DumpJsHeapMemory, call DumpJsHeapMemory() through proxy project. 289 * triggerGC and dump the application's jsheap memory info. 290 * 291 * @param info, pid, tid, needGc, needSnapshot 292 * @return Returns ERR_OK on success, others on failure. 293 */ 294 virtual int32_t DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info) override; 295 296 /** 297 * DumpCjHeapMemory, call DumpCJHeapMemory() through proxy project. 298 * triggerGC and dump the application's cjheap memory info. 299 * 300 * @param info, pid, needGc, needSnapshot 301 * @return Returns ERR_OK on success, others on failure. 302 */ 303 virtual int32_t DumpCjHeapMemory(OHOS::AppExecFwk::CjHeapDumpInfo &info) override; 304 305 // the function about service running info 306 /** 307 * QueryServiceState, Query application service status. 308 * 309 * @return the application service status. 310 */ 311 AppMgrServiceState QueryServiceState(); 312 313 /** 314 * GetAmsMgr, call GetAmsMgr() through proxy object, get AMS interface instance. 315 * 316 * @return sptr<IAmsMgr>, return to AMS interface instance. 317 */ 318 virtual sptr<IAmsMgr> GetAmsMgr() override; 319 320 /** 321 * Notify that the ability stage has been updated 322 * @param recordId, the app record. 323 */ 324 virtual void AddAbilityStageDone(const int32_t recordId) override; 325 326 /** 327 * Start all resident process 328 */ 329 virtual void StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos) override; 330 331 /** 332 * Start user test process. 333 * @param want, want object. 334 * @param observer, test observer remote object. 335 * @param bundleInfo, bundle info. 336 * @param userId the user id. 337 * @return Returns ERR_OK on success, others on failure. 338 */ 339 virtual int StartUserTestProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer, 340 const AppExecFwk::BundleInfo &bundleInfo, int32_t userId) override; 341 342 /** 343 * @brief Finish user test. 344 * @param msg user test message. 345 * @param resultCode user test result Code. 346 * @param bundleName user test bundleName. 347 * 348 * @return Returns ERR_OK on success, others on failure. 349 */ 350 virtual int FinishUserTest( 351 const std::string &msg, const int64_t &resultCode, const std::string &bundleName) override; 352 353 /** 354 * @brief Application hidumper. 355 * @param fd Indicates the fd. 356 * @param args Indicates the params. 357 * @return Returns the dump result. 358 */ 359 int Dump(int fd, const std::vector<std::u16string>& args) override; 360 361 /** 362 * @brief called when the abilityStage's onAcceptWant lifecycle completed. 363 * @param recordId record id of the ability. 364 * @param want want has been accepted. 365 * @param flag flag get from OnAcceptWant. 366 */ 367 virtual void ScheduleAcceptWantDone( 368 const int32_t recordId, const AAFwk::Want &want, const std::string &flag) override; 369 370 virtual void ScheduleNewProcessRequestDone( 371 const int32_t recordId, const AAFwk::Want &want, const std::string &flag) override; 372 373 /** 374 * Get the token of ability records by process ID. 375 * 376 * @param pid The process id. 377 * @param tokens The token of ability records. 378 * @return Returns ERR_OK on success, others on failure. 379 */ 380 virtual int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens) override; 381 382 /** 383 * Prestart nwebspawn process. 384 * 385 * @return Returns ERR_OK on success, others on failure. 386 */ 387 virtual int PreStartNWebSpawnProcess() override; 388 389 /** 390 * Start nweb render process, called by nweb host. 391 * 392 * @param renderParam, params passed to renderProcess. 393 * @param ipcFd, ipc file descriptor for web browser and render process. 394 * @param sharedFd, shared memory file descriptor. 395 * @param crashFd, crash signal file descriptor. 396 * @param renderPid, created render pid. 397 * @param isGPU, is or not gpu process 398 * @return Returns ERR_OK on success, others on failure. 399 */ 400 virtual int StartRenderProcess(const std::string &renderParam, 401 int32_t ipcFd, int32_t sharedFd, 402 int32_t crashFd, pid_t &renderPid, bool isGPU = false) override; 403 404 /** 405 * Render process call this to attach to app manager service. 406 * 407 * @param renderScheduler scheduler of render process. 408 */ 409 virtual void AttachRenderProcess(const sptr<IRemoteObject> &shceduler) override; 410 411 /** 412 * Get render process termination status, called by nweb host. 413 * 414 * @param renderPid, target render pid. 415 * @param status, termination status of the render process. 416 * @return Returns ERR_OK on success, others on failure. 417 */ 418 virtual int GetRenderProcessTerminationStatus(pid_t renderPid, int &status) override; 419 420 /** 421 * GetConfiguration 422 * 423 * @param info to retrieve configuration data. 424 * @return ERR_OK ,return back success,others fail. 425 */ 426 virtual int32_t GetConfiguration(Configuration& config) override; 427 428 /** 429 * UpdateConfiguration, ANotify application update system environment changes. 430 * 431 * @param config System environment change parameters. 432 * @param userId configuration for the user 433 * @return Returns ERR_OK on success, others on failure. 434 */ 435 virtual int32_t UpdateConfiguration(const Configuration &config, const int32_t userId = -1) override; 436 437 /** 438 * Internally, an asynchronous task will be initiated and tasks in the task list will be scheduled according to 439 * the policy. 440 * If during the execution of a task, a caller triggers UpdateConfiguration or UpdateConfigurationForBackgroundApp, 441 * the asynchronous task will be terminated. 442 * If the task has not been executed and the app is switched to the foreground, the task will be updated 443 * when brought to the foreground. The task will not be updated while in the background. 444 * Supported scope: colormode 445 * Only SA can call this method. 446 * 447 * @param appInfos Background application information. 448 * @param policy Update policy. 449 * @param userId configuration for the user 450 * @return Returns ERR_OK on success, others on failure. 451 */ 452 virtual int32_t UpdateConfigurationForBackgroundApp(const std::vector<BackgroundAppInfo> &appInfos, 453 const AppExecFwk::ConfigurationPolicy &policy, const int32_t userId = -1) override; 454 455 /** 456 * Update config by bundle name. 457 * 458 * @param config Application environment change parameters. 459 * @param name Application bundle name. 460 * @return Returns ERR_OK on success, others on failure. 461 */ 462 virtual int32_t UpdateConfigurationByBundleName(const Configuration &config, const std::string &name, 463 int32_t appIndex = 0) override; 464 465 /** 466 * @brief register a configuration observer which will receive notifies when updated. 467 * @param observer the configuration observer to receive notify. 468 * 469 * @return Returns ERR_OK on success, others on failure. 470 */ 471 virtual int32_t RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override; 472 473 /** 474 * @brief unregister a configuration observer registered before. 475 * @param observer the configuration observer registered before. 476 * 477 * @return Returns ERR_OK on success, others on failure. 478 */ 479 virtual int32_t UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override; 480 481 /** 482 * Register KIA interceptor. 483 * @param interceptor KIA interceptor. 484 * @return Returns ERR_OK on success, others on failure. 485 */ 486 virtual int32_t RegisterKiaInterceptor(const sptr<IKiaInterceptor> &interceptor) override; 487 488 /** 489 * Check if the given pid is a KIA process. 490 * @param pid process id. 491 * @return Returns true if it is a KIA process, false otherwise. 492 */ 493 virtual int32_t CheckIsKiaProcess(pid_t pid, bool &isKia) override; 494 495 /** 496 * @brief Get the running state of application by bundle name. 497 * 498 * @param bundleName Bundle name 499 * @return Returns true if process is running, false if process isn't running. 500 */ 501 bool GetAppRunningStateByBundleName(const std::string &bundleName) override; 502 503 /** 504 * @brief Notify application load patch. 505 * 506 * @param bundleName Bundle name 507 * @param callback called when LoadPatch finished. 508 * @return Returns ERR_OK on success, error code on failure. 509 */ 510 int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override; 511 512 /** 513 * @brief Notify application reload page. 514 * 515 * @param bundleName Bundle name 516 * @param callback called when HotReload finished. 517 * @return Returns ERR_OK on success, error code on failure. 518 */ 519 int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override; 520 521 /** 522 * @brief Notify application unload patch. 523 * 524 * @param bundleName Bundle name 525 * @param callback called when UnloadPatch finished. 526 * @return Returns ERR_OK on success, error code on failure. 527 */ 528 int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override; 529 530 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE 531 int32_t SetContinuousTaskProcess(int32_t pid, bool isContinuousTask) override; 532 #endif 533 534 /** 535 * @brief Check whether the shared bundle is running. 536 * 537 * @param bundleName Shared bundle name. 538 * @param versionCode Shared bundle version code. 539 * @return Returns the shared bundle running result. The result is true if running, false otherwise. 540 */ 541 virtual bool IsSharedBundleRunning(const std::string &bundleName, uint32_t versionCode) override; 542 543 /** 544 * start native process for debugger. 545 * 546 * @param want param to start a process. 547 */ 548 virtual int32_t StartNativeProcessForDebugger(const AAFwk::Want &want) override; 549 550 /** 551 * Get bundleName by pid. 552 * 553 * @param pid process id. 554 * @param bundleName Output parameters, return bundleName. 555 * @param uid Output parameters, return userId. 556 * @return Returns ERR_OK on success, others on failure. 557 */ 558 virtual int32_t GetBundleNameByPid(const int32_t pid, std::string &bundleName, int32_t &uid) override; 559 560 /** 561 * Get running process information by pid. 562 * 563 * @param pid process id. 564 * @param info Output parameters, return runningProcessInfo. 565 * @return Returns ERR_OK on success, others on failure. 566 */ 567 virtual int32_t GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info) override; 568 569 /** 570 * Get running process information by child process pid. 571 * 572 * @param childPid child process id. 573 * @param info Output parameters, return runningProcessInfo. 574 * @return Returns ERR_OK on success, others on failure. 575 */ 576 virtual int32_t GetRunningProcessInfoByChildProcessPid(const pid_t childPid, 577 OHOS::AppExecFwk::RunningProcessInfo &info) override; 578 579 /** 580 * Notify Fault Data 581 * 582 * @param faultData the fault data. 583 * @return Returns ERR_OK on success, others on failure. 584 */ 585 int32_t NotifyAppFault(const FaultData &faultData) override; 586 587 /** 588 * Notify Fault Data By SA 589 * 590 * @param faultData the fault data notified by SA. 591 * @return Returns ERR_OK on success, others on failure. 592 */ 593 int32_t NotifyAppFaultBySA(const AppFaultDataBySA &faultData) override; 594 595 /** 596 * Set Appfreeze Detect Filter 597 * 598 * @param pid the process pid. 599 * @return Returns true on success, others on failure. 600 */ 601 bool SetAppFreezeFilter(int32_t pid) override; 602 603 /** 604 * get memorySize by pid. 605 * 606 * @param pid process id. 607 * @param memorySize Output parameters, return memorySize in KB. 608 * @return Returns ERR_OK on success, others on failure. 609 */ 610 virtual int32_t GetProcessMemoryByPid(const int32_t pid, int32_t &memorySize) override; 611 612 /** 613 * get application processes information list by bundleName. 614 * 615 * @param bundleName Bundle name. 616 * @param userId user Id in Application record. 617 * @param info Output parameters, return running process info list. 618 * @return Returns ERR_OK on success, others on failure. 619 */ 620 virtual int32_t GetRunningProcessInformation( 621 const std::string &bundleName, int32_t userId, std::vector<RunningProcessInfo> &info) override; 622 623 /** 624 * on add systemAbility. 625 * 626 * @return 627 */ 628 virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 629 630 /** 631 * on remove systemAbility. 632 * 633 * @return 634 */ 635 virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 636 637 /** 638 * @brief Notify NativeEngine GC of status change. 639 * 640 * @param state GC state 641 * @param pid pid 642 * 643 * @return Is the status change completed. 644 */ 645 virtual int32_t ChangeAppGcState(pid_t pid, int32_t state, uint64_t tid = 0) override; 646 647 /** 648 * Register appRunning status listener. 649 * 650 * @param listener Running status listener. 651 * @return Returns ERR_OK on success, others on failure. 652 */ 653 int32_t RegisterAppRunningStatusListener(const sptr<IRemoteObject> &listener) override; 654 655 /** 656 * Unregister appRunning status listener. 657 * 658 * @param listener Running status listener. 659 * @return Returns ERR_OK on success, others on failure. 660 */ 661 int32_t UnregisterAppRunningStatusListener(const sptr<IRemoteObject> &listener) override; 662 663 /** 664 * Register application foreground state observer. 665 * @param observer Is App Foreground Statue Observer 666 * @return Returns ERR_OK on success, others on failure. 667 */ 668 int32_t RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer) override; 669 670 /** 671 * Unregister application foreground state observer. 672 * @param observer Is App Foreground Statue Observer 673 * @return Returns ERR_OK on success, others on failure. 674 */ 675 int32_t UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer) override; 676 677 #ifdef SUPPORT_CHILD_PROCESS 678 /** 679 * Start child process, called by ChildProcessManager. 680 * 681 * @param childPid Created child process pid. 682 * @param request Child process start request params. 683 * @return Returns ERR_OK on success, others on failure. 684 */ 685 int32_t StartChildProcess(pid_t &childPid, const ChildProcessRequest &request) override; 686 687 /** 688 * Get child process record for self. 689 * 690 * @return child process info. 691 */ 692 int32_t GetChildProcessInfoForSelf(ChildProcessInfo &info) override; 693 694 /** 695 * Attach child process scheduler to app manager service. 696 * 697 * @param childScheduler scheduler of child process. 698 */ 699 void AttachChildProcess(const sptr<IRemoteObject> &childScheduler) override; 700 701 /** 702 * Exit child process, called by itself. 703 */ 704 void ExitChildProcessSafely() override; 705 #endif // SUPPORT_CHILD_PROCESS 706 707 /** 708 * @brief register a render process state observer to receive change. 709 * @param observer the render process state observer to receive notify. 710 * 711 * @return Returns ERR_OK on success, others on failure. 712 */ 713 int32_t RegisterRenderStateObserver(const sptr<IRenderStateObserver> &observer) override; 714 715 /** 716 * @brief unregister a render process state observer registered before. 717 * @param observer the render process state observer registered before. 718 * 719 * @return Returns ERR_OK on success, others on failure. 720 */ 721 int32_t UnregisterRenderStateObserver(const sptr<IRenderStateObserver> &observer) override; 722 723 /** 724 * @brief update a render process state. 725 * @param renderPid the pid of the render process to be updated. 726 * @param state the new state value of the render process. 727 * 728 * @return Returns ERR_OK on success, others on failure. 729 */ 730 int32_t UpdateRenderState(pid_t renderPid, int32_t state) override; 731 732 /** 733 * @brief mark a process which is going restart. 734 * @param uid the uid of the process. 735 * @param instanceKey the instance key of the process. 736 * 737 * @return Returns ERR_OK on success, others on failure. 738 */ 739 int32_t SignRestartAppFlag(int32_t uid, const std::string &instanceKey) override; 740 741 /** 742 * Get appRunningUniqueId by pid. 743 * @param pid pid. 744 * @param appRunningUniqueId appRunningUniqueId. 745 * @return Returns ERR_OK on success, others on failure. 746 */ 747 int32_t GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId) override; 748 749 /* 750 * Get all uiextension root host process id, need apply permission ohos.permission.GET_RUNNING_INFO. 751 * If specified pid mismatch UIExtensionAbility type, return empty vector. 752 * @param pid Process id. 753 * @param hostPids All host process id. 754 * @return Returns ERR_OK on success, others on failure. 755 */ 756 int32_t GetAllUIExtensionRootHostPid(pid_t pid, std::vector<pid_t> &hostPids) override; 757 758 /** 759 * Get all uiextension provider process id, need apply permission ohos.permission.GET_RUNNING_INFO. 760 * If specified hostPid didn't start any UIExtensionAbility, return empty vector. 761 * @param hostPid Host process id. 762 * @param providerPids All provider process id started by specified hostPid. 763 * @return Returns ERR_OK on success, others on failure. 764 */ 765 int32_t GetAllUIExtensionProviderPid(pid_t hostPid, std::vector<pid_t> &providerPids) override; 766 767 /** 768 * @brief Notify memory size state changed: LOW_MEMORY, MEMORY_RECOVERY, REQUIRE_BIG_MEMORY, NO_REQUIRE_BIG_MEMORY. 769 * @param memorySizeState Indicates the memory size state. 770 * @return Returns ERR_OK on success, others on failure. 771 */ 772 int32_t NotifyMemorySizeStateChanged(int32_t memorySizeState) override; 773 774 /** 775 * Set application assertion pause state. 776 * 777 * @param flag assertion pause state. 778 */ 779 void SetAppAssertionPauseState(bool flag) override; 780 781 /** 782 * @brief set support process cache by self 783 */ 784 int32_t SetSupportedProcessCacheSelf(bool isSupport) override; 785 786 int32_t SetSupportedProcessCache(int32_t pid, bool isSupport) override; 787 788 int32_t IsProcessCacheSupported(int32_t pid, bool &isSupported) override; 789 790 int32_t SetProcessCacheEnable(int32_t pid, bool enable) override; 791 792 /** 793 * Elevate the current process to be a candidate master process. 794 * 795 * @param isInsertToHead Whether insert current process to the head of candidate master process list. 796 * @return Return ERR_OK if success, others fail. 797 */ 798 int32_t PromoteCurrentToCandidateMasterProcess(bool isInsertToHead) override; 799 800 /** 801 * Revoke current process as a candidate master process. 802 * 803 * @return Return ERR_OK if success, others fail. 804 */ 805 int32_t DemoteCurrentFromCandidateMasterProcess() override; 806 807 /** 808 * set browser channel for caller 809 */ 810 virtual void SaveBrowserChannel(sptr<IRemoteObject> browser) override; 811 812 /** 813 * Check caller is test ability 814 * 815 * @param pid, the pid of ability. 816 * @return Returns ERR_OK is test ability, others is not test ability. 817 */ 818 int32_t CheckCallingIsUserTestMode(const pid_t pid, bool &isUserTest) override; 819 820 #ifdef SUPPORT_CHILD_PROCESS 821 /** 822 * Start native child process, callde by ChildProcessManager. 823 * @param libName lib file name to be load in child process 824 * @param childProcessCount current started child process count 825 * @param callback callback for notify start result 826 * @return Returns ERR_OK on success, others on failure. 827 */ 828 int32_t CreateNativeChildProcess(const std::string &libName, int32_t childProcessCount, 829 const sptr<IRemoteObject> &callback, const std::string &customProcessName) override; 830 #endif // SUPPORT_CHILD_PROCESS 831 832 /** 833 * Register callback to notify native child exit. 834 * @param notify, callback to notify 835 * @return Returns ERR_OK on success, others on failure. 836 */ 837 virtual int32_t RegisterNativeChildExitNotify(const sptr<INativeChildNotify> notify) override; 838 839 /** 840 * Unregister callback to notify native child exit. 841 * @param notify, callback to notify 842 * @return Returns ERR_OK on success, others on failure. 843 */ 844 virtual int32_t UnregisterNativeChildExitNotify(const sptr<INativeChildNotify> notify) override; 845 846 /** 847 * Notify that the process depends on web by itself. 848 */ 849 virtual int32_t NotifyProcessDependedOnWeb() override; 850 851 /** 852 * Kill process depended on web by sa. 853 */ 854 virtual void KillProcessDependedOnWeb() override; 855 856 /** 857 * Restart resident process depended on web. 858 */ 859 virtual void RestartResidentProcessDependedOnWeb() override; 860 861 virtual int32_t KillAppSelfWithInstanceKey(const std::string &instanceKey, bool clearPageStack, 862 const std::string& reason) override; 863 864 virtual void UpdateInstanceKeyBySpecifiedId(int32_t specifiedId, std::string &instanceKey) override; 865 866 int32_t IsSpecifiedModuleLoaded(const AAFwk::Want &want, const AbilityInfo &abilityInfo, bool &result, 867 bool &isDebug) override; 868 869 /** 870 * update process rss and pss value. 871 * 872 * @param procMemStates, the memory states of all apps. 873 * @return ERR_OK, return back success, others fail. 874 */ 875 virtual int32_t UpdateProcessMemoryState(const std::vector<ProcessMemoryState> &procMemState) override; 876 877 /** 878 * Launch ability when reuseDelegatorWindow is true. 879 * 880 * @param token Ability identify. 881 * @return Returns ERR_OK on success, others on failure. 882 */ 883 int32_t LaunchAbility(sptr<IRemoteObject> token) override; 884 885 virtual int32_t QueryRunningSharedBundles(pid_t pid, std::map<std::string, uint32_t> &sharedBundles) override; 886 private: 887 /** 888 * Init, Initialize application services. 889 * 890 * @return ERR_OK, return back success, others fail. 891 */ 892 ErrCode Init(); 893 894 // the function that overrode from SystemAbility 895 /** 896 * OnStart, Start application service. 897 * 898 * @return 899 */ 900 virtual void OnStart() override; 901 902 /** 903 * OnStop, Stop application service. 904 * 905 * @return 906 */ 907 virtual void OnStop() override; 908 909 /** 910 * @brief Judge whether the application service is ready. 911 * 912 * @return Returns true means service is ready, otherwise service is not ready. 913 */ 914 bool IsReady() const; 915 916 /** 917 * SetInnerService, Setting application service Inner instance. 918 * 919 * @return 920 */ 921 void SetInnerService(const std::shared_ptr<AppMgrServiceInner> &innerService); 922 923 /** 924 * Register application or process state observer. 925 * @param observer, ability token. 926 * @return Returns ERR_OK on success, others on failure. 927 */ 928 virtual int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer, 929 const std::vector<std::string> &bundleNameList = {}) override; 930 931 /** 932 * Unregister application or process state observer. 933 * @param observer, ability token. 934 * @return Returns ERR_OK on success, others on failure. 935 */ 936 virtual int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer) override; 937 938 /** 939 * Register application or process state observer. 940 * @param observer, Is ability foreground state observer 941 * @return Returns ERR_OK on success, others on failure. 942 */ 943 int32_t RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer) override; 944 945 /** 946 * Unregister application or process state observer. 947 * @param observer, Is ability foreground state observer 948 * @return Returns ERR_OK on success, others on failure. 949 */ 950 int32_t UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer) override; 951 952 /** 953 * Get foreground applications. 954 * @param list, foreground apps. 955 * @return Returns ERR_OK on success, others on failure. 956 */ 957 virtual int32_t GetForegroundApplications(std::vector<AppStateData> &list) override; 958 959 int Dump(const std::vector<std::u16string>& args, std::string& result); 960 int ShowHelp(const std::vector<std::u16string>& args, std::string& result); 961 int DumpIpc(const std::vector<std::u16string>& args, std::string& result); 962 int DumpIpcAllStart(std::string& result); 963 int DumpIpcAllStop(std::string& result); 964 int DumpIpcAllStat(std::string& result); 965 int DumpIpcStart(const int32_t pid, std::string& result); 966 int DumpIpcStop(const int32_t pid, std::string& result); 967 int DumpIpcStat(const int32_t pid, std::string& result); 968 969 int DumpFfrt(const std::vector<std::u16string>& args, std::string& result); 970 971 bool JudgeAppSelfCalled(int32_t recordId); 972 973 /** 974 * @brief Notify AbilityManagerService the page show. 975 * @param token Ability identify. 976 * @param pageStateData The data of ability's page state. 977 * @return Returns ERR_OK on success, others on failure. 978 */ 979 virtual int32_t NotifyPageShow(const sptr<IRemoteObject> &token, const PageStateData &pageStateData) override; 980 981 /** 982 * @brief Notify AbilityManagerService the page hide. 983 * @param token Ability identify. 984 * @param pageStateData The data of ability's page state. 985 * @return Returns ERR_OK on success, others on failure. 986 */ 987 virtual int32_t NotifyPageHide(const sptr<IRemoteObject> &token, const PageStateData &pageStateData) override; 988 989 /** 990 * Check whether the bundle is running. 991 * 992 * @param bundleName Indicates the bundle name of the bundle. 993 * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise. 994 * @return Return ERR_OK if success, others fail. 995 */ 996 int32_t IsApplicationRunning(const std::string &bundleName, bool &isRunning) override; 997 998 /** 999 * Check whether the bundle is running. 1000 * 1001 * @param bundleName Indicates the bundle name of the bundle. 1002 * @param appCloneIndex the appindex of the bundle. 1003 * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise. 1004 * @return Return ERR_OK if success, others fail. 1005 */ 1006 int32_t IsAppRunning(const std::string &bundleName, int32_t appCloneIndex, bool &isRunning) override; 1007 1008 /** 1009 * Check whether the process of the application under the specified user exists. 1010 * 1011 * @param bundleName Indicates the bundle name of the bundle. 1012 * @param userId the userId of the bundle. 1013 * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise. 1014 * @return Return ERR_OK if success, others fail. 1015 */ 1016 int32_t IsAppRunningByBundleNameAndUserId(const std::string &bundleName, int32_t userId, bool &isRunning) override; 1017 1018 /** 1019 * Whether the current application process is the last surviving process. 1020 * 1021 * @return Returns true is final application process, others return false. 1022 */ 1023 bool IsFinalAppProcess() override; 1024 1025 /** 1026 * Get pids of processes which belong to specific bundle name and support process cache feature. 1027 * 1028 * @param bundleName bundle name. 1029 * @param pidList pid list of processes that support process cache. 1030 * @return Returns ERR_OK on success, others on failure. 1031 */ 1032 virtual int32_t GetSupportedProcessCachePids(const std::string &bundleName, 1033 std::vector<int32_t> &pidList) override; 1034 1035 /** 1036 * Launch Application Finished 1037 * @param recordId id of the app record 1038 */ 1039 virtual void PreloadModuleFinished(const int32_t recordId) override; 1040 1041 enum DumpIpcKey { 1042 KEY_DUMP_IPC_START = 0, 1043 KEY_DUMP_IPC_STOP, 1044 KEY_DUMP_IPC_STAT, 1045 }; 1046 1047 private: 1048 int DumpIpcAllInner(const AppMgrService::DumpIpcKey key, std::string& result); 1049 int DumpIpcWithPidInner(const AppMgrService::DumpIpcKey key, 1050 const std::string& optionPid, std::string& result); 1051 1052 int DumpFfrtInner(const std::string& pidsRaw, std::string& result); 1053 1054 private: 1055 AppMgrServiceState appMgrServiceState_; 1056 std::shared_ptr<AppMgrServiceInner> appMgrServiceInner_; 1057 std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_; 1058 std::shared_ptr<AMSEventHandler> eventHandler_; 1059 sptr<ISystemAbilityManager> systemAbilityMgr_; 1060 sptr<IAmsMgr> amsMgrScheduler_; 1061 1062 bool GetDumpIpcKeyByOption(const std::string &option, DumpIpcKey &key); 1063 1064 DISALLOW_COPY_AND_MOVE(AppMgrService); 1065 }; 1066 } // namespace AppExecFwk 1067 } // namespace OHOS 1068 #endif // OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H 1069