• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "app_account_proxy.h"
17 
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace AccountSA {
AppAccountProxy(const sptr<IRemoteObject> & object)23 AppAccountProxy::AppAccountProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IAppAccount>(object)
24 {}
25 
~AppAccountProxy()26 AppAccountProxy::~AppAccountProxy()
27 {}
28 
SendRequestWithTwoStr(MessageParcel & reply,AppAccountInterfaceCode code,const std::string & str1,const std::string & str2)29 ErrCode AppAccountProxy::SendRequestWithTwoStr(MessageParcel &reply, AppAccountInterfaceCode code,
30     const std::string &str1, const std::string &str2)
31 {
32     MessageParcel data;
33     if (!data.WriteInterfaceToken(GetDescriptor())) {
34         ACCOUNT_LOGE("failed to write descriptor!");
35         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
36     }
37 
38     if (!data.WriteString(str1)) {
39         ACCOUNT_LOGE("failed to write string for str1 %{public}s", str1.c_str());
40         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
41     }
42 
43     if (!data.WriteString(str2)) {
44         ACCOUNT_LOGE("failed to write string for str2 %{public}s", str2.c_str());
45         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
46     }
47 
48     ErrCode result = SendRequest(code, data, reply);
49     if (result != ERR_OK) {
50         return result;
51     }
52     if (!reply.ReadInt32(result)) {
53         ACCOUNT_LOGE("failed to read result for code %{public}d.", code);
54         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
55     }
56     if (result != ERR_OK) {
57         ACCOUNT_LOGE("result failed for code %{public}d, result %{public}d.", code, result);
58     }
59     return result;
60 }
61 
AddAccount(const std::string & name,const std::string & extraInfo)62 ErrCode AppAccountProxy::AddAccount(const std::string &name, const std::string &extraInfo)
63 {
64     MessageParcel reply;
65     return SendRequestWithTwoStr(reply, AppAccountInterfaceCode::ADD_ACCOUNT, name, extraInfo);
66 }
67 
AddAccountImplicitly(const std::string & owner,const std::string & authType,const AAFwk::Want & options,const sptr<IAppAccountAuthenticatorCallback> & callback)68 ErrCode AppAccountProxy::AddAccountImplicitly(const std::string &owner, const std::string &authType,
69     const AAFwk::Want &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
70 {
71     MessageParcel data;
72     MessageParcel reply;
73 
74     if (!data.WriteInterfaceToken(GetDescriptor())) {
75         ACCOUNT_LOGE("failed to write descriptor!");
76         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
77     }
78 
79     if (!data.WriteString(owner)) {
80         ACCOUNT_LOGE("failed to write string for name");
81         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
82     }
83     if (!data.WriteString(authType)) {
84         ACCOUNT_LOGE("failed to write string for authType");
85         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
86     }
87     if (!data.WriteParcelable(&options)) {
88         ACCOUNT_LOGE("failed to write parcelable for options");
89         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
90     }
91     if ((callback != nullptr) && (!data.WriteRemoteObject(callback->AsObject()))) {
92         ACCOUNT_LOGE("failed to write remote object for callback");
93         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
94     }
95     ErrCode result = SendRequest(AppAccountInterfaceCode::ADD_ACCOUNT_IMPLICITLY, data, reply);
96     if (result != ERR_OK) {
97         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
98         return result;
99     }
100     result = reply.ReadInt32();
101     return result;
102 }
103 
104 
CreateAccount(const std::string & name,const CreateAccountOptions & options)105 ErrCode AppAccountProxy::CreateAccount(const std::string &name, const CreateAccountOptions &options)
106 {
107     MessageParcel data;
108     if (!data.WriteInterfaceToken(GetDescriptor())) {
109         ACCOUNT_LOGE("failed to write descriptor!");
110         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
111     }
112     if (!data.WriteString(name)) {
113         ACCOUNT_LOGE("failed to write name");
114         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
115     }
116     if (!data.WriteParcelable(&options)) {
117         ACCOUNT_LOGE("failed to write options");
118         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
119     }
120     MessageParcel reply;
121     ErrCode result = SendRequest(AppAccountInterfaceCode::CREATE_ACCOUNT, data, reply);
122     if (result != ERR_OK) {
123         return result;
124     }
125     return reply.ReadInt32();
126 }
127 
CreateAccountImplicitly(const std::string & owner,const CreateAccountImplicitlyOptions & options,const sptr<IAppAccountAuthenticatorCallback> & callback)128 ErrCode AppAccountProxy::CreateAccountImplicitly(const std::string &owner,
129     const CreateAccountImplicitlyOptions &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
130 {
131     MessageParcel data;
132     if (!data.WriteInterfaceToken(GetDescriptor())) {
133         ACCOUNT_LOGE("failed to write descriptor!");
134         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
135     }
136     if (!data.WriteString(owner)) {
137         ACCOUNT_LOGE("failed to write owner");
138         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
139     }
140     if (!data.WriteParcelable(&options)) {
141         ACCOUNT_LOGE("failed to write options");
142         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
143     }
144     if ((callback != nullptr) && (!data.WriteRemoteObject(callback->AsObject()))) {
145         ACCOUNT_LOGE("failed to write remote object for callback");
146         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
147     }
148     MessageParcel reply;
149     ErrCode result = SendRequest(AppAccountInterfaceCode::CREATE_ACCOUNT_IMPLICITLY, data, reply);
150     if (result != ERR_OK) {
151         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
152         return result;
153     }
154     return reply.ReadInt32();
155 }
156 
DeleteAccount(const std::string & name)157 ErrCode AppAccountProxy::DeleteAccount(const std::string &name)
158 {
159     MessageParcel data;
160     MessageParcel reply;
161 
162     if (!data.WriteInterfaceToken(GetDescriptor())) {
163         ACCOUNT_LOGE("failed to write descriptor!");
164         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
165     }
166 
167     if (!data.WriteString(name)) {
168         ACCOUNT_LOGE("failed to write string for name");
169         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
170     }
171 
172     ErrCode result = SendRequest(AppAccountInterfaceCode::DELETE_ACCOUNT, data, reply);
173     if (result != ERR_OK) {
174         return result;
175     }
176 
177     result = reply.ReadInt32();
178 
179     return result;
180 }
181 
GetAccountExtraInfo(const std::string & name,std::string & extraInfo)182 ErrCode AppAccountProxy::GetAccountExtraInfo(const std::string &name, std::string &extraInfo)
183 {
184     MessageParcel data;
185     MessageParcel reply;
186 
187     if (!data.WriteInterfaceToken(GetDescriptor())) {
188         ACCOUNT_LOGE("failed to write descriptor!");
189         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
190     }
191 
192     if (!data.WriteString(name)) {
193         ACCOUNT_LOGE("failed to write string for name");
194         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
195     }
196 
197     ErrCode result = SendRequest(AppAccountInterfaceCode::GET_ACCOUNT_EXTRA_INFO, data, reply);
198     if (result != ERR_OK) {
199         return result;
200     }
201 
202     result = reply.ReadInt32();
203     extraInfo = reply.ReadString();
204 
205     return result;
206 }
207 
SetAccountExtraInfo(const std::string & name,const std::string & extraInfo)208 ErrCode AppAccountProxy::SetAccountExtraInfo(const std::string &name, const std::string &extraInfo)
209 {
210     MessageParcel reply;
211     return SendRequestWithTwoStr(reply, AppAccountInterfaceCode::SET_ACCOUNT_EXTRA_INFO, name, extraInfo);
212 }
213 
EnableAppAccess(const std::string & name,const std::string & authorizedApp)214 ErrCode AppAccountProxy::EnableAppAccess(const std::string &name, const std::string &authorizedApp)
215 {
216     MessageParcel reply;
217     return SendRequestWithTwoStr(reply, AppAccountInterfaceCode::ENABLE_APP_ACCESS, name, authorizedApp);
218 }
219 
DisableAppAccess(const std::string & name,const std::string & authorizedApp)220 ErrCode AppAccountProxy::DisableAppAccess(const std::string &name, const std::string &authorizedApp)
221 {
222     MessageParcel reply;
223     return SendRequestWithTwoStr(reply, AppAccountInterfaceCode::DISABLE_APP_ACCESS, name, authorizedApp);
224 }
225 
SetAppAccess(const std::string & name,const std::string & authorizedApp,bool isAccessible)226 ErrCode AppAccountProxy::SetAppAccess(const std::string &name, const std::string &authorizedApp, bool isAccessible)
227 {
228     MessageParcel data;
229     MessageParcel reply;
230 
231     if (!data.WriteInterfaceToken(GetDescriptor())) {
232         ACCOUNT_LOGE("failed to write descriptor!");
233         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
234     }
235 
236     if (!data.WriteString(name)) {
237         ACCOUNT_LOGE("failed to write string for name");
238         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
239     }
240 
241     if (!data.WriteString(authorizedApp)) {
242         ACCOUNT_LOGE("failed to write string for authorizedApp");
243         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
244     }
245 
246     if (!data.WriteBool(isAccessible)) {
247         ACCOUNT_LOGE("failed to write string for isVisible");
248         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
249     }
250 
251     ErrCode result = SendRequest(AppAccountInterfaceCode::SET_APP_ACCESS, data, reply);
252     if (result != ERR_OK) {
253         return result;
254     }
255 
256     return reply.ReadInt32();
257 }
258 
CheckAppAccountSyncEnable(const std::string & name,bool & syncEnable)259 ErrCode AppAccountProxy::CheckAppAccountSyncEnable(const std::string &name, bool &syncEnable)
260 {
261     MessageParcel data;
262     MessageParcel reply;
263 
264     if (!data.WriteInterfaceToken(GetDescriptor())) {
265         ACCOUNT_LOGE("failed to write descriptor!");
266         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
267     }
268 
269     if (!data.WriteString(name)) {
270         ACCOUNT_LOGE("failed to write string for name");
271         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
272     }
273 
274     ErrCode result = SendRequest(AppAccountInterfaceCode::CHECK_APP_ACCOUNT_SYNC_ENABLE, data, reply);
275     if (result != ERR_OK) {
276         return result;
277     }
278 
279     result = reply.ReadInt32();
280     syncEnable = reply.ReadBool();
281 
282     return result;
283 }
284 
SetAppAccountSyncEnable(const std::string & name,const bool & syncEnable)285 ErrCode AppAccountProxy::SetAppAccountSyncEnable(const std::string &name, const bool &syncEnable)
286 {
287     MessageParcel data;
288     MessageParcel reply;
289 
290     if (!data.WriteInterfaceToken(GetDescriptor())) {
291         ACCOUNT_LOGE("failed to write descriptor!");
292         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
293     }
294 
295     if (!data.WriteString(name)) {
296         ACCOUNT_LOGE("failed to write string for name");
297         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
298     }
299 
300     if (!data.WriteBool(syncEnable)) {
301         ACCOUNT_LOGE("failed to write bool for syncEnable");
302         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
303     }
304 
305     ErrCode result = SendRequest(AppAccountInterfaceCode::SET_APP_ACCOUNT_SYNC_ENABLE, data, reply);
306     if (result != ERR_OK) {
307         return result;
308     }
309 
310     result = reply.ReadInt32();
311 
312     return result;
313 }
314 
GetAssociatedData(const std::string & name,const std::string & key,std::string & value)315 ErrCode AppAccountProxy::GetAssociatedData(const std::string &name, const std::string &key, std::string &value)
316 {
317     MessageParcel reply;
318     ErrCode result = SendRequestWithTwoStr(reply, AppAccountInterfaceCode::GET_ASSOCIATED_DATA, name, key);
319     if (result != ERR_OK) {
320         return result;
321     }
322     value = reply.ReadString();
323     return result;
324 }
325 
SetAssociatedData(const std::string & name,const std::string & key,const std::string & value)326 ErrCode AppAccountProxy::SetAssociatedData(const std::string &name, const std::string &key, const std::string &value)
327 {
328     MessageParcel data;
329     MessageParcel reply;
330 
331     if (!data.WriteInterfaceToken(GetDescriptor())) {
332         ACCOUNT_LOGE("failed to write descriptor!");
333         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
334     }
335 
336     if (!data.WriteString(name)) {
337         ACCOUNT_LOGE("failed to write string for name");
338         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
339     }
340 
341     if (!data.WriteString(key)) {
342         ACCOUNT_LOGE("failed to write string for key");
343         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
344     }
345 
346     if (!data.WriteString(value)) {
347         ACCOUNT_LOGE("failed to write string for value");
348         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
349     }
350 
351     ErrCode result = SendRequest(AppAccountInterfaceCode::SET_ASSOCIATED_DATA, data, reply);
352     if (result != ERR_OK) {
353         return result;
354     }
355 
356     result = reply.ReadInt32();
357 
358     return result;
359 }
360 
GetAccountCredential(const std::string & name,const std::string & credentialType,std::string & credential)361 ErrCode AppAccountProxy::GetAccountCredential(
362     const std::string &name, const std::string &credentialType, std::string &credential)
363 {
364     MessageParcel reply;
365     ErrCode result = SendRequestWithTwoStr(
366         reply, AppAccountInterfaceCode::GET_ACCOUNT_CREDENTIAL, name, credentialType);
367     if (result == ERR_OK) {
368         credential = reply.ReadString();
369     }
370     return result;
371 }
372 
SetAccountCredential(const std::string & name,const std::string & credentialType,const std::string & credential)373 ErrCode AppAccountProxy::SetAccountCredential(
374     const std::string &name, const std::string &credentialType, const std::string &credential)
375 {
376     MessageParcel data;
377     MessageParcel reply;
378 
379     if (!data.WriteInterfaceToken(GetDescriptor())) {
380         ACCOUNT_LOGE("failed to write descriptor!");
381         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
382     }
383 
384     if (!data.WriteString(name)) {
385         ACCOUNT_LOGE("failed to write string for name");
386         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
387     }
388 
389     if (!data.WriteString(credentialType)) {
390         ACCOUNT_LOGE("failed to write string for credentialType");
391         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
392     }
393 
394     if (!data.WriteString(credential)) {
395         ACCOUNT_LOGE("failed to write string for credential");
396         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
397     }
398 
399     ErrCode result = SendRequest(AppAccountInterfaceCode::SET_ACCOUNT_CREDENTIAL, data, reply);
400     if (result != ERR_OK) {
401         return result;
402     }
403 
404     result = reply.ReadInt32();
405 
406     return result;
407 }
408 
Authenticate(const std::string & name,const std::string & owner,const std::string & authType,const AAFwk::Want & options,const sptr<IAppAccountAuthenticatorCallback> & callback)409 ErrCode AppAccountProxy::Authenticate(const std::string &name, const std::string &owner, const std::string &authType,
410     const AAFwk::Want &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
411 {
412     MessageParcel data;
413     MessageParcel reply;
414 
415     if (!data.WriteInterfaceToken(GetDescriptor())) {
416         ACCOUNT_LOGE("failed to write descriptor!");
417         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
418     }
419 
420     if (!data.WriteString(name)) {
421         ACCOUNT_LOGE("failed to write string for name");
422         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
423     }
424     if (!data.WriteString(owner)) {
425         ACCOUNT_LOGE("failed to write string for owner");
426         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
427     }
428     if (!data.WriteString(authType)) {
429         ACCOUNT_LOGE("failed to write string for authType");
430         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
431     }
432     if (!data.WriteParcelable(&options)) {
433         ACCOUNT_LOGE("failed to write parcelable for options");
434         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
435     }
436     if ((callback != nullptr) && (!data.WriteRemoteObject(callback->AsObject()))) {
437         ACCOUNT_LOGE("failed to write remote object for callback");
438         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
439     }
440     ErrCode result = SendRequest(AppAccountInterfaceCode::AUTHENTICATE, data, reply);
441     if (result != ERR_OK) {
442         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
443         return result;
444     }
445     result = reply.ReadInt32();
446     return result;
447 }
448 
WriteGetAuthTokenParam(const std::string & name,const std::string & owner,const std::string & authType,MessageParcel & data)449 ErrCode AppAccountProxy::WriteGetAuthTokenParam(
450     const std::string &name, const std::string &owner, const std::string &authType, MessageParcel &data)
451 {
452     if (!data.WriteInterfaceToken(GetDescriptor())) {
453         ACCOUNT_LOGE("failed to write descriptor!");
454         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
455     }
456     if (!data.WriteString(name)) {
457         ACCOUNT_LOGE("failed to write string for name");
458         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
459     }
460     if (!data.WriteString(owner)) {
461         ACCOUNT_LOGE("failed to write string for owner");
462         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
463     }
464     if (!data.WriteString(authType)) {
465         ACCOUNT_LOGE("failed to write string for authType");
466         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
467     }
468     return ERR_OK;
469 }
470 
GetOAuthToken(const std::string & name,const std::string & owner,const std::string & authType,std::string & token)471 ErrCode AppAccountProxy::GetOAuthToken(
472     const std::string &name, const std::string &owner, const std::string &authType, std::string &token)
473 {
474     MessageParcel data;
475     MessageParcel reply;
476 
477     ErrCode result = WriteGetAuthTokenParam(name, owner, authType, data);
478     if (result != ERR_OK) {
479         return result;
480     }
481 
482     result = SendRequest(AppAccountInterfaceCode::GET_OAUTH_TOKEN, data, reply);
483     if (result != ERR_OK) {
484         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
485         return result;
486     }
487     result = reply.ReadInt32();
488     token = reply.ReadString();
489     return result;
490 }
491 
GetAuthToken(const std::string & name,const std::string & owner,const std::string & authType,std::string & token)492 ErrCode AppAccountProxy::GetAuthToken(
493     const std::string &name, const std::string &owner, const std::string &authType, std::string &token)
494 {
495     MessageParcel data;
496     MessageParcel reply;
497 
498     ErrCode result = WriteGetAuthTokenParam(name, owner, authType, data);
499     if (result != ERR_OK) {
500         return result;
501     }
502 
503     result = SendRequest(AppAccountInterfaceCode::GET_AUTH_TOKEN, data, reply);
504     if (result != ERR_OK) {
505         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
506         return result;
507     }
508     result = reply.ReadInt32();
509     token = reply.ReadString();
510     return result;
511 }
512 
SetOAuthToken(const std::string & name,const std::string & authType,const std::string & token)513 ErrCode AppAccountProxy::SetOAuthToken(
514     const std::string &name, const std::string &authType, const std::string &token)
515 {
516     MessageParcel data;
517     MessageParcel reply;
518 
519     if (!data.WriteInterfaceToken(GetDescriptor())) {
520         ACCOUNT_LOGE("failed to write descriptor!");
521         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
522     }
523 
524     if (!data.WriteString(name)) {
525         ACCOUNT_LOGE("failed to write string for name");
526         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
527     }
528     if (!data.WriteString(authType)) {
529         ACCOUNT_LOGE("failed to write string for authType");
530         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
531     }
532     if (!data.WriteString(token)) {
533         ACCOUNT_LOGE("failed to write string for token");
534         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
535     }
536     ErrCode result = SendRequest(AppAccountInterfaceCode::SET_OAUTH_TOKEN, data, reply);
537     if (result != ERR_OK) {
538         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
539         return result;
540     }
541     result = reply.ReadInt32();
542     return result;
543 }
544 
WriteDeleteAuthTokenParam(const std::string & name,const std::string & owner,const std::string & authType,const std::string & token,MessageParcel & data)545 ErrCode AppAccountProxy::WriteDeleteAuthTokenParam(const std::string &name, const std::string &owner,
546     const std::string &authType, const std::string &token, MessageParcel &data)
547 {
548     if (!data.WriteInterfaceToken(GetDescriptor())) {
549         ACCOUNT_LOGE("failed to write descriptor!");
550         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
551     }
552 
553     if (!data.WriteString(name)) {
554         ACCOUNT_LOGE("failed to write string for token");
555         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
556     }
557     if (!data.WriteString(owner)) {
558         ACCOUNT_LOGE("failed to write string for owner");
559         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
560     }
561     if (!data.WriteString(authType)) {
562         ACCOUNT_LOGE("failed to write string for authType");
563         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
564     }
565     if (!data.WriteString(token)) {
566         ACCOUNT_LOGE("failed to write string for token");
567         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
568     }
569     return ERR_OK;
570 }
571 
DeleteOAuthToken(const std::string & name,const std::string & owner,const std::string & authType,const std::string & token)572 ErrCode AppAccountProxy::DeleteOAuthToken(
573     const std::string &name, const std::string &owner, const std::string &authType, const std::string &token)
574 {
575     MessageParcel data;
576     MessageParcel reply;
577 
578     ErrCode result = WriteDeleteAuthTokenParam(name, owner, authType, token, data);
579     if (result != ERR_OK) {
580         return result;
581     }
582 
583     result = SendRequest(AppAccountInterfaceCode::DELETE_OAUTH_TOKEN, data, reply);
584     if (result != ERR_OK) {
585         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
586         return result;
587     }
588     return reply.ReadInt32();
589 }
590 
DeleteAuthToken(const std::string & name,const std::string & owner,const std::string & authType,const std::string & token)591 ErrCode AppAccountProxy::DeleteAuthToken(
592     const std::string &name, const std::string &owner, const std::string &authType, const std::string &token)
593 {
594     MessageParcel data;
595     MessageParcel reply;
596 
597     ErrCode result = WriteDeleteAuthTokenParam(name, owner, authType, token, data);
598     if (result != ERR_OK) {
599         return result;
600     }
601     result = SendRequest(AppAccountInterfaceCode::DELETE_AUTH_TOKEN, data, reply);
602     if (result != ERR_OK) {
603         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
604         return result;
605     }
606     return reply.ReadInt32();
607 }
608 
WriteTokenVisibilityParam(const std::string & name,const std::string & authType,const std::string & bundleName,MessageParcel & data)609 ErrCode AppAccountProxy::WriteTokenVisibilityParam(
610     const std::string &name, const std::string &authType, const std::string &bundleName, MessageParcel &data)
611 {
612     if (!data.WriteInterfaceToken(GetDescriptor())) {
613         ACCOUNT_LOGE("failed to write descriptor!");
614         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
615     }
616 
617     if (!data.WriteString(name)) {
618         ACCOUNT_LOGE("failed to write string for name");
619         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
620     }
621     if (!data.WriteString(authType)) {
622         ACCOUNT_LOGE("failed to write string for authType");
623         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
624     }
625     if (!data.WriteString(bundleName)) {
626         ACCOUNT_LOGE("failed to write string for bundleName");
627         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
628     }
629     return ERR_OK;
630 }
631 
SetAuthTokenVisibility(const std::string & name,const std::string & authType,const std::string & bundleName,bool isVisible)632 ErrCode AppAccountProxy::SetAuthTokenVisibility(
633     const std::string &name, const std::string &authType, const std::string &bundleName, bool isVisible)
634 {
635     MessageParcel data;
636     MessageParcel reply;
637 
638     ErrCode result = WriteTokenVisibilityParam(name, authType, bundleName, data);
639     if (result != ERR_OK) {
640         return result;
641     }
642 
643     if (!data.WriteBool(isVisible)) {
644         ACCOUNT_LOGE("failed to write string for isVisible");
645         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
646     }
647     result = SendRequest(AppAccountInterfaceCode::SET_AUTH_TOKEN_VISIBILITY, data, reply);
648     if (result != ERR_OK) {
649         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
650         return result;
651     }
652     return reply.ReadInt32();
653 }
654 
SetOAuthTokenVisibility(const std::string & name,const std::string & authType,const std::string & bundleName,bool isVisible)655 ErrCode AppAccountProxy::SetOAuthTokenVisibility(
656     const std::string &name, const std::string &authType, const std::string &bundleName, bool isVisible)
657 {
658     MessageParcel data;
659     MessageParcel reply;
660 
661     ErrCode result = WriteTokenVisibilityParam(name, authType, bundleName, data);
662     if (result != ERR_OK) {
663         return result;
664     }
665 
666     if (!data.WriteBool(isVisible)) {
667         ACCOUNT_LOGE("failed to write string for isVisible");
668         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
669     }
670     result = SendRequest(AppAccountInterfaceCode::SET_OAUTH_TOKEN_VISIBILITY, data, reply);
671     if (result != ERR_OK) {
672         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
673         return result;
674     }
675     return reply.ReadInt32();
676 }
677 
CheckAuthTokenVisibility(const std::string & name,const std::string & authType,const std::string & bundleName,bool & isVisible)678 ErrCode AppAccountProxy::CheckAuthTokenVisibility(
679     const std::string &name, const std::string &authType, const std::string &bundleName, bool &isVisible)
680 {
681     MessageParcel data;
682     MessageParcel reply;
683 
684     ErrCode result = WriteTokenVisibilityParam(name, authType, bundleName, data);
685     if (result != ERR_OK) {
686         return result;
687     }
688 
689     result = SendRequest(AppAccountInterfaceCode::CHECK_AUTH_TOKEN_VISIBILITY, data, reply);
690     if (result != ERR_OK) {
691         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
692         return result;
693     }
694     result = reply.ReadInt32();
695     isVisible = reply.ReadBool();
696     return result;
697 }
698 
CheckOAuthTokenVisibility(const std::string & name,const std::string & authType,const std::string & bundleName,bool & isVisible)699 ErrCode AppAccountProxy::CheckOAuthTokenVisibility(
700     const std::string &name, const std::string &authType, const std::string &bundleName, bool &isVisible)
701 {
702     MessageParcel data;
703     MessageParcel reply;
704 
705     ErrCode result = WriteTokenVisibilityParam(name, authType, bundleName, data);
706     if (result != ERR_OK) {
707         return result;
708     }
709 
710     result = SendRequest(AppAccountInterfaceCode::CHECK_OAUTH_TOKEN_VISIBILITY, data, reply);
711     if (result != ERR_OK) {
712         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
713         return result;
714     }
715     result = reply.ReadInt32();
716     isVisible = reply.ReadBool();
717     return result;
718 }
719 
GetAuthenticatorInfo(const std::string & owner,AuthenticatorInfo & info)720 ErrCode AppAccountProxy::GetAuthenticatorInfo(const std::string &owner, AuthenticatorInfo &info)
721 {
722     MessageParcel data;
723     MessageParcel reply;
724 
725     if (!data.WriteInterfaceToken(GetDescriptor())) {
726         ACCOUNT_LOGE("failed to write descriptor!");
727         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
728     }
729 
730     if (!data.WriteString(owner)) {
731         ACCOUNT_LOGE("failed to write string for owner");
732         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
733     }
734     ErrCode result = SendRequest(AppAccountInterfaceCode::GET_AUTHENTICATOR_INFO, data, reply);
735     if (result != ERR_OK) {
736         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
737         return result;
738     }
739     result = reply.ReadInt32();
740     info.owner = reply.ReadString();
741     info.iconId = reply.ReadInt32();
742     info.labelId = reply.ReadInt32();
743     return result;
744 }
745 
GetAllOAuthTokens(const std::string & name,const std::string & owner,std::vector<OAuthTokenInfo> & tokenInfos)746 ErrCode AppAccountProxy::GetAllOAuthTokens(
747     const std::string &name, const std::string &owner, std::vector<OAuthTokenInfo> &tokenInfos)
748 {
749     tokenInfos.clear();
750     MessageParcel reply;
751     ErrCode result = SendRequestWithTwoStr(reply, AppAccountInterfaceCode::GET_ALL_OAUTH_TOKENS, name, owner);
752     if (result != ERR_OK) {
753         return result;
754     }
755     uint32_t size = reply.ReadUint32();
756     for (uint32_t i = 0; i < size; ++i) {
757         OAuthTokenInfo tokenInfo;
758         tokenInfo.token = reply.ReadString();
759         tokenInfo.authType = reply.ReadString();
760         tokenInfos.push_back(tokenInfo);
761     }
762     return result;
763 }
764 
WriteGetAuthListParam(const std::string & name,const std::string & authType,MessageParcel & data)765 ErrCode AppAccountProxy::WriteGetAuthListParam(
766     const std::string &name, const std::string &authType, MessageParcel &data)
767 {
768     if (!data.WriteInterfaceToken(GetDescriptor())) {
769         ACCOUNT_LOGE("failed to write descriptor!");
770         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
771     }
772 
773     if (!data.WriteString(name)) {
774         ACCOUNT_LOGE("failed to write string for name");
775         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
776     }
777     if (!data.WriteString(authType)) {
778         ACCOUNT_LOGE("failed to write string for authType");
779         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
780     }
781     return ERR_OK;
782 }
783 
GetOAuthList(const std::string & name,const std::string & authType,std::set<std::string> & oauthList)784 ErrCode AppAccountProxy::GetOAuthList(
785     const std::string &name, const std::string &authType, std::set<std::string> &oauthList)
786 {
787     MessageParcel data;
788     MessageParcel reply;
789 
790     ErrCode result = WriteGetAuthListParam(name, authType, data);
791     if (result != ERR_OK) {
792         return result;
793     }
794 
795     result = SendRequest(AppAccountInterfaceCode::GET_OAUTH_LIST, data, reply);
796     if (result != ERR_OK) {
797         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
798         return result;
799     }
800     result = reply.ReadInt32();
801     uint32_t size = reply.ReadUint32();
802     for (uint32_t i = 0; i < size; ++i) {
803         oauthList.emplace(reply.ReadString());
804     }
805     return result;
806 }
807 
GetAuthList(const std::string & name,const std::string & authType,std::set<std::string> & oauthList)808 ErrCode AppAccountProxy::GetAuthList(
809     const std::string &name, const std::string &authType, std::set<std::string> &oauthList)
810 {
811     MessageParcel data;
812     MessageParcel reply;
813 
814     ErrCode result = WriteGetAuthListParam(name, authType, data);
815     if (result != ERR_OK) {
816         return result;
817     }
818 
819     result = SendRequest(AppAccountInterfaceCode::GET_AUTH_LIST, data, reply);
820     if (result != ERR_OK) {
821         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
822         return result;
823     }
824     result = reply.ReadInt32();
825     uint32_t size = reply.ReadUint32();
826     for (uint32_t i = 0; i < size; ++i) {
827         oauthList.emplace(reply.ReadString());
828     }
829     return result;
830 }
831 
GetAuthenticatorCallback(const std::string & sessionId,sptr<IRemoteObject> & callback)832 ErrCode AppAccountProxy::GetAuthenticatorCallback(const std::string &sessionId, sptr<IRemoteObject> &callback)
833 {
834     MessageParcel data;
835     MessageParcel reply;
836 
837     if (!data.WriteInterfaceToken(GetDescriptor())) {
838         ACCOUNT_LOGE("failed to write descriptor!");
839         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
840     }
841 
842     if (!data.WriteString(sessionId)) {
843         ACCOUNT_LOGE("failed to write string for sessionId");
844         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
845     }
846     ErrCode result = SendRequest(AppAccountInterfaceCode::GET_AUTHENTICATOR_CALLBACK, data, reply);
847     if (result != ERR_OK) {
848         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
849         return result;
850     }
851     result = reply.ReadInt32();
852     callback = reply.ReadRemoteObject();
853     return result;
854 }
855 
GetAllAccounts(const std::string & owner,std::vector<AppAccountInfo> & appAccounts)856 ErrCode AppAccountProxy::GetAllAccounts(const std::string &owner, std::vector<AppAccountInfo> &appAccounts)
857 {
858     MessageParcel data;
859     MessageParcel reply;
860 
861     if (!data.WriteInterfaceToken(GetDescriptor())) {
862         ACCOUNT_LOGE("failed to write descriptor!");
863         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
864     }
865 
866     if (!data.WriteString(owner)) {
867         ACCOUNT_LOGE("failed to write string for owner");
868         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
869     }
870 
871     ErrCode result = SendRequest(AppAccountInterfaceCode::GET_ALL_ACCOUNTS, data, reply);
872     if (result != ERR_OK) {
873         return result;
874     }
875 
876     result = reply.ReadInt32();
877 
878     if (!ReadParcelableVector(appAccounts, reply)) {
879         ACCOUNT_LOGE("failed to read parcelable for AppAccountInfo");
880         return ERR_APPACCOUNT_KIT_READ_PARCELABLE_APP_ACCOUNT_INFO;
881     }
882 
883     return result;
884 }
885 
GetAllAccessibleAccounts(std::vector<AppAccountInfo> & appAccounts)886 ErrCode AppAccountProxy::GetAllAccessibleAccounts(std::vector<AppAccountInfo> &appAccounts)
887 {
888     MessageParcel data;
889     MessageParcel reply;
890 
891     if (!data.WriteInterfaceToken(GetDescriptor())) {
892         ACCOUNT_LOGE("failed to write descriptor!");
893         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
894     }
895 
896     ErrCode result = SendRequest(AppAccountInterfaceCode::GET_ALL_ACCESSIBLE_ACCOUNTS, data, reply);
897     if (result != ERR_OK) {
898         return result;
899     }
900 
901     result = reply.ReadInt32();
902 
903     if (!ReadParcelableVector(appAccounts, reply)) {
904         ACCOUNT_LOGE("failed to read parcelable for AppAccountInfo");
905         return ERR_APPACCOUNT_KIT_READ_PARCELABLE_APP_ACCOUNT_INFO;
906     }
907 
908     return result;
909 }
910 
QueryAllAccessibleAccounts(const std::string & owner,std::vector<AppAccountInfo> & appAccounts)911 ErrCode AppAccountProxy::QueryAllAccessibleAccounts(const std::string &owner, std::vector<AppAccountInfo> &appAccounts)
912 {
913     MessageParcel data;
914     if (!data.WriteInterfaceToken(GetDescriptor())) {
915         ACCOUNT_LOGE("failed to write descriptor!");
916         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
917     }
918     if (!data.WriteString(owner)) {
919         ACCOUNT_LOGE("failed to write string for owner");
920         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
921     }
922     MessageParcel reply;
923     ErrCode result = SendRequest(AppAccountInterfaceCode::QUERY_ALL_ACCESSIBLE_ACCOUNTS, data, reply);
924     if (result != ERR_OK) {
925         return result;
926     }
927     result = reply.ReadInt32();
928     if (!ReadParcelableVector(appAccounts, reply)) {
929         ACCOUNT_LOGE("failed to read parcelable for AppAccountInfo");
930         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
931     }
932     return result;
933 }
934 
CheckAppAccess(const std::string & name,const std::string & authorizedApp,bool & isAccessible)935 ErrCode AppAccountProxy::CheckAppAccess(
936     const std::string &name, const std::string &authorizedApp, bool &isAccessible)
937 {
938     MessageParcel reply;
939     ErrCode result = SendRequestWithTwoStr(reply, AppAccountInterfaceCode::CHECK_APP_ACCESS, name, authorizedApp);
940     if (result == ERR_OK) {
941         isAccessible = reply.ReadBool();
942     }
943     return result;
944 }
945 
DeleteAccountCredential(const std::string & name,const std::string & credentialType)946 ErrCode AppAccountProxy::DeleteAccountCredential(
947     const std::string &name, const std::string &credentialType)
948 {
949     MessageParcel reply;
950     return SendRequestWithTwoStr(reply, AppAccountInterfaceCode::DELETE_ACCOUNT_CREDENTIAL, name, credentialType);
951 }
952 
SelectAccountsByOptions(const SelectAccountsOptions & options,const sptr<IAppAccountAuthenticatorCallback> & callback)953 ErrCode AppAccountProxy::SelectAccountsByOptions(
954     const SelectAccountsOptions &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
955 {
956     MessageParcel data;
957     if (!data.WriteInterfaceToken(GetDescriptor())) {
958         ACCOUNT_LOGE("failed to write descriptor!");
959         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
960     }
961     if (!data.WriteParcelable(&options)) {
962         ACCOUNT_LOGE("failed to write parcelable for options");
963         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
964     }
965     if ((callback != nullptr) && (!data.WriteRemoteObject(callback->AsObject()))) {
966         ACCOUNT_LOGE("failed to write remote object for callback");
967         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
968     }
969     MessageParcel reply;
970     ErrCode result = SendRequest(AppAccountInterfaceCode::SELECT_ACCOUNTS_BY_OPTIONS, data, reply);
971     if (result != ERR_OK) {
972         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
973         return result;
974     }
975     return reply.ReadInt32();
976 }
977 
VerifyCredential(const std::string & name,const std::string & owner,const VerifyCredentialOptions & options,const sptr<IAppAccountAuthenticatorCallback> & callback)978 ErrCode AppAccountProxy::VerifyCredential(const std::string &name, const std::string &owner,
979     const VerifyCredentialOptions &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
980 {
981     MessageParcel data;
982     if (!data.WriteInterfaceToken(GetDescriptor())) {
983         ACCOUNT_LOGE("failed to write descriptor!");
984         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
985     }
986     if (!data.WriteString(name)) {
987         ACCOUNT_LOGE("failed to write string for name");
988         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
989     }
990     if (!data.WriteString(owner)) {
991         ACCOUNT_LOGE("failed to write string for owner");
992         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
993     }
994     if (!data.WriteParcelable(&options)) {
995         ACCOUNT_LOGE("failed to write string for options");
996         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
997     }
998     if ((callback != nullptr) && (!data.WriteRemoteObject(callback->AsObject()))) {
999         ACCOUNT_LOGE("failed to write string for callback");
1000         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1001     }
1002     MessageParcel reply;
1003     ErrCode result = SendRequest(AppAccountInterfaceCode::VERIFY_CREDENTIAL, data, reply);
1004     if (result != ERR_OK) {
1005         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
1006         return result;
1007     }
1008     return reply.ReadInt32();
1009 }
1010 
CheckAccountLabels(const std::string & name,const std::string & owner,const std::vector<std::string> & labels,const sptr<IAppAccountAuthenticatorCallback> & callback)1011 ErrCode AppAccountProxy::CheckAccountLabels(const std::string &name, const std::string &owner,
1012     const std::vector<std::string> &labels, const sptr<IAppAccountAuthenticatorCallback> &callback)
1013 {
1014     MessageParcel data;
1015     if (!data.WriteInterfaceToken(GetDescriptor())) {
1016         ACCOUNT_LOGE("failed to write descriptor!");
1017         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1018     }
1019     if (!data.WriteString(name)) {
1020         ACCOUNT_LOGE("failed to write string for name");
1021         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1022     }
1023     if (!data.WriteString(owner)) {
1024         ACCOUNT_LOGE("failed to write string for owner");
1025         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1026     }
1027     if (!data.WriteStringVector(labels)) {
1028         ACCOUNT_LOGE("failed to write string vector for labels");
1029         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1030     }
1031     if ((callback != nullptr) && (!data.WriteRemoteObject(callback->AsObject()))) {
1032         ACCOUNT_LOGE("failed to write string for callback");
1033         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1034     }
1035     MessageParcel reply;
1036     ErrCode result = SendRequest(AppAccountInterfaceCode::CHECK_ACCOUNT_LABELS, data, reply);
1037     if (result != ERR_OK) {
1038         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
1039         return result;
1040     }
1041     return reply.ReadInt32();
1042 }
1043 
SetAuthenticatorProperties(const std::string & owner,const SetPropertiesOptions & options,const sptr<IAppAccountAuthenticatorCallback> & callback)1044 ErrCode AppAccountProxy::SetAuthenticatorProperties(const std::string &owner,
1045     const SetPropertiesOptions &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
1046 {
1047     MessageParcel data;
1048     if (!data.WriteInterfaceToken(GetDescriptor())) {
1049         ACCOUNT_LOGE("failed to write descriptor!");
1050         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1051     }
1052     if (!data.WriteString(owner)) {
1053         ACCOUNT_LOGE("failed to write string for owner");
1054         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1055     }
1056     if (!data.WriteParcelable(&options)) {
1057         ACCOUNT_LOGE("failed to write string for options");
1058         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1059     }
1060     if ((callback != nullptr) && (!data.WriteRemoteObject(callback->AsObject()))) {
1061         ACCOUNT_LOGE("failed to write remote object for callback");
1062         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1063     }
1064     MessageParcel reply;
1065     ErrCode result = SendRequest(AppAccountInterfaceCode::SET_AUTHENTICATOR_PROPERTIES, data, reply);
1066     if (result != ERR_OK) {
1067         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
1068         return result;
1069     }
1070     return reply.ReadInt32();
1071 }
1072 
ExecuteRequest(const AccountCapabilityRequest & request,const sptr<IAppAccountAuthorizationExtensionCallback> & callback)1073 ErrCode AppAccountProxy::ExecuteRequest(
1074     const AccountCapabilityRequest &request, const sptr<IAppAccountAuthorizationExtensionCallback> &callback)
1075 {
1076     MessageParcel data;
1077     if (!data.WriteInterfaceToken(GetDescriptor())) {
1078         ACCOUNT_LOGE("failed to write descriptor!");
1079         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1080     }
1081     if (!data.WriteParcelable(&request)) {
1082         ACCOUNT_LOGE("failed to write string for request");
1083         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1084     }
1085     if ((callback != nullptr) && (!data.WriteRemoteObject(callback->AsObject()))) {
1086         ACCOUNT_LOGE("failed to write remote object for callback");
1087         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1088     }
1089     MessageParcel reply;
1090     ErrCode result = SendRequest(AppAccountInterfaceCode::EXECUTE_REQUEST, data, reply);
1091     if (result != ERR_OK) {
1092         ACCOUNT_LOGE("failed to send request, errCode: %{public}d", result);
1093         return result;
1094     }
1095     if (!reply.ReadInt32(result)) {
1096         ACCOUNT_LOGE("failed to read result for check os account constraint enable.");
1097         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1098     }
1099     return result;
1100 }
1101 
SubscribeAppAccount(const AppAccountSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & eventListener)1102 ErrCode AppAccountProxy::SubscribeAppAccount(
1103     const AppAccountSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &eventListener)
1104 {
1105     MessageParcel data;
1106     MessageParcel reply;
1107 
1108     if (!data.WriteInterfaceToken(GetDescriptor())) {
1109         ACCOUNT_LOGE("failed to write descriptor!");
1110         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1111     }
1112 
1113     if (!data.WriteParcelable(&subscribeInfo)) {
1114         ACCOUNT_LOGE("failed to write parcelable for subscribeInfo");
1115         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1116     }
1117 
1118     if (!data.WriteRemoteObject(eventListener)) {
1119         ACCOUNT_LOGE("failed to write remote object for eventListener");
1120         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1121     }
1122 
1123     ErrCode result = SendRequest(AppAccountInterfaceCode::SUBSCRIBE_ACCOUNT, data, reply);
1124     if (result != ERR_OK) {
1125         return result;
1126     }
1127 
1128     result = reply.ReadInt32();
1129 
1130     return result;
1131 }
1132 
UnsubscribeAppAccount(const sptr<IRemoteObject> & eventListener)1133 ErrCode AppAccountProxy::UnsubscribeAppAccount(const sptr<IRemoteObject> &eventListener)
1134 {
1135     MessageParcel data;
1136     MessageParcel reply;
1137 
1138     if (!data.WriteInterfaceToken(GetDescriptor())) {
1139         ACCOUNT_LOGE("failed to write descriptor!");
1140         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1141     }
1142 
1143     if (!data.WriteRemoteObject(eventListener)) {
1144         ACCOUNT_LOGE("failed to write remote object for eventListener");
1145         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1146     }
1147 
1148     ErrCode result = SendRequest(AppAccountInterfaceCode::UNSUBSCRIBE_ACCOUNT, data, reply);
1149     if (result != ERR_OK) {
1150         return result;
1151     }
1152 
1153     result = reply.ReadInt32();
1154 
1155     return result;
1156 }
1157 
SendRequest(AppAccountInterfaceCode code,MessageParcel & data,MessageParcel & reply)1158 ErrCode AppAccountProxy::SendRequest(AppAccountInterfaceCode code, MessageParcel &data, MessageParcel &reply)
1159 {
1160     sptr<IRemoteObject> remote = Remote();
1161     if (remote == nullptr) {
1162         ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
1163         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
1164     }
1165 
1166     MessageOption option(MessageOption::TF_SYNC);
1167     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
1168     if (result != ERR_OK) {
1169         ACCOUNT_LOGE("failed to SendRequest, code = %{public}d, result = %{public}d", code, result);
1170         return ERR_APPACCOUNT_KIT_SEND_REQUEST;
1171     }
1172 
1173     return ERR_OK;
1174 }
1175 
1176 template<typename T>
WriteParcelableVector(const std::vector<T> & parcelableVector,MessageParcel & data)1177 bool AppAccountProxy::WriteParcelableVector(const std::vector<T> &parcelableVector, MessageParcel &data)
1178 {
1179     if (!data.WriteUint32(parcelableVector.size())) {
1180         ACCOUNT_LOGE("failed to WriteInt32 for parcelableVector.size()");
1181         return false;
1182     }
1183 
1184     for (const auto &parcelable : parcelableVector) {
1185         if (!data.WriteParcelable(&parcelable)) {
1186             ACCOUNT_LOGE("failed to WriteParcelable for parcelable");
1187             return false;
1188         }
1189     }
1190 
1191     return true;
1192 }
1193 
1194 template<typename T>
ReadParcelableVector(std::vector<T> & parcelableVector,MessageParcel & data)1195 bool AppAccountProxy::ReadParcelableVector(std::vector<T> &parcelableVector, MessageParcel &data)
1196 {
1197     uint32_t size = 0;
1198     if (!data.ReadUint32(size)) {
1199         ACCOUNT_LOGE("failed to ReadInt32 for size");
1200         return false;
1201     }
1202 
1203     parcelableVector.clear();
1204     for (uint32_t index = 0; index < size; index += 1) {
1205         std::shared_ptr<T> parcelable(data.ReadParcelable<T>());
1206         if (parcelable == nullptr) {
1207             ACCOUNT_LOGE("failed to ReadParcelable for T");
1208             return false;
1209         }
1210         parcelableVector.emplace_back(*parcelable);
1211     }
1212 
1213     return true;
1214 }
1215 }  // namespace AccountSA
1216 }  // namespace OHOS
1217