1 /*
2  * Copyright (c) 2021-2024 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_EXTENSION_H
17 #define OHOS_ABILITY_RUNTIME_EXTENSION_H
18 
19 #include <string>
20 
21 #include "ability_transaction_callback_info.h"
22 #include "napi_remote_object.h"
23 #include "iremote_object.h"
24 #include "session_info.h"
25 #include "foundation/window/window_manager/interfaces/innerkits/wm/window.h"
26 #include "launch_param.h"
27 #include "ui_extension_window_command.h"
28 #include "want.h"
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 struct AbilityInfo;
33 class OHOSApplication;
34 class AbilityHandler;
35 class AbilityLocalRecord;
36 class Configuration;
37 struct InsightIntentExecuteResult;
38 }
39 namespace AbilityRuntime {
40 using Want = OHOS::AAFwk::Want;
41 /**
42  * @brief The Extension component to schedule task with no pages.
43  */
44 class Extension : public std::enable_shared_from_this<Extension> {
45 public:
46     Extension() = default;
47     virtual ~Extension() = default;
48 
49     /**
50      * @brief Init the extension.
51      *
52      * @param record the extension record.
53      * @param application the application info.
54      * @param handler the extension handler.
55      * @param token the remote token.
56      */
57     virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
58         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
59         std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
60         const sptr<IRemoteObject> &token);
61 
62     /**
63      * @brief Sets the first want object.
64      *
65      * @param Want information of other ability or extension.
66      */
67     void SetLaunchWant(const AAFwk::Want &want);
68 
69     /**
70      * @brief Obtains the first want object.
71      *
72      * @return Returns the first want object.
73      */
74     std::shared_ptr<AAFwk::Want> GetLaunchWant();
75 
76     /**
77      * @brief Sets the last want object.
78      *
79      * @param Want information of other ability or extension.
80      */
81     void SetLastRequestWant(const AAFwk::Want &want);
82 
83     /**
84      * @brief Sets the callingInfo from IPC.
85      *
86      * @param CallingInfo information of caller.
87      */
88     void SetCallingInfo(const CallingInfo &callingInfo);
89 
90     /**
91      * @return std::shared_ptr<CallingInfo> the pointer of callingInfo.
92      */
93     std::shared_ptr<CallingInfo> GetCallingInfo();
94 
95     /**
96      * @brief Called when this extension is started. You must override this function if you want to perform some
97      *        initialization operations during extension startup.
98      *
99      * This function can be called only once in the entire lifecycle of an extension.
100      * @param Want Indicates the {@link Want} structure containing startup information about the extension.
101      */
102     virtual void OnStart(const AAFwk::Want &want);
103     virtual void OnStart(const AAFwk::Want &want, sptr<AAFwk::SessionInfo> sessionInfo);
104 
105     /**
106      * @brief Called when this Service extension is connected for the first time.
107      *
108      * You can override this function to implement your own processing logic.
109      *
110      * @param want Indicates the {@link Want} structure containing connection information about the Service extension.
111      * @return Returns a pointer to the <b>sid</b> of the connected Service extension.
112      */
113     virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want);
114 
115     /**
116      * @brief Called when this Service extension is connected for the first time.
117      *
118      * You can override this function to implement your own processing logic.
119      *
120      * @param want Indicates the {@link Want} structure containing connection information about the Service extension.
121      * @param callbackInfo Indicates the lifecycle transaction callback information
122      * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback
123      * @return Returns a pointer to the <b>sid</b> of the connected Service extension.
124      */
125     virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want,
126         AppExecFwk::AbilityTransactionCallbackInfo<sptr<IRemoteObject>> *callbackInfo, bool &isAsyncCallback);
127 
128     /**
129      * @brief Called when all abilities connected to this Service extension are disconnected.
130      *
131      * You can override this function to implement your own processing logic.
132      *
133      */
134     virtual void OnDisconnect(const AAFwk::Want &want);
135 
136     /**
137      * @brief Called when all abilities connected to this Service extension are disconnected.
138      *
139      * You can override this function to implement your own processing logic.
140      * @param callbackInfo Indicates the lifecycle transaction callback information
141      * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback
142      */
143     virtual void OnDisconnect(const AAFwk::Want &want, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo,
144         bool &isAsyncCallback);
145 
146     /**
147      * @brief Called back when Service is started.
148      * This method can be called only by Service. You can use the StartAbility(ohos.aafwk.content.Want) method to start
149      * Service. Then the system calls back the current method to use the transferred want parameter to execute its own
150      * logic.
151      *
152      * @param want Indicates the want of Service to start.
153      * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
154      * destroyed, and the value false indicates a normal startup.
155      * @param startId Indicates the number of times the Service extension has been started. The startId is
156      * incremented by 1 every time the extension is started. For example, if the extension has been started
157      * for six times, the value of startId is 6.
158      */
159     virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId);
160 
161     virtual void OnCommandWindow(const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo,
162         AAFwk::WindowCommand winCmd);
163     /**
164      * @brief Called when this extension enters the <b>STATE_STOP</b> state.
165      *
166      * The extension in the <b>STATE_STOP</b> is being destroyed.
167      * You can override this function to implement your own processing logic.
168      */
169     virtual void OnStop();
170     /**
171      * @brief Called when this extension enters the <b>STATE_STOP</b> state.
172      *
173      * The ability in the <b>STATE_STOP</b> is being destroyed.
174      * You can override this function to implement your own processing logic.
175      *
176      * @param callbackInfo Indicates the lifecycle transaction callback information
177      * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback
178      */
179     virtual void OnStop(AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo, bool &isAsyncCallback);
180 
181     /**
182      * @brief Called when the system configuration is updated.
183      *
184      * @param configuration Indicates the updated configuration information.
185      */
186 
187     /**
188      * @brief The callback of OnStop.
189      */
190     virtual void OnStopCallBack();
191 
192     virtual void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration);
193 
194     /**
195      * @brief Notify current memory level.
196      *
197      * @param level Current memory level.
198      */
199     virtual void OnMemoryLevel(int level);
200 
201     /**
202      * @brief Called when this extension enters the <b>STATE_FOREGROUND</b> state.
203      *
204      *
205      * The extension in the <b>STATE_FOREGROUND</b> state is visible.
206      * You can override this function to implement your own processing logic.
207      */
208     virtual void OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo);
209 
210     /**
211      * @brief Called when this extension enters the <b>STATE_BACKGROUND</b> state.
212      *
213      *
214      * The extension in the <b>STATE_BACKGROUND</b> state is invisible.
215      * You can override this function to implement your own processing logic.
216      */
217     virtual void OnBackground();
218 
219     /**
220      * @brief Called when extension need dump info.
221      *
222      * @param params The params from service.
223      * @param info The dump info to show.
224      */
225     virtual void Dump(const std::vector<std::string> &params, std::vector<std::string> &info);
226 
227     void SetExtensionWindowLifeCycleListener(const sptr<Rosen::IWindowLifeCycle> &listener);
228 
229     /**
230      * @brief Set the launch param.
231      *
232      * @param launchParam The launch param.
233      */
234     void SetLaunchParam(const AAFwk::LaunchParam &launchParam);
235 
236     /**
237      * @brief Get the launch param.
238      *
239      * @return Launch param information.
240      */
241     const AAFwk::LaunchParam &GetLaunchParam() const;
242 
243     /**
244      * @brief Called when startAbilityForResult(ohos.aafwk.content.Want,int) is called to start an extension ability
245      * and the result is returned.
246      * @param requestCode Indicates the request code returned after the ability is started. You can define the request
247      * code to identify the results returned by abilities. The value ranges from 0 to 65535.
248      * @param resultCode Indicates the result code returned after the ability is started. You can define the result
249      * code to identify an error.
250      * @param want Indicates the data returned after the ability is started. You can define the data returned. The
251      * value can be null.
252      */
253     virtual void OnAbilityResult(int requestCode, int resultCode, const Want &want);
254 
255     virtual void OnCommandWindowDone(const sptr<AAFwk::SessionInfo> &sessionInfo, AAFwk::WindowCommand winCmd);
256 
257     virtual void OnInsightIntentExecuteDone(const sptr<AAFwk::SessionInfo> &sessionInfo,
258         const AppExecFwk::InsightIntentExecuteResult &result);
259 
260     virtual bool HandleInsightIntent(const AAFwk::Want &want);
261 
262     virtual bool OnInsightIntentExecuteDone(uint64_t intentId, const AppExecFwk::InsightIntentExecuteResult &result);
263 
264     std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo_ = nullptr;
265 protected:
266     std::shared_ptr<AppExecFwk::AbilityHandler> handler_ = nullptr;
267 
268     //  window scene
269     sptr<Rosen::IWindowLifeCycle> extensionWindowLifeCycleListener_ = nullptr;
270 private:
271     std::shared_ptr<AppExecFwk::OHOSApplication> application_ = nullptr;
272     std::shared_ptr<AAFwk::Want> launchWant_ = nullptr;
273     std::shared_ptr<AAFwk::Want> lastRequestWant_ = nullptr;
274     AAFwk::LaunchParam launchParam_;
275     std::shared_ptr<CallingInfo> callingInfo_ = nullptr;
276 };
277 }  // namespace AbilityRuntime
278 }  // namespace OHOS
279 #endif  // OHOS_ABILITY_RUNTIME_EXTENSION_H
280