1 /*
2 * Copyright (c) 2021-2022 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 {
OsAccountProxy(const sptr<IRemoteObject> & object)20 OsAccountProxy::OsAccountProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IOsAccount>(object)
21 {}
22
~OsAccountProxy()23 OsAccountProxy::~OsAccountProxy()
24 {}
25
CreateOsAccount(const std::string & name,const OsAccountType & type,OsAccountInfo & osAccountInfo)26 ErrCode OsAccountProxy::CreateOsAccount(
27 const std::string &name, const OsAccountType &type, OsAccountInfo &osAccountInfo)
28 {
29 MessageParcel data;
30 MessageParcel reply;
31
32 if (!data.WriteInterfaceToken(GetDescriptor())) {
33 ACCOUNT_LOGE("failed to write descriptor!");
34 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
35 }
36
37 if (!data.WriteString(name)) {
38 ACCOUNT_LOGE("failed to write string for name");
39 return ERR_OSACCOUNT_KIT_WRITE_LOCALNAME_ERROR;
40 }
41
42 if (!data.WriteInt32(type)) {
43 ACCOUNT_LOGE("failed to write account type.");
44 return ERR_OSACCOUNT_KIT_WRITE_OSACCOUNT_TYPE_ERROR;
45 }
46 ErrCode result = SendRequest(IOsAccount::Message::CREATE_OS_ACCOUNT, data, reply);
47 if (result != ERR_OK) {
48 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
49 return result;
50 }
51
52 result = reply.ReadInt32();
53 if (result != ERR_OK) {
54 ACCOUNT_LOGE("failed to read reply for create os account.");
55 return result;
56 }
57 std::shared_ptr<OsAccountInfo> infoPtr(reply.ReadParcelable<OsAccountInfo>());
58 if (infoPtr == nullptr) {
59 ACCOUNT_LOGE("failed to read OsAccountInfo");
60 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
61 }
62 osAccountInfo = *infoPtr;
63 return ERR_OK;
64 }
65
CreateOsAccountForDomain(const OsAccountType & type,const DomainAccountInfo & domainInfo,OsAccountInfo & osAccountInfo)66 ErrCode OsAccountProxy::CreateOsAccountForDomain(
67 const OsAccountType &type, const DomainAccountInfo &domainInfo, OsAccountInfo &osAccountInfo)
68 {
69 MessageParcel data;
70 MessageParcel reply;
71
72 if (!data.WriteInterfaceToken(GetDescriptor())) {
73 ACCOUNT_LOGE("failed to write descriptor!");
74 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
75 }
76
77 if (!data.WriteInt32(type)) {
78 ACCOUNT_LOGE("failed to write type ");
79 return ERR_OSACCOUNT_KIT_WRITE_OSACCOUNT_TYPE_ERROR;
80 }
81
82 if (!data.WriteString(domainInfo.domain_)) {
83 ACCOUNT_LOGE("failed to write string for domain");
84 return ERR_OSACCOUNT_KIT_WRITE_DOMAIN_ERROR;
85 }
86
87 if (!data.WriteString(domainInfo.accountName_)) {
88 ACCOUNT_LOGE("failed to write string for domain account name");
89 return ERR_OSACCOUNT_KIT_WRITE_DOMAIN_ACCOUNT_NAME_ERROR;
90 }
91
92 ErrCode result = SendRequest(IOsAccount::Message::CREATE_OS_ACCOUNT_FOR_DOMAIN, data, reply);
93 if (result != ERR_OK) {
94 ACCOUNT_LOGE("failed to send request, result %{public}d.", result);
95 return result;
96 }
97
98 result = reply.ReadInt32();
99 if (result != ERR_OK) {
100 ACCOUNT_LOGE("failed to read reply for create os account for domain, result %{public}d.", result);
101 return result;
102 }
103 std::shared_ptr<OsAccountInfo> infoPtr(reply.ReadParcelable<OsAccountInfo>());
104 if (infoPtr == nullptr) {
105 ACCOUNT_LOGE("failed to read OsAccountInfo");
106 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
107 }
108 osAccountInfo = *infoPtr;
109 return ERR_OK;
110 }
111
RemoveOsAccount(const int id)112 ErrCode OsAccountProxy::RemoveOsAccount(const int id)
113 {
114 MessageParcel data;
115 MessageParcel reply;
116
117 if (!data.WriteInterfaceToken(GetDescriptor())) {
118 ACCOUNT_LOGE("failed to write descriptor!");
119 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
120 }
121
122 if (!data.WriteInt32(id)) {
123 ACCOUNT_LOGE("failed to write int for id");
124 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
125 }
126
127 ErrCode result = SendRequest(IOsAccount::Message::REMOVE_OS_ACCOUNT, data, reply);
128 if (result != ERR_OK) {
129 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
130 return result;
131 }
132
133 result = reply.ReadInt32();
134 if (result != ERR_OK) {
135 ACCOUNT_LOGE("failed to read reply for remove os account info, result %{public}d.", result);
136 return result;
137 }
138
139 return ERR_OK;
140 }
141
IsOsAccountExists(const int id,bool & isOsAccountExists)142 ErrCode OsAccountProxy::IsOsAccountExists(const int id, bool &isOsAccountExists)
143 {
144 MessageParcel data;
145 MessageParcel reply;
146
147 if (!data.WriteInterfaceToken(GetDescriptor())) {
148 ACCOUNT_LOGE("failed to write descriptor!");
149 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
150 }
151
152 if (!data.WriteInt32(id)) {
153 ACCOUNT_LOGE("failed to write int for id");
154 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
155 }
156
157 ErrCode result = SendRequest(IOsAccount::Message::IS_OS_ACCOUNT_EXISTS, data, reply);
158 if (result != ERR_OK) {
159 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
160 return result;
161 }
162 result = reply.ReadInt32();
163 if (result != ERR_OK) {
164 ACCOUNT_LOGE("failed to read reply for is os account exists, result %{public}d.", result);
165 return result;
166 }
167 isOsAccountExists = reply.ReadBool();
168
169 return ERR_OK;
170 }
171
IsOsAccountActived(const int id,bool & isOsAccountActived)172 ErrCode OsAccountProxy::IsOsAccountActived(const int id, bool &isOsAccountActived)
173 {
174 MessageParcel data;
175 MessageParcel reply;
176
177 if (!data.WriteInterfaceToken(GetDescriptor())) {
178 ACCOUNT_LOGE("failed to write descriptor!");
179 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
180 }
181
182 if (!data.WriteInt32(id)) {
183 ACCOUNT_LOGE("failed to write int for id");
184 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
185 }
186 ErrCode result = SendRequest(IOsAccount::Message::IS_OS_ACCOUNT_ACTIVED, data, reply);
187 if (result != ERR_OK) {
188 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
189 return result;
190 }
191 result = reply.ReadInt32();
192 if (result != ERR_OK) {
193 ACCOUNT_LOGE("failed to read reply for is os account activated, result %{public}d.", result);
194 return result;
195 }
196 isOsAccountActived = reply.ReadBool();
197
198 return ERR_OK;
199 }
200
CheckOsAccountConstraintEnabled(IOsAccount::Message code,const int id,const std::string & constraint,bool & isEnabled)201 ErrCode OsAccountProxy::CheckOsAccountConstraintEnabled(
202 IOsAccount::Message code, const int id, const std::string &constraint, bool &isEnabled)
203 {
204 MessageParcel data;
205 if (!data.WriteInterfaceToken(GetDescriptor())) {
206 ACCOUNT_LOGE("failed to write descriptor!");
207 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
208 }
209 if (!data.WriteInt32(id)) {
210 ACCOUNT_LOGE("failed to write int for id");
211 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
212 }
213 if (!data.WriteString(constraint)) {
214 ACCOUNT_LOGE("failed to write string for constraint");
215 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
216 }
217 MessageParcel reply;
218 ErrCode ret = SendRequest(code, data, reply);
219 if (ret != ERR_OK) {
220 ACCOUNT_LOGE("SendRequest err, result %{public}d.", ret);
221 return ret;
222 }
223 if (!reply.ReadInt32(ret)) {
224 ACCOUNT_LOGE("failed to read result for check os account constraint enable.");
225 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
226 }
227 if (ret != ERR_OK) {
228 ACCOUNT_LOGE("check os account constraint enabled failed, result %{public}d.", ret);
229 return ret;
230 }
231 if (!reply.ReadBool(isEnabled)) {
232 ACCOUNT_LOGE("failed to read result for check os account constraint enable.");
233 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
234 }
235 return ERR_OK;
236 }
237
IsOsAccountConstraintEnable(const int id,const std::string & constraint,bool & isConstraintEnable)238 ErrCode OsAccountProxy::IsOsAccountConstraintEnable(
239 const int id, const std::string &constraint, bool &isConstraintEnable)
240 {
241 return CheckOsAccountConstraintEnabled(
242 IOsAccount::Message::IS_OS_ACCOUNT_CONSTRAINT_ENABLE, id, constraint, isConstraintEnable);
243 }
244
CheckOsAccountConstraintEnabled(const int id,const std::string & constraint,bool & isEnabled)245 ErrCode OsAccountProxy::CheckOsAccountConstraintEnabled(
246 const int id, const std::string &constraint, bool &isEnabled)
247 {
248 return CheckOsAccountConstraintEnabled(
249 IOsAccount::Message::CHECK_OS_ACCOUNT_CONSTRAINT_ENABLED, id, constraint, isEnabled);
250 }
251
IsOsAccountVerified(const int id,bool & isVerified)252 ErrCode OsAccountProxy::IsOsAccountVerified(const int id, bool &isVerified)
253 {
254 MessageParcel data;
255 MessageParcel reply;
256
257 if (!data.WriteInterfaceToken(GetDescriptor())) {
258 ACCOUNT_LOGE("failed to write descriptor!");
259 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
260 }
261
262 if (!data.WriteInt32(id)) {
263 ACCOUNT_LOGE("failed to write int for id");
264 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
265 }
266 ErrCode result = SendRequest(IOsAccount::Message::IS_OS_ACCOUNT_VERIFIED, data, reply);
267 if (result != ERR_OK) {
268 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
269 return result;
270 }
271 result = reply.ReadInt32();
272 if (result != ERR_OK) {
273 ACCOUNT_LOGE("failed to read reply for is os account verified, result %{public}d.", result);
274 return result;
275 }
276 isVerified = reply.ReadBool();
277
278 return ERR_OK;
279 }
280
GetCreatedOsAccountsCount(unsigned int & osAccountsCount)281 ErrCode OsAccountProxy::GetCreatedOsAccountsCount(unsigned int &osAccountsCount)
282 {
283 osAccountsCount = 0;
284 MessageParcel data;
285 MessageParcel reply;
286
287 if (!data.WriteInterfaceToken(GetDescriptor())) {
288 ACCOUNT_LOGE("failed to write descriptor!");
289 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
290 }
291
292 ErrCode result = SendRequest(IOsAccount::Message::GET_CREATED_OS_ACCOUNT_COUNT, data, reply);
293 if (result != ERR_OK) {
294 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
295 return result;
296 }
297 result = reply.ReadInt32();
298 if (result != ERR_OK) {
299 ACCOUNT_LOGE("failed to read reply for get os account count, result %{public}d.", result);
300 return result;
301 }
302 osAccountsCount = reply.ReadUint32();
303
304 return ERR_OK;
305 }
306
GetOsAccountLocalIdFromProcess(int & id)307 ErrCode OsAccountProxy::GetOsAccountLocalIdFromProcess(int &id)
308 {
309 MessageParcel data;
310 MessageParcel reply;
311
312 if (!data.WriteInterfaceToken(GetDescriptor())) {
313 ACCOUNT_LOGE("failed to write descriptor!");
314 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
315 }
316
317 ErrCode result = SendRequest(IOsAccount::Message::GET_OS_ACCOUNT_LOCAL_ID_FROM_PROCESS, data, reply);
318 if (result != ERR_OK) {
319 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
320 return result;
321 }
322 result = reply.ReadInt32();
323 if (result != ERR_OK) {
324 ACCOUNT_LOGE("failed to read reply for get os account id from process, result %{public}d.", result);
325 return result;
326 }
327 id = reply.ReadInt32();
328
329 return ERR_OK;
330 }
331
IsMainOsAccount(bool & isMainOsAccount)332 ErrCode OsAccountProxy::IsMainOsAccount(bool &isMainOsAccount)
333 {
334 MessageParcel data;
335 MessageParcel reply;
336
337 if (!data.WriteInterfaceToken(GetDescriptor())) {
338 ACCOUNT_LOGE("failed to write descriptor!");
339 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
340 }
341
342 ErrCode result = SendRequest(IOsAccount::Message::IS_MAIN_OS_ACCOUNT, data, reply);
343 if (result != ERR_OK) {
344 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
345 return result;
346 }
347 result = reply.ReadInt32();
348 if (result != ERR_OK) {
349 ACCOUNT_LOGE("failed to read reply for is main os account, result %{public}d.", result);
350 return result;
351 }
352 isMainOsAccount = reply.ReadBool();
353
354 return ERR_OK;
355 }
356
GetOsAccountLocalIdFromDomain(const DomainAccountInfo & domainInfo,int & id)357 ErrCode OsAccountProxy::GetOsAccountLocalIdFromDomain(const DomainAccountInfo &domainInfo, int &id)
358 {
359 MessageParcel data;
360 MessageParcel reply;
361
362 if (!data.WriteInterfaceToken(GetDescriptor())) {
363 ACCOUNT_LOGE("failed to write descriptor!");
364 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
365 }
366
367 if (!data.WriteString(domainInfo.domain_)) {
368 ACCOUNT_LOGE("failed to write int for domain.");
369 return ERR_OSACCOUNT_KIT_WRITE_DOMAIN_ERROR;
370 }
371 if (!data.WriteString(domainInfo.accountName_)) {
372 ACCOUNT_LOGE("failed to write int for domain account name.");
373 return ERR_OSACCOUNT_KIT_WRITE_DOMAIN_ACCOUNT_NAME_ERROR;
374 }
375 ErrCode result = SendRequest(IOsAccount::Message::GET_OS_ACCOUNT_LOCAL_ID_FROM_DOMAIN, data, reply);
376 if (result != ERR_OK) {
377 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
378 return result;
379 }
380 result = reply.ReadInt32();
381 if (result != ERR_OK) {
382 ACCOUNT_LOGE("read from reply err, result %{public}d.", result);
383 return result;
384 }
385 id = reply.ReadInt32();
386
387 return ERR_OK;
388 }
389
QueryMaxOsAccountNumber(int & maxOsAccountNumber)390 ErrCode OsAccountProxy::QueryMaxOsAccountNumber(int &maxOsAccountNumber)
391 {
392 MessageParcel data;
393 MessageParcel reply;
394
395 if (!data.WriteInterfaceToken(GetDescriptor())) {
396 ACCOUNT_LOGE("failed to write descriptor!");
397 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
398 }
399
400 ErrCode result = SendRequest(IOsAccount::Message::QUERY_MAX_OS_ACCOUNT_NUMBER, data, reply);
401 if (result != ERR_OK) {
402 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
403 return result;
404 }
405 result = reply.ReadInt32();
406 if (result != ERR_OK) {
407 ACCOUNT_LOGE("failed to read reply for query os account number, result %{public}d.", result);
408 return result;
409 }
410 maxOsAccountNumber = reply.ReadInt32();
411
412 return ERR_OK;
413 }
414
GetOsAccountAllConstraints(const int id,std::vector<std::string> & constraints)415 ErrCode OsAccountProxy::GetOsAccountAllConstraints(const int id, std::vector<std::string> &constraints)
416 {
417 MessageParcel data;
418 MessageParcel reply;
419
420 if (!data.WriteInterfaceToken(GetDescriptor())) {
421 ACCOUNT_LOGE("failed to write descriptor!");
422 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
423 }
424
425 if (!data.WriteInt32(id)) {
426 ACCOUNT_LOGE("failed to write int for id");
427 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
428 }
429 ErrCode result = SendRequest(IOsAccount::Message::GET_OS_ACCOUNT_ALL_CONSTRAINTS, data, reply);
430 if (result != ERR_OK) {
431 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
432 return result;
433 }
434 result = reply.ReadInt32();
435 if (result != ERR_OK) {
436 ACCOUNT_LOGE("failed to read reply, result %{public}d.", result);
437 return result;
438 }
439 bool readFlag = reply.ReadStringVector(&constraints);
440 if (!readFlag) {
441 ACCOUNT_LOGE("ReadStringVector failed, result %{public}d.", result);
442 return ERR_OSACCOUNT_KIT_READ_CONSTRAINTS_ERROR;
443 }
444
445 return ERR_OK;
446 }
447
QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> & osAccountInfos)448 ErrCode OsAccountProxy::QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &osAccountInfos)
449 {
450 MessageParcel data;
451 MessageParcel reply;
452
453 if (!data.WriteInterfaceToken(GetDescriptor())) {
454 ACCOUNT_LOGE("failed to write descriptor!");
455 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
456 }
457
458 ErrCode result = SendRequest(IOsAccount::Message::QUERY_ALL_CREATED_OS_ACCOUNTS, data, reply);
459 if (result != ERR_OK) {
460 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
461 return result;
462 }
463 result = reply.ReadInt32();
464 if (result != ERR_OK) {
465 ACCOUNT_LOGE("failed to read reply for query all os accounts, result %{public}d.", result);
466 return result;
467 }
468 ReadParcelableVector(osAccountInfos, reply);
469
470 return ERR_OK;
471 }
472
QueryCurrentOsAccount(OsAccountInfo & osAccountInfo)473 ErrCode OsAccountProxy::QueryCurrentOsAccount(OsAccountInfo &osAccountInfo)
474 {
475 MessageParcel data;
476 MessageParcel reply;
477
478 if (!data.WriteInterfaceToken(GetDescriptor())) {
479 ACCOUNT_LOGE("failed to write descriptor!");
480 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
481 }
482
483 ErrCode result = SendRequest(IOsAccount::Message::QUERY_CURRENT_OS_ACCOUNT, data, reply);
484 if (result != ERR_OK) {
485 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
486 return result;
487 }
488 result = reply.ReadInt32();
489 if (result != ERR_OK) {
490 ACCOUNT_LOGE("failed to read reply for query current os account, result %{public}d.", result);
491 return result;
492 }
493 std::shared_ptr<OsAccountInfo> infoPtr(reply.ReadParcelable<OsAccountInfo>());
494 if (infoPtr == nullptr) {
495 ACCOUNT_LOGE("failed to read OsAccountInfo");
496 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
497 }
498 osAccountInfo = *infoPtr;
499 return ERR_OK;
500 }
501
QueryOsAccountById(const int id,OsAccountInfo & osAccountInfo)502 ErrCode OsAccountProxy::QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo)
503 {
504 MessageParcel data;
505 MessageParcel reply;
506
507 if (!data.WriteInterfaceToken(GetDescriptor())) {
508 ACCOUNT_LOGE("failed to write descriptor!");
509 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
510 }
511
512 if (!data.WriteInt32(id)) {
513 ACCOUNT_LOGE("failed to write int for id");
514 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
515 }
516 ErrCode result = SendRequest(IOsAccount::Message::QUERY_OS_ACCOUNT_BY_ID, data, reply);
517 if (result != ERR_OK) {
518 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
519 return result;
520 }
521 result = reply.ReadInt32();
522 if (result != ERR_OK) {
523 ACCOUNT_LOGE("failed to read reply for query os account by id, result %{public}d.", result);
524 return result;
525 }
526 std::shared_ptr<OsAccountInfo> infoPtr(reply.ReadParcelable<OsAccountInfo>());
527 if (infoPtr == nullptr) {
528 ACCOUNT_LOGE("failed to read OsAccountInfo");
529 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
530 }
531 osAccountInfo = *infoPtr;
532 return ERR_OK;
533 }
534
GetOsAccountTypeFromProcess(OsAccountType & type)535 ErrCode OsAccountProxy::GetOsAccountTypeFromProcess(OsAccountType &type)
536 {
537 MessageParcel data;
538 MessageParcel reply;
539
540 if (!data.WriteInterfaceToken(GetDescriptor())) {
541 ACCOUNT_LOGE("failed to write descriptor!");
542 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
543 }
544
545 ErrCode result = SendRequest(IOsAccount::Message::GET_OS_ACCOUNT_TYPE_FROM_PROCESS, data, reply);
546 if (result != ERR_OK) {
547 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
548 return result;
549 }
550 result = reply.ReadInt32();
551 if (result != ERR_OK) {
552 ACCOUNT_LOGE("failed to read reply for get os account type by process, result %{public}d.", result);
553 return result;
554 }
555 type = static_cast<OsAccountType>(reply.ReadInt32());
556
557 return ERR_OK;
558 }
559
GetOsAccountProfilePhoto(const int id,std::string & photo)560 ErrCode OsAccountProxy::GetOsAccountProfilePhoto(const int id, std::string &photo)
561 {
562 MessageParcel data;
563 MessageParcel reply;
564
565 if (!data.WriteInterfaceToken(GetDescriptor())) {
566 ACCOUNT_LOGE("failed to write descriptor!");
567 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
568 }
569
570 if (!data.WriteInt32(id)) {
571 ACCOUNT_LOGE("failed to write int for id");
572 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
573 }
574 ErrCode result = SendRequest(IOsAccount::Message::GET_OS_ACCOUNT_PROFILE_PHOTO, data, reply);
575 if (result != ERR_OK) {
576 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
577 return result;
578 }
579 result = reply.ReadInt32();
580 if (result != ERR_OK) {
581 ACCOUNT_LOGE("failed to read reply for get os account profile photo, result %{public}d.", result);
582 return result;
583 }
584 photo = reply.ReadString();
585
586 return ERR_OK;
587 }
588
IsMultiOsAccountEnable(bool & isMultiOsAccountEnable)589 ErrCode OsAccountProxy::IsMultiOsAccountEnable(bool &isMultiOsAccountEnable)
590 {
591 MessageParcel data;
592 MessageParcel reply;
593
594 if (!data.WriteInterfaceToken(GetDescriptor())) {
595 ACCOUNT_LOGE("failed to write descriptor!");
596 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
597 }
598
599 ErrCode result = SendRequest(IOsAccount::Message::IS_MULTI_OS_ACCOUNT_ENABLE, data, reply);
600 if (result != ERR_OK) {
601 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
602 return result;
603 }
604 result = reply.ReadInt32();
605 if (result != ERR_OK) {
606 ACCOUNT_LOGE("failed to read reply for is multi os account enable.");
607 return result;
608 }
609 isMultiOsAccountEnable = reply.ReadBool();
610
611 return ERR_OK;
612 }
613
SetOsAccountName(const int id,const std::string & name)614 ErrCode OsAccountProxy::SetOsAccountName(const int id, const std::string &name)
615 {
616 MessageParcel data;
617 MessageParcel reply;
618
619 if (!data.WriteInterfaceToken(GetDescriptor())) {
620 ACCOUNT_LOGE("failed to write descriptor!");
621 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
622 }
623
624 if (!data.WriteInt32(id)) {
625 ACCOUNT_LOGE("failed to write int for id %{public}d.", id);
626 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
627 }
628 if (!data.WriteString(name)) {
629 ACCOUNT_LOGE("failed to write string for name");
630 return ERR_OSACCOUNT_KIT_WRITE_STRING_LOCAL_NAME_ERROR;
631 }
632 ErrCode result = SendRequest(IOsAccount::Message::SET_OS_ACCOUNT_NAME, data, reply);
633 if (result != ERR_OK) {
634 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
635 return result;
636 }
637 result = reply.ReadInt32();
638 if (result != ERR_OK) {
639 ACCOUNT_LOGE("failed to read reply for set os account name, result %{public}d.", result);
640 return result;
641 }
642
643 return ERR_OK;
644 }
645
SetOsAccountConstraints(const int id,const std::vector<std::string> & constraints,const bool enable)646 ErrCode OsAccountProxy::SetOsAccountConstraints(
647 const int id, const std::vector<std::string> &constraints, const bool enable)
648 {
649 MessageParcel data;
650 MessageParcel reply;
651
652 if (!data.WriteInterfaceToken(GetDescriptor())) {
653 ACCOUNT_LOGE("failed to write descriptor!");
654 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
655 }
656
657 if (!data.WriteInt32(id)) {
658 ACCOUNT_LOGE("failed to write int for id");
659 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
660 }
661 if (!data.WriteStringVector(constraints)) {
662 ACCOUNT_LOGE("failed to write stringVector for constraints");
663 return ERR_OSACCOUNT_KIT_WRITE_STRING_VECTOR_CONSTRAINTS_ERROR;
664 }
665 if (!data.WriteBool(enable)) {
666 ACCOUNT_LOGE("failed to write bool for enable");
667 return ERR_OSACCOUNT_KIT_WRITE_BOOL_ENABLE_ERROR;
668 }
669 ErrCode result = SendRequest(IOsAccount::Message::SET_OS_ACCOUNT_CONSTRAINTS, data, reply);
670 if (result != ERR_OK) {
671 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
672 return result;
673 }
674 result = reply.ReadInt32();
675 if (result != ERR_OK) {
676 ACCOUNT_LOGE("failed to read reply for set os account constraints, result %{public}d.", result);
677 return result;
678 }
679
680 return ERR_OK;
681 }
682
SetOsAccountProfilePhoto(const int id,const std::string & photo)683 ErrCode OsAccountProxy::SetOsAccountProfilePhoto(const int id, const std::string &photo)
684 {
685 MessageParcel data;
686 MessageParcel reply;
687
688 if (!data.WriteInterfaceToken(GetDescriptor())) {
689 ACCOUNT_LOGE("failed to write descriptor!");
690 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
691 }
692
693 if (!data.WriteInt32(id)) {
694 ACCOUNT_LOGE("failed to write int for id");
695 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
696 }
697 if (!data.WriteString(photo)) {
698 ACCOUNT_LOGE("failed to write string for photo");
699 return ERR_OSACCOUNT_KIT_WRITE_STRING_PHOTO_ERROR;
700 }
701 ErrCode result = SendRequest(IOsAccount::Message::SET_OS_ACCOUNT_PROFILE_PHOTO, data, reply);
702 if (result != ERR_OK) {
703 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
704 return result;
705 }
706 result = reply.ReadInt32();
707 if (result != ERR_OK) {
708 ACCOUNT_LOGE("failed to read reply for set os account profile photo, result %{public}d.", result);
709 return result;
710 }
711
712 return ERR_OK;
713 }
714
ActivateOsAccount(const int id)715 ErrCode OsAccountProxy::ActivateOsAccount(const int id)
716 {
717 MessageParcel data;
718 MessageParcel reply;
719
720 if (!data.WriteInterfaceToken(GetDescriptor())) {
721 ACCOUNT_LOGE("failed to write descriptor!");
722 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
723 }
724
725 if (!data.WriteInt32(id)) {
726 ACCOUNT_LOGE("failed to write int for id");
727 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
728 }
729 ErrCode result = SendRequest(IOsAccount::Message::ACTIVATE_OS_ACCOUNT, data, reply);
730 if (result != ERR_OK) {
731 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
732 return result;
733 }
734 result = reply.ReadInt32();
735 if (result != ERR_OK) {
736 ACCOUNT_LOGE("failed to read reply for activate os account, result %{public}d.", result);
737 return result;
738 }
739
740 return ERR_OK;
741 }
742
StartOsAccount(const int id)743 ErrCode OsAccountProxy::StartOsAccount(const int id)
744 {
745 MessageParcel data;
746 MessageParcel reply;
747
748 if (!data.WriteInterfaceToken(GetDescriptor())) {
749 ACCOUNT_LOGE("failed to write descriptor!");
750 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
751 }
752
753 if (!data.WriteInt32(id)) {
754 ACCOUNT_LOGE("failed to write int for id");
755 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
756 }
757 ErrCode result = SendRequest(IOsAccount::Message::START_OS_ACCOUNT, 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 start os account, result %{public}d.", result);
765 return result;
766 }
767
768 return ERR_OK;
769 }
770
StopOsAccount(const int id)771 ErrCode OsAccountProxy::StopOsAccount(const int id)
772 {
773 MessageParcel data;
774 MessageParcel reply;
775
776 if (!data.WriteInterfaceToken(GetDescriptor())) {
777 ACCOUNT_LOGE("failed to write descriptor!");
778 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
779 }
780
781 if (!data.WriteInt32(id)) {
782 ACCOUNT_LOGE("failed to write int for id");
783 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
784 }
785 ErrCode result = SendRequest(IOsAccount::Message::STOP_OS_ACCOUNT, data, reply);
786 if (result != ERR_OK) {
787 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
788 return result;
789 }
790 result = reply.ReadInt32();
791 if (result != ERR_OK) {
792 ACCOUNT_LOGE("failed to read reply for stop os account, result %{public}d.", result);
793 return result;
794 }
795
796 return ERR_OK;
797 }
798
GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber,int & id)799 ErrCode OsAccountProxy::GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber, int &id)
800 {
801 MessageParcel data;
802 MessageParcel reply;
803
804 if (!data.WriteInterfaceToken(GetDescriptor())) {
805 ACCOUNT_LOGE("failed to write descriptor!");
806 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
807 }
808
809 if (!data.WriteInt64(serialNumber)) {
810 ACCOUNT_LOGE("failed to write int for serialNumber");
811 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
812 }
813 ErrCode result = SendRequest(IOsAccount::Message::GET_OS_ACCOUNT_LOCAL_ID_FOR_SERIAL_NUMBER, data, reply);
814 if (result != ERR_OK) {
815 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
816 return result;
817 }
818 result = reply.ReadInt32();
819 if (result != ERR_OK) {
820 ACCOUNT_LOGE("failed to read reply for get os account id by serial number, result %{public}d.", result);
821 return result;
822 }
823 id = reply.ReadInt32();
824
825 return ERR_OK;
826 }
827
GetSerialNumberByOsAccountLocalId(const int & id,int64_t & serialNumber)828 ErrCode OsAccountProxy::GetSerialNumberByOsAccountLocalId(const int &id, int64_t &serialNumber)
829 {
830 MessageParcel data;
831 MessageParcel reply;
832
833 if (!data.WriteInterfaceToken(GetDescriptor())) {
834 ACCOUNT_LOGE("failed to write descriptor!");
835 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
836 }
837
838 if (!data.WriteInt32(id)) {
839 ACCOUNT_LOGE("failed to write int for id");
840 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
841 }
842 ErrCode result = SendRequest(IOsAccount::Message::GET_SERIAL_NUMBER_FOR_OS_ACCOUNT, data, reply);
843 if (result != ERR_OK) {
844 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
845 return result;
846 }
847 result = reply.ReadInt32();
848 if (result != ERR_OK) {
849 ACCOUNT_LOGE("failed to read reply for get serial number by os account id, result %{public}d.", result);
850 return result;
851 }
852 serialNumber = reply.ReadInt64();
853
854 return ERR_OK;
855 }
856
SubscribeOsAccount(const OsAccountSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & eventListener)857 ErrCode OsAccountProxy::SubscribeOsAccount(
858 const OsAccountSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &eventListener)
859 {
860 MessageParcel data;
861 MessageParcel reply;
862
863 if (!data.WriteInterfaceToken(GetDescriptor())) {
864 ACCOUNT_LOGE("failed to write descriptor!");
865 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
866 }
867
868 if (!data.WriteParcelable(&subscribeInfo)) {
869 ACCOUNT_LOGE("failed to write parcelable for subscribeInfo");
870 return ERR_OSACCOUNT_KIT_WRITE_PARCELABLE_SUBSCRIBE_INFO_ERROR;
871 }
872
873 if (!data.WriteRemoteObject(eventListener)) {
874 ACCOUNT_LOGE("failed to write remote object for eventListener");
875 return ERR_OSACCOUNT_KIT_WRITE_PARCELABLE_EVENT_LISTENER_ERROR;
876 }
877
878 ErrCode result = SendRequest(IOsAccount::Message::SUBSCRIBE_ACCOUNT, data, reply);
879 if (result != ERR_OK) {
880 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
881 return result;
882 }
883
884 result = reply.ReadInt32();
885 if (result != ERR_OK) {
886 ACCOUNT_LOGE("failed to read reply for subscriber os account, result %{public}d.", result);
887 return result;
888 }
889
890 return ERR_OK;
891 }
892
UnsubscribeOsAccount(const sptr<IRemoteObject> & eventListener)893 ErrCode OsAccountProxy::UnsubscribeOsAccount(const sptr<IRemoteObject> &eventListener)
894 {
895 MessageParcel data;
896 MessageParcel reply;
897
898 if (!data.WriteInterfaceToken(GetDescriptor())) {
899 ACCOUNT_LOGE("failed to write descriptor!");
900 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
901 }
902
903 if (!data.WriteRemoteObject(eventListener)) {
904 ACCOUNT_LOGE("failed to write remote object for eventListener");
905 return ERR_OSACCOUNT_KIT_WRITE_PARCELABLE_EVENT_LISTENER_ERROR;
906 }
907
908 ErrCode result = SendRequest(IOsAccount::Message::UNSUBSCRIBE_ACCOUNT, data, reply);
909 if (result != ERR_OK) {
910 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
911 return result;
912 }
913
914 result = reply.ReadInt32();
915 if (result != ERR_OK) {
916 ACCOUNT_LOGE("failed to read reply for unsubscribe os account.");
917 }
918
919 return result;
920 }
GetOsAccountSwitchMod()921 OS_ACCOUNT_SWITCH_MOD OsAccountProxy::GetOsAccountSwitchMod()
922 {
923 MessageParcel data;
924 MessageParcel reply;
925
926 if (!data.WriteInterfaceToken(GetDescriptor())) {
927 ACCOUNT_LOGE("failed to write descriptor!");
928 return OS_ACCOUNT_SWITCH_MOD::ERROR_MOD;
929 }
930
931 ErrCode result = SendRequest(IOsAccount::Message::GET_OS_ACCOUNT_SWITCH_MOD, data, reply);
932 if (result != ERR_OK) {
933 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
934 return OS_ACCOUNT_SWITCH_MOD::ERROR_MOD;
935 }
936
937 OS_ACCOUNT_SWITCH_MOD osAccountSwitchMod = static_cast<OS_ACCOUNT_SWITCH_MOD>(reply.ReadInt32());
938
939 return osAccountSwitchMod;
940 }
941
SendRequest(IOsAccount::Message code,MessageParcel & data,MessageParcel & reply)942 ErrCode OsAccountProxy::SendRequest(IOsAccount::Message code, MessageParcel &data, MessageParcel &reply)
943 {
944 sptr<IRemoteObject> remote = Remote();
945 if (remote == nullptr) {
946 ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
947 return ERR_OSACCOUNT_KIT_REMOTE_IS_NULLPTR;
948 }
949
950 MessageOption option(MessageOption::TF_SYNC);
951 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
952 if (result != ERR_OK) {
953 ACCOUNT_LOGE("failed to SendRequest, code = %{public}d, result = %{public}d", code, result);
954 }
955
956 return result;
957 }
958
IsCurrentOsAccountVerified(bool & isVerified)959 ErrCode OsAccountProxy::IsCurrentOsAccountVerified(bool &isVerified)
960 {
961 MessageParcel data;
962 MessageParcel reply;
963
964 if (!data.WriteInterfaceToken(GetDescriptor())) {
965 ACCOUNT_LOGE("failed to write descriptor!");
966 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
967 }
968
969 ErrCode result = SendRequest(IOsAccount::Message::IS_CURRENT_OS_ACCOUNT_VERIFIED, data, reply);
970 if (result != ERR_OK) {
971 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
972 return result;
973 }
974
975 result = reply.ReadInt32();
976 if (result != ERR_OK) {
977 ACCOUNT_LOGE("failed to read reply for is current os account verified, result %{public}d.", result);
978 return result;
979 }
980 isVerified = reply.ReadBool();
981
982 return ERR_OK;
983 }
984
IsOsAccountCompleted(const int id,bool & isOsAccountCompleted)985 ErrCode OsAccountProxy::IsOsAccountCompleted(const int id, bool &isOsAccountCompleted)
986 {
987 MessageParcel data;
988 MessageParcel reply;
989
990 if (!data.WriteInterfaceToken(GetDescriptor())) {
991 ACCOUNT_LOGE("failed to write descriptor!");
992 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
993 }
994
995 if (!data.WriteInt32(id)) {
996 ACCOUNT_LOGE("failed to write int for id");
997 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
998 }
999 ErrCode result = SendRequest(IOsAccount::Message::IS_OS_ACCOUNT_COMPLETED, data, reply);
1000 if (result != ERR_OK) {
1001 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1002 return result;
1003 }
1004
1005 result = reply.ReadInt32();
1006 if (result != ERR_OK) {
1007 ACCOUNT_LOGE("failed to read reply for is os account completed, result %{public}d.", result);
1008 return result;
1009 }
1010 isOsAccountCompleted = reply.ReadBool();
1011 return ERR_OK;
1012 }
1013
SetCurrentOsAccountIsVerified(const bool isVerified)1014 ErrCode OsAccountProxy::SetCurrentOsAccountIsVerified(const bool isVerified)
1015 {
1016 MessageParcel data;
1017 MessageParcel reply;
1018
1019 if (!data.WriteInterfaceToken(GetDescriptor())) {
1020 ACCOUNT_LOGE("failed to write descriptor!");
1021 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1022 }
1023
1024 if (!data.WriteBool(isVerified)) {
1025 ACCOUNT_LOGE("failed to write bool for isVerified");
1026 return ERR_OSACCOUNT_KIT_WRITE_BOOL_IS_OSACCOUNT_VERIFIED_ERROR;
1027 }
1028 ErrCode result = SendRequest(IOsAccount::Message::SET_CURRENT_OS_ACCOUNT_IS_VERIFIED, data, reply);
1029 if (result != ERR_OK) {
1030 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1031 return result;
1032 }
1033
1034 result = reply.ReadInt32();
1035 if (result != ERR_OK) {
1036 ACCOUNT_LOGE("failed to read reply for set current os account verified, result %{public}d.", result);
1037 return result;
1038 }
1039 return ERR_OK;
1040 }
1041
SetOsAccountIsVerified(const int id,const bool isVerified)1042 ErrCode OsAccountProxy::SetOsAccountIsVerified(const int id, const bool isVerified)
1043 {
1044 MessageParcel data;
1045 MessageParcel reply;
1046
1047 if (!data.WriteInterfaceToken(GetDescriptor())) {
1048 ACCOUNT_LOGE("failed to write descriptor!");
1049 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1050 }
1051
1052 if (!data.WriteInt32(id)) {
1053 ACCOUNT_LOGE("failed to write int for id");
1054 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
1055 }
1056 if (!data.WriteBool(isVerified)) {
1057 ACCOUNT_LOGE("failed to write bool for isVerified");
1058 return ERR_OSACCOUNT_KIT_WRITE_BOOL_IS_OSACCOUNT_VERIFIED_ERROR;
1059 }
1060
1061 ErrCode result = SendRequest(IOsAccount::Message::SET_OS_ACCOUNT_IS_VERIFIED, data, reply);
1062 if (result != ERR_OK) {
1063 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1064 return result;
1065 }
1066
1067 result = reply.ReadInt32();
1068 if (result != ERR_OK) {
1069 ACCOUNT_LOGE("failed to read reply for set os account verified, result %{public}d.", result);
1070 return result;
1071 }
1072 return ERR_OK;
1073 }
1074
DumpState(const int & id,std::vector<std::string> & state)1075 ErrCode OsAccountProxy::DumpState(const int &id, std::vector<std::string> &state)
1076 {
1077 MessageParcel data;
1078 MessageParcel reply;
1079
1080 if (!data.WriteInterfaceToken(GetDescriptor())) {
1081 ACCOUNT_LOGE("failed to write descriptor!");
1082 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1083 }
1084
1085 if (!data.WriteInt32(id)) {
1086 ACCOUNT_LOGE("failed to write int for id");
1087 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
1088 }
1089
1090 ErrCode result = SendRequest(IOsAccount::Message::DUMP_STATE, data, reply);
1091 if (result != ERR_OK) {
1092 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1093 return result;
1094 }
1095
1096 result = reply.ReadInt32();
1097 if (result != ERR_OK) {
1098 ACCOUNT_LOGE("failed to read reply for dump state, result %{public}d.", result);
1099 return result;
1100 }
1101
1102 uint32_t size = reply.ReadUint32();
1103 for (uint32_t i = 0; i < size; i++) {
1104 std::string info = reply.ReadString();
1105 state.emplace_back(info);
1106 }
1107 return ERR_OK;
1108 }
1109
CreateBasicAccounts()1110 void OsAccountProxy::CreateBasicAccounts()
1111 {
1112 ACCOUNT_LOGI("Do nothing.");
1113 }
1114
GetCreatedOsAccountNumFromDatabase(const std::string & storeID,int & createdOsAccountNum)1115 ErrCode OsAccountProxy::GetCreatedOsAccountNumFromDatabase(const std::string& storeID,
1116 int &createdOsAccountNum)
1117 {
1118 MessageParcel data;
1119 MessageParcel reply;
1120
1121 if (!data.WriteInterfaceToken(GetDescriptor())) {
1122 ACCOUNT_LOGE("failed to write descriptor!");
1123 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1124 }
1125
1126 if (!data.WriteString(storeID)) {
1127 ACCOUNT_LOGE("failed to write string for storeID");
1128 return ERR_OSACCOUNT_KIT_WRITE_STRING_STOREID_ERROR;
1129 }
1130 ErrCode result = SendRequest(IOsAccount::Message::GET_CREATED_OS_ACCOUNT_NUM_FROM_DATABASE, data, reply);
1131 if (result != ERR_OK) {
1132 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1133 return result;
1134 }
1135
1136 result = reply.ReadInt32();
1137 if (result != ERR_OK) {
1138 ACCOUNT_LOGE("failed to read reply, result %{public}d.", result);
1139 return result;
1140 }
1141 createdOsAccountNum = reply.ReadInt32();
1142 return ERR_OK;
1143 }
1144
GetSerialNumberFromDatabase(const std::string & storeID,int64_t & serialNumber)1145 ErrCode OsAccountProxy::GetSerialNumberFromDatabase(const std::string& storeID, int64_t &serialNumber)
1146 {
1147 MessageParcel data;
1148 MessageParcel reply;
1149
1150 if (!data.WriteInterfaceToken(GetDescriptor())) {
1151 ACCOUNT_LOGE("failed to write descriptor!");
1152 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1153 }
1154
1155 if (!data.WriteString(storeID)) {
1156 ACCOUNT_LOGE("failed to write string for storeID");
1157 return ERR_OSACCOUNT_KIT_WRITE_STRING_STOREID_ERROR;
1158 }
1159 ErrCode result = SendRequest(IOsAccount::Message::GET_SERIAL_NUM_FROM_DATABASE, data, reply);
1160 if (result != ERR_OK) {
1161 ACCOUNT_LOGE("SendRequest failed, result %{public}d.", result);
1162 return result;
1163 }
1164
1165 result = reply.ReadInt32();
1166 if (result != ERR_OK) {
1167 ACCOUNT_LOGE("failed to read reply, result %{public}d.", result);
1168 return result;
1169 }
1170 serialNumber = reply.ReadInt64();
1171 return ERR_OK;
1172 }
1173
GetMaxAllowCreateIdFromDatabase(const std::string & storeID,int & id)1174 ErrCode OsAccountProxy::GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id)
1175 {
1176 MessageParcel data;
1177 MessageParcel reply;
1178
1179 if (!data.WriteInterfaceToken(GetDescriptor())) {
1180 ACCOUNT_LOGE("failed to write descriptor!");
1181 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1182 }
1183
1184 if (!data.WriteString(storeID)) {
1185 ACCOUNT_LOGE("failed to write string for isVerified");
1186 return ERR_OSACCOUNT_KIT_WRITE_STRING_STOREID_ERROR;
1187 }
1188 ErrCode result = SendRequest(IOsAccount::Message::GET_MAX_ALLOW_CREATE_ID_FROM_DATABASE, data, reply);
1189 if (result != ERR_OK) {
1190 ACCOUNT_LOGE("SendRequest failed, result %{public}d.", result);
1191 return result;
1192 }
1193
1194 result = reply.ReadInt32();
1195 if (result != ERR_OK) {
1196 ACCOUNT_LOGE("failed to read reply, result %{public}d.", result);
1197 return result;
1198 }
1199 id = reply.ReadInt32();
1200 return ERR_OK;
1201 }
1202
GetOsAccountFromDatabase(const std::string & storeID,const int id,OsAccountInfo & osAccountInfo)1203 ErrCode OsAccountProxy::GetOsAccountFromDatabase(const std::string& storeID,
1204 const int id, OsAccountInfo &osAccountInfo)
1205 {
1206 MessageParcel data;
1207 MessageParcel reply;
1208
1209 if (!data.WriteInterfaceToken(GetDescriptor())) {
1210 ACCOUNT_LOGE("failed to write descriptor!");
1211 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1212 }
1213
1214 if (!data.WriteString(storeID)) {
1215 ACCOUNT_LOGE("failed to write string for storeID");
1216 return ERR_OSACCOUNT_KIT_WRITE_STRING_STOREID_ERROR;
1217 }
1218 if (!data.WriteInt32(id)) {
1219 ACCOUNT_LOGE("failed to write int for id");
1220 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
1221 }
1222
1223 ErrCode result = SendRequest(IOsAccount::Message::GET_OS_ACCOUNT_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 std::shared_ptr<OsAccountInfo> infoPtr(reply.ReadParcelable<OsAccountInfo>());
1235 if (infoPtr == nullptr) {
1236 ACCOUNT_LOGE("failed to read OsAccountInfo");
1237 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
1238 }
1239 osAccountInfo = *infoPtr;
1240 return ERR_OK;
1241 }
1242
GetOsAccountListFromDatabase(const std::string & storeID,std::vector<OsAccountInfo> & osAccountList)1243 ErrCode OsAccountProxy::GetOsAccountListFromDatabase(const std::string& storeID,
1244 std::vector<OsAccountInfo> &osAccountList)
1245 {
1246 MessageParcel data;
1247 MessageParcel reply;
1248
1249 if (!data.WriteInterfaceToken(GetDescriptor())) {
1250 ACCOUNT_LOGE("failed to write descriptor!");
1251 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1252 }
1253
1254 if (!data.WriteString(storeID)) {
1255 ACCOUNT_LOGE("failed to write string for storeID");
1256 return ERR_OSACCOUNT_KIT_WRITE_STRING_STOREID_ERROR;
1257 }
1258 ErrCode result = SendRequest(IOsAccount::Message::GET_OS_ACCOUNT_LIST_FROM_DATABASE, data, reply);
1259 if (result != ERR_OK) {
1260 ACCOUNT_LOGE("SendRequest failed, result %{public}d.", result);
1261 return result;
1262 }
1263
1264 result = reply.ReadInt32();
1265 if (result != ERR_OK) {
1266 ACCOUNT_LOGE("failed to read reply, result %{public}d.", result);
1267 return result;
1268 }
1269 ReadParcelableVector(osAccountList, reply);
1270 return ERR_OK;
1271 }
1272
QueryActiveOsAccountIds(std::vector<int32_t> & ids)1273 ErrCode OsAccountProxy::QueryActiveOsAccountIds(std::vector<int32_t>& ids)
1274 {
1275 MessageParcel data;
1276 MessageParcel reply;
1277
1278 if (!data.WriteInterfaceToken(GetDescriptor())) {
1279 ACCOUNT_LOGE("failed to write descriptor!");
1280 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1281 }
1282
1283 ErrCode result = SendRequest(IOsAccount::Message::QUERY_ACTIVE_OS_ACCOUNT_IDS, data, reply);
1284 if (result != ERR_OK) {
1285 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1286 return result;
1287 }
1288
1289 result = reply.ReadInt32();
1290 if (result != ERR_OK) {
1291 ACCOUNT_LOGE("failed to read reply for query active os account ids, result %{public}d.", result);
1292 return result;
1293 }
1294
1295 bool readFlag = reply.ReadInt32Vector(&ids);
1296 if (!readFlag) {
1297 ACCOUNT_LOGE("failed to read vector for active ids.");
1298 return ERR_OSACCOUNT_KIT_QUERY_ACTIVE_OS_ACCOUNT_IDS_ERROR;
1299 }
1300 return ERR_OK;
1301 }
1302
QueryOsAccountConstraintSourceTypes(const int32_t id,const std::string & constraint,std::vector<ConstraintSourceTypeInfo> & constraintSourceTypeInfos)1303 ErrCode OsAccountProxy::QueryOsAccountConstraintSourceTypes(const int32_t id,
1304 const std::string &constraint, std::vector<ConstraintSourceTypeInfo> &constraintSourceTypeInfos)
1305 {
1306 constraintSourceTypeInfos.clear();
1307 MessageParcel data;
1308 MessageParcel reply;
1309
1310 if (!data.WriteInterfaceToken(GetDescriptor())) {
1311 ACCOUNT_LOGE("failed to write descriptor!");
1312 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1313 }
1314
1315 if (!data.WriteInt32(id)) {
1316 ACCOUNT_LOGE("failed to write int for id");
1317 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
1318 }
1319
1320 if (!data.WriteString(constraint)) {
1321 ACCOUNT_LOGE("failed to write string for constraint");
1322 return ERR_OSACCOUNT_KIT_WRITE_STRING_STOREID_ERROR;
1323 }
1324
1325 ErrCode result = SendRequest(IOsAccount::Message::QUERY_OS_ACCOUNT_CONSTRAINT_SOURCE_TYPES, data, reply);
1326 if (result != ERR_OK) {
1327 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1328 return result;
1329 }
1330 result = reply.ReadInt32();
1331 if (result != ERR_OK) {
1332 return result;
1333 }
1334 uint32_t size = reply.ReadUint32();
1335 for (uint32_t i = 0; i < size; ++i) {
1336 ConstraintSourceTypeInfo constraintSrcInfo;
1337 constraintSrcInfo.localId = reply.ReadInt32();
1338 constraintSrcInfo.typeInfo = static_cast<ConstraintSourceType>(reply.ReadInt32());
1339 constraintSourceTypeInfos.push_back(constraintSrcInfo);
1340 }
1341 return ERR_OK;
1342 }
1343
SetGlobalOsAccountConstraints(const std::vector<std::string> & constraints,const bool enable,const int32_t enforcerId,const bool isDeviceOwner)1344 ErrCode OsAccountProxy::SetGlobalOsAccountConstraints(const std::vector<std::string> &constraints,
1345 const bool enable, const int32_t enforcerId, const bool isDeviceOwner)
1346 {
1347 MessageParcel data;
1348 MessageParcel reply;
1349
1350 if (!data.WriteInterfaceToken(GetDescriptor())) {
1351 ACCOUNT_LOGE("failed to write descriptor!");
1352 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1353 }
1354 if (!data.WriteStringVector(constraints)) {
1355 ACCOUNT_LOGE("failed to write stringVector for constraints");
1356 return ERR_OSACCOUNT_KIT_WRITE_STRING_VECTOR_CONSTRAINTS_ERROR;
1357 }
1358
1359 if (!data.WriteBool(enable)) {
1360 ACCOUNT_LOGE("failed to write bool for enable");
1361 return ERR_OSACCOUNT_KIT_WRITE_BOOL_ENABLE_ERROR;
1362 }
1363
1364 if (!data.WriteInt32(enforcerId)) {
1365 ACCOUNT_LOGE("failed to write int for enforcerId");
1366 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
1367 }
1368
1369 if (!data.WriteBool(isDeviceOwner)) {
1370 ACCOUNT_LOGE("failed to write bool for isDeviceOwner");
1371 return ERR_OSACCOUNT_KIT_WRITE_BOOL_ENABLE_ERROR;
1372 }
1373 ErrCode result = SendRequest(IOsAccount::Message::SET_GLOBAL_OS_ACCOUNT_CONSTRAINTS, data, reply);
1374 if (result != ERR_OK) {
1375 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1376 return result;
1377 }
1378 result = reply.ReadInt32();
1379 if (result != ERR_OK) {
1380 ACCOUNT_LOGE("failed to read reply for set global os account constraints.");
1381 return result;
1382 }
1383 return ERR_OK;
1384 }
1385
SetSpecificOsAccountConstraints(const std::vector<std::string> & constraints,const bool enable,const int32_t targetId,const int32_t enforcerId,const bool isDeviceOwner)1386 ErrCode OsAccountProxy::SetSpecificOsAccountConstraints(const std::vector<std::string> &constraints,
1387 const bool enable, const int32_t targetId, const int32_t enforcerId, const bool isDeviceOwner)
1388 {
1389 MessageParcel data;
1390 MessageParcel reply;
1391
1392 if (!data.WriteInterfaceToken(GetDescriptor())) {
1393 ACCOUNT_LOGE("failed to write descriptor!");
1394 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
1395 }
1396 if (!data.WriteStringVector(constraints)) {
1397 ACCOUNT_LOGE("failed to write stringVector for constraints");
1398 return ERR_OSACCOUNT_KIT_WRITE_STRING_VECTOR_CONSTRAINTS_ERROR;
1399 }
1400
1401 if (!data.WriteBool(enable)) {
1402 ACCOUNT_LOGE("failed to write bool for enable");
1403 return ERR_OSACCOUNT_KIT_WRITE_BOOL_ENABLE_ERROR;
1404 }
1405
1406 if (!data.WriteInt32(targetId)) {
1407 ACCOUNT_LOGE("failed to write int for targetId");
1408 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
1409 }
1410
1411 if (!data.WriteInt32(enforcerId)) {
1412 ACCOUNT_LOGE("failed to write int for enforcerId");
1413 return ERR_OSACCOUNT_KIT_WRITE_INT_LOCAL_ID_ERROR;
1414 }
1415
1416 if (!data.WriteBool(isDeviceOwner)) {
1417 ACCOUNT_LOGE("failed to write bool for isDeviceOwner");
1418 return ERR_OSACCOUNT_KIT_WRITE_BOOL_ENABLE_ERROR;
1419 }
1420 ErrCode result = SendRequest(IOsAccount::Message::SET_SPECIFIC_OS_ACCOUNT_CONSTRAINTS, data, reply);
1421 if (result != ERR_OK) {
1422 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
1423 return result;
1424 }
1425 result = reply.ReadInt32();
1426 if (result != ERR_OK) {
1427 ACCOUNT_LOGE("failed to read reply for set specific os account constraints.");
1428 return result;
1429 }
1430 return ERR_OK;
1431 }
1432
1433 template<typename T>
WriteParcelableVector(const std::vector<T> & parcelableVector,MessageParcel & data)1434 bool OsAccountProxy::WriteParcelableVector(const std::vector<T> &parcelableVector, MessageParcel &data)
1435 {
1436 if (!data.WriteUint32(parcelableVector.size())) {
1437 ACCOUNT_LOGE("Account write ParcelableVector failed");
1438 return false;
1439 }
1440
1441 for (auto &parcelable : parcelableVector) {
1442 if (!data.WriteParcelable(&parcelable)) {
1443 ACCOUNT_LOGE("Account write ParcelableVector failed");
1444 return false;
1445 }
1446 }
1447 return true;
1448 }
1449 template<typename T>
ReadParcelableVector(std::vector<T> & parcelableInfos,MessageParcel & data)1450 bool OsAccountProxy::ReadParcelableVector(std::vector<T> &parcelableInfos, MessageParcel &data)
1451 {
1452 uint32_t infoSize = 0;
1453 if (!data.ReadUint32(infoSize)) {
1454 ACCOUNT_LOGE("Account read Parcelable size failed.");
1455 return false;
1456 }
1457
1458 parcelableInfos.clear();
1459 for (uint32_t index = 0; index < infoSize; index++) {
1460 std::shared_ptr<T> info(data.ReadParcelable<T>());
1461 if (info == nullptr) {
1462 ACCOUNT_LOGE("Account read Parcelable infos failed.");
1463 return false;
1464 }
1465 parcelableInfos.emplace_back(*info);
1466 }
1467
1468 return true;
1469 }
1470 } // namespace AccountSA
1471 } // namespace OHOS
1472