• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 {
UriPermissionManagerProxy(const sptr<IRemoteObject> & impl)24 UriPermissionManagerProxy::UriPermissionManagerProxy(const sptr<IRemoteObject> &impl)
25     : IRemoteProxy<IUriPermissionManager>(impl) {}
26 
GrantUriPermission(const Uri & uri,unsigned int flag,const std::string targetBundleName,int autoremove,int32_t appIndex)27 int UriPermissionManagerProxy::GrantUriPermission(const Uri &uri, unsigned int flag,
28     const std::string targetBundleName, int autoremove, int32_t appIndex)
29 {
30     HILOG_DEBUG("UriPermissionManagerProxy::GrantUriPermission is called.");
31     MessageParcel data;
32     if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
33         HILOG_ERROR("Write interface token failed.");
34         return INNER_ERR;
35     }
36     if (!data.WriteParcelable(&uri)) {
37         HILOG_ERROR("Write uri failed.");
38         return INNER_ERR;
39     }
40     if (!data.WriteInt32(flag)) {
41         HILOG_ERROR("Write flag failed.");
42         return INNER_ERR;
43     }
44     if (!data.WriteString(targetBundleName)) {
45         HILOG_ERROR("Write targetBundleName failed.");
46         return INNER_ERR;
47     }
48     if (!data.WriteInt32(autoremove)) {
49         HILOG_ERROR("Write autoremove failed.");
50         return INNER_ERR;
51     }
52     if (!data.WriteInt32(appIndex)) {
53         HILOG_ERROR("Write appIndex failed.");
54         return INNER_ERR;
55     }
56     MessageParcel reply;
57     MessageOption option;
58     int error = Remote()->SendRequest(UriPermMgrCmd::ON_GRANT_URI_PERMISSION, data, reply, option);
59     if (error != ERR_OK) {
60         HILOG_ERROR("SendRequest fial, error: %{public}d", error);
61         return INNER_ERR;
62     }
63     return reply.ReadInt32();
64 }
65 
RevokeUriPermission(const Security::AccessToken::AccessTokenID tokenId)66 void UriPermissionManagerProxy::RevokeUriPermission(const Security::AccessToken::AccessTokenID tokenId)
67 {
68     HILOG_DEBUG("UriPermissionManagerProxy::RevokeUriPermission is called.");
69     MessageParcel data;
70     if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
71         HILOG_ERROR("Write interface token failed.");
72         return;
73     }
74     if (!data.WriteInt32(tokenId)) {
75         HILOG_ERROR("Write AccessTokenID failed.");
76         return;
77     }
78     MessageParcel reply;
79     MessageOption option;
80     int error = Remote()->SendRequest(UriPermMgrCmd::ON_REVOKE_URI_PERMISSION, data, reply, option);
81     if (error != ERR_OK) {
82         HILOG_ERROR("SendRequest fail, error: %{public}d", error);
83     }
84 }
85 
RevokeAllUriPermissions(const Security::AccessToken::AccessTokenID tokenId)86 int UriPermissionManagerProxy::RevokeAllUriPermissions(const Security::AccessToken::AccessTokenID tokenId)
87 {
88     HILOG_DEBUG("UriPermissionManagerProxy::RevokeAllUriPermissions is called.");
89     MessageParcel data;
90     if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
91         HILOG_ERROR("Write interface token failed.");
92         return INNER_ERR;
93     }
94     if (!data.WriteInt32(tokenId)) {
95         HILOG_ERROR("Write AccessTokenID failed.");
96         return INNER_ERR;
97     }
98     MessageParcel reply;
99     MessageOption option;
100     int error = Remote()->SendRequest(UriPermMgrCmd::ON_REVOKE_ALL_URI_PERMISSION, data, reply, option);
101     if (error != ERR_OK) {
102         HILOG_ERROR("SendRequest fail, error: %{public}d", error);
103         return INNER_ERR;
104     }
105     return ERR_OK;
106 }
107 
RevokeUriPermissionManually(const Uri & uri,const std::string bundleName)108 int UriPermissionManagerProxy::RevokeUriPermissionManually(const Uri &uri, const std::string bundleName)
109 {
110     HILOG_DEBUG("UriPermissionManagerProxy::RevokeUriPermissionManually is called.");
111     MessageParcel data;
112     if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
113         HILOG_ERROR("Write interface token failed.");
114         return INNER_ERR;
115     }
116     if (!data.WriteParcelable(&uri)) {
117         HILOG_ERROR("Write uri failed.");
118         return INNER_ERR;
119     }
120     if (!data.WriteString(bundleName)) {
121         HILOG_ERROR("Write bundleName failed.");
122         return INNER_ERR;
123     }
124     MessageParcel reply;
125     MessageOption option;
126     int error = Remote()->SendRequest(UriPermMgrCmd::ON_REVOKE_URI_PERMISSION_MANUALLY, data, reply, option);
127     if (error != ERR_OK) {
128         HILOG_ERROR("SendRequest fail, error: %{public}d", error);
129         return INNER_ERR;
130     }
131     return reply.ReadInt32();
132 }
133 
CheckPersistableUriPermissionProxy(const Uri & uri,uint32_t flag,uint32_t tokenId)134 bool UriPermissionManagerProxy::CheckPersistableUriPermissionProxy(const Uri& uri, uint32_t flag, uint32_t tokenId)
135 {
136     HILOG_DEBUG("UriPermissionManagerProxy::CheckPersistableUriPermissionProxy is called.");
137     MessageParcel data;
138     if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
139         HILOG_ERROR("Write interface token failed.");
140         return false;
141     }
142     if (!data.WriteParcelable(&uri)) {
143         HILOG_ERROR("Write uri failed.");
144         return false;
145     }
146     if (!data.WriteInt32(flag)) {
147         HILOG_ERROR("Write flag failed.");
148         return false;
149     }
150     if (!data.WriteInt32(tokenId)) {
151         HILOG_ERROR("Write tokenId failed.");
152         return false;
153     }
154     MessageParcel reply;
155     MessageOption option;
156     int error = Remote()->SendRequest(UriPermMgrCmd::ON_CHECK_PERSISTABLE_URIPERMISSION_PROXY, data, reply, option);
157     if (error != ERR_OK) {
158         HILOG_ERROR("SendRequest fail, error: %{public}d", error);
159         return false;
160     }
161     return reply.ReadBool();
162 }
163 
VerifyUriPermission(const Uri & uri,uint32_t flag,uint32_t tokenId)164 bool UriPermissionManagerProxy::VerifyUriPermission(const Uri& uri, uint32_t flag, uint32_t tokenId)
165 {
166     HILOG_DEBUG("UriPermissionManagerProxy::VerifyUriPermission is called.");
167     MessageParcel data;
168     if (!data.WriteInterfaceToken(IUriPermissionManager::GetDescriptor())) {
169         HILOG_ERROR("Write interface token failed.");
170         return false;
171     }
172     if (!data.WriteParcelable(&uri)) {
173         HILOG_ERROR("Write uri failed.");
174         return false;
175     }
176     if (!data.WriteInt32(flag)) {
177         HILOG_ERROR("Write flag failed.");
178         return false;
179     }
180     if (!data.WriteInt32(tokenId)) {
181         HILOG_ERROR("Write tokenId failed.");
182         return false;
183     }
184     MessageParcel reply;
185     MessageOption option;
186     int error = Remote()->SendRequest(UriPermMgrCmd::ON_VERIFY_URI_PERMISSION, data, reply, option);
187     if (error != ERR_OK) {
188         HILOG_ERROR("SendRequest fail, error: %{public}d", error);
189         return false;
190     }
191     return reply.ReadBool();
192 }
193 }  // namespace AAFwk
194 }  // namespace OHOS
195