• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_stub.h"
16 #include "account_log_wrapper.h"
17 
18 namespace OHOS {
19 namespace AccountSA {
20 const std::map<uint32_t, OsAccountStub::MessageProcFunction> OsAccountStub::messageProcMap_ = {
21     {
22         static_cast<uint32_t>(IOsAccount::Message::CREATE_OS_ACCOUNT),
23         &OsAccountStub::ProcCreateOsAccount,
24     },
25     {
26         static_cast<uint32_t>(IOsAccount::Message::CREATE_OS_ACCOUNT_FOR_DOMAIN),
27         &OsAccountStub::ProcCreateOsAccountForDomain,
28     },
29     {
30         static_cast<uint32_t>(IOsAccount::Message::REMOVE_OS_ACCOUNT),
31         &OsAccountStub::ProcRemoveOsAccount,
32     },
33     {
34         static_cast<uint32_t>(IOsAccount::Message::IS_OS_ACCOUNT_EXISTS),
35         &OsAccountStub::ProcIsOsAccountExists,
36     },
37     {
38         static_cast<uint32_t>(IOsAccount::Message::IS_OS_ACCOUNT_ACTIVED),
39         &OsAccountStub::ProcIsOsAccountActived,
40     },
41     {
42         static_cast<uint32_t>(IOsAccount::Message::IS_OS_ACCOUNT_CONSTRAINT_ENABLE),
43         &OsAccountStub::ProcIsOsAccountConstraintEnable,
44     },
45     {
46         static_cast<uint32_t>(IOsAccount::Message::IS_OS_ACCOUNT_VERIFIED),
47         &OsAccountStub::ProcIsOsAccountVerified,
48     },
49     {
50         static_cast<uint32_t>(IOsAccount::Message::GET_CREATED_OS_ACCOUNT_COUNT),
51         &OsAccountStub::ProcGetCreatedOsAccountsCount,
52     },
53     {
54         static_cast<uint32_t>(IOsAccount::Message::GET_OS_ACCOUNT_LOCAL_ID_FROM_PROCESS),
55         &OsAccountStub::ProcGetOsAccountLocalIdFromProcess,
56     },
57     {
58         static_cast<uint32_t>(IOsAccount::Message::GET_OS_ACCOUNT_LOCAL_ID_FROM_DOMAIN),
59         &OsAccountStub::ProcGetOsAccountLocalIdFromDomain,
60     },
61     {
62         static_cast<uint32_t>(IOsAccount::Message::QUERY_MAX_OS_ACCOUNT_NUMBER),
63         &OsAccountStub::ProcQueryMaxOsAccountNumber,
64     },
65     {
66         static_cast<uint32_t>(IOsAccount::Message::GET_OS_ACCOUNT_ALL_CONSTRAINTS),
67         &OsAccountStub::ProcGetOsAccountAllConstraints,
68     },
69     {
70         static_cast<uint32_t>(IOsAccount::Message::QUERY_ALL_CREATED_OS_ACCOUNTS),
71         &OsAccountStub::ProcQueryAllCreatedOsAccounts,
72     },
73     {
74         static_cast<uint32_t>(IOsAccount::Message::QUERY_CURRENT_OS_ACCOUNT),
75         &OsAccountStub::ProcQueryCurrentOsAccount,
76     },
77     {
78         static_cast<uint32_t>(IOsAccount::Message::QUERY_OS_ACCOUNT_BY_ID),
79         &OsAccountStub::ProcQueryOsAccountById,
80     },
81     {
82         static_cast<uint32_t>(IOsAccount::Message::GET_OS_ACCOUNT_TYPE_FROM_PROCESS),
83         &OsAccountStub::ProcGetOsAccountTypeFromProcess,
84     },
85     {
86         static_cast<uint32_t>(IOsAccount::Message::GET_OS_ACCOUNT_PROFILE_PHOTO),
87         &OsAccountStub::ProcGetOsAccountProfilePhoto,
88     },
89     {
90         static_cast<uint32_t>(IOsAccount::Message::IS_MULTI_OS_ACCOUNT_ENABLE),
91         &OsAccountStub::ProcIsMultiOsAccountEnable,
92     },
93     {
94         static_cast<uint32_t>(IOsAccount::Message::SET_OS_ACCOUNT_NAME),
95         &OsAccountStub::ProcSetOsAccountName,
96     },
97     {
98         static_cast<uint32_t>(IOsAccount::Message::SET_OS_ACCOUNT_CONSTRAINTS),
99         &OsAccountStub::ProcSetOsAccountConstraints,
100     },
101     {
102         static_cast<uint32_t>(IOsAccount::Message::SET_OS_ACCOUNT_PROFILE_PHOTO),
103         &OsAccountStub::ProcSetOsAccountProfilePhoto,
104     },
105     {
106         static_cast<uint32_t>(IOsAccount::Message::ACTIVATE_OS_ACCOUNT),
107         &OsAccountStub::ProcActivateOsAccount,
108     },
109     {
110         static_cast<uint32_t>(IOsAccount::Message::START_OS_ACCOUNT),
111         &OsAccountStub::ProcStartOsAccount,
112     },
113     {
114         static_cast<uint32_t>(IOsAccount::Message::STOP_OS_ACCOUNT),
115         &OsAccountStub::ProcStopOsAccount,
116     },
117     {
118         static_cast<uint32_t>(IOsAccount::Message::SUBSCRIBE_ACCOUNT),
119         &OsAccountStub::ProcSubscribeOsAccount,
120     },
121     {
122         static_cast<uint32_t>(IOsAccount::Message::UNSUBSCRIBE_ACCOUNT),
123         &OsAccountStub::ProcUnsubscribeOsAccount,
124     },
125     {
126         static_cast<uint32_t>(IOsAccount::Message::GET_OS_ACCOUNT_LOCAL_ID_FOR_SERIAL_NUMBER),
127         &OsAccountStub::ProcGetOsAccountLocalIdBySerialNumber,
128     },
129     {
130         static_cast<uint32_t>(IOsAccount::Message::GET_SERIAL_NUMBER_FOR_OS_ACCOUNT),
131         &OsAccountStub::ProcGetSerialNumberByOsAccountLocalId,
132     },
133     {
134         static_cast<uint32_t>(IOsAccount::Message::GET_OS_ACCOUNT_SWITCH_MOD),
135         &OsAccountStub::ProcGetOsAccountSwitchMod,
136     },
137     {
138         static_cast<uint32_t>(IOsAccount::Message::IS_CURRENT_OS_ACCOUNT_VERIFIED),
139         &OsAccountStub::ProcIsCurrentOsAccountVerified,
140     },
141     {
142         static_cast<uint32_t>(IOsAccount::Message::IS_OS_ACCOUNT_COMPLETED),
143         &OsAccountStub::ProcIsOsAccountCompleted,
144     },
145     {
146         static_cast<uint32_t>(IOsAccount::Message::SET_CURRENT_OS_ACCOUNT_IS_VERIFIED),
147         &OsAccountStub::ProcSetCurrentOsAccountIsVerified,
148     },
149     {
150         static_cast<uint32_t>(IOsAccount::Message::SET_OS_ACCOUNT_IS_VERIFIED),
151         &OsAccountStub::ProcSetOsAccountIsVerified,
152     },
153     {
154         static_cast<uint32_t>(IOsAccount::Message::DUMP_STATE),
155         &OsAccountStub::ProcDumpState,
156     },
157     {
158         static_cast<uint32_t>(IOsAccount::Message::GET_CREATED_OS_ACCOUNT_NUM_FROM_DATABASE),
159         &OsAccountStub::ProcGetCreatedOsAccountNumFromDatabase,
160     },
161     {
162         static_cast<uint32_t>(IOsAccount::Message::GET_SERIAL_NUM_FROM_DATABASE),
163         &OsAccountStub::ProcGetSerialNumberFromDatabase,
164     },
165     {
166         static_cast<uint32_t>(IOsAccount::Message::GET_MAX_ALLOW_CREATE_ID_FROM_DATABASE),
167         &OsAccountStub::ProcGetMaxAllowCreateIdFromDatabase,
168     },
169     {
170         static_cast<uint32_t>(IOsAccount::Message::GET_OS_ACCOUNT_FROM_DATABASE),
171         &OsAccountStub::ProcGetOsAccountFromDatabase,
172     },
173     {
174         static_cast<uint32_t>(IOsAccount::Message::GET_OS_ACCOUNT_LIST_FROM_DATABASE),
175         &OsAccountStub::ProcGetOsAccountListFromDatabase,
176     },
177     {
178         static_cast<uint32_t>(IOsAccount::Message::QUERY_ACTIVE_OS_ACCOUNT_IDS),
179         &OsAccountStub::ProcQueryActiveOsAccountIds,
180     },
181 };
182 
OsAccountStub()183 OsAccountStub::OsAccountStub()
184 {}
185 
~OsAccountStub()186 OsAccountStub::~OsAccountStub()
187 {}
188 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)189 int OsAccountStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
190 {
191     if (data.ReadInterfaceToken() != GetDescriptor()) {
192         ACCOUNT_LOGE("check descriptor failed! code %{public}u.", code);
193         return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
194     }
195 
196     auto messageProc = messageProcMap_.find(code);
197     if (messageProc != messageProcMap_.end()) {
198         auto messageProcFunction = messageProc->second;
199         if (messageProcFunction != nullptr) {
200             return (this->*messageProcFunction)(data, reply);
201         }
202     }
203     ACCOUNT_LOGI("end, code = %{public}u, flags = %{public}u", code, option.GetFlags());
204     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
205 }
206 
207 template<typename T>
WriteParcelableVector(const std::vector<T> & parcelableVector,MessageParcel & data)208 bool OsAccountStub::WriteParcelableVector(const std::vector<T> &parcelableVector, MessageParcel &data)
209 {
210     if (!data.WriteUint32(parcelableVector.size())) {
211         ACCOUNT_LOGE("Account write ParcelableVector size failed");
212         return false;
213     }
214 
215     for (auto parcelable : parcelableVector) {
216         ACCOUNT_LOGI("Account write ParcelableVector insert");
217         if (!data.WriteParcelable(&parcelable)) {
218             ACCOUNT_LOGE("Account write ParcelableVector Parcelable failed");
219             return false;
220         }
221     }
222     return true;
223 }
224 
225 template<typename T>
ReadParcelableVector(std::vector<T> & parcelableInfos,MessageParcel & data)226 bool OsAccountStub::ReadParcelableVector(std::vector<T> &parcelableInfos, MessageParcel &data)
227 {
228     uint32_t infoSize = 0;
229     if (!data.ReadUint32(infoSize)) {
230         ACCOUNT_LOGE("read Parcelable size failed.");
231         return false;
232     }
233 
234     parcelableInfos.clear();
235     for (uint32_t index = 0; index < infoSize; index++) {
236         std::shared_ptr<T> info(data.ReadParcelable<T>());
237         if (info == nullptr) {
238             ACCOUNT_LOGE("read Parcelable infos failed.");
239             return false;
240         }
241         parcelableInfos.emplace_back(*info);
242     }
243 
244     return true;
245 }
246 
ProcCreateOsAccount(MessageParcel & data,MessageParcel & reply)247 ErrCode OsAccountStub::ProcCreateOsAccount(MessageParcel &data, MessageParcel &reply)
248 {
249     std::string name = data.ReadString();
250     if (name.size() == 0) {
251         ACCOUNT_LOGE("failed to read string for name");
252         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_LOCALNAME_ERROR);
253         return ERR_NONE;
254     }
255     OsAccountType type = static_cast<OsAccountType>(data.ReadInt32());
256     OsAccountInfo osAccountInfo;
257     ErrCode result = CreateOsAccount(name, type, osAccountInfo);
258     if (!reply.WriteInt32(result)) {
259         ACCOUNT_LOGE("failed to write reply");
260         return IPC_STUB_WRITE_PARCEL_ERR;
261     }
262     if (!reply.WriteParcelable(&osAccountInfo)) {
263         ACCOUNT_LOGE("failed to write reply");
264         return IPC_STUB_WRITE_PARCEL_ERR;
265     }
266     return ERR_NONE;
267 }
268 
ProcCreateOsAccountForDomain(MessageParcel & data,MessageParcel & reply)269 ErrCode OsAccountStub::ProcCreateOsAccountForDomain(MessageParcel &data, MessageParcel &reply)
270 {
271     OsAccountType type = static_cast<OsAccountType>(data.ReadInt32());
272     std::string domain = data.ReadString();
273     std::string domainAccountName = data.ReadString();
274 
275     if (domain.empty() || domain.size() > Constants::DOMAIN_NAME_MAX_SIZE) {
276         ACCOUNT_LOGE("read invalid domain length %{public}zu.", domain.size());
277         reply.WriteInt32(ERR_OSACCOUNT_KIT_DOMAIN_NAME_LENGTH_INVALID_ERROR);
278         return ERR_NONE;
279     }
280 
281     if (domainAccountName.empty() || domainAccountName.size() > Constants::DOMAIN_ACCOUNT_NAME_MAX_SIZE) {
282         ACCOUNT_LOGE("read invalid domain account name length %{public}zu.", domainAccountName.size());
283         reply.WriteInt32(ERR_OSACCOUNT_KIT_DOMAIN_ACCOUNT_NAME_LENGTH_INVALID_ERROR);
284         return ERR_NONE;
285     }
286 
287     OsAccountInfo osAccountInfo;
288     DomainAccountInfo domainInfo(domain, domainAccountName);
289     ErrCode result = CreateOsAccountForDomain(type, domainInfo, osAccountInfo);
290     if (!reply.WriteInt32(result)) {
291         ACCOUNT_LOGE("failed to write reply");
292         return IPC_STUB_WRITE_PARCEL_ERR;
293     }
294     if (!reply.WriteParcelable(&osAccountInfo)) {
295         ACCOUNT_LOGE("failed to write reply");
296         return IPC_STUB_WRITE_PARCEL_ERR;
297     }
298     return ERR_NONE;
299 }
300 
ProcRemoveOsAccount(MessageParcel & data,MessageParcel & reply)301 ErrCode OsAccountStub::ProcRemoveOsAccount(MessageParcel &data, MessageParcel &reply)
302 {
303     int localId = data.ReadInt32();
304     if (localId == -1) {
305         ACCOUNT_LOGE("failed to read int for osAccountType");
306         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
307         return ERR_NONE;
308     }
309 
310     ErrCode result = RemoveOsAccount(localId);
311     if (!reply.WriteInt32(result)) {
312         ACCOUNT_LOGE("failed to write reply");
313         return IPC_STUB_WRITE_PARCEL_ERR;
314     }
315     return ERR_NONE;
316 }
317 
ProcSetOsAccountName(MessageParcel & data,MessageParcel & reply)318 ErrCode OsAccountStub::ProcSetOsAccountName(MessageParcel &data, MessageParcel &reply)
319 {
320     int localId = data.ReadInt32();
321     if (localId == -1) {
322         ACCOUNT_LOGE("failed to read int for localId");
323         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
324         return ERR_NONE;
325     }
326     std::string localName = data.ReadString();
327     ErrCode result = SetOsAccountName(localId, localName);
328     if (!reply.WriteInt32(result)) {
329         ACCOUNT_LOGE("failed to write reply");
330         return IPC_STUB_WRITE_PARCEL_ERR;
331     }
332     return ERR_NONE;
333 }
334 
ProcSetOsAccountConstraints(MessageParcel & data,MessageParcel & reply)335 ErrCode OsAccountStub::ProcSetOsAccountConstraints(MessageParcel &data, MessageParcel &reply)
336 {
337     int localId = data.ReadInt32();
338     if (localId == -1) {
339         ACCOUNT_LOGE("failed to read int for localId");
340         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
341         return ERR_NONE;
342     }
343     std::vector<std::string> constraints;
344     bool stringVectorReadSucess = data.ReadStringVector(&constraints);
345     if (!stringVectorReadSucess) {
346         ACCOUNT_LOGE("failed to read StringVector for constraints");
347         return ERR_OSACCOUNT_KIT_READ_STRINGVECTOR_CONSTRAINTS_ERROR;
348     }
349     bool enable = data.ReadBool();
350     ErrCode result = SetOsAccountConstraints(localId, constraints, enable);
351     if (!reply.WriteInt32(result)) {
352         ACCOUNT_LOGE("failed to write reply");
353         return IPC_STUB_WRITE_PARCEL_ERR;
354     }
355     return ERR_NONE;
356 }
357 
ProcSetOsAccountProfilePhoto(MessageParcel & data,MessageParcel & reply)358 ErrCode OsAccountStub::ProcSetOsAccountProfilePhoto(MessageParcel &data, MessageParcel &reply)
359 {
360     int localId = data.ReadInt32();
361     if (localId == -1) {
362         ACCOUNT_LOGE("failed to read int for localId");
363         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
364         return ERR_NONE;
365     }
366     std::string photo = data.ReadString();
367     ErrCode result = SetOsAccountProfilePhoto(localId, photo);
368     if (!reply.WriteInt32(result)) {
369         ACCOUNT_LOGE("failed to write reply");
370         return IPC_STUB_WRITE_PARCEL_ERR;
371     }
372     return ERR_NONE;
373 }
374 
ProcQueryOsAccountById(MessageParcel & data,MessageParcel & reply)375 ErrCode OsAccountStub::ProcQueryOsAccountById(MessageParcel &data, MessageParcel &reply)
376 {
377     int localId = data.ReadInt32();
378     if (localId == -1) {
379         ACCOUNT_LOGE("failed to read int for localId");
380         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
381         return ERR_NONE;
382     }
383     OsAccountInfo osAccountInfo = OsAccountInfo();
384     ErrCode result = QueryOsAccountById(localId, osAccountInfo);
385     if (!reply.WriteInt32(result)) {
386         ACCOUNT_LOGE("failed to write reply");
387         return IPC_STUB_WRITE_PARCEL_ERR;
388     }
389     if (!reply.WriteParcelable(&osAccountInfo)) {
390         ACCOUNT_LOGE("failed to write reply");
391         return IPC_STUB_WRITE_PARCEL_ERR;
392     }
393     return ERR_NONE;
394 }
395 
ProcQueryCurrentOsAccount(MessageParcel & data,MessageParcel & reply)396 ErrCode OsAccountStub::ProcQueryCurrentOsAccount(MessageParcel &data, MessageParcel &reply)
397 {
398     OsAccountInfo osAccountInfo = OsAccountInfo();
399     ErrCode result = QueryCurrentOsAccount(osAccountInfo);
400     if (!reply.WriteInt32(result)) {
401         ACCOUNT_LOGE("failed to write reply");
402         return IPC_STUB_WRITE_PARCEL_ERR;
403     }
404     if (!reply.WriteParcelable(&osAccountInfo)) {
405         ACCOUNT_LOGE("failed to write reply");
406         return IPC_STUB_WRITE_PARCEL_ERR;
407     }
408     return ERR_NONE;
409 }
410 
ProcQueryAllCreatedOsAccounts(MessageParcel & data,MessageParcel & reply)411 ErrCode OsAccountStub::ProcQueryAllCreatedOsAccounts(MessageParcel &data, MessageParcel &reply)
412 {
413     std::vector<OsAccountInfo> osAccountInfos;
414     osAccountInfos.clear();
415     ErrCode result = QueryAllCreatedOsAccounts(osAccountInfos);
416     if (!reply.WriteInt32(result)) {
417         ACCOUNT_LOGE("failed to write reply");
418         return IPC_STUB_WRITE_PARCEL_ERR;
419     }
420     if (!WriteParcelableVector(osAccountInfos, reply)) {
421         ACCOUNT_LOGE("failed to write reply");
422         return IPC_STUB_WRITE_PARCEL_ERR;
423     }
424     return ERR_NONE;
425 }
426 
ProcQueryMaxOsAccountNumber(MessageParcel & data,MessageParcel & reply)427 ErrCode OsAccountStub::ProcQueryMaxOsAccountNumber(MessageParcel &data, MessageParcel &reply)
428 {
429     int maxOsAccountNumber = 0;
430     ErrCode result = QueryMaxOsAccountNumber(maxOsAccountNumber);
431     if (!reply.WriteInt32(result)) {
432         ACCOUNT_LOGE("failed to write reply");
433         return IPC_STUB_WRITE_PARCEL_ERR;
434     }
435     if (!reply.WriteInt32(maxOsAccountNumber)) {
436         ACCOUNT_LOGE("failed to write reply");
437         return IPC_STUB_WRITE_PARCEL_ERR;
438     }
439     return ERR_NONE;
440 }
441 
ProcGetCreatedOsAccountsCount(MessageParcel & data,MessageParcel & reply)442 ErrCode OsAccountStub::ProcGetCreatedOsAccountsCount(MessageParcel &data, MessageParcel &reply)
443 {
444     unsigned int osAccountsCount = 0;
445     ErrCode result = GetCreatedOsAccountsCount(osAccountsCount);
446     if (!reply.WriteInt32(result)) {
447         ACCOUNT_LOGE("failed to write reply");
448         return IPC_STUB_WRITE_PARCEL_ERR;
449     }
450     if (!reply.WriteUint32(osAccountsCount)) {
451         ACCOUNT_LOGE("failed to write reply");
452         return IPC_STUB_WRITE_PARCEL_ERR;
453     }
454     return ERR_NONE;
455 }
456 
ProcGetOsAccountAllConstraints(MessageParcel & data,MessageParcel & reply)457 ErrCode OsAccountStub::ProcGetOsAccountAllConstraints(MessageParcel &data, MessageParcel &reply)
458 {
459     int localId = data.ReadInt32();
460     if (localId == -1) {
461         ACCOUNT_LOGE("failed to read int for localId");
462         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
463         return ERR_NONE;
464     }
465     std::vector<std::string> constraints;
466     ErrCode result = GetOsAccountAllConstraints(localId, constraints);
467     if (!reply.WriteInt32(result)) {
468         ACCOUNT_LOGE("failed to write reply");
469         return IPC_STUB_WRITE_PARCEL_ERR;
470     }
471     if (!reply.WriteStringVector(constraints)) {
472         ACCOUNT_LOGE("failed to write reply");
473         return IPC_STUB_WRITE_PARCEL_ERR;
474     }
475     return ERR_NONE;
476 }
477 
ProcGetOsAccountLocalIdFromProcess(MessageParcel & data,MessageParcel & reply)478 ErrCode OsAccountStub::ProcGetOsAccountLocalIdFromProcess(MessageParcel &data, MessageParcel &reply)
479 {
480     int localId = -1;
481     ErrCode result = GetOsAccountLocalIdFromProcess(localId);
482     if (!reply.WriteInt32(result)) {
483         ACCOUNT_LOGE("failed to write reply");
484         return IPC_STUB_WRITE_PARCEL_ERR;
485     }
486     if (!reply.WriteInt32(localId)) {
487         ACCOUNT_LOGE("failed to write reply");
488         return IPC_STUB_WRITE_PARCEL_ERR;
489     }
490     return ERR_NONE;
491 }
492 
ProcGetOsAccountProfilePhoto(MessageParcel & data,MessageParcel & reply)493 ErrCode OsAccountStub::ProcGetOsAccountProfilePhoto(MessageParcel &data, MessageParcel &reply)
494 {
495     int localId = data.ReadInt32();
496     if (localId == -1) {
497         ACCOUNT_LOGE("failed to read int for localId");
498         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
499         return ERR_NONE;
500     }
501     std::string photo;
502     ErrCode result = GetOsAccountProfilePhoto(localId, photo);
503     if (!reply.WriteInt32(result)) {
504         ACCOUNT_LOGE("failed to write reply");
505         return IPC_STUB_WRITE_PARCEL_ERR;
506     }
507     if (!reply.WriteString(photo)) {
508         ACCOUNT_LOGE("failed to write reply");
509         return IPC_STUB_WRITE_PARCEL_ERR;
510     }
511     return ERR_NONE;
512 }
513 
ProcGetOsAccountLocalIdFromDomain(MessageParcel & data,MessageParcel & reply)514 ErrCode OsAccountStub::ProcGetOsAccountLocalIdFromDomain(MessageParcel &data, MessageParcel &reply)
515 {
516     std::string domain = data.ReadString();
517     std::string domainAccountName = data.ReadString();
518     if (domain.empty() || domain.size() > Constants::DOMAIN_NAME_MAX_SIZE) {
519         ACCOUNT_LOGE("failed to read string for domain name. length %{public}zu.", domain.size());
520         reply.WriteInt32(ERR_OSACCOUNT_KIT_DOMAIN_NAME_LENGTH_INVALID_ERROR);
521         return ERR_NONE;
522     }
523 
524     if (domainAccountName.empty() || domainAccountName.size() > Constants::DOMAIN_ACCOUNT_NAME_MAX_SIZE) {
525         ACCOUNT_LOGE("failed to read string for domainAccountName. length %{public}zu.", domainAccountName.size());
526         reply.WriteInt32(ERR_OSACCOUNT_KIT_DOMAIN_ACCOUNT_NAME_LENGTH_INVALID_ERROR);
527         return ERR_NONE;
528     }
529 
530     int localId = -1;
531     DomainAccountInfo domainInfo(domain, domainAccountName);
532     ErrCode result = GetOsAccountLocalIdFromDomain(domainInfo, localId);
533     if (!reply.WriteInt32(result)) {
534         ACCOUNT_LOGE("failed to write reply");
535         return IPC_STUB_WRITE_PARCEL_ERR;
536     }
537     if (!reply.WriteInt32(localId)) {
538         ACCOUNT_LOGE("failed to write reply");
539         return IPC_STUB_WRITE_PARCEL_ERR;
540     }
541     return ERR_NONE;
542 }
543 
ProcGetOsAccountTypeFromProcess(MessageParcel & data,MessageParcel & reply)544 ErrCode OsAccountStub::ProcGetOsAccountTypeFromProcess(MessageParcel &data, MessageParcel &reply)
545 {
546     OsAccountType type = OsAccountType::ADMIN;
547     ErrCode result = GetOsAccountTypeFromProcess(type);
548     if (!reply.WriteInt32(result)) {
549         ACCOUNT_LOGE("failed to write reply");
550         return IPC_STUB_WRITE_PARCEL_ERR;
551     }
552     if (!reply.WriteInt32(type)) {
553         ACCOUNT_LOGE("failed to write reply");
554         return IPC_STUB_WRITE_PARCEL_ERR;
555     }
556     return ERR_NONE;
557 }
558 
ProcGetApplicationConstraints(MessageParcel & data,MessageParcel & reply)559 ErrCode OsAccountStub::ProcGetApplicationConstraints(MessageParcel &data, MessageParcel &reply)
560 {
561     return ERR_NONE;
562 }
563 
ProcGetApplicationConstraintsByNumber(MessageParcel & data,MessageParcel & reply)564 ErrCode OsAccountStub::ProcGetApplicationConstraintsByNumber(MessageParcel &data, MessageParcel &reply)
565 {
566     return ERR_NONE;
567 }
568 
ProcGetOsAccountLocalIdBySerialNumber(MessageParcel & data,MessageParcel & reply)569 ErrCode OsAccountStub::ProcGetOsAccountLocalIdBySerialNumber(MessageParcel &data, MessageParcel &reply)
570 {
571     int64_t serialNumber = data.ReadInt64();
572     int id = 0;
573     ErrCode result = GetOsAccountLocalIdBySerialNumber(serialNumber, id);
574     if (!reply.WriteInt32(result)) {
575         ACCOUNT_LOGE("failed to write reply");
576         return IPC_STUB_WRITE_PARCEL_ERR;
577     }
578     if (!reply.WriteInt32(id)) {
579         ACCOUNT_LOGE("failed to write reply");
580         return IPC_STUB_WRITE_PARCEL_ERR;
581     }
582     return ERR_NONE;
583 }
584 
ProcGetSerialNumberByOsAccountLocalId(MessageParcel & data,MessageParcel & reply)585 ErrCode OsAccountStub::ProcGetSerialNumberByOsAccountLocalId(MessageParcel &data, MessageParcel &reply)
586 {
587     int id = data.ReadInt32();
588     int64_t serialNumber = 0;
589     ErrCode result = GetSerialNumberByOsAccountLocalId(id, serialNumber);
590     if (!reply.WriteInt32(result)) {
591         ACCOUNT_LOGE("failed to write reply");
592         return IPC_STUB_WRITE_PARCEL_ERR;
593     }
594     if (!reply.WriteInt64(serialNumber)) {
595         ACCOUNT_LOGE("failed to write reply");
596         return IPC_STUB_WRITE_PARCEL_ERR;
597     }
598     return ERR_NONE;
599 }
600 
ProcIsOsAccountActived(MessageParcel & data,MessageParcel & reply)601 ErrCode OsAccountStub::ProcIsOsAccountActived(MessageParcel &data, MessageParcel &reply)
602 {
603     int localId = data.ReadInt32();
604     if (localId == -1) {
605         ACCOUNT_LOGE("failed to read int for osAccountType");
606         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
607         return ERR_NONE;
608     }
609     bool isOsAccountActived = false;
610     ErrCode result = IsOsAccountActived(localId, isOsAccountActived);
611     if (!reply.WriteInt32(result)) {
612         ACCOUNT_LOGE("failed to write reply");
613         return IPC_STUB_WRITE_PARCEL_ERR;
614     }
615     if (!reply.WriteBool(isOsAccountActived)) {
616         ACCOUNT_LOGE("failed to write reply");
617         return IPC_STUB_WRITE_PARCEL_ERR;
618     }
619     return ERR_NONE;
620 }
621 
ProcIsOsAccountConstraintEnable(MessageParcel & data,MessageParcel & reply)622 ErrCode OsAccountStub::ProcIsOsAccountConstraintEnable(MessageParcel &data, MessageParcel &reply)
623 {
624     int localId = data.ReadInt32();
625     if (localId == -1) {
626         ACCOUNT_LOGE("failed to read int for osAccountType");
627         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
628         return ERR_NONE;
629     }
630     std::string constraint = data.ReadString();
631     bool isConstraintEnable = false;
632     ErrCode result = IsOsAccountConstraintEnable(localId, constraint, isConstraintEnable);
633     if (!reply.WriteInt32(result)) {
634         ACCOUNT_LOGE("failed to write reply");
635         return IPC_STUB_WRITE_PARCEL_ERR;
636     }
637     if (!reply.WriteBool(isConstraintEnable)) {
638         ACCOUNT_LOGE("failed to write reply");
639         return IPC_STUB_WRITE_PARCEL_ERR;
640     }
641     return ERR_NONE;
642 }
643 
ProcIsMultiOsAccountEnable(MessageParcel & data,MessageParcel & reply)644 ErrCode OsAccountStub::ProcIsMultiOsAccountEnable(MessageParcel &data, MessageParcel &reply)
645 {
646     bool isMultiOsAccountEnable = false;
647     ErrCode result = IsMultiOsAccountEnable(isMultiOsAccountEnable);
648     if (!reply.WriteInt32(result)) {
649         ACCOUNT_LOGE("failed to write reply");
650         return IPC_STUB_WRITE_PARCEL_ERR;
651     }
652     if (!reply.WriteBool(isMultiOsAccountEnable)) {
653         ACCOUNT_LOGE("failed to write reply");
654         return IPC_STUB_WRITE_PARCEL_ERR;
655     }
656     return ERR_NONE;
657 }
658 
ProcIsOsAccountVerified(MessageParcel & data,MessageParcel & reply)659 ErrCode OsAccountStub::ProcIsOsAccountVerified(MessageParcel &data, MessageParcel &reply)
660 {
661     int localId = data.ReadInt32();
662     if (localId == -1) {
663         ACCOUNT_LOGE("failed to read int for localId");
664         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
665         return ERR_NONE;
666     }
667     bool isVerified = false;
668     ErrCode result = IsOsAccountVerified(localId, isVerified);
669     if (!reply.WriteInt32(result)) {
670         ACCOUNT_LOGE("failed to write reply");
671         return IPC_STUB_WRITE_PARCEL_ERR;
672     }
673     if (!reply.WriteBool(isVerified)) {
674         ACCOUNT_LOGE("failed to write reply");
675         return IPC_STUB_WRITE_PARCEL_ERR;
676     }
677     return ERR_NONE;
678 }
679 
ProcIsOsAccountExists(MessageParcel & data,MessageParcel & reply)680 ErrCode OsAccountStub::ProcIsOsAccountExists(MessageParcel &data, MessageParcel &reply)
681 {
682     int localId = data.ReadInt32();
683     if (localId == -1) {
684         ACCOUNT_LOGE("failed to read int for osAccountType");
685         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
686         return ERR_NONE;
687     }
688     bool isOsAccountExists = false;
689     ErrCode result = IsOsAccountExists(localId, isOsAccountExists);
690     if (!reply.WriteInt32(result)) {
691         ACCOUNT_LOGE("failed to write reply");
692         return IPC_STUB_WRITE_PARCEL_ERR;
693     }
694     if (!reply.WriteBool(isOsAccountExists)) {
695         ACCOUNT_LOGE("failed to write reply");
696         return IPC_STUB_WRITE_PARCEL_ERR;
697     }
698     return ERR_NONE;
699 }
700 
ProcSubscribeOsAccount(MessageParcel & data,MessageParcel & reply)701 ErrCode OsAccountStub::ProcSubscribeOsAccount(MessageParcel &data, MessageParcel &reply)
702 {
703     std::unique_ptr<OsAccountSubscribeInfo> subscribeInfo(data.ReadParcelable<OsAccountSubscribeInfo>());
704     if (!subscribeInfo) {
705         ACCOUNT_LOGE("failed to read parcelable for subscribeInfo");
706         return IPC_STUB_INVALID_DATA_ERR;
707     }
708 
709     sptr<IRemoteObject> eventListener = data.ReadRemoteObject();
710     if (eventListener == nullptr) {
711         ACCOUNT_LOGE("failed to read remote object for eventListener");
712         return IPC_STUB_INVALID_DATA_ERR;
713     }
714 
715     ErrCode result = SubscribeOsAccount(*subscribeInfo, eventListener);
716     if (!reply.WriteInt32(result)) {
717         ACCOUNT_LOGE("failed to write reply");
718         return IPC_STUB_WRITE_PARCEL_ERR;
719     }
720 
721     return ERR_NONE;
722 }
723 
ProcUnsubscribeOsAccount(MessageParcel & data,MessageParcel & reply)724 ErrCode OsAccountStub::ProcUnsubscribeOsAccount(MessageParcel &data, MessageParcel &reply)
725 {
726     sptr<IRemoteObject> eventListener = data.ReadRemoteObject();
727     if (eventListener == nullptr) {
728         ACCOUNT_LOGE("failed to read remote object for eventListener");
729         return IPC_STUB_INVALID_DATA_ERR;
730     }
731 
732     ErrCode result = UnsubscribeOsAccount(eventListener);
733     if (!reply.WriteInt32(result)) {
734         ACCOUNT_LOGE("failed to write reply");
735         return IPC_STUB_WRITE_PARCEL_ERR;
736     }
737     return ERR_NONE;
738 }
739 
ProcActivateOsAccount(MessageParcel & data,MessageParcel & reply)740 ErrCode OsAccountStub::ProcActivateOsAccount(MessageParcel &data, MessageParcel &reply)
741 {
742     int localId = data.ReadInt32();
743     if (localId == -1) {
744         ACCOUNT_LOGE("failed to read int for localId");
745         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
746         return ERR_NONE;
747     }
748     ErrCode result = ActivateOsAccount(localId);
749     if (!reply.WriteInt32(result)) {
750         ACCOUNT_LOGE("failed to write reply");
751         return IPC_STUB_WRITE_PARCEL_ERR;
752     }
753     return ERR_NONE;
754 }
755 
ProcStartOsAccount(MessageParcel & data,MessageParcel & reply)756 ErrCode OsAccountStub::ProcStartOsAccount(MessageParcel &data, MessageParcel &reply)
757 {
758     int localId = data.ReadInt32();
759     if (localId == -1) {
760         ACCOUNT_LOGE("failed to read int for localId");
761         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
762         return ERR_NONE;
763     }
764     ErrCode result = StartOsAccount(localId);
765     if (!reply.WriteInt32(result)) {
766         ACCOUNT_LOGE("failed to write reply");
767         return IPC_STUB_WRITE_PARCEL_ERR;
768     }
769     return ERR_NONE;
770 }
771 
ProcStopOsAccount(MessageParcel & data,MessageParcel & reply)772 ErrCode OsAccountStub::ProcStopOsAccount(MessageParcel &data, MessageParcel &reply)
773 {
774     int localId = data.ReadInt32();
775     if (localId == -1) {
776         ACCOUNT_LOGE("failed to read int for localId");
777         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
778         return ERR_NONE;
779     }
780     ErrCode result = StopOsAccount(localId);
781     if (!reply.WriteInt32(result)) {
782         ACCOUNT_LOGE("failed to write reply");
783         return IPC_STUB_WRITE_PARCEL_ERR;
784     }
785     return ERR_NONE;
786 }
787 
ProcGetOsAccountSwitchMod(MessageParcel & data,MessageParcel & reply)788 ErrCode OsAccountStub::ProcGetOsAccountSwitchMod(MessageParcel &data, MessageParcel &reply)
789 {
790     OS_ACCOUNT_SWITCH_MOD osAccountSwitchMod = GetOsAccountSwitchMod();
791     if (!reply.WriteInt32(osAccountSwitchMod)) {
792         ACCOUNT_LOGE("failed to write reply");
793         return IPC_STUB_WRITE_PARCEL_ERR;
794     }
795     return ERR_NONE;
796 }
797 
ProcIsCurrentOsAccountVerified(MessageParcel & data,MessageParcel & reply)798 ErrCode OsAccountStub::ProcIsCurrentOsAccountVerified(MessageParcel &data, MessageParcel &reply)
799 {
800     bool isVerified = false;
801     ErrCode result = IsCurrentOsAccountVerified(isVerified);
802     if (!reply.WriteInt32(result)) {
803         ACCOUNT_LOGE("failed to write reply");
804         return IPC_STUB_WRITE_PARCEL_ERR;
805     }
806     reply.WriteBool(isVerified);
807     return ERR_NONE;
808 }
809 
ProcIsOsAccountCompleted(MessageParcel & data,MessageParcel & reply)810 ErrCode OsAccountStub::ProcIsOsAccountCompleted(MessageParcel &data, MessageParcel &reply)
811 {
812     int localId = data.ReadInt32();
813     if (localId == -1) {
814         ACCOUNT_LOGE("failed to read int for localId");
815         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
816         return ERR_NONE;
817     }
818     bool isOsAccountCompleted = false;
819     ErrCode result = IsOsAccountCompleted(localId, isOsAccountCompleted);
820     if (!reply.WriteInt32(result)) {
821         ACCOUNT_LOGE("failed to write reply");
822         return IPC_STUB_WRITE_PARCEL_ERR;
823     }
824     reply.WriteBool(isOsAccountCompleted);
825     return ERR_NONE;
826 }
827 
ProcSetCurrentOsAccountIsVerified(MessageParcel & data,MessageParcel & reply)828 ErrCode OsAccountStub::ProcSetCurrentOsAccountIsVerified(MessageParcel &data, MessageParcel &reply)
829 {
830     bool isVerified = data.ReadBool();
831     ErrCode result = SetCurrentOsAccountIsVerified(isVerified);
832     if (!reply.WriteInt32(result)) {
833         ACCOUNT_LOGE("failed to write reply");
834         return IPC_STUB_WRITE_PARCEL_ERR;
835     }
836     return ERR_NONE;
837 }
838 
ProcSetOsAccountIsVerified(MessageParcel & data,MessageParcel & reply)839 ErrCode OsAccountStub::ProcSetOsAccountIsVerified(MessageParcel &data, MessageParcel &reply)
840 {
841     int localId = data.ReadInt32();
842     if (localId == -1) {
843         ACCOUNT_LOGE("failed to read int for localId");
844         reply.WriteInt32(ERR_OSACCOUNT_KIT_READ_IN_LOCALID_ERROR);
845         return ERR_NONE;
846     }
847     bool isVerified = data.ReadBool();
848     ErrCode result = SetOsAccountIsVerified(localId, isVerified);
849     if (!reply.WriteInt32(result)) {
850         ACCOUNT_LOGE("failed to write reply");
851         return IPC_STUB_WRITE_PARCEL_ERR;
852     }
853     return ERR_NONE;
854 }
855 
ProcDumpState(MessageParcel & data,MessageParcel & reply)856 ErrCode OsAccountStub::ProcDumpState(MessageParcel &data, MessageParcel &reply)
857 {
858     int id = data.ReadInt32();
859     std::vector<std::string> state;
860 
861     ErrCode result = DumpState(id, state);
862     if (!reply.WriteInt32(result)) {
863         ACCOUNT_LOGE("failed to write reply");
864         return IPC_STUB_WRITE_PARCEL_ERR;
865     }
866 
867     if (!reply.WriteUint32(state.size())) {
868         ACCOUNT_LOGE("failed to write reply");
869         return IPC_STUB_WRITE_PARCEL_ERR;
870     }
871 
872     for (auto info : state) {
873         if (!reply.WriteString(info)) {
874             ACCOUNT_LOGE("failed to write reply");
875             return IPC_STUB_WRITE_PARCEL_ERR;
876         }
877     }
878 
879     return ERR_NONE;
880 }
881 
ProcGetCreatedOsAccountNumFromDatabase(MessageParcel & data,MessageParcel & reply)882 ErrCode OsAccountStub::ProcGetCreatedOsAccountNumFromDatabase(MessageParcel &data, MessageParcel &reply)
883 {
884     std::string storeID = data.ReadString();
885     int createdOsAccountNum = -1;
886     ErrCode result = GetCreatedOsAccountNumFromDatabase(storeID, createdOsAccountNum);
887     if (!reply.WriteInt32(result)) {
888         ACCOUNT_LOGE("failed to write reply");
889         return IPC_STUB_WRITE_PARCEL_ERR;
890     }
891     if (!reply.WriteInt32(createdOsAccountNum)) {
892         ACCOUNT_LOGE("failed to write reply");
893         return IPC_STUB_WRITE_PARCEL_ERR;
894     }
895     return ERR_NONE;
896 }
897 
ProcGetSerialNumberFromDatabase(MessageParcel & data,MessageParcel & reply)898 ErrCode OsAccountStub::ProcGetSerialNumberFromDatabase(MessageParcel &data, MessageParcel &reply)
899 {
900     std::string storeID = data.ReadString();
901     int64_t serialNumber = -1;
902     ErrCode result = GetSerialNumberFromDatabase(storeID, serialNumber);
903     if (!reply.WriteInt32(result)) {
904         ACCOUNT_LOGE("failed to write reply");
905         return IPC_STUB_WRITE_PARCEL_ERR;
906     }
907     if (!reply.WriteInt64(serialNumber)) {
908         ACCOUNT_LOGE("failed to write reply");
909         return IPC_STUB_WRITE_PARCEL_ERR;
910     }
911     return ERR_NONE;
912 }
913 
ProcGetMaxAllowCreateIdFromDatabase(MessageParcel & data,MessageParcel & reply)914 ErrCode OsAccountStub::ProcGetMaxAllowCreateIdFromDatabase(MessageParcel &data, MessageParcel &reply)
915 {
916     std::string storeID = data.ReadString();
917     int id = -1;
918     ErrCode result = GetMaxAllowCreateIdFromDatabase(storeID, id);
919     if (!reply.WriteInt32(result)) {
920         ACCOUNT_LOGE("failed to write reply");
921         return IPC_STUB_WRITE_PARCEL_ERR;
922     }
923     if (!reply.WriteInt32(id)) {
924         ACCOUNT_LOGE("failed to write reply");
925         return IPC_STUB_WRITE_PARCEL_ERR;
926     }
927     return ERR_NONE;
928 }
929 
ProcGetOsAccountFromDatabase(MessageParcel & data,MessageParcel & reply)930 ErrCode OsAccountStub::ProcGetOsAccountFromDatabase(MessageParcel &data, MessageParcel &reply)
931 {
932     std::string storeID = data.ReadString();
933     int id = data.ReadInt32();
934     OsAccountInfo osAccountInfo;
935     ErrCode result = GetOsAccountFromDatabase(storeID, id, osAccountInfo);
936     if (!reply.WriteInt32(result)) {
937         ACCOUNT_LOGE("failed to write reply");
938         return IPC_STUB_WRITE_PARCEL_ERR;
939     }
940     if (!reply.WriteParcelable(&osAccountInfo)) {
941         ACCOUNT_LOGE("failed to write reply");
942         return IPC_STUB_WRITE_PARCEL_ERR;
943     }
944     return ERR_NONE;
945 }
946 
ProcGetOsAccountListFromDatabase(MessageParcel & data,MessageParcel & reply)947 ErrCode OsAccountStub::ProcGetOsAccountListFromDatabase(MessageParcel &data, MessageParcel &reply)
948 {
949     std::string storeID = data.ReadString();
950     std::vector<OsAccountInfo> osAccountList;
951     ErrCode result = GetOsAccountListFromDatabase(storeID, osAccountList);
952     if (!reply.WriteInt32(result)) {
953         ACCOUNT_LOGE("failed to write reply");
954         return IPC_STUB_WRITE_PARCEL_ERR;
955     }
956     if (!WriteParcelableVector(osAccountList, reply)) {
957         ACCOUNT_LOGE("ProcGetOsAccountListFromDatabase osAccountInfos failed stub");
958         ACCOUNT_LOGE("failed to write reply");
959         return IPC_STUB_WRITE_PARCEL_ERR;
960     }
961     return ERR_NONE;
962 }
963 
ProcQueryActiveOsAccountIds(MessageParcel & data,MessageParcel & reply)964 ErrCode OsAccountStub::ProcQueryActiveOsAccountIds(MessageParcel &data, MessageParcel &reply)
965 {
966     std::vector<int32_t> ids;
967     ErrCode result = QueryActiveOsAccountIds(ids);
968     if (!reply.WriteInt32(result)) {
969         ACCOUNT_LOGE("failed to write reply");
970         return IPC_STUB_WRITE_PARCEL_ERR;
971     }
972     if (!reply.WriteInt32Vector(ids)) {
973         ACCOUNT_LOGE("failed to write active list");
974         return IPC_STUB_WRITE_PARCEL_ERR;
975     }
976     return ERR_NONE;
977 }
978 }  // namespace AccountSA
979 }  // namespace OHOS
980