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_core_proxy.h"
17 
18 #include <string_ex.h>
19 
20 #include "input_attribute.h"
21 #include "itypes_util.h"
22 #include "message_option.h"
23 #include "message_parcel.h"
24 
25 namespace OHOS {
26 namespace MiscServices {
InputMethodCoreProxy(const OHOS::sptr<OHOS::IRemoteObject> & impl)27 InputMethodCoreProxy::InputMethodCoreProxy(const OHOS::sptr<OHOS::IRemoteObject> &impl)
28     : IRemoteProxy<IInputMethodCore>(impl)
29 {
30 }
31 
32 InputMethodCoreProxy::~InputMethodCoreProxy() = default;
33 
InitInputControlChannel(const sptr<IInputControlChannel> & inputControlChannel)34 int32_t InputMethodCoreProxy::InitInputControlChannel(const sptr<IInputControlChannel> &inputControlChannel)
35 {
36     return SendRequest(INIT_INPUT_CONTROL_CHANNEL, [&inputControlChannel](MessageParcel &data) {
37         return ITypesUtil::Marshal(data, inputControlChannel->AsObject());
38     });
39 }
40 
StartInput(const InputClientInfo & clientInfo,bool isBindFromClient)41 int32_t InputMethodCoreProxy::StartInput(const InputClientInfo &clientInfo, bool isBindFromClient)
42 {
43     IMSA_HILOGI("CoreProxy start.");
44     return SendRequest(START_INPUT, [&clientInfo, isBindFromClient](MessageParcel &data) {
45         return ITypesUtil::Marshal(data, isBindFromClient, clientInfo, clientInfo.channel);
46     });
47 }
48 
OnSecurityChange(int32_t security)49 int32_t InputMethodCoreProxy::OnSecurityChange(int32_t security)
50 {
51     return SendRequest(SECURITY_CHANGE, [security](MessageParcel& data) {
52         return ITypesUtil::Marshal(data, security);
53     });
54 }
55 
OnConnectSystemCmd(const sptr<IRemoteObject> & channel,sptr<IRemoteObject> & agent)56 int32_t InputMethodCoreProxy::OnConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent)
57 {
58     return SendRequest(
59         ON_CONNECT_SYSTEM_CMD, [channel](MessageParcel &data) { return data.WriteRemoteObject(channel); },
60         [&agent](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, agent); });
61 }
62 
StopInputService(bool isTerminateIme)63 int32_t InputMethodCoreProxy::StopInputService(bool isTerminateIme)
64 {
65     return SendRequest(STOP_INPUT_SERVICE,
66         [isTerminateIme](MessageParcel &data) { return ITypesUtil::Marshal(data, isTerminateIme); });
67 }
68 
ShowKeyboard()69 int32_t InputMethodCoreProxy::ShowKeyboard()
70 {
71     return SendRequest(SHOW_KEYBOARD);
72 }
73 
HideKeyboard()74 int32_t InputMethodCoreProxy::HideKeyboard()
75 {
76     return SendRequest(HIDE_KEYBOARD);
77 }
78 
SetSubtype(const SubProperty & property)79 int32_t InputMethodCoreProxy::SetSubtype(const SubProperty &property)
80 {
81     return SendRequest(SET_SUBTYPE, [&property](MessageParcel &data) { return ITypesUtil::Marshal(data, property); });
82 }
83 
StopInput(const sptr<IRemoteObject> & channel)84 int32_t InputMethodCoreProxy::StopInput(const sptr<IRemoteObject> &channel)
85 {
86     return SendRequest(STOP_INPUT, [&channel](MessageParcel &data) { return ITypesUtil::Marshal(data, channel); });
87 }
88 
IsEnable()89 bool InputMethodCoreProxy::IsEnable()
90 {
91     bool isEnable = false;
92     SendRequest(
93         IS_ENABLE, nullptr, [&isEnable](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isEnable); });
94     return isEnable;
95 }
96 
IsPanelShown(const PanelInfo & panelInfo,bool & isShown)97 int32_t InputMethodCoreProxy::IsPanelShown(const PanelInfo &panelInfo, bool &isShown)
98 {
99     return SendRequest(
100         IS_PANEL_SHOWN, [&panelInfo](MessageParcel &data) { return ITypesUtil::Marshal(data, panelInfo); },
101         [&isShown](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isShown); });
102 }
103 
OnClientInactive(const sptr<IRemoteObject> & channel)104 void InputMethodCoreProxy::OnClientInactive(const sptr<IRemoteObject> &channel)
105 {
106     SendRequest(
107         ON_CLIENT_INACTIVE, [&channel](MessageParcel &data) { return ITypesUtil::Marshal(data, channel); }, nullptr,
108         MessageOption::TF_ASYNC);
109 }
110 
SendRequest(int code,ParcelHandler input,ParcelHandler output,MessageOption option)111 int32_t InputMethodCoreProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output, MessageOption option)
112 {
113     IMSA_HILOGD("InputMethodCoreProxy, run in, code = %{public}d.", code);
114     MessageParcel data;
115     MessageParcel reply;
116     if (!data.WriteInterfaceToken(GetDescriptor())) {
117         IMSA_HILOGE("InputMethodCoreProxy::write interface token failed!");
118         return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
119     }
120     if (input != nullptr && (!input(data))) {
121         IMSA_HILOGE("InputMethodCoreProxy::write data failed!");
122         return ErrorCode::ERROR_EX_PARCELABLE;
123     }
124     auto remote = Remote();
125     if (remote == nullptr) {
126         IMSA_HILOGE("InputMethodCoreProxy::remote is nullptr!");
127         return ErrorCode::ERROR_EX_NULL_POINTER;
128     }
129     auto ret = remote->SendRequest(code, data, reply, option);
130     if (ret != NO_ERROR) {
131         IMSA_HILOGE("InputMethodCoreProxy send request failed, code: %{public}d, ret: %{public}d!", code, ret);
132         return ret;
133     }
134     ret = reply.ReadInt32();
135     if (ret != NO_ERROR) {
136         IMSA_HILOGE("InputMethodCoreProxy::reply error, ret: %{public}d!", ret);
137         return ret;
138     }
139     if (output != nullptr && (!output(reply))) {
140         IMSA_HILOGE("InputMethodCoreProxy::reply parcel error!");
141         return ErrorCode::ERROR_EX_PARCELABLE;
142     }
143     return ret;
144 }
145 } // namespace MiscServices
146 } // namespace OHOS
147