• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_SERVICE_EXTENSION_CONTEXT_H
17 #define OHOS_ABILITY_RUNTIME_SERVICE_EXTENSION_CONTEXT_H
18 
19 #include "extension_context.h"
20 
21 #include "ability_connect_callback.h"
22 #include "connection_manager.h"
23 #include "free_install_observer_interface.h"
24 #include "local_call_container.h"
25 #include "start_options.h"
26 #include "want.h"
27 
28 namespace OHOS {
29 namespace AbilityRuntime {
30 /**
31  * @brief context supply for service
32  *
33  */
34 class ServiceExtensionContext : public ExtensionContext {
35 public:
36     ServiceExtensionContext() = default;
37     virtual ~ServiceExtensionContext() = default;
38 
39     /**
40      * @brief Starts a new ability.
41      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
42      * to start a specific ability. The system locates the target ability from installed abilities based on the value
43      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
44      *
45      * @param want Indicates the Want containing information about the target ability to start.
46      *
47      * @return errCode ERR_OK on success, others on failure.
48      */
49     ErrCode StartAbility(const AAFwk::Want &want) const;
50 
51     ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions) const;
52 
53     /**
54      * @brief Starts a new ability using the original caller information.
55      * Start a new ability as if it was started by the ability that started current ability. This is for the confirm
56      * ability and selection ability, which passthrough their want to the target.
57      *
58      * @param want Indicates the Want containing information about the target ability to start.
59      *
60      * @return errCode ERR_OK on success, others on failure.
61      */
62     ErrCode StartAbilityAsCaller(const AAFwk::Want &want) const;
63 
64     ErrCode StartAbilityAsCaller(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions) const;
65 
66     /**
67      * call function by callback object
68      *
69      * @param want Request info for ability.
70      * @param callback Indicates the callback object.
71      * @param accountId Indicates the account to start.
72      *
73      * @return Returns zero on success, others on failure.
74      */
75     ErrCode StartAbilityByCall(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback,
76         int32_t accountId = DEFAULT_INVAL_VALUE);
77 
78     ErrCode AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> &observer);
79 
80     /**
81      * caller release by callback object
82      *
83      * @param callback Indicates the callback object.
84      *
85      * @return Returns zero on success, others on failure.
86      */
87     ErrCode ReleaseCall(const std::shared_ptr<CallerCallBack> &callback) const;
88 
89     /**
90      * clear failed call connection by callback object
91      *
92      * @param callback Indicates the callback object.
93      *
94      * @return void.
95      */
96     void ClearFailedCallConnection(const std::shared_ptr<CallerCallBack> &callback) const;
97 
98     /**
99      * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
100      *
101      * @param want Indicates the want containing information about the ability to connect
102      *
103      * @param conn Indicates the callback object when the target ability is connected.
104      *
105      * @return Returns zero on success, others on failure.
106      */
107     ErrCode ConnectAbility(
108         const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const;
109 
110     /**
111      * @brief Starts a new ability.
112      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
113      * to start a specific ability. The system locates the target ability from installed abilities based on the value
114      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
115      *
116      * @param want Indicates the Want containing information about the target ability to start.
117      * @param accountId caller user.
118      *
119      * @return errCode ERR_OK on success, others on failure.
120      */
121     ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId) const;
122 
123     ErrCode StartAbilityWithAccount(
124         const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions) const;
125 
126     ErrCode StartUIAbilities(const std::vector<AAFwk::Want> &wantList, const std::string &requestKey);
127 
128     ErrCode StartServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1) const;
129 
130     ErrCode StartUIServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1) const;
131 
132     ErrCode StopServiceExtensionAbility(const AAFwk::Want& want, int32_t accountId = -1) const;
133 
134     /**
135      * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
136      *
137      * @param want Indicates the want containing information about the ability to connect.
138      *
139      * @param accountId caller user.
140      *
141      * @param conn Indicates the callback object when the target ability is connected.
142      *
143      * @return Returns zero on success, others on failure.
144      */
145     ErrCode ConnectAbilityWithAccount(
146         const AAFwk::Want &want, int accountId, const sptr<AbilityConnectCallback> &connectCallback) const;
147 
148     /**
149      * @brief Disconnects the current ability from an ability.
150      *
151      * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection
152      * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities.
153      *
154      * @return errCode ERR_OK on success, others on failure.
155      */
156     ErrCode DisconnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback,
157         int32_t accountId = -1) const;
158 
159     /**
160      * @brief Destroys the current ability.
161      *
162      * @return errCode ERR_OK on success, others on failure.
163      */
164     ErrCode TerminateAbility();
165 
166     ErrCode RequestModalUIExtension(const Want &want);
167 
168     ErrCode PreStartMission(const std::string& bundleName, const std::string& moduleName,
169         const std::string& abilityName, const std::string& startTime);
170 
171     using SelfType = ServiceExtensionContext;
172     static const size_t CONTEXT_TYPE_ID;
173 
174     ErrCode OpenLink(const AAFwk::Want& want, int reuqestCode);
175     ErrCode OpenAtomicService(const AAFwk::Want &want, const AAFwk::StartOptions &options);
176 
177     ErrCode AddCompletionHandlerForAtomicService(const std::string &requestId, OnAtomicRequestSuccess onRequestSucc,
178         OnAtomicRequestFailure onRequestFail, const std::string &appId) override;
179 
180     void OnRequestSuccess(const std::string &requestId, const AppExecFwk::ElementName &element,
181         const std::string &message) override;
182 
183     void OnRequestFailure(const std::string &requestId, const AppExecFwk::ElementName &element,
184         const std::string &message, int32_t resultCode = 0) override;
185 
186 protected:
IsContext(size_t contextTypeId)187     bool IsContext(size_t contextTypeId) override
188     {
189         return contextTypeId == CONTEXT_TYPE_ID || ExtensionContext::IsContext(contextTypeId);
190     }
191 
192 private:
193     static int ILLEGAL_REQUEST_CODE;
194     std::shared_ptr<LocalCallContainer> localCallContainer_ = nullptr;
195 
196     /**
197      * @brief Get Current Ability Type
198      *
199      * @return Current Ability Type
200      */
201     OHOS::AppExecFwk::AbilityType GetAbilityInfoType() const;
202 
203     void GetFailureInfoByMessage(const std::string &message, int32_t &failureCode,
204         std::string &failureMessage, int32_t resultCode);
205 
206     std::mutex onRequestResultMutex_;
207     std::vector<std::shared_ptr<OnAtomicRequestResult>> onAtomicRequestResults_;
208 };
209 }  // namespace AbilityRuntime
210 }  // namespace OHOS
211 #endif  // OHOS_ABILITY_RUNTIME_SERVICE_EXTENSION_CONTEXT_H
212