1 /*
2  * Copyright (C) 2022 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 "accessible_ability_client_proxy.h"
17 #include "accessibility_element_info_parcel.h"
18 #include "accessibility_event_info_parcel.h"
19 #include "accessibility_ipc_interface_code.h"
20 #include "hilog_wrapper.h"
21 
22 namespace OHOS {
23 namespace Accessibility {
AccessibleAbilityClientProxy(const sptr<IRemoteObject> & object)24 AccessibleAbilityClientProxy::AccessibleAbilityClientProxy(const sptr<IRemoteObject> &object)
25     : IRemoteProxy<IAccessibleAbilityClient>(object)
26 {
27 }
28 
WriteInterfaceToken(MessageParcel & data)29 bool AccessibleAbilityClientProxy::WriteInterfaceToken(MessageParcel &data)
30 {
31     HILOG_DEBUG();
32     if (!data.WriteInterfaceToken(AccessibleAbilityClientProxy::GetDescriptor())) {
33         HILOG_ERROR("write interface token failed");
34         return false;
35     }
36     return true;
37 }
38 
SendTransactCmd(AccessibilityInterfaceCode code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39 bool AccessibleAbilityClientProxy::SendTransactCmd(AccessibilityInterfaceCode code, MessageParcel &data,
40     MessageParcel &reply,  MessageOption &option)
41 {
42     HILOG_DEBUG();
43 
44     sptr<IRemoteObject> remoteClientProxy = Remote();
45     if (remoteClientProxy == nullptr) {
46         HILOG_ERROR("fail to send transact cmd %{public}d due to remote object", code);
47         return false;
48     }
49     int32_t resultClientProxy =
50         remoteClientProxy->SendRequest(static_cast<uint32_t>(code), data, reply, option);
51     if (resultClientProxy != NO_ERROR) {
52         HILOG_ERROR("receive error transact code %{public}d in transact cmd %{public}d", resultClientProxy, code);
53         return false;
54     }
55     return true;
56 }
57 
Init(const sptr<IAccessibleAbilityChannel> & channel,const int32_t channelId)58 void AccessibleAbilityClientProxy::Init(const sptr<IAccessibleAbilityChannel> &channel, const int32_t channelId)
59 {
60     MessageParcel data;
61     MessageParcel reply;
62     MessageOption option(MessageOption::TF_ASYNC);
63 
64     HILOG_DEBUG();
65 
66     if (!WriteInterfaceToken(data)) {
67         return;
68     }
69     if (channel == nullptr) {
70         HILOG_ERROR("channel is null.");
71         return;
72     }
73     if (!data.WriteRemoteObject(channel->AsObject())) {
74         HILOG_ERROR("fail, channel write parcelable error");
75         return;
76     }
77 
78     if (!data.WriteInt32(channelId)) {
79         HILOG_ERROR("fail, channelId write int32 error");
80         return;
81     }
82 
83     if (!SendTransactCmd(AccessibilityInterfaceCode::INIT, data, reply, option)) {
84         HILOG_ERROR("Init fail");
85         return;
86     }
87 }
88 
Disconnect(const int32_t channelId)89 void AccessibleAbilityClientProxy::Disconnect(const int32_t channelId)
90 {
91     MessageParcel data;
92     MessageParcel reply;
93     MessageOption option(MessageOption::TF_SYNC);
94 
95     HILOG_DEBUG();
96 
97     if (!WriteInterfaceToken(data)) {
98         return;
99     }
100 
101     if (!data.WriteInt32(channelId)) {
102         HILOG_ERROR("fail, channelId write int32 error");
103         return;
104     }
105 
106     if (!SendTransactCmd(AccessibilityInterfaceCode::DISCONNECT, data, reply, option)) {
107         HILOG_ERROR("Disconnect fail");
108         return;
109     }
110 }
111 
OnAccessibilityEvent(const AccessibilityEventInfo & eventInfo)112 void AccessibleAbilityClientProxy::OnAccessibilityEvent(const AccessibilityEventInfo &eventInfo)
113 {
114     MessageParcel data;
115     MessageParcel reply;
116     MessageOption option(MessageOption::TF_ASYNC);
117     AccessibilityEventInfoParcel eventInfoParcel(eventInfo);
118 
119     HILOG_DEBUG();
120 
121     if (!WriteInterfaceToken(data)) {
122         return;
123     }
124     if (!data.WriteParcelable(&eventInfoParcel)) {
125         HILOG_ERROR("fail, eventInfo write parcelable error");
126         return;
127     }
128     if (!SendTransactCmd(AccessibilityInterfaceCode::ON_ACCESSIBILITY_EVENT, data, reply, option)) {
129         HILOG_ERROR("OnAccessibilityEvent fail");
130         return;
131     }
132 }
133 
OnKeyPressEvent(const MMI::KeyEvent & keyEvent,const int32_t sequence)134 void AccessibleAbilityClientProxy::OnKeyPressEvent(const MMI::KeyEvent &keyEvent, const int32_t sequence)
135 {
136     MessageParcel data;
137     MessageParcel reply;
138     MessageOption option(MessageOption::TF_ASYNC);
139 
140     HILOG_DEBUG();
141 
142     if (!WriteInterfaceToken(data)) {
143         return;
144     }
145     if (!data.WriteInt32(sequence)) {
146         HILOG_ERROR("fail, sequence write int32 error");
147         return;
148     }
149 
150     if (!keyEvent.WriteToParcel(data)) {
151         HILOG_ERROR("fail, keyEvent WriteToParcel error");
152         return;
153     }
154 
155     if (!SendTransactCmd(AccessibilityInterfaceCode::ON_KEY_PRESS_EVENT, data, reply, option)) {
156         HILOG_ERROR("OnKeyPressEvent fail");
157         return;
158     }
159 }
160 } // namespace Accessibility
161 } // namespace OHOS