1 /* 2 * Copyright (c) 2021-2022 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_AAFWK_ABILITY_MANAGER_SERVICE_H 17 #define OHOS_AAFWK_ABILITY_MANAGER_SERVICE_H 18 19 #include <memory> 20 #include <shared_mutex> 21 #include <singleton.h> 22 #include <thread_ex.h> 23 #include <unordered_map> 24 #include <map> 25 26 #include "ability_connect_manager.h" 27 #include "ability_event_handler.h" 28 #include "ability_manager_stub.h" 29 #include "ability_stack_manager.h" 30 #include "app_scheduler.h" 31 #include "bundlemgr/bundle_mgr_interface.h" 32 #include "bundle_constants.h" 33 #include "data_ability_manager.h" 34 #include "hilog_wrapper.h" 35 #include "iremote_object.h" 36 #include "mission_list_manager.h" 37 #include "system_ability.h" 38 #include "uri.h" 39 #include "ability_config.h" 40 #include "pending_want_manager.h" 41 #include "ams_configuration_parameter.h" 42 #include "user_controller.h" 43 44 namespace OHOS { 45 namespace AAFwk { 46 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 47 const int32_t BASE_USER_RANGE = 200000; 48 using OHOS::AppExecFwk::IAbilityController; 49 50 class PendingWantManager; 51 /** 52 * @class AbilityManagerService 53 * AbilityManagerService provides a facility for managing ability life cycle. 54 */ 55 class AbilityManagerService : public SystemAbility, 56 public AbilityManagerStub, 57 public AppStateCallback, 58 public std::enable_shared_from_this<AbilityManagerService> { 59 DECLARE_DELAYED_SINGLETON(AbilityManagerService) 60 DECLEAR_SYSTEM_ABILITY(AbilityManagerService) 61 public: 62 void OnStart() override; 63 void OnStop() override; 64 ServiceRunningState QueryServiceState() const; 65 66 /** 67 * StartAbility with want, send want to ability manager service. 68 * 69 * @param want, the want of the ability to start. 70 * @param requestCode, Ability request code. 71 * @param userId, Designation User ID. 72 * @return Returns ERR_OK on success, others on failure. 73 */ 74 virtual int StartAbility( 75 const Want &want, int32_t userId = DEFAULT_INVAL_VALUE, int requestCode = DEFAULT_INVAL_VALUE) override; 76 77 /** 78 * StartAbility with want, send want to ability manager service. 79 * 80 * @param want, the want of the ability to start. 81 * @param callerToken, caller ability token. 82 * @param userId, Designation User ID. 83 * @param requestCode the resultCode of the ability to start. 84 * @return Returns ERR_OK on success, others on failure. 85 */ 86 virtual int StartAbility( 87 const Want &want, 88 const sptr<IRemoteObject> &callerToken, 89 int32_t userId = DEFAULT_INVAL_VALUE, 90 int requestCode = DEFAULT_INVAL_VALUE) override; 91 92 /** 93 * Starts a new ability with specific start settings. 94 * 95 * @param want Indicates the ability to start. 96 * @param abilityStartSetting Indicates the setting ability used to start. 97 * @param callerToken, caller ability token. 98 * @param userId, Designation User ID. 99 * @param requestCode the resultCode of the ability to start. 100 * @return Returns ERR_OK on success, others on failure. 101 */ 102 virtual int StartAbility( 103 const Want &want, 104 const AbilityStartSetting &abilityStartSetting, 105 const sptr<IRemoteObject> &callerToken, 106 int32_t userId = DEFAULT_INVAL_VALUE, 107 int requestCode = DEFAULT_INVAL_VALUE) override; 108 109 /** 110 * Starts a new ability with specific start options. 111 * 112 * @param want, the want of the ability to start. 113 * @param startOptions Indicates the options used to start. 114 * @param callerToken, caller ability token. 115 * @param userId, Designation User ID. 116 * @param requestCode the resultCode of the ability to start. 117 * @return Returns ERR_OK on success, others on failure. 118 */ 119 virtual int StartAbility( 120 const Want &want, 121 const StartOptions &startOptions, 122 const sptr<IRemoteObject> &callerToken, 123 int32_t userId = DEFAULT_INVAL_VALUE, 124 int requestCode = DEFAULT_INVAL_VALUE) override; 125 126 /** 127 * TerminateAbility, terminate the special ability. 128 * 129 * @param token, the token of the ability to terminate. 130 * @param resultCode, the resultCode of the ability to terminate. 131 * @param resultWant, the Want of the ability to return. 132 * @return Returns ERR_OK on success, others on failure. 133 */ 134 virtual int TerminateAbility(const sptr<IRemoteObject> &token, int resultCode = DEFAULT_INVAL_VALUE, 135 const Want *resultWant = nullptr) override; 136 137 /** 138 * TerminateAbility, terminate the special ability. 139 * 140 * @param callerToken, caller ability token. 141 * @param requestCode, Ability request code. 142 * @return Returns ERR_OK on success, others on failure. 143 */ 144 virtual int TerminateAbilityByCaller(const sptr<IRemoteObject> &callerToken, int requestCode) override; 145 146 /** 147 * CloseAbility, close the special ability. 148 * 149 * @param token, the token of the ability to terminate. 150 * @param resultCode, the resultCode of the ability to terminate. 151 * @param resultWant, the Want of the ability to return. 152 * @return Returns ERR_OK on success, others on failure. 153 */ 154 virtual int CloseAbility(const sptr<IRemoteObject> &token, int resultCode = DEFAULT_INVAL_VALUE, 155 const Want *resultWant = nullptr) override; 156 157 /** 158 * MinimizeAbility, minimize the special ability. 159 * 160 * @param token, ability token. 161 * @param fromUser mark the minimize operation source. 162 * @return Returns ERR_OK on success, others on failure. 163 */ 164 virtual int MinimizeAbility(const sptr<IRemoteObject> &token, bool fromUser = false) override; 165 166 /** 167 * ConnectAbility, connect session with service ability. 168 * 169 * @param want, Special want for service type's ability. 170 * @param connect, Callback used to notify caller the result of connecting or disconnecting. 171 * @param callerToken, caller ability token. 172 * @param userId, Designation User ID. 173 * @return Returns ERR_OK on success, others on failure. 174 */ 175 virtual int ConnectAbility( 176 const Want &want, 177 const sptr<IAbilityConnection> &connect, 178 const sptr<IRemoteObject> &callerToken, 179 int32_t userId = DEFAULT_INVAL_VALUE) override; 180 181 /** 182 * ContinueMission, continue ability from mission center. 183 * 184 * @param srcDeviceId, origin deviceId. 185 * @param dstDeviceId, target deviceId. 186 * @param missionId, indicates which ability to continue. 187 * @param callBack, notify result back. 188 * @param wantParams, extended params. 189 * @return Returns ERR_OK on success, others on failure. 190 */ 191 virtual int ContinueMission(const std::string &srcDeviceId, const std::string &dstDeviceId, 192 int32_t missionId, const sptr<IRemoteObject> &callBack, AAFwk::WantParams &wantParams) override; 193 194 /** 195 * ContinueAbility, continue ability to ability. 196 * 197 * @param deviceId, target deviceId. 198 * @param missionId, indicates which ability to continue. 199 * @return Returns ERR_OK on success, others on failure. 200 */ 201 virtual int ContinueAbility(const std::string &deviceId, int32_t missionId) override; 202 203 /** 204 * StartContinuation, continue ability to remote. 205 * 206 * @param want, Indicates the ability to start. 207 * @param abilityToken, Caller ability token. 208 * @param status, continue status. 209 * @return Returns ERR_OK on success, others on failure. 210 */ 211 virtual int StartContinuation(const Want &want, const sptr<IRemoteObject> &abilityToken, int32_t status) override; 212 213 /** 214 * NotifyCompleteContinuation, notify continuation complete to dms. 215 * @param deviceId, source device which start a continuation. 216 * @param sessionId, represent a continuaion. 217 * @param isSuccess, continuation result. 218 * @return 219 */ 220 virtual void NotifyCompleteContinuation(const std::string &deviceId, int32_t sessionId, bool isSuccess) override; 221 222 /** 223 * NotifyContinuationResult, notify continue result to ability. 224 * 225 * @param missionId, Caller mission id. 226 * @param result, continuation result. 227 * @return Returns ERR_OK on success, others on failure. 228 */ 229 virtual int NotifyContinuationResult(int32_t missionId, int32_t result) override; 230 231 /** 232 * RegisterMissionListener, register remote device mission listener. 233 * 234 * @param deviceId, Indicates the remote device Id. 235 * @param listener, listener. 236 * @return Returns ERR_OK on success, others on failure. 237 */ 238 virtual int RegisterMissionListener(const std::string &deviceId, 239 const sptr<IRemoteMissionListener> &listener) override; 240 241 /** 242 * UnRegisterMissionListener, unregister remote device mission listener. 243 * 244 * @param deviceId, Indicates the remote device Id. 245 * @param listener, listener. 246 * @return Returns ERR_OK on success, others on failure. 247 */ 248 virtual int UnRegisterMissionListener(const std::string &deviceId, 249 const sptr<IRemoteMissionListener> &listener)override; 250 251 virtual int DisconnectAbility(const sptr<IAbilityConnection> &connect) override; 252 253 /** 254 * AcquireDataAbility, acquire a data ability by its authority, if it not existed, 255 * AMS loads it synchronously. 256 * 257 * @param uri, data ability uri. 258 * @param tryBind, true: when a data ability is died, ams will kill this client, or do nothing. 259 * @param callerToken, specifies the caller ability token. 260 * @return returns the data ability ipc object, or nullptr for failed. 261 */ 262 virtual sptr<IAbilityScheduler> AcquireDataAbility( 263 const Uri &uri, bool tryBind, const sptr<IRemoteObject> &callerToken) override; 264 265 /** 266 * ReleaseDataAbility, release the data ability that referenced by 'dataAbilityToken'. 267 * 268 * @param dataAbilityToken, specifies the data ability that will be released. 269 * @param callerToken, specifies the caller ability token. 270 * @return returns ERR_OK if succeeded, or error codes for failed. 271 */ 272 virtual int ReleaseDataAbility( 273 sptr<IAbilityScheduler> dataAbilityScheduler, const sptr<IRemoteObject> &callerToken) override; 274 275 /** 276 * AddWindowInfo, add windowToken to AbilityRecord. 277 * 278 * @param token, the token of the ability. 279 * @param windowToken, window id of the ability. 280 */ 281 virtual void AddWindowInfo(const sptr<IRemoteObject> &token, int32_t windowToken) override; 282 283 /** 284 * AttachAbilityThread, ability call this interface after loaded. 285 * 286 * @param scheduler,.the interface handler of kit ability. 287 * @param token,.ability's token. 288 * @return Returns ERR_OK on success, others on failure. 289 */ 290 virtual int AttachAbilityThread( 291 const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token) override; 292 293 /** 294 * AbilityTransitionDone, ability call this interface after lift cycle was changed. 295 * 296 * @param token,.ability's token. 297 * @param state,.the state of ability lift cycle. 298 * @return Returns ERR_OK on success, others on failure. 299 */ 300 virtual int AbilityTransitionDone(const sptr<IRemoteObject> &token, int state, const PacMap &saveData) override; 301 302 /** 303 * ScheduleConnectAbilityDone, service ability call this interface while session was connected. 304 * 305 * @param token,.service ability's token. 306 * @param remoteObject,.the session proxy of service ability. 307 * @return Returns ERR_OK on success, others on failure. 308 */ 309 virtual int ScheduleConnectAbilityDone( 310 const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &remoteObject) override; 311 312 /** 313 * starting system ui ServiceExt abilites. 314 * 315 */ 316 void StartingSystemUiAbility(); 317 318 /** 319 * ScheduleDisconnectAbilityDone, service ability call this interface while session was disconnected. 320 * 321 * @param token,.service ability's token. 322 * @return Returns ERR_OK on success, others on failure. 323 */ 324 virtual int ScheduleDisconnectAbilityDone(const sptr<IRemoteObject> &token) override; 325 326 /** 327 * ScheduleCommandAbilityDone, service ability call this interface while session was commanded. 328 * 329 * @param token,.service ability's token. 330 * @return Returns ERR_OK on success, others on failure. 331 */ 332 virtual int ScheduleCommandAbilityDone(const sptr<IRemoteObject> &token) override; 333 334 /** 335 * GetEventHandler, get the ability manager service's handler. 336 * 337 * @return Returns AbilityEventHandler ptr. 338 */ 339 std::shared_ptr<AbilityEventHandler> GetEventHandler(); 340 341 /** 342 * SetStackManager, set the user id of stack manager. 343 * 344 * @param userId, user id. 345 */ 346 void SetStackManager(int userId, bool switchUser); 347 348 /** 349 * GetStackManager, get the current stack manager. 350 * 351 * @return Returns AbilityStackManager ptr. 352 */ 353 std::shared_ptr<AbilityStackManager> GetStackManager(); 354 355 /** 356 * DumpWaittingAbilityQueue. 357 * 358 * @param result, result. 359 */ 360 void DumpWaittingAbilityQueue(std::string &result); 361 362 /** 363 * dump ability stack info, about userID, mission stack info, 364 * mission record info and ability info. 365 * 366 * @param state Ability stack info. 367 * @return Returns ERR_OK on success, others on failure. 368 */ 369 virtual void DumpState(const std::string &args, std::vector<std::string> &info) override; 370 virtual void DumpSysState( 371 const std::string& args, std::vector<std::string>& info, bool isClient, bool isUserID, int UserID) override; 372 373 /** 374 * Obtains information about ability stack that are running on the device. 375 * 376 * @param stackInfo Ability stack info. 377 * @return Returns ERR_OK on success, others on failure. 378 */ 379 virtual int GetAllStackInfo(StackInfo &stackInfo) override; 380 381 /** 382 * Destroys this Service ability if the number of times it 383 * has been started equals the number represented by 384 * the given startId. 385 * 386 * @param token ability's token. 387 * @param startId is incremented by 1 every time this ability is started. 388 * @return Returns true if the startId matches the number of startup times 389 * and this Service ability will be destroyed; returns false otherwise. 390 */ 391 virtual int TerminateAbilityResult(const sptr<IRemoteObject> &token, int startId) override; 392 393 /** 394 * Destroys this Service ability by Want. 395 * 396 * @param want, Special want for service type's ability. 397 * @return Returns true if this Service ability will be destroyed; returns false otherwise. 398 */ 399 virtual int StopServiceAbility(const Want &want, int32_t userId = DEFAULT_INVAL_VALUE) override; 400 401 /** 402 * Get the list of the missions that the user has recently launched, 403 * with the most recent being first and older ones after in order. 404 * 405 * @param recentList recent mission info 406 * @param numMax The maximum number of entries to return in the list. The 407 * actual number returned may be smaller, depending on how many tasks the 408 * user has started and the maximum number the system can remember. 409 * @param falgs Information about what to return. May be any combination 410 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}. 411 * @return Returns ERR_OK on success, others on failure. 412 */ 413 virtual int GetRecentMissions( 414 const int32_t numMax, const int32_t flags, std::vector<AbilityMissionInfo> &recentList) override; 415 416 /** 417 * Get mission snapshot by mission id 418 * 419 * @param missionId the id of the mission to retrieve the sAutoapshots 420 * @return Returns ERR_OK on success, others on failure. 421 */ 422 virtual int GetMissionSnapshot(const int32_t missionId, MissionPixelMap &missionPixelMap) override; 423 424 /** 425 * Ask that the mission associated with a given mission ID be moved to the 426 * front of the stack, so it is now visible to the user. 427 * 428 * @param missionId. 429 * @return Returns ERR_OK on success, others on failure. 430 */ 431 virtual int MoveMissionToTop(int32_t missionId) override; 432 433 /** 434 * Requires that tasks associated with a given capability token be moved to the background 435 * 436 * @param token ability token 437 * @param nonFirst If nonfirst is false and not the lowest ability of the mission, you cannot move mission to end 438 * @return Returns ERR_OK on success, others on failure. 439 */ 440 virtual int MoveMissionToEnd(const sptr<IRemoteObject> &token, const bool nonFirst) override; 441 442 /** 443 * Remove the specified mission from the stack by mission id 444 * 445 * @param missionId. 446 * @return Returns ERR_OK on success, others on failure. 447 */ 448 virtual int RemoveMission(int id) override; 449 450 /** 451 * Kill the process immediately. 452 * 453 * @param bundleName. 454 * @return Returns ERR_OK on success, others on failure. 455 */ 456 virtual int KillProcess(const std::string &bundleName) override; 457 458 /** 459 * ClearUpApplicationData, call ClearUpApplicationData() through proxy project, 460 * clear the application data. 461 * 462 * @param bundleName, bundle name in Application record. 463 * @return ERR_OK, return back success, others fail. 464 */ 465 virtual int ClearUpApplicationData(const std::string &bundleName) override; 466 467 /** 468 * Uninstall app 469 * 470 * @param bundleName bundle name of uninstalling app. 471 * @param uid uid of bundle. 472 * @return Returns ERR_OK on success, others on failure. 473 */ 474 virtual int UninstallApp(const std::string &bundleName, int32_t uid) override; 475 476 /** 477 * Moving mission to the specified stack by mission option(Enter floating window mode). 478 * @param missionOption, target mission option 479 * @return Returns ERR_OK on success, others on failure. 480 */ 481 virtual int MoveMissionToFloatingStack(const MissionOption &missionOption) override; 482 483 /** 484 * Moving mission to the specified stack by mission option(Enter floating window mode). 485 * @param primary, display primary mission option 486 * @param secondary, display secondary mission option 487 * @return Returns ERR_OK on success, others on failure. 488 */ 489 virtual int MoveMissionToSplitScreenStack(const MissionOption &primary, const MissionOption &secondary) override; 490 491 /** 492 * Change the focus of ability in the mission stack. 493 * @param lostToken, the token of lost focus ability 494 * @param getToken, the token of get focus ability 495 * @return Returns ERR_OK on success, others on failure. 496 */ 497 virtual int ChangeFocusAbility( 498 const sptr<IRemoteObject> &lostFocusToken, const sptr<IRemoteObject> &getFocusToken) override; 499 500 /** 501 * minimize multiwindow by mission id. 502 * @param missionId, the id of target mission 503 * @return Returns ERR_OK on success, others on failure. 504 */ 505 virtual int MinimizeMultiWindow(int missionId) override; 506 507 /** 508 * maximize multiwindow by mission id. 509 * @param missionId, the id of target mission 510 * @return Returns ERR_OK on success, others on failure. 511 */ 512 virtual int MaximizeMultiWindow(int missionId) override; 513 514 /** 515 * get missions info of floating mission stack. 516 * @param list, mission info. 517 * @return Returns ERR_OK on success, others on failure. 518 */ 519 virtual int GetFloatingMissions(std::vector<AbilityMissionInfo> &list) override; 520 521 /** 522 * close multiwindow by mission id. 523 * @param missionId, the id of target mission. 524 * @return Returns ERR_OK on success, others on failure. 525 */ 526 virtual int CloseMultiWindow(int missionId) override; 527 528 /** 529 * set special mission stack default settings. 530 * @param stackSetting, mission stack default settings. 531 * @return Returns ERR_OK on success, others on failure. 532 */ 533 virtual int SetMissionStackSetting(const StackSetting &stackSetting) override; 534 535 /** 536 * @brief Checks whether this ability is the first ability in a mission. 537 * 538 * @return Returns true is first in Mission. 539 */ 540 virtual bool IsFirstInMission(const sptr<IRemoteObject> &token) override; 541 542 /** 543 * Save the top ability States and move them to the background 544 * @return Returns ERR_OK on success, others on failure. 545 */ 546 virtual int PowerOff() override; 547 548 /** 549 * Restore the state before top ability poweroff 550 * @return Returns ERR_OK on success, others on failure. 551 */ 552 virtual int PowerOn() override; 553 554 /** 555 * Sets the application to start its ability in lock mission mode. 556 * @param missionId luck mission id 557 * @return Returns ERR_OK on success, others on failure. 558 */ 559 virtual int LockMission(int missionId) override; 560 561 /** 562 * Unlocks this ability by exiting the lock mission mode. 563 * @param missionId unluck mission id 564 * @return Returns ERR_OK on success, others on failure. 565 */ 566 virtual int UnlockMission(int missionId) override; 567 568 /** 569 * Sets description information about the mission containing this ability. 570 * 571 * @param description Indicates the object containing information about the 572 * mission. This parameter cannot be null. 573 * @return Returns ERR_OK on success, others on failure. 574 */ 575 virtual int SetMissionDescriptionInfo( 576 const sptr<IRemoteObject> &token, const MissionDescriptionInfo &description) override; 577 578 /** 579 * get current system mission lock mode state. 580 * 581 * @return Returns 0: LOCK_MISSION_STATE_NONE, 1: LOCK_MISSION_STATE_LOCKED 582 */ 583 virtual int GetMissionLockModeState() override; 584 585 /** 586 * Updates the configuration by modifying the configuration. 587 * 588 * @param config Indicates the new configuration 589 * @return Returns ERR_OK on success, others on failure. 590 */ 591 virtual int UpdateConfiguration(const AppExecFwk::Configuration &config) override; 592 593 /** 594 * remove all service record. 595 * 596 */ 597 void RemoveAllServiceRecord(); 598 599 /** 600 * InitMissionListManager, set the user id of mission list manager. 601 * 602 * @param userId, user id. 603 */ 604 void InitMissionListManager(int userId, bool switchUser); 605 606 virtual sptr<IWantSender> GetWantSender( 607 const WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken) override; 608 609 virtual int SendWantSender(const sptr<IWantSender> &target, const SenderInfo &senderInfo) override; 610 611 virtual void CancelWantSender(const sptr<IWantSender> &sender) override; 612 613 virtual int GetPendingWantUid(const sptr<IWantSender> &target) override; 614 615 virtual int GetPendingWantUserId(const sptr<IWantSender> &target) override; 616 617 virtual std::string GetPendingWantBundleName(const sptr<IWantSender> &target) override; 618 619 virtual int GetPendingWantCode(const sptr<IWantSender> &target) override; 620 621 virtual int GetPendingWantType(const sptr<IWantSender> &target) override; 622 623 virtual void RegisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &receiver) override; 624 625 virtual void UnregisterCancelListener( 626 const sptr<IWantSender> &sender, const sptr<IWantReceiver> &receiver) override; 627 628 virtual int GetPendingRequestWant(const sptr<IWantSender> &target, std::shared_ptr<Want> &want) override; 629 630 virtual int GetWantSenderInfo(const sptr<IWantSender> &target, std::shared_ptr<WantSenderInfo> &info) override; 631 632 /** 633 * set lock screen Permit list 634 * 635 * @param isAllow whether to allow startup on lock screen. 636 * @return Returns ERR_OK on success, others on failure. 637 */ 638 virtual int SetShowOnLockScreen(bool isAllow) override; 639 640 virtual int LockMissionForCleanup(int32_t missionId) override; 641 642 virtual int UnlockMissionForCleanup(int32_t missionId) override; 643 644 virtual int RegisterMissionListener(const sptr<IMissionListener> &listener) override; 645 646 virtual int UnRegisterMissionListener(const sptr<IMissionListener> &listener) override; 647 648 virtual int GetMissionInfos(const std::string& deviceId, int32_t numMax, 649 std::vector<MissionInfo> &missionInfos) override; 650 651 virtual int GetMissionInfo(const std::string& deviceId, int32_t missionId, 652 MissionInfo &missionInfo) override; 653 654 virtual int CleanMission(int32_t missionId) override; 655 656 virtual int CleanAllMissions() override; 657 658 virtual int MoveMissionToFront(int32_t missionId) override; 659 660 virtual int MoveMissionToFront(int32_t missionId, const StartOptions &startOptions) override; 661 662 virtual int32_t GetMissionIdByToken(const sptr<IRemoteObject> &token) override; 663 664 virtual int StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag) override; 665 666 virtual int StopSyncRemoteMissions(const std::string& devId) override; 667 668 /** 669 * Get system memory information. 670 * @param SystemMemoryAttr, memory information. 671 */ 672 virtual void GetSystemMemoryAttr(AppExecFwk::SystemMemoryAttr &memoryInfo) override; 673 virtual int GetAppMemorySize() override; 674 675 virtual bool IsRamConstrainedDevice() override; 676 /** 677 * Start Ability, connect session with common ability. 678 * 679 * @param want, Special want for service type's ability. 680 * @param connect, Callback used to notify caller the result of connecting or disconnecting. 681 * @return Returns ERR_OK on success, others on failure. 682 */ 683 virtual int StartAbilityByCall( 684 const Want &want, const sptr<IAbilityConnection> &connect, const sptr<IRemoteObject> &callerToken) override; 685 686 /** 687 * Release Ability, disconnect session with common ability. 688 * 689 * @param connect, Callback used to notify caller the result of connecting or disconnecting. 690 * @param element, the element of target service. 691 * @return Returns ERR_OK on success, others on failure. 692 */ 693 virtual int ReleaseAbility( 694 const sptr<IAbilityConnection> &connect, const AppExecFwk::ElementName &element) override; 695 696 /** 697 * get service record by element name. 698 * 699 */ 700 std::shared_ptr<AbilityRecord> GetServiceRecordByElementName(const std::string &element); 701 std::list<std::shared_ptr<ConnectionRecord>> GetConnectRecordListByCallback(sptr<IAbilityConnection> callback); 702 703 void OnAbilityDied(std::shared_ptr<AbilityRecord> abilityRecord); 704 void OnCallConnectDied(std::shared_ptr<CallRecord> callRecord); 705 void GetMaxRestartNum(int &max); 706 bool IsUseNewMission(); 707 void HandleLoadTimeOut(int64_t eventId); 708 void HandleActiveTimeOut(int64_t eventId); 709 void HandleInactiveTimeOut(int64_t eventId); 710 void HandleForegroundNewTimeOut(int64_t eventId); 711 void HandleBackgroundNewTimeOut(int64_t eventId); 712 713 void RestartAbility(const sptr<IRemoteObject> &token); 714 void NotifyBmsAbilityLifeStatus( 715 const std::string &bundleName, const std::string &abilityName, const int64_t launchTime, const int uid); 716 717 int StartAbilityInner( 718 const Want &want, 719 const sptr<IRemoteObject> &callerToken, 720 int requestCode, 721 int callerUid = DEFAULT_INVAL_VALUE, 722 int32_t userId = DEFAULT_INVAL_VALUE); 723 724 int CheckPermission(const std::string &bundleName, const std::string &permission); 725 void UpdateLockScreenState(bool isLockScreen); 726 727 728 void OnAcceptWantResponse(const AAFwk::Want &want, const std::string &flag); 729 void OnStartSpecifiedAbilityTimeoutResponse(const AAFwk::Want &want); 730 731 virtual int GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info) override; 732 virtual int GetExtensionRunningInfos(int upperLimit, std::vector<ExtensionRunningInfo> &info) override; 733 virtual int GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> &info) override; 734 int GetProcessRunningInfosByUserId(std::vector<AppExecFwk::RunningProcessInfo> &info, int32_t userId); 735 void GetAbilityRunningInfo(std::vector<AbilityRunningInfo> &info, std::shared_ptr<AbilityRecord> &abilityRecord); 736 void GetExtensionRunningInfo(std::shared_ptr<AbilityRecord> &abilityRecord, const int32_t userId, 737 std::vector<ExtensionRunningInfo> &info); 738 739 int GetMissionSaveTime() const; 740 741 /** 742 * generate ability request. 743 * 744 */ 745 int GenerateAbilityRequest( 746 const Want &want, 747 int requestCode, 748 AbilityRequest &request, 749 const sptr<IRemoteObject> &callerToken, 750 int32_t userId); 751 752 /** 753 * Get mission id by target ability token. 754 * 755 * @param token target ability token. 756 * @return the missionId of target mission. 757 */ 758 int32_t GetMissionIdByAbilityToken(const sptr<IRemoteObject> &token); 759 760 /** 761 * Get ability token by target mission id. 762 * 763 * @param missionId target missionId. 764 * @return the ability token of target mission. 765 */ 766 sptr<IRemoteObject> GetAbilityTokenByMissionId(int32_t missionId); 767 768 virtual int StartUser(int userId) override; 769 770 virtual int StopUser(int userId, const sptr<IStopUserCallback> &callback) override; 771 772 virtual int SetMissionLabel(const sptr<IRemoteObject> &abilityToken, const std::string &label) override; 773 774 /** 775 * Called when client complete dump. 776 * 777 * @param infos The dump info. 778 * @param callerToken The caller ability token. 779 * @return Returns ERR_OK on success, others on failure. 780 */ 781 virtual int DumpAbilityInfoDone(std::vector<std::string> &infos, const sptr<IRemoteObject> &callerToken) override; 782 783 #ifdef SUPPORT_GRAPHICS 784 virtual int SetMissionIcon(const sptr<IRemoteObject> &token, 785 const std::shared_ptr<OHOS::Media::PixelMap> &icon) override; 786 #endif 787 788 void ClearUserData(int32_t userId); 789 790 virtual int RegisterSnapshotHandler(const sptr<ISnapshotHandler>& handler) override; 791 792 virtual int32_t GetMissionSnapshot(const std::string& deviceId, int32_t missionId, 793 MissionSnapshot& snapshot) override; 794 795 /** 796 * Set ability controller. 797 * 798 * @param abilityController, The ability controller. 799 * @return Returns ERR_OK on success, others on failure. 800 */ 801 virtual int SetAbilityController(const sptr<IAbilityController> &abilityController, bool imAStabilityTest) override; 802 803 /** 804 * Is user a stability test. 805 * 806 * @return Returns true if user is a stability test. 807 */ 808 virtual bool IsRunningInStabilityTest() override; 809 810 virtual int StartUserTest(const Want &want, const sptr<IRemoteObject> &observer) override; 811 812 virtual int FinishUserTest(const std::string &msg, const int &resultCode, const std::string &bundleName) override; 813 814 /** 815 * GetCurrentTopAbility, get the token of current top ability. 816 * 817 * @param token, the token of current top ability. 818 * @return Returns ERR_OK on success, others on failure. 819 */ 820 virtual int GetCurrentTopAbility(sptr<IRemoteObject> &token) override; 821 822 /** 823 * The delegator calls this interface to move the ability to the foreground. 824 * 825 * @param token, ability's token. 826 * @return Returns ERR_OK on success, others on failure. 827 */ 828 virtual int DelegatorDoAbilityForeground(const sptr<IRemoteObject> &token) override; 829 830 /** 831 * The delegator calls this interface to move the ability to the background. 832 * 833 * @param token, ability's token. 834 * @return Returns ERR_OK on success, others on failure. 835 */ 836 virtual int DelegatorDoAbilityBackground(const sptr<IRemoteObject> &token) override; 837 838 /** 839 * Calls this interface to move the ability to the foreground. 840 * 841 * @param token, ability's token. 842 * @param flag, use for lock or unlock flag and so on. 843 * @return Returns ERR_OK on success, others on failure. 844 */ 845 virtual int DoAbilityForeground(const sptr<IRemoteObject> &token, uint32_t flag) override; 846 847 /** 848 * Calls this interface to move the ability to the background. 849 * 850 * @param token, ability's token. 851 * @param flag, use for lock or unlock flag and so on. 852 * @return Returns ERR_OK on success, others on failure. 853 */ 854 virtual int DoAbilityBackground(const sptr<IRemoteObject> &token, uint32_t flag) override; 855 856 bool IsAbilityControllerStart(const Want &want, const std::string &bundleName); 857 858 bool IsAbilityControllerForeground(const std::string &bundleName); 859 860 bool IsAbilityControllerStartById(int32_t missionId); 861 862 void GrantUriPermission(const Want &want, int32_t validUserId, uint32_t targetTokenId); 863 864 /** 865 * Send not response process ID to ability manager service. 866 * @param pid The not response process ID. 867 * @return Returns ERR_OK on success, others on failure. 868 */ 869 virtual int SendANRProcessID(int pid) override; 870 871 /** 872 * force timeout ability. 873 * 874 * @param abilityName. 875 * @param state. 876 * @return Returns ERR_OK on success, others on failure. 877 */ 878 virtual int ForceTimeoutForTest(const std::string &abilityName, const std::string &state) override; 879 880 bool GetDataAbilityUri(const std::vector<AppExecFwk::AbilityInfo> &abilityInfos, 881 const std::string &mainAbility, std::string &uri); 882 883 virtual AppExecFwk::ElementName GetTopAbility() override; 884 885 // MSG 0 - 20 represents timeout message 886 static constexpr uint32_t LOAD_TIMEOUT_MSG = 0; 887 static constexpr uint32_t ACTIVE_TIMEOUT_MSG = 1; 888 static constexpr uint32_t INACTIVE_TIMEOUT_MSG = 2; 889 static constexpr uint32_t BACKGROUND_TIMEOUT_MSG = 3; 890 static constexpr uint32_t TERMINATE_TIMEOUT_MSG = 4; 891 static constexpr uint32_t FOREGROUNDNEW_TIMEOUT_MSG = 5; 892 static constexpr uint32_t BACKGROUNDNEW_TIMEOUT_MSG = 6; 893 894 static constexpr uint32_t LOAD_TIMEOUT = 3000; // ms 895 static constexpr uint32_t ACTIVE_TIMEOUT = 5000; // ms 896 static constexpr uint32_t INACTIVE_TIMEOUT = 500; // ms 897 static constexpr uint32_t BACKGROUND_TIMEOUT = 10000; // ms 898 static constexpr uint32_t TERMINATE_TIMEOUT = 10000; // ms 899 static constexpr uint32_t CONNECT_TIMEOUT = 3000; // ms 900 static constexpr uint32_t DISCONNECT_TIMEOUT = 500; // ms 901 static constexpr uint32_t COMMAND_TIMEOUT = 5000; // ms 902 static constexpr uint32_t SYSTEM_UI_TIMEOUT = 5000; // ms 903 static constexpr uint32_t RESTART_TIMEOUT = 5000; // ms 904 static constexpr uint32_t RESTART_ABILITY_TIMEOUT = 500; // ms 905 static constexpr uint32_t FOREGROUNDNEW_TIMEOUT = 5000; // ms 906 static constexpr uint32_t BACKGROUNDNEW_TIMEOUT = 3000; // ms 907 static constexpr uint32_t DUMP_TIMEOUT = 1000; // ms 908 909 static constexpr uint32_t MIN_DUMP_ARGUMENT_NUM = 2; 910 static constexpr uint32_t MAX_WAIT_SYSTEM_UI_NUM = 600; 911 static constexpr uint32_t MAX_WAIT_SETTINGS_DATA_NUM = 300; 912 913 enum DumpKey { 914 KEY_DUMP_ALL = 0, 915 KEY_DUMP_STACK_LIST, 916 KEY_DUMP_STACK, 917 KEY_DUMP_MISSION, 918 KEY_DUMP_TOP_ABILITY, 919 KEY_DUMP_WAIT_QUEUE, 920 KEY_DUMP_SERVICE, 921 KEY_DUMP_DATA, 922 KEY_DUMP_FOCUS_ABILITY, 923 KEY_DUMP_WINDOW_MODE, 924 KEY_DUMP_MISSION_LIST, 925 KEY_DUMP_MISSION_INFOS, 926 }; 927 928 enum DumpsysKey { 929 KEY_DUMPSYS_ALL = 0, 930 KEY_DUMPSYS_MISSION_LIST, 931 KEY_DUMPSYS_ABILITY, 932 KEY_DUMPSYS_SERVICE, 933 KEY_DUMPSYS_PENDING, 934 KEY_DUMPSYS_PROCESS, 935 KEY_DUMPSYS_DATA, 936 }; 937 938 friend class AbilityStackManager; 939 friend class UserController; 940 941 protected: 942 void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state) override; 943 int GetUidByBundleName(std::string bundleName); 944 945 void OnAppStateChanged(const AppInfo &info) override; 946 947 private: 948 int TerminateAbilityWithFlag(const sptr<IRemoteObject> &token, int resultCode = DEFAULT_INVAL_VALUE, 949 const Want *resultWant = nullptr, bool flag = true); 950 /** 951 * initialization of ability manager service. 952 * 953 */ 954 bool Init(); 955 /** 956 * initialization of u0 user. 957 * 958 */ 959 void InitU0User(); 960 /** 961 * start highest priority ability. 962 * 963 */ 964 void StartHighestPriorityAbility(bool isBoot); 965 /** 966 * starting settings data ability. 967 * 968 */ 969 void StartingSettingsDataAbility(); 970 /** 971 * starting screen lock ability. 972 * 973 */ 974 void StartingScreenLockAbility(); 975 /** 976 * starting phone service ability. 977 * 978 */ 979 void StartingPhoneServiceAbility(); 980 981 /** 982 * starting system ui abilites. 983 * 984 */ 985 void StartingSystemUiAbility(const SatrtUiMode &mode); 986 987 /** 988 * starting mms ability. 989 * 990 */ 991 void StartingMmsAbility(); 992 993 /** 994 * connet bms. 995 * 996 */ 997 void ConnectBmsService(); 998 999 /** 1000 * get the user id. 1001 * 1002 */ 1003 int GetUserId(); 1004 1005 /** 1006 * Determine whether it is a system APP 1007 * 1008 */ 1009 bool IsSystemUiApp(const AppExecFwk::AbilityInfo &info) const; 1010 /** 1011 * Select to start the application according to the configuration file of AMS 1012 * 1013 */ 1014 void StartSystemApplication(); 1015 /** 1016 * Get parameters from the global 1017 * 1018 */ 1019 void GetGlobalConfiguration(); 1020 1021 sptr<AppExecFwk::IBundleMgr> GetBundleManager(); 1022 int StartRemoteAbility(const Want &want, int requestCode); 1023 int ConnectLocalAbility( 1024 const Want &want, 1025 const int32_t userId, 1026 const sptr<IAbilityConnection> &connect, 1027 const sptr<IRemoteObject> &callerToken); 1028 int DisconnectLocalAbility(const sptr<IAbilityConnection> &connect); 1029 int ConnectRemoteAbility(const Want &want, const sptr<IRemoteObject> &connect); 1030 int DisconnectRemoteAbility(const sptr<IRemoteObject> &connect); 1031 int PreLoadAppDataAbilities(const std::string &bundleName, const int32_t userId); 1032 void UpdateCallerInfo(Want& want); 1033 1034 bool CheckIfOperateRemote(const Want &want); 1035 bool GetLocalDeviceId(std::string& localDeviceId); 1036 std::string AnonymizeDeviceId(const std::string& deviceId); 1037 bool VerificationToken(const sptr<IRemoteObject> &token); 1038 void RequestPermission(const Want *resultWant); 1039 1040 bool CheckIsRemote(const std::string& deviceId); 1041 int GetRemoteMissionInfos(const std::string& deviceId, int32_t numMax, 1042 std::vector<MissionInfo> &missionInfos); 1043 int GetRemoteMissionInfo(const std::string& deviceId, int32_t missionId, 1044 MissionInfo &missionInfo); 1045 int32_t GetRemoteMissionSnapshotInfo(const std::string& deviceId, int32_t missionId, 1046 MissionSnapshot& missionSnapshot); 1047 int StartRemoteAbilityByCall(const Want &want, const sptr<IRemoteObject> &connect); 1048 int ReleaseRemoteAbility(const sptr<IRemoteObject> &connect, const AppExecFwk::ElementName &element); 1049 1050 void DumpInner(const std::string &args, std::vector<std::string> &info); 1051 void DumpStackListInner(const std::string &args, std::vector<std::string> &info); 1052 void DumpStackInner(const std::string &args, std::vector<std::string> &info); 1053 void DumpMissionInner(const std::string &args, std::vector<std::string> &info); 1054 void DumpTopAbilityInner(const std::string &args, std::vector<std::string> &info); 1055 void DumpWaittingAbilityQueueInner(const std::string &args, std::vector<std::string> &info); 1056 void DumpStateInner(const std::string &args, std::vector<std::string> &info); 1057 void DataDumpStateInner(const std::string &args, std::vector<std::string> &info); 1058 void DumpFocusMapInner(const std::string &args, std::vector<std::string> &info); 1059 #ifdef SUPPORT_GRAPHICS 1060 void DumpWindowModeInner(const std::string &args, std::vector<std::string> &info); 1061 #endif 1062 void DumpMissionListInner(const std::string &args, std::vector<std::string> &info); 1063 void DumpMissionInfosInner(const std::string &args, std::vector<std::string> &info); 1064 void DumpFuncInit(); 1065 bool CheckCallerIsSystemAppByIpc(); 1066 bool IsExistFile(const std::string &path); 1067 1068 int CheckCallPermissions(const AbilityRequest &abilityRequest); 1069 1070 bool JudgeMultiUserConcurrency(const AppExecFwk::AbilityInfo &info, const int32_t userId); 1071 /** 1072 * dumpsys info 1073 * 1074 */ 1075 void DumpSysFuncInit(); 1076 void DumpSysInner( 1077 const std::string &args, std::vector<std::string> &info, bool isClient, bool isUserID, int userId); 1078 void DumpSysMissionListInner( 1079 const std::string &args, std::vector<std::string> &info, bool isClient, bool isUserID, int userId); 1080 void DumpSysAbilityInner( 1081 const std::string &args, std::vector<std::string> &info, bool isClient, bool isUserID, int userId); 1082 void DumpSysStateInner( 1083 const std::string &args, std::vector<std::string> &info, bool isClient, bool isUserID, int userId); 1084 void DumpSysPendingInner( 1085 const std::string &args, std::vector<std::string> &info, bool isClient, bool isUserID, int userId); 1086 void DumpSysProcess( 1087 const std::string &args, std::vector<std::string> &info, bool isClient, bool isUserID, int userId); 1088 void DataDumpSysStateInner( 1089 const std::string &args, std::vector<std::string> &info, bool isClient, bool isUserID, int userId); 1090 1091 void InitConnectManager(int32_t userId, bool switchUser); 1092 void InitDataAbilityManager(int32_t userId, bool switchUser); 1093 void InitPendWantManager(int32_t userId, bool switchUser); 1094 1095 int32_t InitAbilityInfoFromExtension(AppExecFwk::ExtensionAbilityInfo &extensionInfo, 1096 AppExecFwk::AbilityInfo &abilityInfo); 1097 #ifdef SUPPORT_GRAPHICS 1098 int32_t ShowPickerDialog(const Want& want, int32_t userId); 1099 #endif 1100 1101 // multi user 1102 void StartFreezingScreen(); 1103 void StopFreezingScreen(); 1104 void UserStarted(int32_t userId); 1105 void SwitchToUser(int32_t userId); 1106 void StartLauncherAbility(int32_t userId); 1107 void SwitchToUser(int32_t oldUserId, int32_t userId); 1108 void SwitchManagers(int32_t userId, bool switchUser = true); 1109 void StartUserApps(int32_t userId, bool isBoot); 1110 void StartSystemAbilityByUser(int32_t userId, bool isBoot); 1111 void PauseOldUser(int32_t userId); 1112 void PauseOldStackManager(int32_t userId); 1113 void PauseOldMissionListManager(int32_t userId); 1114 void PauseOldConnectManager(int32_t userId); 1115 bool IsSystemUI(const std::string &bundleName) const; 1116 1117 bool VerificationAllToken(const sptr<IRemoteObject> &token); 1118 std::shared_ptr<DataAbilityManager> GetDataAbilityManager(const sptr<IAbilityScheduler> &scheduler); 1119 bool CheckDataAbilityRequest(AbilityRequest &abilityRequest); 1120 std::shared_ptr<AbilityStackManager> GetStackManagerByUserId(int32_t userId); 1121 std::shared_ptr<MissionListManager> GetListManagerByUserId(int32_t userId); 1122 std::shared_ptr<AbilityConnectManager> GetConnectManagerByUserId(int32_t userId); 1123 std::shared_ptr<DataAbilityManager> GetDataAbilityManagerByUserId(int32_t userId); 1124 std::shared_ptr<AbilityStackManager> GetStackManagerByToken(const sptr<IRemoteObject> &token); 1125 std::shared_ptr<AbilityConnectManager> GetConnectManagerByToken(const sptr<IRemoteObject> &token); 1126 std::shared_ptr<DataAbilityManager> GetDataAbilityManagerByToken(const sptr<IRemoteObject> &token); 1127 1128 int32_t GetValidUserId(const int32_t userId); 1129 1130 int DelegatorMoveMissionToFront(int32_t missionId); 1131 1132 bool IsNeedTimeoutForTest(const std::string &abilityName, const std::string &state) const; 1133 1134 void StartupResidentProcess(int userId); 1135 1136 int VerifyMissionPermission(); 1137 1138 int VerifyAccountPermission(int32_t userId); 1139 1140 using DumpFuncType = void (AbilityManagerService::*)(const std::string &args, std::vector<std::string> &info); 1141 std::map<uint32_t, DumpFuncType> dumpFuncMap_; 1142 1143 using DumpSysFuncType = void (AbilityManagerService::*)( 1144 const std::string& args, std::vector<std::string>& state, bool isClient, bool isUserID, int UserID); 1145 std::map<uint32_t, DumpSysFuncType> dumpsysFuncMap_; 1146 1147 int CheckStaticCfgPermission(AppExecFwk::AbilityInfo &abilityInfo); 1148 void GrantUriPermission(const Want &want, int32_t validUserId); 1149 bool VerifyUriPermisson(const AbilityRequest &abilityRequest, const Want &want); 1150 1151 bool SetANRMissionByProcessID(int pid); 1152 1153 void StartMainElement(int userId, std::vector<AppExecFwk::BundleInfo> &bundleInfos); 1154 1155 bool GetValidDataAbilityUri(const std::string &abilityInfoUri, std::string &adjustUri); 1156 1157 constexpr static int REPOLL_TIME_MICRO_SECONDS = 1000000; 1158 constexpr static int WAITING_BOOT_ANIMATION_TIMER = 5; 1159 1160 std::shared_ptr<AppExecFwk::EventRunner> eventLoop_; 1161 std::shared_ptr<AbilityEventHandler> handler_; 1162 ServiceRunningState state_; 1163 std::unordered_map<int, std::shared_ptr<AbilityStackManager>> stackManagers_; 1164 std::shared_ptr<AbilityStackManager> currentStackManager_; 1165 std::unordered_map<int, std::shared_ptr<AbilityConnectManager>> connectManagers_; 1166 std::shared_ptr<AbilityConnectManager> connectManager_; 1167 sptr<AppExecFwk::IBundleMgr> iBundleManager_; 1168 std::shared_ptr<AppScheduler> appScheduler_; 1169 std::unordered_map<int, std::shared_ptr<DataAbilityManager>> dataAbilityManagers_; 1170 std::shared_ptr<DataAbilityManager> dataAbilityManager_; 1171 std::shared_ptr<DataAbilityManager> systemDataAbilityManager_; 1172 std::unordered_map<int, std::shared_ptr<PendingWantManager>> pendingWantManagers_; 1173 std::shared_ptr<PendingWantManager> pendingWantManager_; 1174 std::shared_ptr<AmsConfigurationParameter> amsConfigResolver_; 1175 const static std::map<std::string, AbilityManagerService::DumpKey> dumpMap; 1176 const static std::map<std::string, AbilityManagerService::DumpsysKey> dumpsysMap; 1177 1178 // new ams here 1179 bool useNewMission_ {false}; 1180 std::unordered_map<int, std::shared_ptr<MissionListManager>> missionListManagers_; 1181 std::shared_ptr<MissionListManager> currentMissionListManager_; 1182 std::shared_ptr<UserController> userController_; 1183 sptr<AppExecFwk::IAbilityController> abilityController_ = nullptr; 1184 bool controllerIsAStabilityTest_ = false; 1185 std::recursive_mutex globalLock_; 1186 std::shared_mutex managersMutex_; 1187 1188 std::multimap<std::string, std::string> timeoutMap_; 1189 }; 1190 1191 } // namespace AAFwk 1192 } // namespace OHOS 1193 #endif // OHOS_AAFWK_ABILITY_MANAGER_SERVICE_H 1194