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 
16 #include "app_account_event_stub.h"
17 
18 #include "account_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace AccountSA {
AppAccountEventStub()22 AppAccountEventStub::AppAccountEventStub()
23 {}
24 
~AppAccountEventStub()25 AppAccountEventStub::~AppAccountEventStub()
26 {}
27 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)28 int AppAccountEventStub::OnRemoteRequest(
29     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
30 {
31     if (data.ReadInterfaceToken() != GetDescriptor()) {
32         ACCOUNT_LOGE("failed to check descriptor! code %{public}u.", code);
33         return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
34     }
35 
36     switch (code) {
37         case static_cast<uint32_t>(AppAccountEventInterfaceCode::ACCOUNT_CHANGED):
38             return ProcOnAccountsChanged(data);
39         default:
40             ACCOUNT_LOGI("default, code = %{public}u, flags = %{public}u", code, option.GetFlags());
41             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
42     }
43 
44     return ERR_NONE;
45 }
46 
ProcOnAccountsChanged(MessageParcel & data)47 ErrCode AppAccountEventStub::ProcOnAccountsChanged(MessageParcel &data)
48 {
49     uint32_t size = 0;
50     if (!data.ReadUint32(size)) {
51         ACCOUNT_LOGE("failed to the account list size");
52         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
53     }
54 
55     if (size > Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT) {
56         ACCOUNT_LOGE("ReadAppAccountList failed");
57         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
58     }
59     std::vector<AppAccountInfo> accounts;
60     for (uint32_t index = 0; index < size; index++) {
61         std::shared_ptr<AppAccountInfo> account(data.ReadParcelable<AppAccountInfo>());
62         if (account == nullptr) {
63             ACCOUNT_LOGE("failed read app account info");
64             return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
65         }
66         accounts.emplace_back(*account);
67     }
68     OnAccountsChanged(accounts);
69     return ERR_NONE;
70 }
71 }  // namespace AccountSA
72 }  // namespace OHOS
73