• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
18 #include "os_account_event_proxy.h"
19 
20 namespace OHOS {
21 namespace AccountSA {
OsAccountEventProxy(const sptr<IRemoteObject> & object)22 OsAccountEventProxy::OsAccountEventProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IOsAccountEvent>(object)
23 {}
24 
~OsAccountEventProxy()25 OsAccountEventProxy::~OsAccountEventProxy()
26 {}
27 
OnAccountsChanged(const int & localId)28 void OsAccountEventProxy::OnAccountsChanged(const int &localId)
29 {
30     MessageParcel data;
31     MessageParcel reply;
32 
33     if (!data.WriteInterfaceToken(GetDescriptor())) {
34         ACCOUNT_LOGE("failed to write descriptor!");
35         return;
36     }
37 
38     if (!data.WriteInt32(localId)) {
39         ACCOUNT_LOGE("failed to write WriteInt32 localId %{public}d.", localId);
40         return;
41     }
42 
43     ErrCode result = SendRequest(OsAccountEventInterfaceCode::ACCOUNT_CHANGED, data, reply);
44     if (result != ERR_OK) {
45         ACCOUNT_LOGE("SendRequest for account changed failed! result %{public}d, localId %{public}d.",
46             result, localId);
47         return;
48     }
49 }
50 
OnAccountsSwitch(const int & newId,const int & oldId)51 void OsAccountEventProxy::OnAccountsSwitch(const int &newId, const int &oldId)
52 {
53     MessageParcel data;
54     if (!data.WriteInterfaceToken(GetDescriptor())) {
55         ACCOUNT_LOGE("Write descriptor failed.");
56         return;
57     }
58 
59     if (!data.WriteInt32(newId)) {
60         ACCOUNT_LOGE("Write newId failed.");
61         return;
62     }
63 
64     if (!data.WriteInt32(oldId)) {
65         ACCOUNT_LOGE("Write oldId failed.");
66         return;
67     }
68     MessageParcel reply;
69     ErrCode result = SendRequest(OsAccountEventInterfaceCode::ACCOUNT_SWITCHED, data, reply);
70     if (result != ERR_OK) {
71         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
72         return;
73     }
74 }
75 
SendRequest(OsAccountEventInterfaceCode code,MessageParcel & data,MessageParcel & reply)76 ErrCode OsAccountEventProxy::SendRequest(OsAccountEventInterfaceCode code, MessageParcel &data, MessageParcel &reply)
77 {
78     sptr<IRemoteObject> remote = Remote();
79     if (remote == nullptr) {
80         ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
81         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
82     }
83 
84     MessageOption option(MessageOption::TF_SYNC);
85     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
86     if (result != ERR_OK) {
87         ACCOUNT_LOGE("failed to send os account event request, code = %{public}d, result = %{public}d", code, result);
88     }
89     return result;
90 }
91 }  // namespace AccountSA
92 }  // namespace OHOS
93