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 
IsOsAccountDeactivating(const int id,bool & isDeactivating)313 ErrCode OsAccountProxy::IsOsAccountDeactivating(const int id, bool &isDeactivating)
314 {
315     MessageParcel reply;
316     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::IS_OS_ACCOUNT_DEACTIVATING, reply, id);
317     if (result != ERR_OK) {
318         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
319         return result;
320     }
321     if (!reply.ReadBool(isDeactivating)) {
322         ACCOUNT_LOGE("Failed to read result for check os account deactivating state.");
323         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
324     }
325     return ERR_OK;
326 }
327 
GetCreatedOsAccountsCount(unsigned int & osAccountsCount)328 ErrCode OsAccountProxy::GetCreatedOsAccountsCount(unsigned int &osAccountsCount)
329 {
330     osAccountsCount = 0;
331     MessageParcel data;
332     MessageParcel reply;
333 
334     if (!data.WriteInterfaceToken(GetDescriptor())) {
335         ACCOUNT_LOGE("failed to write descriptor!");
336         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
337     }
338 
339     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_CREATED_OS_ACCOUNT_COUNT, data, reply);
340     if (result != ERR_OK) {
341         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
342         return result;
343     }
344     result = reply.ReadInt32();
345     if (result != ERR_OK) {
346         ACCOUNT_LOGE("failed to read reply for get os account count, result %{public}d.", result);
347         return result;
348     }
349     osAccountsCount = reply.ReadUint32();
350 
351     return ERR_OK;
352 }
353 
GetOsAccountLocalIdFromProcess(int & id)354 ErrCode OsAccountProxy::GetOsAccountLocalIdFromProcess(int &id)
355 {
356     MessageParcel data;
357     MessageParcel reply;
358 
359     if (!data.WriteInterfaceToken(GetDescriptor())) {
360         ACCOUNT_LOGE("failed to write descriptor!");
361         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
362     }
363 
364     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_LOCAL_ID_FROM_PROCESS, data, reply);
365     if (result != ERR_OK) {
366         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
367         return result;
368     }
369     result = reply.ReadInt32();
370     if (result != ERR_OK) {
371         ACCOUNT_LOGE("failed to read reply for get os account id from process, result %{public}d.", result);
372         return result;
373     }
374     id = reply.ReadInt32();
375 
376     return ERR_OK;
377 }
378 
IsMainOsAccount(bool & isMainOsAccount)379 ErrCode OsAccountProxy::IsMainOsAccount(bool &isMainOsAccount)
380 {
381     MessageParcel data;
382     MessageParcel reply;
383 
384     if (!data.WriteInterfaceToken(GetDescriptor())) {
385         ACCOUNT_LOGE("failed to write descriptor!");
386         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
387     }
388 
389     ErrCode result = SendRequest(OsAccountInterfaceCode::IS_MAIN_OS_ACCOUNT, data, reply);
390     if (result != ERR_OK) {
391         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
392         return result;
393     }
394     result = reply.ReadInt32();
395     if (result != ERR_OK) {
396         ACCOUNT_LOGE("failed to read reply for is main os account, result %{public}d.", result);
397         return result;
398     }
399     isMainOsAccount = reply.ReadBool();
400 
401     return ERR_OK;
402 }
403 
GetOsAccountLocalIdFromDomain(const DomainAccountInfo & domainInfo,int & id)404 ErrCode OsAccountProxy::GetOsAccountLocalIdFromDomain(const DomainAccountInfo &domainInfo, int &id)
405 {
406     MessageParcel data;
407     MessageParcel reply;
408 
409     if (!data.WriteInterfaceToken(GetDescriptor())) {
410         ACCOUNT_LOGE("failed to write descriptor!");
411         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
412     }
413 
414     if (!data.WriteString(domainInfo.domain_)) {
415         ACCOUNT_LOGE("failed to write int for domain.");
416         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
417     }
418     if (!data.WriteString(domainInfo.accountName_)) {
419         ACCOUNT_LOGE("failed to write int for domain account name.");
420         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
421     }
422     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_LOCAL_ID_FROM_DOMAIN, data, reply);
423     if (result != ERR_OK) {
424         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
425         return result;
426     }
427     result = reply.ReadInt32();
428     if (result != ERR_OK) {
429         ACCOUNT_LOGE("read from reply err, result %{public}d.", result);
430         return result;
431     }
432     id = reply.ReadInt32();
433 
434     return ERR_OK;
435 }
436 
QueryMaxOsAccountNumber(uint32_t & maxOsAccountNumber)437 ErrCode OsAccountProxy::QueryMaxOsAccountNumber(uint32_t &maxOsAccountNumber)
438 {
439     MessageParcel data;
440     MessageParcel reply;
441 
442     if (!data.WriteInterfaceToken(GetDescriptor())) {
443         ACCOUNT_LOGE("failed to write descriptor!");
444         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
445     }
446 
447     ErrCode result = SendRequest(OsAccountInterfaceCode::QUERY_MAX_OS_ACCOUNT_NUMBER, data, reply);
448     if (result != ERR_OK) {
449         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
450         return result;
451     }
452     result = reply.ReadInt32();
453     if (result != ERR_OK) {
454         ACCOUNT_LOGE("failed to read reply for query os account number, result %{public}d.", result);
455         return result;
456     }
457     maxOsAccountNumber = static_cast<uint32_t>(reply.ReadInt32());
458 
459     return ERR_OK;
460 }
461 
QueryMaxLoggedInOsAccountNumber(uint32_t & maxNum)462 ErrCode OsAccountProxy::QueryMaxLoggedInOsAccountNumber(uint32_t &maxNum)
463 {
464     MessageParcel data;
465     if (!data.WriteInterfaceToken(GetDescriptor())) {
466         ACCOUNT_LOGE("Failed to write descriptor!");
467         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
468     }
469 
470     MessageParcel reply;
471     ErrCode result = SendRequest(OsAccountInterfaceCode::QUERY_MAX_LOGGED_IN_OS_ACCOUNT_NUMBER, data, reply);
472     if (result != ERR_OK) {
473         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
474         return result;
475     }
476     if (!reply.ReadInt32(result)) {
477         ACCOUNT_LOGE("Failed to read errCode");
478         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
479     }
480     if (result != ERR_OK) {
481         return result;
482     }
483     if (!reply.ReadUint32(maxNum)) {
484         ACCOUNT_LOGE("Failed to read maxNum");
485         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
486     }
487     return ERR_OK;
488 }
489 
GetOsAccountAllConstraints(const int id,std::vector<std::string> & constraints)490 ErrCode OsAccountProxy::GetOsAccountAllConstraints(const int id, std::vector<std::string> &constraints)
491 {
492     MessageParcel reply;
493     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::GET_OS_ACCOUNT_ALL_CONSTRAINTS, reply, id);
494     if (result != ERR_OK) {
495         return result;
496     }
497     bool readFlag = reply.ReadStringVector(&constraints);
498     if (!readFlag) {
499         ACCOUNT_LOGE("ReadStringVector failed, result %{public}d.", result);
500         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
501     }
502     return ERR_OK;
503 }
504 
QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> & osAccountInfos)505 ErrCode OsAccountProxy::QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &osAccountInfos)
506 {
507     MessageParcel data;
508     MessageParcel reply;
509 
510     if (!data.WriteInterfaceToken(GetDescriptor())) {
511         ACCOUNT_LOGE("failed to write descriptor!");
512         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
513     }
514 
515     ErrCode result = SendRequest(OsAccountInterfaceCode::QUERY_ALL_CREATED_OS_ACCOUNTS, data, reply);
516     if (result != ERR_OK) {
517         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
518         return result;
519     }
520     result = reply.ReadInt32();
521     if (result != ERR_OK) {
522         ACCOUNT_LOGE("failed to read reply for query all os accounts, result %{public}d.", result);
523         return result;
524     }
525     ReadOsAccountInfoList(reply, osAccountInfos);
526 
527     return ERR_OK;
528 }
529 
QueryCurrentOsAccount(OsAccountInfo & osAccountInfo)530 ErrCode OsAccountProxy::QueryCurrentOsAccount(OsAccountInfo &osAccountInfo)
531 {
532     MessageParcel data;
533     MessageParcel reply;
534 
535     if (!data.WriteInterfaceToken(GetDescriptor())) {
536         ACCOUNT_LOGE("failed to write descriptor!");
537         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
538     }
539 
540     ErrCode result = SendRequest(OsAccountInterfaceCode::QUERY_CURRENT_OS_ACCOUNT, data, reply);
541     if (result != ERR_OK) {
542         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
543         return result;
544     }
545     result = reply.ReadInt32();
546     if (result != ERR_OK) {
547         ACCOUNT_LOGE("failed to read reply for query current os account, result %{public}d.", result);
548         return result;
549     }
550     if (!ReadOsAccountInfo(reply, osAccountInfo)) {
551         ACCOUNT_LOGE("Failed to read account info.");
552         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
553     }
554     return ERR_OK;
555 }
556 
QueryOsAccountById(const int id,OsAccountInfo & osAccountInfo)557 ErrCode OsAccountProxy::QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo)
558 {
559     MessageParcel reply;
560     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::QUERY_OS_ACCOUNT_BY_ID, reply, id);
561     if (result != ERR_OK) {
562         return result;
563     }
564     if (!ReadOsAccountInfo(reply, osAccountInfo)) {
565         ACCOUNT_LOGE("Failed to read account info.");
566         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
567     }
568 
569     return ERR_OK;
570 }
571 
GetOsAccountTypeFromProcess(OsAccountType & type)572 ErrCode OsAccountProxy::GetOsAccountTypeFromProcess(OsAccountType &type)
573 {
574     MessageParcel data;
575     MessageParcel reply;
576 
577     if (!data.WriteInterfaceToken(GetDescriptor())) {
578         ACCOUNT_LOGE("failed to write descriptor!");
579         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
580     }
581 
582     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_TYPE_FROM_PROCESS, data, reply);
583     if (result != ERR_OK) {
584         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
585         return result;
586     }
587     result = reply.ReadInt32();
588     if (result != ERR_OK) {
589         ACCOUNT_LOGE("failed to read reply for get os account type by process, result %{public}d.", result);
590         return result;
591     }
592     type = static_cast<OsAccountType>(reply.ReadInt32());
593 
594     return ERR_OK;
595 }
596 
GetOsAccountType(const int id,OsAccountType & type)597 ErrCode OsAccountProxy::GetOsAccountType(const int id, OsAccountType& type)
598 {
599     MessageParcel reply;
600     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::GET_OS_ACCOUNT_TYPE, reply, id);
601     if (result != ERR_OK) {
602         return result;
603     }
604 
605     int32_t typeResult = 0;
606     if (!reply.ReadInt32(typeResult)) {
607         ACCOUNT_LOGE("Failed to read type.");
608         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
609     }
610     type = static_cast<OsAccountType>(typeResult);
611 
612     return ERR_OK;
613 }
614 
GetOsAccountProfilePhoto(const int id,std::string & photo)615 ErrCode OsAccountProxy::GetOsAccountProfilePhoto(const int id, std::string &photo)
616 {
617     MessageParcel reply;
618     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::GET_OS_ACCOUNT_PROFILE_PHOTO, reply, id);
619     if (result != ERR_OK) {
620         return result;
621     }
622 
623     int32_t photoSize;
624     if (!reply.ReadInt32(photoSize)) {
625         ACCOUNT_LOGE("Failed to read photoSize");
626         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
627     }
628 
629     if (photoSize - 1 > static_cast<int32_t>(Constants::LOCAL_PHOTO_MAX_SIZE) || photoSize < 1) {
630         ACCOUNT_LOGE("PhotoSize is invalid, photosize = %{public}d", photoSize);
631         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
632     }
633     auto readRawData = reply.ReadRawData(photoSize);
634     if (readRawData == nullptr) {
635         ACCOUNT_LOGE("Failed to read photoData, id=%{public}d", id);
636         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
637     }
638     const char *photoData = reinterpret_cast<const char *>(readRawData);
639     photo = std::string(photoData, photoSize - 1);
640 
641     return ERR_OK;
642 }
643 
IsMultiOsAccountEnable(bool & isMultiOsAccountEnable)644 ErrCode OsAccountProxy::IsMultiOsAccountEnable(bool &isMultiOsAccountEnable)
645 {
646     MessageParcel data;
647     MessageParcel reply;
648 
649     if (!data.WriteInterfaceToken(GetDescriptor())) {
650         ACCOUNT_LOGE("failed to write descriptor!");
651         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
652     }
653 
654     ErrCode result = SendRequest(OsAccountInterfaceCode::IS_MULTI_OS_ACCOUNT_ENABLE, data, reply);
655     if (result != ERR_OK) {
656         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
657         return result;
658     }
659     result = reply.ReadInt32();
660     if (result != ERR_OK) {
661         ACCOUNT_LOGE("failed to read reply for is multi os account enable.");
662         return result;
663     }
664     isMultiOsAccountEnable = reply.ReadBool();
665 
666     return ERR_OK;
667 }
668 
SetOsAccountName(const int id,const std::string & name)669 ErrCode OsAccountProxy::SetOsAccountName(const int id, const std::string &name)
670 {
671     MessageParcel data;
672     if (!data.WriteInterfaceToken(GetDescriptor())) {
673         ACCOUNT_LOGE("failed to write descriptor!");
674         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
675     }
676     if (!data.WriteInt32(id)) {
677         ACCOUNT_LOGE("failed to write int for id %{public}d.", id);
678         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
679     }
680     if (!data.WriteString(name)) {
681         ACCOUNT_LOGE("failed to write string for name");
682         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
683     }
684 
685     MessageParcel reply;
686     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_OS_ACCOUNT_NAME, data, reply);
687     if (result != ERR_OK) {
688         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
689         return result;
690     }
691     result = reply.ReadInt32();
692     if (result != ERR_OK) {
693         ACCOUNT_LOGE("failed to read reply for set os account name, result %{public}d.", result);
694         return result;
695     }
696 
697     return ERR_OK;
698 }
699 
SetOsAccountConstraints(const int id,const std::vector<std::string> & constraints,const bool enable)700 ErrCode OsAccountProxy::SetOsAccountConstraints(
701     const int id, const std::vector<std::string> &constraints, const bool enable)
702 {
703     MessageParcel data;
704     if (!data.WriteInterfaceToken(GetDescriptor())) {
705         ACCOUNT_LOGE("failed to write descriptor!");
706         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
707     }
708     if (!data.WriteInt32(id)) {
709         ACCOUNT_LOGE("failed to write id for setting constraints");
710         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
711     }
712     if (!data.WriteStringVector(constraints)) {
713         ACCOUNT_LOGE("failed to write stringVector for constraints");
714         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
715     }
716     if (!data.WriteBool(enable)) {
717         ACCOUNT_LOGE("failed to write bool for enable");
718         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
719     }
720 
721     MessageParcel reply;
722     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_OS_ACCOUNT_CONSTRAINTS, data, reply);
723     if (result != ERR_OK) {
724         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
725         return result;
726     }
727     result = reply.ReadInt32();
728     if (result != ERR_OK) {
729         ACCOUNT_LOGE("failed to read reply for set os account constraints, result %{public}d.", result);
730         return result;
731     }
732 
733     return ERR_OK;
734 }
735 
SetOsAccountProfilePhoto(const int id,const std::string & photo)736 ErrCode OsAccountProxy::SetOsAccountProfilePhoto(const int id, const std::string &photo)
737 {
738     MessageParcel data;
739     if (!data.WriteInterfaceToken(GetDescriptor())) {
740         ACCOUNT_LOGE("failed to write descriptor!");
741         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
742     }
743     if (!data.WriteInt32(id)) {
744         ACCOUNT_LOGE("failed to write id for setting photo");
745         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
746     }
747     if (!data.WriteInt32(photo.size() + 1)) {
748         ACCOUNT_LOGE("Failed to write photo size");
749         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
750     }
751     if (!data.WriteRawData(photo.c_str(), photo.size() + 1)) {
752         ACCOUNT_LOGE("Failed to write string for photo");
753         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
754     }
755 
756     MessageParcel reply;
757     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_OS_ACCOUNT_PROFILE_PHOTO, data, reply);
758     if (result != ERR_OK) {
759         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
760         return result;
761     }
762     result = reply.ReadInt32();
763     if (result != ERR_OK) {
764         ACCOUNT_LOGE("failed to read reply for set os account profile photo, result %{public}d.", result);
765         return result;
766     }
767 
768     return ERR_OK;
769 }
770 
ActivateOsAccount(const int id)771 ErrCode OsAccountProxy::ActivateOsAccount(const int id)
772 {
773     MessageParcel reply;
774     return SendRequestWithAccountId(OsAccountInterfaceCode::ACTIVATE_OS_ACCOUNT, reply, id);
775 }
776 
DeactivateOsAccount(const int id)777 ErrCode OsAccountProxy::DeactivateOsAccount(const int id)
778 {
779     MessageParcel reply;
780     return SendRequestWithAccountId(OsAccountInterfaceCode::DEACTIVATE_OS_ACCOUNT, reply, id);
781 }
782 
DeactivateAllOsAccounts()783 ErrCode OsAccountProxy::DeactivateAllOsAccounts()
784 {
785     MessageParcel data;
786     MessageParcel reply;
787     if (!data.WriteInterfaceToken(GetDescriptor())) {
788         ACCOUNT_LOGE("Write descriptor failed.");
789         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
790     }
791     ErrCode result = SendRequest(OsAccountInterfaceCode::DEACTIVATE_ALL_OS_ACCOUNTS, data, reply);
792     if (result != ERR_OK) {
793         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
794         return result;
795     }
796 
797     if (!reply.ReadInt32(result)) {
798         ACCOUNT_LOGE("Read result failed.");
799         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
800     }
801     if (result != ERR_OK) {
802         ACCOUNT_LOGE("Deactivate all os account failed, result=%{public}d.", result);
803     }
804     return result;
805 }
806 
StartOsAccount(const int id)807 ErrCode OsAccountProxy::StartOsAccount(const int id)
808 {
809     MessageParcel reply;
810     return SendRequestWithAccountId(OsAccountInterfaceCode::START_OS_ACCOUNT, reply, id);
811 }
812 
GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber,int & id)813 ErrCode OsAccountProxy::GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber, int &id)
814 {
815     MessageParcel data;
816     MessageParcel reply;
817 
818     if (!data.WriteInterfaceToken(GetDescriptor())) {
819         ACCOUNT_LOGE("failed to write descriptor!");
820         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
821     }
822 
823     if (!data.WriteInt64(serialNumber)) {
824         ACCOUNT_LOGE("failed to write int for serialNumber");
825         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
826     }
827     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_LOCAL_ID_FOR_SERIAL_NUMBER, data, reply);
828     if (result != ERR_OK) {
829         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
830         return result;
831     }
832     result = reply.ReadInt32();
833     if (result != ERR_OK) {
834         ACCOUNT_LOGE("failed to read reply for get os account id by serial number, result %{public}d.", result);
835         return result;
836     }
837     id = reply.ReadInt32();
838 
839     return ERR_OK;
840 }
841 
GetSerialNumberByOsAccountLocalId(const int & id,int64_t & serialNumber)842 ErrCode OsAccountProxy::GetSerialNumberByOsAccountLocalId(const int &id, int64_t &serialNumber)
843 {
844     MessageParcel reply;
845     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::GET_SERIAL_NUMBER_FOR_OS_ACCOUNT, reply, id);
846     if (result == ERR_OK) {
847         serialNumber = reply.ReadInt64();
848     }
849     return result;
850 }
851 
SubscribeOsAccount(const OsAccountSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & eventListener)852 ErrCode OsAccountProxy::SubscribeOsAccount(
853     const OsAccountSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &eventListener)
854 {
855     MessageParcel data;
856     MessageParcel reply;
857 
858     if (!data.WriteInterfaceToken(GetDescriptor())) {
859         ACCOUNT_LOGE("failed to write descriptor!");
860         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
861     }
862 
863     if (!data.WriteParcelable(&subscribeInfo)) {
864         ACCOUNT_LOGE("failed to write parcelable for subscribeInfo");
865         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
866     }
867 
868     if (!data.WriteRemoteObject(eventListener)) {
869         ACCOUNT_LOGE("failed to write remote object for eventListener");
870         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
871     }
872 
873     ErrCode result = SendRequest(OsAccountInterfaceCode::SUBSCRIBE_OS_ACCOUNT, data, reply);
874     if (result != ERR_OK) {
875         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
876         return result;
877     }
878 
879     result = reply.ReadInt32();
880     if (result != ERR_OK) {
881         ACCOUNT_LOGE("failed to read reply for subscriber os account, result %{public}d.", result);
882         return result;
883     }
884 
885     return ERR_OK;
886 }
887 
UnsubscribeOsAccount(const sptr<IRemoteObject> & eventListener)888 ErrCode OsAccountProxy::UnsubscribeOsAccount(const sptr<IRemoteObject> &eventListener)
889 {
890     MessageParcel data;
891     MessageParcel reply;
892 
893     if (!data.WriteInterfaceToken(GetDescriptor())) {
894         ACCOUNT_LOGE("failed to write descriptor!");
895         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
896     }
897 
898     if (!data.WriteRemoteObject(eventListener)) {
899         ACCOUNT_LOGE("failed to write remote object for eventListener");
900         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
901     }
902 
903     ErrCode result = SendRequest(OsAccountInterfaceCode::UNSUBSCRIBE_OS_ACCOUNT, data, reply);
904     if (result != ERR_OK) {
905         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
906         return result;
907     }
908 
909     result = reply.ReadInt32();
910     if (result != ERR_OK) {
911         ACCOUNT_LOGE("failed to read reply for unsubscribe os account.");
912     }
913 
914     return result;
915 }
GetOsAccountSwitchMod()916 OS_ACCOUNT_SWITCH_MOD OsAccountProxy::GetOsAccountSwitchMod()
917 {
918     MessageParcel data;
919     MessageParcel reply;
920 
921     if (!data.WriteInterfaceToken(GetDescriptor())) {
922         ACCOUNT_LOGE("failed to write descriptor!");
923         return OS_ACCOUNT_SWITCH_MOD::ERROR_MOD;
924     }
925 
926     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_SWITCH_MOD, data, reply);
927     if (result != ERR_OK) {
928         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
929         return OS_ACCOUNT_SWITCH_MOD::ERROR_MOD;
930     }
931 
932     OS_ACCOUNT_SWITCH_MOD osAccountSwitchMod = static_cast<OS_ACCOUNT_SWITCH_MOD>(reply.ReadInt32());
933 
934     return osAccountSwitchMod;
935 }
936 
SendRequestWithAccountId(OsAccountInterfaceCode code,MessageParcel & reply,int id)937 ErrCode OsAccountProxy::SendRequestWithAccountId(OsAccountInterfaceCode code, MessageParcel &reply, int id)
938 {
939     MessageParcel data;
940     if (!data.WriteInterfaceToken(GetDescriptor())) {
941         ACCOUNT_LOGE("failed to write descriptor!");
942         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
943     }
944 
945     if (!data.WriteInt32(id)) {
946         ACCOUNT_LOGE("failed to write int for id");
947         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
948     }
949 
950     ErrCode result = SendRequest(code, data, reply);
951     if (result != ERR_OK) {
952         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
953         return result;
954     }
955     if (!reply.ReadInt32(result)) {
956         ACCOUNT_LOGE("failed to read result for Message code %{public}d.", code);
957         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
958     }
959     if (result != ERR_OK) {
960         ACCOUNT_LOGE("failed to read reply for code %{public}d, result %{public}d.", code, result);
961     }
962     return result;
963 }
964 
SendRequest(OsAccountInterfaceCode code,MessageParcel & data,MessageParcel & reply)965 ErrCode OsAccountProxy::SendRequest(OsAccountInterfaceCode code, MessageParcel &data, MessageParcel &reply)
966 {
967     sptr<IRemoteObject> remote = Remote();
968     if (remote == nullptr) {
969         ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
970         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
971     }
972 
973     MessageOption option(MessageOption::TF_SYNC);
974     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
975     if (result != ERR_OK) {
976         ACCOUNT_LOGE("failed to send os account request, code = %{public}d, result = %{public}d", code, result);
977     }
978     return result;
979 }
980 
IsCurrentOsAccountVerified(bool & isVerified)981 ErrCode OsAccountProxy::IsCurrentOsAccountVerified(bool &isVerified)
982 {
983     MessageParcel data;
984     MessageParcel reply;
985 
986     if (!data.WriteInterfaceToken(GetDescriptor())) {
987         ACCOUNT_LOGE("failed to write descriptor!");
988         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
989     }
990 
991     ErrCode result = SendRequest(OsAccountInterfaceCode::IS_CURRENT_OS_ACCOUNT_VERIFIED, data, reply);
992     if (result != ERR_OK) {
993         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
994         return result;
995     }
996 
997     result = reply.ReadInt32();
998     if (result != ERR_OK) {
999         ACCOUNT_LOGE("failed to read reply for is current os account verified, result %{public}d.", result);
1000         return result;
1001     }
1002     isVerified = reply.ReadBool();
1003 
1004     return ERR_OK;
1005 }
1006 
IsOsAccountCompleted(const int id,bool & isOsAccountCompleted)1007 ErrCode OsAccountProxy::IsOsAccountCompleted(const int id, bool &isOsAccountCompleted)
1008 {
1009     MessageParcel reply;
1010     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::IS_OS_ACCOUNT_COMPLETED, reply, id);
1011     if (result != ERR_OK) {
1012         return result;
1013     }
1014     if (!reply.ReadBool(isOsAccountCompleted)) {
1015         ACCOUNT_LOGE("failed to read isOsAccountCompleted");
1016         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1017     }
1018     return ERR_OK;
1019 }
1020 
SetCurrentOsAccountIsVerified(const bool isVerified)1021 ErrCode OsAccountProxy::SetCurrentOsAccountIsVerified(const bool isVerified)
1022 {
1023     MessageParcel data;
1024     MessageParcel reply;
1025 
1026     if (!data.WriteInterfaceToken(GetDescriptor())) {
1027         ACCOUNT_LOGE("failed to write descriptor!");
1028         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1029     }
1030 
1031     if (!data.WriteBool(isVerified)) {
1032         ACCOUNT_LOGE("failed to write bool for isVerified");
1033         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1034     }
1035     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_CURRENT_OS_ACCOUNT_IS_VERIFIED, data, reply);
1036     if (result != ERR_OK) {
1037         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1038         return result;
1039     }
1040 
1041     result = reply.ReadInt32();
1042     if (result != ERR_OK) {
1043         ACCOUNT_LOGE("failed to read reply for set current os account verified, result %{public}d.", result);
1044         return result;
1045     }
1046     return ERR_OK;
1047 }
1048 
SetOsAccountIsVerified(const int id,const bool isVerified)1049 ErrCode OsAccountProxy::SetOsAccountIsVerified(const int id, const bool isVerified)
1050 {
1051     MessageParcel data;
1052     if (!data.WriteInterfaceToken(GetDescriptor())) {
1053         ACCOUNT_LOGE("failed to write descriptor!");
1054         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1055     }
1056     if (!data.WriteInt32(id)) {
1057         ACCOUNT_LOGE("failed to write id for setting verified status");
1058         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1059     }
1060     if (!data.WriteBool(isVerified)) {
1061         ACCOUNT_LOGE("failed to write bool for isVerified");
1062         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1063     }
1064 
1065     MessageParcel reply;
1066     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_OS_ACCOUNT_IS_VERIFIED, data, reply);
1067     if (result != ERR_OK) {
1068         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1069         return result;
1070     }
1071 
1072     result = reply.ReadInt32();
1073     if (result != ERR_OK) {
1074         ACCOUNT_LOGE("failed to read reply for set os account verified, result %{public}d.", result);
1075         return result;
1076     }
1077     return ERR_OK;
1078 }
1079 
DumpState(const int & id,std::vector<std::string> & state)1080 ErrCode OsAccountProxy::DumpState(const int &id, std::vector<std::string> &state)
1081 {
1082     MessageParcel reply;
1083     ErrCode result = SendRequestWithAccountId(OsAccountInterfaceCode::DUMP_STATE, reply, id);
1084     if (result != ERR_OK) {
1085         return result;
1086     }
1087 
1088     uint32_t size = reply.ReadUint32();
1089     for (uint32_t i = 0; i < size; i++) {
1090         std::string info = reply.ReadString();
1091         state.emplace_back(info);
1092     }
1093     return ERR_OK;
1094 }
1095 
GetCreatedOsAccountNumFromDatabase(const std::string & storeID,int & createdOsAccountNum)1096 ErrCode OsAccountProxy::GetCreatedOsAccountNumFromDatabase(const std::string& storeID,
1097     int &createdOsAccountNum)
1098 {
1099     MessageParcel data;
1100     MessageParcel reply;
1101 
1102     if (!data.WriteInterfaceToken(GetDescriptor())) {
1103         ACCOUNT_LOGE("failed to write descriptor!");
1104         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1105     }
1106 
1107     if (!data.WriteString(storeID)) {
1108         ACCOUNT_LOGE("failed to write storeID for getting created os account");
1109         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1110     }
1111     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_CREATED_OS_ACCOUNT_NUM_FROM_DATABASE, data, reply);
1112     if (result != ERR_OK) {
1113         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1114         return result;
1115     }
1116 
1117     result = reply.ReadInt32();
1118     if (result != ERR_OK) {
1119         ACCOUNT_LOGE("failed to read reply, result %{public}d.", result);
1120         return result;
1121     }
1122     createdOsAccountNum = reply.ReadInt32();
1123     return ERR_OK;
1124 }
1125 
GetSerialNumberFromDatabase(const std::string & storeID,int64_t & serialNumber)1126 ErrCode OsAccountProxy::GetSerialNumberFromDatabase(const std::string& storeID, int64_t &serialNumber)
1127 {
1128     MessageParcel data;
1129     MessageParcel reply;
1130 
1131     if (!data.WriteInterfaceToken(GetDescriptor())) {
1132         ACCOUNT_LOGE("failed to write descriptor!");
1133         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1134     }
1135 
1136     if (!data.WriteString(storeID)) {
1137         ACCOUNT_LOGE("failed to write storeID for getting serial number from database");
1138         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1139     }
1140     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_SERIAL_NUM_FROM_DATABASE, data, reply);
1141     if (result != ERR_OK) {
1142         ACCOUNT_LOGE("SendRequest failed, result %{public}d.", result);
1143         return result;
1144     }
1145 
1146     result = reply.ReadInt32();
1147     if (result != ERR_OK) {
1148         ACCOUNT_LOGE("failed to read reply, result %{public}d.", result);
1149         return result;
1150     }
1151     serialNumber = reply.ReadInt64();
1152     return ERR_OK;
1153 }
1154 
GetMaxAllowCreateIdFromDatabase(const std::string & storeID,int & id)1155 ErrCode OsAccountProxy::GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id)
1156 {
1157     MessageParcel data;
1158     MessageParcel reply;
1159 
1160     if (!data.WriteInterfaceToken(GetDescriptor())) {
1161         ACCOUNT_LOGE("failed to write descriptor!");
1162         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1163     }
1164 
1165     if (!data.WriteString(storeID)) {
1166         ACCOUNT_LOGE("failed to write string for isVerified");
1167         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1168     }
1169     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_MAX_ALLOW_CREATE_ID_FROM_DATABASE, data, reply);
1170     if (result != ERR_OK) {
1171         ACCOUNT_LOGE("SendRequest failed, result %{public}d.", result);
1172         return result;
1173     }
1174 
1175     result = reply.ReadInt32();
1176     if (result != ERR_OK) {
1177         ACCOUNT_LOGE("failed to read reply, result %{public}d.", result);
1178         return result;
1179     }
1180     id = reply.ReadInt32();
1181     return ERR_OK;
1182 }
1183 
GetOsAccountFromDatabase(const std::string & storeID,const int id,OsAccountInfo & osAccountInfo)1184 ErrCode OsAccountProxy::GetOsAccountFromDatabase(const std::string& storeID,
1185     const int id, OsAccountInfo &osAccountInfo)
1186 {
1187     MessageParcel data;
1188     MessageParcel reply;
1189 
1190     if (!data.WriteInterfaceToken(GetDescriptor())) {
1191         ACCOUNT_LOGE("failed to write descriptor!");
1192         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1193     }
1194 
1195     if (!data.WriteString(storeID)) {
1196         ACCOUNT_LOGE("failed to write storeID for getting os account form database");
1197         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1198     }
1199     if (!data.WriteInt32(id)) {
1200         ACCOUNT_LOGE("failed to write int for id");
1201         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1202     }
1203 
1204     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_FROM_DATABASE, data, reply);
1205     if (result != ERR_OK) {
1206         ACCOUNT_LOGE("SendRequest failed, result %{public}d.", result);
1207         return result;
1208     }
1209 
1210     result = reply.ReadInt32();
1211     if (result != ERR_OK) {
1212         ACCOUNT_LOGE("Failed to read reply, result %{public}d.", result);
1213         return result;
1214     }
1215     if (!ReadOsAccountInfo(reply, osAccountInfo)) {
1216         ACCOUNT_LOGE("Failed to read account info.");
1217         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1218     }
1219 
1220     return ERR_OK;
1221 }
1222 
GetOsAccountListFromDatabase(const std::string & storeID,std::vector<OsAccountInfo> & osAccountList)1223 ErrCode OsAccountProxy::GetOsAccountListFromDatabase(const std::string& storeID,
1224     std::vector<OsAccountInfo> &osAccountList)
1225 {
1226     MessageParcel data;
1227     MessageParcel reply;
1228 
1229     if (!data.WriteInterfaceToken(GetDescriptor())) {
1230         ACCOUNT_LOGE("failed to write descriptor!");
1231         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1232     }
1233 
1234     if (!data.WriteString(storeID)) {
1235         ACCOUNT_LOGE("failed to write storeID for getting os account list from database");
1236         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1237     }
1238     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_LIST_FROM_DATABASE, data, reply);
1239     if (result != ERR_OK) {
1240         ACCOUNT_LOGE("SendRequest failed, result %{public}d.", result);
1241         return result;
1242     }
1243 
1244     result = reply.ReadInt32();
1245     if (result != ERR_OK) {
1246         ACCOUNT_LOGE("failed to read reply, result %{public}d.", result);
1247         return result;
1248     }
1249     ReadOsAccountInfoList(reply, osAccountList);
1250     return ERR_OK;
1251 }
1252 
QueryActiveOsAccountIds(std::vector<int32_t> & ids)1253 ErrCode OsAccountProxy::QueryActiveOsAccountIds(std::vector<int32_t>& ids)
1254 {
1255     MessageParcel data;
1256     MessageParcel reply;
1257 
1258     if (!data.WriteInterfaceToken(GetDescriptor())) {
1259         ACCOUNT_LOGE("failed to write descriptor!");
1260         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1261     }
1262 
1263     ErrCode result = SendRequest(OsAccountInterfaceCode::QUERY_ACTIVE_OS_ACCOUNT_IDS, data, reply);
1264     if (result != ERR_OK) {
1265         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1266         return result;
1267     }
1268 
1269     result = reply.ReadInt32();
1270     if (result != ERR_OK) {
1271         ACCOUNT_LOGE("failed to read reply for query active os account ids, result %{public}d.", result);
1272         return result;
1273     }
1274 
1275     bool readFlag = reply.ReadInt32Vector(&ids);
1276     if (!readFlag) {
1277         ACCOUNT_LOGE("failed to read vector for active ids.");
1278         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1279     }
1280     return ERR_OK;
1281 }
1282 
QueryOsAccountConstraintSourceTypes(const int32_t id,const std::string & constraint,std::vector<ConstraintSourceTypeInfo> & constraintSourceTypeInfos)1283 ErrCode OsAccountProxy::QueryOsAccountConstraintSourceTypes(const int32_t id,
1284     const std::string &constraint, std::vector<ConstraintSourceTypeInfo> &constraintSourceTypeInfos)
1285 {
1286     constraintSourceTypeInfos.clear();
1287     MessageParcel data;
1288     if (!data.WriteInterfaceToken(GetDescriptor())) {
1289         ACCOUNT_LOGE("failed to write descriptor!");
1290         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1291     }
1292     if (!data.WriteInt32(id)) {
1293         ACCOUNT_LOGE("failed to write id for setting constraint source types");
1294         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1295     }
1296     if (!data.WriteString(constraint)) {
1297         ACCOUNT_LOGE("failed to write string for constraint");
1298         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1299     }
1300 
1301     MessageParcel reply;
1302     ErrCode result = SendRequest(OsAccountInterfaceCode::QUERY_OS_ACCOUNT_CONSTRAINT_SOURCE_TYPES, data, reply);
1303     if (result != ERR_OK) {
1304         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1305         return result;
1306     }
1307     result = reply.ReadInt32();
1308     if (result != ERR_OK) {
1309         return result;
1310     }
1311     uint32_t size = reply.ReadUint32();
1312     for (uint32_t i = 0; i < size; ++i) {
1313         ConstraintSourceTypeInfo constraintSrcInfo;
1314         constraintSrcInfo.localId = reply.ReadInt32();
1315         constraintSrcInfo.typeInfo = static_cast<ConstraintSourceType>(reply.ReadInt32());
1316         constraintSourceTypeInfos.push_back(constraintSrcInfo);
1317     }
1318     return ERR_OK;
1319 }
1320 
SetGlobalOsAccountConstraints(const std::vector<std::string> & constraints,const bool enable,const int32_t enforcerId,const bool isDeviceOwner)1321 ErrCode OsAccountProxy::SetGlobalOsAccountConstraints(const std::vector<std::string> &constraints,
1322     const bool enable, const int32_t enforcerId, const bool isDeviceOwner)
1323 {
1324     MessageParcel data;
1325     MessageParcel reply;
1326 
1327     if (!data.WriteInterfaceToken(GetDescriptor())) {
1328         ACCOUNT_LOGE("failed to write descriptor!");
1329         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1330     }
1331     if (!data.WriteStringVector(constraints)) {
1332         ACCOUNT_LOGE("failed to write stringVector for constraints");
1333         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1334     }
1335 
1336     if (!data.WriteBool(enable)) {
1337         ACCOUNT_LOGE("failed to write bool for enable");
1338         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1339     }
1340 
1341     if (!data.WriteInt32(enforcerId)) {
1342         ACCOUNT_LOGE("failed to write int for enforcerId");
1343         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1344     }
1345 
1346     if (!data.WriteBool(isDeviceOwner)) {
1347         ACCOUNT_LOGE("failed to write bool for isDeviceOwner");
1348         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1349     }
1350     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_GLOBAL_OS_ACCOUNT_CONSTRAINTS, data, reply);
1351     if (result != ERR_OK) {
1352         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1353         return result;
1354     }
1355     result = reply.ReadInt32();
1356     if (result != ERR_OK) {
1357         ACCOUNT_LOGE("failed to read reply for set global os account constraints.");
1358         return result;
1359     }
1360     return ERR_OK;
1361 }
1362 
SetSpecificOsAccountConstraints(const std::vector<std::string> & constraints,const bool enable,const int32_t targetId,const int32_t enforcerId,const bool isDeviceOwner)1363 ErrCode OsAccountProxy::SetSpecificOsAccountConstraints(const std::vector<std::string> &constraints,
1364     const bool enable, const int32_t targetId, const int32_t enforcerId, const bool isDeviceOwner)
1365 {
1366     MessageParcel data;
1367     MessageParcel reply;
1368 
1369     if (!data.WriteInterfaceToken(GetDescriptor())) {
1370         ACCOUNT_LOGE("failed to write descriptor!");
1371         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1372     }
1373     if (!data.WriteStringVector(constraints)) {
1374         ACCOUNT_LOGE("failed to write stringVector for constraints");
1375         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1376     }
1377 
1378     if (!data.WriteBool(enable)) {
1379         ACCOUNT_LOGE("failed to write bool for enable");
1380         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1381     }
1382 
1383     if (!data.WriteInt32(targetId)) {
1384         ACCOUNT_LOGE("failed to write int for targetId");
1385         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1386     }
1387 
1388     if (!data.WriteInt32(enforcerId)) {
1389         ACCOUNT_LOGE("failed to write int for enforcerId");
1390         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1391     }
1392 
1393     if (!data.WriteBool(isDeviceOwner)) {
1394         ACCOUNT_LOGE("failed to write bool for isDeviceOwner");
1395         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1396     }
1397     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_SPECIFIC_OS_ACCOUNT_CONSTRAINTS, data, reply);
1398     if (result != ERR_OK) {
1399         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1400         return result;
1401     }
1402     result = reply.ReadInt32();
1403     if (result != ERR_OK) {
1404         ACCOUNT_LOGE("failed to read reply for set specific os account constraints.");
1405         return result;
1406     }
1407     return ERR_OK;
1408 }
1409 
SetDefaultActivatedOsAccount(const int32_t id)1410 ErrCode OsAccountProxy::SetDefaultActivatedOsAccount(const int32_t id)
1411 {
1412     MessageParcel data;
1413     if (!data.WriteInterfaceToken(GetDescriptor())) {
1414         ACCOUNT_LOGE("failed to write descriptor!");
1415         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1416     }
1417     if (!data.WriteInt32(id)) {
1418         ACCOUNT_LOGE("failed to write id for setting default activated os account");
1419         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1420     }
1421     MessageParcel reply;
1422     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_DEFAULT_ACTIVATED_OS_ACCOUNT, data, reply);
1423     if (result != ERR_OK) {
1424         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1425         return result;
1426     }
1427     if (!reply.ReadInt32(result)) {
1428         ACCOUNT_LOGE("failed to read result for set default activated os account.");
1429         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1430     }
1431     return result;
1432 }
1433 
GetDefaultActivatedOsAccount(int32_t & id)1434 ErrCode OsAccountProxy::GetDefaultActivatedOsAccount(int32_t &id)
1435 {
1436     MessageParcel data;
1437     if (!data.WriteInterfaceToken(GetDescriptor())) {
1438         ACCOUNT_LOGE("failed to write descriptor!");
1439         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1440     }
1441     MessageParcel reply;
1442     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_DEFAULT_ACTIVATED_OS_ACCOUNT, data, reply);
1443     if (result != ERR_OK) {
1444         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1445         return result;
1446     }
1447     if (!reply.ReadInt32(result)) {
1448         ACCOUNT_LOGE("failed to read result for get default activated os account.");
1449         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1450     }
1451     if (result != ERR_OK) {
1452         ACCOUNT_LOGE("failed to get default activated os account, result %{public}d.", result);
1453         return result;
1454     }
1455     if (!reply.ReadInt32(id)) {
1456         ACCOUNT_LOGE("failed to read local id");
1457         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1458     }
1459     return ERR_OK;
1460 }
1461 
ReadOsAccountInfo(MessageParcel & data,OsAccountInfo & accountInfo)1462 bool OsAccountProxy::ReadOsAccountInfo(MessageParcel &data, OsAccountInfo &accountInfo)
1463 {
1464     int32_t accountSize;
1465     if (!data.ReadInt32(accountSize)) {
1466         ACCOUNT_LOGE("Failed to read accountSize");
1467         return false;
1468     }
1469     auto readRawData = data.ReadRawData(accountSize);
1470     if (readRawData == nullptr) {
1471         ACCOUNT_LOGE("Failed to read accountData accountSize = %{public}d", accountSize);
1472         return false;
1473     }
1474     const char *accountData = reinterpret_cast<const char *>(readRawData);
1475     std::string accountJson = std::string(accountData, accountSize - 1);
1476     nlohmann::json jsonObject = nlohmann::json::parse(accountJson, nullptr, false);
1477     if (jsonObject.is_discarded()) {
1478         ACCOUNT_LOGE("AccountJson is discarded");
1479         return false;
1480     }
1481     accountInfo.FromJson(jsonObject);
1482 
1483     return true;
1484 }
1485 
ReadOsAccountInfoList(MessageParcel & data,std::vector<OsAccountInfo> & infoList)1486 bool OsAccountProxy::ReadOsAccountInfoList(MessageParcel &data, std::vector<OsAccountInfo> &infoList)
1487 {
1488     infoList.clear();
1489     uint32_t accountsStrLength;
1490     if (!data.ReadUint32(accountsStrLength)) {
1491         ACCOUNT_LOGE("Failed to read accountsStrLength");
1492         return false;
1493     }
1494     if (accountsStrLength == 0) {
1495         ACCOUNT_LOGE("accountsStrLength is invalid, accountsStrLength = %{public}d", accountsStrLength);
1496         return false;
1497     }
1498     auto readRawData = data.ReadRawData(accountsStrLength);
1499     if (readRawData == nullptr) {
1500         ACCOUNT_LOGE("Failed to read accountArrayData accountArraySize = %{public}d", accountsStrLength);
1501         return false;
1502     }
1503     const char *accountsStrData = reinterpret_cast<const char *>(readRawData);
1504     std::string accountsStr = std::string(accountsStrData, accountsStrLength - 1);
1505     nlohmann::json accounts = nlohmann::json::parse(accountsStr, nullptr, false);
1506     if (accounts.is_discarded()) {
1507         ACCOUNT_LOGE("JsonArray is discarded");
1508         return false;
1509     }
1510 
1511     for (const auto &json : accounts) {
1512         OsAccountInfo accountInfo;
1513         accountInfo.FromJson(json);
1514         infoList.emplace_back(accountInfo);
1515     }
1516 
1517     return true;
1518 }
1519 
GetOsAccountShortName(std::string & shortName)1520 ErrCode OsAccountProxy::GetOsAccountShortName(std::string &shortName)
1521 {
1522     MessageParcel data;
1523     if (!data.WriteInterfaceToken(GetDescriptor())) {
1524         ACCOUNT_LOGE("failed to write descriptor!");
1525         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1526     }
1527 
1528     MessageParcel reply;
1529     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_SHORT_NAME, data, reply);
1530     if (result != ERR_OK) {
1531         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1532         return result;
1533     }
1534 
1535     result = reply.ReadInt32();
1536     if (result != ERR_OK) {
1537         ACCOUNT_LOGE("failed to read reply for is current os account verified, result %{public}d.", result);
1538         return result;
1539     }
1540     if (!reply.ReadString(shortName)) {
1541         ACCOUNT_LOGE("failed to read short name");
1542         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1543     }
1544 
1545     return ERR_OK;
1546 }
1547 
GetOsAccountName(std::string & name)1548 ErrCode OsAccountProxy::GetOsAccountName(std::string &name)
1549 {
1550     MessageParcel data;
1551     if (!data.WriteInterfaceToken(GetDescriptor())) {
1552         ACCOUNT_LOGE("Failed to write descriptor!");
1553         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1554     }
1555 
1556     MessageParcel reply;
1557     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_NAME, data, reply);
1558     if (result != ERR_OK) {
1559         ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1560         return result;
1561     }
1562 
1563     if (!reply.ReadInt32(result)) {
1564         ACCOUNT_LOGE("Read result from reply failed.");
1565         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1566     }
1567     if (result != ERR_OK) {
1568         ACCOUNT_LOGE("Failed to read reply for is current os account verified, result=%{public}d.", result);
1569         return result;
1570     }
1571     if (!reply.ReadString(name)) {
1572         ACCOUNT_LOGE("Failed to read short name");
1573         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1574     }
1575 
1576     return ERR_OK;
1577 }
1578 
GetOsAccountShortNameById(const int32_t id,std::string & shortName)1579 ErrCode OsAccountProxy::GetOsAccountShortNameById(const int32_t id, std::string &shortName)
1580 {
1581     MessageParcel data;
1582     if (!data.WriteInterfaceToken(GetDescriptor())) {
1583         ACCOUNT_LOGE("Write descriptor failed.");
1584         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1585     }
1586     if (!data.WriteInt32(id)) {
1587         ACCOUNT_LOGE("Write id failed.");
1588         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1589     }
1590 
1591     MessageParcel reply;
1592     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_SHORT_NAME_BY_ID, data, reply);
1593     if (result != ERR_OK) {
1594         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
1595         return result;
1596     }
1597 
1598     if (!reply.ReadInt32(result)) {
1599         ACCOUNT_LOGE("Read result from reply failed.");
1600         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1601     }
1602     if (result != ERR_OK) {
1603         ACCOUNT_LOGE("Get os account short name failed, result=%{public}d.", result);
1604         return result;
1605     }
1606     if (!reply.ReadString(shortName)) {
1607         ACCOUNT_LOGE("Read short name failed.");
1608         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1609     }
1610 
1611     return ERR_OK;
1612 }
1613 
IsOsAccountForeground(const int32_t localId,const uint64_t displayId,bool & isForeground)1614 ErrCode OsAccountProxy::IsOsAccountForeground(const int32_t localId, const uint64_t displayId, bool &isForeground)
1615 {
1616     MessageParcel data;
1617     if (!data.WriteInterfaceToken(GetDescriptor())) {
1618         ACCOUNT_LOGE("Write descriptor failed.");
1619         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1620     }
1621     if (!data.WriteInt32(localId)) {
1622         ACCOUNT_LOGE("Write localId failed.");
1623         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1624     }
1625     if (!data.WriteUint64(displayId)) {
1626         ACCOUNT_LOGE("Write displayId failed.");
1627         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1628     }
1629     MessageParcel reply;
1630     ErrCode result = SendRequest(OsAccountInterfaceCode::IS_OS_ACCOUNT_FOREGROUND, data, reply);
1631     if (result != ERR_OK) {
1632         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
1633         return result;
1634     }
1635     if (!reply.ReadInt32(result)) {
1636         ACCOUNT_LOGE("Read result failed.");
1637         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1638     }
1639     if (result != ERR_OK) {
1640         ACCOUNT_LOGE("IsOsAccountForeground failed, result=%{public}d.", result);
1641         return result;
1642     }
1643     if (!reply.ReadBool(isForeground)) {
1644         ACCOUNT_LOGE("Read isForeground failed.");
1645         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1646     }
1647     return ERR_OK;
1648 }
1649 
GetForegroundOsAccountLocalId(const uint64_t displayId,int32_t & localId)1650 ErrCode OsAccountProxy::GetForegroundOsAccountLocalId(const uint64_t displayId, int32_t &localId)
1651 {
1652     MessageParcel data;
1653     if (!data.WriteInterfaceToken(GetDescriptor())) {
1654         ACCOUNT_LOGE("Write descriptor failed.");
1655         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1656     }
1657     if (!data.WriteUint64(displayId)) {
1658         ACCOUNT_LOGE("Write displayId failed.");
1659         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1660     }
1661     MessageParcel reply;
1662     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_FOREGROUND_OS_ACCOUNT_LOCAL_ID, data, reply);
1663     if (result != ERR_OK) {
1664         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
1665         return result;
1666     }
1667     if (!reply.ReadInt32(result)) {
1668         ACCOUNT_LOGE("Read result from reply failed.");
1669         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1670     }
1671     if (result != ERR_OK) {
1672         ACCOUNT_LOGE("GetForegroundOsAccountLocalId failed, result=%{public}d.", result);
1673         return result;
1674     }
1675     if (!reply.ReadInt32(localId)) {
1676         ACCOUNT_LOGE("Read localId failed");
1677         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1678     }
1679     return ERR_OK;
1680 }
1681 
GetForegroundOsAccounts(std::vector<ForegroundOsAccount> & accounts)1682 ErrCode OsAccountProxy::GetForegroundOsAccounts(std::vector<ForegroundOsAccount> &accounts)
1683 {
1684     MessageParcel data;
1685     if (!data.WriteInterfaceToken(GetDescriptor())) {
1686         ACCOUNT_LOGE("Write descriptor failed");
1687         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1688     }
1689     MessageParcel reply;
1690     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_FOREGROUND_OS_ACCOUNTS, data, reply);
1691     if (result != ERR_OK) {
1692         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
1693         return result;
1694     }
1695     if (!reply.ReadInt32(result)) {
1696         ACCOUNT_LOGE("Read result failed.");
1697         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1698     }
1699     if (result != ERR_OK) {
1700         ACCOUNT_LOGE("GetForegroundOsAccounts failed, result=%{public}d.", result);
1701         return result;
1702     }
1703     uint32_t size = 0;
1704     if (!reply.ReadUint32(size)) {
1705         ACCOUNT_LOGE("Read foregroundAccounts size failed.");
1706         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1707     }
1708     if (size >= ACCOUNT_MAX_SIZE) {
1709         ACCOUNT_LOGE("Account size exceeded.");
1710         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
1711     }
1712     accounts.clear();
1713     for (uint32_t i = 0; i < size; ++i) {
1714         ForegroundOsAccount foregroundOsAccount;
1715         if (!reply.ReadInt32(foregroundOsAccount.localId) || !reply.ReadUint64(foregroundOsAccount.displayId)) {
1716             ACCOUNT_LOGE("Read ForegroundOsAccount failed.");
1717             return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1718         }
1719         accounts.emplace_back(foregroundOsAccount);
1720     }
1721     return ERR_OK;
1722 }
1723 
GetBackgroundOsAccountLocalIds(std::vector<int32_t> & localIds)1724 ErrCode OsAccountProxy::GetBackgroundOsAccountLocalIds(std::vector<int32_t> &localIds)
1725 {
1726     MessageParcel data;
1727     if (!data.WriteInterfaceToken(GetDescriptor())) {
1728         ACCOUNT_LOGE("Write descriptor failed.");
1729         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1730     }
1731     MessageParcel reply;
1732     ErrCode result = SendRequest(OsAccountInterfaceCode::GET_BACKGROUND_OS_ACCOUNT_LOCAL_IDS, data, reply);
1733     if (result != ERR_OK) {
1734         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
1735         return result;
1736     }
1737     if (!reply.ReadInt32(result)) {
1738         ACCOUNT_LOGE("Read result failed.");
1739         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1740     }
1741     if (result != ERR_OK) {
1742         ACCOUNT_LOGE("GetBackgroundOsAccountLocalIds failed, result=%{public}d.", result);
1743         return result;
1744     }
1745     localIds.clear();
1746     if (!reply.ReadInt32Vector(&localIds)) {
1747         ACCOUNT_LOGE("Read localIds failed.");
1748         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1749     }
1750     return ERR_OK;
1751 }
1752 
SetOsAccountToBeRemoved(int32_t localId,bool toBeRemoved)1753 ErrCode OsAccountProxy::SetOsAccountToBeRemoved(int32_t localId, bool toBeRemoved)
1754 {
1755     MessageParcel data;
1756     if (!data.WriteInterfaceToken(GetDescriptor())) {
1757         ACCOUNT_LOGE("Write descriptor failed.");
1758         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1759     }
1760     if (!data.WriteInt32(localId)) {
1761         ACCOUNT_LOGE("Write localId failed.");
1762         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1763     }
1764     if (!data.WriteBool(toBeRemoved)) {
1765         ACCOUNT_LOGE("Write toBeRemoved failed.");
1766         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
1767     }
1768     MessageParcel reply;
1769     ErrCode result = SendRequest(OsAccountInterfaceCode::SET_OS_ACCOUNT_TO_BE_REMOVED, data, reply);
1770     if (result != ERR_OK) {
1771         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
1772         return result;
1773     }
1774     if (!reply.ReadInt32(result)) {
1775         ACCOUNT_LOGE("Read result failed.");
1776         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1777     }
1778     return result;
1779 }
1780 }  // namespace AccountSA
1781 }  // namespace OHOS
1782