• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_ABILITY_CONTEXT_H
17 #define OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_H
18 
19 #include "ability_connect_callback.h"
20 #include "ability_info.h"
21 #include "ability_lifecycle_observer_interface.h"
22 #include "caller_callback.h"
23 #include "configuration.h"
24 #include "free_install_observer_interface.h"
25 #include "context.h"
26 #include "iability_callback.h"
27 #include "js_ui_extension_callback.h"
28 #include "mission_info.h"
29 #include "native_engine/native_reference.h"
30 #include "native_engine/native_value.h"
31 #include "start_options.h"
32 #include "want.h"
33 
34 #ifdef SUPPORT_GRAPHICS
35 #include "pixel_map.h"
36 #endif
37 
38 namespace OHOS {
39 namespace Ace {
40 class UIContent;
41 }
42 
43 namespace AbilityRuntime {
44 using RuntimeTask = std::function<void(int, const AAFwk::Want&, bool)>;
45 using PermissionRequestTask = std::function<void(const std::vector<std::string>&, const std::vector<int>&)>;
46 using RequestDialogResultTask = std::function<void(int32_t resultCode, const AAFwk::Want&)>;
47 class LocalCallContainer;
48 constexpr int32_t DEFAULT_INVAL_VALUE = -1;
49 class AbilityContext : public Context {
50 public:
51     virtual ~AbilityContext() = default;
52 
53     /**
54      * @brief Starts a new ability.
55      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
56      * to start a specific ability. The system locates the target ability from installed abilities based on the value
57      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
58      *
59      * @param want Indicates the Want containing information about the target ability to start.
60      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
61      * template is started. You can define the request code to identify the results returned by abilities. The value
62      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
63      * template.
64      */
65     virtual ErrCode StartAbility(const AAFwk::Want &want, int requestCode) = 0;
66 
67     /**
68      * @brief Starts a new ability.
69      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
70      * to start a specific ability. The system locates the target ability from installed abilities based on the value
71      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
72      *
73      * @param want Indicates the Want containing information about the target ability to start.
74      * @param accountId ability caller accountId.
75      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
76      * template is started. You can define the request code to identify the results returned by abilities. The value
77      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
78      * template.
79      */
80     virtual ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId, int requestCode) = 0;
81 
82     /**
83      * @brief Starts a new ability.
84      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
85      * to start a specific ability. The system locates the target ability from installed abilities based on the value
86      * of the want parameter and the value of the start options and then starts it. You can specify the ability to
87      * start using the two parameters.
88      *
89      * @param want Indicates the Want containing application side information about the target ability to start.
90      * @param startOptions Indicates the StartOptions containing service side information about the target ability to
91      * start.
92      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
93      * template is started. You can define the request code to identify the results returned by abilities. The value
94      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
95      * template.
96      */
97     virtual ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode) = 0;
98 
99     /**
100      * @brief Starts a new ability using the original caller information.
101      * Start a new ability as if it was started by the ability that started current ability. This is for the confirm
102      * ability and selection ability, which passthrough their want to the target.
103      *
104      * @param want Indicates the Want containing information about the target ability to start.
105      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
106      * template is started. You can define the request code to identify the results returned by abilities. The value
107      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
108      * template.
109      */
110     virtual ErrCode StartAbilityAsCaller(const AAFwk::Want &want, int requestCode) = 0;
111 
112     /**
113      * @brief Starts a new ability using the original caller information.
114      * Start a new ability as if it was started by the ability that started current ability. This is for the confirm
115      * ability and selection ability, which passthrough their want to the target.
116      *
117      * @param want Indicates the Want containing information about the target ability to start.
118      * @param startOptions Indicates the StartOptions containing service side information about the target ability to
119      * start.
120      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
121      * template is started. You can define the request code to identify the results returned by abilities. The value
122      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
123      * template.
124      */
125     virtual ErrCode StartAbilityAsCaller(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions,
126         int requestCode) = 0;
127 
128     /**
129      * @brief Starts a new ability.
130      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
131      * to start a specific ability. The system locates the target ability from installed abilities based on the value
132      * of the want parameter and the value of the start options and then starts it. You can specify the ability to
133      * start using the two parameters.
134      *
135      * @param want Indicates the Want containing application side information about the target ability to start.
136      * @param accountId caller userId.
137      * @param startOptions Indicates the StartOptions containing service side information about the target ability to
138      * start.
139      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
140      * template is started. You can define the request code to identify the results returned by abilities. The value
141      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
142      * template.
143      */
144     virtual ErrCode StartAbilityWithAccount(
145         const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions, int requestCode) = 0;
146 
147     virtual ErrCode StartAbilityForResult(const AAFwk::Want &Want, int requestCode, RuntimeTask &&task) = 0;
148 
149     virtual ErrCode StartAbilityForResultWithAccount(
150         const AAFwk::Want &Want, int accountId, int requestCode, RuntimeTask &&task) = 0;
151 
152     virtual ErrCode StartAbilityForResult(const AAFwk::Want &Want, const AAFwk::StartOptions &startOptions,
153         int requestCode, RuntimeTask &&task) = 0;
154 
155     virtual ErrCode StartAbilityForResultWithAccount(const AAFwk::Want &Want, int accountId,
156         const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task) = 0;
157 
158     virtual ErrCode StartServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1) = 0;
159 
160     virtual ErrCode StopServiceExtensionAbility(const AAFwk::Want& want, int32_t accountId = -1) = 0;
161 
162     virtual ErrCode TerminateAbilityWithResult(const AAFwk::Want &want, int resultCode) = 0;
163 
164     virtual ErrCode BackToCallerAbilityWithResult(const AAFwk::Want &want, int resultCode, int64_t requestCode) = 0;
165 
166     virtual ErrCode RestoreWindowStage(napi_env env, napi_value contentStorage) = 0;
167 
168     virtual void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData) = 0;
169 
170     virtual ErrCode RequestModalUIExtension(const AAFwk::Want& want) = 0;
171 
172     virtual ErrCode OpenLink(const AAFwk::Want& want, int requestCode) = 0;
173 
174     virtual ErrCode OpenAtomicService(AAFwk::Want& want, const AAFwk::StartOptions &options, int requestCode,
175         RuntimeTask &&task) = 0;
176 
177     virtual ErrCode AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> &observer) = 0;
178 
ChangeAbilityVisibility(bool isShow)179     virtual ErrCode ChangeAbilityVisibility(bool isShow) { return 0; }
180 
181     /**
182     * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
183     *
184     * @param want Indicates the want containing information about the ability to connect
185     * @param connectCallback Indicates the callback object when the target ability is connected.
186     * @return True means success and false means failure
187     */
188     virtual ErrCode ConnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) = 0;
189 
190     /**
191      * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
192      * @param want Indicates the want containing information about the ability to connect
193      * @param accountId caller userId.
194      * @param connectCallback Indicates the callback object when the target ability is connected.
195      * @return True means success and false means failure
196      */
197     virtual ErrCode ConnectAbilityWithAccount(const AAFwk::Want &want, int accountId,
198                                 const sptr<AbilityConnectCallback> &connectCallback) = 0;
199 
200     /**
201     * @brief Disconnects the current ability from an ability
202     *
203     * @param want Indicates the want containing information about the ability to disconnect
204     * @param connectCallback Indicates the callback object when the target ability is connected.
205     * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities.
206     */
207     virtual void DisconnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback,
208         int32_t accountId = -1) = 0;
209 
210     /**
211      * @brief get ability info of the current ability
212      *
213      * @return the ability info of the current ability
214      */
215     virtual std::shared_ptr<AppExecFwk::AbilityInfo> GetAbilityInfo() const = 0;
216 
217     /**
218      * @brief Minimize the current ability.
219      *
220      * @param fromUser mark the minimize operation source.
221      */
222     virtual void MinimizeAbility(bool fromUser = false) = 0;
223 
224     /**
225      * @brief Get OnBackPressedCallBack.
226      * @param needMoveToBackground true if ability will be moved to background; false if ability will be terminated.
227      *
228      * @return Returns ERR_OK on success, others on failure.
229      */
230     virtual ErrCode OnBackPressedCallBack(bool &needMoveToBackground) = 0;
231 
232     virtual ErrCode MoveAbilityToBackground() = 0;
233 
234     virtual ErrCode MoveUIAbilityToBackground() = 0;
235 
236     virtual ErrCode TerminateSelf() = 0;
237 
238     virtual ErrCode CloseAbility() = 0;
239 
240     /**
241      * @brief Get ContentStorage.
242      *
243      * @return Returns the ContentStorage.
244      */
245     virtual std::unique_ptr<NativeReference>& GetContentStorage() = 0;
246 
247     /**
248      * call function by callback object
249      *
250      * @param want Request info for ability.
251      * @param callback Indicates the callback object.
252      * @param accountId Indicates the account to start.
253      *
254      * @return Returns zero on success, others on failure.
255      */
256     virtual ErrCode StartAbilityByCall(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback,
257         int32_t accountId = DEFAULT_INVAL_VALUE) = 0;
258 
259     /**
260      * caller release by callback object
261      *
262      * @param callback Indicates the callback object.
263      *
264      * @return Returns zero on success, others on failure.
265      */
266     virtual ErrCode ReleaseCall(const std::shared_ptr<CallerCallBack> &callback) = 0;
267 
268     /**
269      * clear failed call connection by callback object
270      *
271      * @param callback Indicates the callback object.
272      *
273      * @return void.
274      */
275     virtual void ClearFailedCallConnection(const std::shared_ptr<CallerCallBack> &callback) = 0;
276 
277     /**
278      * @brief Get LocalCallContainer.
279      *
280      * @return Returns the LocalCallContainer.
281      */
282     virtual std::shared_ptr<LocalCallContainer> GetLocalCallContainer() = 0;
283 
284     virtual void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config) = 0;
285 
286     virtual void RegisterAbilityCallback(std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback) = 0;
287 
288     virtual void SetWeakSessionToken(const wptr<IRemoteObject>& sessionToken) = 0;
289     virtual void SetAbilityRecordId(int32_t abilityRecordId) = 0;
290     virtual int32_t GetAbilityRecordId() = 0;
291 
292     /**
293      * @brief Requests dialogService from the system.
294      * This method is called for dialog request. This is an asynchronous method. When it is executed,
295      * the task will be called back.
296      *
297      * @param env js env.
298      * @param want Indicates the dialog service to be requested.
299      * @param task The callback or promise fo js interface.
300      * @return Returns ERR_OK if success.
301      */
302     virtual ErrCode RequestDialogService(napi_env env, AAFwk::Want &want, RequestDialogResultTask &&task) = 0;
303 
304     /**
305      * @brief Requests dialogService from the system.
306      * This method is called for dialog request. This is an asynchronous method. When it is executed,
307      * the task will be called back.
308      *
309      * @param want Indicates the dialog service to be requested.
310      * @param task The callback or promise fo js interface.
311      * @return Returns ERR_OK if success.
312      */
313     virtual ErrCode RequestDialogService(AAFwk::Want &want, RequestDialogResultTask &&task) = 0;
314 
315     /**
316      * @brief Report drawn completed.
317      *
318      * @return Returns ERR_OK on success, others on failure.
319      */
320     virtual ErrCode ReportDrawnCompleted() = 0;
321 
322     virtual ErrCode GetMissionId(int32_t &missionId) = 0;
323 
324     /**
325      * Set mission continue state of this ability.
326      *
327      * @param state the mission continuation state of this ability.
328      * @return Returns ERR_OK if success.
329      */
330     virtual ErrCode SetMissionContinueState(const AAFwk::ContinueState &state) = 0;
331 
332     /**
333      * Register lifecycle observer on ability.
334      *
335      * @param observer the lifecycle observer to be registered on ability.
336      */
337     virtual void RegisterAbilityLifecycleObserver(const std::shared_ptr<AppExecFwk::ILifecycleObserver> &observer) = 0;
338 
339     /**
340      * Unregister lifecycle observer on ability.
341      *
342      * @param observer the lifecycle observer to be unregistered on ability.
343      */
344     virtual void UnregisterAbilityLifecycleObserver(
345         const std::shared_ptr<AppExecFwk::ILifecycleObserver> &observer) = 0;
346 
347     virtual std::shared_ptr<AAFwk::Want> GetWant() = 0;
348 
349 #ifdef SUPPORT_GRAPHICS
350     /**
351      * @brief Set mission label of this ability.
352      *
353      * @param label the label of this ability.
354      * @return Returns ERR_OK if success.
355      */
356     virtual ErrCode SetMissionLabel(const std::string &label) = 0;
357 
358     /**
359      * @brief Set mission icon of this ability.
360      *
361      * @param icon the icon of this ability.
362      * @return Returns ERR_OK if success.
363      */
364     virtual ErrCode SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon) = 0;
365 
366     virtual int GetCurrentWindowMode() = 0;
367 
368     /**
369      * @brief Get window rectangle of this ability.
370      *
371      * @param the left position of window rectangle.
372      * @param the top position of window rectangle.
373      * @param the width position of window rectangle.
374      * @param the height position of window rectangle.
375      */
376     virtual void GetWindowRect(int32_t &left, int32_t &top, int32_t &width, int32_t &height) = 0;
377 
378     /**
379      * @brief Get ui content object.
380      *
381      * @return UIContent object of ACE.
382      */
383     virtual Ace::UIContent* GetUIContent() = 0;
384     virtual ErrCode StartAbilityByType(const std::string &type, AAFwk::WantParams &wantParam,
385         const std::shared_ptr<JsUIExtensionCallback> &uiExtensionCallbacks) = 0;
386     virtual ErrCode CreateModalUIExtensionWithApp(const AAFwk::Want &want) = 0;
387     virtual void EraseUIExtension(int32_t sessionId) = 0;
388 #endif
389     virtual bool IsTerminating() = 0;
390     virtual void SetTerminating(bool state) = 0;
391     virtual void InsertResultCallbackTask(int requestCode, RuntimeTask&& task) = 0;
392     virtual void RemoveResultCallbackTask(int requestCode) = 0;
393     using SelfType = AbilityContext;
394     static const size_t CONTEXT_TYPE_ID;
395 
396 protected:
IsContext(size_t contextTypeId)397     bool IsContext(size_t contextTypeId) override
398     {
399         return contextTypeId == CONTEXT_TYPE_ID || Context::IsContext(contextTypeId);
400     }
401 };
402 } // namespace AbilityRuntime
403 } // namespace OHOS
404 #endif // OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_H
405