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_data_channel_stub.h"
17 
18 #include "global.h"
19 #include "input_method_controller.h"
20 #include "ipc_object_stub.h"
21 #include "ipc_skeleton.h"
22 #include "ipc_types.h"
23 #include "itypes_util.h"
24 #include "message.h"
25 namespace OHOS {
26 namespace MiscServices {
InputDataChannelStub()27 InputDataChannelStub::InputDataChannelStub() {}
28 
~InputDataChannelStub()29 InputDataChannelStub::~InputDataChannelStub() {}
30 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)31 int32_t InputDataChannelStub::OnRemoteRequest(
32     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
33 {
34     IMSA_HILOGD("InputDataChannelStub, code: %{public}u, callingPid: %{public}d, callingUid: %{public}d.", code,
35         IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
36     auto descriptorToken = data.ReadInterfaceToken();
37     if (descriptorToken != IInputDataChannel::GetDescriptor()) {
38         IMSA_HILOGE("descriptor error!");
39         return ErrorCode::ERROR_STATUS_UNKNOWN_TRANSACTION;
40     }
41     if (code < DATA_CHANNEL_CMD_BEGIN || code >= DATA_CHANNEL_CMD_END) {
42         IMSA_HILOGE("code error, code = %{public}u, callingPid: %{public}d, callingUid: %{public}d.", code,
43             IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
44         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
45     }
46     return (this->*HANDLERS[code])(data, reply);
47 }
48 
InsertTextOnRemote(MessageParcel & data,MessageParcel & reply)49 int32_t InputDataChannelStub::InsertTextOnRemote(MessageParcel &data, MessageParcel &reply)
50 {
51     std::u16string text;
52     if (!ITypesUtil::Unmarshal(data, text)) {
53         IMSA_HILOGE("failed to read message parcel!");
54         return ErrorCode::ERROR_EX_PARCELABLE;
55     }
56     return reply.WriteInt32(InsertText(text)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
57 }
58 
DeleteForwardOnRemote(MessageParcel & data,MessageParcel & reply)59 int32_t InputDataChannelStub::DeleteForwardOnRemote(MessageParcel &data, MessageParcel &reply)
60 {
61     int32_t length = 0;
62     if (!ITypesUtil::Unmarshal(data, length)) {
63         IMSA_HILOGE("failed to read message parcel!");
64         return ErrorCode::ERROR_EX_PARCELABLE;
65     }
66     return reply.WriteInt32(DeleteForward(length)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
67 }
68 
DeleteBackwardOnRemote(MessageParcel & data,MessageParcel & reply)69 int32_t InputDataChannelStub::DeleteBackwardOnRemote(MessageParcel &data, MessageParcel &reply)
70 {
71     int32_t length = 0;
72     if (!ITypesUtil::Unmarshal(data, length)) {
73         IMSA_HILOGE("failed to read message parcel!");
74         return ErrorCode::ERROR_EX_PARCELABLE;
75     }
76     return reply.WriteInt32(DeleteBackward(length)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
77 }
78 
GetTextBeforeCursorOnRemote(MessageParcel & data,MessageParcel & reply)79 int32_t InputDataChannelStub::GetTextBeforeCursorOnRemote(MessageParcel &data, MessageParcel &reply)
80 {
81     int32_t length = 0;
82     if (!ITypesUtil::Unmarshal(data, length)) {
83         IMSA_HILOGE("failed to unmarshal!");
84         return ErrorCode::ERROR_EX_PARCELABLE;
85     }
86     std::u16string text;
87     return ITypesUtil::Marshal(reply, GetTextBeforeCursor(length, text), text) ? ErrorCode::NO_ERROR
88                                                                                : ErrorCode::ERROR_EX_PARCELABLE;
89 }
90 
GetTextAfterCursorOnRemote(MessageParcel & data,MessageParcel & reply)91 int32_t InputDataChannelStub::GetTextAfterCursorOnRemote(MessageParcel &data, MessageParcel &reply)
92 {
93     int32_t length = 0;
94     if (!ITypesUtil::Unmarshal(data, length)) {
95         IMSA_HILOGE("failed to unmarshal!");
96         return ErrorCode::ERROR_EX_PARCELABLE;
97     }
98     std::u16string text;
99     return ITypesUtil::Marshal(reply, GetTextAfterCursor(length, text), text) ? ErrorCode::NO_ERROR
100                                                                               : ErrorCode::ERROR_EX_PARCELABLE;
101 }
102 
GetTextConfigOnRemote(MessageParcel & data,MessageParcel & reply)103 int32_t InputDataChannelStub::GetTextConfigOnRemote(MessageParcel &data, MessageParcel &reply)
104 {
105     TextTotalConfig config;
106     return ITypesUtil::Marshal(reply, GetTextConfig(config), config) ? ErrorCode::NO_ERROR
107                                                                      : ErrorCode::ERROR_EX_PARCELABLE;
108 }
109 
SendKeyboardStatusOnRemote(MessageParcel & data,MessageParcel & reply)110 int32_t InputDataChannelStub::SendKeyboardStatusOnRemote(MessageParcel &data, MessageParcel &reply)
111 {
112     int32_t status = -1;
113     if (!ITypesUtil::Unmarshal(data, status)) {
114         IMSA_HILOGE("failed to read message parcel!");
115         return ErrorCode::ERROR_EX_PARCELABLE;
116     }
117     SendKeyboardStatus(static_cast<KeyboardStatus>(status));
118     return reply.WriteInt32(ErrorCode::NO_ERROR) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
119 }
120 
SendFunctionKeyOnRemote(MessageParcel & data,MessageParcel & reply)121 int32_t InputDataChannelStub::SendFunctionKeyOnRemote(MessageParcel &data, MessageParcel &reply)
122 {
123     int32_t functionKey = 0;
124     if (!ITypesUtil::Unmarshal(data, functionKey)) {
125         IMSA_HILOGE("failed to read message parcel!");
126         return ErrorCode::ERROR_EX_PARCELABLE;
127     }
128     return reply.WriteInt32(SendFunctionKey(functionKey)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
129 }
130 
MoveCursorOnRemote(MessageParcel & data,MessageParcel & reply)131 int32_t InputDataChannelStub::MoveCursorOnRemote(MessageParcel &data, MessageParcel &reply)
132 {
133     int32_t direction = 0;
134     if (!ITypesUtil::Unmarshal(data, direction)) {
135         IMSA_HILOGE("failed to read message parcel!");
136         return ErrorCode::ERROR_EX_PARCELABLE;
137     }
138     return reply.WriteInt32(MoveCursor(direction)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
139 }
140 
GetEnterKeyTypeOnRemote(MessageParcel & data,MessageParcel & reply)141 int32_t InputDataChannelStub::GetEnterKeyTypeOnRemote(MessageParcel &data, MessageParcel &reply)
142 {
143     int32_t type = 0;
144     return ITypesUtil::Marshal(reply, GetEnterKeyType(type), type) ? ErrorCode::NO_ERROR
145                                                                    : ErrorCode::ERROR_EX_PARCELABLE;
146 }
147 
GetInputPatternOnRemote(MessageParcel & data,MessageParcel & reply)148 int32_t InputDataChannelStub::GetInputPatternOnRemote(MessageParcel &data, MessageParcel &reply)
149 {
150     int32_t pattern = 0;
151     return ITypesUtil::Marshal(reply, GetInputPattern(pattern), pattern) ? ErrorCode::NO_ERROR
152                                                                          : ErrorCode::ERROR_EX_PARCELABLE;
153 }
154 
SelectByRangeOnRemote(MessageParcel & data,MessageParcel & reply)155 int32_t InputDataChannelStub::SelectByRangeOnRemote(MessageParcel &data, MessageParcel &reply)
156 {
157     int32_t start = 0;
158     int32_t end = 0;
159     if (!ITypesUtil::Unmarshal(data, start, end)) {
160         IMSA_HILOGE("failed to read message parcel!");
161         return ErrorCode::ERROR_EX_PARCELABLE;
162     }
163     return reply.WriteInt32(SelectByRange(start, end)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
164 }
165 
SelectByMovementOnRemote(MessageParcel & data,MessageParcel & reply)166 int32_t InputDataChannelStub::SelectByMovementOnRemote(MessageParcel &data, MessageParcel &reply)
167 {
168     int32_t direction = 0;
169     int32_t skip = 0;
170     if (!ITypesUtil::Unmarshal(data, direction, skip)) {
171         IMSA_HILOGE("failed to read message parcel");
172         return ErrorCode::ERROR_EX_PARCELABLE;
173     }
174     return reply.WriteInt32(SelectByMovement(direction, skip)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
175 }
176 
HandleExtendActionOnRemote(MessageParcel & data,MessageParcel & reply)177 int32_t InputDataChannelStub::HandleExtendActionOnRemote(MessageParcel &data, MessageParcel &reply)
178 {
179     int32_t action = 0;
180     if (!ITypesUtil::Unmarshal(data, action)) {
181         IMSA_HILOGE("failed to read message parcel!");
182         return ErrorCode::ERROR_EX_PARCELABLE;
183     }
184     return reply.WriteInt32(HandleExtendAction(action)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
185 }
186 
GetTextIndexAtCursorOnRemote(MessageParcel & data,MessageParcel & reply)187 int32_t InputDataChannelStub::GetTextIndexAtCursorOnRemote(MessageParcel &data, MessageParcel &reply)
188 {
189     int32_t index = -1;
190     return ITypesUtil::Marshal(reply, GetTextIndexAtCursor(index), index) ? ErrorCode::NO_ERROR
191                                                                           : ErrorCode::ERROR_EX_PARCELABLE;
192 }
193 
NotifyPanelStatusInfoOnRemote(MessageParcel & data,MessageParcel & reply)194 int32_t InputDataChannelStub::NotifyPanelStatusInfoOnRemote(MessageParcel &data, MessageParcel &reply)
195 {
196     PanelStatusInfo info{};
197     if (!ITypesUtil::Unmarshal(data, info)) {
198         IMSA_HILOGE("failed to read message parcel!");
199         return ErrorCode::ERROR_EX_PARCELABLE;
200     }
201     NotifyPanelStatusInfo(info);
202     return reply.WriteInt32(ErrorCode::NO_ERROR) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
203 }
204 
NotifyKeyboardHeightOnRemote(MessageParcel & data,MessageParcel & reply)205 int32_t InputDataChannelStub::NotifyKeyboardHeightOnRemote(MessageParcel &data, MessageParcel &reply)
206 {
207     uint32_t height = 0;
208     if (!ITypesUtil::Unmarshal(data, height)) {
209         IMSA_HILOGE("failed to read message parcel!");
210         return ErrorCode::ERROR_EX_PARCELABLE;
211     }
212     NotifyKeyboardHeight(height);
213     return reply.WriteInt32(ErrorCode::NO_ERROR) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
214 }
215 
SendPrivateCommandOnRemote(MessageParcel & data,MessageParcel & reply)216 int32_t InputDataChannelStub::SendPrivateCommandOnRemote(MessageParcel &data, MessageParcel &reply)
217 {
218     std::unordered_map<std::string, PrivateDataValue> privateCommand;
219     if (!ITypesUtil::Unmarshal(data, privateCommand)) {
220         IMSA_HILOGE("failed to read message parcel!");
221         return ErrorCode::ERROR_EX_PARCELABLE;
222     }
223     return reply.WriteInt32(SendPrivateCommand(privateCommand)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
224 }
225 
SetPreviewTextOnRemote(MessageParcel & data,MessageParcel & reply)226 int32_t InputDataChannelStub::SetPreviewTextOnRemote(MessageParcel &data, MessageParcel &reply)
227 {
228     std::string text;
229     Range range;
230     if (!ITypesUtil::Unmarshal(data, text, range)) {
231         IMSA_HILOGE("failed to read message parcel!");
232         return ErrorCode::ERROR_EX_PARCELABLE;
233     }
234     return reply.WriteInt32(SetPreviewText(text, range)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
235 }
236 
FinishTextPreviewOnRemote(MessageParcel & data,MessageParcel & reply)237 int32_t InputDataChannelStub::FinishTextPreviewOnRemote(MessageParcel &data, MessageParcel &reply)
238 {
239     bool isAsync = false;
240     return reply.WriteInt32(FinishTextPreview(isAsync)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
241 }
242 
InsertText(const std::u16string & text)243 int32_t InputDataChannelStub::InsertText(const std::u16string &text)
244 {
245     return InputMethodController::GetInstance()->InsertText(text);
246 }
247 
DeleteForward(int32_t length)248 int32_t InputDataChannelStub::DeleteForward(int32_t length)
249 {
250     return InputMethodController::GetInstance()->DeleteForward(length);
251 }
252 
DeleteBackward(int32_t length)253 int32_t InputDataChannelStub::DeleteBackward(int32_t length)
254 {
255     return InputMethodController::GetInstance()->DeleteBackward(length);
256 }
257 
GetTextBeforeCursor(int32_t number,std::u16string & text)258 int32_t InputDataChannelStub::GetTextBeforeCursor(int32_t number, std::u16string &text)
259 {
260     return InputMethodController::GetInstance()->GetLeft(number, text);
261 }
262 
GetTextAfterCursor(int32_t number,std::u16string & text)263 int32_t InputDataChannelStub::GetTextAfterCursor(int32_t number, std::u16string &text)
264 {
265     return InputMethodController::GetInstance()->GetRight(number, text);
266 }
267 
GetTextIndexAtCursor(int32_t & index)268 int32_t InputDataChannelStub::GetTextIndexAtCursor(int32_t &index)
269 {
270     return InputMethodController::GetInstance()->GetTextIndexAtCursor(index);
271 }
272 
GetEnterKeyType(int32_t & keyType)273 int32_t InputDataChannelStub::GetEnterKeyType(int32_t &keyType)
274 {
275     return InputMethodController::GetInstance()->GetEnterKeyType(keyType);
276 }
277 
GetInputPattern(int32_t & inputPattern)278 int32_t InputDataChannelStub::GetInputPattern(int32_t &inputPattern)
279 {
280     return InputMethodController::GetInstance()->GetInputPattern(inputPattern);
281 }
282 
GetTextConfig(TextTotalConfig & textConfig)283 int32_t InputDataChannelStub::GetTextConfig(TextTotalConfig &textConfig)
284 {
285     return InputMethodController::GetInstance()->GetTextConfig(textConfig);
286 }
287 
SendKeyboardStatus(KeyboardStatus status)288 void InputDataChannelStub::SendKeyboardStatus(KeyboardStatus status)
289 {
290     InputMethodController::GetInstance()->SendKeyboardStatus(status);
291 }
292 
SendFunctionKey(int32_t funcKey)293 int32_t InputDataChannelStub::SendFunctionKey(int32_t funcKey)
294 {
295     return InputMethodController::GetInstance()->SendFunctionKey(funcKey);
296 }
297 
MoveCursor(int32_t keyCode)298 int32_t InputDataChannelStub::MoveCursor(int32_t keyCode)
299 {
300     return InputMethodController::GetInstance()->MoveCursor(static_cast<Direction>(keyCode));
301 }
302 
SelectByRange(int32_t start,int32_t end)303 int32_t InputDataChannelStub::SelectByRange(int32_t start, int32_t end)
304 {
305     InputMethodController::GetInstance()->SelectByRange(start, end);
306     return ErrorCode::NO_ERROR;
307 }
308 
SelectByMovement(int32_t direction,int32_t cursorMoveSkip)309 int32_t InputDataChannelStub::SelectByMovement(int32_t direction, int32_t cursorMoveSkip)
310 {
311     InputMethodController::GetInstance()->SelectByMovement(direction, cursorMoveSkip);
312     return ErrorCode::NO_ERROR;
313 }
314 
HandleExtendAction(int32_t action)315 int32_t InputDataChannelStub::HandleExtendAction(int32_t action)
316 {
317     return InputMethodController::GetInstance()->HandleExtendAction(action);
318 }
319 
NotifyPanelStatusInfo(const PanelStatusInfo & info)320 void InputDataChannelStub::NotifyPanelStatusInfo(const PanelStatusInfo &info)
321 {
322     InputMethodController::GetInstance()->NotifyPanelStatusInfo(info);
323 }
324 
NotifyKeyboardHeight(uint32_t height)325 void InputDataChannelStub::NotifyKeyboardHeight(uint32_t height)
326 {
327     InputMethodController::GetInstance()->NotifyKeyboardHeight(height);
328 }
329 
SendPrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)330 int32_t InputDataChannelStub::SendPrivateCommand(
331     const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
332 {
333     return InputMethodController::GetInstance()->ReceivePrivateCommand(privateCommand);
334 }
335 
SetPreviewText(const std::string & text,const Range & range)336 int32_t InputDataChannelStub::SetPreviewText(const std::string &text, const Range &range)
337 {
338     return InputMethodController::GetInstance()->SetPreviewText(text, range);
339 }
340 
FinishTextPreview(bool isAsync)341 int32_t InputDataChannelStub::FinishTextPreview(bool isAsync)
342 {
343     return InputMethodController::GetInstance()->FinishTextPreview();
344 }
345 } // namespace MiscServices
346 } // namespace OHOS
347