• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "os_account_proxy.h"
16 #include "account_log_wrapper.h"
17 
18 namespace OHOS {
19 namespace AccountSA {
20 namespace {
21 const uint32_t ACCOUNT_MAX_SIZE = 1000;
22 }
23 
OsAccountProxy(const sptr<IRemoteObject> & object)24 OsAccountProxy::OsAccountProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IOsAccount>(object)
25 {}
26 
~OsAccountProxy()27 OsAccountProxy::~OsAccountProxy()
28 {}
29 
CreateOsAccount(const std::string & name,const OsAccountType & type,OsAccountInfo & osAccountInfo)30 ErrCode OsAccountProxy::CreateOsAccount(
31     const std::string &name, const OsAccountType &type, OsAccountInfo &osAccountInfo)
32 {
33     MessageParcel data;
34     MessageParcel reply;
35 
36     if (!data.WriteInterfaceToken(GetDescriptor())) {
37         ACCOUNT_LOGE("failed to write descriptor!");
38         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
39     }
40 
41     if (!data.WriteString(name)) {
42         ACCOUNT_LOGE("failed to write os account name");
43         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
44     }
45 
46     if (!data.WriteInt32(static_cast<int32_t>(type))) {
47         ACCOUNT_LOGE("failed to write os account type");
48         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
49     }
50     ErrCode result = SendRequest(OsAccountInterfaceCode::CREATE_OS_ACCOUNT, data, reply);
51     if (result != ERR_OK) {
52         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
53         return result;
54     }
55 
56     result = reply.ReadInt32();
57     if (result != ERR_OK) {
58         ACCOUNT_LOGE("failed to read reply for create os account.");
59         return result;
60     }
61     if (!ReadOsAccountInfo(reply, osAccountInfo)) {
62         ACCOUNT_LOGE("Failed to read account info.");
63         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
64     }
65     return ERR_OK;
66 }
67 
CreateOsAccount(const std::string & localName,const std::string & shortName,const OsAccountType & type,OsAccountInfo & osAccountInfo,const CreateOsAccountOptions & options)68 ErrCode OsAccountProxy::CreateOsAccount(const std::string &localName, const std::string &shortName,
69     const OsAccountType &type, OsAccountInfo &osAccountInfo, const CreateOsAccountOptions &options)
70 {
71     MessageParcel data;
72 
73     if (!data.WriteInterfaceToken(GetDescriptor())) {
74         ACCOUNT_LOGE("failed to write descriptor!");
75         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
76     }
77 
78     if (!data.WriteString(localName)) {
79         ACCOUNT_LOGE("failed to write os account local name");
80         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
81     }
82 
83     if (!data.WriteBool(options.hasShortName)) {
84         ACCOUNT_LOGE("failed to write hasShortName");
85         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
86     }
87 
88     if (options.hasShortName && !data.WriteString(shortName)) {
89         ACCOUNT_LOGE("failed to write os account short name");
90         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
91     }
92 
93     if (!data.WriteInt32(static_cast<int32_t>(type))) {
94         ACCOUNT_LOGE("failed to write os account type");
95         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
96     }
97 
98     if (!data.WriteParcelable(&options)) {
99         ACCOUNT_LOGE("failed to write options");
100         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
101     }
102 
103     MessageParcel reply;
104     ErrCode result = SendRequest(OsAccountInterfaceCode::CREATE_OS_ACCOUNT_WITH_SHORT_NAME, data, reply);
105     if (result != ERR_OK) {
106         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
107         return result;
108     }
109 
110     result = reply.ReadInt32();
111     if (result != ERR_OK) {
112         ACCOUNT_LOGE("failed to read reply for create os account.");
113         return result;
114     }
115     if (!ReadOsAccountInfo(reply, osAccountInfo)) {
116         ACCOUNT_LOGE("Failed to read account info.");
117         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
118     }
119     return ERR_OK;
120 }
121 
CreateOsAccountWithFullInfo(OsAccountInfo & osAccountInfo,const CreateOsAccountOptions & options)122 ErrCode OsAccountProxy::CreateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo, const CreateOsAccountOptions &options)
123 {
124     MessageParcel data;
125     if (!data.WriteInterfaceToken(GetDescriptor())) {
126         ACCOUNT_LOGE("failed to write descriptor!");
127         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
128     }
129 
130     if (!data.WriteParcelable(&osAccountInfo)) {
131         ACCOUNT_LOGE("failed to write osAccountInfo info ");
132         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
133     }
134 
135     if (!data.WriteParcelable(&options)) {
136         ACCOUNT_LOGE("failed to write options");
137         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
138     }
139 
140     MessageParcel reply;
141     ErrCode result = SendRequest(OsAccountInterfaceCode::CREATE_OS_ACCOUNT_WITH_FULL_INFO, data, reply);
142     if (result != ERR_OK) {
143         return result;
144     }
145 
146     result = reply.ReadInt32();
147     if (result != ERR_OK) {
148         ACCOUNT_LOGE("failed to read reply for create os account with full user info, result %{public}d.", result);
149         return result;
150     }
151     return ERR_OK;
152 }
153 
UpdateOsAccountWithFullInfo(OsAccountInfo & osAccountInfo)154 ErrCode OsAccountProxy::UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo)
155 {
156     MessageParcel data;
157     if (!data.WriteInterfaceToken(GetDescriptor())) {
158         ACCOUNT_LOGE("failed to write descriptor!");
159         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
160     }
161 
162     if (!data.WriteParcelable(&osAccountInfo)) {
163         ACCOUNT_LOGE("failed to write osAccountInfo info ");
164         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
165     }
166 
167     MessageParcel reply;
168     ErrCode result = SendRequest(OsAccountInterfaceCode::UPDATE_OS_ACCOUNT_WITH_FULL_INFO, data, reply);
169     if (result != ERR_OK) {
170         return result;
171     }
172 
173     result = reply.ReadInt32();
174     if (result != ERR_OK) {
175         ACCOUNT_LOGE("failed to read reply for update os account with full user info, result %{public}d.", result);
176         return result;
177     }
178     return ERR_OK;
179 }
180 
CreateOsAccountForDomain(const OsAccountType & type,const DomainAccountInfo & domainInfo,const sptr<IDomainAccountCallback> & callback,const CreateOsAccountForDomainOptions & options)181 ErrCode OsAccountProxy::CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo,
182     const sptr<IDomainAccountCallback> &callback, const CreateOsAccountForDomainOptions& options)
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.WriteInt32(static_cast<int32_t>(type))) {
193         ACCOUNT_LOGE("Failed to write type ");
194         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
195     }
196 
197     if (!data.WriteParcelable(&domainInfo)) {
198         ACCOUNT_LOGE("Fail to write domainInfo");
199         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
200     }
201 
202     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
203         ACCOUNT_LOGE("Fail to write callback");
204         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
205     }
206 
207     if (!data.WriteParcelable(&options)) {
208         ACCOUNT_LOGE("Failed to write options");
209         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
210     }
211 
212     ErrCode result = SendRequest(OsAccountInterfaceCode::CREATE_OS_ACCOUNT_FOR_DOMAIN, data, reply);
213     if (result != ERR_OK) {
214         ACCOUNT_LOGE("Failed to send request, result %{public}d.", result);
215         return result;
216     }
217 
218     result = reply.ReadInt32();
219     if (result != ERR_OK) {
220         ACCOUNT_LOGE("Failed to read reply for create os account for domain, result %{public}d.", result);
221         return result;
222     }
223     return ERR_OK;
224 }
225 
RemoveOsAccount(const int id)226 ErrCode OsAccountProxy::RemoveOsAccount(const int id)
227 {
228     MessageParcel reply;
229     return SendRequestWithAccountId(OsAccountInterfaceCode::REMOVE_OS_ACCOUNT, reply, id);
230 }
231 
IsOsAccountExists(const int id,bool & isOsAccountExists)232 ErrCode OsAccountProxy::IsOsAccountExists(const int id, bool &isOsAccountExists)
233 {
234     MessageParcel reply;
235     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::IS_OS_ACCOUNT_EXISTS, reply, id);
236     if (result == ERR_OK) {
237         isOsAccountExists = reply.ReadBool();
238     }
239     return result;
240 }
241 
IsOsAccountActived(const int id,bool & isOsAccountActived)242 ErrCode OsAccountProxy::IsOsAccountActived(const int id, bool &isOsAccountActived)
243 {
244     MessageParcel reply;
245     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::IS_OS_ACCOUNT_ACTIVED, reply, id);
246     if (result == ERR_OK) {
247         isOsAccountActived = reply.ReadBool();
248     }
249     return result;
250 }
251 
CheckOsAccountConstraintEnabled(OsAccountInterfaceCode code,const int id,const std::string & constraint,bool & isEnabled)252 ErrCode OsAccountProxy::CheckOsAccountConstraintEnabled(
253     OsAccountInterfaceCode code, const int id, const std::string &constraint, bool &isEnabled)
254 {
255     MessageParcel data;
256     if (!data.WriteInterfaceToken(GetDescriptor())) {
257         ACCOUNT_LOGE("failed to write descriptor!");
258         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
259     }
260     if (!data.WriteInt32(id)) {
261         ACCOUNT_LOGE("failed to write int for id");
262         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
263     }
264     if (!data.WriteString(constraint)) {
265         ACCOUNT_LOGE("failed to write string for constraint");
266         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
267     }
268     MessageParcel reply;
269     ErrCode ret = SendRequest(code, data, reply);
270     if (ret != ERR_OK) {
271         ACCOUNT_LOGE("SendRequest err, result %{public}d.", ret);
272         return ret;
273     }
274     if (!reply.ReadInt32(ret)) {
275         ACCOUNT_LOGE("failed to read result for check os account constraint enable.");
276         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
277     }
278     if (ret != ERR_OK) {
279         ACCOUNT_LOGE("failed to check os account constraint enabled, result %{public}d.", ret);
280         return ret;
281     }
282     if (!reply.ReadBool(isEnabled)) {
283         ACCOUNT_LOGE("failed to read result for check os account constraint enable.");
284         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
285     }
286     return ERR_OK;
287 }
288 
IsOsAccountConstraintEnable(const int id,const std::string & constraint,bool & isConstraintEnable)289 ErrCode OsAccountProxy::IsOsAccountConstraintEnable(
290     const int id, const std::string &constraint, bool &isConstraintEnable)
291 {
292     return CheckOsAccountConstraintEnabled(
293         OsAccountInterfaceCode::IS_OS_ACCOUNT_CONSTRAINT_ENABLE, id, constraint, isConstraintEnable);
294 }
295 
CheckOsAccountConstraintEnabled(const int id,const std::string & constraint,bool & isEnabled)296 ErrCode OsAccountProxy::CheckOsAccountConstraintEnabled(
297     const int id, const std::string &constraint, bool &isEnabled)
298 {
299     return CheckOsAccountConstraintEnabled(
300         OsAccountInterfaceCode::CHECK_OS_ACCOUNT_CONSTRAINT_ENABLED, id, constraint, isEnabled);
301 }
302 
IsOsAccountVerified(const int id,bool & isVerified)303 ErrCode OsAccountProxy::IsOsAccountVerified(const int id, bool &isVerified)
304 {
305     MessageParcel reply;
306     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::IS_OS_ACCOUNT_VERIFIED, reply, id);
307     if (result == ERR_OK) {
308         isVerified = reply.ReadBool();
309     }
310     return result;
311 }
312 
GetCreatedOsAccountsCount(unsigned int & osAccountsCount)313 ErrCode OsAccountProxy::GetCreatedOsAccountsCount(unsigned int &osAccountsCount)
314 {
315     osAccountsCount = 0;
316     MessageParcel data;
317     MessageParcel reply;
318 
319     if (!data.WriteInterfaceToken(GetDescriptor())) {
320         ACCOUNT_LOGE("failed to write descriptor!");
321         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
322     }
323 
324     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_CREATED_OS_ACCOUNT_COUNT, data, reply);
325     if (result != ERR_OK) {
326         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
327         return result;
328     }
329     result = reply.ReadInt32();
330     if (result != ERR_OK) {
331         ACCOUNT_LOGE("failed to read reply for get os account count, result %{public}d.", result);
332         return result;
333     }
334     osAccountsCount = reply.ReadUint32();
335 
336     return ERR_OK;
337 }
338 
GetOsAccountLocalIdFromProcess(int & id)339 ErrCode OsAccountProxy::GetOsAccountLocalIdFromProcess(int &id)
340 {
341     MessageParcel data;
342     MessageParcel reply;
343 
344     if (!data.WriteInterfaceToken(GetDescriptor())) {
345         ACCOUNT_LOGE("failed to write descriptor!");
346         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
347     }
348 
349     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_LOCAL_ID_FROM_PROCESS, data, reply);
350     if (result != ERR_OK) {
351         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
352         return result;
353     }
354     result = reply.ReadInt32();
355     if (result != ERR_OK) {
356         ACCOUNT_LOGE("failed to read reply for get os account id from process, result %{public}d.", result);
357         return result;
358     }
359     id = reply.ReadInt32();
360 
361     return ERR_OK;
362 }
363 
IsMainOsAccount(bool & isMainOsAccount)364 ErrCode OsAccountProxy::IsMainOsAccount(bool &isMainOsAccount)
365 {
366     MessageParcel data;
367     MessageParcel reply;
368 
369     if (!data.WriteInterfaceToken(GetDescriptor())) {
370         ACCOUNT_LOGE("failed to write descriptor!");
371         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
372     }
373 
374     ErrCode result = SendRequest(OsAccountInterfaceCode::IS_MAIN_OS_ACCOUNT, data, reply);
375     if (result != ERR_OK) {
376         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
377         return result;
378     }
379     result = reply.ReadInt32();
380     if (result != ERR_OK) {
381         ACCOUNT_LOGE("failed to read reply for is main os account, result %{public}d.", result);
382         return result;
383     }
384     isMainOsAccount = reply.ReadBool();
385 
386     return ERR_OK;
387 }
388 
GetOsAccountLocalIdFromDomain(const DomainAccountInfo & domainInfo,int & id)389 ErrCode OsAccountProxy::GetOsAccountLocalIdFromDomain(const DomainAccountInfo &domainInfo, int &id)
390 {
391     MessageParcel data;
392     MessageParcel reply;
393 
394     if (!data.WriteInterfaceToken(GetDescriptor())) {
395         ACCOUNT_LOGE("failed to write descriptor!");
396         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
397     }
398 
399     if (!data.WriteString(domainInfo.domain_)) {
400         ACCOUNT_LOGE("failed to write int for domain.");
401         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
402     }
403     if (!data.WriteString(domainInfo.accountName_)) {
404         ACCOUNT_LOGE("failed to write int for domain account name.");
405         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
406     }
407     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_LOCAL_ID_FROM_DOMAIN, data, reply);
408     if (result != ERR_OK) {
409         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
410         return result;
411     }
412     result = reply.ReadInt32();
413     if (result != ERR_OK) {
414         ACCOUNT_LOGE("read from reply err, result %{public}d.", result);
415         return result;
416     }
417     id = reply.ReadInt32();
418 
419     return ERR_OK;
420 }
421 
QueryMaxOsAccountNumber(uint32_t & maxOsAccountNumber)422 ErrCode OsAccountProxy::QueryMaxOsAccountNumber(uint32_t &maxOsAccountNumber)
423 {
424     MessageParcel data;
425     MessageParcel reply;
426 
427     if (!data.WriteInterfaceToken(GetDescriptor())) {
428         ACCOUNT_LOGE("failed to write descriptor!");
429         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
430     }
431 
432     ErrCode result = SendRequest(OsAccountInterfaceCode::QUERY_MAX_OS_ACCOUNT_NUMBER, data, reply);
433     if (result != ERR_OK) {
434         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
435         return result;
436     }
437     result = reply.ReadInt32();
438     if (result != ERR_OK) {
439         ACCOUNT_LOGE("failed to read reply for query os account number, result %{public}d.", result);
440         return result;
441     }
442     maxOsAccountNumber = static_cast<uint32_t>(reply.ReadInt32());
443 
444     return ERR_OK;
445 }
446 
QueryMaxLoggedInOsAccountNumber(uint32_t & maxNum)447 ErrCode OsAccountProxy::QueryMaxLoggedInOsAccountNumber(uint32_t &maxNum)
448 {
449     MessageParcel data;
450     if (!data.WriteInterfaceToken(GetDescriptor())) {
451         ACCOUNT_LOGE("Failed to write descriptor!");
452         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
453     }
454 
455     MessageParcel reply;
456     ErrCode result = SendRequest(OsAccountInterfaceCode::QUERY_MAX_LOGGED_IN_OS_ACCOUNT_NUMBER, data, reply);
457     if (result != ERR_OK) {
458         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
459         return result;
460     }
461     if (!reply.ReadInt32(result)) {
462         ACCOUNT_LOGE("Failed to read errCode");
463         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
464     }
465     if (result != ERR_OK) {
466         return result;
467     }
468     if (!reply.ReadUint32(maxNum)) {
469         ACCOUNT_LOGE("Failed to read maxNum");
470         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
471     }
472     return ERR_OK;
473 }
474 
GetOsAccountAllConstraints(const int id,std::vector<std::string> & constraints)475 ErrCode OsAccountProxy::GetOsAccountAllConstraints(const int id, std::vector<std::string> &constraints)
476 {
477     MessageParcel reply;
478     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::GET_OS_ACCOUNT_ALL_CONSTRAINTS, reply, id);
479     if (result != ERR_OK) {
480         return result;
481     }
482     bool readFlag = reply.ReadStringVector(&constraints);
483     if (!readFlag) {
484         ACCOUNT_LOGE("ReadStringVector failed, result %{public}d.", result);
485         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
486     }
487     return ERR_OK;
488 }
489 
QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> & osAccountInfos)490 ErrCode OsAccountProxy::QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &osAccountInfos)
491 {
492     MessageParcel data;
493     MessageParcel reply;
494 
495     if (!data.WriteInterfaceToken(GetDescriptor())) {
496         ACCOUNT_LOGE("failed to write descriptor!");
497         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
498     }
499 
500     ErrCode result = SendRequest(OsAccountInterfaceCode::QUERY_ALL_CREATED_OS_ACCOUNTS, data, reply);
501     if (result != ERR_OK) {
502         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
503         return result;
504     }
505     result = reply.ReadInt32();
506     if (result != ERR_OK) {
507         ACCOUNT_LOGE("failed to read reply for query all os accounts, result %{public}d.", result);
508         return result;
509     }
510     ReadOsAccountInfoList(reply, osAccountInfos);
511 
512     return ERR_OK;
513 }
514 
QueryCurrentOsAccount(OsAccountInfo & osAccountInfo)515 ErrCode OsAccountProxy::QueryCurrentOsAccount(OsAccountInfo &osAccountInfo)
516 {
517     MessageParcel data;
518     MessageParcel reply;
519 
520     if (!data.WriteInterfaceToken(GetDescriptor())) {
521         ACCOUNT_LOGE("failed to write descriptor!");
522         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
523     }
524 
525     ErrCode result = SendRequest(OsAccountInterfaceCode::QUERY_CURRENT_OS_ACCOUNT, data, reply);
526     if (result != ERR_OK) {
527         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
528         return result;
529     }
530     result = reply.ReadInt32();
531     if (result != ERR_OK) {
532         ACCOUNT_LOGE("failed to read reply for query current os account, result %{public}d.", result);
533         return result;
534     }
535     if (!ReadOsAccountInfo(reply, osAccountInfo)) {
536         ACCOUNT_LOGE("Failed to read account info.");
537         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
538     }
539     return ERR_OK;
540 }
541 
QueryOsAccountById(const int id,OsAccountInfo & osAccountInfo)542 ErrCode OsAccountProxy::QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo)
543 {
544     MessageParcel reply;
545     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::QUERY_OS_ACCOUNT_BY_ID, reply, id);
546     if (result != ERR_OK) {
547         return result;
548     }
549     if (!ReadOsAccountInfo(reply, osAccountInfo)) {
550         ACCOUNT_LOGE("Failed to read account info.");
551         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
552     }
553 
554     return ERR_OK;
555 }
556 
GetOsAccountTypeFromProcess(OsAccountType & type)557 ErrCode OsAccountProxy::GetOsAccountTypeFromProcess(OsAccountType &type)
558 {
559     MessageParcel data;
560     MessageParcel reply;
561 
562     if (!data.WriteInterfaceToken(GetDescriptor())) {
563         ACCOUNT_LOGE("failed to write descriptor!");
564         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
565     }
566 
567     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_TYPE_FROM_PROCESS, data, reply);
568     if (result != ERR_OK) {
569         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
570         return result;
571     }
572     result = reply.ReadInt32();
573     if (result != ERR_OK) {
574         ACCOUNT_LOGE("failed to read reply for get os account type by process, result %{public}d.", result);
575         return result;
576     }
577     type = static_cast<OsAccountType>(reply.ReadInt32());
578 
579     return ERR_OK;
580 }
581 
GetOsAccountType(const int id,OsAccountType & type)582 ErrCode OsAccountProxy::GetOsAccountType(const int id, OsAccountType& type)
583 {
584     MessageParcel reply;
585     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::GET_OS_ACCOUNT_TYPE, reply, id);
586     if (result != ERR_OK) {
587         return result;
588     }
589 
590     int32_t typeResult = 0;
591     if (!reply.ReadInt32(typeResult)) {
592         ACCOUNT_LOGE("Failed to read type.");
593         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
594     }
595     type = static_cast<OsAccountType>(typeResult);
596 
597     return ERR_OK;
598 }
599 
GetOsAccountProfilePhoto(const int id,std::string & photo)600 ErrCode OsAccountProxy::GetOsAccountProfilePhoto(const int id, std::string &photo)
601 {
602     MessageParcel reply;
603     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::GET_OS_ACCOUNT_PROFILE_PHOTO, reply, id);
604     if (result != ERR_OK) {
605         return result;
606     }
607 
608     int32_t photoSize;
609     if (!reply.ReadInt32(photoSize)) {
610         ACCOUNT_LOGE("Failed to read photoSize");
611         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
612     }
613 
614     if (photoSize - 1 > static_cast<int32_t>(Constants::LOCAL_PHOTO_MAX_SIZE) || photoSize < 1) {
615         ACCOUNT_LOGE("PhotoSize is invalid, photosize = %{public}d", photoSize);
616         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
617     }
618     auto readRawData = reply.ReadRawData(photoSize);
619     if (readRawData == nullptr) {
620         ACCOUNT_LOGE("Failed to read photoData, id=%{public}d", id);
621         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
622     }
623     const char *photoData = reinterpret_cast<const char *>(readRawData);
624     photo = std::string(photoData, photoSize - 1);
625 
626     return ERR_OK;
627 }
628 
IsMultiOsAccountEnable(bool & isMultiOsAccountEnable)629 ErrCode OsAccountProxy::IsMultiOsAccountEnable(bool &isMultiOsAccountEnable)
630 {
631     MessageParcel data;
632     MessageParcel reply;
633 
634     if (!data.WriteInterfaceToken(GetDescriptor())) {
635         ACCOUNT_LOGE("failed to write descriptor!");
636         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
637     }
638 
639     ErrCode result = SendRequest(OsAccountInterfaceCode::IS_MULTI_OS_ACCOUNT_ENABLE, data, reply);
640     if (result != ERR_OK) {
641         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
642         return result;
643     }
644     result = reply.ReadInt32();
645     if (result != ERR_OK) {
646         ACCOUNT_LOGE("failed to read reply for is multi os account enable.");
647         return result;
648     }
649     isMultiOsAccountEnable = reply.ReadBool();
650 
651     return ERR_OK;
652 }
653 
SetOsAccountName(const int id,const std::string & name)654 ErrCode OsAccountProxy::SetOsAccountName(const int id, const std::string &name)
655 {
656     MessageParcel data;
657     if (!data.WriteInterfaceToken(GetDescriptor())) {
658         ACCOUNT_LOGE("failed to write descriptor!");
659         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
660     }
661     if (!data.WriteInt32(id)) {
662         ACCOUNT_LOGE("failed to write int for id %{public}d.", id);
663         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
664     }
665     if (!data.WriteString(name)) {
666         ACCOUNT_LOGE("failed to write string for name");
667         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
668     }
669 
670     MessageParcel reply;
671     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_OS_ACCOUNT_NAME, data, reply);
672     if (result != ERR_OK) {
673         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
674         return result;
675     }
676     result = reply.ReadInt32();
677     if (result != ERR_OK) {
678         ACCOUNT_LOGE("failed to read reply for set os account name, result %{public}d.", result);
679         return result;
680     }
681 
682     return ERR_OK;
683 }
684 
SetOsAccountConstraints(const int id,const std::vector<std::string> & constraints,const bool enable)685 ErrCode OsAccountProxy::SetOsAccountConstraints(
686     const int id, const std::vector<std::string> &constraints, const bool enable)
687 {
688     MessageParcel data;
689     if (!data.WriteInterfaceToken(GetDescriptor())) {
690         ACCOUNT_LOGE("failed to write descriptor!");
691         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
692     }
693     if (!data.WriteInt32(id)) {
694         ACCOUNT_LOGE("failed to write id for setting constraints");
695         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
696     }
697     if (!data.WriteStringVector(constraints)) {
698         ACCOUNT_LOGE("failed to write stringVector for constraints");
699         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
700     }
701     if (!data.WriteBool(enable)) {
702         ACCOUNT_LOGE("failed to write bool for enable");
703         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
704     }
705 
706     MessageParcel reply;
707     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_OS_ACCOUNT_CONSTRAINTS, data, reply);
708     if (result != ERR_OK) {
709         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
710         return result;
711     }
712     result = reply.ReadInt32();
713     if (result != ERR_OK) {
714         ACCOUNT_LOGE("failed to read reply for set os account constraints, result %{public}d.", result);
715         return result;
716     }
717 
718     return ERR_OK;
719 }
720 
SetOsAccountProfilePhoto(const int id,const std::string & photo)721 ErrCode OsAccountProxy::SetOsAccountProfilePhoto(const int id, const std::string &photo)
722 {
723     MessageParcel data;
724     if (!data.WriteInterfaceToken(GetDescriptor())) {
725         ACCOUNT_LOGE("failed to write descriptor!");
726         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
727     }
728     if (!data.WriteInt32(id)) {
729         ACCOUNT_LOGE("failed to write id for setting photo");
730         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
731     }
732     if (!data.WriteInt32(photo.size() + 1)) {
733         ACCOUNT_LOGE("Failed to write photo size");
734         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
735     }
736     if (!data.WriteRawData(photo.c_str(), photo.size() + 1)) {
737         ACCOUNT_LOGE("Failed to write string for photo");
738         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
739     }
740 
741     MessageParcel reply;
742     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_OS_ACCOUNT_PROFILE_PHOTO, data, reply);
743     if (result != ERR_OK) {
744         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
745         return result;
746     }
747     result = reply.ReadInt32();
748     if (result != ERR_OK) {
749         ACCOUNT_LOGE("failed to read reply for set os account profile photo, result %{public}d.", result);
750         return result;
751     }
752 
753     return ERR_OK;
754 }
755 
ActivateOsAccount(const int id)756 ErrCode OsAccountProxy::ActivateOsAccount(const int id)
757 {
758     MessageParcel reply;
759     return SendRequestWithAccountId(OsAccountInterfaceCode::ACTIVATE_OS_ACCOUNT, reply, id);
760 }
761 
DeactivateOsAccount(const int id)762 ErrCode OsAccountProxy::DeactivateOsAccount(const int id)
763 {
764     MessageParcel reply;
765     return SendRequestWithAccountId(OsAccountInterfaceCode::DEACTIVATE_OS_ACCOUNT, reply, id);
766 }
767 
DeactivateAllOsAccounts()768 ErrCode OsAccountProxy::DeactivateAllOsAccounts()
769 {
770     MessageParcel data;
771     MessageParcel reply;
772     if (!data.WriteInterfaceToken(GetDescriptor())) {
773         ACCOUNT_LOGE("Write descriptor failed.");
774         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
775     }
776     ErrCode result = SendRequest(OsAccountInterfaceCode::DEACTIVATE_ALL_OS_ACCOUNTS, data, reply);
777     if (result != ERR_OK) {
778         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
779         return result;
780     }
781 
782     if (!reply.ReadInt32(result)) {
783         ACCOUNT_LOGE("Read result failed.");
784         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
785     }
786     if (result != ERR_OK) {
787         ACCOUNT_LOGE("Deactivate all os account failed, result=%{public}d.", result);
788     }
789     return result;
790 }
791 
StartOsAccount(const int id)792 ErrCode OsAccountProxy::StartOsAccount(const int id)
793 {
794     MessageParcel reply;
795     return SendRequestWithAccountId(OsAccountInterfaceCode::START_OS_ACCOUNT, reply, id);
796 }
797 
GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber,int & id)798 ErrCode OsAccountProxy::GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber, int &id)
799 {
800     MessageParcel data;
801     MessageParcel reply;
802 
803     if (!data.WriteInterfaceToken(GetDescriptor())) {
804         ACCOUNT_LOGE("failed to write descriptor!");
805         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
806     }
807 
808     if (!data.WriteInt64(serialNumber)) {
809         ACCOUNT_LOGE("failed to write int for serialNumber");
810         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
811     }
812     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_LOCAL_ID_FOR_SERIAL_NUMBER, data, reply);
813     if (result != ERR_OK) {
814         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
815         return result;
816     }
817     result = reply.ReadInt32();
818     if (result != ERR_OK) {
819         ACCOUNT_LOGE("failed to read reply for get os account id by serial number, result %{public}d.", result);
820         return result;
821     }
822     id = reply.ReadInt32();
823 
824     return ERR_OK;
825 }
826 
GetSerialNumberByOsAccountLocalId(const int & id,int64_t & serialNumber)827 ErrCode OsAccountProxy::GetSerialNumberByOsAccountLocalId(const int &id, int64_t &serialNumber)
828 {
829     MessageParcel reply;
830     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::GET_SERIAL_NUMBER_FOR_OS_ACCOUNT, reply, id);
831     if (result == ERR_OK) {
832         serialNumber = reply.ReadInt64();
833     }
834     return result;
835 }
836 
SubscribeOsAccount(const OsAccountSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & eventListener)837 ErrCode OsAccountProxy::SubscribeOsAccount(
838     const OsAccountSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &eventListener)
839 {
840     MessageParcel data;
841     MessageParcel reply;
842 
843     if (!data.WriteInterfaceToken(GetDescriptor())) {
844         ACCOUNT_LOGE("failed to write descriptor!");
845         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
846     }
847 
848     if (!data.WriteParcelable(&subscribeInfo)) {
849         ACCOUNT_LOGE("failed to write parcelable for subscribeInfo");
850         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
851     }
852 
853     if (!data.WriteRemoteObject(eventListener)) {
854         ACCOUNT_LOGE("failed to write remote object for eventListener");
855         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
856     }
857 
858     ErrCode result = SendRequest(OsAccountInterfaceCode::SUBSCRIBE_OS_ACCOUNT, data, reply);
859     if (result != ERR_OK) {
860         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
861         return result;
862     }
863 
864     result = reply.ReadInt32();
865     if (result != ERR_OK) {
866         ACCOUNT_LOGE("failed to read reply for subscriber os account, result %{public}d.", result);
867         return result;
868     }
869 
870     return ERR_OK;
871 }
872 
UnsubscribeOsAccount(const sptr<IRemoteObject> & eventListener)873 ErrCode OsAccountProxy::UnsubscribeOsAccount(const sptr<IRemoteObject> &eventListener)
874 {
875     MessageParcel data;
876     MessageParcel reply;
877 
878     if (!data.WriteInterfaceToken(GetDescriptor())) {
879         ACCOUNT_LOGE("failed to write descriptor!");
880         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
881     }
882 
883     if (!data.WriteRemoteObject(eventListener)) {
884         ACCOUNT_LOGE("failed to write remote object for eventListener");
885         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
886     }
887 
888     ErrCode result = SendRequest(OsAccountInterfaceCode::UNSUBSCRIBE_OS_ACCOUNT, data, reply);
889     if (result != ERR_OK) {
890         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
891         return result;
892     }
893 
894     result = reply.ReadInt32();
895     if (result != ERR_OK) {
896         ACCOUNT_LOGE("failed to read reply for unsubscribe os account.");
897     }
898 
899     return result;
900 }
GetOsAccountSwitchMod()901 OS_ACCOUNT_SWITCH_MOD OsAccountProxy::GetOsAccountSwitchMod()
902 {
903     MessageParcel data;
904     MessageParcel reply;
905 
906     if (!data.WriteInterfaceToken(GetDescriptor())) {
907         ACCOUNT_LOGE("failed to write descriptor!");
908         return OS_ACCOUNT_SWITCH_MOD::ERROR_MOD;
909     }
910 
911     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_SWITCH_MOD, data, reply);
912     if (result != ERR_OK) {
913         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
914         return OS_ACCOUNT_SWITCH_MOD::ERROR_MOD;
915     }
916 
917     OS_ACCOUNT_SWITCH_MOD osAccountSwitchMod = static_cast<OS_ACCOUNT_SWITCH_MOD>(reply.ReadInt32());
918 
919     return osAccountSwitchMod;
920 }
921 
SendRequestWithAccountId(OsAccountInterfaceCode code,MessageParcel & reply,int id)922 ErrCode OsAccountProxy::SendRequestWithAccountId(OsAccountInterfaceCode code, MessageParcel &reply, int id)
923 {
924     MessageParcel data;
925     if (!data.WriteInterfaceToken(GetDescriptor())) {
926         ACCOUNT_LOGE("failed to write descriptor!");
927         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
928     }
929 
930     if (!data.WriteInt32(id)) {
931         ACCOUNT_LOGE("failed to write int for id");
932         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
933     }
934 
935     ErrCode result = SendRequest(code, data, reply);
936     if (result != ERR_OK) {
937         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
938         return result;
939     }
940     if (!reply.ReadInt32(result)) {
941         ACCOUNT_LOGE("failed to read result for Message code %{public}d.", code);
942         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
943     }
944     if (result != ERR_OK) {
945         ACCOUNT_LOGE("failed to read reply for code %{public}d, result %{public}d.", code, result);
946     }
947     return result;
948 }
949 
SendRequest(OsAccountInterfaceCode code,MessageParcel & data,MessageParcel & reply)950 ErrCode OsAccountProxy::SendRequest(OsAccountInterfaceCode code, MessageParcel &data, MessageParcel &reply)
951 {
952     sptr<IRemoteObject> remote = Remote();
953     if (remote == nullptr) {
954         ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
955         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
956     }
957 
958     MessageOption option(MessageOption::TF_SYNC);
959     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
960     if (result != ERR_OK) {
961         ACCOUNT_LOGE("failed to send os account request, code = %{public}d, result = %{public}d", code, result);
962     }
963     return result;
964 }
965 
IsCurrentOsAccountVerified(bool & isVerified)966 ErrCode OsAccountProxy::IsCurrentOsAccountVerified(bool &isVerified)
967 {
968     MessageParcel data;
969     MessageParcel reply;
970 
971     if (!data.WriteInterfaceToken(GetDescriptor())) {
972         ACCOUNT_LOGE("failed to write descriptor!");
973         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
974     }
975 
976     ErrCode result = SendRequest(OsAccountInterfaceCode::IS_CURRENT_OS_ACCOUNT_VERIFIED, data, reply);
977     if (result != ERR_OK) {
978         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
979         return result;
980     }
981 
982     result = reply.ReadInt32();
983     if (result != ERR_OK) {
984         ACCOUNT_LOGE("failed to read reply for is current os account verified, result %{public}d.", result);
985         return result;
986     }
987     isVerified = reply.ReadBool();
988 
989     return ERR_OK;
990 }
991 
IsOsAccountCompleted(const int id,bool & isOsAccountCompleted)992 ErrCode OsAccountProxy::IsOsAccountCompleted(const int id, bool &isOsAccountCompleted)
993 {
994     MessageParcel reply;
995     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::IS_OS_ACCOUNT_COMPLETED, reply, id);
996     if (result != ERR_OK) {
997         return result;
998     }
999     if (!reply.ReadBool(isOsAccountCompleted)) {
1000         ACCOUNT_LOGE("failed to read isOsAccountCompleted");
1001         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1002     }
1003     return ERR_OK;
1004 }
1005 
SetCurrentOsAccountIsVerified(const bool isVerified)1006 ErrCode OsAccountProxy::SetCurrentOsAccountIsVerified(const bool isVerified)
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     if (!data.WriteBool(isVerified)) {
1017         ACCOUNT_LOGE("failed to write bool for isVerified");
1018         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1019     }
1020     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_CURRENT_OS_ACCOUNT_IS_VERIFIED, data, reply);
1021     if (result != ERR_OK) {
1022         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1023         return result;
1024     }
1025 
1026     result = reply.ReadInt32();
1027     if (result != ERR_OK) {
1028         ACCOUNT_LOGE("failed to read reply for set current os account verified, result %{public}d.", result);
1029         return result;
1030     }
1031     return ERR_OK;
1032 }
1033 
SetOsAccountIsVerified(const int id,const bool isVerified)1034 ErrCode OsAccountProxy::SetOsAccountIsVerified(const int id, const bool isVerified)
1035 {
1036     MessageParcel data;
1037     if (!data.WriteInterfaceToken(GetDescriptor())) {
1038         ACCOUNT_LOGE("failed to write descriptor!");
1039         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1040     }
1041     if (!data.WriteInt32(id)) {
1042         ACCOUNT_LOGE("failed to write id for setting verified status");
1043         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1044     }
1045     if (!data.WriteBool(isVerified)) {
1046         ACCOUNT_LOGE("failed to write bool for isVerified");
1047         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1048     }
1049 
1050     MessageParcel reply;
1051     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_OS_ACCOUNT_IS_VERIFIED, data, reply);
1052     if (result != ERR_OK) {
1053         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1054         return result;
1055     }
1056 
1057     result = reply.ReadInt32();
1058     if (result != ERR_OK) {
1059         ACCOUNT_LOGE("failed to read reply for set os account verified, result %{public}d.", result);
1060         return result;
1061     }
1062     return ERR_OK;
1063 }
1064 
DumpState(const int & id,std::vector<std::string> & state)1065 ErrCode OsAccountProxy::DumpState(const int &id, std::vector<std::string> &state)
1066 {
1067     MessageParcel reply;
1068     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::DUMP_STATE, reply, id);
1069     if (result != ERR_OK) {
1070         return result;
1071     }
1072 
1073     uint32_t size = reply.ReadUint32();
1074     for (uint32_t i = 0; i < size; i++) {
1075         std::string info = reply.ReadString();
1076         state.emplace_back(info);
1077     }
1078     return ERR_OK;
1079 }
1080 
GetCreatedOsAccountNumFromDatabase(const std::string & storeID,int & createdOsAccountNum)1081 ErrCode OsAccountProxy::GetCreatedOsAccountNumFromDatabase(const std::string& storeID,
1082     int &createdOsAccountNum)
1083 {
1084     MessageParcel data;
1085     MessageParcel reply;
1086 
1087     if (!data.WriteInterfaceToken(GetDescriptor())) {
1088         ACCOUNT_LOGE("failed to write descriptor!");
1089         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1090     }
1091 
1092     if (!data.WriteString(storeID)) {
1093         ACCOUNT_LOGE("failed to write storeID for getting created os account");
1094         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1095     }
1096     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_CREATED_OS_ACCOUNT_NUM_FROM_DATABASE, data, reply);
1097     if (result != ERR_OK) {
1098         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1099         return result;
1100     }
1101 
1102     result = reply.ReadInt32();
1103     if (result != ERR_OK) {
1104         ACCOUNT_LOGE("failed to read reply, result %{public}d.", result);
1105         return result;
1106     }
1107     createdOsAccountNum = reply.ReadInt32();
1108     return ERR_OK;
1109 }
1110 
GetSerialNumberFromDatabase(const std::string & storeID,int64_t & serialNumber)1111 ErrCode OsAccountProxy::GetSerialNumberFromDatabase(const std::string& storeID, int64_t &serialNumber)
1112 {
1113     MessageParcel data;
1114     MessageParcel reply;
1115 
1116     if (!data.WriteInterfaceToken(GetDescriptor())) {
1117         ACCOUNT_LOGE("failed to write descriptor!");
1118         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1119     }
1120 
1121     if (!data.WriteString(storeID)) {
1122         ACCOUNT_LOGE("failed to write storeID for getting serial number from database");
1123         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1124     }
1125     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_SERIAL_NUM_FROM_DATABASE, data, reply);
1126     if (result != ERR_OK) {
1127         ACCOUNT_LOGE("SendRequest failed, result %{public}d.", result);
1128         return result;
1129     }
1130 
1131     result = reply.ReadInt32();
1132     if (result != ERR_OK) {
1133         ACCOUNT_LOGE("failed to read reply, result %{public}d.", result);
1134         return result;
1135     }
1136     serialNumber = reply.ReadInt64();
1137     return ERR_OK;
1138 }
1139 
GetMaxAllowCreateIdFromDatabase(const std::string & storeID,int & id)1140 ErrCode OsAccountProxy::GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id)
1141 {
1142     MessageParcel data;
1143     MessageParcel reply;
1144 
1145     if (!data.WriteInterfaceToken(GetDescriptor())) {
1146         ACCOUNT_LOGE("failed to write descriptor!");
1147         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1148     }
1149 
1150     if (!data.WriteString(storeID)) {
1151         ACCOUNT_LOGE("failed to write string for isVerified");
1152         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1153     }
1154     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_MAX_ALLOW_CREATE_ID_FROM_DATABASE, data, reply);
1155     if (result != ERR_OK) {
1156         ACCOUNT_LOGE("SendRequest failed, result %{public}d.", result);
1157         return result;
1158     }
1159 
1160     result = reply.ReadInt32();
1161     if (result != ERR_OK) {
1162         ACCOUNT_LOGE("failed to read reply, result %{public}d.", result);
1163         return result;
1164     }
1165     id = reply.ReadInt32();
1166     return ERR_OK;
1167 }
1168 
GetOsAccountFromDatabase(const std::string & storeID,const int id,OsAccountInfo & osAccountInfo)1169 ErrCode OsAccountProxy::GetOsAccountFromDatabase(const std::string& storeID,
1170     const int id, OsAccountInfo &osAccountInfo)
1171 {
1172     MessageParcel data;
1173     MessageParcel reply;
1174 
1175     if (!data.WriteInterfaceToken(GetDescriptor())) {
1176         ACCOUNT_LOGE("failed to write descriptor!");
1177         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1178     }
1179 
1180     if (!data.WriteString(storeID)) {
1181         ACCOUNT_LOGE("failed to write storeID for getting os account form database");
1182         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1183     }
1184     if (!data.WriteInt32(id)) {
1185         ACCOUNT_LOGE("failed to write int for id");
1186         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1187     }
1188 
1189     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_FROM_DATABASE, data, reply);
1190     if (result != ERR_OK) {
1191         ACCOUNT_LOGE("SendRequest failed, result %{public}d.", result);
1192         return result;
1193     }
1194 
1195     result = reply.ReadInt32();
1196     if (result != ERR_OK) {
1197         ACCOUNT_LOGE("Failed to read reply, result %{public}d.", result);
1198         return result;
1199     }
1200     if (!ReadOsAccountInfo(reply, osAccountInfo)) {
1201         ACCOUNT_LOGE("Failed to read account info.");
1202         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1203     }
1204 
1205     return ERR_OK;
1206 }
1207 
GetOsAccountListFromDatabase(const std::string & storeID,std::vector<OsAccountInfo> & osAccountList)1208 ErrCode OsAccountProxy::GetOsAccountListFromDatabase(const std::string& storeID,
1209     std::vector<OsAccountInfo> &osAccountList)
1210 {
1211     MessageParcel data;
1212     MessageParcel reply;
1213 
1214     if (!data.WriteInterfaceToken(GetDescriptor())) {
1215         ACCOUNT_LOGE("failed to write descriptor!");
1216         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1217     }
1218 
1219     if (!data.WriteString(storeID)) {
1220         ACCOUNT_LOGE("failed to write storeID for getting os account list from database");
1221         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1222     }
1223     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_LIST_FROM_DATABASE, data, reply);
1224     if (result != ERR_OK) {
1225         ACCOUNT_LOGE("SendRequest failed, result %{public}d.", result);
1226         return result;
1227     }
1228 
1229     result = reply.ReadInt32();
1230     if (result != ERR_OK) {
1231         ACCOUNT_LOGE("failed to read reply, result %{public}d.", result);
1232         return result;
1233     }
1234     ReadOsAccountInfoList(reply, osAccountList);
1235     return ERR_OK;
1236 }
1237 
QueryActiveOsAccountIds(std::vector<int32_t> & ids)1238 ErrCode OsAccountProxy::QueryActiveOsAccountIds(std::vector<int32_t>& ids)
1239 {
1240     MessageParcel data;
1241     MessageParcel reply;
1242 
1243     if (!data.WriteInterfaceToken(GetDescriptor())) {
1244         ACCOUNT_LOGE("failed to write descriptor!");
1245         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1246     }
1247 
1248     ErrCode result = SendRequest(OsAccountInterfaceCode::QUERY_ACTIVE_OS_ACCOUNT_IDS, data, reply);
1249     if (result != ERR_OK) {
1250         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1251         return result;
1252     }
1253 
1254     result = reply.ReadInt32();
1255     if (result != ERR_OK) {
1256         ACCOUNT_LOGE("failed to read reply for query active os account ids, result %{public}d.", result);
1257         return result;
1258     }
1259 
1260     bool readFlag = reply.ReadInt32Vector(&ids);
1261     if (!readFlag) {
1262         ACCOUNT_LOGE("failed to read vector for active ids.");
1263         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1264     }
1265     return ERR_OK;
1266 }
1267 
QueryOsAccountConstraintSourceTypes(const int32_t id,const std::string & constraint,std::vector<ConstraintSourceTypeInfo> & constraintSourceTypeInfos)1268 ErrCode OsAccountProxy::QueryOsAccountConstraintSourceTypes(const int32_t id,
1269     const std::string &constraint, std::vector<ConstraintSourceTypeInfo> &constraintSourceTypeInfos)
1270 {
1271     constraintSourceTypeInfos.clear();
1272     MessageParcel data;
1273     if (!data.WriteInterfaceToken(GetDescriptor())) {
1274         ACCOUNT_LOGE("failed to write descriptor!");
1275         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1276     }
1277     if (!data.WriteInt32(id)) {
1278         ACCOUNT_LOGE("failed to write id for setting constraint source types");
1279         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1280     }
1281     if (!data.WriteString(constraint)) {
1282         ACCOUNT_LOGE("failed to write string for constraint");
1283         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1284     }
1285 
1286     MessageParcel reply;
1287     ErrCode result = SendRequest(OsAccountInterfaceCode::QUERY_OS_ACCOUNT_CONSTRAINT_SOURCE_TYPES, data, reply);
1288     if (result != ERR_OK) {
1289         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1290         return result;
1291     }
1292     result = reply.ReadInt32();
1293     if (result != ERR_OK) {
1294         return result;
1295     }
1296     uint32_t size = reply.ReadUint32();
1297     for (uint32_t i = 0; i < size; ++i) {
1298         ConstraintSourceTypeInfo constraintSrcInfo;
1299         constraintSrcInfo.localId = reply.ReadInt32();
1300         constraintSrcInfo.typeInfo = static_cast<ConstraintSourceType>(reply.ReadInt32());
1301         constraintSourceTypeInfos.push_back(constraintSrcInfo);
1302     }
1303     return ERR_OK;
1304 }
1305 
SetGlobalOsAccountConstraints(const std::vector<std::string> & constraints,const bool enable,const int32_t enforcerId,const bool isDeviceOwner)1306 ErrCode OsAccountProxy::SetGlobalOsAccountConstraints(const std::vector<std::string> &constraints,
1307     const bool enable, const int32_t enforcerId, const bool isDeviceOwner)
1308 {
1309     MessageParcel data;
1310     MessageParcel reply;
1311 
1312     if (!data.WriteInterfaceToken(GetDescriptor())) {
1313         ACCOUNT_LOGE("failed to write descriptor!");
1314         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1315     }
1316     if (!data.WriteStringVector(constraints)) {
1317         ACCOUNT_LOGE("failed to write stringVector for constraints");
1318         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1319     }
1320 
1321     if (!data.WriteBool(enable)) {
1322         ACCOUNT_LOGE("failed to write bool for enable");
1323         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1324     }
1325 
1326     if (!data.WriteInt32(enforcerId)) {
1327         ACCOUNT_LOGE("failed to write int for enforcerId");
1328         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1329     }
1330 
1331     if (!data.WriteBool(isDeviceOwner)) {
1332         ACCOUNT_LOGE("failed to write bool for isDeviceOwner");
1333         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1334     }
1335     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_GLOBAL_OS_ACCOUNT_CONSTRAINTS, data, reply);
1336     if (result != ERR_OK) {
1337         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1338         return result;
1339     }
1340     result = reply.ReadInt32();
1341     if (result != ERR_OK) {
1342         ACCOUNT_LOGE("failed to read reply for set global os account constraints.");
1343         return result;
1344     }
1345     return ERR_OK;
1346 }
1347 
SetSpecificOsAccountConstraints(const std::vector<std::string> & constraints,const bool enable,const int32_t targetId,const int32_t enforcerId,const bool isDeviceOwner)1348 ErrCode OsAccountProxy::SetSpecificOsAccountConstraints(const std::vector<std::string> &constraints,
1349     const bool enable, const int32_t targetId, const int32_t enforcerId, const bool isDeviceOwner)
1350 {
1351     MessageParcel data;
1352     MessageParcel reply;
1353 
1354     if (!data.WriteInterfaceToken(GetDescriptor())) {
1355         ACCOUNT_LOGE("failed to write descriptor!");
1356         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1357     }
1358     if (!data.WriteStringVector(constraints)) {
1359         ACCOUNT_LOGE("failed to write stringVector for constraints");
1360         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1361     }
1362 
1363     if (!data.WriteBool(enable)) {
1364         ACCOUNT_LOGE("failed to write bool for enable");
1365         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1366     }
1367 
1368     if (!data.WriteInt32(targetId)) {
1369         ACCOUNT_LOGE("failed to write int for targetId");
1370         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1371     }
1372 
1373     if (!data.WriteInt32(enforcerId)) {
1374         ACCOUNT_LOGE("failed to write int for enforcerId");
1375         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1376     }
1377 
1378     if (!data.WriteBool(isDeviceOwner)) {
1379         ACCOUNT_LOGE("failed to write bool for isDeviceOwner");
1380         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1381     }
1382     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_SPECIFIC_OS_ACCOUNT_CONSTRAINTS, data, reply);
1383     if (result != ERR_OK) {
1384         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1385         return result;
1386     }
1387     result = reply.ReadInt32();
1388     if (result != ERR_OK) {
1389         ACCOUNT_LOGE("failed to read reply for set specific os account constraints.");
1390         return result;
1391     }
1392     return ERR_OK;
1393 }
1394 
SetDefaultActivatedOsAccount(const int32_t id)1395 ErrCode OsAccountProxy::SetDefaultActivatedOsAccount(const int32_t id)
1396 {
1397     MessageParcel data;
1398     if (!data.WriteInterfaceToken(GetDescriptor())) {
1399         ACCOUNT_LOGE("failed to write descriptor!");
1400         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1401     }
1402     if (!data.WriteInt32(id)) {
1403         ACCOUNT_LOGE("failed to write id for setting default activated os account");
1404         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1405     }
1406     MessageParcel reply;
1407     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_DEFAULT_ACTIVATED_OS_ACCOUNT, data, reply);
1408     if (result != ERR_OK) {
1409         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1410         return result;
1411     }
1412     if (!reply.ReadInt32(result)) {
1413         ACCOUNT_LOGE("failed to read result for set default activated os account.");
1414         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1415     }
1416     return result;
1417 }
1418 
GetDefaultActivatedOsAccount(int32_t & id)1419 ErrCode OsAccountProxy::GetDefaultActivatedOsAccount(int32_t &id)
1420 {
1421     MessageParcel data;
1422     if (!data.WriteInterfaceToken(GetDescriptor())) {
1423         ACCOUNT_LOGE("failed to write descriptor!");
1424         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1425     }
1426     MessageParcel reply;
1427     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_DEFAULT_ACTIVATED_OS_ACCOUNT, data, reply);
1428     if (result != ERR_OK) {
1429         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1430         return result;
1431     }
1432     if (!reply.ReadInt32(result)) {
1433         ACCOUNT_LOGE("failed to read result for get default activated os account.");
1434         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1435     }
1436     if (result != ERR_OK) {
1437         ACCOUNT_LOGE("failed to get default activated os account, result %{public}d.", result);
1438         return result;
1439     }
1440     if (!reply.ReadInt32(id)) {
1441         ACCOUNT_LOGE("failed to read local id");
1442         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1443     }
1444     return ERR_OK;
1445 }
1446 
ReadOsAccountInfo(MessageParcel & data,OsAccountInfo & accountInfo)1447 bool OsAccountProxy::ReadOsAccountInfo(MessageParcel &data, OsAccountInfo &accountInfo)
1448 {
1449     int32_t accountSize;
1450     if (!data.ReadInt32(accountSize)) {
1451         ACCOUNT_LOGE("Failed to read accountSize");
1452         return false;
1453     }
1454     auto readRawData = data.ReadRawData(accountSize);
1455     if (readRawData == nullptr) {
1456         ACCOUNT_LOGE("Failed to read accountData accountSize = %{public}d", accountSize);
1457         return false;
1458     }
1459     const char *accountData = reinterpret_cast<const char *>(readRawData);
1460     std::string accountJson = std::string(accountData, accountSize - 1);
1461     nlohmann::json jsonObject = nlohmann::json::parse(accountJson, nullptr, false);
1462     if (jsonObject.is_discarded()) {
1463         ACCOUNT_LOGE("AccountJson is discarded");
1464         return false;
1465     }
1466     accountInfo.FromJson(jsonObject);
1467 
1468     return true;
1469 }
1470 
ReadOsAccountInfoList(MessageParcel & data,std::vector<OsAccountInfo> & infoList)1471 bool OsAccountProxy::ReadOsAccountInfoList(MessageParcel &data, std::vector<OsAccountInfo> &infoList)
1472 {
1473     infoList.clear();
1474     uint32_t accountsStrLength;
1475     if (!data.ReadUint32(accountsStrLength)) {
1476         ACCOUNT_LOGE("Failed to read accountsStrLength");
1477         return false;
1478     }
1479     if (accountsStrLength == 0) {
1480         ACCOUNT_LOGE("accountsStrLength is invalid, accountsStrLength = %{public}d", accountsStrLength);
1481         return false;
1482     }
1483     auto readRawData = data.ReadRawData(accountsStrLength);
1484     if (readRawData == nullptr) {
1485         ACCOUNT_LOGE("Failed to read accountArrayData accountArraySize = %{public}d", accountsStrLength);
1486         return false;
1487     }
1488     const char *accountsStrData = reinterpret_cast<const char *>(readRawData);
1489     std::string accountsStr = std::string(accountsStrData, accountsStrLength - 1);
1490     nlohmann::json accounts = nlohmann::json::parse(accountsStr, nullptr, false);
1491     if (accounts.is_discarded()) {
1492         ACCOUNT_LOGE("JsonArray is discarded");
1493         return false;
1494     }
1495 
1496     for (const auto &json : accounts) {
1497         OsAccountInfo accountInfo;
1498         accountInfo.FromJson(json);
1499         infoList.emplace_back(accountInfo);
1500     }
1501 
1502     return true;
1503 }
1504 
GetOsAccountShortName(std::string & shortName)1505 ErrCode OsAccountProxy::GetOsAccountShortName(std::string &shortName)
1506 {
1507     MessageParcel data;
1508     if (!data.WriteInterfaceToken(GetDescriptor())) {
1509         ACCOUNT_LOGE("failed to write descriptor!");
1510         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1511     }
1512 
1513     MessageParcel reply;
1514     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_SHORT_NAME, data, reply);
1515     if (result != ERR_OK) {
1516         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1517         return result;
1518     }
1519 
1520     result = reply.ReadInt32();
1521     if (result != ERR_OK) {
1522         ACCOUNT_LOGE("failed to read reply for is current os account verified, result %{public}d.", result);
1523         return result;
1524     }
1525     if (!reply.ReadString(shortName)) {
1526         ACCOUNT_LOGE("failed to read short name");
1527         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1528     }
1529 
1530     return ERR_OK;
1531 }
1532 
GetOsAccountName(std::string & name)1533 ErrCode OsAccountProxy::GetOsAccountName(std::string &name)
1534 {
1535     MessageParcel data;
1536     if (!data.WriteInterfaceToken(GetDescriptor())) {
1537         ACCOUNT_LOGE("Failed to write descriptor!");
1538         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1539     }
1540 
1541     MessageParcel reply;
1542     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_NAME, data, reply);
1543     if (result != ERR_OK) {
1544         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1545         return result;
1546     }
1547 
1548     if (!reply.ReadInt32(result)) {
1549         ACCOUNT_LOGE("Read result from reply failed.");
1550         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1551     }
1552     if (result != ERR_OK) {
1553         ACCOUNT_LOGE("Failed to read reply for is current os account verified, result=%{public}d.", result);
1554         return result;
1555     }
1556     if (!reply.ReadString(name)) {
1557         ACCOUNT_LOGE("Failed to read short name");
1558         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1559     }
1560 
1561     return ERR_OK;
1562 }
1563 
GetOsAccountShortNameById(const int32_t id,std::string & shortName)1564 ErrCode OsAccountProxy::GetOsAccountShortNameById(const int32_t id, std::string &shortName)
1565 {
1566     MessageParcel data;
1567     if (!data.WriteInterfaceToken(GetDescriptor())) {
1568         ACCOUNT_LOGE("Write descriptor failed.");
1569         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1570     }
1571     if (!data.WriteInt32(id)) {
1572         ACCOUNT_LOGE("Write id failed.");
1573         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1574     }
1575 
1576     MessageParcel reply;
1577     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_SHORT_NAME_BY_ID, data, reply);
1578     if (result != ERR_OK) {
1579         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
1580         return result;
1581     }
1582 
1583     if (!reply.ReadInt32(result)) {
1584         ACCOUNT_LOGE("Read result from reply failed.");
1585         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1586     }
1587     if (result != ERR_OK) {
1588         ACCOUNT_LOGE("Get os account short name failed, result=%{public}d.", result);
1589         return result;
1590     }
1591     if (!reply.ReadString(shortName)) {
1592         ACCOUNT_LOGE("Read short name failed.");
1593         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1594     }
1595 
1596     return ERR_OK;
1597 }
1598 
IsOsAccountForeground(const int32_t localId,const uint64_t displayId,bool & isForeground)1599 ErrCode OsAccountProxy::IsOsAccountForeground(const int32_t localId, const uint64_t displayId, bool &isForeground)
1600 {
1601     MessageParcel data;
1602     if (!data.WriteInterfaceToken(GetDescriptor())) {
1603         ACCOUNT_LOGE("Write descriptor failed.");
1604         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1605     }
1606     if (!data.WriteInt32(localId)) {
1607         ACCOUNT_LOGE("Write localId failed.");
1608         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1609     }
1610     if (!data.WriteUint64(displayId)) {
1611         ACCOUNT_LOGE("Write displayId failed.");
1612         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1613     }
1614     MessageParcel reply;
1615     ErrCode result = SendRequest(OsAccountInterfaceCode::IS_OS_ACCOUNT_FOREGROUND, data, reply);
1616     if (result != ERR_OK) {
1617         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
1618         return result;
1619     }
1620     if (!reply.ReadInt32(result)) {
1621         ACCOUNT_LOGE("Read result failed.");
1622         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1623     }
1624     if (result != ERR_OK) {
1625         ACCOUNT_LOGE("IsOsAccountForeground failed, result=%{public}d.", result);
1626         return result;
1627     }
1628     if (!reply.ReadBool(isForeground)) {
1629         ACCOUNT_LOGE("Read isForeground failed.");
1630         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1631     }
1632     return ERR_OK;
1633 }
1634 
GetForegroundOsAccountLocalId(const uint64_t displayId,int32_t & localId)1635 ErrCode OsAccountProxy::GetForegroundOsAccountLocalId(const uint64_t displayId, int32_t &localId)
1636 {
1637     MessageParcel data;
1638     if (!data.WriteInterfaceToken(GetDescriptor())) {
1639         ACCOUNT_LOGE("Write descriptor failed.");
1640         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1641     }
1642     if (!data.WriteUint64(displayId)) {
1643         ACCOUNT_LOGE("Write displayId failed.");
1644         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1645     }
1646     MessageParcel reply;
1647     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_FOREGROUND_OS_ACCOUNT_LOCAL_ID, data, reply);
1648     if (result != ERR_OK) {
1649         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
1650         return result;
1651     }
1652     if (!reply.ReadInt32(result)) {
1653         ACCOUNT_LOGE("Read result from reply failed.");
1654         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1655     }
1656     if (result != ERR_OK) {
1657         ACCOUNT_LOGE("GetForegroundOsAccountLocalId failed, result=%{public}d.", result);
1658         return result;
1659     }
1660     if (!reply.ReadInt32(localId)) {
1661         ACCOUNT_LOGE("Read localId failed");
1662         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1663     }
1664     return ERR_OK;
1665 }
1666 
GetForegroundOsAccounts(std::vector<ForegroundOsAccount> & accounts)1667 ErrCode OsAccountProxy::GetForegroundOsAccounts(std::vector<ForegroundOsAccount> &accounts)
1668 {
1669     MessageParcel data;
1670     if (!data.WriteInterfaceToken(GetDescriptor())) {
1671         ACCOUNT_LOGE("Write descriptor failed");
1672         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1673     }
1674     MessageParcel reply;
1675     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_FOREGROUND_OS_ACCOUNTS, data, reply);
1676     if (result != ERR_OK) {
1677         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
1678         return result;
1679     }
1680     if (!reply.ReadInt32(result)) {
1681         ACCOUNT_LOGE("Read result failed.");
1682         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1683     }
1684     if (result != ERR_OK) {
1685         ACCOUNT_LOGE("GetForegroundOsAccounts failed, result=%{public}d.", result);
1686         return result;
1687     }
1688     uint32_t size = 0;
1689     if (!reply.ReadUint32(size)) {
1690         ACCOUNT_LOGE("Read foregroundAccounts size failed.");
1691         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1692     }
1693     if (size >= ACCOUNT_MAX_SIZE) {
1694         ACCOUNT_LOGE("Account size exceeded.");
1695         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
1696     }
1697     accounts.clear();
1698     for (uint32_t i = 0; i < size; ++i) {
1699         ForegroundOsAccount foregroundOsAccount;
1700         if (!reply.ReadInt32(foregroundOsAccount.localId) || !reply.ReadUint64(foregroundOsAccount.displayId)) {
1701             ACCOUNT_LOGE("Read ForegroundOsAccount failed.");
1702             return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1703         }
1704         accounts.emplace_back(foregroundOsAccount);
1705     }
1706     return ERR_OK;
1707 }
1708 
GetBackgroundOsAccountLocalIds(std::vector<int32_t> & localIds)1709 ErrCode OsAccountProxy::GetBackgroundOsAccountLocalIds(std::vector<int32_t> &localIds)
1710 {
1711     MessageParcel data;
1712     if (!data.WriteInterfaceToken(GetDescriptor())) {
1713         ACCOUNT_LOGE("Write descriptor failed.");
1714         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1715     }
1716     MessageParcel reply;
1717     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_BACKGROUND_OS_ACCOUNT_LOCAL_IDS, data, reply);
1718     if (result != ERR_OK) {
1719         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
1720         return result;
1721     }
1722     if (!reply.ReadInt32(result)) {
1723         ACCOUNT_LOGE("Read result failed.");
1724         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1725     }
1726     if (result != ERR_OK) {
1727         ACCOUNT_LOGE("GetBackgroundOsAccountLocalIds failed, result=%{public}d.", result);
1728         return result;
1729     }
1730     localIds.clear();
1731     if (!reply.ReadInt32Vector(&localIds)) {
1732         ACCOUNT_LOGE("Read localIds failed.");
1733         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1734     }
1735     return ERR_OK;
1736 }
1737 
SetOsAccountToBeRemoved(int32_t localId,bool toBeRemoved)1738 ErrCode OsAccountProxy::SetOsAccountToBeRemoved(int32_t localId, bool toBeRemoved)
1739 {
1740     MessageParcel data;
1741     if (!data.WriteInterfaceToken(GetDescriptor())) {
1742         ACCOUNT_LOGE("Write descriptor failed.");
1743         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1744     }
1745     if (!data.WriteInt32(localId)) {
1746         ACCOUNT_LOGE("Write localId failed.");
1747         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1748     }
1749     if (!data.WriteBool(toBeRemoved)) {
1750         ACCOUNT_LOGE("Write toBeRemoved failed.");
1751         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1752     }
1753     MessageParcel reply;
1754     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_OS_ACCOUNT_TO_BE_REMOVED, data, reply);
1755     if (result != ERR_OK) {
1756         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
1757         return result;
1758     }
1759     if (!reply.ReadInt32(result)) {
1760         ACCOUNT_LOGE("Read result failed.");
1761         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1762     }
1763     return result;
1764 }
1765 }  // namespace AccountSA
1766 }  // namespace OHOS
1767