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_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 {
InputDataChannelProxy(const sptr<IRemoteObject> & object)26 InputDataChannelProxy::InputDataChannelProxy(const sptr<IRemoteObject> &object)
27 : IRemoteProxy<IInputDataChannel>(object)
28 {
29 }
30
InsertText(const std::u16string & text)31 int32_t InputDataChannelProxy::InsertText(const std::u16string &text)
32 {
33 return SendRequest(INSERT_TEXT, [&text](MessageParcel &parcel) {
34 return ITypesUtil::Marshal(parcel, text);
35 });
36 }
37
DeleteForward(int32_t length)38 int32_t InputDataChannelProxy::DeleteForward(int32_t length)
39 {
40 return SendRequest(DELETE_FORWARD, [length](MessageParcel &parcel) {
41 return ITypesUtil::Marshal(parcel, length);
42 });
43 }
44
DeleteBackward(int32_t length)45 int32_t InputDataChannelProxy::DeleteBackward(int32_t length)
46 {
47 return SendRequest(DELETE_BACKWARD, [length](MessageParcel &parcel) {
48 return ITypesUtil::Marshal(parcel, length);
49 });
50 }
51
GetTextBeforeCursor(int32_t number,std::u16string & text)52 int32_t InputDataChannelProxy::GetTextBeforeCursor(int32_t number, std::u16string &text)
53 {
54 return SendRequest(
55 GET_TEXT_BEFORE_CURSOR,
56 [number](MessageParcel &parcel) {
57 return ITypesUtil::Marshal(parcel, number);
58 },
59 [&text](MessageParcel &parcel) {
60 return ITypesUtil::Unmarshal(parcel, text);
61 });
62 }
63
GetTextAfterCursor(int32_t number,std::u16string & text)64 int32_t InputDataChannelProxy::GetTextAfterCursor(int32_t number, std::u16string &text)
65 {
66 return SendRequest(
67 GET_TEXT_AFTER_CURSOR,
68 [number](MessageParcel &parcel) {
69 return ITypesUtil::Marshal(parcel, number);
70 },
71 [&text](MessageParcel &parcel) {
72 return ITypesUtil::Unmarshal(parcel, text);
73 });
74 }
75
SendKeyboardStatus(KeyboardStatus status)76 void InputDataChannelProxy::SendKeyboardStatus(KeyboardStatus status)
77 {
78 SendRequest(SEND_KEYBOARD_STATUS, [status](MessageParcel &parcel) {
79 return ITypesUtil::Marshal(parcel, static_cast<int32_t>(status));
80 });
81 }
82
NotifyPanelStatusInfo(const PanelStatusInfo & info)83 void InputDataChannelProxy::NotifyPanelStatusInfo(const PanelStatusInfo &info)
84 {
85 SendRequest(NOTIFY_PANEL_STATUS_INFO, [&info](MessageParcel &parcel) {
86 return ITypesUtil::Marshal(parcel, info);
87 });
88 }
89
SendFunctionKey(int32_t funcKey)90 int32_t InputDataChannelProxy::SendFunctionKey(int32_t funcKey)
91 {
92 return SendRequest(SEND_FUNCTION_KEY, [funcKey](MessageParcel &parcel) {
93 return ITypesUtil::Marshal(parcel, funcKey);
94 });
95 }
96
MoveCursor(int32_t keyCode)97 int32_t InputDataChannelProxy::MoveCursor(int32_t keyCode)
98 {
99 return SendRequest(MOVE_CURSOR, [keyCode](MessageParcel &parcel) {
100 return ITypesUtil::Marshal(parcel, keyCode);
101 });
102 }
103
GetEnterKeyType(int32_t & keyType)104 int32_t InputDataChannelProxy::GetEnterKeyType(int32_t &keyType)
105 {
106 return SendRequest(GET_ENTER_KEY_TYPE, nullptr, [&keyType](MessageParcel &parcel) {
107 return ITypesUtil::Unmarshal(parcel, keyType);
108 });
109 }
110
GetInputPattern(int32_t & inputPattern)111 int32_t InputDataChannelProxy::GetInputPattern(int32_t &inputPattern)
112 {
113 return SendRequest(GET_INPUT_PATTERN, nullptr, [&inputPattern](MessageParcel &parcel) {
114 return ITypesUtil::Unmarshal(parcel, inputPattern);
115 });
116 }
117
GetTextIndexAtCursor(int32_t & index)118 int32_t InputDataChannelProxy::GetTextIndexAtCursor(int32_t &index)
119 {
120 return SendRequest(GET_TEXT_INDEX_AT_CURSOR, nullptr, [&index](MessageParcel &parcel) {
121 return ITypesUtil::Unmarshal(parcel, index);
122 });
123 }
124
GetTextConfig(TextTotalConfig & textConfig)125 int32_t InputDataChannelProxy::GetTextConfig(TextTotalConfig &textConfig)
126 {
127 return SendRequest(GET_TEXT_CONFIG, nullptr, [&textConfig](MessageParcel &parcel) {
128 return ITypesUtil::Unmarshal(parcel, textConfig);
129 });
130 }
131
SelectByRange(int32_t start,int32_t end)132 int32_t InputDataChannelProxy::SelectByRange(int32_t start, int32_t end)
133 {
134 return SendRequest(SELECT_BY_RANGE, [start, end](MessageParcel &parcel) {
135 return ITypesUtil::Marshal(parcel, start, end);
136 });
137 }
138
SelectByMovement(int32_t direction,int32_t cursorMoveSkip)139 int32_t InputDataChannelProxy::SelectByMovement(int32_t direction, int32_t cursorMoveSkip)
140 {
141 return SendRequest(SELECT_BY_MOVEMENT, [direction, cursorMoveSkip](MessageParcel &parcel) {
142 return ITypesUtil::Marshal(parcel, direction, cursorMoveSkip);
143 });
144 }
145
HandleExtendAction(int32_t action)146 int32_t InputDataChannelProxy::HandleExtendAction(int32_t action)
147 {
148 return SendRequest(HANDLE_EXTEND_ACTION, [action](MessageParcel &parcel) {
149 return ITypesUtil::Marshal(parcel, action);
150 });
151 }
152
NotifyKeyboardHeight(uint32_t height)153 void InputDataChannelProxy::NotifyKeyboardHeight(uint32_t height)
154 {
155 SendRequest(NOTIFY_KEYBOARD_HEIGHT, [height](MessageParcel &parcel) {
156 return ITypesUtil::Marshal(parcel, height);
157 });
158 }
159
SendPrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)160 int32_t InputDataChannelProxy::SendPrivateCommand(
161 const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
162 {
163 return SendRequest(SEND_PRIVATE_COMMAND, [&privateCommand](MessageParcel &parcel) {
164 return ITypesUtil::Marshal(parcel, privateCommand);
165 });
166 }
167
SetPreviewText(const std::string & text,const Range & range)168 int32_t InputDataChannelProxy::SetPreviewText(const std::string &text, const Range &range)
169 {
170 return SendRequest(SET_PREVIEW_TEXT, [&text, &range](MessageParcel &parcel) {
171 return ITypesUtil::Marshal(parcel, text, range);
172 });
173 }
174
FinishTextPreview(bool isAsync)175 int32_t InputDataChannelProxy::FinishTextPreview(bool isAsync)
176 {
177 if (isAsync) {
178 return SendRequest(FINISH_TEXT_PREVIEW, nullptr, nullptr, MessageOption::TF_ASYNC);
179 } else {
180 return SendRequest(FINISH_TEXT_PREVIEW);
181 }
182 }
183
SendMessage(const ArrayBuffer & arraybuffer)184 int32_t InputDataChannelProxy::SendMessage(const ArrayBuffer &arraybuffer)
185 {
186 return SendRequest(SEND_MESSAGE, [&arraybuffer](MessageParcel &parcel) {
187 return ITypesUtil::Marshal(parcel, arraybuffer);
188 });
189 }
190
GetMessageOption(int32_t code,MessageOption & option)191 void InputDataChannelProxy::GetMessageOption(int32_t code, MessageOption &option)
192 {
193 switch (code) {
194 case SEND_KEYBOARD_STATUS:
195 case NOTIFY_KEYBOARD_HEIGHT:
196 case NOTIFY_PANEL_STATUS_INFO:
197 IMSA_HILOGD("Async IPC.");
198 option.SetFlags(MessageOption::TF_ASYNC);
199 break;
200
201 default:
202 option.SetFlags(MessageOption::TF_SYNC);
203 break;
204 }
205 }
206
SendRequest(int code,ParcelHandler input,ParcelHandler output,MessageOption option)207 int32_t InputDataChannelProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output, MessageOption option)
208 {
209 IMSA_HILOGD("InputDataChannelProxy run in, code = %{public}d", code);
210 MessageParcel data;
211 MessageParcel reply;
212 GetMessageOption(code, option);
213
214 if (!data.WriteInterfaceToken(GetDescriptor())) {
215 IMSA_HILOGE("write interface token failed!");
216 return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
217 }
218 if (input != nullptr && (!input(data))) {
219 IMSA_HILOGE("write data failed!");
220 return ErrorCode::ERROR_EX_PARCELABLE;
221 }
222 auto remote = Remote();
223 if (remote == nullptr) {
224 IMSA_HILOGE("remote is nullptr!");
225 return ErrorCode::ERROR_IPC_REMOTE_NULLPTR;
226 }
227 auto ret = remote->SendRequest(code, data, reply, option);
228 if (ret != NO_ERROR) {
229 IMSA_HILOGE("send request failed, code: %{public}d ret %{public}d", code, ret);
230 return ret;
231 }
232 if (option.GetFlags() == MessageOption::TF_ASYNC) {
233 return ErrorCode::NO_ERROR;
234 }
235 ret = reply.ReadInt32();
236 if (ret != NO_ERROR) {
237 IMSA_HILOGE("reply error, ret: %{public}d", ret);
238 return ret;
239 }
240 if (output != nullptr && (!output(reply))) {
241 IMSA_HILOGE("reply parcel error!");
242 return ErrorCode::ERROR_EX_PARCELABLE;
243 }
244 return ret;
245 }
246 } // namespace MiscServices
247 } // namespace OHOS