• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "cpp/mutex.h"
25 #include "cpp/condition_variable.h"
26 
27 #include "ability_connect_callback_interface.h"
28 #include "ability_info.h"
29 #include "ability_start_setting.h"
30 #include "ability_state.h"
31 #include "ability_token_stub.h"
32 #include "app_scheduler.h"
33 #include "application_info.h"
34 #include "bundlemgr/bundle_mgr_interface.h"
35 #include "call_container.h"
36 #include "ipc_skeleton.h"
37 #include "lifecycle_deal.h"
38 #include "lifecycle_state_info.h"
39 #include "session_info.h"
40 #include "ui_extension_window_command.h"
41 #include "uri.h"
42 #include "want.h"
43 #ifdef SUPPORT_GRAPHICS
44 #include "ability_window_configuration.h"
45 #include "resource_manager.h"
46 #include "start_options.h"
47 #include "window_manager_service_handler.h"
48 #endif
49 
50 namespace OHOS {
51 namespace AAFwk {
52 using Closure = std::function<void()>;
53 
54 class AbilityRecord;
55 class ConnectionRecord;
56 class Mission;
57 class MissionList;
58 class CallContainer;
59 
60 constexpr const char* ABILITY_TOKEN_NAME = "AbilityToken";
61 constexpr const char* LAUNCHER_BUNDLE_NAME = "com.ohos.launcher";
62 
63 /**
64  * @class Token
65  * Token is identification of ability and used to interact with kit and wms.
66  */
67 class Token : public AbilityTokenStub {
68 public:
69     explicit Token(std::weak_ptr<AbilityRecord> abilityRecord);
70     virtual ~Token();
71 
72     std::shared_ptr<AbilityRecord> GetAbilityRecord() const;
73     static std::shared_ptr<AbilityRecord> GetAbilityRecordByToken(const sptr<IRemoteObject> &token);
74 
75 private:
76     std::weak_ptr<AbilityRecord> abilityRecord_;  // ability of this token
77 };
78 
79 /**
80  * @class AbilityResult
81  * Record requestCode of for-result start mode and result.
82  */
83 class AbilityResult {
84 public:
85     AbilityResult() = default;
AbilityResult(int requestCode,int resultCode,const Want & resultWant)86     AbilityResult(int requestCode, int resultCode, const Want &resultWant)
87         : requestCode_(requestCode), resultCode_(resultCode), resultWant_(resultWant)
88     {}
~AbilityResult()89     virtual ~AbilityResult()
90     {}
91 
92     int requestCode_ = -1;  // requestCode of for-result start mode
93     int resultCode_ = -1;   // resultCode of for-result start mode
94     Want resultWant_;       // for-result start mode ability will send the result to caller
95 };
96 
97 /**
98  * @class SystemAbilityCallerRecord
99  * Record system caller ability of for-result start mode and result.
100  */
101 class SystemAbilityCallerRecord {
102 public:
SystemAbilityCallerRecord(std::string & srcAbilityId,const sptr<IRemoteObject> & callerToken)103     SystemAbilityCallerRecord(std::string &srcAbilityId, const sptr<IRemoteObject> &callerToken)
104         : srcAbilityId_(srcAbilityId), callerToken_(callerToken)
105     {}
~SystemAbilityCallerRecord()106     virtual ~SystemAbilityCallerRecord()
107     {}
108 
GetSrcAbilityId()109     std::string GetSrcAbilityId()
110     {
111         return srcAbilityId_;
112     }
GetCallerToken()113     const sptr<IRemoteObject> GetCallerToken()
114     {
115         return callerToken_;
116     }
SetResult(Want & want,int resultCode)117     void SetResult(Want &want, int resultCode)
118     {
119         resultWant_ = want;
120         resultCode_ = resultCode;
121     }
GetResultWant()122     Want &GetResultWant()
123     {
124         return resultWant_;
125     }
GetResultCode()126     int &GetResultCode()
127     {
128         return resultCode_;
129     }
130     /**
131      * Set result to system ability.
132      *
133      */
134     void SetResultToSystemAbility(std::shared_ptr<SystemAbilityCallerRecord> callerSystemAbilityRecord,
135         Want &resultWant, int resultCode);
136     /**
137      * Send result to system ability.
138      *
139      */
140     void SendResultToSystemAbility(int requestCode,
141         const std::shared_ptr<SystemAbilityCallerRecord> callerSystemAbilityRecord,
142         int32_t callerUid, uint32_t accessToken, bool schedulerdied);
143 
144 private:
145     std::string srcAbilityId_;
146     sptr<IRemoteObject> callerToken_;
147     Want resultWant_;
148     int resultCode_ = -1;
149 };
150 
151 /**
152  * @class CallerRecord
153  * Record caller ability of for-result start mode and result.
154  */
155 class CallerRecord {
156 public:
157     CallerRecord() = default;
CallerRecord(int requestCode,std::weak_ptr<AbilityRecord> caller)158     CallerRecord(int requestCode, std::weak_ptr<AbilityRecord> caller) : requestCode_(requestCode), caller_(caller)
159     {}
CallerRecord(int requestCode,std::shared_ptr<SystemAbilityCallerRecord> saCaller)160     CallerRecord(int requestCode, std::shared_ptr<SystemAbilityCallerRecord> saCaller) : requestCode_(requestCode),
161         saCaller_(saCaller)
162     {}
~CallerRecord()163     virtual ~CallerRecord()
164     {}
165 
GetRequestCode()166     int GetRequestCode()
167     {
168         return requestCode_;
169     }
GetCaller()170     std::shared_ptr<AbilityRecord> GetCaller()
171     {
172         return caller_.lock();
173     }
GetSaCaller()174     std::shared_ptr<SystemAbilityCallerRecord> GetSaCaller()
175     {
176         return saCaller_;
177     }
178 
179 private:
180     int requestCode_ = -1;  // requestCode of for-result start mode
181     std::weak_ptr<AbilityRecord> caller_;
182     std::shared_ptr<SystemAbilityCallerRecord> saCaller_ = nullptr;
183 };
184 
185 /**
186  * @class AbilityRequest
187  * Wrap parameters of starting ability.
188  */
189 enum AbilityCallType {
190     INVALID_TYPE = 0,
191     CALL_REQUEST_TYPE,
192     START_OPTIONS_TYPE,
193     START_SETTINGS_TYPE,
194     START_EXTENSION_TYPE,
195 };
196 
197 enum CollaboratorType {
198     DEFAULT_TYPE = 0,
199     RESERVE_TYPE,
200     OTHERS_TYPE
201 };
202 
203 struct ComponentRequest {
204     sptr<IRemoteObject> callerToken = nullptr;
205     int requestCode = 0;
206     int componentStatus = 0;
207     int requestResult = 0;
208 };
209 
210 struct AbilityRequest {
211     Want want;
212     AppExecFwk::AbilityInfo abilityInfo;
213     AppExecFwk::ApplicationInfo appInfo;
214     int32_t uid = 0;
215     int requestCode = -1;
216     bool restart = false;
217     int32_t restartCount = -1;
218     int64_t restartTime = 0;
219     bool startRecent = false;
220     int32_t collaboratorType = CollaboratorType::DEFAULT_TYPE;
221 
222     // call ability
223     int callerUid = -1;
224     AbilityCallType callType = AbilityCallType::INVALID_TYPE;
225     sptr<IRemoteObject> callerToken = nullptr;
226     uint32_t callerAccessTokenId = -1;
227     sptr<IAbilityConnection> connect = nullptr;
228 
229     std::shared_ptr<AbilityStartSetting> startSetting = nullptr;
230     std::string specifiedFlag;
231     sptr<IRemoteObject> abilityInfoCallback = nullptr;
232 
233     AppExecFwk::ExtensionAbilityType extensionType = AppExecFwk::ExtensionAbilityType::UNSPECIFIED;
234 
235     sptr<SessionInfo> sessionInfo;
236 
IsContinuationAbilityRequest237     bool IsContinuation() const
238     {
239         auto flags = want.GetFlags();
240         if ((flags & Want::FLAG_ABILITY_CONTINUATION) == Want::FLAG_ABILITY_CONTINUATION) {
241             return true;
242         }
243         return false;
244     }
245 
IsAcquireShareDataAbilityRequest246     bool IsAcquireShareData() const
247     {
248         return want.GetBoolParam(Want::PARAM_ABILITY_ACQUIRE_SHARE_DATA, false);
249     }
250 
IsAppRecoveryAbilityRequest251     bool IsAppRecovery() const
252     {
253         return want.GetBoolParam(Want::PARAM_ABILITY_RECOVERY_RESTART, false);
254     }
255 
IsCallTypeAbilityRequest256     bool IsCallType(const AbilityCallType & type) const
257     {
258         return (callType == type);
259     }
260 
DumpAbilityRequest261     void Dump(std::vector<std::string> &state)
262     {
263         std::string dumpInfo = "      want [" + want.ToUri() + "]";
264         state.push_back(dumpInfo);
265         dumpInfo = "      app name [" + abilityInfo.applicationName + "]";
266         state.push_back(dumpInfo);
267         dumpInfo = "      main name [" + abilityInfo.name + "]";
268         state.push_back(dumpInfo);
269         dumpInfo = "      request code [" + std::to_string(requestCode) + "]";
270         state.push_back(dumpInfo);
271     }
272 
273     void Voluation(const Want &srcWant, int srcRequestCode,
274         const sptr<IRemoteObject> &srcCallerToken, const std::shared_ptr<AbilityStartSetting> srcStartSetting = nullptr,
275         int srcCallerUid = -1)
276     {
277         want = srcWant;
278         requestCode = srcRequestCode;
279         callerToken = srcCallerToken;
280         startSetting = srcStartSetting;
281         callerUid = srcCallerUid == -1 ? IPCSkeleton::GetCallingUid() : srcCallerUid;
282     }
283 };
284 
285 // new version
286 enum ResolveResultType {
287     OK_NO_REMOTE_OBJ = 0,
288     OK_HAS_REMOTE_OBJ,
289     NG_INNER_ERROR,
290 };
291 /**
292  * @class AbilityRecord
293  * AbilityRecord records ability info and states and used to schedule ability life.
294  */
295 class AbilityRecord : public std::enable_shared_from_this<AbilityRecord> {
296 public:
297     AbilityRecord(const Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
298         const AppExecFwk::ApplicationInfo &applicationInfo, int requestCode = -1);
299 
300     virtual ~AbilityRecord();
301 
302     /**
303      * CreateAbilityRecord.
304      *
305      * @param abilityRequest,create ability record.
306      * @return Returns ability record ptr.
307      */
308     static std::shared_ptr<AbilityRecord> CreateAbilityRecord(const AbilityRequest &abilityRequest);
309 
310     /**
311      * Init ability record.
312      *
313      * @return Returns true on success, others on failure.
314      */
315     bool Init();
316 
317     /**
318      * load ability.
319      *
320      * @return Returns ERR_OK on success, others on failure.
321      */
322     int LoadAbility();
323 
324     /**
325      * foreground the ability.
326      *
327      */
328     void ForegroundAbility(uint32_t sceneFlag = 0);
329     void ForegroundAbility(const Closure &task, uint32_t sceneFlag = 0);
330 
331     /**
332      * process request of foregrounding the ability.
333      *
334      */
335     void ProcessForegroundAbility(uint32_t tokenId, uint32_t sceneFlag = 0);
336 
337     /**
338      * move the ability to back ground.
339      *
340      * @param task timeout task.
341      */
342     void BackgroundAbility(const Closure &task);
343 
344     /**
345      * prepare terminate ability.
346      *
347      * @return Returns true on stop terminating; returns false on terminate.
348      */
349     bool PrepareTerminateAbility();
350 
351     /**
352      * terminate ability.
353      *
354      * @return Returns ERR_OK on success, others on failure.
355      */
356     int TerminateAbility();
357 
358     /**
359      * get ability's info.
360      *
361      * @return ability info.
362      */
363     const AppExecFwk::AbilityInfo &GetAbilityInfo() const;
364 
365     /**
366      * get application's info.
367      *
368      * @return application info.
369      */
370     const AppExecFwk::ApplicationInfo &GetApplicationInfo() const;
371 
372     /**
373      * set ability's state.
374      *
375      * @param state, ability's state.
376      */
377     void SetAbilityState(AbilityState state);
378 
379     bool GetAbilityForegroundingFlag() const;
380 
381     void SetAbilityForegroundingFlag();
382 
383     /**
384      * get ability's state.
385      *
386      * @return ability state.
387      */
388     AbilityState GetAbilityState() const;
389 
390     bool IsForeground() const;
391 
392     /**
393      * set ability scheduler for accessing ability thread.
394      *
395      * @param scheduler , ability scheduler.
396      */
397     void SetScheduler(const sptr<IAbilityScheduler> &scheduler);
398 
GetScheduler()399     inline sptr<IAbilityScheduler> GetScheduler() const
400     {
401         return scheduler_;
402     }
403 
GetSessionInfo()404     inline sptr<SessionInfo> GetSessionInfo() const
405     {
406         return sessionInfo_;
407     }
408 
409     /**
410      * get ability's token.
411      *
412      * @return ability's token.
413      */
414     sptr<Token> GetToken() const;
415 
416     /**
417      * set ability's previous ability record.
418      *
419      * @param abilityRecord , previous ability record
420      */
421     void SetPreAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord);
422 
423     /**
424      * get ability's previous ability record.
425      *
426      * @return previous ability record
427      */
428     std::shared_ptr<AbilityRecord> GetPreAbilityRecord() const;
429 
430     /**
431      * set ability's next ability record.
432      *
433      * @param abilityRecord , next ability record
434      */
435     void SetNextAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord);
436 
437     /**
438      * get ability's previous ability record.
439      *
440      * @return previous ability record
441      */
442     std::shared_ptr<AbilityRecord> GetNextAbilityRecord() const;
443 
444     /**
445      * check whether the ability is ready.
446      *
447      * @return true : ready ,false: not ready
448      */
449     bool IsReady() const;
450 
451     void UpdateRecoveryInfo(bool hasRecoverInfo);
452 
453     bool GetRecoveryInfo();
454 
455     void InitPersistableUriPermissionConfig();
456 
457 #ifdef SUPPORT_GRAPHICS
458     /**
459      * check whether the ability 's window is attached.
460      *
461      * @return true : attached ,false: not attached
462      */
463     bool IsWindowAttached() const;
464 
IsStartingWindow()465     inline bool IsStartingWindow() const
466     {
467         return isStartingWindow_;
468     }
469 
SetStartingWindow(bool isStartingWindow)470     inline void SetStartingWindow(bool isStartingWindow)
471     {
472         isStartingWindow_ = isStartingWindow;
473     }
474 
475     void PostCancelStartingWindowHotTask();
476 
477     /**
478      * process request of foregrounding the ability.
479      *
480      */
481     void ProcessForegroundAbility(bool isRecent, const AbilityRequest &abilityRequest,
482         std::shared_ptr<StartOptions> &startOptions, const std::shared_ptr<AbilityRecord> &callerAbility,
483         uint32_t sceneFlag = 0);
484 
485     void ProcessForegroundAbility(const std::shared_ptr<AbilityRecord> &callerAbility, bool needExit = true,
486         uint32_t sceneFlag = 0);
487     void NotifyAnimationFromTerminatingAbility() const;
488     void NotifyAnimationFromMinimizeAbility(bool& animaEnabled);
489 
490     void SetCompleteFirstFrameDrawing(const bool flag);
491     bool IsCompleteFirstFrameDrawing() const;
492 #endif
493 
494     /**
495      * check whether the ability is launcher.
496      *
497      * @return true : lanucher ,false: not lanucher
498      */
499     bool IsLauncherAbility() const;
500 
501     /**
502      * check whether the ability is terminating.
503      *
504      * @return true : yes ,false: not
505      */
506     bool IsTerminating() const;
507 
508     /**
509      * set the ability is terminating.
510      *
511      */
512     void SetTerminatingState();
513 
514     /**
515      * set the ability is new want flag.
516      *
517      * @return isNewWant
518      */
519     void SetIsNewWant(bool isNewWant);
520 
521     /**
522      * check whether the ability is new want flag.
523      *
524      * @return true : yes ,false: not
525      */
526     bool IsNewWant() const;
527 
528     /**
529      * check whether the ability is created by connect ability mode.
530      *
531      * @return true : yes ,false: not
532      */
533     bool IsCreateByConnect() const;
534 
535     /**
536      * set the ability is created by connect ability mode.
537      *
538      */
539     void SetCreateByConnectMode();
540 
541     /**
542      * active the ability.
543      *
544      */
545     virtual void Activate();
546 
547     /**
548      * inactive the ability.
549      *
550      */
551     virtual void Inactivate();
552 
553     /**
554      * terminate the ability.
555      *
556      */
557     void Terminate(const Closure &task);
558 
559     /**
560      * connect the ability.
561      *
562      */
563     void ConnectAbility();
564 
565     /**
566      * disconnect the ability.
567      *
568      */
569     void DisconnectAbility();
570 
571     /**
572      * Command the ability.
573      *
574      */
575     void CommandAbility();
576 
577     void CommandAbilityWindow(const sptr<SessionInfo> &sessionInfo, WindowCommand winCmd);
578 
579     /**
580      * save ability state.
581      *
582      */
583     void SaveAbilityState();
584     void SaveAbilityState(const PacMap &inState);
585 
586     /**
587      * restore ability state.
588      *
589      */
590     void RestoreAbilityState();
591 
592     /**
593      * notify top active ability updated.
594      *
595      */
596     void TopActiveAbilityChanged(bool flag);
597 
598     /**
599      * set the want for start ability.
600      *
601      */
602     void SetWant(const Want &want);
603 
604     /**
605      * get the want for start ability.
606      *
607      */
608     const Want &GetWant() const;
609 
610     /**
611      * get request code of the ability to start.
612      *
613      */
614     int GetRequestCode() const;
615 
616     /**
617      * set the result object of the ability which one need to be terminated.
618      *
619      */
620     void SetResult(const std::shared_ptr<AbilityResult> &result);
621 
622     /**
623      * get the result object of the ability which one need to be terminated.
624      *
625      */
626     std::shared_ptr<AbilityResult> GetResult() const;
627 
628     /**
629      * send result object to caller ability thread.
630      *
631      */
632     void SendResult(bool isSandboxApp, uint32_t tokeId);
633 
634     /**
635      * send result object to caller ability thread for sandbox app file saving.
636      */
637     void SendSandboxSavefileResult(const Want &want, int resultCode, int requestCode);
638 
639     /**
640      * send result object to caller ability.
641      *
642      */
643     void SendResultToCallers(bool schedulerdied = false);
644 
645     /**
646      * save result object to caller ability.
647      *
648      */
649     void SaveResultToCallers(const int resultCode, const Want *resultWant);
650 
651     /**
652      * save result to caller ability.
653      *
654      */
655     void SaveResult(int resultCode, const Want *resultWant, std::shared_ptr<CallerRecord> caller);
656 
657     /**
658      * add connect record to the list.
659      *
660      */
661     void AddConnectRecordToList(const std::shared_ptr<ConnectionRecord> &connRecord);
662 
663     /**
664      * get the list of connect record.
665      *
666      */
667     std::list<std::shared_ptr<ConnectionRecord>> GetConnectRecordList() const;
668 
669     /**
670      * get the list of connect record.
671      *
672      */
673     std::list<std::shared_ptr<ConnectionRecord>> GetConnectingRecordList();
674 
675     /**
676      * remove the connect record from list.
677      *
678      */
679     void RemoveConnectRecordFromList(const std::shared_ptr<ConnectionRecord> &connRecord);
680 
681     /**
682      * check whether connect list is empty.
683      *
684      */
685     bool IsConnectListEmpty();
686 
687     /**
688      * add caller record
689      *
690      */
691     void AddCallerRecord(const sptr<IRemoteObject> &callerToken, int requestCode, std::string srcAbilityId = "");
692 
693     /**
694      * get caller record to list.
695      *
696      */
697     std::list<std::shared_ptr<CallerRecord>> GetCallerRecordList() const;
698     std::shared_ptr<AbilityRecord> GetCallerRecord() const;
699 
700     /**
701      * get connecting record from list.
702      *
703      */
704     std::shared_ptr<ConnectionRecord> GetConnectingRecord() const;
705 
706     /**
707      * get disconnecting record from list.
708      *
709      */
710     std::shared_ptr<ConnectionRecord> GetDisconnectingRecord() const;
711 
712     /**
713      * convert ability state (enum type to string type).
714      *
715      */
716     static std::string ConvertAbilityState(const AbilityState &state);
717 
718     static std::string ConvertAppState(const AppState &state);
719 
720     /**
721      * convert life cycle state to ability state .
722      *
723      */
724     static int ConvertLifeCycleToAbilityState(const AbilityLifeCycleState &state);
725 
726     /**
727      * get the ability record id.
728      *
729      */
GetRecordId()730     inline int GetRecordId() const
731     {
732         return recordId_;
733     }
734 
735     /**
736      * dump ability info.
737      *
738      */
739     void Dump(std::vector<std::string> &info);
740 
741     void DumpClientInfo(std::vector<std::string> &info, const std::vector<std::string> &params,
742         bool isClient = false, bool dumpConfig = true) const;
743 
744     /**
745      * Called when client complete dump.
746      *
747      * @param infos The dump info.
748      */
749     void DumpAbilityInfoDone(std::vector<std::string> &infos);
750 
751     /**
752      * dump ability state info.
753      *
754      */
755     void DumpAbilityState(std::vector<std::string> &info, bool isClient, const std::vector<std::string> &params);
756 
757     void SetStartTime();
758 
759     int64_t GetStartTime() const;
760 
761     /**
762      * dump service info.
763      *
764      */
765     void DumpService(std::vector<std::string> &info, bool isClient = false) const;
766 
767     /**
768      * dump service info.
769      *
770      */
771     void DumpService(std::vector<std::string> &info, std::vector<std::string> &params, bool isClient = false) const;
772 
773     /**
774      * set aconnect remote object.
775      *
776      */
777     void SetConnRemoteObject(const sptr<IRemoteObject> &remoteObject);
778 
779     /**
780      * get connect remote object.
781      *
782      */
783     sptr<IRemoteObject> GetConnRemoteObject() const;
784 
785     void AddStartId();
786     int GetStartId() const;
787 
788     void SetIsUninstallAbility();
789     /**
790      * Determine whether ability is uninstalled
791      *
792      * @return true: uninstalled false: installed
793      */
794     bool IsUninstallAbility() const;
795     void ShareData(const int32_t &uniqueId);
796     void SetLauncherRoot();
797     bool IsLauncherRoot() const;
798     bool IsAbilityState(const AbilityState &state) const;
799     bool IsActiveState() const;
800 
801     void SetStartSetting(const std::shared_ptr<AbilityStartSetting> &setting);
802     std::shared_ptr<AbilityStartSetting> GetStartSetting() const;
803 
804     void SetRestarting(const bool isRestart);
805     void SetRestarting(const bool isRestart, int32_t canReStartCount);
806     int32_t GetRestartCount() const;
807     void SetRestartCount(int32_t restartCount);
808     void SetKeepAlive();
809     int64_t GetRestartTime();
810     void SetRestartTime(const int64_t restartTime);
811     void SetAppIndex(const int32_t appIndex);
812     int32_t GetAppIndex() const;
813     bool IsRestarting() const;
814     void SetAppState(const AppState &state);
815     AppState GetAppState() const;
816 
817     void SetLaunchReason(const LaunchReason &reason);
818     void SetLastExitReason(const LastExitReason &reason);
819     void ContinueAbility(const std::string &deviceId, uint32_t versionCode);
820     void NotifyContinuationResult(int32_t result);
821     std::shared_ptr<MissionList> GetOwnedMissionList() const;
822 
823     void SetMission(const std::shared_ptr<Mission> &mission);
824     void SetMissionList(const std::shared_ptr<MissionList> &missionList);
825     std::shared_ptr<Mission> GetMission() const;
826     int32_t GetMissionId() const;
827 
828     void SetUid(int32_t uid);
829     int32_t GetUid();
830     int32_t GetPid();
831     void SetSwitchingPause(bool state);
832     bool IsSwitchingPause();
833     void SetOwnerMissionUserId(int32_t userId);
834     int32_t GetOwnerMissionUserId();
835 
836     // new version
837     ResolveResultType Resolve(const AbilityRequest &abilityRequest);
838     bool ReleaseCall(const sptr<IAbilityConnection> &connect);
839     bool IsNeedToCallRequest() const;
840     bool IsStartedByCall() const;
841     void SetStartedByCall(const bool isFlag);
842     void CallRequest();
843     bool CallRequestDone(const sptr<IRemoteObject> &callStub) const;
844     bool IsStartToBackground() const;
845     void SetStartToBackground(const bool flag);
846     bool IsStartToForeground() const;
847     void SetStartToForeground(const bool flag);
848     void SetSessionInfo(sptr<SessionInfo> sessionInfo);
849     void SetMinimizeReason(bool fromUser);
850     bool IsMinimizeFromUser() const;
851     void SetClearMissionFlag(bool clearMissionFlag);
852     bool IsClearMissionFlag();
853 
854     void SetSpecifiedFlag(const std::string &flag);
855     std::string GetSpecifiedFlag() const;
856     void SetWindowMode(int32_t windowMode);
857     void RemoveWindowMode();
858     LifeCycleStateInfo lifeCycleStateInfo_;                // target life state info
859     #ifdef ABILITY_COMMAND_FOR_TEST
860     int BlockAbility();
861     #endif
862 
863     bool CanRestartRootLauncher();
864 
865     bool CanRestartResident();
866 
867     std::string GetLabel();
GetAbilityRecordId()868     inline int64_t GetAbilityRecordId() const
869     {
870         return recordId_;
871     }
872 
873     void SetPendingState(AbilityState state);
874     AbilityState GetPendingState() const;
875 
876     bool IsNeedBackToOtherMissionStack();
877     void SetNeedBackToOtherMissionStack(bool isNeedBackToOtherMissionStack);
878     std::shared_ptr<AbilityRecord> GetOtherMissionStackAbilityRecord() const;
879     void SetOtherMissionStackAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord);
880     void RevokeUriPermission();
881     void RemoveAbilityDeathRecipient() const;
882     bool IsExistConnection(const sptr<IAbilityConnection> &connect);
883 
884     int32_t GetCollaboratorType() const;
885 
886 protected:
887     void SendEvent(uint32_t msg, uint32_t timeOut, int32_t param = -1);
888 
889     sptr<Token> token_ = {};                               // used to interact with kit and wms
890     std::unique_ptr<LifecycleDeal> lifecycleDeal_ = {};    // life manager used to schedule life
891     AbilityState currentState_ = AbilityState::INITIAL;    // current life state
892     Want want_ = {};                                       // want to start this ability
893 
894 private:
895     /**
896      * get the type of ability.
897      *
898      */
899     void GetAbilityTypeString(std::string &typeStr);
900     void OnSchedulerDied(const wptr<IRemoteObject> &remote);
901     void GrantUriPermission(Want &want, std::string targetBundleName, bool isSandboxApp, uint32_t tokenId);
902     void GrantDmsUriPermission(Want &want, std::string targetBundleName);
903     bool IsDmsCall();
904     int32_t GetCurrentAccountId() const;
905 
906     /**
907      * add system ability caller record
908      *
909      */
910     void AddSystemAbilityCallerRecord(const sptr<IRemoteObject> &callerToken, int requestCode,
911         std::string srcAbilityId);
912 
913     bool IsSystemAbilityCall(const sptr<IRemoteObject> &callerToken);
914 
915     void HandleDlpAttached();
916     void HandleDlpClosed();
917     void NotifyRemoveShellProcess(int32_t type);
918     void NotifyAnimationAbilityDied();
SetCallerAccessTokenId(uint32_t callerAccessTokenId)919     inline void SetCallerAccessTokenId(uint32_t callerAccessTokenId)
920     {
921         callerAccessTokenId_ = callerAccessTokenId;
922     }
923 
924 #ifdef SUPPORT_GRAPHICS
925     std::shared_ptr<Want> GetWantFromMission() const;
926     void SetShowWhenLocked(const AppExecFwk::AbilityInfo &abilityInfo, sptr<AbilityTransitionInfo> &info) const;
927     void SetAbilityTransitionInfo(const AppExecFwk::AbilityInfo &abilityInfo,
928         sptr<AbilityTransitionInfo> &info) const;
929     void SetAbilityTransitionInfo(sptr<AbilityTransitionInfo>& info) const;
930     sptr<IWindowManagerServiceHandler> GetWMSHandler() const;
931     void SetWindowModeAndDisplayId(sptr<AbilityTransitionInfo> &info, const std::shared_ptr<Want> &want) const;
932     sptr<AbilityTransitionInfo> CreateAbilityTransitionInfo();
933     sptr<AbilityTransitionInfo> CreateAbilityTransitionInfo(const std::shared_ptr<StartOptions> &startOptions,
934         const std::shared_ptr<Want> &want) const;
935     sptr<AbilityTransitionInfo> CreateAbilityTransitionInfo(const AbilityRequest &abilityRequest) const;
936     sptr<AbilityTransitionInfo> CreateAbilityTransitionInfo(const std::shared_ptr<StartOptions> &startOptions,
937         const std::shared_ptr<Want> &want, const AbilityRequest &abilityRequest);
938     std::shared_ptr<Global::Resource::ResourceManager> CreateResourceManager() const;
939     std::shared_ptr<Media::PixelMap> GetPixelMap(const uint32_t windowIconId,
940         std::shared_ptr<Global::Resource::ResourceManager> resourceMgr) const;
941 
942     void AnimationTask(bool isRecent, const AbilityRequest &abilityRequest,
943         const std::shared_ptr<StartOptions> &startOptions, const std::shared_ptr<AbilityRecord> &callerAbility);
944     void NotifyAnimationFromStartingAbility(const std::shared_ptr<AbilityRecord> &callerAbility,
945         const AbilityRequest &abilityRequest) const;
946     void NotifyAnimationFromRecentTask(const std::shared_ptr<StartOptions> &startOptions,
947         const std::shared_ptr<Want> &want) const;
948     void NotifyAnimationFromTerminatingAbility(const std::shared_ptr<AbilityRecord> &callerAbility, bool needExit,
949         bool flag);
950 
951     void StartingWindowTask(bool isRecent, bool isCold, const AbilityRequest &abilityRequest,
952         std::shared_ptr<StartOptions> &startOptions);
953     void StartingWindowColdTask(bool isRecnet, const AbilityRequest &abilityRequest,
954         std::shared_ptr<StartOptions> &startOptions);
955     void PostCancelStartingWindowColdTask();
956     void StartingWindowHot(const std::shared_ptr<StartOptions> &startOptions, const std::shared_ptr<Want> &want,
957         const AbilityRequest &abilityRequest);
958     void StartingWindowHot();
959     void StartingWindowCold(const std::shared_ptr<StartOptions> &startOptions, const std::shared_ptr<Want> &want,
960         const AbilityRequest &abilityRequest);
961     void InitColdStartingWindowResource(const std::shared_ptr<Global::Resource::ResourceManager> &resourceMgr);
962     void GetColdStartingWindowResource(std::shared_ptr<Media::PixelMap> &bg, uint32_t &bgColor);
963     void SetAbilityStateInner(AbilityState state);
964 #endif
965 
966     static int64_t abilityRecordId;
967     int recordId_ = 0;                                // record id
968     AppExecFwk::AbilityInfo abilityInfo_ = {};             // the ability info get from BMS
969     AppExecFwk::ApplicationInfo applicationInfo_ = {};     // the ability info get from BMS
970     std::weak_ptr<AbilityRecord> preAbilityRecord_ = {};   // who starts this ability record
971     std::weak_ptr<AbilityRecord> nextAbilityRecord_ = {};  // ability that started by this ability
972     int64_t startTime_ = 0;                           // records first time of ability start
973     int64_t restartTime_ = 0;                         // the time of last trying restart
974     bool isReady_ = false;                            // is ability thread attached?
975     bool isWindowAttached_ = false;                   // Is window of this ability attached?
976     bool isLauncherAbility_ = false;                  // is launcher?
977     bool isKeepAlive_ = false;                 // is keep alive or resident ability?
978 
979     sptr<IAbilityScheduler> scheduler_ = {};       // kit scheduler
980     bool isTerminating_ = false;              // is terminating ?
981     bool isCreateByConnect_ = false;          // is created by connect ability mode?
982 
983     int requestCode_ = -1;  // requestCode_: >= 0 for-result start mode; <0 for normal start mode in default.
984     sptr<IRemoteObject::DeathRecipient> schedulerDeathRecipient_ = {};  // scheduler binderDied Recipient
985 
986     /**
987      * result_: ability starts with for-result mode will send result before being terminated.
988      * Its caller will receive results before active.
989      * Now we assume only one result generate when terminate.
990      */
991     std::shared_ptr<AbilityResult> result_ = {};
992 
993     /**
994      * When this ability startAbilityForResult another ability, if another ability is terminated,
995      * this ability will move to foreground, during this time, isAbilityForegrounding_ is true,
996      * isAbilityForegrounding_ will be set to false when this ability is background
997      */
998     bool isAbilityForegrounding_ = false;
999 
1000     // service(ability) can be connected by multi-pages(abilites), so need to store this service's connections
1001     std::list<std::shared_ptr<ConnectionRecord>> connRecordList_ = {};
1002     // service(ability) onConnect() return proxy of service ability
1003     sptr<IRemoteObject> connRemoteObject_ = {};
1004     int startId_ = 0;  // service(ability) start id
1005 
1006     // page(ability) can be started by multi-pages(abilites), so need to store this ability's caller
1007     std::list<std::shared_ptr<CallerRecord>> callerList_ = {};
1008 
1009     bool isUninstall_ = false;
1010     const static std::map<AbilityState, std::string> stateToStrMap;
1011     const static std::map<AbilityLifeCycleState, AbilityState> convertStateMap;
1012     const static std::map<AppState, std::string> appStateToStrMap_;
1013 
1014     bool isLauncherRoot_ = false;
1015 
1016     PacMap stateDatas_;             // ability saved ability state data
1017     bool isRestarting_ = false;     // is restarting ?
1018     AppState appState_ = AppState::BEGIN;
1019 
1020     int32_t uid_ = 0;
1021     int32_t pid_ = 0;
1022     std::weak_ptr<MissionList> missionList_;
1023     std::weak_ptr<Mission> mission_;
1024     int32_t missionId_ = -1;
1025     int32_t ownerMissionUserId_ = -1;
1026     bool isSwitchingPause_ = false;
1027 
1028     // new version
1029     std::shared_ptr<CallContainer> callContainer_ = nullptr;
1030     bool isStartedByCall_ = false;
1031     bool isStartToBackground_ = false;
1032     bool isStartToForeground_ = false;
1033     int32_t appIndex_ = 0;
1034     bool minimizeReason_ = false;
1035 
1036     bool clearMissionFlag_ = false;
1037 
1038     int32_t restartCount_ = -1;
1039     int32_t restartMax_ = -1;
1040     std::string specifiedFlag_;
1041     ffrt::mutex lock_;
1042     mutable ffrt::mutex dumpInfoLock_;
1043     mutable ffrt::mutex dumpLock_;
1044     mutable ffrt::condition_variable dumpCondition_;
1045     mutable bool isDumpTimeout_ = false;
1046     std::vector<std::string> dumpInfos_;
1047     std::atomic<AbilityState> pendingState_ = AbilityState::INITIAL;    // pending life state
1048 
1049     // scene session
1050     sptr<SessionInfo> sessionInfo_ = nullptr;
1051     std::unordered_set<uint64_t> sessionIds_;
1052 
1053 #ifdef SUPPORT_GRAPHICS
1054     bool isStartingWindow_ = false;
1055     uint32_t bgColor_ = 0;
1056     std::shared_ptr<Media::PixelMap> startingWindowBg_ = nullptr;
1057 
1058     bool isCompleteFirstFrameDrawing_ = false;
1059 #endif
1060 
1061     bool isGrantedUriPermission_ = false;
1062     uint32_t callerAccessTokenId_ = -1;
1063     bool isNeedBackToOtherMissionStack_ = false;
1064     std::weak_ptr<AbilityRecord> otherMissionStackAbilityRecord_; // who starts this ability record by SA
1065     int32_t collaboratorType_ = 0;
1066     bool isGrantPersistableUriPermissionEnable_ = false;
1067 };
1068 }  // namespace AAFwk
1069 }  // namespace OHOS
1070 #endif  // OHOS_ABILITY_RUNTIME_ABILITY_RECORD_H
1071