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