1 /*
2 * Copyright (c) 2022-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 #include "uri_permission_manager_proxy.h"
17
18 #include "ability_manager_errors.h"
19 #include "hilog_wrapper.h"
20 #include "parcel.h"
21
22 namespace OHOS {
23 namespace AAFwk {
24 namespace {
25 const int MAX_URI_COUNT = 500;
26 }
UriPermissionManagerProxy(const sptr<IRemoteObject> & impl)27 UriPermissionManagerProxy::UriPermissionManagerProxy(const sptr<IRemoteObject> &impl)
28 : IRemoteProxy<IUriPermissionManager>(impl) {}
29
GrantUriPermission(const Uri & uri,unsigned int flag,const std::string targetBundleName,int32_t appIndex,uint32_t initiatorTokenId)30 int UriPermissionManagerProxy::GrantUriPermission(const Uri &uri, unsigned int flag,
31 const std::string targetBundleName, int32_t appIndex, uint32_t initiatorTokenId)
32 {
33 HILOG_DEBUG("UriPermissionManagerProxy::GrantUriPermission is called.");
34 MessageParcel data;
35 if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
36 HILOG_ERROR("Write interface token failed.");
37 return INNER_ERR;
38 }
39 if (!data.WriteParcelable(&uri)) {
40 HILOG_ERROR("Write uri failed.");
41 return INNER_ERR;
42 }
43 if (!data.WriteInt32(flag)) {
44 HILOG_ERROR("Write flag failed.");
45 return INNER_ERR;
46 }
47 if (!data.WriteString(targetBundleName)) {
48 HILOG_ERROR("Write targetBundleName failed.");
49 return INNER_ERR;
50 }
51 if (!data.WriteInt32(appIndex)) {
52 HILOG_ERROR("Write appIndex failed.");
53 return INNER_ERR;
54 }
55 if (!data.WriteUint32(initiatorTokenId)) {
56 HILOG_ERROR("Write initiatorTokenId failed.");
57 return INNER_ERR;
58 }
59 MessageParcel reply;
60 MessageOption option;
61 int error = SendTransactCmd(UriPermMgrCmd::ON_GRANT_URI_PERMISSION, data, reply, option);
62 if (error != ERR_OK) {
63 HILOG_ERROR("SendRequest fial, error: %{public}d", error);
64 return INNER_ERR;
65 }
66 return reply.ReadInt32();
67 }
68
GrantUriPermission(const std::vector<Uri> & uriVec,unsigned int flag,const std::string targetBundleName,int32_t appIndex,uint32_t initiatorTokenId)69 int UriPermissionManagerProxy::GrantUriPermission(const std::vector<Uri> &uriVec, unsigned int flag,
70 const std::string targetBundleName, int32_t appIndex, uint32_t initiatorTokenId)
71 {
72 HILOG_DEBUG("UriPermissionManagerProxy::GrantUriPermission is called.");
73 MessageParcel data;
74 if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
75 HILOG_ERROR("Write interface token failed.");
76 return INNER_ERR;
77 }
78 if (!data.WriteUint32(uriVec.size())) {
79 HILOG_ERROR("Write size of uriVec failed.");
80 return INNER_ERR;
81 }
82 for (const auto &uri : uriVec) {
83 if (!data.WriteParcelable(&uri)) {
84 HILOG_ERROR("Write uri failed.");
85 return INNER_ERR;
86 }
87 }
88 if (!data.WriteInt32(flag)) {
89 HILOG_ERROR("Write flag failed.");
90 return INNER_ERR;
91 }
92 if (!data.WriteString(targetBundleName)) {
93 HILOG_ERROR("Write targetBundleName failed.");
94 return INNER_ERR;
95 }
96 if (!data.WriteInt32(appIndex)) {
97 HILOG_ERROR("Write appIndex failed.");
98 return INNER_ERR;
99 }
100 if (!data.WriteUint32(initiatorTokenId)) {
101 HILOG_ERROR("Write initiatorTokenId failed.");
102 return INNER_ERR;
103 }
104 MessageParcel reply;
105 MessageOption option;
106 int error = SendTransactCmd(UriPermMgrCmd::ON_BATCH_GRANT_URI_PERMISSION, data, reply, option);
107 if (error != ERR_OK) {
108 HILOG_ERROR("SendRequest fial, error: %{public}d", error);
109 return INNER_ERR;
110 }
111 return reply.ReadInt32();
112 }
113
GrantUriPermissionFor2In1(const std::vector<Uri> & uriVec,unsigned int flag,const std::string & targetBundleName,int32_t appIndex,bool isSystemAppCall)114 int UriPermissionManagerProxy::GrantUriPermissionFor2In1(const std::vector<Uri> &uriVec, unsigned int flag,
115 const std::string &targetBundleName, int32_t appIndex, bool isSystemAppCall)
116 {
117 HILOG_DEBUG("Called.");
118 MessageParcel data;
119 if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
120 HILOG_ERROR("Write interface token failed.");
121 return INNER_ERR;
122 }
123 if (uriVec.size() > MAX_URI_COUNT) {
124 HILOG_ERROR("Exceeded maximum uri count.");
125 return INNER_ERR;
126 }
127 if (!data.WriteUint32(uriVec.size())) {
128 HILOG_ERROR("Write size of uriVec failed.");
129 return INNER_ERR;
130 }
131 for (const auto &uri : uriVec) {
132 if (!data.WriteParcelable(&uri)) {
133 HILOG_ERROR("Write uri failed.");
134 return INNER_ERR;
135 }
136 }
137 if (!data.WriteInt32(flag)) {
138 HILOG_ERROR("Write flag failed.");
139 return INNER_ERR;
140 }
141 if (!data.WriteString(targetBundleName)) {
142 HILOG_ERROR("Write targetBundleName failed.");
143 return INNER_ERR;
144 }
145 if (!data.WriteInt32(appIndex)) {
146 HILOG_ERROR("Write appIndex failed.");
147 return INNER_ERR;
148 }
149 if (!data.WriteBool(isSystemAppCall)) {
150 HILOG_ERROR("Write isSystemAppCall failed.");
151 return INNER_ERR;
152 }
153 MessageParcel reply;
154 MessageOption option;
155 int error = SendTransactCmd(UriPermMgrCmd::ON_BATCH_GRANT_URI_PERMISSION_FOR_2_IN_1, data, reply, option);
156 if (error != ERR_OK) {
157 HILOG_ERROR("SendRequest fial, error: %{public}d", error);
158 return INNER_ERR;
159 }
160 return reply.ReadInt32();
161 }
162
RevokeUriPermission(const Security::AccessToken::AccessTokenID tokenId)163 void UriPermissionManagerProxy::RevokeUriPermission(const Security::AccessToken::AccessTokenID tokenId)
164 {
165 HILOG_DEBUG("UriPermissionManagerProxy::RevokeUriPermission is called.");
166 MessageParcel data;
167 if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
168 HILOG_ERROR("Write interface token failed.");
169 return;
170 }
171 if (!data.WriteInt32(tokenId)) {
172 HILOG_ERROR("Write AccessTokenID failed.");
173 return;
174 }
175 MessageParcel reply;
176 MessageOption option;
177 int error = SendTransactCmd(UriPermMgrCmd::ON_REVOKE_URI_PERMISSION, data, reply, option);
178 if (error != ERR_OK) {
179 HILOG_ERROR("SendRequest fail, error: %{public}d", error);
180 }
181 }
182
RevokeAllUriPermissions(const Security::AccessToken::AccessTokenID tokenId)183 int UriPermissionManagerProxy::RevokeAllUriPermissions(const Security::AccessToken::AccessTokenID tokenId)
184 {
185 HILOG_DEBUG("UriPermissionManagerProxy::RevokeAllUriPermissions is called.");
186 MessageParcel data;
187 if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
188 HILOG_ERROR("Write interface token failed.");
189 return INNER_ERR;
190 }
191 if (!data.WriteInt32(tokenId)) {
192 HILOG_ERROR("Write AccessTokenID failed.");
193 return INNER_ERR;
194 }
195 MessageParcel reply;
196 MessageOption option;
197 int error = SendTransactCmd(UriPermMgrCmd::ON_REVOKE_ALL_URI_PERMISSION, data, reply, option);
198 if (error != ERR_OK) {
199 HILOG_ERROR("SendRequest fail, error: %{public}d", error);
200 return INNER_ERR;
201 }
202 return ERR_OK;
203 }
204
RevokeUriPermissionManually(const Uri & uri,const std::string bundleName)205 int UriPermissionManagerProxy::RevokeUriPermissionManually(const Uri &uri, const std::string bundleName)
206 {
207 HILOG_DEBUG("UriPermissionManagerProxy::RevokeUriPermissionManually is called.");
208 MessageParcel data;
209 if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
210 HILOG_ERROR("Write interface token failed.");
211 return INNER_ERR;
212 }
213 if (!data.WriteParcelable(&uri)) {
214 HILOG_ERROR("Write uri failed.");
215 return INNER_ERR;
216 }
217 if (!data.WriteString(bundleName)) {
218 HILOG_ERROR("Write bundleName failed.");
219 return INNER_ERR;
220 }
221 MessageParcel reply;
222 MessageOption option;
223 int error = SendTransactCmd(UriPermMgrCmd::ON_REVOKE_URI_PERMISSION_MANUALLY, data, reply, option);
224 if (error != ERR_OK) {
225 HILOG_ERROR("SendRequest fail, error: %{public}d", error);
226 return INNER_ERR;
227 }
228 return reply.ReadInt32();
229 }
230
CheckPersistableUriPermissionProxy(const Uri & uri,uint32_t flag,uint32_t tokenId)231 bool UriPermissionManagerProxy::CheckPersistableUriPermissionProxy(const Uri& uri, uint32_t flag, uint32_t tokenId)
232 {
233 HILOG_DEBUG("UriPermissionManagerProxy::CheckPersistableUriPermissionProxy is called.");
234 MessageParcel data;
235 if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
236 HILOG_ERROR("Write interface token failed.");
237 return false;
238 }
239 if (!data.WriteParcelable(&uri)) {
240 HILOG_ERROR("Write uri failed.");
241 return false;
242 }
243 if (!data.WriteInt32(flag)) {
244 HILOG_ERROR("Write flag failed.");
245 return false;
246 }
247 if (!data.WriteInt32(tokenId)) {
248 HILOG_ERROR("Write tokenId failed.");
249 return false;
250 }
251 MessageParcel reply;
252 MessageOption option;
253 int error = SendTransactCmd(UriPermMgrCmd::ON_CHECK_PERSISTABLE_URIPERMISSION_PROXY, data, reply, option);
254 if (error != ERR_OK) {
255 HILOG_ERROR("SendRequest fail, error: %{public}d", error);
256 return false;
257 }
258 return reply.ReadBool();
259 }
260
VerifyUriPermission(const Uri & uri,uint32_t flag,uint32_t tokenId)261 bool UriPermissionManagerProxy::VerifyUriPermission(const Uri& uri, uint32_t flag, uint32_t tokenId)
262 {
263 HILOG_DEBUG("UriPermissionManagerProxy::VerifyUriPermission is called.");
264 MessageParcel data;
265 if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
266 HILOG_ERROR("Write interface token failed.");
267 return false;
268 }
269 if (!data.WriteParcelable(&uri)) {
270 HILOG_ERROR("Write uri failed.");
271 return false;
272 }
273 if (!data.WriteInt32(flag)) {
274 HILOG_ERROR("Write flag failed.");
275 return false;
276 }
277 if (!data.WriteInt32(tokenId)) {
278 HILOG_ERROR("Write tokenId failed.");
279 return false;
280 }
281 MessageParcel reply;
282 MessageOption option;
283 int error = SendTransactCmd(UriPermMgrCmd::ON_VERIFY_URI_PERMISSION, data, reply, option);
284 if (error != ERR_OK) {
285 HILOG_ERROR("SendRequest fail, error: %{public}d", error);
286 return false;
287 }
288 return reply.ReadBool();
289 }
290
IsAuthorizationUriAllowed(uint32_t fromTokenId)291 bool UriPermissionManagerProxy::IsAuthorizationUriAllowed(uint32_t fromTokenId)
292 {
293 HILOG_DEBUG("UriPermissionManagerProxy::IsAuthorizationUriAllowed is called.");
294 MessageParcel data;
295 if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
296 HILOG_ERROR("Write interface token failed.");
297 return false;
298 }
299 if (!data.WriteInt32(fromTokenId)) {
300 HILOG_ERROR("Write fromTokenId failed.");
301 return false;
302 }
303 MessageParcel reply;
304 MessageOption option;
305 int error = SendTransactCmd(UriPermMgrCmd::ON_IS_Authorization_URI_ALLOWED, data, reply, option);
306 if (error != ERR_OK) {
307 HILOG_ERROR("SendRequest fail, error: %{public}d", error);
308 return false;
309 }
310 return reply.ReadBool();
311 }
312
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)313 int32_t UriPermissionManagerProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
314 MessageParcel &reply, MessageOption &option)
315 {
316 sptr<IRemoteObject> remote = Remote();
317 if (remote == nullptr) {
318 HILOG_ERROR("remote object is nullptr.");
319 return ERR_NULL_OBJECT;
320 }
321
322 int32_t ret = remote->SendRequest(code, data, reply, option);
323 if (ret != NO_ERROR) {
324 HILOG_ERROR("SendRequest failed. code is %{public}d, ret is %{public}d.", code, ret);
325 return ret;
326 }
327 return NO_ERROR;
328 }
329 } // namespace AAFwk
330 } // namespace OHOS
331