1 /*
2  * Copyright (c) 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 "app_account_authorization_extension_proxy.h"
17 
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace AccountSA {
AppAccountAuthorizationExtensionProxy(const sptr<IRemoteObject> & object)23 AppAccountAuthorizationExtensionProxy::AppAccountAuthorizationExtensionProxy(const sptr<IRemoteObject> &object)
24     : IRemoteProxy<IAppAccountAuthorizationExtension>(object)
25 {}
26 
~AppAccountAuthorizationExtensionProxy()27 AppAccountAuthorizationExtensionProxy::~AppAccountAuthorizationExtensionProxy()
28 {}
29 
SendRequest(AppAccountAuthorizationExtensionInterfaceCode code,MessageParcel & data,MessageParcel & reply)30 ErrCode AppAccountAuthorizationExtensionProxy::SendRequest(
31     AppAccountAuthorizationExtensionInterfaceCode code, MessageParcel &data, MessageParcel &reply)
32 {
33     sptr<IRemoteObject> remote = Remote();
34     if (remote == nullptr) {
35         ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
36         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
37     }
38     MessageOption option(MessageOption::TF_SYNC);
39     ErrCode result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
40     if (result != ERR_OK) {
41         ACCOUNT_LOGE("fail to send request, result: %{public}d", result);
42         return result;
43     }
44     if (!reply.ReadInt32(result)) {
45         ACCOUNT_LOGE("fail to read result");
46         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
47     }
48     return result;
49 }
50 
WriteRequest(MessageParcel & data,const AuthorizationRequest & request)51 static ErrCode WriteRequest(MessageParcel &data, const AuthorizationRequest &request)
52 {
53     if (!data.WriteInt32(request.isEnableContext)) {
54         ACCOUNT_LOGE("failed to write request isEnableContext");
55         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
56     }
57     if (!data.WriteInt32(request.callerUid)) {
58         ACCOUNT_LOGE("failed to write request callerUid");
59         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
60     }
61     if (!data.WriteParcelable(&request.parameters)) {
62         ACCOUNT_LOGE("fail to write write request parameters");
63         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
64     }
65     if ((request.callback == nullptr) || (!data.WriteRemoteObject(request.callback->AsObject()))) {
66         ACCOUNT_LOGE("fail to write request callback");
67         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
68     }
69     return ERR_OK;
70 }
71 
StartAuthorization(const AuthorizationRequest & request)72 ErrCode AppAccountAuthorizationExtensionProxy::StartAuthorization(const AuthorizationRequest &request)
73 {
74     MessageParcel data;
75     if (!data.WriteInterfaceToken(GetDescriptor())) {
76         ACCOUNT_LOGE("fail to write descriptor");
77         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
78     }
79     ErrCode result = WriteRequest(data, request);
80     if (result != ERR_OK) {
81         return result;
82     }
83     MessageParcel reply;
84     return SendRequest(
85         AppAccountAuthorizationExtensionInterfaceCode::START_AUTHENTICATION, data, reply);
86 }
87 }  // namespace AccountSA
88 }  // namespace OHOS
89