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 "input_client_stub.h"
17
18 #include "global.h"
19 #include "input_method_controller.h"
20 #include "ipc_object_stub.h"
21 #include "ipc_types.h"
22 #include "ipc_skeleton.h"
23 #include "itypes_util.h"
24 #include "message.h"
25
26 namespace OHOS {
27 namespace MiscServices {
InputClientStub()28 InputClientStub::InputClientStub()
29 {
30 }
31
~InputClientStub()32 InputClientStub::~InputClientStub()
33 {
34 }
35
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int32_t InputClientStub::OnRemoteRequest(
37 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
38 {
39 IMSA_HILOGI("code = %{public}u, callingPid:%{public}d, callingUid:%{public}d", code, IPCSkeleton::GetCallingPid(),
40 IPCSkeleton::GetCallingUid());
41 auto descriptorToken = data.ReadInterfaceToken();
42 if (descriptorToken != GetDescriptor()) {
43 return ErrorCode::ERROR_STATUS_UNKNOWN_TRANSACTION;
44 }
45 switch (code) {
46 case ON_INPUT_READY: {
47 OnInputReadyOnRemote(data, reply);
48 break;
49 }
50 case ON_INPUT_STOP: {
51 OnInputStopOnRemote(data, reply);
52 break;
53 }
54 case ON_SWITCH_INPUT: {
55 OnSwitchInputOnRemote(data, reply);
56 break;
57 }
58 case ON_PANEL_STATUS_CHANGE: {
59 OnPanelStatusChangeOnRemote(data, reply);
60 break;
61 }
62 default:
63 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
64 }
65 return NO_ERROR;
66 }
67
OnInputReadyOnRemote(MessageParcel & data,MessageParcel & reply)68 void InputClientStub::OnInputReadyOnRemote(MessageParcel &data, MessageParcel &reply)
69 {
70 auto object = data.ReadRemoteObject();
71 InputMethodController::GetInstance()->OnInputReady(object);
72 }
73
OnInputStopOnRemote(MessageParcel & data,MessageParcel & reply)74 void InputClientStub::OnInputStopOnRemote(MessageParcel &data, MessageParcel &reply)
75 {
76 int32_t ret = SendMessage(MessageID::MSG_ID_ON_INPUT_STOP);
77 if (!ITypesUtil::Marshal(reply, ret)) {
78 IMSA_HILOGE("failed to write reply");
79 }
80 }
81
OnSwitchInputOnRemote(MessageParcel & data,MessageParcel & reply)82 void InputClientStub::OnSwitchInputOnRemote(MessageParcel &data, MessageParcel &reply)
83 {
84 IMSA_HILOGI("InputClientStub::OnSwitchInputOnRemote");
85 if (msgHandler == nullptr) {
86 IMSA_HILOGE("InputClientStub::msgHandler is nullptr");
87 return;
88 }
89 auto *parcel = new (std::nothrow) MessageParcel();
90 if (parcel == nullptr) {
91 IMSA_HILOGE("parcel is nullptr");
92 reply.WriteInt32(ErrorCode::ERROR_EX_NULL_POINTER);
93 return;
94 }
95 Property property;
96 SubProperty subProperty;
97 if (!ITypesUtil::Unmarshal(data, property, subProperty)) {
98 IMSA_HILOGE("read message parcel failed");
99 reply.WriteInt32(ErrorCode::ERROR_EX_PARCELABLE);
100 delete parcel;
101 return;
102 }
103 if (!ITypesUtil::Marshal(*parcel, property, subProperty)) {
104 IMSA_HILOGE("write message parcel failed");
105 reply.WriteInt32(ErrorCode::ERROR_EX_PARCELABLE);
106 delete parcel;
107 return;
108 }
109 auto *msg = new (std::nothrow) Message(MessageID::MSG_ID_ON_SWITCH_INPUT, parcel);
110 if (msg == nullptr) {
111 IMSA_HILOGE("msg is nullptr");
112 delete parcel;
113 reply.WriteInt32(ErrorCode::ERROR_EX_NULL_POINTER);
114 return;
115 }
116 msgHandler->SendMessage(msg);
117 reply.WriteInt32(ErrorCode::NO_ERROR);
118 }
119
OnPanelStatusChangeOnRemote(MessageParcel & data,MessageParcel & reply)120 void InputClientStub::OnPanelStatusChangeOnRemote(MessageParcel &data, MessageParcel &reply)
121 {
122 IMSA_HILOGD("InputClientStub::OnPanelStatusChangeOnRemote");
123 if (msgHandler == nullptr) {
124 IMSA_HILOGE("InputClientStub::msgHandler is nullptr");
125 return;
126 }
127 auto *parcel = new (std::nothrow) MessageParcel();
128 if (parcel == nullptr) {
129 IMSA_HILOGE("parcel is nullptr");
130 reply.WriteInt32(ErrorCode::ERROR_EX_NULL_POINTER);
131 return;
132 }
133 uint32_t status;
134 std::vector<InputWindowInfo> windowInfo;
135 if (!ITypesUtil::Unmarshal(data, status, windowInfo)) {
136 IMSA_HILOGE("read message parcel failed");
137 reply.WriteInt32(ErrorCode::ERROR_EX_PARCELABLE);
138 delete parcel;
139 return;
140 }
141 if (!ITypesUtil::Marshal(*parcel, status, windowInfo)) {
142 IMSA_HILOGE("write message parcel failed");
143 reply.WriteInt32(ErrorCode::ERROR_EX_PARCELABLE);
144 delete parcel;
145 return;
146 }
147 auto *msg = new (std::nothrow) Message(MessageID::MSG_ID_ON_PANEL_STATUS_CHANGE, parcel);
148 if (msg == nullptr) {
149 IMSA_HILOGE("msg is nullptr");
150 delete parcel;
151 reply.WriteInt32(ErrorCode::ERROR_EX_NULL_POINTER);
152 return;
153 }
154 msgHandler->SendMessage(msg);
155 reply.WriteInt32(ErrorCode::NO_ERROR);
156 }
157
OnInputReady(const sptr<IInputMethodAgent> & agent)158 int32_t InputClientStub::OnInputReady(const sptr<IInputMethodAgent> &agent)
159 {
160 return ErrorCode::NO_ERROR;
161 }
162
OnInputStop()163 int32_t InputClientStub::OnInputStop()
164 {
165 return ErrorCode::NO_ERROR;
166 }
167
SetHandler(MessageHandler * handler)168 void InputClientStub::SetHandler(MessageHandler *handler)
169 {
170 msgHandler = handler;
171 }
172
OnSwitchInput(const Property & property,const SubProperty & subProperty)173 int32_t InputClientStub::OnSwitchInput(const Property &property, const SubProperty &subProperty)
174 {
175 return ErrorCode::NO_ERROR;
176 }
177
OnPanelStatusChange(const InputWindowStatus & status,const std::vector<InputWindowInfo> & windowInfo)178 int32_t InputClientStub::OnPanelStatusChange(
179 const InputWindowStatus &status, const std::vector<InputWindowInfo> &windowInfo)
180 {
181 return ErrorCode::NO_ERROR;
182 }
183
SendMessage(int code,ParcelHandler input)184 int32_t InputClientStub::SendMessage(int code, ParcelHandler input)
185 {
186 IMSA_HILOGD("InputClientStub run in");
187 if (msgHandler == nullptr) {
188 IMSA_HILOGE("msgHandler_ is nullptr");
189 return ErrorCode::ERROR_EX_NULL_POINTER;
190 }
191 auto *parcel = new (std::nothrow) MessageParcel();
192 if (parcel == nullptr) {
193 IMSA_HILOGE("parcel is nullptr");
194 return ErrorCode::ERROR_EX_NULL_POINTER;
195 }
196 if (input != nullptr && (!input(*parcel))) {
197 IMSA_HILOGE("write data failed");
198 delete parcel;
199 return ErrorCode::ERROR_EX_PARCELABLE;
200 }
201 auto *msg = new (std::nothrow) Message(code, parcel);
202 if (msg == nullptr) {
203 IMSA_HILOGE("msg is nullptr");
204 delete parcel;
205 return ErrorCode::ERROR_EX_NULL_POINTER;
206 }
207 msgHandler->SendMessage(msg);
208 return ErrorCode::NO_ERROR;
209 }
210 } // namespace MiscServices
211 } // namespace OHOS
212