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