1 /*
2  * Copyright (c) 2023-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_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 "free_install_observer_interface.h"
24 #include "start_options.h"
25 #include "want.h"
26 #include "window.h"
27 
28 namespace OHOS {
29 namespace Ace {
30 class UIContent;
31 }
32 namespace AbilityRuntime {
33 using RuntimeTask = std::function<void(int, const AAFwk::Want &, bool)>;
34 /**
35  * @brief context supply for UIExtension
36  *
37  */
38 class UIExtensionContext : public ExtensionContext {
39 public:
40     UIExtensionContext() = default;
41     virtual ~UIExtensionContext() = default;
42 
43     /**
44      * @brief Starts a new ability.
45      * An ability using the AbilityInfo.AbilityType.EXTENSION or AbilityInfo.AbilityType.PAGE template uses this method
46      * to start a specific ability. The system locates the target ability from installed abilities based on the value
47      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
48      *
49      * @param want Indicates the Want containing information about the target ability to start.
50      *
51      * @return errCode ERR_OK on success, others on failure.
52      */
53     virtual ErrCode StartAbility(const AAFwk::Want &want) const;
54     virtual ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions) const;
55     virtual ErrCode StartAbility(const AAFwk::Want &want, int requestCode) const;
56     virtual ErrCode StartUIServiceExtension(const AAFwk::Want& want, int32_t accountId = -1) const;
57     /**
58      * @brief Destroys the current ui extension ability.
59      *
60      * @return errCode ERR_OK on success, others on failure.
61      */
62     virtual ErrCode TerminateSelf();
63         /**
64      * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
65      *
66      * @param want Indicates the want containing information about the ability to connect
67      *
68      * @param conn Indicates the callback object when the target ability is connected.
69      *
70      * @return Returns zero on success, others on failure.
71      */
72     virtual ErrCode ConnectAbility(
73         const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const;
74 
75     virtual ErrCode ConnectUIServiceExtensionAbility(
76         const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const;
77 
78     /**
79      * @brief Disconnects the current ability from an ability.
80      *
81      * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection
82      * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities.
83      *
84      * @return errCode ERR_OK on success, others on failure.
85      */
86     virtual ErrCode DisconnectAbility(
87         const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const;
88     /**
89      * Start other ability for result.
90      *
91      * @param want Information of other ability.
92      * @param startOptions Indicates the StartOptions containing service side information about the target ability to
93      * start.
94      * @param requestCode Request code for abilityMS to return result.
95      * @param task Represent std::function<void(int, const AAFwk::Want &, bool)>.
96      *
97      * @return errCode ERR_OK on success, others on failure.
98      */
99     virtual ErrCode StartAbilityForResult(const AAFwk::Want &want, int requestCode, RuntimeTask &&task);
100     virtual ErrCode StartAbilityForResult(
101         const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task);
102 
103     /**
104      * Starts a new ability for result using the original caller information.
105      *
106      * @param want Information of other ability.
107      * @param requestCode Request code for abilityMS to return result.
108      * @param task Represent std::function<void(int, const AAFwk::Want &, bool)>.
109      *
110      * @return errCode ERR_OK on success, others on failure.
111      */
112     virtual ErrCode StartAbilityForResultAsCaller(const AAFwk::Want &want, int requestCode, RuntimeTask &&task);
113 
114     /**
115      * Starts a new ability for result using the original caller information.
116      *
117      * @param want Information of other ability.
118      * @param startOptions Indicates the StartOptions containing service side information about the target ability to
119      * start.
120      * @param requestCode Request code for abilityMS to return result.
121      * @param task Represent std::function<void(int, const AAFwk::Want &, bool)>.
122      *
123      * @return errCode ERR_OK on success, others on failure.
124      */
125     virtual ErrCode StartAbilityForResultAsCaller(
126         const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task);
127 
128     /**
129      * @brief Called when startAbilityForResult(ohos.aafwk.content.Want,int) is called to start an extension ability
130      * and the result is returned.
131      * @param requestCode Indicates the request code returned after the ability is started. You can define the request
132      * code to identify the results returned by abilities. The value ranges from 0 to 65535.
133      * @param resultCode Indicates the result code returned after the ability is started. You can define the result
134      * code to identify an error.
135      * @param resultData Indicates the data returned after the ability is started. You can define the data returned. The
136      * value can be null.
137      */
138     virtual void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData);
139 
140     virtual int GenerateCurRequestCode();
141 
142     virtual ErrCode ReportDrawnCompleted();
143 
144     void SetWindow(sptr<Rosen::Window> window);
145 
146     sptr<Rosen::Window> GetWindow();
147 
148     Ace::UIContent* GetUIContent();
149 
150     ErrCode OpenLink(const AAFwk::Want& want, int reuqestCode);
151 
152     ErrCode OpenAtomicService(AAFwk::Want& want, const AAFwk::StartOptions &options, int requestCode,
153         RuntimeTask &&task);
154 
155     ErrCode AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> &observer);
156 
157     void InsertResultCallbackTask(int requestCode, RuntimeTask&& task);
158 
159     void RemoveResultCallbackTask(int requestCode);
160 
161     using SelfType = UIExtensionContext;
162     static const size_t CONTEXT_TYPE_ID;
163 
164 protected:
IsContext(size_t contextTypeId)165     bool IsContext(size_t contextTypeId) override
166     {
167         return contextTypeId == CONTEXT_TYPE_ID || ExtensionContext::IsContext(contextTypeId);
168     }
169 
170 private:
171     static int ILLEGAL_REQUEST_CODE;
172     std::map<int, RuntimeTask> resultCallbacks_;
173 
174     static int32_t curRequestCode_;
175     static std::mutex requestCodeMutex_;
176 
177     sptr<Rosen::Window> window_ = nullptr;
178 
179     std::mutex mutexlock_;
180     /**
181      * @brief Get Current Ability Type
182      *
183      * @return Current Ability Type
184      */
185     OHOS::AppExecFwk::AbilityType GetAbilityInfoType() const;
186 
187     void OnAbilityResultInner(int requestCode, int resultCode, const AAFwk::Want &resultData);
188 };
189 }  // namespace AbilityRuntime
190 }  // namespace OHOS
191 #endif  // OHOS_ABILITY_RUNTIME_UI_EXTENSION_CONTEXT_H
192