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_ABILITY_RECORD_H 17 #define OHOS_ABILITY_RUNTIME_ABILITY_RECORD_H 18 19 #include <ctime> 20 #include <functional> 21 #include <list> 22 #include <memory> 23 #include <vector> 24 #include <set> 25 #include <utility> 26 #include "cpp/mutex.h" 27 #include "cpp/condition_variable.h" 28 29 #include "ability_connect_callback_interface.h" 30 #include "ability_info.h" 31 #include "ability_start_setting.h" 32 #include "ability_state.h" 33 #include "ability_token_stub.h" 34 #include "app_scheduler.h" 35 #include "application_info.h" 36 #include "bundlemgr/bundle_mgr_interface.h" 37 #include "call_container.h" 38 #include "exit_reason.h" 39 #include "ipc_skeleton.h" 40 #include "lifecycle_deal.h" 41 #include "lifecycle_state_info.h" 42 #include "session_info.h" 43 #include "ui_extension_window_command.h" 44 #include "uri.h" 45 #include "want.h" 46 #include "window_config.h" 47 #ifdef SUPPORT_GRAPHICS 48 #include "ability_window_configuration.h" 49 #include "resource_manager.h" 50 #include "start_options.h" 51 #include "window_manager_service_handler.h" 52 #endif 53 54 namespace OHOS { 55 namespace AAFwk { 56 using Closure = std::function<void()>; 57 58 class AbilityRecord; 59 class ConnectionRecord; 60 class CallContainer; 61 62 constexpr const char* ABILITY_TOKEN_NAME = "AbilityToken"; 63 constexpr const char* LAUNCHER_BUNDLE_NAME = "com.ohos.launcher"; 64 65 /** 66 * @class Token 67 * Token is identification of ability and used to interact with kit and wms. 68 */ 69 class Token : public AbilityTokenStub { 70 public: 71 explicit Token(std::weak_ptr<AbilityRecord> abilityRecord); 72 virtual ~Token(); 73 74 std::shared_ptr<AbilityRecord> GetAbilityRecord() const; 75 static std::shared_ptr<AbilityRecord> GetAbilityRecordByToken(const sptr<IRemoteObject> &token); 76 77 private: 78 std::weak_ptr<AbilityRecord> abilityRecord_; // ability of this token 79 }; 80 81 /** 82 * @class AbilityResult 83 * Record requestCode of for-result start mode and result. 84 */ 85 class AbilityResult { 86 public: 87 AbilityResult() = default; AbilityResult(int requestCode, int resultCode, const Want &resultWant)88 AbilityResult(int requestCode, int resultCode, const Want &resultWant) 89 : requestCode_(requestCode), resultCode_(resultCode), resultWant_(resultWant) 90 {} ~AbilityResult()91 virtual ~AbilityResult() 92 {} 93 94 int requestCode_ = -1; // requestCode of for-result start mode 95 int resultCode_ = -1; // resultCode of for-result start mode 96 Want resultWant_; // for-result start mode ability will send the result to caller 97 }; 98 99 /** 100 * @class SystemAbilityCallerRecord 101 * Record system caller ability of for-result start mode and result. 102 */ 103 class SystemAbilityCallerRecord { 104 public: SystemAbilityCallerRecord(std::string &srcAbilityId, const sptr<IRemoteObject> &callerToken)105 SystemAbilityCallerRecord(std::string &srcAbilityId, const sptr<IRemoteObject> &callerToken) 106 : srcAbilityId_(srcAbilityId), callerToken_(callerToken) 107 {} ~SystemAbilityCallerRecord()108 virtual ~SystemAbilityCallerRecord() 109 {} 110 GetSrcAbilityId()111 std::string GetSrcAbilityId() 112 { 113 return srcAbilityId_; 114 } GetCallerToken()115 const sptr<IRemoteObject> GetCallerToken() 116 { 117 return callerToken_; 118 } SetResult(Want &want, int resultCode)119 void SetResult(Want &want, int resultCode) 120 { 121 resultWant_ = want; 122 resultCode_ = resultCode; 123 } GetResultWant()124 Want &GetResultWant() 125 { 126 return resultWant_; 127 } GetResultCode()128 int &GetResultCode() 129 { 130 return resultCode_; 131 } 132 /** 133 * Set result to system ability. 134 * 135 */ 136 void SetResultToSystemAbility(std::shared_ptr<SystemAbilityCallerRecord> callerSystemAbilityRecord, 137 Want &resultWant, int resultCode); 138 /** 139 * Send result to system ability. 140 * 141 */ 142 void SendResultToSystemAbility(int requestCode, 143 const std::shared_ptr<SystemAbilityCallerRecord> callerSystemAbilityRecord, 144 int32_t callerUid, uint32_t accessToken, bool schedulerdied); 145 146 private: 147 std::string srcAbilityId_; 148 sptr<IRemoteObject> callerToken_; 149 Want resultWant_; 150 int resultCode_ = -1; 151 }; 152 153 /** 154 * @struct CallerAbilityInfo 155 * caller ability info. 156 */ 157 struct CallerAbilityInfo { 158 public: 159 std::string callerBundleName; 160 std::string callerAbilityName; 161 int32_t callerTokenId = 0; 162 int32_t callerUid = 0; 163 int32_t callerPid = 0; 164 std::string callerNativeName; 165 }; 166 167 /** 168 * @class CallerRecord 169 * Record caller ability of for-result start mode and result. 170 */ 171 class CallerRecord { 172 public: 173 CallerRecord() = default; 174 CallerRecord(int requestCode, std::weak_ptr<AbilityRecord> caller); CallerRecord(int requestCode, std::shared_ptr<SystemAbilityCallerRecord> saCaller)175 CallerRecord(int requestCode, std::shared_ptr<SystemAbilityCallerRecord> saCaller) : requestCode_(requestCode), 176 saCaller_(saCaller) 177 {} ~CallerRecord()178 virtual ~CallerRecord() 179 {} 180 GetRequestCode()181 int GetRequestCode() 182 { 183 return requestCode_; 184 } GetCaller()185 std::shared_ptr<AbilityRecord> GetCaller() 186 { 187 return caller_.lock(); 188 } GetSaCaller()189 std::shared_ptr<SystemAbilityCallerRecord> GetSaCaller() 190 { 191 return saCaller_; 192 } GetCallerInfo()193 std::shared_ptr<CallerAbilityInfo> GetCallerInfo() 194 { 195 return callerInfo_; 196 } IsHistoryRequestCode(int32_t requestCode)197 bool IsHistoryRequestCode(int32_t requestCode) 198 { 199 return requestCodeSet_.count(requestCode) > 0; 200 } RemoveHistoryRequestCode(int32_t requestCode)201 void RemoveHistoryRequestCode(int32_t requestCode) 202 { 203 requestCodeSet_.erase(requestCode); 204 } AddHistoryRequestCode(int32_t requestCode)205 void AddHistoryRequestCode(int32_t requestCode) 206 { 207 requestCodeSet_.insert(requestCode); 208 } SetRequestCodeSet(const std::set<int32_t> &requestCodeSet)209 void SetRequestCodeSet(const std::set<int32_t> &requestCodeSet) 210 { 211 requestCodeSet_ = requestCodeSet; 212 } GetRequestCodeSet()213 std::set<int32_t> GetRequestCodeSet() 214 { 215 return requestCodeSet_; 216 } 217 218 private: 219 int requestCode_ = -1; // requestCode of for-result start mode 220 std::weak_ptr<AbilityRecord> caller_; 221 std::shared_ptr<SystemAbilityCallerRecord> saCaller_ = nullptr; 222 std::shared_ptr<CallerAbilityInfo> callerInfo_ = nullptr; 223 std::set<int32_t> requestCodeSet_; 224 }; 225 226 /** 227 * @class AbilityRequest 228 * Wrap parameters of starting ability. 229 */ 230 enum AbilityCallType { 231 INVALID_TYPE = 0, 232 CALL_REQUEST_TYPE, 233 START_OPTIONS_TYPE, 234 START_SETTINGS_TYPE, 235 START_EXTENSION_TYPE, 236 }; 237 238 enum CollaboratorType { 239 DEFAULT_TYPE = 0, 240 RESERVE_TYPE, 241 OTHERS_TYPE 242 }; 243 244 struct AbilityRequest { 245 Want want; 246 AppExecFwk::AbilityInfo abilityInfo; 247 AppExecFwk::ApplicationInfo appInfo; 248 int32_t uid = 0; 249 int requestCode = -1; 250 bool restart = false; 251 int32_t restartCount = -1; 252 int64_t restartTime = 0; 253 bool startRecent = false; 254 int32_t collaboratorType = CollaboratorType::DEFAULT_TYPE; 255 256 // call ability 257 int callerUid = -1; 258 AbilityCallType callType = AbilityCallType::INVALID_TYPE; 259 sptr<IRemoteObject> callerToken = nullptr; 260 sptr<IRemoteObject> asCallerSourceToken = nullptr; 261 uint32_t callerAccessTokenId = -1; 262 sptr<IAbilityConnection> connect = nullptr; 263 264 std::shared_ptr<AbilityStartSetting> startSetting = nullptr; 265 std::shared_ptr<ProcessOptions> processOptions = nullptr; 266 std::shared_ptr<StartWindowOption> startWindowOption = nullptr; 267 std::string specifiedFlag; 268 int32_t userId = -1; 269 bool callSpecifiedFlagTimeout = false; 270 sptr<IRemoteObject> abilityInfoCallback = nullptr; 271 272 AppExecFwk::ExtensionAbilityType extensionType = AppExecFwk::ExtensionAbilityType::UNSPECIFIED; 273 AppExecFwk::ExtensionProcessMode extensionProcessMode = AppExecFwk::ExtensionProcessMode::UNDEFINED; 274 275 sptr<SessionInfo> sessionInfo; 276 uint32_t specifyTokenId = 0; 277 bool uriReservedFlag = false; 278 std::string reservedBundleName; 279 bool isFromIcon = false; IsContinuationOHOS::AAFwk::AbilityRequest280 std::pair<bool, LaunchReason> IsContinuation() const 281 { 282 auto flags = want.GetFlags(); 283 if ((flags & Want::FLAG_ABILITY_CONTINUATION) == Want::FLAG_ABILITY_CONTINUATION) { 284 return {true, LaunchReason::LAUNCHREASON_CONTINUATION}; 285 } 286 if ((flags & Want::FLAG_ABILITY_PREPARE_CONTINUATION) == Want::FLAG_ABILITY_PREPARE_CONTINUATION) { 287 return {true, LaunchReason::LAUNCHREASON_PREPARE_CONTINUATION}; 288 } 289 return {false, LaunchReason::LAUNCHREASON_UNKNOWN}; 290 } 291 IsAcquireShareDataOHOS::AAFwk::AbilityRequest292 bool IsAcquireShareData() const 293 { 294 return want.GetBoolParam(Want::PARAM_ABILITY_ACQUIRE_SHARE_DATA, false); 295 } 296 IsAppRecoveryOHOS::AAFwk::AbilityRequest297 bool IsAppRecovery() const 298 { 299 return want.GetBoolParam(Want::PARAM_ABILITY_RECOVERY_RESTART, false); 300 } 301 IsCallTypeOHOS::AAFwk::AbilityRequest302 bool IsCallType(const AbilityCallType & type) const 303 { 304 return (callType == type); 305 } 306 DumpOHOS::AAFwk::AbilityRequest307 void Dump(std::vector<std::string> &state) 308 { 309 std::string dumpInfo = " want [" + want.ToUri() + "]"; 310 state.push_back(dumpInfo); 311 dumpInfo = " app name [" + abilityInfo.applicationName + "]"; 312 state.push_back(dumpInfo); 313 dumpInfo = " main name [" + abilityInfo.name + "]"; 314 state.push_back(dumpInfo); 315 dumpInfo = " request code [" + std::to_string(requestCode) + "]"; 316 state.push_back(dumpInfo); 317 } 318 VoluationOHOS::AAFwk::AbilityRequest319 void Voluation(const Want &srcWant, int srcRequestCode, 320 const sptr<IRemoteObject> &srcCallerToken, const std::shared_ptr<AbilityStartSetting> srcStartSetting = nullptr, 321 int srcCallerUid = -1) 322 { 323 want = srcWant; 324 requestCode = srcRequestCode; 325 callerToken = srcCallerToken; 326 startSetting = srcStartSetting; 327 callerUid = srcCallerUid == -1 ? IPCSkeleton::GetCallingUid() : srcCallerUid; 328 } 329 }; 330 331 // new version 332 enum ResolveResultType { 333 OK_NO_REMOTE_OBJ = 0, 334 OK_HAS_REMOTE_OBJ, 335 NG_INNER_ERROR, 336 }; 337 338 enum class AbilityWindowState { 339 FOREGROUND = 0, 340 BACKGROUND, 341 TERMINATE, 342 FOREGROUNDING, 343 BACKGROUNDING, 344 TERMINATING 345 }; 346 347 enum class AbilityVisibilityState { 348 INITIAL = 0, 349 FOREGROUND_HIDE, 350 FOREGROUND_SHOW, 351 UNSPECIFIED, 352 }; 353 354 struct LaunchDebugInfo { 355 public: 356 void Update(const Want &want); 357 358 bool isDebugAppSet = false; 359 bool isNativeDebugSet = false; 360 bool isPerfCmdSet = false; 361 bool debugApp = false; 362 bool nativeDebug = false; 363 std::string perfCmd; 364 }; 365 366 /** 367 * @class AbilityRecord 368 * AbilityRecord records ability info and states and used to schedule ability life. 369 */ 370 class AbilityRecord : public std::enable_shared_from_this<AbilityRecord> { 371 public: 372 AbilityRecord(const Want &want, const AppExecFwk::AbilityInfo &abilityInfo, 373 const AppExecFwk::ApplicationInfo &applicationInfo, int requestCode = -1); 374 375 virtual ~AbilityRecord(); 376 377 /** 378 * CreateAbilityRecord. 379 * 380 * @param abilityRequest,create ability record. 381 * @return Returns ability record ptr. 382 */ 383 static std::shared_ptr<AbilityRecord> CreateAbilityRecord(const AbilityRequest &abilityRequest); 384 385 /** 386 * Init ability record. 387 * 388 * @return Returns true on success, others on failure. 389 */ 390 bool Init(); 391 392 /** 393 * load UI ability. 394 * 395 */ 396 void LoadUIAbility(); 397 398 /** 399 * load ability. 400 * 401 * @return Returns ERR_OK on success, others on failure. 402 */ 403 int LoadAbility(); 404 405 /** 406 * foreground the ability. 407 * 408 */ 409 void ForegroundAbility(uint32_t sceneFlag = 0); 410 void ForegroundUIExtensionAbility(uint32_t sceneFlag = 0); 411 412 /** 413 * process request of foregrounding the ability. 414 * 415 */ 416 void ProcessForegroundAbility(uint32_t tokenId, uint32_t sceneFlag = 0); 417 418 /** 419 * post foreground timeout task for ui ability. 420 * 421 */ 422 void PostForegroundTimeoutTask(); 423 424 void RemoveForegroundTimeoutTask(); 425 426 void RemoveLoadTimeoutTask(); 427 428 void PostUIExtensionAbilityTimeoutTask(uint32_t messageId); 429 430 /** 431 * move the ability to back ground. 432 * 433 * @param task timeout task. 434 */ 435 void BackgroundAbility(const Closure &task); 436 437 /** 438 * prepare terminate ability. 439 * 440 * @return Returns true on stop terminating; returns false on terminate. 441 */ 442 bool PrepareTerminateAbility(); 443 444 /** 445 * terminate ability. 446 * 447 * @return Returns ERR_OK on success, others on failure. 448 */ 449 int TerminateAbility(); 450 451 /** 452 * get ability's info. 453 * 454 * @return ability info. 455 */ 456 const AppExecFwk::AbilityInfo &GetAbilityInfo() const; 457 458 /** 459 * get application's info. 460 * 461 * @return application info. 462 */ 463 const AppExecFwk::ApplicationInfo &GetApplicationInfo() const; 464 465 /** 466 * set ability's state. 467 * 468 * @param state, ability's state. 469 */ 470 void SetAbilityState(AbilityState state); 471 472 bool GetAbilityForegroundingFlag() const; 473 474 void SetAbilityForegroundingFlag(); 475 476 /** 477 * get ability's state. 478 * 479 * @return ability state. 480 */ 481 AbilityState GetAbilityState() const; 482 483 /** 484 * get ability's windowconfig. 485 * 486 * @return ability windowconfig. 487 */ 488 WindowConfig GetAbilityWindowConfig() const; 489 490 bool IsForeground() const; 491 492 AbilityVisibilityState GetAbilityVisibilityState() const; 493 void SetAbilityVisibilityState(AbilityVisibilityState state); 494 495 void UpdateAbilityVisibilityState(); 496 497 /** 498 * set ability scheduler for accessing ability thread. 499 * 500 * @param scheduler , ability scheduler. 501 */ 502 void SetScheduler(const sptr<IAbilityScheduler> &scheduler); 503 GetScheduler() const504 inline sptr<IAbilityScheduler> GetScheduler() const 505 { 506 return scheduler_; 507 } 508 509 sptr<SessionInfo> GetSessionInfo() const; 510 511 /** 512 * get ability's token. 513 * 514 * @return ability's token. 515 */ 516 sptr<Token> GetToken() const; 517 518 /** 519 * set ability's previous ability record. 520 * 521 * @param abilityRecord , previous ability record 522 */ 523 void SetPreAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord); 524 525 /** 526 * get ability's previous ability record. 527 * 528 * @return previous ability record 529 */ 530 std::shared_ptr<AbilityRecord> GetPreAbilityRecord() const; 531 532 /** 533 * set ability's next ability record. 534 * 535 * @param abilityRecord , next ability record 536 */ 537 void SetNextAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord); 538 539 /** 540 * get ability's previous ability record. 541 * 542 * @return previous ability record 543 */ 544 std::shared_ptr<AbilityRecord> GetNextAbilityRecord() const; 545 546 /** 547 * check whether the ability is ready. 548 * 549 * @return true : ready ,false: not ready 550 */ 551 bool IsReady() const; 552 void SetLoadState(AbilityLoadState loadState); 553 AbilityLoadState GetLoadState() const; 554 555 void UpdateRecoveryInfo(bool hasRecoverInfo); 556 557 bool GetRecoveryInfo(); 558 559 #ifdef SUPPORT_SCREEN 560 /** 561 * check whether the ability 's window is attached. 562 * 563 * @return true : attached ,false: not attached 564 */ 565 bool IsWindowAttached() const; 566 IsStartingWindow() const567 inline bool IsStartingWindow() const 568 { 569 return isStartingWindow_; 570 } 571 SetStartingWindow(bool isStartingWindow)572 inline void SetStartingWindow(bool isStartingWindow) 573 { 574 isStartingWindow_ = isStartingWindow; 575 } 576 577 void PostCancelStartingWindowHotTask(); 578 579 /** 580 * process request of foregrounding the ability. 581 * 582 */ 583 void ProcessForegroundAbility(bool isRecent, const AbilityRequest &abilityRequest, 584 std::shared_ptr<StartOptions> &startOptions, const std::shared_ptr<AbilityRecord> &callerAbility, 585 uint32_t sceneFlag = 0); 586 587 void ProcessForegroundAbility(const std::shared_ptr<AbilityRecord> &callerAbility, bool needExit = true, 588 uint32_t sceneFlag = 0); 589 void NotifyAnimationFromTerminatingAbility() const; 590 void NotifyAnimationFromMinimizeAbility(bool& animaEnabled); 591 592 bool ReportAtomicServiceDrawnCompleteEvent(); 593 void SetCompleteFirstFrameDrawing(const bool flag); 594 bool IsCompleteFirstFrameDrawing() const; 595 bool GetColdStartFlag(); 596 void SetColdStartFlag(bool isColdStart); 597 #endif 598 599 bool GrantUriPermissionForServiceExtension(); 600 601 bool GrantUriPermissionForUIExtension(); 602 603 /** 604 * check whether the ability is launcher. 605 * 606 * @return true : launcher ,false: not launcher 607 */ 608 bool IsLauncherAbility() const; 609 610 /** 611 * check whether the ability is terminating. 612 * 613 * @return true : yes ,false: not 614 */ 615 bool IsTerminating() const; 616 617 /** 618 * set the ability is terminating. 619 * 620 */ 621 void SetTerminatingState(); 622 623 /** 624 * set the ability is new want flag. 625 * 626 * @return isNewWant 627 */ 628 void SetIsNewWant(bool isNewWant); 629 630 /** 631 * check whether the ability is new want flag. 632 * 633 * @return true : yes ,false: not 634 */ 635 bool IsNewWant() const; 636 637 /** 638 * check whether the ability is created by connect ability mode. 639 * 640 * @return true : yes ,false: not 641 */ 642 bool IsCreateByConnect() const; 643 644 /** 645 * set the ability is created by connect ability mode. 646 * 647 */ 648 void SetCreateByConnectMode(bool isCreatedByConnect = true); 649 650 /** 651 * active the ability. 652 * 653 */ 654 virtual void Activate(); 655 656 /** 657 * inactive the ability. 658 * 659 */ 660 virtual void Inactivate(); 661 662 /** 663 * terminate the ability. 664 * 665 */ 666 void Terminate(const Closure &task); 667 668 /** 669 * connect the ability. 670 * 671 */ 672 void ConnectAbility(); 673 674 /** 675 * connect the ability with want. 676 * 677 */ 678 void ConnectAbilityWithWant(const Want &want); 679 680 /** 681 * disconnect the ability. 682 * 683 */ 684 void DisconnectAbility(); 685 686 /** 687 * disconnect the ability with want 688 * 689 */ 690 void DisconnectAbilityWithWant(const Want &want); 691 692 /** 693 * Command the ability. 694 * 695 */ 696 void CommandAbility(); 697 698 void CommandAbilityWindow(const sptr<SessionInfo> &sessionInfo, WindowCommand winCmd); 699 700 /** 701 * save ability state. 702 * 703 */ 704 void SaveAbilityState(); 705 void SaveAbilityState(const PacMap &inState); 706 void SaveAbilityWindowConfig(const WindowConfig &windowConfig); 707 708 /** 709 * restore ability state. 710 * 711 */ 712 void RestoreAbilityState(); 713 714 /** 715 * notify top active ability updated. 716 * 717 */ 718 void TopActiveAbilityChanged(bool flag); 719 720 /** 721 * set the want for start ability. 722 * 723 */ 724 void SetWant(const Want &want); 725 726 /** 727 * get the want for start ability. 728 * 729 */ 730 Want GetWant() const; 731 732 /** 733 * remove signature info of want. 734 * 735 */ 736 void RemoveSignatureInfo(); 737 738 /** 739 * remove specified wantParam for start ability. 740 * 741 */ 742 void RemoveSpecifiedWantParam(const std::string &key); 743 744 /** 745 * get request code of the ability to start. 746 * 747 */ 748 int GetRequestCode() const; 749 750 /** 751 * set the result object of the ability which one need to be terminated. 752 * 753 */ 754 void SetResult(const std::shared_ptr<AbilityResult> &result); 755 756 /** 757 * get the result object of the ability which one need to be terminated. 758 * 759 */ 760 std::shared_ptr<AbilityResult> GetResult() const; 761 762 /** 763 * send result object to caller ability thread. 764 * 765 */ 766 void SendResult(bool isSandboxApp, uint32_t tokeId); 767 768 /** 769 * send result object to caller ability thread. 770 * 771 */ 772 void SendResultByBackToCaller(const std::shared_ptr<AbilityResult> &result); 773 774 /** 775 * send result object to caller ability thread for sandbox app file saving. 776 */ 777 void SendSandboxSavefileResult(const Want &want, int resultCode, int requestCode); 778 779 /** 780 * send result object to caller ability. 781 * 782 */ 783 void SendResultToCallers(bool schedulerdied = false); 784 785 /** 786 * save result object to caller ability. 787 * 788 */ 789 void SaveResultToCallers(const int resultCode, const Want *resultWant); 790 791 std::shared_ptr<AbilityRecord> GetCallerByRequestCode(int32_t requestCode, int32_t pid); 792 793 /** 794 * save result to caller ability. 795 * 796 */ 797 void SaveResult(int resultCode, const Want *resultWant, std::shared_ptr<CallerRecord> caller); 798 799 bool NeedConnectAfterCommand(); 800 801 /** 802 * add connect record to the list. 803 * 804 */ 805 void AddConnectRecordToList(const std::shared_ptr<ConnectionRecord> &connRecord); 806 807 /** 808 * get the list of connect record. 809 * 810 */ 811 std::list<std::shared_ptr<ConnectionRecord>> GetConnectRecordList() const; 812 813 /** 814 * get the list of connect record. 815 * 816 */ 817 std::list<std::shared_ptr<ConnectionRecord>> GetConnectingRecordList(); 818 819 /** 820 * get the count of In Progress record. 821 * 822 */ 823 uint32_t GetInProgressRecordCount(); 824 /** 825 * remove the connect record from list. 826 * 827 */ 828 void RemoveConnectRecordFromList(const std::shared_ptr<ConnectionRecord> &connRecord); 829 830 /** 831 * check whether connect list is empty. 832 * 833 */ 834 bool IsConnectListEmpty(); 835 836 void RemoveCallerRequestCode(std::shared_ptr<AbilityRecord> callerAbilityRecord, int32_t requestCode); 837 838 /** 839 * add caller record 840 * 841 */ 842 void AddCallerRecord(const sptr<IRemoteObject> &callerToken, int requestCode, const Want &want, 843 std::string srcAbilityId = "", uint32_t callingTokenId = 0); 844 845 /** 846 * get caller record to list. 847 * 848 */ 849 std::list<std::shared_ptr<CallerRecord>> GetCallerRecordList() const; 850 std::shared_ptr<AbilityRecord> GetCallerRecord() const; 851 852 std::shared_ptr<CallerAbilityInfo> GetCallerInfo() const; 853 854 /** 855 * get connecting record from list. 856 * 857 */ 858 std::shared_ptr<ConnectionRecord> GetConnectingRecord() const; 859 860 /** 861 * get disconnecting record from list. 862 * 863 */ 864 std::shared_ptr<ConnectionRecord> GetDisconnectingRecord() const; 865 866 /** 867 * convert ability state (enum type to string type). 868 * 869 */ 870 static std::string ConvertAbilityState(const AbilityState &state); 871 872 static std::string ConvertAppState(const AppState &state); 873 874 /** 875 * convert life cycle state to ability state . 876 * 877 */ 878 static int ConvertLifeCycleToAbilityState(const AbilityLifeCycleState &state); 879 880 /** 881 * get the ability record id. 882 * 883 */ GetRecordId() const884 inline int GetRecordId() const 885 { 886 return recordId_; 887 } 888 889 /** 890 * dump ability info. 891 * 892 */ 893 void Dump(std::vector<std::string> &info); 894 895 void DumpClientInfo(std::vector<std::string> &info, const std::vector<std::string> ¶ms, 896 bool isClient = false, bool dumpConfig = true) const; 897 898 /** 899 * Called when client complete dump. 900 * 901 * @param infos The dump info. 902 */ 903 void DumpAbilityInfoDone(std::vector<std::string> &infos); 904 905 /** 906 * dump ability state info. 907 * 908 */ 909 void DumpAbilityState(std::vector<std::string> &info, bool isClient, const std::vector<std::string> ¶ms); 910 911 void SetStartTime(); 912 913 int64_t GetStartTime() const; 914 915 /** 916 * dump service info. 917 * 918 */ 919 void DumpService(std::vector<std::string> &info, bool isClient = false) const; 920 921 /** 922 * dump service info. 923 * 924 */ 925 void DumpService(std::vector<std::string> &info, std::vector<std::string> ¶ms, bool isClient = false) const; 926 927 /** 928 * set connect remote object. 929 * 930 */ 931 void SetConnRemoteObject(const sptr<IRemoteObject> &remoteObject); 932 933 /** 934 * get connect remote object. 935 * 936 */ 937 sptr<IRemoteObject> GetConnRemoteObject() const; 938 939 void AddStartId(); 940 int GetStartId() const; 941 942 void SetIsUninstallAbility(); 943 /** 944 * Determine whether ability is uninstalled 945 * 946 * @return true: uninstalled false: installed 947 */ 948 bool IsUninstallAbility() const; 949 void ShareData(const int32_t &uniqueId); 950 void SetLauncherRoot(); 951 bool IsLauncherRoot() const; 952 bool IsAbilityState(const AbilityState &state) const; 953 bool IsActiveState() const; 954 955 void SetStartSetting(const std::shared_ptr<AbilityStartSetting> &setting); 956 std::shared_ptr<AbilityStartSetting> GetStartSetting() const; 957 958 void SetRestarting(const bool isRestart); 959 void SetRestarting(const bool isRestart, int32_t canReStartCount); 960 int32_t GetRestartCount() const; 961 void SetRestartCount(int32_t restartCount); 962 bool GetKeepAlive() const; SetKeepAliveBundle(bool value)963 void SetKeepAliveBundle(bool value) 964 { 965 keepAliveBundle_ = value; 966 } IsKeepAliveBundle() const967 bool IsKeepAliveBundle() const 968 { 969 return keepAliveBundle_; 970 } 971 void SetLoading(bool status); 972 bool IsLoading() const; 973 int64_t GetRestartTime(); 974 void SetRestartTime(const int64_t restartTime); 975 void SetAppIndex(const int32_t appIndex); 976 int32_t GetAppIndex() const; 977 bool IsRestarting() const; 978 void SetAppState(const AppState &state); 979 AppState GetAppState() const; 980 981 void SetLaunchReason(const LaunchReason &reason); 982 void SetLastExitReason(const ExitReason &exitReason); 983 void ContinueAbility(const std::string &deviceId, uint32_t versionCode); 984 void NotifyContinuationResult(int32_t result); 985 986 void SetMissionId(int32_t missionId); 987 int32_t GetMissionId() const; 988 989 void SetUid(int32_t uid); 990 int32_t GetUid(); 991 int32_t GetPid(); 992 void SetSwitchingPause(bool state); 993 bool IsSwitchingPause(); 994 void SetOwnerMissionUserId(int32_t userId); 995 int32_t GetOwnerMissionUserId(); 996 997 // new version 998 ResolveResultType Resolve(const AbilityRequest &abilityRequest); 999 bool ReleaseCall(const sptr<IAbilityConnection> &connect); 1000 bool IsNeedToCallRequest() const; 1001 bool IsStartedByCall() const; 1002 void SetStartedByCall(const bool isFlag); 1003 void CallRequest(); 1004 bool CallRequestDone(const sptr<IRemoteObject> &callStub) const; 1005 bool IsStartToBackground() const; 1006 void SetStartToBackground(const bool flag); 1007 bool IsStartToForeground() const; 1008 void SetStartToForeground(const bool flag); 1009 void SetSessionInfo(sptr<SessionInfo> sessionInfo); 1010 void UpdateSessionInfo(sptr<IRemoteObject> sessionToken); 1011 void SetMinimizeReason(bool fromUser); 1012 void SetSceneFlag(uint32_t sceneFlag); 1013 bool IsMinimizeFromUser() const; 1014 void SetClearMissionFlag(bool clearMissionFlag); 1015 bool IsClearMissionFlag(); 1016 1017 void SetSpecifiedFlag(const std::string &flag); 1018 std::string GetSpecifiedFlag() const; 1019 void SetWindowMode(int32_t windowMode); 1020 void RemoveWindowMode(); 1021 LifeCycleStateInfo lifeCycleStateInfo_; // target life state info 1022 1023 bool CanRestartRootLauncher(); 1024 1025 bool CanRestartResident(); 1026 1027 std::string GetLabel(); GetAbilityRecordId() const1028 inline int64_t GetAbilityRecordId() const 1029 { 1030 return recordId_; 1031 } 1032 1033 void SetPendingState(AbilityState state); 1034 AbilityState GetPendingState() const; 1035 1036 bool IsNeedBackToOtherMissionStack(); 1037 void SetNeedBackToOtherMissionStack(bool isNeedBackToOtherMissionStack); 1038 std::shared_ptr<AbilityRecord> GetOtherMissionStackAbilityRecord() const; 1039 void SetOtherMissionStackAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord); 1040 void RevokeUriPermission(); 1041 void RemoveAbilityDeathRecipient() const; 1042 bool IsExistConnection(const sptr<IAbilityConnection> &connect); 1043 1044 int32_t GetCollaboratorType() const; 1045 1046 std::string GetMissionAffinity() const; 1047 1048 void SetLockedState(bool lockedState); 1049 bool GetLockedState(); 1050 1051 void SetAttachDebug(const bool isAttachDebug); 1052 void SetAssertDebug(bool isAssertDebug); 1053 int32_t CreateModalUIExtension(const Want &want); 1054 1055 AppExecFwk::ElementName GetElementName() const; 1056 bool IsDebugApp() const; 1057 bool IsDebug() const; 1058 1059 void AddAbilityWindowStateMap(uint64_t uiExtensionComponentId, 1060 AbilityWindowState abilityWindowState); 1061 1062 void RemoveAbilityWindowStateMap(uint64_t uiExtensionComponentId); 1063 1064 bool IsAbilityWindowReady(); 1065 1066 void SetAbilityWindowState(const sptr<SessionInfo> &sessionInfo, 1067 WindowCommand winCmd, bool isFinished); 1068 1069 void SetUIExtensionAbilityId(const int32_t uiExtensionAbilityId); 1070 int32_t GetUIExtensionAbilityId() const; 1071 1072 void OnProcessDied(); 1073 1074 void SetProcessName(const std::string &process); 1075 1076 std::string GetProcessName() const; 1077 1078 void SetURI(const std::string &uri); 1079 std::string GetURI() const; 1080 1081 void DoBackgroundAbilityWindowDelayed(bool needBackground); 1082 bool BackgroundAbilityWindowDelayed(); 1083 1084 bool IsSceneBoard() const; 1085 1086 void SetRestartAppFlag(bool isRestartApp); 1087 bool GetRestartAppFlag() const; 1088 1089 void UpdateUIExtensionInfo(const WantParams &wantParams); 1090 1091 void SetSpecifyTokenId(const uint32_t specifyTokenId); 1092 1093 void SaveConnectWant(const Want &want); 1094 1095 void UpdateConnectWant(); 1096 1097 void RemoveConnectWant(); 1098 1099 void UpdateDmsCallerInfo(Want &want); 1100 GetInstanceKey() const1101 inline std::string GetInstanceKey() const 1102 { 1103 return instanceKey_; 1104 } 1105 SetInstanceKey(const std::string& key)1106 void SetInstanceKey(const std::string& key) 1107 { 1108 instanceKey_ = key; 1109 } 1110 SetSecurityFlag(bool securityFlag)1111 void SetSecurityFlag(bool securityFlag) 1112 { 1113 securityFlag_ = securityFlag; 1114 } 1115 GetSecurityFlag() const1116 bool GetSecurityFlag() const 1117 { 1118 return securityFlag_; 1119 } 1120 GetFreezeStrategy() const1121 FreezeStrategy GetFreezeStrategy() const 1122 { 1123 return freezeStrategy_; 1124 } 1125 SetFreezeStrategy(FreezeStrategy value)1126 void SetFreezeStrategy(FreezeStrategy value) 1127 { 1128 freezeStrategy_ = value; 1129 } 1130 1131 protected: 1132 void SendEvent(uint32_t msg, uint32_t timeOut, int32_t param = -1, bool isExtension = false); 1133 1134 sptr<Token> token_ = {}; // used to interact with kit and wms 1135 std::unique_ptr<LifecycleDeal> lifecycleDeal_ = {}; // life manager used to schedule life 1136 AbilityState currentState_ = AbilityState::INITIAL; // current life state 1137 Want want_ = {}; // want to start this ability 1138 1139 private: 1140 /** 1141 * get the type of ability. 1142 * 1143 */ 1144 void GetAbilityTypeString(std::string &typeStr); 1145 void OnSchedulerDied(const wptr<IRemoteObject> &remote); 1146 void GrantUriPermission(Want &want, std::string targetBundleName, bool isSandboxApp, uint32_t tokenId); 1147 void GrantDmsUriPermission(Want &want, std::string targetBundleName); 1148 bool IsDmsCall(Want &want); 1149 int32_t GetCurrentAccountId() const; 1150 1151 /** 1152 * add system ability caller record 1153 * 1154 */ 1155 void AddSystemAbilityCallerRecord(const sptr<IRemoteObject> &callerToken, int requestCode, 1156 std::string srcAbilityId); 1157 1158 bool IsSystemAbilityCall(const sptr<IRemoteObject> &callerToken, uint32_t callingTokenId = 0); 1159 1160 void RecordSaCallerInfo(const Want &want); 1161 1162 #ifdef WITH_DLP 1163 void HandleDlpAttached(); 1164 void HandleDlpClosed(); 1165 #endif // WITH_DLP 1166 void NotifyRemoveShellProcess(int32_t type); 1167 void NotifyAnimationAbilityDied(); SetCallerAccessTokenId(uint32_t callerAccessTokenId)1168 inline void SetCallerAccessTokenId(uint32_t callerAccessTokenId) 1169 { 1170 callerAccessTokenId_ = callerAccessTokenId; 1171 } 1172 1173 bool GrantPermissionToShell(const std::vector<std::string> &uriVec, uint32_t flag, std::string targetPkg); 1174 1175 void GrantUriPermissionInner(Want &want, std::vector<std::string> &uriVec, const std::string &targetBundleName, 1176 uint32_t tokenId); 1177 void GrantUriPermissionFor2In1Inner( 1178 Want &want, std::vector<std::string> &uriVec, const std::string &targetBundleName, uint32_t tokenId); 1179 1180 LastExitReason CovertAppExitReasonToLastReason(const Reason exitReason); 1181 1182 void NotifyMissionBindPid(); 1183 1184 void DumpUIExtensionRootHostInfo(std::vector<std::string> &info) const; 1185 1186 void DumpUIExtensionPid(std::vector<std::string> &info, bool isUIExtension) const; 1187 1188 void PublishFileOpenEvent(const Want &want); 1189 1190 void SetDebugAppByWaitingDebugFlag(); 1191 void AfterLoaded(); 1192 1193 #ifdef SUPPORT_SCREEN 1194 std::shared_ptr<Want> GetWantFromMission() const; 1195 void SetShowWhenLocked(const AppExecFwk::AbilityInfo &abilityInfo, sptr<AbilityTransitionInfo> &info) const; 1196 void SetAbilityTransitionInfo(const AppExecFwk::AbilityInfo &abilityInfo, 1197 sptr<AbilityTransitionInfo> &info) const; 1198 void SetAbilityTransitionInfo(sptr<AbilityTransitionInfo>& info) const; 1199 sptr<IWindowManagerServiceHandler> GetWMSHandler() const; 1200 void SetWindowModeAndDisplayId(sptr<AbilityTransitionInfo> &info, const std::shared_ptr<Want> &want) const; 1201 sptr<AbilityTransitionInfo> CreateAbilityTransitionInfo(); 1202 sptr<AbilityTransitionInfo> CreateAbilityTransitionInfo(const std::shared_ptr<StartOptions> &startOptions, 1203 const std::shared_ptr<Want> &want) const; 1204 sptr<AbilityTransitionInfo> CreateAbilityTransitionInfo(const AbilityRequest &abilityRequest) const; 1205 sptr<AbilityTransitionInfo> CreateAbilityTransitionInfo(const std::shared_ptr<StartOptions> &startOptions, 1206 const std::shared_ptr<Want> &want, const AbilityRequest &abilityRequest); 1207 std::shared_ptr<Global::Resource::ResourceManager> CreateResourceManager() const; 1208 std::shared_ptr<Media::PixelMap> GetPixelMap(const uint32_t windowIconId, 1209 std::shared_ptr<Global::Resource::ResourceManager> resourceMgr) const; 1210 1211 void AnimationTask(bool isRecent, const AbilityRequest &abilityRequest, 1212 const std::shared_ptr<StartOptions> &startOptions, const std::shared_ptr<AbilityRecord> &callerAbility); 1213 void NotifyAnimationFromStartingAbility(const std::shared_ptr<AbilityRecord> &callerAbility, 1214 const AbilityRequest &abilityRequest) const; 1215 void NotifyAnimationFromRecentTask(const std::shared_ptr<StartOptions> &startOptions, 1216 const std::shared_ptr<Want> &want) const; 1217 void NotifyAnimationFromTerminatingAbility(const std::shared_ptr<AbilityRecord> &callerAbility, bool needExit, 1218 bool flag); 1219 1220 void StartingWindowTask(bool isRecent, bool isCold, const AbilityRequest &abilityRequest, 1221 std::shared_ptr<StartOptions> &startOptions); 1222 void StartingWindowColdTask(bool isRecent, const AbilityRequest &abilityRequest, 1223 std::shared_ptr<StartOptions> &startOptions); 1224 void PostCancelStartingWindowColdTask(); 1225 void StartingWindowHot(const std::shared_ptr<StartOptions> &startOptions, const std::shared_ptr<Want> &want, 1226 const AbilityRequest &abilityRequest); 1227 void StartingWindowHot(); 1228 void StartingWindowCold(const std::shared_ptr<StartOptions> &startOptions, const std::shared_ptr<Want> &want, 1229 const AbilityRequest &abilityRequest); 1230 void InitColdStartingWindowResource(const std::shared_ptr<Global::Resource::ResourceManager> &resourceMgr); 1231 void GetColdStartingWindowResource(std::shared_ptr<Media::PixelMap> &bg, uint32_t &bgColor); 1232 void SetAbilityStateInner(AbilityState state); 1233 #endif 1234 1235 static int64_t abilityRecordId; 1236 int recordId_ = 0; // record id 1237 int32_t uiExtensionAbilityId_ = 0; // uiextension ability id 1238 AppExecFwk::AbilityInfo abilityInfo_ = {}; // the ability info get from BMS 1239 std::weak_ptr<AbilityRecord> preAbilityRecord_ = {}; // who starts this ability record 1240 std::weak_ptr<AbilityRecord> nextAbilityRecord_ = {}; // ability that started by this ability 1241 int64_t startTime_ = 0; // records first time of ability start 1242 int64_t restartTime_ = 0; // the time of last trying restart 1243 std::atomic<AbilityLoadState> loadState_ = AbilityLoadState::INIT; // ability thread attach state 1244 bool isWindowStarted_ = false; // is window hotstart or coldstart? 1245 bool isWindowAttached_ = false; // Is window of this ability attached? 1246 bool isLauncherAbility_ = false; // is launcher? 1247 1248 sptr<IAbilityScheduler> scheduler_ = {}; // kit scheduler 1249 bool isLoading_ = false; // is loading? 1250 bool isTerminating_ = false; // is terminating ? 1251 bool isCreateByConnect_ = false; // is created by connect ability mode? 1252 1253 int requestCode_ = -1; // requestCode_: >= 0 for-result start mode; <0 for normal start mode in default. 1254 sptr<IRemoteObject::DeathRecipient> schedulerDeathRecipient_ = {}; // scheduler binderDied Recipient 1255 1256 /** 1257 * result_: ability starts with for-result mode will send result before being terminated. 1258 * Its caller will receive results before active. 1259 * Now we assume only one result generate when terminate. 1260 */ 1261 std::shared_ptr<AbilityResult> result_ = {}; 1262 1263 /** 1264 * When this ability startAbilityForResult another ability, if another ability is terminated, 1265 * this ability will move to foreground, during this time, isAbilityForegrounding_ is true, 1266 * isAbilityForegrounding_ will be set to false when this ability is background 1267 */ 1268 bool isAbilityForegrounding_ = false; 1269 1270 // service(ability) can be connected by multi-pages(abilities), so need to store this service's connections 1271 mutable ffrt::mutex connRecordListMutex_; 1272 std::list<std::shared_ptr<ConnectionRecord>> connRecordList_ = {}; 1273 // service(ability) onConnect() return proxy of service ability 1274 sptr<IRemoteObject> connRemoteObject_ = {}; 1275 int startId_ = 0; // service(ability) start id 1276 1277 // page(ability) can be started by multi-pages(abilities), so need to store this ability's caller 1278 std::list<std::shared_ptr<CallerRecord>> callerList_ = {}; 1279 1280 bool isUninstall_ = false; 1281 1282 bool isLauncherRoot_ = false; 1283 1284 PacMap stateDatas_; // ability saved ability state data 1285 WindowConfig windowConfig_; 1286 bool isRestarting_ = false; // is restarting ? 1287 AppState appState_ = AppState::BEGIN; 1288 1289 int32_t uid_ = 0; 1290 int32_t pid_ = 0; 1291 int32_t missionId_ = -1; 1292 int32_t ownerMissionUserId_ = -1; 1293 bool isSwitchingPause_ = false; 1294 1295 // new version 1296 std::shared_ptr<CallContainer> callContainer_ = nullptr; 1297 bool isStartedByCall_ = false; 1298 bool isStartToBackground_ = false; 1299 bool isStartToForeground_ = false; 1300 int32_t appIndex_ = 0; 1301 bool minimizeReason_ = false; 1302 1303 bool clearMissionFlag_ = false; 1304 bool keepAliveBundle_ = false; 1305 int32_t restartCount_ = -1; 1306 int32_t restartMax_ = -1; 1307 std::string specifiedFlag_; 1308 std::string uri_; 1309 ffrt::mutex lock_; 1310 mutable ffrt::mutex dumpInfoLock_; 1311 mutable ffrt::mutex dumpLock_; 1312 mutable ffrt::mutex resultLock_; 1313 mutable ffrt::mutex wantLock_; 1314 mutable ffrt::condition_variable dumpCondition_; 1315 mutable bool isDumpTimeout_ = false; 1316 std::vector<std::string> dumpInfos_; 1317 std::atomic<AbilityState> pendingState_ = AbilityState::INITIAL; // pending life state 1318 std::atomic<AbilityVisibilityState> abilityVisibilityState_ = AbilityVisibilityState::INITIAL; 1319 1320 // scene session 1321 sptr<SessionInfo> sessionInfo_ = nullptr; 1322 mutable ffrt::mutex sessionLock_; 1323 std::map<uint64_t, AbilityWindowState> abilityWindowStateMap_; 1324 1325 #ifdef SUPPORT_SCREEN 1326 bool isStartingWindow_ = false; 1327 uint32_t bgColor_ = 0; 1328 std::shared_ptr<Media::PixelMap> startingWindowBg_ = nullptr; 1329 1330 bool isCompleteFirstFrameDrawing_ = false; 1331 bool coldStart_ = false; 1332 #endif 1333 1334 bool isGrantedUriPermission_ = false; 1335 uint32_t callerAccessTokenId_ = -1; 1336 bool isNeedBackToOtherMissionStack_ = false; 1337 std::weak_ptr<AbilityRecord> otherMissionStackAbilityRecord_; // who starts this ability record by SA 1338 int32_t collaboratorType_ = 0; 1339 std::string missionAffinity_ = ""; 1340 bool lockedState_ = false; 1341 bool isAttachDebug_ = false; 1342 bool isAssertDebug_ = false; 1343 bool isAppAutoStartup_ = false; 1344 bool isConnected = false; 1345 std::atomic_bool backgroundAbilityWindowDelayed_ = false; 1346 1347 bool isRestartApp_ = false; // Only app calling RestartApp can be set to true 1348 uint32_t specifyTokenId_ = 0; 1349 1350 std::shared_ptr<Want> connectWant_ = nullptr; 1351 std::shared_ptr<CallerAbilityInfo> saCallerInfo_ = nullptr; 1352 ffrt::mutex connectWantLock_; 1353 bool isLaunching_ = true; 1354 LaunchDebugInfo launchDebugInfo_; 1355 std::string instanceKey_ = ""; 1356 bool securityFlag_ = false; 1357 std::atomic<FreezeStrategy> freezeStrategy_{FreezeStrategy::NOTIFY_FREEZE_MGR}; 1358 }; 1359 } // namespace AAFwk 1360 } // namespace OHOS 1361 #endif // OHOS_ABILITY_RUNTIME_ABILITY_RECORD_H 1362