1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "account_log_wrapper.h"
17 #include "distributed_account_event_proxy.h"
18 
19 namespace OHOS {
20 namespace AccountSA {
DistributedAccountEventProxy(const sptr<IRemoteObject> & object)21 DistributedAccountEventProxy::DistributedAccountEventProxy(
22     const sptr<IRemoteObject> &object) : IRemoteProxy<IDistributedAccountEvent>(object)
23 {}
24 
~DistributedAccountEventProxy()25 DistributedAccountEventProxy::~DistributedAccountEventProxy()
26 {}
27 
OnAccountsChanged(const DistributedAccountEventData & eventData)28 void DistributedAccountEventProxy::OnAccountsChanged(const DistributedAccountEventData &eventData)
29 {
30     MessageParcel data;
31     MessageParcel reply;
32 
33     if (!data.WriteInterfaceToken(GetDescriptor())) {
34         ACCOUNT_LOGE("Write descriptor failed.");
35         return;
36     }
37 
38     if (!data.WriteParcelable(&eventData)) {
39         ACCOUNT_LOGE("Write eventData failed, eventData.id=%{public}d.", eventData.id_);
40         return;
41     }
42 
43     ErrCode result = SendRequest(DistributedAccountEventInterfaceCode::ON_ACCOUNT_CHANGED, data, reply);
44     if (result != ERR_OK) {
45         ACCOUNT_LOGE("SendRequest for account changed failed, result=%{public}d eventData.id=%{public}d.",
46             result, eventData.id_);
47         return;
48     }
49 }
50 
SendRequest(DistributedAccountEventInterfaceCode code,MessageParcel & data,MessageParcel & reply)51 ErrCode DistributedAccountEventProxy::SendRequest(
52     DistributedAccountEventInterfaceCode code, MessageParcel &data, MessageParcel &reply)
53 {
54     sptr<IRemoteObject> remote = Remote();
55     if (remote == nullptr) {
56         ACCOUNT_LOGE("Remote is nullptr, code=%{public}d.", code);
57         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
58     }
59 
60     MessageOption option(MessageOption::TF_SYNC);
61     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
62     if (result != ERR_OK) {
63         ACCOUNT_LOGE("Send distributed account event request failed, code=%{public}d result=%{public}d.",
64             code, result);
65     }
66     return result;
67 }
68 }  // namespace AccountSA
69 }  // namespace OHOS
70