• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 "ui_holder_extension_context.h"
26 #include "want.h"
27 #include "js_ui_extension_callback.h"
28 #include "string_wrapper.h"
29 #ifdef SUPPORT_SCREEN
30 #include "window.h"
31 #endif // SUPPORT_SCREEN
32 
33 namespace OHOS {
34 namespace AbilityRuntime {
35 using RuntimeTask = std::function<void(int, const AAFwk::Want &, bool)>;
36 using AbilityConfigUpdateCallback = std::function<void(AppExecFwk::Configuration &config)>;
37 /**
38  * @brief context supply for UIExtension
39  *
40  */
41 class UIExtensionContext : public UIHolderExtensionContext {
42 public:
43     UIExtensionContext() = default;
44     virtual ~UIExtensionContext() = default;
45 
46     /**
47      * @brief Starts a new ability.
48      * An ability using the AbilityInfo.AbilityType.EXTENSION or AbilityInfo.AbilityType.PAGE template uses this method
49      * to start a specific ability. The system locates the target ability from installed abilities based on the value
50      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
51      *
52      * @param want Indicates the Want containing information about the target ability to start.
53      *
54      * @return errCode ERR_OK on success, others on failure.
55      */
56     virtual ErrCode StartAbility(const AAFwk::Want &want) const;
57     virtual ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions) const;
58     virtual ErrCode StartAbility(const AAFwk::Want &want, int requestCode) const;
59     virtual ErrCode StartUIServiceExtension(const AAFwk::Want& want, int32_t accountId = -1) const;
60     /**
61      * @brief Destroys the current ui extension ability.
62      *
63      * @return errCode ERR_OK on success, others on failure.
64      */
65     virtual ErrCode TerminateSelf();
66         /**
67      * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
68      *
69      * @param want Indicates the want containing information about the ability to connect
70      *
71      * @param conn Indicates the callback object when the target ability is connected.
72      *
73      * @return Returns zero on success, others on failure.
74      */
75     virtual ErrCode ConnectAbility(
76         const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const;
77 
78     virtual ErrCode ConnectUIServiceExtensionAbility(
79         const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const;
80 
81     /**
82      * @brief Disconnects the current ability from an ability.
83      *
84      * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection
85      * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities.
86      *
87      * @return errCode ERR_OK on success, others on failure.
88      */
89     virtual ErrCode DisconnectAbility(
90         const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const;
91 
92     /**
93      * @brief Start service extension ability.
94      *
95      * @param want The want indicates a service extension ability.
96      * @param accountId The account id.
97      * @return ErrCode ERR_OK on success, others on failure.
98      */
99     ErrCode StartServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1);
100 
101     ErrCode StartUIAbilities(const std::vector<AAFwk::Want> &wantList, const std::string &requestKey);
102 
103     /**
104      * Start other ability for result.
105      *
106      * @param want Information of other ability.
107      * @param startOptions Indicates the StartOptions containing service side information about the target ability to
108      * start.
109      * @param requestCode Request code for abilityMS to return result.
110      * @param task Represent std::function<void(int, const AAFwk::Want &, bool)>.
111      *
112      * @return errCode ERR_OK on success, others on failure.
113      */
114     virtual ErrCode StartAbilityForResult(const AAFwk::Want &want, int requestCode, RuntimeTask &&task);
115     virtual ErrCode StartAbilityForResult(
116         const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task);
117 
118     /**
119      * Starts a new ability for result using the original caller information.
120      *
121      * @param want Information of other ability.
122      * @param requestCode Request code for abilityMS to return result.
123      * @param task Represent std::function<void(int, const AAFwk::Want &, bool)>.
124      *
125      * @return errCode ERR_OK on success, others on failure.
126      */
127     virtual ErrCode StartAbilityForResultAsCaller(const AAFwk::Want &want, int requestCode, RuntimeTask &&task);
128 
129     /**
130      * Starts a new ability for result using the original caller information.
131      *
132      * @param want Information of other ability.
133      * @param startOptions Indicates the StartOptions containing service side information about the target ability to
134      * start.
135      * @param requestCode Request code for abilityMS to return result.
136      * @param task Represent std::function<void(int, const AAFwk::Want &, bool)>.
137      *
138      * @return errCode ERR_OK on success, others on failure.
139      */
140     virtual ErrCode StartAbilityForResultAsCaller(
141         const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task);
142 
143     /**
144      * @brief Called when startAbilityForResult(ohos.aafwk.content.Want,int) is called to start an extension ability
145      * and the result is returned.
146      * @param requestCode Indicates the request code returned after the ability is started. You can define the request
147      * code to identify the results returned by abilities. The value ranges from 0 to 65535.
148      * @param resultCode Indicates the result code returned after the ability is started. You can define the result
149      * code to identify an error.
150      * @param resultData Indicates the data returned after the ability is started. You can define the data returned. The
151      * value can be null.
152      */
153     virtual void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData);
154 
155     virtual int GenerateCurRequestCode();
156 
157     virtual ErrCode ReportDrawnCompleted();
158 
159     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
160     void SetAbilityResourceManager(std::shared_ptr<Global::Resource::ResourceManager> abilityResourceMgr);
161     void RegisterAbilityConfigUpdateCallback(AbilityConfigUpdateCallback abilityConfigUpdateCallback);
162     std::shared_ptr<AppExecFwk::Configuration> GetAbilityConfiguration() const;
163     void SetAbilityConfiguration(const AppExecFwk::Configuration &config);
164     void SetAbilityColorMode(int32_t colorMode);
165 
166     /**
167      * @brief Send destroy request to the host component.
168      */
169     void RequestComponentTerminate();
170 
171 #ifdef SUPPORT_SCREEN
172     void SetWindow(sptr<Rosen::Window> window);
173 
174     sptr<Rosen::Window> GetWindow();
175 
176     Ace::UIContent* GetUIContent() override;
177 #endif // SUPPORT_SCREEN
178 
179     ErrCode OpenLink(const AAFwk::Want& want, int reuqestCode);
180 
181     ErrCode OpenAtomicService(AAFwk::Want& want, const AAFwk::StartOptions &options, int requestCode,
182         RuntimeTask &&task);
183 
184     ErrCode AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> &observer);
185 
186     void InsertResultCallbackTask(int requestCode, RuntimeTask&& task);
187 
188     void RemoveResultCallbackTask(int requestCode);
189 
190     ErrCode AddCompletionHandlerForAtomicService(const std::string &requestId, OnAtomicRequestSuccess onRequestSucc,
191         OnAtomicRequestFailure onRequestFail, const std::string &appId) override;
192 
193     void OnRequestSuccess(const std::string &requestId, const AppExecFwk::ElementName &element,
194         const std::string &message) override;
195 
196     void OnRequestFailure(const std::string &requestId, const AppExecFwk::ElementName &element,
197         const std::string &message, int32_t resultCode = 0) override;
198 
199     /**
200      * @brief Start a new ability using type;
201      * @return errCode ERR_OK on success, others on failure.
202     */
203     ErrCode StartAbilityByType(const std::string &type,
204         AAFwk::WantParams &wantParam, const std::shared_ptr<JsUIExtensionCallback> &uiExtensionCallbacks);
205     bool IsTerminating();
206     void SetTerminating(bool state);
207 
208     int32_t GetScreenMode() const;
209     void SetScreenMode(int32_t screenMode);
210     using SelfType = UIExtensionContext;
211     static const size_t CONTEXT_TYPE_ID;
212     int32_t isNotAllow = -1;
213 #ifdef SUPPORT_SCREEN
214 protected:
IsContext(size_t contextTypeId)215     bool IsContext(size_t contextTypeId) override
216     {
217         return contextTypeId == CONTEXT_TYPE_ID || UIHolderExtensionContext::IsContext(contextTypeId);
218     }
219 
220     sptr<Rosen::Window> window_ = nullptr;
221 #endif // SUPPORT_SCREEN
222 private:
223     static int ILLEGAL_REQUEST_CODE;
224     std::map<int, RuntimeTask> resultCallbacks_;
225     static int32_t curRequestCode_;
226     static std::mutex requestCodeMutex_;
227     std::mutex mutexlock_;
228     int32_t screenMode_ = AAFwk::IDLE_SCREEN_MODE;
229     std::shared_ptr<Global::Resource::ResourceManager> abilityResourceMgr_ = nullptr;
230     AbilityConfigUpdateCallback abilityConfigUpdateCallback_ = nullptr;
231     std::shared_ptr<AppExecFwk::Configuration> abilityConfiguration_ = nullptr;
232     bool isTerminating_ = false;
233     /**
234      * @brief Get Current Ability Type
235      *
236      * @return Current Ability Type
237      */
238     OHOS::AppExecFwk::AbilityType GetAbilityInfoType() const;
239 
240     void OnAbilityResultInner(int requestCode, int resultCode, const AAFwk::Want &resultData);
241 
242     void GetFailureInfoByMessage(const std::string &message, int32_t &failureCode,
243         std::string &failureMessage, int32_t resultCode);
244 
245     std::mutex onRequestResultMutex_;
246     std::vector<std::shared_ptr<OnAtomicRequestResult>> onAtomicRequestResults_;
247 };
248 }  // namespace AbilityRuntime
249 }  // namespace OHOS
250 #endif  // OHOS_ABILITY_RUNTIME_UI_EXTENSION_CONTEXT_H
251