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 "privacy_manager_proxy.h"
17
18 #include "accesstoken_log.h"
19 #include "privacy_error.h"
20
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
26 LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PrivacyManagerProxy"
27 };
28 }
29
PrivacyManagerProxy(const sptr<IRemoteObject> & impl)30 PrivacyManagerProxy::PrivacyManagerProxy(const sptr<IRemoteObject>& impl)
31 : IRemoteProxy<IPrivacyManager>(impl) {
32 }
33
~PrivacyManagerProxy()34 PrivacyManagerProxy::~PrivacyManagerProxy()
35 {}
36
AddPermissionUsedRecord(AccessTokenID tokenID,const std::string & permissionName,int32_t successCount,int32_t failCount)37 int32_t PrivacyManagerProxy::AddPermissionUsedRecord(AccessTokenID tokenID, const std::string& permissionName,
38 int32_t successCount, int32_t failCount)
39 {
40 MessageParcel data;
41 data.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
42 if (!data.WriteUint32(tokenID)) {
43 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(%{public}d)", tokenID);
44 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
45 }
46 if (!data.WriteString(permissionName)) {
47 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteString(permissionName)");
48 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
49 }
50 if (!data.WriteInt32(successCount)) {
51 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(successCount)");
52 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
53 }
54 if (!data.WriteInt32(failCount)) {
55 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(failCount)");
56 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
57 }
58
59 MessageParcel reply;
60 if (!SendRequest(IPrivacyManager::InterfaceCode::ADD_PERMISSION_USED_RECORD, data, reply)) {
61 return PrivacyError::ERR_SERVICE_ABNORMAL;
62 }
63
64 return reply.ReadInt32();
65 }
66
StartUsingPermission(AccessTokenID tokenID,const std::string & permissionName)67 int32_t PrivacyManagerProxy::StartUsingPermission(AccessTokenID tokenID, const std::string& permissionName)
68 {
69 MessageParcel data;
70 data.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
71 if (!data.WriteUint32(tokenID)) {
72 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(%{public}d)", tokenID);
73 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
74 }
75 if (!data.WriteString(permissionName)) {
76 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteString(permissionName)");
77 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
78 }
79
80 MessageParcel reply;
81 if (!SendRequest(IPrivacyManager::InterfaceCode::START_USING_PERMISSION, data, reply)) {
82 return PrivacyError::ERR_SERVICE_ABNORMAL;
83 }
84
85 return reply.ReadInt32();
86 }
87
StartUsingPermission(AccessTokenID tokenID,const std::string & permissionName,const sptr<IRemoteObject> & callback)88 int32_t PrivacyManagerProxy::StartUsingPermission(AccessTokenID tokenID, const std::string& permissionName,
89 const sptr<IRemoteObject>& callback)
90 {
91 MessageParcel data;
92 data.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
93 if (!data.WriteUint32(tokenID)) {
94 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(%{public}d)", tokenID);
95 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
96 }
97 if (!data.WriteString(permissionName)) {
98 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteString(permissionName)");
99 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
100 }
101 if (!data.WriteRemoteObject(callback)) {
102 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write remote object.");
103 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
104 }
105
106 MessageParcel reply;
107 if (!SendRequest(IPrivacyManager::InterfaceCode::START_USING_PERMISSION_CALLBACK, data, reply)) {
108 ACCESSTOKEN_LOG_ERROR(LABEL, "SendRequest fail");
109 return PrivacyError::ERR_SERVICE_ABNORMAL;
110 }
111
112 int32_t ret = reply.ReadInt32();
113 ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", ret);
114 return ret;
115 }
116
StopUsingPermission(AccessTokenID tokenID,const std::string & permissionName)117 int32_t PrivacyManagerProxy::StopUsingPermission(AccessTokenID tokenID, const std::string& permissionName)
118 {
119 MessageParcel data;
120 data.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
121 if (!data.WriteUint32(tokenID)) {
122 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(%{public}d)", tokenID);
123 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
124 }
125 if (!data.WriteString(permissionName)) {
126 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteString(permissionName)");
127 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
128 }
129
130 MessageParcel reply;
131 if (!SendRequest(IPrivacyManager::InterfaceCode::STOP_USING_PERMISSION, data, reply)) {
132 return PrivacyError::ERR_SERVICE_ABNORMAL;
133 }
134
135 return reply.ReadInt32();
136 }
137
RemovePermissionUsedRecords(AccessTokenID tokenID,const std::string & deviceID)138 int32_t PrivacyManagerProxy::RemovePermissionUsedRecords(AccessTokenID tokenID, const std::string& deviceID)
139 {
140 MessageParcel data;
141 data.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
142 if (!data.WriteUint32(tokenID)) {
143 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(%{public}d)", tokenID);
144 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
145 }
146 if (!data.WriteString(deviceID)) {
147 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteString(deviceID)");
148 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
149 }
150
151 MessageParcel reply;
152 if (!SendRequest(IPrivacyManager::InterfaceCode::DELETE_PERMISSION_USED_RECORDS, data, reply)) {
153 return PrivacyError::ERR_SERVICE_ABNORMAL;
154 }
155
156 return reply.ReadInt32();
157 }
158
GetPermissionUsedRecords(const PermissionUsedRequestParcel & request,PermissionUsedResultParcel & result)159 int32_t PrivacyManagerProxy::GetPermissionUsedRecords(const PermissionUsedRequestParcel& request,
160 PermissionUsedResultParcel& result)
161 {
162 MessageParcel data;
163 data.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
164 if (!data.WriteParcelable(&request)) {
165 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteParcelable(request)");
166 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
167 }
168
169 MessageParcel reply;
170 if (!SendRequest(IPrivacyManager::InterfaceCode::GET_PERMISSION_USED_RECORDS, data, reply)) {
171 return PrivacyError::ERR_SERVICE_ABNORMAL;
172 }
173
174 int32_t ret = reply.ReadInt32();
175 if (ret != RET_SUCCESS) {
176 return ret;
177 }
178 sptr<PermissionUsedResultParcel> resultSptr = reply.ReadParcelable<PermissionUsedResultParcel>();
179 if (resultSptr == nullptr) {
180 ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable fail");
181 return PrivacyError::ERR_READ_PARCEL_FAILED;
182 }
183 result = *resultSptr;
184 return ret;
185 }
186
GetPermissionUsedRecords(const PermissionUsedRequestParcel & request,const sptr<OnPermissionUsedRecordCallback> & callback)187 int32_t PrivacyManagerProxy::GetPermissionUsedRecords(const PermissionUsedRequestParcel& request,
188 const sptr<OnPermissionUsedRecordCallback>& callback)
189 {
190 MessageParcel data;
191 data.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
192 if (!data.WriteParcelable(&request)) {
193 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteParcelable(request)");
194 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
195 }
196 if (!data.WriteRemoteObject(callback->AsObject())) {
197 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteRemoteObject(callback)");
198 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
199 }
200
201 MessageParcel reply;
202 if (!SendRequest(IPrivacyManager::InterfaceCode::GET_PERMISSION_USED_RECORDS_ASYNC, data, reply)) {
203 return PrivacyError::ERR_SERVICE_ABNORMAL;
204 }
205
206 return reply.ReadInt32();
207 }
208
RegisterPermActiveStatusCallback(std::vector<std::string> & permList,const sptr<IRemoteObject> & callback)209 int32_t PrivacyManagerProxy::RegisterPermActiveStatusCallback(
210 std::vector<std::string>& permList, const sptr<IRemoteObject>& callback)
211 {
212 MessageParcel data;
213 if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) {
214 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken.");
215 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
216 }
217
218 uint32_t listSize = permList.size();
219 if (!data.WriteUint32(listSize)) {
220 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write listSize");
221 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
222 }
223 for (uint32_t i = 0; i < listSize; i++) {
224 if (!data.WriteString(permList[i])) {
225 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permList[%{public}d], %{public}s", i, permList[i].c_str());
226 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
227 }
228 }
229
230 if (!data.WriteRemoteObject(callback)) {
231 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write remote object.");
232 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
233 }
234 MessageParcel reply;
235 if (!SendRequest(IPrivacyManager::InterfaceCode::REGISTER_PERM_ACTIVE_STATUS_CHANGE_CALLBACK, data, reply)) {
236 return PrivacyError::ERR_SERVICE_ABNORMAL;
237 }
238
239 return reply.ReadInt32();
240 }
241
UnRegisterPermActiveStatusCallback(const sptr<IRemoteObject> & callback)242 int32_t PrivacyManagerProxy::UnRegisterPermActiveStatusCallback(const sptr<IRemoteObject>& callback)
243 {
244 MessageParcel data;
245 if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) {
246 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken.");
247 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
248 }
249 if (!data.WriteRemoteObject(callback)) {
250 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write remote object.");
251 return PrivacyError::ERR_WRITE_PARCEL_FAILED;
252 }
253 MessageParcel reply;
254 if (!SendRequest(IPrivacyManager::InterfaceCode::UNREGISTER_PERM_ACTIVE_STATUS_CHANGE_CALLBACK, data, reply)) {
255 return PrivacyError::ERR_SERVICE_ABNORMAL;
256 }
257
258 return reply.ReadInt32();
259 }
260
IsAllowedUsingPermission(AccessTokenID tokenID,const std::string & permissionName)261 bool PrivacyManagerProxy::IsAllowedUsingPermission(AccessTokenID tokenID, const std::string& permissionName)
262 {
263 MessageParcel data;
264 MessageParcel reply;
265 if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) {
266 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken.");
267 return false;
268 }
269 if (!data.WriteUint32(tokenID)) {
270 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(%{public}d)", tokenID);
271 return false;
272 }
273 if (!data.WriteString(permissionName)) {
274 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteString(%{public}s)", permissionName.c_str());
275 return false;
276 }
277 if (!SendRequest(IPrivacyManager::InterfaceCode::IS_ALLOWED_USING_PERMISSION, data, reply)) {
278 return PrivacyError::ERR_SERVICE_ABNORMAL;
279 }
280
281 return reply.ReadBool();
282 }
283
SendRequest(IPrivacyManager::InterfaceCode code,MessageParcel & data,MessageParcel & reply)284 bool PrivacyManagerProxy::SendRequest(
285 IPrivacyManager::InterfaceCode code, MessageParcel& data, MessageParcel& reply)
286 {
287 MessageOption option(MessageOption::TF_SYNC);
288 sptr<IRemoteObject> remote = Remote();
289 if (remote == nullptr) {
290 ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
291 return false;
292 }
293
294 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
295 if (result != NO_ERROR) {
296 ACCESSTOKEN_LOG_ERROR(LABEL, "SendRequest fail, result: %{public}d", result);
297 return false;
298 }
299 return true;
300 }
301 } // namespace AccessToken
302 } // namespace Security
303 } // namespace OHOS
304