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