• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_AAFWK_ABILITY_RECORD_H
17 #define OHOS_AAFWK_ABILITY_RECORD_H
18 
19 #include <ctime>
20 #include <functional>
21 #include <list>
22 #include <memory>
23 #include <vector>
24 
25 #include "ability_info.h"
26 #include "ability_start_setting.h"
27 #include "ability_token_stub.h"
28 #include "ability_window_configuration.h"
29 #include "app_scheduler.h"
30 #include "application_info.h"
31 #include "ability_record_info.h"
32 #include "configuration_holder.h"
33 #include "lifecycle_deal.h"
34 #include "lifecycle_state_info.h"
35 #include "want.h"
36 #include "window_info.h"
37 
38 namespace OHOS {
39 namespace AAFwk {
40 using Closure = std::function<void()>;
41 
42 class AbilityRecord;
43 class MissionRecord;
44 class ConnectionRecord;
45 
46 const std::string ABILITY_TOKEN_NAME = "AbilityToken";
47 const std::string LINE_SEPARATOR = "\n";
48 
49 /**
50  * @class Token
51  * Token is identification of ability and used to interact with kit and wms.
52  */
53 class Token : public AbilityTokenStub {
54 public:
55     explicit Token(std::weak_ptr<AbilityRecord> abilityRecord);
56     virtual ~Token();
57 
58     std::shared_ptr<AbilityRecord> GetAbilityRecord() const;
59     static std::shared_ptr<AbilityRecord> GetAbilityRecordByToken(const sptr<IRemoteObject> &token);
60 
61 private:
62     std::weak_ptr<AbilityRecord> abilityRecord_;  // ability of this token
63 };
64 
65 /**
66  * @class AbilityResult
67  * Record requestCode of for-result start mode and result.
68  */
69 class AbilityResult {
70 public:
71     AbilityResult() = default;
AbilityResult(int requestCode,int resultCode,const Want & resultWant)72     AbilityResult(int requestCode, int resultCode, const Want &resultWant)
73         : requestCode_(requestCode), resultCode_(resultCode), resultWant_(resultWant)
74     {}
~AbilityResult()75     virtual ~AbilityResult()
76     {}
77 
78     int requestCode_ = -1;  // requestCode of for-result start mode
79     int resultCode_ = -1;   // resultCode of for-result start mode
80     Want resultWant_;       // for-result start mode ability will send the result to caller
81 };
82 
83 /**
84  * @class CallerRecord
85  * Record caller ability of for-result start mode and result.
86  */
87 class CallerRecord {
88 public:
89     CallerRecord() = default;
CallerRecord(int requestCode,std::weak_ptr<AbilityRecord> caller)90     CallerRecord(int requestCode, std::weak_ptr<AbilityRecord> caller) : requestCode_(requestCode), caller_(caller)
91     {}
~CallerRecord()92     virtual ~CallerRecord()
93     {}
94 
GetRequestCode()95     int GetRequestCode()
96     {
97         return requestCode_;
98     }
GetCaller()99     std::shared_ptr<AbilityRecord> GetCaller()
100     {
101         return caller_.lock();
102     }
103 
104 private:
105     int requestCode_ = -1;  // requestCode of for-result start mode
106     std::weak_ptr<AbilityRecord> caller_;
107 };
108 
109 /**
110  * @class AbilityRequest
111  * Wrap parameters of starting ability.
112  */
113 struct AbilityRequest {
114     Want want;
115     AppExecFwk::AbilityInfo abilityInfo;
116     AppExecFwk::ApplicationInfo appInfo;
117     int requestCode = -1;
118     bool restart = false;
119     sptr<IRemoteObject> callerToken;
120     std::shared_ptr<AbilityStartSetting> startSetting = nullptr;
DumpAbilityRequest121     void Dump(std::vector<std::string> &state)
122     {
123         std::string dumpInfo = "      want [" + want.ToUri() + "]";
124         state.push_back(dumpInfo);
125         dumpInfo = "      app name [" + abilityInfo.applicationName + "]";
126         state.push_back(dumpInfo);
127         dumpInfo = "      main name [" + abilityInfo.name + "]";
128         state.push_back(dumpInfo);
129         dumpInfo = "      request code [" + std::to_string(requestCode) + "]";
130         state.push_back(dumpInfo);
131     }
132 };
133 
134 /**
135  * @class AbilityRecord
136  * AbilityRecord records ability info and states and used to schedule ability life.
137  */
138 class AbilityRecord : public ConfigurationHolder, public std::enable_shared_from_this<AbilityRecord> {
139 public:
140     AbilityRecord(const Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
141         const AppExecFwk::ApplicationInfo &applicationInfo, int requestCode = -1);
142 
143     virtual ~AbilityRecord();
144 
145     /**
146      * CreateAbilityRecord.
147      *
148      * @param abilityRequest,create ability record.
149      * @return Returns ability record ptr.
150      */
151     static std::shared_ptr<AbilityRecord> CreateAbilityRecord(const AbilityRequest &abilityRequest);
152 
153     /**
154      * Init ability record.
155      *
156      * @return Returns true on success, others on failure.
157      */
158     bool Init();
159 
160     /**
161      * load ability.
162      *
163      * @return Returns ERR_OK on success, others on failure.
164      */
165     int LoadAbility();
166 
167     /**
168      * terminate ability.
169      *
170      * @return Returns ERR_OK on success, others on failure.
171      */
172     int TerminateAbility();
173 
174     /**
175      * set ability's mission record.
176      *
177      * @param missionRecord, mission record.
178      */
179     void SetMissionRecord(const std::shared_ptr<MissionRecord> &missionRecord);
180 
181     /**
182      * set ability's mission stack.
183      *
184      * @param missionStack, mission stack.
185      */
186     void SetMissionStackId(const int stackId);
187 
188     /**
189      * Get ability's mission stack id.
190      */
191     int GetMissionStackId() const;
192 
193     /**
194      * get ability's mission record.
195      *
196      * @return missionRecord, mission record.
197      */
198     std::shared_ptr<MissionRecord> GetMissionRecord() const;
199 
200     int GetMissionRecordId() const;
201 
202     /**
203      * get ability's info.
204      *
205      * @return ability info.
206      */
207     const AppExecFwk::AbilityInfo &GetAbilityInfo() const;
208 
209     /**
210      * get application's info.
211      *
212      * @return application info.
213      */
214     const AppExecFwk::ApplicationInfo &GetApplicationInfo() const;
215 
216     /**
217      * set ability's state.
218      *
219      * @param state, ability's state.
220      */
221     void SetAbilityState(AbilityState state);
222 
223     /**
224      * get ability's state.
225      *
226      * @return ability state.
227      */
228     AbilityState GetAbilityState() const;
229 
230     /**
231      * set ability scheduler for accessing ability thread.
232      *
233      * @param scheduler , ability scheduler.
234      */
235     void SetScheduler(const sptr<IAbilityScheduler> &scheduler);
236 
237     /**
238      * get ability's token.
239      *
240      * @return ability's token.
241      */
242     sptr<Token> GetToken() const;
243 
244     /**
245      * set ability's previous ability record.
246      *
247      * @param abilityRecord , previous ability record
248      */
249     void SetPreAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord);
250 
251     /**
252      * get ability's previous ability record.
253      *
254      * @return previous ability record
255      */
256     std::shared_ptr<AbilityRecord> GetPreAbilityRecord() const;
257 
258     /**
259      * set ability's next ability record.
260      *
261      * @param abilityRecord , next ability record
262      */
263     void SetNextAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord);
264 
265     /**
266      * get ability's previous ability record.
267      *
268      * @return previous ability record
269      */
270     std::shared_ptr<AbilityRecord> GetNextAbilityRecord() const;
271 
272     /**
273      * set ability's back ability record.
274      *
275      * @param abilityRecord , back ability record
276      */
277     void SetBackAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord);
278 
279     /**
280      * get ability's back ability record.
281      *
282      * @return back ability record
283      */
284     std::shared_ptr<AbilityRecord> GetBackAbilityRecord() const;
285 
286     /**
287      * set event id.
288      *
289      * @param eventId
290      */
291     void SetEventId(int64_t eventId);
292 
293     /**
294      * get event id.
295      *
296      * @return eventId
297      */
298     int64_t GetEventId() const;
299 
300     /**
301      * check whether the ability is ready.
302      *
303      * @return true : ready ,false: not ready
304      */
305     bool IsReady() const;
306 
307     /**
308      * check whether the ability 's window is attached.
309      *
310      * @return true : attached ,false: not attached
311      */
312     bool IsWindowAttached() const;
313 
314     /**
315      * check whether the ability is launcher.
316      *
317      * @return true : lanucher ,false: not lanucher
318      */
319     bool IsLauncherAbility() const;
320 
321     /**
322      * check whether the ability is terminating.
323      *
324      * @return true : yes ,false: not
325      */
326     bool IsTerminating() const;
327 
328     /**
329      * check whether the ability force to terminate.
330      *
331      * @return true : yes ,false: not
332      */
333     bool IsForceTerminate() const;
334     void SetForceTerminate(bool flag);
335 
336     /**
337      * set the ability is terminating.
338      *
339      */
340     void SetTerminatingState();
341 
342     /**
343      * set the ability is new want flag.
344      *
345      * @return isNewWant
346      */
347     void SetIsNewWant(bool isNewWant);
348 
349     /**
350      * check whether the ability is new want flag.
351      *
352      * @return true : yes ,false: not
353      */
354     bool IsNewWant() const;
355 
356     /**
357      * check whether the ability is created by connect ability mode.
358      *
359      * @return true : yes ,false: not
360      */
361     bool IsCreateByConnect() const;
362 
363     /**
364      * set the ability is created by connect ability mode.
365      *
366      */
367     void SetCreateByConnectMode();
368 
369     /**
370      * active the ability.
371      *
372      */
373     void Activate();
374 
375     /**
376      * process request of activing the ability.
377      *
378      */
379     void ProcessActivate();
380 
381     /**
382      * process request of activing the ability in moving.
383      *
384      */
385     void ProcessActivateInMoving();
386 
387     /**
388      * process request of inactiving the ability.
389      *
390      */
391     void ProcessInactivate();
392 
393     /**
394      * process request of inactiving the ability in moving.
395      *
396      */
397     void ProcessInactivateInMoving();
398 
399     /**
400      * inactive the ability.
401      *
402      */
403     void Inactivate();
404 
405     /**
406      * move the ability to back ground.
407      *
408      */
409     void MoveToBackground(const Closure &task);
410 
411     /**
412      * terminate the ability.
413      *
414      */
415     void Terminate(const Closure &task);
416 
417     /**
418      * connect the ability.
419      *
420      */
421     void ConnectAbility();
422 
423     /**
424      * disconnect the ability.
425      *
426      */
427     void DisconnectAbility();
428 
429     /**
430      * Command the ability.
431      *
432      */
433     void CommandAbility();
434 
435     /**
436      * save ability state.
437      *
438      */
439     void SaveAbilityState();
440 
441     /**
442      * restore ability state.
443      *
444      */
445     void RestoreAbilityState();
446 
447     /**
448      * notify top active ability updated.
449      *
450      */
451     void TopActiveAbilityChanged(bool flag);
452 
453     /**
454      * set the want for start ability.
455      *
456      */
457     void SetWant(const Want &want);
458 
459     /**
460      * get the want for start ability.
461      *
462      */
463     const Want &GetWant() const;
464 
465     /**
466      * get request code of the ability to start.
467      *
468      */
469     int GetRequestCode() const;
470 
471     /**
472      * set the result object of the ability which one need to be terminated.
473      *
474      */
475     void SetResult(const std::shared_ptr<AbilityResult> &result);
476 
477     /**
478      * get the result object of the ability which one need to be terminated.
479      *
480      */
481     std::shared_ptr<AbilityResult> GetResult() const;
482 
483     /**
484      * send result object to caller ability thread.
485      *
486      */
487     void SendResult();
488 
489     /**
490      * send result object to caller ability.
491      *
492      */
493     void SendResultToCallers();
494 
495     /**
496      * save result object to caller ability.
497      *
498      */
499     void SaveResultToCallers(const int resultCode, const Want *resultWant);
500 
501     /**
502      * add connect record to the list.
503      *
504      */
505     void AddConnectRecordToList(const std::shared_ptr<ConnectionRecord> &connRecord);
506 
507     /**
508      * get the list of connect record.
509      *
510      */
511     std::list<std::shared_ptr<ConnectionRecord>> GetConnectRecordList() const;
512 
513     /**
514      * get the list of connect record.
515      *
516      */
517     std::list<std::shared_ptr<ConnectionRecord>> GetConnectingRecordList();
518 
519     /**
520      * remove the connect record from list.
521      *
522      */
523     void RemoveConnectRecordFromList(const std::shared_ptr<ConnectionRecord> &connRecord);
524 
525     /**
526      * check whether connect list is empty.
527      *
528      */
529     bool IsConnectListEmpty();
530 
531     /**
532      * add ability's window info to record.
533      *
534      */
535     void AddWindowInfo(int windowToken);
536 
537     /**
538      * remove ability's window info from record.
539      *
540      */
541     void RemoveWindowInfo();
542 
543     /**
544      * get ability's window info from record.
545      *
546      */
547     std::shared_ptr<WindowInfo> GetWindowInfo() const;
548 
549     /**
550      * add caller record
551      *
552      */
553     void AddCallerRecord(const sptr<IRemoteObject> &callerToken, int requestCode);
554 
555     /**
556      * get caller record to list.
557      *
558      */
559     std::list<std::shared_ptr<CallerRecord>> GetCallerRecordList() const;
560 
561     /**
562      * get connecting record from list.
563      *
564      */
565     std::shared_ptr<ConnectionRecord> GetConnectingRecord() const;
566 
567     /**
568      * get disconnecting record from list.
569      *
570      */
571     std::shared_ptr<ConnectionRecord> GetDisconnectingRecord() const;
572 
573     /**
574      * convert ability state (enum type to string type).
575      *
576      */
577     static std::string ConvertAbilityState(const AbilityState &state);
578 
579     static std::string ConvertAppState(const AppState &state);
580 
581     /**
582      * convert life cycle state to ability state .
583      *
584      */
585     static int ConvertLifeCycleToAbilityState(const AbilityLifeCycleState &state);
586 
587     /**
588      * get the ability record id.
589      *
590      */
GetRecordId()591     inline int GetRecordId() const
592     {
593         return recordId_;
594     }
595 
596     /**
597      * dump ability info.
598      *
599      */
600     void Dump(std::vector<std::string> &info);
601 
602     void SetStartTime();
603 
604     int64_t GetStartTime() const;
605 
606     /**
607      * dump service info.
608      *
609      */
610     void DumpService(std::vector<std::string> &info) const;
611 
612     /**
613      * get ability record info.
614      *
615      */
616     void GetAbilityRecordInfo(AbilityRecordInfo &recordInfo);
617 
618     /**
619      * set aconnect remote object.
620      *
621      */
622     void SetConnRemoteObject(const sptr<IRemoteObject> &remoteObject);
623 
624     /**
625      * get connect remote object.
626      *
627      */
628     sptr<IRemoteObject> GetConnRemoteObject() const;
629 
630     void AddStartId();
631     int GetStartId() const;
632 
633     void SetIsUninstallAbility();
634     /**
635      * Determine whether ability is uninstalled
636      *
637      * @return true: uninstalled false: installed
638      */
639     bool IsUninstallAbility() const;
640 
641     void SetKernalSystemAbility();
642     bool IsKernalSystemAbility() const;
643 
644     void SetLauncherRoot();
645     bool IsLauncherRoot() const;
646 
647     bool IsAbilityState(const AbilityState &state) const;
648 
649     bool SupportMultWindow() const;
650     void NotifyMultiWinModeChanged(const AbilityWindowConfiguration &winModeKey, bool flag);
651     void SetInMovingState(bool isMoving);
652     bool GetInMovingState() const;
653 
654     bool IsToEnd() const;
655     void SetToEnd(bool isToEnd);
656 
657     void SetStartSetting(const std::shared_ptr<AbilityStartSetting> &setting);
658     std::shared_ptr<AbilityStartSetting> GetStartSetting() const;
659 
660     void SetPowerState(const bool isPower);
661     bool GetPowerState() const;
662 
663     void SetRestarting(const bool isRestart);
664     bool IsRestarting() const;
665     void SetAppState(const AppState &state);
666     AppState GetAppState() const;
667 
668     unsigned int GetIntConfigChanges();
669     void ClearFlag();
670     void SetConfiguration(const std::shared_ptr<DummyConfiguration> &config);
671 
672 protected:
673     virtual bool OnConfigurationChanged(const DummyConfiguration &config, unsigned int configChanges) override;
674     virtual std::shared_ptr<ConfigurationHolder> GetParent() override;
675     virtual unsigned int GetChildSize() override;
676     virtual std::shared_ptr<ConfigurationHolder> FindChild(unsigned int index) override;
677 
678 private:
679     /**
680      * get the type of ability.
681      *
682      */
683     void GetAbilityTypeString(std::string &typeStr);
684     void OnSchedulerDied(const wptr<IRemoteObject> &remote);
685     void SendEvent(uint32_t msg, uint32_t timeOut);
686 
687     static int64_t abilityRecordId;
688     int recordId_ = 0;                                // record id
689     Want want_ = {};                                       // want to start this ability
690     AppExecFwk::AbilityInfo abilityInfo_ = {};             // the ability info get from BMS
691     AppExecFwk::ApplicationInfo applicationInfo_ = {};     // the ability info get from BMS
692     sptr<Token> token_ = {};                               // used to interact with kit and wms
693     std::weak_ptr<MissionRecord> missionRecord_ = {};      // mission of this ability
694     std::weak_ptr<AbilityRecord> preAbilityRecord_ = {};   // who starts this ability record
695     std::weak_ptr<AbilityRecord> nextAbilityRecord_ = {};  // ability that started by this ability
696     std::weak_ptr<AbilityRecord> backAbilityRecord_ = {};  // who back to this ability record
697     std::unique_ptr<LifecycleDeal> lifecycleDeal_ = {};    // life manager used to schedule life
698     int64_t startTime_ = 0;                           // records first time of ability start
699     bool isReady_ = false;                            // is ability thread attached?
700     bool isWindowAttached_ = false;                   // Is window of this ability attached?
701     bool isLauncherAbility_ = false;                  // is launcher?
702     int64_t eventId_ = 0;                             // post event id
703     static int64_t g_abilityRecordEventId_;
704     sptr<IAbilityScheduler> scheduler_ = {};       // kit scheduler
705     bool isTerminating_ = false;              // is terminating ?
706     LifeCycleStateInfo lifeCycleStateInfo_;   // target life state info
707     AbilityState currentState_ = AbilityState::INITIAL;  // current life state
708     std::shared_ptr<WindowInfo> windowInfo_;  // add window info
709     bool isCreateByConnect_ = false;          // is created by connect ability mode?
710     bool isToEnd_ = false;                    // is to end ?
711 
712     int requestCode_ = -1;  // requestCode_: >= 0 for-result start mode; <0 for normal start mode in default.
713     sptr<IRemoteObject::DeathRecipient> schedulerDeathRecipient_ = {};  // scheduler binderDied Recipient
714 
715     /**
716      * result_: ability starts with for-result mode will send result before being terminated.
717      * Its caller will receive results before active.
718      * Now we assume only one result generate when terminate.
719      */
720     std::shared_ptr<AbilityResult> result_ = {};
721 
722     // service(ability) can be connected by multi-pages(abilites), so need to store this service's connections
723     std::list<std::shared_ptr<ConnectionRecord>> connRecordList_ = {};
724     // service(ability) onConnect() return proxy of service ability
725     sptr<IRemoteObject> connRemoteObject_ = {};
726     int startId_ = 0;  // service(ability) start id
727 
728     // page(ability) can be started by multi-pages(abilites), so need to store this ability's caller
729     std::list<std::shared_ptr<CallerRecord>> callerList_ = {};
730 
731     bool isUninstall_ = false;
732     bool isForceTerminate_ = false;
733     const static std::map<AbilityState, std::string> stateToStrMap;
734     const static std::map<AbilityLifeCycleState, AbilityState> convertStateMap;
735     const static std::map<AppState, std::string> appStateToStrMap_;
736 
737     bool isKernalSystemAbility_ = false;
738     bool isLauncherRoot_ = false;
739     bool isPowerState_ = false;  // ability to change state when poweroff and poweron.
740 
741     PacMap stateDatas_;             // ability saved ability state data
742     bool isRestarting_ = false;     // is restarting ?
743     bool isInMovingState_ = false;  // whether complete multi window moving state.
744     AppState appState_ = AppState::BEGIN;
745 };
746 }  // namespace AAFwk
747 }  // namespace OHOS
748 #endif  // OHOS_AAFWK_ABILITY_RECORD_H
749