1 /*
2 * Copyright (c) 2022-2023 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(DomainAccountInterfaceCode code,MessageParcel & data,MessageParcel & reply)29 ErrCode DomainAccountProxy::SendRequest(DomainAccountInterfaceCode 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
HasDomainAccount(const DomainAccountInfo & info,const sptr<IDomainAccountCallback> & callback)40 ErrCode DomainAccountProxy::HasDomainAccount(
41 const DomainAccountInfo &info, const sptr<IDomainAccountCallback> &callback)
42 {
43 MessageParcel data;
44 if (!data.WriteInterfaceToken(GetDescriptor())) {
45 ACCOUNT_LOGE("failed to write descriptor!");
46 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
47 }
48 if (!data.WriteParcelable(&info)) {
49 ACCOUNT_LOGE("fail to write parcelable");
50 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
51 }
52 if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
53 ACCOUNT_LOGE("fail to write callback");
54 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
55 }
56 MessageParcel reply;
57 ErrCode result = SendRequest(DomainAccountInterfaceCode::DOMAIN_HAS_DOMAIN_ACCOUNT, data, reply);
58 if (result != ERR_OK) {
59 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
60 return result;
61 }
62 if (!reply.ReadInt32(result)) {
63 ACCOUNT_LOGE("fail to read result");
64 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
65 }
66 return result;
67 }
68
RegisterPlugin(const sptr<IDomainAccountPlugin> & plugin)69 ErrCode DomainAccountProxy::RegisterPlugin(const sptr<IDomainAccountPlugin> &plugin)
70 {
71 MessageParcel data;
72 if (!data.WriteInterfaceToken(GetDescriptor())) {
73 ACCOUNT_LOGE("fail to write descriptor");
74 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
75 }
76 if ((plugin == nullptr) || (!data.WriteRemoteObject(plugin->AsObject()))) {
77 ACCOUNT_LOGE("fail to write plugin");
78 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
79 }
80 MessageParcel reply;
81 ErrCode result = SendRequest(DomainAccountInterfaceCode::REGISTER_PLUGIN, data, reply);
82 if (result != ERR_OK) {
83 return result;
84 }
85 if (!reply.ReadInt32(result)) {
86 ACCOUNT_LOGE("fail to read result");
87 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
88 }
89 return result;
90 }
91
UnregisterPlugin()92 ErrCode DomainAccountProxy::UnregisterPlugin()
93 {
94 MessageParcel data;
95 if (!data.WriteInterfaceToken(GetDescriptor())) {
96 ACCOUNT_LOGE("fail to write descriptor");
97 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
98 }
99 MessageParcel reply;
100 ErrCode result = SendRequest(DomainAccountInterfaceCode::UNREGISTER_PLUGIN, data, reply);
101 if (result != ERR_OK) {
102 return result;
103 }
104 if (!reply.ReadInt32(result)) {
105 ACCOUNT_LOGE("fail to read result");
106 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
107 }
108 return result;
109 }
110
GetAccountStatus(const DomainAccountInfo & info,DomainAccountStatus & status)111 ErrCode DomainAccountProxy::GetAccountStatus(const DomainAccountInfo &info, DomainAccountStatus &status)
112 {
113 MessageParcel data;
114 if (!data.WriteInterfaceToken(GetDescriptor())) {
115 ACCOUNT_LOGE("fail to write descriptor");
116 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
117 }
118 if (!data.WriteParcelable(&info)) {
119 ACCOUNT_LOGE("fail to write parcelable");
120 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
121 }
122
123 MessageParcel reply;
124 ErrCode result = SendRequest(DomainAccountInterfaceCode::DOMAIN_ACCOUNT_STATUS_ENQUIRY, data, reply);
125 if (result != ERR_OK) {
126 ACCOUNT_LOGE("fail to send request, error: %{public}d", result);
127 return result;
128 }
129 if (!reply.ReadInt32(result)) {
130 ACCOUNT_LOGE("fail to read result");
131 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
132 }
133 if (result != ERR_OK) {
134 ACCOUNT_LOGE("fail to read result");
135 return result;
136 }
137 int replyStatus;
138 if (!reply.ReadInt32(replyStatus)) {
139 ACCOUNT_LOGE("fail to read result");
140 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
141 }
142 status = static_cast<DomainAccountStatus>(replyStatus);
143 return ERR_OK;
144 }
145
RegisterAccountStatusListener(const DomainAccountInfo & info,const sptr<IDomainAccountCallback> & listener)146 ErrCode DomainAccountProxy::RegisterAccountStatusListener(
147 const DomainAccountInfo &info, const sptr<IDomainAccountCallback> &listener)
148 {
149 MessageParcel data;
150 if (!data.WriteInterfaceToken(GetDescriptor())) {
151 ACCOUNT_LOGE("failed to write descriptor!");
152 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
153 }
154 if (!data.WriteParcelable(&info)) {
155 ACCOUNT_LOGE("fail to write parcelable");
156 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
157 }
158 if ((listener == nullptr) || (!data.WriteRemoteObject(listener->AsObject()))) {
159 ACCOUNT_LOGE("fail to write callback");
160 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
161 }
162 MessageParcel reply;
163 ErrCode result =
164 SendRequest(DomainAccountInterfaceCode::DOMAIN_ACCOUNT_STATUS_LISTENER_REGISTER_BY_INFO, data, reply);
165 if (result != ERR_OK) {
166 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
167 return result;
168 }
169 if (!reply.ReadInt32(result)) {
170 ACCOUNT_LOGE("fail to read result");
171 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
172 }
173 return result;
174 }
175
RegisterAccountStatusListener(const sptr<IDomainAccountCallback> & listener)176 ErrCode DomainAccountProxy::RegisterAccountStatusListener(const sptr<IDomainAccountCallback> &listener)
177 {
178 MessageParcel data;
179 if (!data.WriteInterfaceToken(GetDescriptor())) {
180 ACCOUNT_LOGE("failed to write descriptor!");
181 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
182 }
183 if ((listener == nullptr) || (!data.WriteRemoteObject(listener->AsObject()))) {
184 ACCOUNT_LOGE("fail to write callback");
185 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
186 }
187 MessageParcel reply;
188 ErrCode result = SendRequest(DomainAccountInterfaceCode::DOMAIN_ACCOUNT_STATUS_LISTENER_REGISTER, data, reply);
189 if (result != ERR_OK) {
190 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
191 return result;
192 }
193 if (!reply.ReadInt32(result)) {
194 ACCOUNT_LOGE("fail to read result");
195 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
196 }
197 return result;
198 }
199
UnregisterAccountStatusListener(const sptr<IDomainAccountCallback> & listener)200 ErrCode DomainAccountProxy::UnregisterAccountStatusListener(const sptr<IDomainAccountCallback> &listener)
201 {
202 MessageParcel data;
203 if (!data.WriteInterfaceToken(GetDescriptor())) {
204 ACCOUNT_LOGE("failed to write descriptor!");
205 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
206 }
207 if ((listener == nullptr) || (!data.WriteRemoteObject(listener->AsObject()))) {
208 ACCOUNT_LOGE("fail to write callback");
209 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
210 }
211
212 MessageParcel reply;
213 ErrCode result = SendRequest(DomainAccountInterfaceCode::DOMAIN_ACCOUNT_STATUS_LISTENER_UNREGISTER, data, reply);
214 if (result != ERR_OK) {
215 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
216 return result;
217 }
218 if (!reply.ReadInt32(result)) {
219 ACCOUNT_LOGE("fail to read result");
220 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
221 }
222 return result;
223 }
224
UnregisterAccountStatusListener(const DomainAccountInfo & info,const sptr<IDomainAccountCallback> & listener)225 ErrCode DomainAccountProxy::UnregisterAccountStatusListener(
226 const DomainAccountInfo &info, const sptr<IDomainAccountCallback> &listener)
227 {
228 MessageParcel data;
229 if (!data.WriteInterfaceToken(GetDescriptor())) {
230 ACCOUNT_LOGE("failed to write descriptor!");
231 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
232 }
233 if (!data.WriteParcelable(&info)) {
234 ACCOUNT_LOGE("fail to write parcelable");
235 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
236 }
237 if ((listener == nullptr) || (!data.WriteRemoteObject(listener->AsObject()))) {
238 ACCOUNT_LOGE("fail to write callback");
239 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
240 }
241
242 MessageParcel reply;
243 ErrCode result =
244 SendRequest(DomainAccountInterfaceCode::DOMAIN_ACCOUNT_STATUS_LISTENER_UNREGISTER_BY_INFO, data, reply);
245 if (result != ERR_OK) {
246 ACCOUNT_LOGE("SendRequest err, result %{public}d.", result);
247 return result;
248 }
249 if (!reply.ReadInt32(result)) {
250 ACCOUNT_LOGE("fail to read result");
251 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
252 }
253 return result;
254 }
255
Auth(const DomainAccountInfo & info,const std::vector<uint8_t> & password,const sptr<IDomainAuthCallback> & callback)256 ErrCode DomainAccountProxy::Auth(
257 const DomainAccountInfo &info, const std::vector<uint8_t> &password, const sptr<IDomainAuthCallback> &callback)
258 {
259 MessageParcel data;
260 if (!data.WriteInterfaceToken(GetDescriptor())) {
261 ACCOUNT_LOGE("fail to write descriptor");
262 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
263 }
264 if (!data.WriteString(info.accountName_)) {
265 ACCOUNT_LOGE("fail to write name");
266 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
267 }
268 if (!data.WriteString(info.domain_)) {
269 ACCOUNT_LOGE("fail to write domain");
270 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
271 }
272 if (!data.WriteUInt8Vector(password)) {
273 ACCOUNT_LOGE("fail to write password");
274 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
275 }
276 if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
277 ACCOUNT_LOGE("fail to write callback");
278 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
279 }
280 MessageParcel reply;
281 ErrCode result = SendRequest(DomainAccountInterfaceCode::DOMAIN_AUTH, data, reply);
282 if (result != ERR_OK) {
283 ACCOUNT_LOGE("fail to send request, error: %{public}d", result);
284 return result;
285 }
286 if (!reply.ReadInt32(result)) {
287 ACCOUNT_LOGE("fail to read result");
288 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
289 }
290 return result;
291 }
292
AuthUser(int32_t userId,const std::vector<uint8_t> & password,const sptr<IDomainAuthCallback> & callback)293 ErrCode DomainAccountProxy::AuthUser(
294 int32_t userId, const std::vector<uint8_t> &password, const sptr<IDomainAuthCallback> &callback)
295 {
296 MessageParcel data;
297 if (!data.WriteInterfaceToken(GetDescriptor())) {
298 ACCOUNT_LOGE("fail to write descriptor");
299 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
300 }
301 if (!data.WriteInt32(userId)) {
302 ACCOUNT_LOGE("fail to write userId");
303 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
304 }
305 if (!data.WriteUInt8Vector(password)) {
306 ACCOUNT_LOGE("fail to write password");
307 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
308 }
309 if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
310 ACCOUNT_LOGE("fail to write callback");
311 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
312 }
313 MessageParcel reply;
314 ErrCode result = SendRequest(DomainAccountInterfaceCode::DOMAIN_AUTH_USER, data, reply);
315 if (result != ERR_OK) {
316 ACCOUNT_LOGE("fail to send request, error: %{public}d", result);
317 return result;
318 }
319 if (!reply.ReadInt32(result)) {
320 ACCOUNT_LOGE("fail to read result");
321 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
322 }
323 return result;
324 }
325
AuthWithPopup(int32_t userId,const sptr<IDomainAuthCallback> & callback)326 ErrCode DomainAccountProxy::AuthWithPopup(int32_t userId, const sptr<IDomainAuthCallback> &callback)
327 {
328 MessageParcel data;
329 if (!data.WriteInterfaceToken(GetDescriptor())) {
330 ACCOUNT_LOGE("fail to write descriptor");
331 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
332 }
333 if (!data.WriteInt32(userId)) {
334 ACCOUNT_LOGE("fail to write userId");
335 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
336 }
337 if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
338 ACCOUNT_LOGE("fail to write callback");
339 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
340 }
341 MessageParcel reply;
342 ErrCode result = SendRequest(DomainAccountInterfaceCode::DOMAIN_AUTH_WITH_POPUP, data, reply);
343 if (result != ERR_OK) {
344 ACCOUNT_LOGE("fail to send request, error: %{public}d", result);
345 return result;
346 }
347 if (!reply.ReadInt32(result)) {
348 ACCOUNT_LOGE("fail to read result");
349 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
350 }
351 return result;
352 }
353
UpdateAccountToken(const DomainAccountInfo & info,const std::vector<uint8_t> & token)354 ErrCode DomainAccountProxy::UpdateAccountToken(const DomainAccountInfo &info, const std::vector<uint8_t> &token)
355 {
356 MessageParcel data;
357 if (!data.WriteInterfaceToken(GetDescriptor())) {
358 ACCOUNT_LOGE("fail to write descriptor");
359 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
360 }
361 if (!data.WriteParcelable(&info)) {
362 ACCOUNT_LOGE("fail to write parcelable");
363 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
364 }
365 if (!data.WriteUInt8Vector(token)) {
366 ACCOUNT_LOGE("fail to write token");
367 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
368 }
369 MessageParcel reply;
370 ErrCode result = SendRequest(DomainAccountInterfaceCode::DOMAIN_UPDATE_ACCOUNT_TOKEN, data, reply);
371 if (result != ERR_OK) {
372 ACCOUNT_LOGE("fail to send request, error: %{public}d", result);
373 return result;
374 }
375 if (!reply.ReadInt32(result)) {
376 ACCOUNT_LOGE("fail to read result");
377 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
378 }
379 return result;
380 }
381
GetAccessToken(const DomainAccountInfo & info,const AAFwk::WantParams & parameters,const sptr<IDomainAccountCallback> & callback)382 ErrCode DomainAccountProxy::GetAccessToken(
383 const DomainAccountInfo &info, const AAFwk::WantParams ¶meters, const sptr<IDomainAccountCallback> &callback)
384 {
385 MessageParcel data;
386 if (!data.WriteInterfaceToken(GetDescriptor())) {
387 ACCOUNT_LOGE("fail to write descriptor");
388 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
389 }
390 if (!data.WriteParcelable(&info)) {
391 ACCOUNT_LOGE("fail to write info");
392 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
393 }
394 if (!data.WriteParcelable(¶meters)) {
395 ACCOUNT_LOGE("failed to write write parameters");
396 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
397 }
398 if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
399 ACCOUNT_LOGE("fail to write callback");
400 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
401 }
402 MessageParcel reply;
403 ErrCode result = SendRequest(DomainAccountInterfaceCode::DOMAIN_GET_ACCESS_TOKEN, data, reply);
404 if (result != ERR_OK) {
405 ACCOUNT_LOGE("fail to send request, error: %{public}d", result);
406 return result;
407 }
408 if (!reply.ReadInt32(result)) {
409 ACCOUNT_LOGE("fail to read result");
410 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
411 }
412 return result;
413 }
414
GetDomainAccountInfo(const DomainAccountInfo & info,const sptr<IDomainAccountCallback> & callback)415 ErrCode DomainAccountProxy::GetDomainAccountInfo(
416 const DomainAccountInfo &info, const sptr<IDomainAccountCallback> &callback)
417 {
418 MessageParcel data;
419 if (!data.WriteInterfaceToken(GetDescriptor())) {
420 ACCOUNT_LOGE("fail to write descriptor");
421 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
422 }
423 if (!data.WriteParcelable(&info)) {
424 ACCOUNT_LOGE("fail to write accountInfo");
425 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
426 }
427 if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
428 ACCOUNT_LOGE("fail to write callback");
429 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
430 }
431 MessageParcel reply;
432 ErrCode result = SendRequest(DomainAccountInterfaceCode::DOMAIN_GET_ACCOUNT_INFO, data, reply);
433 if (result != ERR_OK) {
434 ACCOUNT_LOGE("fail to send request, error: %{public}d", result);
435 return result;
436 }
437 if (!reply.ReadInt32(result)) {
438 ACCOUNT_LOGE("fail to read result");
439 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
440 }
441 return result;
442 }
443 } // namespace AccountSA
444 } // namespace OHOS