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 "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 #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE
30 static const int MAX_SEC_COMP_ENHANCE_SIZE = 1000;
31 #endif
32 // if change this, copy value in privacy_kit_test.cpp should change together
33 static const uint32_t MAX_PERMISSION_USED_TYPE_SIZE = 2000;
34 }
35 
PrivacyManagerProxy(const sptr<IRemoteObject> & impl)36 PrivacyManagerProxy::PrivacyManagerProxy(const sptr<IRemoteObject>& impl)
37     : IRemoteProxy<IPrivacyManager>(impl) {
38 }
39 
~PrivacyManagerProxy()40 PrivacyManagerProxy::~PrivacyManagerProxy()
41 {}
42 
AddPermissionUsedRecord(const AddPermParamInfoParcel & infoParcel,bool asyncMode)43 int32_t PrivacyManagerProxy::AddPermissionUsedRecord(const AddPermParamInfoParcel& infoParcel, bool asyncMode)
44 {
45     MessageParcel addData;
46     addData.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
47     if (!addData.WriteParcelable(&infoParcel)) {
48         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteParcelable(infoParcel)");
49         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
50     }
51 
52     MessageParcel reply;
53     if (!SendRequest(PrivacyInterfaceCode::ADD_PERMISSION_USED_RECORD, addData, reply, asyncMode)) {
54         return PrivacyError::ERR_SERVICE_ABNORMAL;
55     }
56     int32_t result = reply.ReadInt32();
57     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server data = %{public}d", result);
58     return result;
59 }
60 
StartUsingPermission(AccessTokenID tokenID,int32_t pid,const std::string & permissionName)61 int32_t PrivacyManagerProxy::StartUsingPermission(
62     AccessTokenID tokenID, int32_t pid, const std::string& permissionName)
63 {
64     MessageParcel startData;
65     startData.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
66     if (!startData.WriteUint32(tokenID)) {
67         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
68         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
69     }
70     if (!startData.WriteInt32(pid)) {
71         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write pid");
72         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
73     }
74     if (!startData.WriteString(permissionName)) {
75         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permissionName");
76         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
77     }
78 
79     MessageParcel reply;
80     if (!SendRequest(PrivacyInterfaceCode::START_USING_PERMISSION, startData, reply)) {
81         return PrivacyError::ERR_SERVICE_ABNORMAL;
82     }
83 
84     int32_t result = reply.ReadInt32();
85     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server data = %{public}d", result);
86     return result;
87 }
88 
StartUsingPermission(AccessTokenID tokenID,int32_t pid,const std::string & permissionName,const sptr<IRemoteObject> & callback)89 int32_t PrivacyManagerProxy::StartUsingPermission(
90     AccessTokenID tokenID, int32_t pid, const std::string& permissionName,
91     const sptr<IRemoteObject>& callback)
92 {
93     MessageParcel data;
94     data.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
95     if (!data.WriteUint32(tokenID)) {
96         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
97         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
98     }
99     if (!data.WriteInt32(pid)) {
100         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write pid");
101         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
102     }
103     if (!data.WriteString(permissionName)) {
104         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permissionName");
105         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
106     }
107     if (!data.WriteRemoteObject(callback)) {
108         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write remote object.");
109         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
110     }
111 
112     MessageParcel reply;
113     if (!SendRequest(PrivacyInterfaceCode::START_USING_PERMISSION_CALLBACK, data, reply)) {
114         ACCESSTOKEN_LOG_ERROR(LABEL, "SendRequest fail");
115         return PrivacyError::ERR_SERVICE_ABNORMAL;
116     }
117 
118     int32_t result = reply.ReadInt32();
119     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server data = %{public}d", result);
120     return result;
121 }
122 
StopUsingPermission(AccessTokenID tokenID,int32_t pid,const std::string & permissionName)123 int32_t PrivacyManagerProxy::StopUsingPermission(
124     AccessTokenID tokenID, int32_t pid, const std::string& permissionName)
125 {
126     MessageParcel stopData;
127     stopData.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
128     if (!stopData.WriteUint32(tokenID)) {
129         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
130         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
131     }
132     if (!stopData.WriteInt32(pid)) {
133         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write pid");
134         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
135     }
136     if (!stopData.WriteString(permissionName)) {
137         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permissionName");
138         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
139     }
140 
141     MessageParcel reply;
142     if (!SendRequest(PrivacyInterfaceCode::STOP_USING_PERMISSION, stopData, reply)) {
143         return PrivacyError::ERR_SERVICE_ABNORMAL;
144     }
145 
146     int32_t result = reply.ReadInt32();
147     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server data = %{public}d", result);
148     return result;
149 }
150 
RemovePermissionUsedRecords(AccessTokenID tokenID,const std::string & deviceID)151 int32_t PrivacyManagerProxy::RemovePermissionUsedRecords(AccessTokenID tokenID, const std::string& deviceID)
152 {
153     MessageParcel data;
154     data.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
155     if (!data.WriteUint32(tokenID)) {
156         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(%{public}d)", tokenID);
157         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
158     }
159     if (!data.WriteString(deviceID)) {
160         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteString(deviceID)");
161         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
162     }
163 
164     MessageParcel reply;
165     if (!SendRequest(PrivacyInterfaceCode::DELETE_PERMISSION_USED_RECORDS, data, reply)) {
166         return PrivacyError::ERR_SERVICE_ABNORMAL;
167     }
168 
169     int32_t result = reply.ReadInt32();
170     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server data = %{public}d", result);
171     return result;
172 }
173 
GetPermissionUsedRecords(const PermissionUsedRequestParcel & request,PermissionUsedResultParcel & result)174 int32_t PrivacyManagerProxy::GetPermissionUsedRecords(const PermissionUsedRequestParcel& request,
175     PermissionUsedResultParcel& result)
176 {
177     MessageParcel data;
178     data.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
179     if (!data.WriteParcelable(&request)) {
180         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteParcelable(request)");
181         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
182     }
183 
184     MessageParcel reply;
185     if (!SendRequest(PrivacyInterfaceCode::GET_PERMISSION_USED_RECORDS, data, reply)) {
186         return PrivacyError::ERR_SERVICE_ABNORMAL;
187     }
188 
189     int32_t ret = reply.ReadInt32();
190     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server data = %{public}d", ret);
191     if (ret != RET_SUCCESS) {
192         return ret;
193     }
194     sptr<PermissionUsedResultParcel> resultSptr = reply.ReadParcelable<PermissionUsedResultParcel>();
195     if (resultSptr == nullptr) {
196         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadParcelable fail");
197         return PrivacyError::ERR_READ_PARCEL_FAILED;
198     }
199     result = *resultSptr;
200     return ret;
201 }
202 
GetPermissionUsedRecords(const PermissionUsedRequestParcel & request,const sptr<OnPermissionUsedRecordCallback> & callback)203 int32_t PrivacyManagerProxy::GetPermissionUsedRecords(const PermissionUsedRequestParcel& request,
204     const sptr<OnPermissionUsedRecordCallback>& callback)
205 {
206     MessageParcel data;
207     data.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
208     if (!data.WriteParcelable(&request)) {
209         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteParcelable(request)");
210         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
211     }
212     if (!data.WriteRemoteObject(callback->AsObject())) {
213         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteRemoteObject(callback)");
214         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
215     }
216 
217     MessageParcel reply;
218     if (!SendRequest(PrivacyInterfaceCode::GET_PERMISSION_USED_RECORDS_ASYNC, data, reply)) {
219         return PrivacyError::ERR_SERVICE_ABNORMAL;
220     }
221 
222     int32_t result = reply.ReadInt32();
223     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server data = %{public}d", result);
224     return result;
225 }
226 
RegisterPermActiveStatusCallback(std::vector<std::string> & permList,const sptr<IRemoteObject> & callback)227 int32_t PrivacyManagerProxy::RegisterPermActiveStatusCallback(
228     std::vector<std::string>& permList, const sptr<IRemoteObject>& callback)
229 {
230     MessageParcel data;
231     if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) {
232         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken.");
233         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
234     }
235 
236     uint32_t listSize = permList.size();
237     if (!data.WriteUint32(listSize)) {
238         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write listSize");
239         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
240     }
241     for (uint32_t i = 0; i < listSize; i++) {
242         if (!data.WriteString(permList[i])) {
243             ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write permList[%{public}d], %{public}s", i, permList[i].c_str());
244             return PrivacyError::ERR_WRITE_PARCEL_FAILED;
245         }
246     }
247 
248     if (!data.WriteRemoteObject(callback)) {
249         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write remote object.");
250         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
251     }
252     MessageParcel reply;
253     if (!SendRequest(PrivacyInterfaceCode::REGISTER_PERM_ACTIVE_STATUS_CHANGE_CALLBACK, data, reply)) {
254         return PrivacyError::ERR_SERVICE_ABNORMAL;
255     }
256 
257     int32_t result = reply.ReadInt32();
258     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server data = %{public}d", result);
259     return result;
260 }
261 
UnRegisterPermActiveStatusCallback(const sptr<IRemoteObject> & callback)262 int32_t PrivacyManagerProxy::UnRegisterPermActiveStatusCallback(const sptr<IRemoteObject>& callback)
263 {
264     MessageParcel data;
265     if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) {
266         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken.");
267         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
268     }
269     if (!data.WriteRemoteObject(callback)) {
270         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write remote object.");
271         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
272     }
273     MessageParcel reply;
274     if (!SendRequest(
275         PrivacyInterfaceCode::UNREGISTER_PERM_ACTIVE_STATUS_CHANGE_CALLBACK, data, reply)) {
276         return PrivacyError::ERR_SERVICE_ABNORMAL;
277     }
278 
279     int32_t result = reply.ReadInt32();
280     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server data = %{public}d", result);
281     return result;
282 }
283 
IsAllowedUsingPermission(AccessTokenID tokenID,const std::string & permissionName)284 bool PrivacyManagerProxy::IsAllowedUsingPermission(AccessTokenID tokenID, const std::string& permissionName)
285 {
286     MessageParcel data;
287     MessageParcel reply;
288     if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) {
289         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken.");
290         return false;
291     }
292     if (!data.WriteUint32(tokenID)) {
293         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(%{public}d)", tokenID);
294         return false;
295     }
296     if (!data.WriteString(permissionName)) {
297         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteString(%{public}s)", permissionName.c_str());
298         return false;
299     }
300     if (!SendRequest(PrivacyInterfaceCode::IS_ALLOWED_USING_PERMISSION, data, reply)) {
301         return PrivacyError::ERR_SERVICE_ABNORMAL;
302     }
303 
304     bool result = reply.ReadBool();
305     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server data = %{public}d", result);
306     return result;
307 }
308 
309 #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE
RegisterSecCompEnhance(const SecCompEnhanceDataParcel & enhance)310 int32_t PrivacyManagerProxy::RegisterSecCompEnhance(const SecCompEnhanceDataParcel& enhance)
311 {
312     MessageParcel data;
313     MessageParcel reply;
314     if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) {
315         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken.");
316         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
317     }
318 
319     if (!data.WriteParcelable(&enhance)) {
320         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write parcel.");
321         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
322     }
323 
324     if (!SendRequest(PrivacyInterfaceCode::REGISTER_SEC_COMP_ENHANCE, data, reply)) {
325         return PrivacyError::ERR_SERVICE_ABNORMAL;
326     }
327 
328     int32_t result = reply.ReadInt32();
329     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server data = %{public}d", result);
330     return result;
331 }
332 
UpdateSecCompEnhance(int32_t pid,uint32_t seqNum)333 int32_t PrivacyManagerProxy::UpdateSecCompEnhance(int32_t pid, uint32_t seqNum)
334 {
335     MessageParcel data;
336     MessageParcel reply;
337     if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) {
338         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write GetDescriptor.");
339         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
340     }
341     if (!data.WriteInt32(pid)) {
342         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write pid=%{public}d.", pid);
343         return false;
344     }
345     if (!data.WriteUint32(seqNum)) {
346         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write seqNum=%{public}u.", seqNum);
347         return false;
348     }
349     if (!SendRequest(PrivacyInterfaceCode::UPDATE_SEC_COMP_ENHANCE, data, reply)) {
350         return PrivacyError::ERR_SERVICE_ABNORMAL;
351     }
352 
353     int32_t result = reply.ReadInt32();
354     ACCESSTOKEN_LOG_INFO(LABEL, "Result=%{public}d", result);
355     return result;
356 }
357 
GetSecCompEnhance(int32_t pid,SecCompEnhanceDataParcel & enhanceParcel)358 int32_t PrivacyManagerProxy::GetSecCompEnhance(int32_t pid, SecCompEnhanceDataParcel& enhanceParcel)
359 {
360     MessageParcel data;
361     MessageParcel reply;
362     if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) {
363         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken.");
364         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
365     }
366     if (!data.WriteInt32(pid)) {
367         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteInt32(%{public}d)", pid);
368         return false;
369     }
370     if (!SendRequest(PrivacyInterfaceCode::GET_SEC_COMP_ENHANCE, data, reply)) {
371         return PrivacyError::ERR_SERVICE_ABNORMAL;
372     }
373 
374     int32_t result = reply.ReadInt32();
375     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server data = %{public}d", result);
376     if (result != RET_SUCCESS) {
377         return result;
378     }
379 
380     sptr<SecCompEnhanceDataParcel> parcel = reply.ReadParcelable<SecCompEnhanceDataParcel>();
381     if (parcel != nullptr) {
382         enhanceParcel = *parcel;
383     }
384     return result;
385 }
386 
GetSpecialSecCompEnhance(const std::string & bundleName,std::vector<SecCompEnhanceDataParcel> & enhanceParcelList)387 int32_t PrivacyManagerProxy::GetSpecialSecCompEnhance(const std::string& bundleName,
388     std::vector<SecCompEnhanceDataParcel>& enhanceParcelList)
389 {
390     MessageParcel data;
391     MessageParcel reply;
392     if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) {
393         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken.");
394         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
395     }
396 
397     if (!data.WriteString(bundleName)) {
398         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write string.");
399         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
400     }
401 
402     if (!SendRequest(PrivacyInterfaceCode::GET_SPECIAL_SEC_COMP_ENHANCE, data, reply)) {
403         return PrivacyError::ERR_SERVICE_ABNORMAL;
404     }
405 
406     int32_t result = reply.ReadInt32();
407     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server data = %{public}d", result);
408     if (result != RET_SUCCESS) {
409         return result;
410     }
411 
412     uint32_t size = reply.ReadUint32();
413     if (size > MAX_SEC_COMP_ENHANCE_SIZE) {
414         ACCESSTOKEN_LOG_ERROR(LABEL, "Size = %{public}d get from request is invalid", size);
415         return PrivacyError::ERR_OVERSIZE;
416     }
417     for (uint32_t i = 0; i < size; i++) {
418         sptr<SecCompEnhanceDataParcel> parcel = reply.ReadParcelable<SecCompEnhanceDataParcel>();
419         if (parcel != nullptr) {
420             enhanceParcelList.emplace_back(*parcel);
421         }
422     }
423     return result;
424 }
425 #endif
426 
GetPermissionUsedTypeInfos(const AccessTokenID tokenId,const std::string & permissionName,std::vector<PermissionUsedTypeInfoParcel> & resultsParcel)427 int32_t PrivacyManagerProxy::GetPermissionUsedTypeInfos(const AccessTokenID tokenId, const std::string& permissionName,
428     std::vector<PermissionUsedTypeInfoParcel>& resultsParcel)
429 {
430     MessageParcel data;
431     MessageParcel reply;
432     if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) {
433         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken.");
434         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
435     }
436     if (!data.WriteUint32(tokenId)) {
437         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(%{public}d)", tokenId);
438         return false;
439     }
440     if (!data.WriteString(permissionName)) {
441         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteString(%{public}s)", permissionName.c_str());
442         return false;
443     }
444 
445     if (!SendRequest(PrivacyInterfaceCode::GET_PERMISSION_USED_TYPE_INFOS, data, reply)) {
446         return PrivacyError::ERR_SERVICE_ABNORMAL;
447     }
448 
449     int32_t result = reply.ReadInt32();
450     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server is %{public}d.", result);
451     if (result != RET_SUCCESS) {
452         return result;
453     }
454 
455     uint32_t size = reply.ReadUint32();
456     if (size > MAX_PERMISSION_USED_TYPE_SIZE) {
457         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed, results oversize %{public}d, please add query params!", size);
458         return PrivacyError::ERR_OVERSIZE;
459     }
460     for (uint32_t i = 0; i < size; i++) {
461         sptr<PermissionUsedTypeInfoParcel> parcel = reply.ReadParcelable<PermissionUsedTypeInfoParcel>();
462         if (parcel != nullptr) {
463             resultsParcel.emplace_back(*parcel);
464         }
465     }
466     return result;
467 }
468 
SetMutePolicy(uint32_t policyType,uint32_t callerType,bool isMute)469 int32_t PrivacyManagerProxy::SetMutePolicy(uint32_t policyType, uint32_t callerType, bool isMute)
470 {
471     MessageParcel data;
472     MessageParcel reply;
473     if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) {
474         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken.");
475         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
476     }
477 
478     if (!data.WriteUint32(policyType)) {
479         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(%{public}d)", policyType);
480         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
481     }
482     if (!data.WriteUint32(callerType)) {
483         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(%{public}d)", callerType);
484         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
485     }
486     if (!data.WriteBool(isMute)) {
487         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteBool(%{public}d)", isMute);
488         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
489     }
490     if (!SendRequest(PrivacyInterfaceCode::SET_MUTE_POLICY, data, reply)) {
491         return PrivacyError::ERR_SERVICE_ABNORMAL;
492     }
493     int32_t result = reply.ReadInt32();
494     ACCESSTOKEN_LOG_INFO(LABEL, "result from server is %{public}d.", result);
495     return result;
496 }
497 
SetHapWithFGReminder(uint32_t tokenId,bool isAllowed)498 int32_t PrivacyManagerProxy::SetHapWithFGReminder(uint32_t tokenId, bool isAllowed)
499 {
500     MessageParcel data;
501     MessageParcel reply;
502     if (!data.WriteInterfaceToken(IPrivacyManager::GetDescriptor())) {
503         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write WriteInterfaceToken.");
504         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
505     }
506 
507     if (!data.WriteUint32(tokenId)) {
508         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteUint32(%{public}d)", tokenId);
509         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
510     }
511 
512     if (!data.WriteBool(isAllowed)) {
513         ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to WriteBool(%{public}d)", isAllowed);
514         return PrivacyError::ERR_WRITE_PARCEL_FAILED;
515     }
516     if (!SendRequest(PrivacyInterfaceCode::SET_HAP_WITH_FOREGROUND_REMINDER, data, reply)) {
517         return PrivacyError::ERR_SERVICE_ABNORMAL;
518     }
519     int32_t result = reply.ReadInt32();
520     ACCESSTOKEN_LOG_INFO(LABEL, "Result from server is %{public}d.", result);
521     return result;
522 }
523 
SendRequest(PrivacyInterfaceCode code,MessageParcel & data,MessageParcel & reply,bool asyncMode)524 bool PrivacyManagerProxy::SendRequest(
525     PrivacyInterfaceCode code, MessageParcel& data, MessageParcel& reply, bool asyncMode)
526 {
527     int flag = 0;
528     if (asyncMode) {
529         flag = static_cast<int>(MessageOption::TF_ASYNC);
530     } else {
531         flag = static_cast<int>(MessageOption::TF_SYNC);
532     }
533     MessageOption option(flag);
534     sptr<IRemoteObject> remote = Remote();
535     if (remote == nullptr) {
536         ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service null.");
537         return false;
538     }
539 
540     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
541     if (result != NO_ERROR) {
542         ACCESSTOKEN_LOG_ERROR(LABEL, "SendRequest(code=%{public}d) fail, result: %{public}d", code, result);
543         return false;
544     }
545     return true;
546 }
547 } // namespace AccessToken
548 } // namespace Security
549 } // namespace OHOS
550