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 "itypes_util.h"
22 #include "message_option.h"
23 #include "message_parcel.h"
24
25 namespace OHOS {
26 namespace MiscServices {
InputDataChannelProxy(const sptr<IRemoteObject> & object)27 InputDataChannelProxy::InputDataChannelProxy(const sptr<IRemoteObject> &object)
28 : IRemoteProxy<IInputDataChannel>(object)
29 {
30 }
31
InsertText(const std::u16string & text)32 int32_t InputDataChannelProxy::InsertText(const std::u16string &text)
33 {
34 IMSA_HILOGD("InputDataChannelProxy run in");
35 return SendRequest(
36 INSERT_TEXT, [&text](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, text); });
37 }
38
DeleteForward(int32_t length)39 int32_t InputDataChannelProxy::DeleteForward(int32_t length)
40 {
41 IMSA_HILOGD("InputDataChannelProxy run in");
42 return SendRequest(
43 DELETE_FORWARD, [length](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, length); });
44 }
45
DeleteBackward(int32_t length)46 int32_t InputDataChannelProxy::DeleteBackward(int32_t length)
47 {
48 IMSA_HILOGD("InputDataChannelProxy run in");
49 return SendRequest(
50 DELETE_BACKWARD, [length](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, length); });
51 }
52
GetTextBeforeCursor(int32_t number,std::u16string & text)53 int32_t InputDataChannelProxy::GetTextBeforeCursor(int32_t number, std::u16string &text)
54 {
55 IMSA_HILOGD("InputDataChannelProxy run in");
56 return SendRequest(
57 GET_TEXT_BEFORE_CURSOR, [number](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, number); },
58 [&text](MessageParcel &parcel) { return ITypesUtil::Unmarshal(parcel, text);});
59 }
60
GetTextAfterCursor(int32_t number,std::u16string & text)61 int32_t InputDataChannelProxy::GetTextAfterCursor(int32_t number, std::u16string &text)
62 {
63 IMSA_HILOGD("InputDataChannelProxy run in");
64 return SendRequest(
65 GET_TEXT_AFTER_CURSOR, [number](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, number); },
66 [&text](MessageParcel &parcel) { return ITypesUtil::Unmarshal(parcel, text);});
67 }
68
SendKeyboardStatus(int32_t status)69 void InputDataChannelProxy::SendKeyboardStatus(int32_t status)
70 {
71 IMSA_HILOGD("InputDataChannelProxy run in");
72 SendRequest(
73 SEND_KEYBOARD_STATUS, [status](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, status); });
74 }
75
SendFunctionKey(int32_t funcKey)76 int32_t InputDataChannelProxy::SendFunctionKey(int32_t funcKey)
77 {
78 IMSA_HILOGD("InputDataChannelProxy run in");
79 return SendRequest(
80 SEND_FUNCTION_KEY, [funcKey](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, funcKey); });
81 }
82
MoveCursor(int32_t keyCode)83 int32_t InputDataChannelProxy::MoveCursor(int32_t keyCode)
84 {
85 IMSA_HILOGD("InputDataChannelProxy run in");
86 return SendRequest(
87 MOVE_CURSOR, [keyCode](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, keyCode); });
88 }
89
GetEnterKeyType(int32_t & keyType)90 int32_t InputDataChannelProxy::GetEnterKeyType(int32_t &keyType)
91 {
92 IMSA_HILOGD("InputDataChannelProxy run in");
93 return SendRequest(
94 GET_ENTER_KEY_TYPE, nullptr,
95 [&keyType](MessageParcel &parcel) { return ITypesUtil::Unmarshal(parcel, keyType);});
96 }
97
GetInputPattern(int32_t & inputPattern)98 int32_t InputDataChannelProxy::GetInputPattern(int32_t &inputPattern)
99 {
100 IMSA_HILOGD("InputDataChannelProxy run in");
101 return SendRequest(
102 GET_INPUT_PATTERN, nullptr,
103 [&inputPattern](MessageParcel &parcel) { return ITypesUtil::Unmarshal(parcel, inputPattern);});
104 }
105
GetTextIndexAtCursor(int32_t & index)106 int32_t InputDataChannelProxy::GetTextIndexAtCursor(int32_t &index)
107 {
108 IMSA_HILOGD("InputDataChannelProxy run in");
109 return SendRequest(
110 GET_TEXT_INDEX_AT_CURSOR, nullptr,
111 [&index](MessageParcel &parcel) { return ITypesUtil::Unmarshal(parcel, index);});
112 }
113
GetTextConfig(TextTotalConfig & textConfig)114 int32_t InputDataChannelProxy::GetTextConfig(TextTotalConfig &textConfig)
115 {
116 IMSA_HILOGD("InputDataChannelProxy run in");
117 return SendRequest(GET_TEXT_CONFIG, nullptr, [&textConfig](MessageParcel &parcel) {
118 return ITypesUtil::Unmarshal(parcel, textConfig);
119 });
120 }
121
SelectByRange(int32_t start,int32_t end)122 int32_t InputDataChannelProxy::SelectByRange(int32_t start, int32_t end)
123 {
124 IMSA_HILOGD("InputDataChannelProxy run in");
125 return SendRequest(
126 SELECT_BY_RANGE, [start, end](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, start, end); });
127 }
128
SelectByMovement(int32_t direction,int32_t cursorMoveSkip)129 int32_t InputDataChannelProxy::SelectByMovement(int32_t direction, int32_t cursorMoveSkip)
130 {
131 IMSA_HILOGD("InputDataChannelProxy run in");
132 return SendRequest(SELECT_BY_MOVEMENT, [direction, cursorMoveSkip](MessageParcel &parcel) {
133 return ITypesUtil::Marshal(parcel, direction, cursorMoveSkip);
134 });
135 }
136
HandleExtendAction(int32_t action)137 int32_t InputDataChannelProxy::HandleExtendAction(int32_t action)
138 {
139 IMSA_HILOGD("InputDataChannelProxy run in");
140 return SendRequest(
141 HANDLE_EXTEND_ACTION, [action](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, action); });
142 }
143
SendRequest(int code,ParcelHandler input,ParcelHandler output)144 int32_t InputDataChannelProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
145 {
146 IMSA_HILOGD("InputDataChannelProxy run in");
147 MessageParcel data;
148 MessageParcel reply;
149 MessageOption option{ MessageOption::TF_SYNC };
150 if (!data.WriteInterfaceToken(GetDescriptor())) {
151 IMSA_HILOGE("write interface token failed");
152 return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
153 }
154 if (input != nullptr && (!input(data))) {
155 IMSA_HILOGE("write data failed");
156 return ErrorCode::ERROR_EX_PARCELABLE;
157 }
158 auto ret = Remote()->SendRequest(code, data, reply, option);
159 if (ret != NO_ERROR) {
160 IMSA_HILOGE("InputDataChannelProxy SendRequest failed, ret %{public}d", ret);
161 return ret;
162 }
163 ret = reply.ReadInt32();
164 if (ret != NO_ERROR) {
165 IMSA_HILOGE("reply error, ret %{public}d", ret);
166 return ret;
167 }
168 if (output != nullptr && (!output(reply))) {
169 IMSA_HILOGE("reply parcel error");
170 return ErrorCode::ERROR_EX_PARCELABLE;
171 }
172 return ret;
173 }
174 } // namespace MiscServices
175 } // namespace OHOS
176