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_method_agent_stub.h"
17
18 #include "global.h"
19 #include "input_method_ability.h"
20 #include "ipc_skeleton.h"
21 #include "itypes_util.h"
22 #include "task_manager.h"
23 #include "tasks/task_imsa.h"
24
25 namespace OHOS {
26 namespace MiscServices {
27 using namespace MessageID;
28
InputMethodAgentStub()29 InputMethodAgentStub::InputMethodAgentStub() { }
30
~InputMethodAgentStub()31 InputMethodAgentStub::~InputMethodAgentStub() { }
32
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t InputMethodAgentStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
34 MessageOption &option)
35 {
36 IMSA_HILOGD("InputMethodAgentStub, code = %{public}u, callingPid: %{public}d, callingUid: %{public}d.", code,
37 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
38 auto descriptorToken = data.ReadInterfaceToken();
39 if (descriptorToken != GetDescriptor()) {
40 return ErrorCode::ERROR_STATUS_UNKNOWN_TRANSACTION;
41 }
42
43 switch (code) {
44 case DISPATCH_KEY_EVENT: {
45 return DispatchKeyEventOnRemote(data, reply);
46 }
47 case SET_CALLING_WINDOW_ID: {
48 SetCallingWindow(data.ReadUint32());
49 break;
50 }
51 case ON_CURSOR_UPDATE: {
52 int32_t positionX = data.ReadInt32();
53 int32_t positionY = data.ReadInt32();
54 int32_t height = data.ReadInt32();
55 OnCursorUpdate(positionX, positionY, height);
56 reply.WriteNoException();
57 return ErrorCode::NO_ERROR;
58 }
59 case ON_SELECTION_CHANGE: {
60 std::u16string text = data.ReadString16();
61 int32_t oldBegin = data.ReadInt32();
62 int32_t oldEnd = data.ReadInt32();
63 int32_t newBegin = data.ReadInt32();
64 int32_t newEnd = data.ReadInt32();
65 OnSelectionChange(text, oldBegin, oldEnd, newBegin, newEnd);
66 reply.WriteNoException();
67 return ErrorCode::NO_ERROR;
68 }
69 case SEND_PRIVATE_COMMAND: {
70 return SendPrivateCommandOnRemote(data, reply);
71 }
72 case ON_ATTRIBUTE_CHANGE: {
73 return OnAttributeChangeOnRemote(data, reply);
74 }
75 case SEND_MESSAGE: {
76 return RecvMessageOnRemote(data, reply);
77 }
78 default: {
79 return IRemoteStub::OnRemoteRequest(code, data, reply, option);
80 }
81 }
82 return ErrorCode::NO_ERROR;
83 }
84
DispatchKeyEventOnRemote(MessageParcel & data,MessageParcel & reply)85 int32_t InputMethodAgentStub::DispatchKeyEventOnRemote(MessageParcel &data, MessageParcel &reply)
86 {
87 std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
88 if (keyEvent == nullptr) {
89 IMSA_HILOGE("keyEvent is nullptr!");
90 return ErrorCode::ERROR_NULL_POINTER;
91 }
92 if (!keyEvent->ReadFromParcel(data)) {
93 IMSA_HILOGE("failed to read key event from parcel!");
94 return ErrorCode::ERROR_EX_PARCELABLE;
95 }
96 auto consumerObject = data.ReadRemoteObject();
97 if (consumerObject == nullptr) {
98 IMSA_HILOGE("consumerObject is nullptr!");
99 return ErrorCode::ERROR_EX_PARCELABLE;
100 }
101 sptr<KeyEventConsumerProxy> consumer = new (std::nothrow) KeyEventConsumerProxy(consumerObject);
102 auto ret = InputMethodAbility::GetInstance()->DispatchKeyEvent(keyEvent, consumer);
103 return reply.WriteInt32(ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
104 }
105
SendPrivateCommandOnRemote(MessageParcel & data,MessageParcel & reply)106 int32_t InputMethodAgentStub::SendPrivateCommandOnRemote(MessageParcel &data, MessageParcel &reply)
107 {
108 std::unordered_map<std::string, PrivateDataValue> privateCommand;
109 if (!ITypesUtil::Unmarshal(data, privateCommand)) {
110 IMSA_HILOGE("failed to read message parcel!");
111 return ErrorCode::ERROR_EX_PARCELABLE;
112 }
113 if (!InputMethodAbility::GetInstance()->IsDefaultIme()) {
114 IMSA_HILOGE("current is not default ime!");
115 return ErrorCode::ERROR_NOT_DEFAULT_IME;
116 }
117 auto task = std::make_shared<TaskImsaSendPrivateCommand>(privateCommand);
118 TaskManager::GetInstance().PostTask(task);
119
120 return reply.WriteInt32(ErrorCode::NO_ERROR) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
121 }
122
OnAttributeChangeOnRemote(MessageParcel & data,MessageParcel & reply)123 int32_t InputMethodAgentStub::OnAttributeChangeOnRemote(MessageParcel &data, MessageParcel &reply)
124 {
125 InputAttribute attribute;
126 if (!ITypesUtil::Unmarshal(data, attribute)) {
127 IMSA_HILOGE("failed to read attribute from parcel!");
128 return ErrorCode::ERROR_EX_PARCELABLE;
129 }
130 OnAttributeChange(attribute);
131 reply.WriteNoException();
132 return ErrorCode::NO_ERROR;
133 }
134
DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,sptr<IKeyEventConsumer> & consumer)135 int32_t InputMethodAgentStub::DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent,
136 sptr<IKeyEventConsumer> &consumer)
137 {
138 return false;
139 }
140
SetCallingWindow(uint32_t windowId)141 void InputMethodAgentStub::SetCallingWindow(uint32_t windowId)
142 {
143 InputMethodAbility::GetInstance()->SetCallingWindow(windowId);
144 }
145
OnCursorUpdate(int32_t positionX,int32_t positionY,int height)146 void InputMethodAgentStub::OnCursorUpdate(int32_t positionX, int32_t positionY, int height)
147 {
148 auto task = std::make_shared<TaskImsaOnCursorUpdate>(positionX, positionY, height);
149 TaskManager::GetInstance().PostTask(task);
150 }
151
OnSelectionChange(std::u16string text,int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)152 void InputMethodAgentStub::OnSelectionChange(std::u16string text, int32_t oldBegin, int32_t oldEnd, int32_t newBegin,
153 int32_t newEnd)
154 {
155 auto task = std::make_shared<TaskImsaOnSelectionChange>(text, oldBegin, oldEnd, newBegin, newEnd);
156 TaskManager::GetInstance().PostTask(task);
157 }
158
SendPrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)159 int32_t InputMethodAgentStub::SendPrivateCommand(
160 const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
161 {
162 return ErrorCode::NO_ERROR;
163 }
164
OnAttributeChange(const InputAttribute & attribute)165 void InputMethodAgentStub::OnAttributeChange(const InputAttribute &attribute)
166 {
167 auto task = std::make_shared<TaskImsaAttributeChange>(attribute);
168 TaskManager::GetInstance().PostTask(task);
169 }
170
RecvMessageOnRemote(MessageParcel & data,MessageParcel & reply)171 int32_t InputMethodAgentStub::RecvMessageOnRemote(MessageParcel &data, MessageParcel &reply)
172 {
173 ArrayBuffer arrayBuffer;
174 if (!ITypesUtil::Unmarshal(data, arrayBuffer)) {
175 IMSA_HILOGE("Failed to read arrayBuffer parcel!");
176 return ErrorCode::ERROR_EX_PARCELABLE;
177 }
178 auto ret = InputMethodAbility::GetInstance()->RecvMessage(arrayBuffer);
179 return reply.WriteInt32(ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
180 }
181
SendMessage(const ArrayBuffer & arraybuffer)182 int32_t InputMethodAgentStub::SendMessage(const ArrayBuffer &arraybuffer)
183 {
184 return ErrorCode::NO_ERROR;
185 }
186 } // namespace MiscServices
187 } // namespace OHOS