1 /*
2  * Copyright (C) 2021 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 "keyevent_consumer_proxy.h"
17 
18 #include "global.h"
19 #include "ipc_types.h"
20 #include "itypes_util.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 
24 namespace OHOS {
25 namespace MiscServices {
KeyEventConsumerProxy(const sptr<IRemoteObject> & object)26 KeyEventConsumerProxy::KeyEventConsumerProxy(const sptr<IRemoteObject> &object)
27     : IRemoteProxy<IKeyEventConsumer>(object)
28 {
29 }
30 
OnKeyEventResult(bool isConsumed)31 int32_t KeyEventConsumerProxy::OnKeyEventResult(bool isConsumed)
32 {
33     return SendRequest(
34         KEY_EVENT_RESULT, [isConsumed](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, isConsumed); },
35         nullptr, MessageOption::TF_ASYNC);
36 }
37 
OnKeyEventConsumeResult(bool isConsumed)38 void KeyEventConsumerProxy::OnKeyEventConsumeResult(bool isConsumed)
39 {
40     IMSA_HILOGI("result: %{public}d.", isConsumed);
41     keyEventConsume_ = true;
42     keyEventResult_ = isConsumed;
43     if (keyEventConsume_ && keyCodeConsume_) {
44         OnKeyEventResult(keyCodeResult_ || keyEventResult_);
45     }
46 }
47 
OnKeyCodeConsumeResult(bool isConsumed)48 void KeyEventConsumerProxy::OnKeyCodeConsumeResult(bool isConsumed)
49 {
50     IMSA_HILOGI("result: %{public}d.", isConsumed);
51     keyCodeConsume_ = true;
52     keyCodeResult_ = isConsumed;
53     if (keyEventConsume_ && keyCodeConsume_) {
54         OnKeyEventResult(keyCodeResult_ || keyEventResult_);
55     }
56 }
57 
SendRequest(int code,ParcelHandler input,ParcelHandler output,MessageOption option)58 int32_t KeyEventConsumerProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output, MessageOption option)
59 {
60     IMSA_HILOGD("KeyEventConsumerProxy run in, code = %{public}d.", code);
61     MessageParcel data;
62     MessageParcel reply;
63 
64     if (!data.WriteInterfaceToken(GetDescriptor())) {
65         IMSA_HILOGE("write interface token failed!");
66         return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
67     }
68     if (input != nullptr && (!input(data))) {
69         IMSA_HILOGE("write data failed!");
70         return ErrorCode::ERROR_EX_PARCELABLE;
71     }
72     auto remote = Remote();
73     if (remote == nullptr) {
74         IMSA_HILOGE("KeyEventConsumerProxy remote is nullptr!");
75         return ErrorCode::ERROR_EX_NULL_POINTER;
76     }
77     auto ret = remote->SendRequest(code, data, reply, option);
78     if (ret != NO_ERROR) {
79         IMSA_HILOGE("send request failed, code: %{public}d ret %{public}d!", code, ret);
80         return ret;
81     }
82     if (option.GetFlags() == MessageOption::TF_ASYNC) {
83         return ErrorCode::NO_ERROR;
84     }
85     ret = reply.ReadInt32();
86     if (ret != NO_ERROR) {
87         IMSA_HILOGE("reply error, ret: %{public}d", ret);
88         return ret;
89     }
90     if (output != nullptr && (!output(reply))) {
91         IMSA_HILOGE("reply parcel error!");
92         return ErrorCode::ERROR_EX_PARCELABLE;
93     }
94     return ret;
95 }
96 } // namespace MiscServices
97 } // namespace OHOS
98