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 "app_account_authenticator_callback_proxy.h"
17 
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace AccountSA {
AppAccountAuthenticatorCallbackProxy(const sptr<IRemoteObject> & object)23 AppAccountAuthenticatorCallbackProxy::AppAccountAuthenticatorCallbackProxy(const sptr<IRemoteObject> &object)
24     : IRemoteProxy<IAppAccountAuthenticatorCallback>(object)
25 {}
26 
~AppAccountAuthenticatorCallbackProxy()27 AppAccountAuthenticatorCallbackProxy::~AppAccountAuthenticatorCallbackProxy()
28 {}
29 
OnResult(int32_t resultCode,const AAFwk::Want & result)30 void AppAccountAuthenticatorCallbackProxy::OnResult(int32_t resultCode, const AAFwk::Want &result)
31 {
32     MessageParcel data;
33     MessageParcel reply;
34 
35     if (!data.WriteInterfaceToken(GetDescriptor())) {
36         ACCOUNT_LOGE("failed to write descriptor!");
37         return;
38     }
39 
40     if (!data.WriteInt32(resultCode)) {
41         ACCOUNT_LOGE("failed to write resultCode %{public}d.", resultCode);
42         return;
43     }
44     if (!data.WriteParcelable(&result)) {
45         ACCOUNT_LOGE("failed to write result");
46         return;
47     }
48     SendRequest(AppAccountAuthenticatorCallbackInterfaceCode::ACCOUNT_RESULT, data, reply);
49 }
50 
OnRequestRedirected(AAFwk::Want & request)51 void AppAccountAuthenticatorCallbackProxy::OnRequestRedirected(AAFwk::Want &request)
52 {
53     MessageParcel data;
54     MessageParcel reply;
55 
56     if (!data.WriteInterfaceToken(GetDescriptor())) {
57         ACCOUNT_LOGE("failed to write descriptor!");
58         return;
59     }
60 
61     if (!data.WriteParcelable(&request)) {
62         ACCOUNT_LOGE("failed to write request");
63         return;
64     }
65     SendRequest(AppAccountAuthenticatorCallbackInterfaceCode::ACCOUNT_REQUEST_REDIRECTED, data, reply);
66 }
67 
OnRequestContinued()68 void AppAccountAuthenticatorCallbackProxy::OnRequestContinued()
69 {
70     MessageParcel data;
71     MessageParcel reply;
72 
73     if (!data.WriteInterfaceToken(GetDescriptor())) {
74         ACCOUNT_LOGE("failed to write descriptor!");
75         return;
76     }
77     SendRequest(AppAccountAuthenticatorCallbackInterfaceCode::ACCOUNT_REQUEST_CONTINUED, data, reply);
78 }
79 
SendRequest(AppAccountAuthenticatorCallbackInterfaceCode code,MessageParcel & data,MessageParcel & reply)80 ErrCode AppAccountAuthenticatorCallbackProxy::SendRequest(
81     AppAccountAuthenticatorCallbackInterfaceCode code, MessageParcel &data, MessageParcel &reply)
82 {
83     sptr<IRemoteObject> remoteCallback = Remote();
84     if (remoteCallback == nullptr) {
85         ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
86         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
87     }
88     MessageOption option(MessageOption::TF_SYNC);
89     int32_t result = remoteCallback->SendRequest(static_cast<uint32_t>(code), data, reply, option);
90     if (result != ERR_OK) {
91         ACCOUNT_LOGE("failed to SendRequest, code = %{public}d, result = %{public}d", code, result);
92         return ERR_APPACCOUNT_KIT_SEND_REQUEST;
93     }
94     return ERR_OK;
95 }
96 }  // namespace AccountSA
97 }  // namespace OHOS
98