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 #include "input_method_core_stub.h"
16
17 #include <cstdint>
18 #include <string_ex.h>
19
20 #include "i_input_data_channel.h"
21 #include "input_control_channel_proxy.h"
22 #include "input_method_ability.h"
23 #include "ipc_skeleton.h"
24 #include "itypes_util.h"
25 #include "message_parcel.h"
26 #include "system_cmd_channel_proxy.h"
27 #include "task_manager.h"
28 #include "tasks/task_imsa.h"
29
30 namespace OHOS {
31 namespace MiscServices {
32 using namespace MessageID;
InputMethodCoreStub()33 InputMethodCoreStub::InputMethodCoreStub() { }
34
~InputMethodCoreStub()35 InputMethodCoreStub::~InputMethodCoreStub() { }
36
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int32_t InputMethodCoreStub::OnRemoteRequest(
38 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
39 {
40 IMSA_HILOGD("InputMethodCoreStub, code: %{public}u, callingPid: %{public}d, callingUid: %{public}d.", code,
41 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
42 auto descriptorToken = data.ReadInterfaceToken();
43 if (descriptorToken != IInputMethodCore::GetDescriptor()) {
44 IMSA_HILOGE("InputMethodCoreStub descriptor error!");
45 return ErrorCode::ERROR_STATUS_UNKNOWN_TRANSACTION;
46 }
47 if (code < CORE_CMD_BEGIN || code >= CORE_CMD_END) {
48 IMSA_HILOGE("code error, code = %{public}u, callingPid: %{public}d, callingUid: %{public}d.", code,
49 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
50 return IRemoteStub::OnRemoteRequest(code, data, reply, option);
51 }
52 return (this->*HANDLERS[code])(data, reply);
53 }
54
InitInputControlChannel(const sptr<IInputControlChannel> & inputControlChannel)55 int32_t InputMethodCoreStub::InitInputControlChannel(const sptr<IInputControlChannel> &inputControlChannel)
56 {
57 auto task = std::make_shared<TaskImsaInitInputCtrlChannel>(inputControlChannel->AsObject());
58 TaskManager::GetInstance().PostTask(task);
59 return ErrorCode::NO_ERROR;
60 }
61
ShowKeyboard()62 int32_t InputMethodCoreStub::ShowKeyboard()
63 {
64 auto task = std::make_shared<TaskImsaShowKeyboard>();
65 TaskManager::GetInstance().PostTask(task);
66 return ErrorCode::NO_ERROR;
67 }
68
HideKeyboard()69 int32_t InputMethodCoreStub::HideKeyboard()
70 {
71 auto task = std::make_shared<TaskImsaHideKeyboard>();
72 TaskManager::GetInstance().PostTask(task);
73 return ErrorCode::NO_ERROR;
74 }
75
StopInputService(bool isTerminateIme)76 int32_t InputMethodCoreStub::StopInputService(bool isTerminateIme)
77 {
78 auto task = std::make_shared<TaskImsaStopInputService>(isTerminateIme);
79 TaskManager::GetInstance().PostTask(task);
80 return ErrorCode::NO_ERROR;
81 }
82
InitInputControlChannelOnRemote(MessageParcel & data,MessageParcel & reply)83 int32_t InputMethodCoreStub::InitInputControlChannelOnRemote(MessageParcel &data, MessageParcel &reply)
84 {
85 sptr<IRemoteObject> channelObject = data.ReadRemoteObject();
86 if (channelObject == nullptr) {
87 IMSA_HILOGE("channelObject is nullptr!");
88 return reply.WriteInt32(ErrorCode::ERROR_EX_PARCELABLE) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
89 }
90 sptr<IInputControlChannel> inputControlChannel = new (std::nothrow) InputControlChannelProxy(channelObject);
91 if (inputControlChannel == nullptr) {
92 IMSA_HILOGE("failed to new inputControlChannel!");
93 return reply.WriteInt32(ErrorCode::ERROR_NULL_POINTER) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
94 }
95 auto ret = InitInputControlChannel(inputControlChannel);
96 return reply.WriteInt32(ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
97 }
98
StartInputOnRemote(MessageParcel & data,MessageParcel & reply)99 int32_t InputMethodCoreStub::StartInputOnRemote(MessageParcel &data, MessageParcel &reply)
100 {
101 IMSA_HILOGI(
102 "CoreStub, callingPid/Uid: %{public}d/%{public}d.", IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
103 bool isBindFromClient = false;
104 InputClientInfo clientInfo = {};
105 sptr<IRemoteObject> channel = nullptr;
106 if (!ITypesUtil::Unmarshal(data, isBindFromClient, clientInfo, clientInfo.channel)) {
107 IMSA_HILOGE("Unmarshal failed!");
108 return ErrorCode::ERROR_EX_PARCELABLE;
109 }
110 auto ret = StartInput(clientInfo, isBindFromClient);
111 return ITypesUtil::Marshal(reply, ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
112 }
113
SecurityChangeOnRemote(MessageParcel & data,MessageParcel & reply)114 int32_t InputMethodCoreStub::SecurityChangeOnRemote(MessageParcel &data, MessageParcel &reply)
115 {
116 int32_t security;
117 if (!ITypesUtil::Unmarshal(data, security)) {
118 IMSA_HILOGE("Unmarshal failed!");
119 return ErrorCode::ERROR_EX_PARCELABLE;
120 }
121 auto ret = InputMethodAbility::GetInstance()->OnSecurityChange(security);
122 return ITypesUtil::Marshal(reply, ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
123 }
124
OnConnectSystemCmd(const sptr<IRemoteObject> & channel,sptr<IRemoteObject> & agent)125 int32_t InputMethodCoreStub::OnConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent)
126 {
127 return ErrorCode::NO_ERROR;
128 }
129
OnConnectSystemCmdOnRemote(MessageParcel & data,MessageParcel & reply)130 int32_t InputMethodCoreStub::OnConnectSystemCmdOnRemote(MessageParcel &data, MessageParcel &reply)
131 {
132 sptr<IRemoteObject> channelObject = nullptr;
133 if (!ITypesUtil::Unmarshal(data, channelObject)) {
134 IMSA_HILOGE("failed to read message parcel!");
135 return ErrorCode::ERROR_EX_PARCELABLE;
136 }
137 sptr<IRemoteObject> agent = nullptr;
138 auto ret = InputMethodAbility::GetInstance()->OnConnectSystemCmd(channelObject, agent);
139 return reply.WriteInt32(ret) && reply.WriteRemoteObject(agent) ? ErrorCode::NO_ERROR :
140 ErrorCode::ERROR_EX_PARCELABLE;
141 }
142
SetSubtypeOnRemote(MessageParcel & data,MessageParcel & reply)143 int32_t InputMethodCoreStub::SetSubtypeOnRemote(MessageParcel &data, MessageParcel &reply)
144 {
145 SubProperty property;
146 if (!ITypesUtil::Unmarshal(data, property)) {
147 IMSA_HILOGE("failed to read message parcel!");
148 return ErrorCode::ERROR_EX_PARCELABLE;
149 }
150
151 auto task = std::make_shared<TaskImsaOnSetSubProperty>(property);
152 TaskManager::GetInstance().PostTask(task);
153 return reply.WriteInt32(ErrorCode::NO_ERROR) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
154 }
155
OnSetInputTypeOnRemote(MessageParcel & data,MessageParcel & reply)156 int32_t InputMethodCoreStub::OnSetInputTypeOnRemote(MessageParcel &data, MessageParcel &reply)
157 {
158 InputType inputType;
159 if (!ITypesUtil::Unmarshal(data, inputType)) {
160 IMSA_HILOGE("failed to read inputType parcel!");
161 return ErrorCode::ERROR_EX_PARCELABLE;
162 }
163 InputMethodAbility::GetInstance()->OnSetInputType(inputType);
164 return reply.WriteInt32(ErrorCode::NO_ERROR) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
165 }
166
StopInputOnRemote(MessageParcel & data,MessageParcel & reply)167 int32_t InputMethodCoreStub::StopInputOnRemote(MessageParcel &data, MessageParcel &reply)
168 {
169 sptr<IRemoteObject> channel = nullptr;
170 if (!ITypesUtil::Unmarshal(data, channel)) {
171 IMSA_HILOGE("failed to read message parcel!");
172 return ErrorCode::ERROR_EX_PARCELABLE;
173 }
174 auto ret = StopInput(channel);
175 return ITypesUtil::Marshal(reply, ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
176 }
177
IsEnableOnRemote(MessageParcel & data,MessageParcel & reply)178 int32_t InputMethodCoreStub::IsEnableOnRemote(MessageParcel &data, MessageParcel &reply)
179 {
180 bool isEnable = IsEnable();
181 return ITypesUtil::Marshal(reply, ErrorCode::NO_ERROR, isEnable) ? ErrorCode::NO_ERROR :
182 ErrorCode::ERROR_EX_PARCELABLE;
183 }
184
ShowKeyboardOnRemote(MessageParcel & data,MessageParcel & reply)185 int32_t InputMethodCoreStub::ShowKeyboardOnRemote(MessageParcel &data, MessageParcel &reply)
186 {
187 auto ret = ShowKeyboard();
188 return ITypesUtil::Marshal(reply, ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
189 }
190
HideKeyboardOnRemote(MessageParcel & data,MessageParcel & reply)191 int32_t InputMethodCoreStub::HideKeyboardOnRemote(MessageParcel &data, MessageParcel &reply)
192 {
193 auto ret = HideKeyboard();
194 return ITypesUtil::Marshal(reply, ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
195 }
196
StopInputServiceOnRemote(MessageParcel & data,MessageParcel & reply)197 int32_t InputMethodCoreStub::StopInputServiceOnRemote(MessageParcel &data, MessageParcel &reply)
198 {
199 bool isTerminateIme = false;
200 if (!ITypesUtil::Unmarshal(data, isTerminateIme)) {
201 IMSA_HILOGE("unmarshal failed!");
202 return ErrorCode::ERROR_EX_PARCELABLE;
203 }
204 auto ret = StopInputService(isTerminateIme);
205 return ITypesUtil::Marshal(reply, ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
206 }
207
IsPanelShownOnRemote(MessageParcel & data,MessageParcel & reply)208 int32_t InputMethodCoreStub::IsPanelShownOnRemote(MessageParcel &data, MessageParcel &reply)
209 {
210 PanelInfo info;
211 if (!ITypesUtil::Unmarshal(data, info)) {
212 IMSA_HILOGE("unmarshal failed!");
213 return ErrorCode::ERROR_EX_PARCELABLE;
214 }
215 bool isShown = false;
216 int32_t ret = IsPanelShown(info, isShown);
217 return ITypesUtil::Marshal(reply, ret, isShown) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
218 }
219
OnClientInactiveOnRemote(MessageParcel & data,MessageParcel & reply)220 int32_t InputMethodCoreStub::OnClientInactiveOnRemote(MessageParcel &data, MessageParcel &reply)
221 {
222 sptr<IRemoteObject> channel = nullptr;
223 if (!ITypesUtil::Unmarshal(data, channel)) {
224 IMSA_HILOGE("failed to read message parcel!");
225 return ErrorCode::ERROR_EX_PARCELABLE;
226 }
227
228 auto task = std::make_shared<TaskImsaOnClientInactive>(channel);
229 TaskManager::GetInstance().PostTask(task);
230 return ITypesUtil::Marshal(reply, ErrorCode::NO_ERROR) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
231 }
232
StartInput(const InputClientInfo & clientInfo,bool isBindFromClient)233 int32_t InputMethodCoreStub::StartInput(const InputClientInfo &clientInfo, bool isBindFromClient)
234 {
235 auto task = std::make_shared<TaskImsaStartInput>(clientInfo, isBindFromClient);
236 TaskManager::GetInstance().PostTask(task);
237 return ErrorCode::NO_ERROR;
238 }
239
SetSubtype(const SubProperty & property)240 int32_t InputMethodCoreStub::SetSubtype(const SubProperty &property)
241 {
242 return ErrorCode::NO_ERROR;
243 }
244
OnSecurityChange(int32_t security)245 int32_t InputMethodCoreStub::OnSecurityChange(int32_t security)
246 {
247 return ErrorCode::NO_ERROR;
248 }
249
OnSetInputType(InputType inputType)250 int32_t InputMethodCoreStub::OnSetInputType(InputType inputType)
251 {
252 return ErrorCode::NO_ERROR;
253 }
254
StopInput(const sptr<IRemoteObject> & channel)255 int32_t InputMethodCoreStub::StopInput(const sptr<IRemoteObject> &channel)
256 {
257 auto task = std::make_shared<TaskImsaStopInput>(channel);
258 TaskManager::GetInstance().PostTask(task);
259 return ErrorCode::NO_ERROR;
260 }
261
IsEnable()262 bool InputMethodCoreStub::IsEnable()
263 {
264 return InputMethodAbility::GetInstance()->IsEnable();
265 }
266
IsPanelShown(const PanelInfo & panelInfo,bool & isShown)267 int32_t InputMethodCoreStub::IsPanelShown(const PanelInfo &panelInfo, bool &isShown)
268 {
269 return InputMethodAbility::GetInstance()->IsPanelShown(panelInfo, isShown);
270 }
271
OnClientInactive(const sptr<IRemoteObject> & channel)272 void InputMethodCoreStub::OnClientInactive(const sptr<IRemoteObject> &channel) { }
273
OnCallingDisplayIdChanged(uint64_t dispalyId)274 void InputMethodCoreStub::OnCallingDisplayIdChanged(uint64_t dispalyId) { }
275
OnCallingDisplayChangeOnRemote(MessageParcel & data,MessageParcel & reply)276 int32_t InputMethodCoreStub::OnCallingDisplayChangeOnRemote(MessageParcel &data, MessageParcel &reply)
277 {
278 uint64_t displayId = 0;
279 if (!ITypesUtil::Unmarshal(data, displayId)) {
280 IMSA_HILOGE("unmarshal failed!");
281 return ErrorCode::ERROR_EX_PARCELABLE;
282 }
283 int32_t ret = InputMethodAbility::GetInstance()->OnCallingDisplayIdChanged(displayId);
284 return ITypesUtil::Marshal(reply, ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
285 }
286 } // namespace MiscServices
287 } // namespace OHOS