1 /*
2  * Copyright (c) 2021-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_NATIVE_ABILITY_CONTEXT_H
17 #define OHOS_ABILITY_RUNTIME_NATIVE_ABILITY_CONTEXT_H
18 
19 #include <map>
20 
21 #include "context_container.h"
22 #include "data_ability_helper.h"
23 
24 namespace OHOS {
25 namespace DataShare {
26 class DataShareHelper;
27 }
28 namespace AppExecFwk {
29 class AbilityContext : public ContextContainer {
30 public:
31     AbilityContext() = default;
32     virtual ~AbilityContext() = default;
33 
34     /**
35      * @brief Starts a new ability.
36      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
37      * to start a specific ability. The system locates the target ability from installed abilities based on the value
38      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
39      *
40      * @param want Indicates the Want containing information about the target ability to start.
41      *
42      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
43      * template is started. You can define the request code to identify the results returned by abilities. The value
44      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
45      * template.
46      *
47      * @return errCode ERR_OK on success, others on failure.
48      */
49     using ContextContainer::StartAbility;
50     ErrCode StartAbility(const AAFwk::Want &want, int requestCode) override;
51 
52     /**
53      * @brief Starts a new ability with special ability start setting.
54      *
55      * @param want Indicates the Want containing information about the target ability to start.
56      * @param requestCode Indicates the request code returned after the ability is started. You can define the request
57      * code to identify the results returned by abilities. The value ranges from 0 to 65535.
58      * @param abilityStartSetting Indicates the special start setting used in starting ability.
59      *
60      * @return errCode ERR_OK on success, others on failure.
61      */
62     ErrCode StartAbility(const Want &want, int requestCode, const AbilityStartSetting &abilityStartSetting) override;
63 
64     ErrCode AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> &observer);
65 
66     /**
67      * @brief Destroys the current ability.
68      *
69      * @return errCode ERR_OK on success, others on failure.
70      */
71     ErrCode TerminateAbility() override;
72 
73     /**
74      * @brief Obtains the bundle name of the ability that called the current ability.
75      * You can use the obtained bundle name to check whether the calling ability is allowed to receive the data you will
76      * send. If you did not use Ability.startAbilityForResult(ohos.aafwk.content.Want, int,
77      * ohos.aafwk.ability.startsetting.AbilityStartSetting) to start the calling ability, null is returned.
78      *
79      * @return Returns the bundle name of the calling ability; returns null if no calling ability is available.
80      */
81     std::string GetCallingBundle() override;
82 
83     /**
84      * @brief Obtains the ohos.bundle.ElementName object of the current ability.
85      *
86      * @return Returns the ohos.bundle.ElementName object of the current ability.
87      */
88     std::shared_ptr<ElementName> GetElementName();
89 
90     /**
91      * @brief Obtains the ElementName of the ability that called the current ability.
92      *
93      * @return Returns the ElementName of the calling ability; returns null if no calling ability is available.
94      */
95     std::shared_ptr<ElementName> GetCallingAbility();
96 
97     /**
98      * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
99      *
100      * @param want Indicates the want containing information about the ability to connect
101      *
102      * @param conn Indicates the callback object when the target ability is connected.
103      *
104      * @return True means success and false means failure
105      */
106     bool ConnectAbility(const Want &want, const sptr<AAFwk::IAbilityConnection> &conn) override;
107 
108     /**
109      * @brief Disconnects the current ability from an ability
110      *
111      * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection
112      *              is set up. The IAbilityConnection object uniquely identifies a connection between two abilities.
113      *
114      * @return errCode ERR_OK on success, others on failure.
115      */
116     ErrCode DisconnectAbility(const sptr<AAFwk::IAbilityConnection> &conn) override;
117 
118     /**
119      * @brief Destroys another ability that uses the AbilityInfo.AbilityType.SERVICE template.
120      * The current ability using either the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE
121      * template can call this method to destroy another ability that uses the AbilityInfo.AbilityType.SERVICE
122      * template. The current ability itself can be destroyed by calling the terminateAbility() method.
123      *
124      * @param want Indicates the Want containing information about the ability to destroy.
125      *
126      * @return Returns true if the ability is destroyed successfully; returns false otherwise.
127      */
128     virtual bool StopAbility(const AAFwk::Want &want) override;
129 
130     /**
131      * @brief Obtains a resource manager.
132      *
133      * @return Returns a ResourceManager object.
134      */
135     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
136 
137     /**
138      * @brief Query whether the application of the specified PID and UID has been granted a certain permission
139      *
140      * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
141      * @param pid Process id
142      * @param uid
143      * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
144      * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
145      */
146     virtual int VerifyPermission(const std::string &permission, int pid, int uid) override;
147 
148     /**
149      * @brief Requests certain permissions from the system.
150      * This method is called for permission request. This is an asynchronous method. When it is executed,
151      * the task will be called back.
152      *
153      * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
154      * @param permissionsState Indicates the list of permissions' state to be requested. This parameter cannot be null.
155      * @param task The callback or promise fo js interface.
156      */
157     virtual void RequestPermissionsFromUser(std::vector<std::string> &permissions, std::vector<int> &permissionsState,
158         PermissionRequestTask &&task) override;
159 
160     /**
161      * @brief Set deviceId/bundleName/abilityName of the calling ability
162      *
163      * @param deviceId deviceId of the calling ability
164      *
165      * @param bundleName bundleName of the calling ability
166      *
167      * @param abilityName abilityName of the calling ability
168      */
169     void SetCallingContext(const std::string &deviceId, const std::string &bundleName,
170         const std::string &abilityName, const std::string &moduleName = "");
171 
172     /**
173      * @brief Starts multiple abilities.
174      *
175      * @param wants Indicates the Want containing information array about the target ability to start.
176      */
177     void StartAbilities(const std::vector<AAFwk::Want> &wants) override;
178 
SetAbilityRecordId(int32_t abilityRecordId)179     void SetAbilityRecordId(int32_t abilityRecordId)
180     {
181         abilityRecordId_ = abilityRecordId;
182     }
GetAbilityRecordId()183     int32_t GetAbilityRecordId() const
184     {
185         return abilityRecordId_;
186     }
187 
188     friend DataAbilityHelper;
189     friend OHOS::DataShare::DataShareHelper;
190     static int ABILITY_CONTEXT_DEFAULT_REQUEST_CODE;
191 
192 protected:
193     sptr<IRemoteObject> GetToken() override;
194     sptr<IRemoteObject> GetSessionToken();
195 
196     sptr<IRemoteObject> token_;
197     AAFwk::Want resultWant_;
198     int resultCode_ = -1;
199     std::string callingDeviceId_;
200     std::string callingBundleName_;
201     std::string callingAbilityName_;
202     std::string callingModuleName_;
203     std::map<sptr<AAFwk::IAbilityConnection>, sptr<IRemoteObject>> abilityConnectionMap_;
204     std::mutex sessionTokenMutex_;
205     sptr<IRemoteObject> sessionToken_;
206     int32_t abilityRecordId_ = 0;
207 
208 private:
209     /**
210      * @brief Get Current Ability Type
211      *
212      * @return Current Ability Type
213      */
214     AppExecFwk::AbilityType GetAbilityInfoType();
215     void GetPermissionDes(const std::string &permissionName, std::string &des);
216 };
217 }  // namespace AppExecFwk
218 }  // namespace OHOS
219 #endif  // OHOS_ABILITY_RUNTIME_NATIVE_ABILITY_CONTEXT_H
220