• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_UI_EXTENSION_CONTEXT_H
17 #define OHOS_ABILITY_RUNTIME_UI_EXTENSION_CONTEXT_H
18 
19 #include <map>
20 
21 #include "ability_connect_callback.h"
22 #include "extension_context.h"
23 #include "start_options.h"
24 #include "want.h"
25 
26 namespace OHOS {
27 namespace AbilityRuntime {
28 using RuntimeTask = std::function<void(int, const AAFwk::Want &, bool)>;
29 /**
30  * @brief context supply for UIExtension
31  *
32  */
33 class UIExtensionContext : public ExtensionContext {
34 public:
35     UIExtensionContext() = default;
36     virtual ~UIExtensionContext() = default;
37 
38     /**
39      * @brief Starts a new ability.
40      * An ability using the AbilityInfo.AbilityType.EXTENSION or AbilityInfo.AbilityType.PAGE template uses this method
41      * to start a specific ability. The system locates the target ability from installed abilities based on the value
42      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
43      *
44      * @param want Indicates the Want containing information about the target ability to start.
45      *
46      * @return errCode ERR_OK on success, others on failure.
47      */
48     virtual ErrCode StartAbility(const AAFwk::Want &want) const;
49     virtual ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions) const;
50 
51     /**
52      * @brief Destroys the current ui extension ability.
53      *
54      * @return errCode ERR_OK on success, others on failure.
55      */
56     virtual ErrCode TerminateSelf();
57         /**
58      * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
59      *
60      * @param want Indicates the want containing information about the ability to connect
61      *
62      * @param conn Indicates the callback object when the target ability is connected.
63      *
64      * @return Returns zero on success, others on failure.
65      */
66     virtual ErrCode ConnectAbility(
67         const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const;
68 
69     /**
70      * @brief Disconnects the current ability from an ability.
71      *
72      * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection
73      * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities.
74      *
75      * @return errCode ERR_OK on success, others on failure.
76      */
77     virtual ErrCode DisconnectAbility(
78         const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const;
79     /**
80      * Start other ability for result.
81      *
82      * @param want Information of other ability.
83      * @param startOptions Indicates the StartOptions containing service side information about the target ability to
84      * start.
85      * @param requestCode Request code for abilityMS to return result.
86      * @param task Represent std::function<void(int, const AAFwk::Want &, bool)>.
87      *
88      * @return errCode ERR_OK on success, others on failure.
89      */
90     virtual ErrCode StartAbilityForResult(const AAFwk::Want &want, int requestCode, RuntimeTask &&task);
91     virtual ErrCode StartAbilityForResult(
92         const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task);
93 
94     /**
95      * @brief Called when startAbilityForResult(ohos.aafwk.content.Want,int) is called to start an extension ability
96      * and the result is returned.
97      * @param requestCode Indicates the request code returned after the ability is started. You can define the request
98      * code to identify the results returned by abilities. The value ranges from 0 to 65535.
99      * @param resultCode Indicates the result code returned after the ability is started. You can define the result
100      * code to identify an error.
101      * @param resultData Indicates the data returned after the ability is started. You can define the data returned. The
102      * value can be null.
103      */
104     virtual void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData);
105 
106     virtual int GenerateCurRequestCode();
107 
108     using SelfType = UIExtensionContext;
109     static const size_t CONTEXT_TYPE_ID;
110 
111 private:
112     static int ILLEGAL_REQUEST_CODE;
113     std::map<int, RuntimeTask> resultCallbacks_;
114 
115     int curRequestCode_ = 0;
116 
117     /**
118      * @brief Get Current Ability Type
119      *
120      * @return Current Ability Type
121      */
122     OHOS::AppExecFwk::AbilityType GetAbilityInfoType() const;
123 
124     void OnAbilityResultInner(int requestCode, int resultCode, const AAFwk::Want &resultData);
125 };
126 }  // namespace AbilityRuntime
127 }  // namespace OHOS
128 #endif  // OHOS_ABILITY_RUNTIME_UI_EXTENSION_CONTEXT_H
129