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_proxy.h"
17 
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace AccountSA {
AppAccountEventProxy(const sptr<IRemoteObject> & object)23 AppAccountEventProxy::AppAccountEventProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IAppAccountEvent>(object)
24 {}
25 
~AppAccountEventProxy()26 AppAccountEventProxy::~AppAccountEventProxy()
27 {}
28 
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)29 void AppAccountEventProxy::OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
30 {
31     MessageParcel data;
32     MessageParcel reply;
33 
34     if (!data.WriteInterfaceToken(GetDescriptor())) {
35         ACCOUNT_LOGE("failed to write descriptor!");
36         return;
37     }
38 
39     if (!WriteParcelableVector(accounts, data)) {
40         ACCOUNT_LOGE("failed to write WriteVector accounts");
41         return;
42     }
43 
44     ErrCode result = SendRequest(AppAccountEventInterfaceCode::ACCOUNT_CHANGED, data, reply);
45     if (result != ERR_OK) {
46         ACCOUNT_LOGE("SendRequest failed! error code %{public}d.", result);
47         return;
48     }
49 }
50 
SendRequest(AppAccountEventInterfaceCode code,MessageParcel & data,MessageParcel & reply)51 ErrCode AppAccountEventProxy::SendRequest(AppAccountEventInterfaceCode code, MessageParcel &data, MessageParcel &reply)
52 {
53     sptr<IRemoteObject> remoteEvent = Remote();
54     if (remoteEvent == nullptr) {
55         ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
56         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
57     }
58 
59     MessageOption option(MessageOption::TF_SYNC);
60     int32_t result = remoteEvent->SendRequest(static_cast<uint32_t>(code), data, reply, option);
61     if (result != ERR_OK) {
62         ACCOUNT_LOGE("failed to SendRequest, code = %{public}d, result = %{public}d", code, result);
63         return ERR_APPACCOUNT_KIT_SEND_REQUEST;
64     }
65 
66     return ERR_OK;
67 }
68 
69 template<typename T>
WriteParcelableVector(const std::vector<T> & parcelableVector,Parcel & data)70 bool AppAccountEventProxy::WriteParcelableVector(const std::vector<T> &parcelableVector, Parcel &data)
71 {
72     if (!data.WriteUint32(parcelableVector.size())) {
73         ACCOUNT_LOGE("failed to WriteInt32 for parcelableVector.size()");
74         return false;
75     }
76 
77     for (const auto &parcelable : parcelableVector) {
78         if (!data.WriteParcelable(&parcelable)) {
79             ACCOUNT_LOGE("failed to WriteParcelable for parcelable");
80             return false;
81         }
82     }
83 
84     return true;
85 }
86 }  // namespace AccountSA
87 }  // namespace OHOS
88