1 /*
2 * Copyright (c) 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 "domain_account_proxy.h"
17
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20
21 namespace OHOS {
22 namespace AccountSA {
DomainAccountProxy(const sptr<IRemoteObject> & object)23 DomainAccountProxy::DomainAccountProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IDomainAccount>(object)
24 {}
25
~DomainAccountProxy()26 DomainAccountProxy::~DomainAccountProxy()
27 {}
28
SendRequest(IDomainAccount::Message code,MessageParcel & data,MessageParcel & reply)29 ErrCode DomainAccountProxy::SendRequest(IDomainAccount::Message code, MessageParcel &data, MessageParcel &reply)
30 {
31 sptr<IRemoteObject> remote = Remote();
32 if (remote == nullptr) {
33 ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
34 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
35 }
36 MessageOption option(MessageOption::TF_SYNC);
37 return remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
38 }
39
RegisterPlugin(const sptr<IDomainAccountPlugin> & plugin)40 ErrCode DomainAccountProxy::RegisterPlugin(const sptr<IDomainAccountPlugin> &plugin)
41 {
42 MessageParcel data;
43 if (!data.WriteInterfaceToken(GetDescriptor())) {
44 ACCOUNT_LOGE("fail to write descriptor");
45 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
46 }
47 if ((plugin == nullptr) || (!data.WriteRemoteObject(plugin->AsObject()))) {
48 ACCOUNT_LOGE("fail to write plugin");
49 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
50 }
51 MessageParcel reply;
52 ErrCode result = SendRequest(IDomainAccount::Message::REGISTER_PLUGIN, data, reply);
53 if (result != ERR_OK) {
54 return result;
55 }
56 if (!reply.ReadInt32(result)) {
57 ACCOUNT_LOGE("fail to read result");
58 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
59 }
60 return result;
61 }
62
UnregisterPlugin()63 ErrCode DomainAccountProxy::UnregisterPlugin()
64 {
65 MessageParcel data;
66 if (!data.WriteInterfaceToken(GetDescriptor())) {
67 ACCOUNT_LOGE("fail to write descriptor");
68 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
69 }
70 MessageParcel reply;
71 ErrCode result = SendRequest(IDomainAccount::Message::UNREGISTER_PLUGIN, data, reply);
72 if (result != ERR_OK) {
73 return result;
74 }
75 if (!reply.ReadInt32(result)) {
76 ACCOUNT_LOGE("fail to read result");
77 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
78 }
79 return result;
80 }
81
Auth(const DomainAccountInfo & info,const std::vector<uint8_t> & password,const sptr<IDomainAuthCallback> & callback)82 ErrCode DomainAccountProxy::Auth(const DomainAccountInfo &info, const std::vector<uint8_t> &password,
83 const sptr<IDomainAuthCallback> &callback)
84 {
85 MessageParcel data;
86 if (!data.WriteInterfaceToken(GetDescriptor())) {
87 ACCOUNT_LOGE("fail to write descriptor");
88 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
89 }
90 if (!data.WriteString(info.accountName_)) {
91 ACCOUNT_LOGE("fail to write name");
92 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
93 }
94 if (!data.WriteString(info.domain_)) {
95 ACCOUNT_LOGE("fail to write domain");
96 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
97 }
98 if (!data.WriteUInt8Vector(password)) {
99 ACCOUNT_LOGE("fail to write password");
100 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
101 }
102 if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
103 ACCOUNT_LOGE("fail to write callback");
104 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
105 }
106 MessageParcel reply;
107 ErrCode result = SendRequest(IDomainAccount::Message::DOMAIN_AUTH, data, reply);
108 if (result != ERR_OK) {
109 ACCOUNT_LOGE("fail to send request, error: %{public}d", result);
110 return result;
111 }
112 if (!reply.ReadInt32(result)) {
113 ACCOUNT_LOGE("fail to read result");
114 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
115 }
116 return result;
117 }
118
AuthUser(int32_t userId,const std::vector<uint8_t> & password,const sptr<IDomainAuthCallback> & callback)119 ErrCode DomainAccountProxy::AuthUser(int32_t userId, const std::vector<uint8_t> &password,
120 const sptr<IDomainAuthCallback> &callback)
121 {
122 MessageParcel data;
123 if (!data.WriteInterfaceToken(GetDescriptor())) {
124 ACCOUNT_LOGE("fail to write descriptor");
125 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
126 }
127 if (!data.WriteInt32(userId)) {
128 ACCOUNT_LOGE("fail to write userId");
129 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
130 }
131 if (!data.WriteUInt8Vector(password)) {
132 ACCOUNT_LOGE("fail to write password");
133 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
134 }
135 if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
136 ACCOUNT_LOGE("fail to write callback");
137 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
138 }
139 MessageParcel reply;
140 ErrCode result = SendRequest(IDomainAccount::Message::DOMAIN_AUTH_USER, data, reply);
141 if (result != ERR_OK) {
142 ACCOUNT_LOGE("fail to send request, error: %{public}d", result);
143 return result;
144 }
145 if (!reply.ReadInt32(result)) {
146 ACCOUNT_LOGE("fail to read result");
147 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
148 }
149 return result;
150 }
151 } // namespace AccountSA
152 } // namespace OHOS