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