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
GetMessageOption(int32_t code,MessageOption & option)154 void InputDataChannelProxy::GetMessageOption(int32_t code, MessageOption &option)
155 {
156 switch (code) {
157 case SEND_KEYBOARD_STATUS:
158 case NOTIFY_KEYBOARD_HEIGHT:
159 case NOTIFY_PANEL_STATUS_INFO:
160 IMSA_HILOGD("Async IPC.");
161 option.SetFlags(MessageOption::TF_ASYNC);
162 break;
163
164 default:
165 option.SetFlags(MessageOption::TF_SYNC);
166 break;
167 }
168 }
169
SendRequest(int code,ParcelHandler input,ParcelHandler output,MessageOption option)170 int32_t InputDataChannelProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output, MessageOption option)
171 {
172 IMSA_HILOGD("InputDataChannelProxy run in, code = %{public}d", code);
173 MessageParcel data;
174 MessageParcel reply;
175 GetMessageOption(code, option);
176
177 if (!data.WriteInterfaceToken(GetDescriptor())) {
178 IMSA_HILOGE("write interface token failed!");
179 return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
180 }
181 if (input != nullptr && (!input(data))) {
182 IMSA_HILOGE("write data failed!");
183 return ErrorCode::ERROR_EX_PARCELABLE;
184 }
185 auto remote = Remote();
186 if (remote == nullptr) {
187 IMSA_HILOGE("remote is nullptr!");
188 return ErrorCode::ERROR_EX_NULL_POINTER;
189 }
190 auto ret = remote->SendRequest(code, data, reply, option);
191 if (ret != NO_ERROR) {
192 IMSA_HILOGE("send request failed, code: %{public}d ret %{public}d", code, ret);
193 return ret;
194 }
195 if (option.GetFlags() == MessageOption::TF_ASYNC) {
196 return ErrorCode::NO_ERROR;
197 }
198 ret = reply.ReadInt32();
199 if (ret != NO_ERROR) {
200 IMSA_HILOGE("reply error, ret: %{public}d", ret);
201 return ret;
202 }
203 if (output != nullptr && (!output(reply))) {
204 IMSA_HILOGE("reply parcel error!");
205 return ErrorCode::ERROR_EX_PARCELABLE;
206 }
207 return ret;
208 }
209 } // namespace MiscServices
210 } // namespace OHOS
211