1 /*
2  * Copyright (c) 2022-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_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 StartServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1) const;
127 
128     ErrCode StartUIServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1) const;
129 
130     ErrCode StopServiceExtensionAbility(const AAFwk::Want& want, int32_t accountId = -1) const;
131 
132     /**
133      * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
134      *
135      * @param want Indicates the want containing information about the ability to connect.
136      *
137      * @param accountId caller user.
138      *
139      * @param conn Indicates the callback object when the target ability is connected.
140      *
141      * @return Returns zero on success, others on failure.
142      */
143     ErrCode ConnectAbilityWithAccount(
144         const AAFwk::Want &want, int accountId, const sptr<AbilityConnectCallback> &connectCallback) const;
145 
146     /**
147      * @brief Disconnects the current ability from an ability.
148      *
149      * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection
150      * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities.
151      *
152      * @return errCode ERR_OK on success, others on failure.
153      */
154     ErrCode DisconnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback,
155         int32_t accountId = -1) const;
156 
157     /**
158      * @brief Destroys the current ability.
159      *
160      * @return errCode ERR_OK on success, others on failure.
161      */
162     ErrCode TerminateAbility();
163 
164     ErrCode RequestModalUIExtension(const Want &want);
165 
166     ErrCode PreStartMission(const std::string& bundleName, const std::string& moduleName,
167         const std::string& abilityName, const std::string& startTime);
168 
169     using SelfType = ServiceExtensionContext;
170     static const size_t CONTEXT_TYPE_ID;
171 
172 protected:
IsContext(size_t contextTypeId)173     bool IsContext(size_t contextTypeId) override
174     {
175         return contextTypeId == CONTEXT_TYPE_ID || ExtensionContext::IsContext(contextTypeId);
176     }
177 
178 private:
179     static int ILLEGAL_REQUEST_CODE;
180     std::shared_ptr<LocalCallContainer> localCallContainer_ = nullptr;
181 
182     /**
183      * @brief Get Current Ability Type
184      *
185      * @return Current Ability Type
186      */
187     OHOS::AppExecFwk::AbilityType GetAbilityInfoType() const;
188 };
189 }  // namespace AbilityRuntime
190 }  // namespace OHOS
191 #endif  // OHOS_ABILITY_RUNTIME_SERVICE_EXTENSION_CONTEXT_H
192