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