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_stub.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 #define SWITCH_BEGIN(code) switch (code) {
23 #define SWITCH_CASE(case_code, func) case case_code:\
24     {\
25         result_code = func(data, reply);\
26         break;\
27     }
28 
29 #define SWITCH_END() default:\
30     {\
31         result_code = ERR_CODE_DEFAULT;\
32         HILOG_WARN("AccessibleAbilityClientStub::OnRemoteRequest, default case, need check.");\
33         break;\
34     }\
35 }
36 
37 #define ACCESSIBLE_ABILITY_CLIENT_STUB_CASES() \
38     SWITCH_CASE(AccessibilityInterfaceCode::INIT, HandleInit)\
39     SWITCH_CASE(AccessibilityInterfaceCode::DISCONNECT, HandleDisconnect)\
40     SWITCH_CASE(AccessibilityInterfaceCode::ON_ACCESSIBILITY_EVENT, HandleOnAccessibilityEvent)\
41     SWITCH_CASE(AccessibilityInterfaceCode::ON_KEY_PRESS_EVENT, HandleOnKeyPressEvent)\
42 
43 namespace OHOS {
44 namespace Accessibility {
45 constexpr int32_t ERR_CODE_DEFAULT = -1000;
46 
AccessibleAbilityClientStub()47 AccessibleAbilityClientStub::AccessibleAbilityClientStub()
48 {
49 }
50 
~AccessibleAbilityClientStub()51 AccessibleAbilityClientStub::~AccessibleAbilityClientStub()
52 {
53 }
54 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)55 int AccessibleAbilityClientStub::OnRemoteRequest(uint32_t code,
56     MessageParcel &data, MessageParcel &reply, MessageOption &option)
57 {
58     HILOG_DEBUG("AccessibleAbilityClientStub::OnRemoteRequest, cmd = %d, flags= %d", code, option.GetFlags());
59     std::u16string descriptor = AccessibleAbilityClientStub::GetDescriptor();
60     std::u16string remote = data.ReadInterfaceToken();
61     if (descriptor != remote) {
62         HILOG_ERROR("local descriptor is not equal to remote descriptor");
63         return ERR_INVALID_STATE;
64     }
65 
66     ErrCode result_code = ERR_NONE;
67     SWITCH_BEGIN(code)
68     ACCESSIBLE_ABILITY_CLIENT_STUB_CASES()
69     SWITCH_END()
70 
71     if (result_code != ERR_CODE_DEFAULT) {
72         return result_code;
73     }
74 
75     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
76 }
77 
HandleInit(MessageParcel & data,MessageParcel & reply)78 ErrCode AccessibleAbilityClientStub::HandleInit(MessageParcel &data, MessageParcel &reply)
79 {
80     HILOG_DEBUG();
81     sptr<IRemoteObject> remote = data.ReadRemoteObject();
82     if (!remote) {
83         HILOG_ERROR("object is nullptr.");
84         return ERR_INVALID_VALUE;
85     }
86 
87     sptr<IAccessibleAbilityChannel> channel = iface_cast<IAccessibleAbilityChannel>(remote);
88     if (!channel) {
89         HILOG_ERROR("channel is nullptr.");
90         return ERR_INVALID_VALUE;
91     }
92     int32_t channelId = data.ReadInt32();
93 
94     Init(channel, channelId);
95     return NO_ERROR;
96 }
97 
HandleDisconnect(MessageParcel & data,MessageParcel & reply)98 ErrCode AccessibleAbilityClientStub::HandleDisconnect(MessageParcel &data, MessageParcel &reply)
99 {
100     HILOG_DEBUG();
101     int32_t channelId = data.ReadInt32();
102     Disconnect(channelId);
103     return NO_ERROR;
104 }
105 
HandleOnAccessibilityEvent(MessageParcel & data,MessageParcel & reply)106 ErrCode AccessibleAbilityClientStub::HandleOnAccessibilityEvent(MessageParcel &data, MessageParcel &reply)
107 {
108     HILOG_DEBUG();
109     sptr<AccessibilityEventInfoParcel> eventInfo = data.ReadStrongParcelable<AccessibilityEventInfoParcel>();
110     if (eventInfo == nullptr) {
111         HILOG_ERROR("ReadStrongParcelable<AccessibilityEventInfo> failed");
112         return ERR_INVALID_VALUE;
113     }
114 
115     OnAccessibilityEvent(*eventInfo);
116     return NO_ERROR;
117 }
118 
HandleOnKeyPressEvent(MessageParcel & data,MessageParcel & reply)119 ErrCode AccessibleAbilityClientStub::HandleOnKeyPressEvent(MessageParcel &data, MessageParcel &reply)
120 {
121     HILOG_DEBUG();
122     int32_t sequence = data.ReadInt32();
123 
124     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
125     if (keyEvent == nullptr) {
126         HILOG_ERROR("keyEvent is nullptr");
127         return ERR_INVALID_VALUE;
128     }
129 
130     if (!keyEvent->ReadFromParcel(data)) {
131         HILOG_ERROR("keyEvent ReadFromParcel failed");
132         return ERR_INVALID_VALUE;
133     }
134     OnKeyPressEvent(*keyEvent, sequence);
135     return NO_ERROR;
136 }
137 } // namespace Accessibility
138 } // namespace OHOS