• 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_KERNAL_SYSTEM_APP_MANAGER_H
17 #define OHOS_AAFWK_KERNAL_SYSTEM_APP_MANAGER_H
18 
19 #include <mutex>
20 #include <queue>
21 
22 #include "mission_record.h"
23 #include "mission_stack.h"
24 #include "want.h"
25 
26 namespace OHOS {
27 namespace AAFwk {
28 /**
29  * @class KernalSystemAppManager
30  * KernalSystemAppManager provides a facility for managing systerm ability life cycle.
31  */
32 class KernalSystemAppManager : public std::enable_shared_from_this<KernalSystemAppManager> {
33 public:
34     explicit KernalSystemAppManager(int userId);
35     ~KernalSystemAppManager();
36 
37     /**
38      * init kernal system app manager.
39      *
40      */
41     void Init();
42     /**
43      * StartAbility with request.
44      *
45      * @param abilityRequest, the request of the ability to start.
46      * @return Returns ERR_OK on success, others on failure.
47      */
48     int StartAbility(const AbilityRequest &abilityRequest);
49     /**
50      * attach ability thread ipc object.
51      *
52      * @param scheduler, ability thread ipc object.
53      * @param token, the token of ability.
54      * @return Returns ERR_OK on success, others on failure.
55      */
56     int AttachAbilityThread(const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token);
57     /**
58      * AbilityTransitionDone, ability call this interface after lift cycle was changed.
59      *
60      * @param token,.ability's token.
61      * @param state,.the state of ability lift cycle.
62      * @return Returns ERR_OK on success, others on failure.
63      */
64     int AbilityTransitionDone(const sptr<IRemoteObject> &token, int state);
65     /**
66      * OnAbilityRequestDone, app manager service call this interface after ability request done.
67      *
68      * @param token,ability's token.
69      * @param state,the state of ability lift cycle.
70      */
71     void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state);
72 
73     void OnAppStateChanged(const AppInfo &info);
74 
75     /**
76      * get manager's user id.
77      */
78     int GetManagerUserId() const;
79 
80     void DumpState(std::vector<std::string> &info);
81 
82     void OnAbilityDied(std::shared_ptr<AbilityRecord> abilityRecord);
83 
84     void OnTimeOut(uint32_t msgId, int64_t eventId);
85 
86     /**
87      * get the ability record by token.
88      *
89      * @return abilityRecord, target ability.
90      */
91     std::shared_ptr<AbilityRecord> GetAbilityRecordByToken(const sptr<IRemoteObject> &token);
92 
93     int UpdateConfiguration(const DummyConfiguration &config);
94     void RestartAbility(const std::shared_ptr<AbilityRecord> abilityRecord);
95 
96 private:
97     /**
98      * StartAbilityLocked.
99      *
100      * @param abilityRequest the request of the ability to start.
101      * @return Returns ERR_OK on success, others on failure.
102      */
103     int StartAbilityLocked(const AbilityRequest &abilityRequest);
104     /**
105      * push waitting ability to queue.
106      *
107      * @param abilityRequest, the request of ability.
108      */
109     void EnqueueWaittingAbility(const AbilityRequest &abilityRequest);
110     /**
111      * pop waitting ability.
112      *
113      */
114     void DequeueWaittingAbility();
115     /**
116      * get current top ability of stack.
117      *
118      * @return top ability record.
119      */
120     std::shared_ptr<AbilityRecord> GetCurrentTopAbility() const;
121     /**
122      * get or create the target ability record of system app.
123      *
124      * @param abilityRequest, the request of ability.
125      * @param targetAbility, target ability record.
126      */
127     void GetOrCreateAbilityRecord(const AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> &targetAbility);
128     /**
129      * get the flag of the target ability record.
130      *
131      * @param bundleName, target bundleName.
132      * @param abilityName, target ability name.
133      */
134     static std::string GetFlagOfAbility(const std::string &bundleName, const std::string &abilityName);
135     /**
136      * get the ability record by eventId.
137      *
138      * @return abilityRecord, target ability.
139      */
140     std::shared_ptr<AbilityRecord> GetAbilityRecordByEventId(const int64_t eventId) const;
141     /**
142      * dispatch ability life cycle .
143      *
144      * @param abilityRecord.
145      * @param state.
146      */
147     int DispatchActive(const std::shared_ptr<AbilityRecord> &abilityRecord, int state);
148     int DispatchInactive(const std::shared_ptr<AbilityRecord> &abilityRecord, int state);
149     /**
150      * complete ability life cycle .
151      *
152      * @param abilityRecord.
153      */
154     void CompleteActive(const std::shared_ptr<AbilityRecord> &abilityRecord);
155     void CompleteInactive(const std::shared_ptr<AbilityRecord> &abilityRecord);
156 
157     bool RemoveAbilityRecord(std::shared_ptr<AbilityRecord> ability);
158 
159 private:
160     std::recursive_mutex stackLock_;
161     std::list<std::shared_ptr<AbilityRecord>> abilities_;
162     std::queue<AbilityRequest> waittingAbilityQueue_;
163     int userId_;
164 };
165 }  // namespace AAFwk
166 }  // namespace OHOS
167 #endif  // OHOS_AAFWK_KERNAL_SYSTEM_APP_MANAGER_H
168