1 /*
2  * Copyright (c) 2020 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_SLITE_ABILITY_SERVICE_H
17 #define OHOS_ABILITY_SLITE_ABILITY_SERVICE_H
18 
19 #include <cstdlib>
20 
21 #include "ability_list.h"
22 #include "ability_record.h"
23 #include "ability_record_observer.h"
24 #include "adapter.h"
25 #include "bms_helper.h"
26 #include "js_ability_thread.h"
27 #include "mission_info.h"
28 #include "nocopyable.h"
29 #include "want.h"
30 #include "slite_ability.h"
31 #include "slite_ability_state.h"
32 
33 namespace OHOS {
34 namespace AbilitySlite {
35 struct AbilityOperation {
AbilityOperationAbilityOperation36     AbilityOperation() {}
~AbilityOperationAbilityOperation37     ~AbilityOperation() {}
38 
39     uint16_t msgId = 0;
40     Want *want = nullptr;
41     uint64_t token = 0;
42 };
43 
44 class AbilityRecordManager : public NoCopyable {
45 public:
46     typedef void (AbilityRecordManager::*LifecycleFunc)(uint16_t token);
47 
48     struct LifecycleFuncStr {
49         int32_t state;
50         LifecycleFunc func_ptr;
51     };
52 
GetInstance()53     static AbilityRecordManager &GetInstance()
54     {
55         static AbilityRecordManager instance;
56         return instance;
57     }
58 
59     ~AbilityRecordManager() override;
60 
61     int32_t StartAbility(const Want *want);
62 
63     int32_t TerminateAbility(uint16_t token);
64 
65     int32_t TerminateMission(uint32_t mission);
66 
67     int32_t TerminateAll(const char *excludedBundleName);
68 
69     int32_t ForceStop(const Want *want);
70 
71     int32_t ForceStopBundle(uint16_t token);
72 
73     int32_t SchedulerLifecycleDone(uint64_t token, int32_t state);
74 
75     ElementName *GetTopAbility();
76 
77     MissionInfoList *GetMissionInfos(uint32_t maxNum) const;
78 
79     void setNativeAbility(const SliteAbility *ability);
80 
81     void StartLauncher();
82 
83     void SetIsAppScheduling(bool runState);
84 
85     bool GetIsAppScheduling();
86 
87     int32_t RunOperation();
88 
89     int32_t AddAbilityRecordObserver(AbilityRecordObserver *observer);
90     int32_t RemoveAbilityRecordObserver(AbilityRecordObserver *observer);
91 
92     uint32_t curTask_ = 0;
93 
94 private:
95     static uint16_t GenerateToken();
96 
97     uint32_t GenerateMission();
98 
99     AbilityRecordManager();
100 
101     int32_t StartAbility(AbilitySvcInfo *info);
102 
103     int32_t StartAbility(const AbilityRecord *record);
104 
105     int32_t StartRemoteAbility(const Want *want);
106 
107     int32_t PreCheckStartAbility(const AbilitySvcInfo &info);
108 
109     bool CheckResponse(const char *bundleName);
110 
111     int32_t SchedulerLifecycle(uint64_t token, int32_t state);
112 
113     int32_t ScheduleLifecycleInner(const AbilityRecord *record, int32_t state);
114 
115     void SchedulerAbilityLifecycle(SliteAbility *ability, const Want &want, int32_t state);
116 
117     int32_t CreateAppTask(AbilityRecord *record);
118 
119     void OnCreateDone(uint16_t token);
120 
121     void OnForegroundDone(uint16_t token);
122 
123     void OnBackgroundDone(uint16_t token);
124 
125     void OnDestroyDone(uint16_t token);
126 
127     void DeleteRecordInfo(uint16_t token);
128 
129     void DeleteAbilityThread(AbilityRecord *record);
130 
131     int32_t SendMsgToAbilityThread(int32_t state, const AbilityRecord *record);
132 
133     void SetAbilityStateAndNotify(uint64_t token, int32_t state);
134 
135     void UpdateRecord(AbilitySvcInfo *info);
136 
137     int32_t TerminateAbility(uint16_t token, const Want* want);
138 
139     int32_t ForceStopBundleInner(uint16_t token);
140 
141     bool IsLauncher(const char *bundleName);
142 
143     Want *CreateWant(const AbilityRecord *record);
144 
145     bool NeedToBeTerminated(const char *bundleName);
146 
147     Want *CopyWant(const Want *want);
148 
149     int32_t AddAbilityOperation(uint16_t msgId, const Want *want, uint64_t token);
150 
151     uint16_t pendingToken_ { 0 };
152 #ifndef _MINI_MULTI_TASKS_
153     AbilityRecord *pendingRecord = nullptr;
154 #endif
155     List<AbilityOperation *> abilityOperation_ {};
156     bool isAppScheduling_ = false;
157 
158     AbilityList abilityList_ {};
159     SliteAbility *nativeAbility_ = nullptr;
160 };
161 } // namespace AbilitySlite
162 } // namespace OHOS
163 #endif  // OHOS_ABILITY_SLITE_ABILITY_SERVICE_H
164