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_system_ability_proxy.h"
17 
18 #include "element_name.h"
19 #include "global.h"
20 #include "inputmethod_service_ipc_interface_code.h"
21 #include "itypes_util.h"
22 #include "message_option.h"
23 
24 namespace OHOS {
25 namespace MiscServices {
26 using namespace ErrorCode;
27 
InputMethodSystemAbilityProxy(const sptr<IRemoteObject> & object)28 InputMethodSystemAbilityProxy::InputMethodSystemAbilityProxy(const sptr<IRemoteObject> &object)
29     : IRemoteProxy<IInputMethodSystemAbility>(object)
30 {
31 }
32 
StartInput(InputClientInfo & inputClientInfo,sptr<IRemoteObject> & agent)33 int32_t InputMethodSystemAbilityProxy::StartInput(InputClientInfo &inputClientInfo, sptr<IRemoteObject> &agent)
34 {
35     return SendRequest(
36         static_cast<uint32_t>(InputMethodInterfaceCode::START_INPUT),
37         [&inputClientInfo](MessageParcel &data) {
38             return ITypesUtil::Marshal(
39                 data, inputClientInfo, inputClientInfo.client->AsObject(), inputClientInfo.channel);
40         },
41         [&agent](MessageParcel &reply) {
42             agent = reply.ReadRemoteObject();
43             return true;
44         });
45 }
46 
ConnectSystemCmd(const sptr<IRemoteObject> & channel,sptr<IRemoteObject> & agent)47 int32_t InputMethodSystemAbilityProxy::ConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent)
48 {
49     return SendRequest(
50         static_cast<uint32_t>(InputMethodInterfaceCode::CONNECT_SYSTEM_CMD),
51         [channel](MessageParcel &data) { return data.WriteRemoteObject(channel); },
52         [&agent](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, agent); });
53 }
54 
ShowCurrentInput()55 int32_t InputMethodSystemAbilityProxy::ShowCurrentInput()
56 {
57     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SHOW_CURRENT_INPUT));
58 }
59 
HideCurrentInput()60 int32_t InputMethodSystemAbilityProxy::HideCurrentInput()
61 {
62     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::HIDE_CURRENT_INPUT));
63 }
64 
StopInputSession()65 int32_t InputMethodSystemAbilityProxy::StopInputSession()
66 {
67     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::STOP_INPUT_SESSION));
68 }
69 
ShowInput(sptr<IInputClient> client)70 int32_t InputMethodSystemAbilityProxy::ShowInput(sptr<IInputClient> client)
71 {
72     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SHOW_INPUT),
73         [client](MessageParcel &data) { return data.WriteRemoteObject(client->AsObject()); });
74 }
75 
HideInput(sptr<IInputClient> client)76 int32_t InputMethodSystemAbilityProxy::HideInput(sptr<IInputClient> client)
77 {
78     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::HIDE_INPUT),
79         [client](MessageParcel &data) { return data.WriteRemoteObject(client->AsObject()); });
80 }
81 
ReleaseInput(sptr<IInputClient> client)82 int32_t InputMethodSystemAbilityProxy::ReleaseInput(sptr<IInputClient> client)
83 {
84     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::RELEASE_INPUT),
85         [client](MessageParcel &data) { return data.WriteRemoteObject(client->AsObject()); });
86 }
87 
RequestShowInput()88 int32_t InputMethodSystemAbilityProxy::RequestShowInput()
89 {
90     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::REQUEST_SHOW_INPUT));
91 }
92 
RequestHideInput()93 int32_t InputMethodSystemAbilityProxy::RequestHideInput()
94 {
95     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::REQUEST_HIDE_INPUT));
96 }
97 
InitConnect()98 int32_t InputMethodSystemAbilityProxy::InitConnect()
99 {
100     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::INIT_CONNECT));
101 }
102 
DisplayOptionalInputMethod()103 int32_t InputMethodSystemAbilityProxy::DisplayOptionalInputMethod()
104 {
105     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::DISPLAY_OPTIONAL_INPUT_METHOD));
106 }
107 
SetCoreAndAgent(const sptr<IInputMethodCore> & core,const sptr<IRemoteObject> & agent)108 int32_t InputMethodSystemAbilityProxy::SetCoreAndAgent(
109     const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent)
110 {
111     return SendRequest(
112         static_cast<uint32_t>(InputMethodInterfaceCode::SET_CORE_AND_AGENT), [core, agent](MessageParcel &data) {
113             return data.WriteRemoteObject(core->AsObject()) && data.WriteRemoteObject(agent);
114         });
115 }
116 
GetDefaultInputMethod(std::shared_ptr<Property> & property,bool isBrief)117 int32_t InputMethodSystemAbilityProxy::GetDefaultInputMethod(std::shared_ptr<Property> &property, bool isBrief)
118 {
119     return SendRequest(
120         static_cast<uint32_t>(InputMethodInterfaceCode::GET_DEFAULT_INPUT_METHOD),
121         [isBrief](
122             MessageParcel &data) { return ITypesUtil::Marshal(data, isBrief); },
123         [&property](MessageParcel &reply) {
124             property = std::make_shared<Property>();
125             return ITypesUtil::Unmarshal(reply, *property);
126         });
127 }
128 
GetInputMethodConfig(OHOS::AppExecFwk::ElementName & inputMethodConfig)129 int32_t InputMethodSystemAbilityProxy::GetInputMethodConfig(OHOS::AppExecFwk::ElementName &inputMethodConfig)
130 {
131     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_INPUT_METHOD_SETTINGS), nullptr,
132         [&inputMethodConfig](MessageParcel& reply) {
133             return ITypesUtil::Unmarshal(reply, inputMethodConfig);
134         });
135 }
136 
GetSecurityMode(int32_t & security)137 int32_t InputMethodSystemAbilityProxy::GetSecurityMode(int32_t &security)
138 {
139     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_SECURITY_MODE), nullptr,
140         [&security](MessageParcel& reply) {
141             return ITypesUtil::Unmarshal(reply, security);
142         });
143 }
144 
UnRegisteredProxyIme(UnRegisteredType type,const sptr<IInputMethodCore> & core)145 int32_t InputMethodSystemAbilityProxy::UnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core)
146 {
147     return SendRequest(
148         static_cast<uint32_t>(InputMethodInterfaceCode::UNREGISTERED_PROXY_IME), [&type, &core](MessageParcel &data) {
149             return ITypesUtil::Marshal(data, static_cast<int32_t>(type), core->AsObject());
150         });
151 }
152 
GetCurrentInputMethod()153 std::shared_ptr<Property> InputMethodSystemAbilityProxy::GetCurrentInputMethod()
154 {
155     std::shared_ptr<Property> property = nullptr;
156     int32_t ret = SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_CURRENT_INPUT_METHOD), nullptr,
157         [&property](MessageParcel &reply) {
158             property = std::make_shared<Property>();
159             return ITypesUtil::Unmarshal(reply, *property);
160         });
161     return ret != ErrorCode::NO_ERROR ? nullptr : property;
162 }
163 
GetCurrentInputMethodSubtype()164 std::shared_ptr<SubProperty> InputMethodSystemAbilityProxy::GetCurrentInputMethodSubtype()
165 {
166     std::shared_ptr<SubProperty> property = nullptr;
167     int32_t ret = SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::GET_CURRENT_INPUT_METHOD_SUBTYPE),
168         nullptr, [&property](MessageParcel &reply) {
169             property = std::make_shared<SubProperty>();
170             return ITypesUtil::Unmarshal(reply, *property);
171         });
172     return ret != ErrorCode::NO_ERROR ? nullptr : property;
173 }
174 
IsDefaultImeSet()175 bool InputMethodSystemAbilityProxy::IsDefaultImeSet()
176 {
177     bool isDefaultImeSet = false;
178     IMSA_HILOGI("InputMethodSystemAbilityProxy::IsDefaultImeSet enter.");
179     SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::IS_DEFAULT_IME_SET), nullptr,
180         [&isDefaultImeSet](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isDefaultImeSet); });
181     return isDefaultImeSet;
182 }
183 
EnableIme(const std::string & bundleName)184 bool InputMethodSystemAbilityProxy::EnableIme(const std::string &bundleName)
185 {
186     bool enableIme = false;
187     IMSA_HILOGI("InputMethodSystemAbilityProxy::EnableIme enter.");
188     SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::ENABLE_IME),
189         [&bundleName](MessageParcel &data) { return ITypesUtil::Marshal(data, bundleName); },
190         [&enableIme](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, enableIme); });
191     return enableIme;
192 }
193 
ListInputMethod(InputMethodStatus status,std::vector<Property> & props)194 int32_t InputMethodSystemAbilityProxy::ListInputMethod(InputMethodStatus status, std::vector<Property> &props)
195 {
196     return SendRequest(
197         static_cast<uint32_t>(InputMethodInterfaceCode::LIST_INPUT_METHOD),
198         [status](MessageParcel &data) { return ITypesUtil::Marshal(data, uint32_t(status)); },
199         [&props](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, props); });
200 }
201 
ShowCurrentInputDeprecated()202 int32_t InputMethodSystemAbilityProxy::ShowCurrentInputDeprecated()
203 {
204     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SHOW_CURRENT_INPUT_DEPRECATED));
205 }
206 
HideCurrentInputDeprecated()207 int32_t InputMethodSystemAbilityProxy::HideCurrentInputDeprecated()
208 {
209     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::HIDE_CURRENT_INPUT_DEPRECATED));
210 }
211 
ListInputMethodSubtype(const std::string & name,std::vector<SubProperty> & subProps)212 int32_t InputMethodSystemAbilityProxy::ListInputMethodSubtype(
213     const std::string &name, std::vector<SubProperty> &subProps)
214 {
215     return SendRequest(
216         static_cast<uint32_t>(InputMethodInterfaceCode::LIST_INPUT_METHOD_SUBTYPE),
217         [&name](MessageParcel &data) { return ITypesUtil::Marshal(data, name); },
218         [&subProps](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, subProps); });
219 }
220 
ListCurrentInputMethodSubtype(std::vector<SubProperty> & subProps)221 int32_t InputMethodSystemAbilityProxy::ListCurrentInputMethodSubtype(std::vector<SubProperty> &subProps)
222 {
223     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::LIST_CURRENT_INPUT_METHOD_SUBTYPE), nullptr,
224         [&subProps](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, subProps); });
225 }
226 
SwitchInputMethod(const std::string & name,const std::string & subName,SwitchTrigger trigger)227 int32_t InputMethodSystemAbilityProxy::SwitchInputMethod(
228     const std::string &name, const std::string &subName, SwitchTrigger trigger)
229 {
230     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::SWITCH_INPUT_METHOD),
231         [&name, &subName, trigger](MessageParcel &data) { return ITypesUtil::Marshal(data, name, subName, trigger); });
232 }
233 
PanelStatusChange(const InputWindowStatus & status,const ImeWindowInfo & info)234 int32_t InputMethodSystemAbilityProxy::PanelStatusChange(const InputWindowStatus &status, const ImeWindowInfo &info)
235 {
236     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::PANEL_STATUS_CHANGE),
237         [status, &info](MessageParcel &data) {
238             return ITypesUtil::Marshal(data, static_cast<uint32_t>(status), info);
239         });
240 }
241 
UpdateListenEventFlag(InputClientInfo & clientInfo,uint32_t eventFlag)242 int32_t InputMethodSystemAbilityProxy::UpdateListenEventFlag(InputClientInfo &clientInfo, uint32_t eventFlag)
243 {
244     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::UPDATE_LISTEN_EVENT_FLAG),
245         [&clientInfo, eventFlag](MessageParcel &data) {
246             return ITypesUtil::Marshal(data, clientInfo, clientInfo.client->AsObject(), clientInfo.channel, eventFlag);
247         });
248 }
249 
IsCurrentIme()250 bool InputMethodSystemAbilityProxy::IsCurrentIme()
251 {
252     bool isCurrentIme = false;
253     SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::IS_CURRENT_IME), nullptr,
254         [&isCurrentIme](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isCurrentIme); });
255     return isCurrentIme;
256 }
257 
IsCurrentImeByPid(int32_t pid)258 bool InputMethodSystemAbilityProxy::IsCurrentImeByPid(int32_t pid)
259 {
260     bool isCurrentIme = false;
261     SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::IS_CURRENT_IME_BY_PID),
262         [&pid](MessageParcel &data) { return ITypesUtil::Marshal(data, pid); },
263         [&isCurrentIme](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isCurrentIme); });
264     return isCurrentIme;
265 }
266 
IsInputTypeSupported(InputType type)267 bool InputMethodSystemAbilityProxy::IsInputTypeSupported(InputType type)
268 {
269     bool isSupported = false;
270     SendRequest(
271         static_cast<uint32_t>(InputMethodInterfaceCode::IS_INPUT_TYPE_SUPPORTED),
272         [type](MessageParcel &data) { return ITypesUtil::Marshal(data, type); },
273         [&isSupported](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isSupported); });
274     return isSupported;
275 }
276 
StartInputType(InputType type)277 int32_t InputMethodSystemAbilityProxy::StartInputType(InputType type)
278 {
279     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::START_INPUT_TYPE),
280         [&type](MessageParcel &data) { return ITypesUtil::Marshal(data, type); });
281 }
282 
ExitCurrentInputType()283 int32_t InputMethodSystemAbilityProxy::ExitCurrentInputType()
284 {
285     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::EXIT_CURRENT_INPUT_TYPE));
286 }
287 
IsPanelShown(const PanelInfo & panelInfo,bool & isShown)288 int32_t InputMethodSystemAbilityProxy::IsPanelShown(const PanelInfo &panelInfo, bool &isShown)
289 {
290     return SendRequest(
291         static_cast<uint32_t>(InputMethodInterfaceCode::IS_PANEL_SHOWN),
292         [&panelInfo](MessageParcel &data) { return ITypesUtil::Marshal(data, panelInfo); },
293         [&isShown](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isShown); });
294 }
295 
IsDefaultIme()296 int32_t InputMethodSystemAbilityProxy::IsDefaultIme()
297 {
298     return SendRequest(static_cast<uint32_t>(InputMethodInterfaceCode::IS_DEFAULT_IME));
299 }
300 
GetMessageOption(int32_t code,MessageOption & option)301 void InputMethodSystemAbilityProxy::GetMessageOption(int32_t code, MessageOption &option)
302 {
303     switch (code) {
304         case static_cast<int32_t>(InputMethodInterfaceCode::PANEL_STATUS_CHANGE): {
305             IMSA_HILOGD("Async IPC.");
306             option.SetFlags(MessageOption::TF_ASYNC);
307             break;
308         }
309         default:
310             option.SetFlags(MessageOption::TF_SYNC);
311             break;
312     }
313 }
314 
SendRequest(int code,ParcelHandler input,ParcelHandler output)315 int32_t InputMethodSystemAbilityProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
316 {
317     IMSA_HILOGD("IMSAProxy, code = %{public}d.", code);
318     MessageParcel data;
319     MessageParcel reply;
320     MessageOption option;
321     GetMessageOption(code, option);
322 
323     if (!data.WriteInterfaceToken(GetDescriptor())) {
324         IMSA_HILOGE("write interface token failed!");
325         return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
326     }
327     if (input != nullptr && (!input(data))) {
328         IMSA_HILOGE("write data failed!");
329         return ErrorCode::ERROR_EX_PARCELABLE;
330     }
331     auto remote = Remote();
332     if (remote == nullptr) {
333         IMSA_HILOGE("remote is nullptr!");
334         return ErrorCode::ERROR_EX_NULL_POINTER;
335     }
336     auto ret = remote->SendRequest(code, data, reply, option);
337     if (ret != NO_ERROR) {
338         IMSA_HILOGE("failed to send request, code: %{public}d, ret %{public}d!", code, ret);
339         return ret;
340     }
341     if (option.GetFlags() == MessageOption::TF_ASYNC) {
342         return ErrorCode::NO_ERROR;
343     }
344     ret = reply.ReadInt32();
345     if (ret != NO_ERROR) {
346         IMSA_HILOGE("dispose failed in service, code: %{public}d, ret: %{public}d!", code, ret);
347         return ret;
348     }
349     if (output != nullptr && (!output(reply))) {
350         IMSA_HILOGE("reply parcel error!");
351         return ErrorCode::ERROR_EX_PARCELABLE;
352     }
353     return ErrorCode::NO_ERROR;
354 }
355 } // namespace MiscServices
356 } // namespace OHOS