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 HILOG_DEBUG();
50 memberFuncMap_[static_cast<uint32_t>(AccessibilityInterfaceCode::INIT)] =
51 &AccessibleAbilityClientStub::HandleInit;
52 memberFuncMap_[static_cast<uint32_t>(AccessibilityInterfaceCode::DISCONNECT)] =
53 &AccessibleAbilityClientStub::HandleDisconnect;
54 memberFuncMap_[static_cast<uint32_t>(AccessibilityInterfaceCode::ON_ACCESSIBILITY_EVENT)] =
55 &AccessibleAbilityClientStub::HandleOnAccessibilityEvent;
56 memberFuncMap_[static_cast<uint32_t>(AccessibilityInterfaceCode::ON_KEY_PRESS_EVENT)] =
57 &AccessibleAbilityClientStub::HandleOnKeyPressEvent;
58 }
59
~AccessibleAbilityClientStub()60 AccessibleAbilityClientStub::~AccessibleAbilityClientStub()
61 {
62 HILOG_DEBUG();
63 memberFuncMap_.clear();
64 }
65
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)66 int AccessibleAbilityClientStub::OnRemoteRequest(uint32_t code,
67 MessageParcel &data, MessageParcel &reply, MessageOption &option)
68 {
69 HILOG_DEBUG("AccessibleAbilityClientStub::OnRemoteRequest, cmd = %d, flags= %d", code, option.GetFlags());
70 std::u16string descriptor = AccessibleAbilityClientStub::GetDescriptor();
71 std::u16string remote = data.ReadInterfaceToken();
72 if (descriptor != remote) {
73 HILOG_ERROR("local descriptor is not equal to remote descriptor");
74 return ERR_INVALID_STATE;
75 }
76
77 ErrCode result_code = ERR_NONE;
78 SWITCH_BEGIN(code)
79 ACCESSIBLE_ABILITY_CLIENT_STUB_CASES()
80 SWITCH_END()
81
82 if (result_code != ERR_CODE_DEFAULT) {
83 return result_code;
84 }
85
86 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
87 }
88
HandleInit(MessageParcel & data,MessageParcel & reply)89 ErrCode AccessibleAbilityClientStub::HandleInit(MessageParcel &data, MessageParcel &reply)
90 {
91 HILOG_DEBUG();
92 sptr<IRemoteObject> remote = data.ReadRemoteObject();
93 if (remote == nullptr) {
94 HILOG_ERROR("object is nullptr.");
95 return ERR_INVALID_VALUE;
96 }
97
98 sptr<IAccessibleAbilityChannel> channel = iface_cast<IAccessibleAbilityChannel>(remote);
99 if (channel == nullptr) {
100 HILOG_ERROR("channel is nullptr.");
101 return ERR_INVALID_VALUE;
102 }
103 int32_t channelId = data.ReadInt32();
104
105 Init(channel, channelId);
106 return NO_ERROR;
107 }
108
HandleDisconnect(MessageParcel & data,MessageParcel & reply)109 ErrCode AccessibleAbilityClientStub::HandleDisconnect(MessageParcel &data, MessageParcel &reply)
110 {
111 HILOG_DEBUG();
112 int32_t channelId = data.ReadInt32();
113 Disconnect(channelId);
114 return NO_ERROR;
115 }
116
HandleOnAccessibilityEvent(MessageParcel & data,MessageParcel & reply)117 ErrCode AccessibleAbilityClientStub::HandleOnAccessibilityEvent(MessageParcel &data, MessageParcel &reply)
118 {
119 HILOG_DEBUG();
120 sptr<AccessibilityEventInfoParcel> eventInfo = data.ReadStrongParcelable<AccessibilityEventInfoParcel>();
121 if (!eventInfo) {
122 HILOG_ERROR("ReadStrongParcelable<AccessibilityEventInfo> failed");
123 return ERR_INVALID_VALUE;
124 }
125
126 OnAccessibilityEvent(*eventInfo);
127 return NO_ERROR;
128 }
129
HandleOnKeyPressEvent(MessageParcel & data,MessageParcel & reply)130 ErrCode AccessibleAbilityClientStub::HandleOnKeyPressEvent(MessageParcel &data, MessageParcel &reply)
131 {
132 HILOG_DEBUG();
133 int32_t sequence = data.ReadInt32();
134
135 std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
136 if (!keyEvent->ReadFromParcel(data)) {
137 HILOG_ERROR("keyEvent ReadFromParcel failed");
138 return ERR_INVALID_VALUE;
139 }
140 OnKeyPressEvent(*keyEvent, sequence);
141 return NO_ERROR;
142 }
143 } // namespace Accessibility
144 } // namespace OHOS