1 /*
2  * Copyright (c) 2022 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_CONTEXT_ABILITY_LIFECYCLE_CALLBACK_H
17 #define OHOS_ABILITY_RUNTIME_CONTEXT_ABILITY_LIFECYCLE_CALLBACK_H
18 
19 #include <map>
20 #include <memory>
21 
22 class NativeReference;
23 typedef struct napi_env__* napi_env;
24 typedef struct napi_value__* napi_value;
25 
26 namespace OHOS {
27 namespace AbilityRuntime {
28 class AbilityLifecycleCallback {
29 public:
~AbilityLifecycleCallback()30     virtual ~AbilityLifecycleCallback() {}
31     /**
32      * Called back when the ability is started for initialization.
33      *
34      * @since 9
35      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
36      * @param ability: Indicates the ability to register for listening.
37      * @StageModelOnly
38      */
39     virtual void OnAbilityCreate(const std::shared_ptr<NativeReference> &ability) = 0;
40 
41     /**
42      * Called back when the window stage is created.
43      *
44      * @since 9
45      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
46      * @param ability: Indicates the ability to register for listening.
47      * @param windowStage: Indicates the window stage to create.
48      * @StageModelOnly
49      */
50     virtual void OnWindowStageCreate(const std::shared_ptr<NativeReference> &ability,
51         const std::shared_ptr<NativeReference> &windowStage) = 0;
52 
53     /**
54      * Called back when the window stage is destroy.
55      *
56      * @since 9
57      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
58      * @param ability: Indicates the ability to register for listening.
59      * @param windowStage: Indicates the window stage to destroy.
60      * @StageModelOnly
61      */
62     virtual void OnWindowStageDestroy(const std::shared_ptr<NativeReference> &ability,
63         const std::shared_ptr<NativeReference> &windowStage) = 0;
64 
65     /**
66      * Called back when the window stage is active.
67      *
68      * @since 9
69      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
70      * @param ability: Indicates the ability to register for listening.
71      * @param windowStage: Indicates the window stage to active.
72      * @StageModelOnly
73      */
74     virtual void OnWindowStageActive(const std::shared_ptr<NativeReference> &ability,
75         const std::shared_ptr<NativeReference> &windowStage) = 0;
76 
77     /**
78      * Called back when the window stage is inactive.
79      *
80      * @since 9
81      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
82      * @param ability: Indicates the ability to register for listening.
83      * @param windowStage: Indicates the window stage to inactive.
84      * @StageModelOnly
85      */
86     virtual void OnWindowStageInactive(const std::shared_ptr<NativeReference> &ability,
87         const std::shared_ptr<NativeReference> &windowStage) = 0;
88 
89     /**
90      * Called back when the ability is destroy.
91      *
92      * @since 9
93      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
94      * @param ability: Indicates the ability to register for listening.
95      * @StageModelOnly
96      */
97     virtual void OnAbilityDestroy(const std::shared_ptr<NativeReference> &ability) = 0;
98 
99     /**
100      * Called back after the UIAbility called onNewWant.
101      *
102      * @since 12
103      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
104      * @param ability: Indicates the ability to register for listening.
105      * @StageModelOnly
106      */
OnNewWant(const std::shared_ptr<NativeReference> & ability)107     virtual void OnNewWant(const std::shared_ptr<NativeReference> &ability)
108     {}
109 
110     /**
111      * Called back before the UIAbility will called onNewWant.
112      *
113      * @since 12
114      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
115      * @param ability: Indicates the ability to register for listening.
116      * @StageModelOnly
117      */
OnWillNewWant(const std::shared_ptr<NativeReference> & ability)118     virtual void OnWillNewWant(const std::shared_ptr<NativeReference> &ability)
119     {}
120 
121     /**
122      * Called back before an ability is started for initialization.
123      *
124      * @since 12
125      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
126      * @param ability: Indicates the ability to register for listening.
127      * @StageModelOnly
128      */
OnAbilityWillCreate(const std::shared_ptr<NativeReference> & ability)129     virtual void OnAbilityWillCreate(const std::shared_ptr<NativeReference> &ability)
130     {}
131 
132     /**
133      * Called back before a window stage is created.
134      *
135      * @since 12
136      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
137      * @param ability: Indicates the ability to register for listening.
138      * @param windowStage: Indicates the window stage to active.
139      * @StageModelOnly
140      */
OnWindowStageWillCreate(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)141     virtual void OnWindowStageWillCreate(const std::shared_ptr<NativeReference> &ability,
142         const std::shared_ptr<NativeReference> &windowStage) {}
143 
144     /**
145      * Called back before a window stage is destroyed.
146      *
147      * @since 12
148      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
149      * @param ability: Indicates the ability to register for listening.
150      * @param windowStage: Indicates the window stage to active.
151      * @StageModelOnly
152      */
OnWindowStageWillDestroy(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)153     virtual void OnWindowStageWillDestroy(const std::shared_ptr<NativeReference> &ability,
154         const std::shared_ptr<NativeReference> &windowStage) {}
155 
156     /**
157      * Called back before an ability is destroyed.
158      *
159      * @since 12
160      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
161      * @param ability: Indicates the ability to register for listening.
162      * @StageModelOnly
163      */
OnAbilityWillDestroy(const std::shared_ptr<NativeReference> & ability)164     virtual void OnAbilityWillDestroy(const std::shared_ptr<NativeReference> &ability) {}
165 
166     /**
167      * Called back before the state of an ability changes to foreground.
168      *
169      * @since 12
170      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
171      * @param ability: Indicates the ability to register for listening.
172      * @StageModelOnly
173      */
OnAbilityWillForeground(const std::shared_ptr<NativeReference> & ability)174     virtual void OnAbilityWillForeground(const std::shared_ptr<NativeReference> &ability) {}
175 
176     /**
177      * Called back before the state of an ability changes to background.
178      *
179      * @since 12
180      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
181      * @param ability: Indicates the ability to register for listening.
182      * @StageModelOnly
183      */
OnAbilityWillBackground(const std::shared_ptr<NativeReference> & ability)184     virtual void OnAbilityWillBackground(const std::shared_ptr<NativeReference> &ability) {}
185 
186     /**
187      * Called back when the ability is foreground.
188      *
189      * @since 9
190      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
191      * @param ability: Indicates the ability to register for listening.
192      * @StageModelOnly
193      */
194     virtual void OnAbilityForeground(const std::shared_ptr<NativeReference> &ability) = 0;
195 
196     /**
197      * Called back when the ability is background.
198      *
199      * @since 9
200      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
201      * @param ability: Indicates the ability to register for listening.
202      * @StageModelOnly
203      */
204     virtual void OnAbilityBackground(const std::shared_ptr<NativeReference> &ability) = 0;
205 
206     /**
207      * Called back when the ability is continue.
208      *
209      * @since 9
210      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
211      * @param ability: Indicates the ability to register for listening.
212      * @StageModelOnly
213      */
214     virtual void OnAbilityContinue(const std::shared_ptr<NativeReference> &ability) = 0;
215 
OnAbilityWillContinue(const std::shared_ptr<NativeReference> & ability)216     virtual void OnAbilityWillContinue(const std::shared_ptr<NativeReference> &ability) {}
OnWindowStageWillRestore(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)217     virtual void OnWindowStageWillRestore(const std::shared_ptr<NativeReference> &ability,
218         const std::shared_ptr<NativeReference> &windowStage) {}
OnWindowStageRestore(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)219     virtual void OnWindowStageRestore(const std::shared_ptr<NativeReference> &ability,
220         const std::shared_ptr<NativeReference> &windowStage) {}
OnAbilityWillSaveState(const std::shared_ptr<NativeReference> & ability)221     virtual void OnAbilityWillSaveState(const std::shared_ptr<NativeReference> &ability) {}
OnAbilitySaveState(const std::shared_ptr<NativeReference> & ability)222     virtual void OnAbilitySaveState(const std::shared_ptr<NativeReference> &ability) {}
223 };
224 
225 class JsAbilityLifecycleCallback : public AbilityLifecycleCallback,
226                                    public std::enable_shared_from_this<JsAbilityLifecycleCallback> {
227 public:
228     explicit JsAbilityLifecycleCallback(napi_env env);
229     void OnAbilityCreate(const std::shared_ptr<NativeReference> &ability) override;
230     void OnWindowStageCreate(const std::shared_ptr<NativeReference> &ability,
231         const std::shared_ptr<NativeReference> &windowStage) override;
232     void OnWindowStageDestroy(const std::shared_ptr<NativeReference> &ability,
233         const std::shared_ptr<NativeReference> &windowStage) override;
234     void OnWindowStageActive(const std::shared_ptr<NativeReference> &ability,
235         const std::shared_ptr<NativeReference> &windowStage) override;
236     void OnWindowStageInactive(const std::shared_ptr<NativeReference> &ability,
237         const std::shared_ptr<NativeReference> &windowStage) override;
238     void OnAbilityWillCreate(const std::shared_ptr<NativeReference> &ability) override;
239     void OnWindowStageWillCreate(const std::shared_ptr<NativeReference> &ability,
240         const std::shared_ptr<NativeReference> &windowStage) override;
241     void OnWindowStageWillDestroy(const std::shared_ptr<NativeReference> &ability,
242         const std::shared_ptr<NativeReference> &windowStage) override;
243     void OnAbilityWillDestroy(const std::shared_ptr<NativeReference> &ability) override;
244     void OnAbilityWillForeground(const std::shared_ptr<NativeReference> &ability) override;
245     void OnAbilityWillBackground(const std::shared_ptr<NativeReference> &ability) override;
246     void OnNewWant(const std::shared_ptr<NativeReference> &ability) override;
247     void OnWillNewWant(const std::shared_ptr<NativeReference> &ability) override;
248     void OnAbilityDestroy(const std::shared_ptr<NativeReference> &ability) override;
249     void OnAbilityForeground(const std::shared_ptr<NativeReference> &ability) override;
250     void OnAbilityBackground(const std::shared_ptr<NativeReference> &ability) override;
251     void OnAbilityContinue(const std::shared_ptr<NativeReference> &ability) override;
252     void OnAbilityWillContinue(const std::shared_ptr<NativeReference> &ability) override;
253     void OnWindowStageWillRestore(const std::shared_ptr<NativeReference> &ability,
254         const std::shared_ptr<NativeReference> &windowStage) override;
255     void OnWindowStageRestore(const std::shared_ptr<NativeReference> &ability,
256         const std::shared_ptr<NativeReference> &windowStage) override;
257     void OnAbilityWillSaveState(const std::shared_ptr<NativeReference> &ability) override;
258     void OnAbilitySaveState(const std::shared_ptr<NativeReference> &ability) override;
259     int32_t Register(napi_value jsCallback, bool isSync = false);
260     bool UnRegister(int32_t callbackId, bool isSync = false);
261     bool IsEmpty() const;
262     static int32_t serialNumber_;
263 
264 private:
265     napi_env env_ = nullptr;
266     std::shared_ptr<NativeReference> jsCallback_;
267     std::map<int32_t, std::shared_ptr<NativeReference>> callbacks_;
268     std::map<int32_t, std::shared_ptr<NativeReference>> callbacksSync_;
269     void CallJsMethod(const std::string &methodName, const std::shared_ptr<NativeReference> &ability);
270     void CallWindowStageJsMethod(const std::string &methodName, const std::shared_ptr<NativeReference> &ability,
271         const std::shared_ptr<NativeReference> &windowStage);
272     void CallJsMethodInnerCommon(const std::string &methodName, const std::shared_ptr<NativeReference> &ability,
273         const std::shared_ptr<NativeReference> &windowStage,
274         const std::map<int32_t, std::shared_ptr<NativeReference>> callbacks);
275 };
276 }  // namespace AbilityRuntime
277 }  // namespace OHOS
278 #endif  // OHOS_ABILITY_RUNTIME_CONTEXT_ABILITY_LIFECYCLE_CALLBACK_H
279