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_types.h" 22 #include "message.h" 23 24 namespace OHOS { 25 namespace MiscServices { 26 constexpr int32_t WAIT_TIME_STUB = 110; InputDataChannelStub()27 InputDataChannelStub::InputDataChannelStub() : msgHandler(nullptr) 28 { 29 } 30 ~InputDataChannelStub()31 InputDataChannelStub::~InputDataChannelStub() 32 { 33 } 34 OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int32_t InputDataChannelStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, 36 MessageOption &option) 37 { 38 IMSA_HILOGI("InputDataChannelStub::OnRemoteRequest code = %{public}d", code); 39 auto descriptorToken = data.ReadInterfaceToken(); 40 if (descriptorToken != GetDescriptor()) { 41 return ErrorCode::ERROR_STATUS_UNKNOWN_TRANSACTION; 42 } 43 switch (code) { 44 case INSERT_TEXT: { 45 auto text = data.ReadString16(); 46 reply.WriteInt32(InsertText(text)); 47 break; 48 } 49 case DELETE_FORWARD: { 50 auto length = data.ReadInt32(); 51 reply.WriteInt32(DeleteForward(length)); 52 break; 53 } 54 case DELETE_BACKWARD: { 55 auto length = data.ReadInt32(); 56 DeleteBackward(length); 57 break; 58 } 59 case GET_TEXT_BEFORE_CURSOR: { 60 auto number = data.ReadInt32(); 61 std::u16string text; 62 reply.WriteInt32(HandleGetOperation(number, text, GET_TEXT_BEFORE_CURSOR)); 63 reply.WriteString16(text); 64 break; 65 } 66 case GET_TEXT_AFTER_CURSOR: { 67 auto number = data.ReadInt32(); 68 std::u16string text; 69 reply.WriteInt32(HandleGetOperation(number, text, GET_TEXT_AFTER_CURSOR)); 70 reply.WriteString16(text); 71 break; 72 } 73 case SEND_KEYBOARD_STATUS: { 74 auto status = data.ReadInt32(); 75 SendKeyboardStatus(status); 76 break; 77 } 78 case SEND_FUNCTION_KEY: { 79 auto funcKey = data.ReadInt32(); 80 reply.WriteInt32(SendFunctionKey(funcKey)); 81 break; 82 } 83 case MOVE_CURSOR: { 84 auto keyCode = data.ReadInt32(); 85 reply.WriteInt32(MoveCursor(keyCode)); 86 break; 87 } 88 case GET_ENTER_KEY_TYPE: { 89 int32_t keyType = 0; 90 reply.WriteInt32(GetEnterKeyType(keyType)); 91 reply.WriteInt32(keyType); 92 break; 93 } 94 case GET_INPUT_PATTERN: { 95 int32_t inputPattern = 0; 96 reply.WriteInt32(GetInputPattern(inputPattern)); 97 reply.WriteInt32(inputPattern); 98 break; 99 } 100 case HANDLE_SET_SELECTION: { 101 auto start = data.ReadInt32(); 102 auto end = data.ReadInt32(); 103 HandleSetSelection(start, end); 104 break; 105 } 106 case HANDLE_EXTEND_ACTION: { 107 auto action = data.ReadInt32(); 108 HandleExtendAction(action); 109 break; 110 } 111 case HANDLE_SELECT: { 112 auto keyCode = data.ReadInt32(); 113 auto cursorMoveSkip = data.ReadInt32(); 114 HandleSelect(keyCode, cursorMoveSkip); 115 break; 116 } 117 default: 118 return IPCObjectStub::OnRemoteRequest(code, data, reply, option); 119 } 120 return NO_ERROR; 121 } 122 InsertText(const std::u16string & text)123 int32_t InputDataChannelStub::InsertText(const std::u16string& text) 124 { 125 IMSA_HILOGI("InputDataChannelStub::InsertText"); 126 if (msgHandler) { 127 MessageParcel *parcel = new MessageParcel; 128 parcel->WriteString16(text); 129 Message *msg = new Message(MessageID::MSG_ID_INSERT_CHAR, parcel); 130 msgHandler->SendMessage(msg); 131 IMSA_HILOGI("InputDataChannelStub::InsertText return true"); 132 return ErrorCode::NO_ERROR; 133 } 134 return ErrorCode::ERROR_CLIENT_NULL_POINTER; 135 } 136 DeleteForward(int32_t length)137 int32_t InputDataChannelStub::DeleteForward(int32_t length) 138 { 139 IMSA_HILOGI("InputDataChannelStub::DeleteForward"); 140 if (!msgHandler) { 141 return ErrorCode::ERROR_CLIENT_NULL_POINTER; 142 } 143 MessageParcel *parcel = new MessageParcel; 144 parcel->WriteInt32(length); 145 Message *msg = new Message(MessageID::MSG_ID_DELETE_FORWARD, parcel); 146 msgHandler->SendMessage(msg); 147 148 return ErrorCode::NO_ERROR; 149 } 150 DeleteBackward(int32_t length)151 int32_t InputDataChannelStub::DeleteBackward(int32_t length) 152 { 153 IMSA_HILOGI("InputDataChannelStub::DeleteBackward"); 154 if (msgHandler) { 155 MessageParcel *parcel = new MessageParcel; 156 parcel->WriteInt32(length); 157 Message *msg = new Message(MessageID::MSG_ID_DELETE_BACKWARD, parcel); 158 msgHandler->SendMessage(msg); 159 return ErrorCode::NO_ERROR; 160 } 161 return ErrorCode::ERROR_CLIENT_NULL_POINTER; 162 } 163 164 GetTextBeforeCursor(int32_t number,std::u16string & text)165 int32_t InputDataChannelStub::GetTextBeforeCursor(int32_t number, std::u16string &text) 166 { 167 IMSA_HILOGI("InputDataChannelStub::GetTextBeforeCursor"); 168 return InputMethodController::GetInstance()->GetTextBeforeCursor(number, text); 169 } 170 GetTextAfterCursor(int32_t number,std::u16string & text)171 int32_t InputDataChannelStub::GetTextAfterCursor(int32_t number, std::u16string &text) 172 { 173 IMSA_HILOGI("InputDataChannelStub::GetTextAfterCursor"); 174 return InputMethodController::GetInstance()->GetTextAfterCursor(number, text); 175 } 176 GetEnterKeyType(int32_t & keyType)177 int32_t InputDataChannelStub::GetEnterKeyType(int32_t &keyType) 178 { 179 IMSA_HILOGI("InputDataChannelStub::GetEnterKeyType"); 180 return InputMethodController::GetInstance()->GetEnterKeyType(keyType); 181 } 182 GetInputPattern(int32_t & inputPattern)183 int32_t InputDataChannelStub::GetInputPattern(int32_t &inputPattern) 184 { 185 IMSA_HILOGI("InputDataChannelStub::GetInputPattern"); 186 return InputMethodController::GetInstance()->GetInputPattern(inputPattern); 187 } 188 HandleGetOperation(int32_t number,std::u16string & text,int32_t msgType)189 int32_t InputDataChannelStub::HandleGetOperation(int32_t number, std::u16string &text, int32_t msgType) 190 { 191 IMSA_HILOGI("InputDataChannelStub::start, msgId: %{public}d, number: %{public}d", msgType, number); 192 if (msgHandler == nullptr) { 193 return ErrorCode::ERROR_CLIENT_NULL_POINTER; 194 } 195 int32_t msgId; 196 if (msgType == GET_TEXT_BEFORE_CURSOR) { 197 msgId = MessageID::MSG_ID_GET_TEXT_BEFORE_CURSOR; 198 } else { 199 msgId = MessageID::MSG_ID_GET_TEXT_AFTER_CURSOR; 200 } 201 MessageParcel *parcel = new MessageParcel; 202 Message *msg = new Message(msgId, parcel); 203 msgHandler->SendMessage(msg); 204 205 std::unique_lock<std::mutex> lock(getOperationListenerLock_); 206 getOperationListenerCv_.wait_for(lock, std::chrono::milliseconds(WAIT_TIME_STUB)); 207 if (msgType == GET_TEXT_BEFORE_CURSOR) { 208 return InputMethodController::GetInstance()->GetTextBeforeCursor(number, text); 209 } else { 210 return InputMethodController::GetInstance()->GetTextAfterCursor(number, text); 211 } 212 } 213 NotifyGetOperationCompletion()214 void InputDataChannelStub::NotifyGetOperationCompletion() 215 { 216 getOperationListenerCv_.notify_one(); 217 } 218 SendKeyboardStatus(int32_t status)219 void InputDataChannelStub::SendKeyboardStatus(int32_t status) 220 { 221 IMSA_HILOGI("InputDataChannelStub::SendKeyboardStatus"); 222 if (msgHandler) { 223 MessageParcel *parcel = new MessageParcel; 224 parcel->WriteInt32(status); 225 Message *msg = new Message(MessageID::MSG_ID_SEND_KEYBOARD_STATUS, parcel); 226 msgHandler->SendMessage(msg); 227 } 228 } 229 SendFunctionKey(int32_t funcKey)230 int32_t InputDataChannelStub::SendFunctionKey(int32_t funcKey) 231 { 232 IMSA_HILOGI("InputDataChannelStub::SendFunctionKey"); 233 if (msgHandler) { 234 MessageParcel *parcel = new MessageParcel; 235 parcel->WriteInt32(funcKey); 236 Message *msg = new Message(MessageID::MSG_ID_SEND_FUNCTION_KEY, parcel); 237 msgHandler->SendMessage(msg); 238 return ErrorCode::NO_ERROR; 239 } 240 return ErrorCode::ERROR_CLIENT_NULL_POINTER; 241 } 242 MoveCursor(int32_t keyCode)243 int32_t InputDataChannelStub::MoveCursor(int32_t keyCode) 244 { 245 IMSA_HILOGI("InputDataChannelStub::MoveCursor"); 246 if (msgHandler) { 247 MessageParcel *parcel = new MessageParcel; 248 parcel->WriteInt32(keyCode); 249 Message *msg = new Message(MessageID::MSG_ID_MOVE_CURSOR, parcel); 250 msgHandler->SendMessage(msg); 251 return ErrorCode::NO_ERROR; 252 } 253 return ErrorCode::ERROR_CLIENT_NULL_POINTER; 254 } 255 HandleSetSelection(int32_t start,int32_t end)256 void InputDataChannelStub::HandleSetSelection(int32_t start, int32_t end) 257 { 258 IMSA_HILOGI("InputDataChannelStub::HandleSetSelection"); 259 if (msgHandler) { 260 MessageParcel *parcel = new MessageParcel; 261 parcel->WriteInt32(start); 262 parcel->WriteInt32(end); 263 Message *msg = new Message(MessageID::MSG_ID_HANDLE_SET_SELECTION, parcel); 264 msgHandler->SendMessage(msg); 265 } 266 } 267 HandleExtendAction(int32_t action)268 void InputDataChannelStub::HandleExtendAction(int32_t action) 269 { 270 IMSA_HILOGI("InputDataChannelStub::HandleExtendAction"); 271 if (msgHandler) { 272 MessageParcel *parcel = new MessageParcel; 273 parcel->WriteInt32(action); 274 Message *msg = new Message(MessageID::MSG_ID_HANDLE_EXTEND_ACTION, parcel); 275 msgHandler->SendMessage(msg); 276 } 277 } 278 HandleSelect(int32_t keyCode,int32_t cursorMoveSkip)279 void InputDataChannelStub::HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) 280 { 281 IMSA_HILOGI("InputDataChannelStub::HandleSelect"); 282 if (msgHandler) { 283 MessageParcel *parcel = new MessageParcel; 284 parcel->WriteInt32(keyCode); 285 parcel->WriteInt32(cursorMoveSkip); 286 Message *msg = new Message(MessageID::MSG_ID_HANDLE_SELECT, parcel); 287 msgHandler->SendMessage(msg); 288 } 289 } 290 SetHandler(MessageHandler * handler)291 void InputDataChannelStub::SetHandler(MessageHandler *handler) 292 { 293 msgHandler = handler; 294 } 295 } // namespace MiscServices 296 } // namespace OHOS 297