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_callback_stub.h"
17 
18 #include <securec.h>
19 #include "account_log_wrapper.h"
20 #include "app_account_common.h"
21 #include "ipc_skeleton.h"
22 #include "want.h"
23 
24 namespace OHOS {
25 namespace AccountSA {
26 
AppAccountAuthorizationExtensionCallbackStub()27 AppAccountAuthorizationExtensionCallbackStub::AppAccountAuthorizationExtensionCallbackStub()
28 {}
29 
~AppAccountAuthorizationExtensionCallbackStub()30 AppAccountAuthorizationExtensionCallbackStub::~AppAccountAuthorizationExtensionCallbackStub()
31 {}
32 
OnRemoteRequest(std::uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t AppAccountAuthorizationExtensionCallbackStub::OnRemoteRequest(
34     std::uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
35 {
36     if (data.ReadInterfaceToken() != GetDescriptor()) {
37         ACCOUNT_LOGE("check descriptor failed!");
38         return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
39     }
40     switch (code) {
41         case static_cast<uint32_t>(AppAccountAuthorizationExtensionCallbackInterfaceCode::ON_RESULT):
42             return ProcOnResult(data, reply);
43         case static_cast<uint32_t>(AppAccountAuthorizationExtensionCallbackInterfaceCode::ON_REQUEST_REDIRECTED):
44             return ProcOnRequestRedirected(data, reply);
45         default:
46             break;
47     }
48     ACCOUNT_LOGW("remote request unhandled: %{public}d", code);
49     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
50 }
51 
ProcOnResult(MessageParcel & data,MessageParcel & reply)52 int32_t AppAccountAuthorizationExtensionCallbackStub::ProcOnResult(MessageParcel &data, MessageParcel &reply)
53 {
54     AsyncCallbackError businessError;
55     if (!data.ReadInt32(businessError.code)) {
56         ACCOUNT_LOGE("failed to read code");
57         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
58     }
59     if (!data.ReadString(businessError.message)) {
60         ACCOUNT_LOGE("failed to read message");
61         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
62     }
63     std::shared_ptr<AAFwk::WantParams> errParameters(data.ReadParcelable<AAFwk::WantParams>());
64     if (errParameters == nullptr) {
65         ACCOUNT_LOGE("failed to read errParameters");
66         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
67     }
68     businessError.data = *(errParameters);
69     std::shared_ptr<AAFwk::WantParams> parameters(data.ReadParcelable<AAFwk::WantParams>());
70     if (parameters == nullptr) {
71         ACCOUNT_LOGE("failed to read extension parameters");
72         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
73     }
74     OnResult(businessError, (*parameters));
75     return ERR_NONE;
76 }
77 
ProcOnRequestRedirected(MessageParcel & data,MessageParcel & reply)78 int32_t AppAccountAuthorizationExtensionCallbackStub::ProcOnRequestRedirected(MessageParcel &data, MessageParcel &reply)
79 {
80     std::shared_ptr<AAFwk::Want> requestPtr(data.ReadParcelable<AAFwk::Want>());
81     if (requestPtr == nullptr) {
82         ACCOUNT_LOGE("failed to read extension requestPtr");
83         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
84     }
85     OnRequestRedirected(*requestPtr);
86     return ERR_NONE;
87 }
88 }  // namespace AccountSA
89 }  // namespace OHOS
90