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 "accessibility_element_operator_callback_stub.h"
17 #include "accessibility_element_info_parcel.h"
18 #include "accessibility_ipc_interface_code.h"
19 #include "hilog_wrapper.h"
20 #include "parcel_util.h"
21
22 namespace OHOS {
23 namespace Accessibility {
AccessibilityElementOperatorCallbackStub()24 AccessibilityElementOperatorCallbackStub::AccessibilityElementOperatorCallbackStub()
25 {
26 memberFuncMap_[static_cast<uint32_t>(
27 AccessibilityInterfaceCode::SET_RESULT_BY_ACCESSIBILITY_ID)] =
28 &AccessibilityElementOperatorCallbackStub::HandleSetSearchElementInfoByAccessibilityIdResult;
29 memberFuncMap_[static_cast<uint32_t>(AccessibilityInterfaceCode::SET_RESULT_BY_TEXT)] =
30 &AccessibilityElementOperatorCallbackStub::HandleSetSearchElementInfoByTextResult;
31 memberFuncMap_[static_cast<uint32_t>(
32 AccessibilityInterfaceCode::SET_RESULT_FOCUSED_INFO)] =
33 &AccessibilityElementOperatorCallbackStub::HandleSetFindFocusedElementInfoResult;
34 memberFuncMap_[static_cast<uint32_t>(AccessibilityInterfaceCode::SET_RESULT_FOCUS_MOVE)] =
35 &AccessibilityElementOperatorCallbackStub::HandleSetFocusMoveSearchResult;
36 memberFuncMap_[static_cast<uint32_t>(
37 AccessibilityInterfaceCode::SET_RESULT_PERFORM_ACTION)] =
38 &AccessibilityElementOperatorCallbackStub::HandleSetExecuteActionResult;
39 }
40
~AccessibilityElementOperatorCallbackStub()41 AccessibilityElementOperatorCallbackStub::~AccessibilityElementOperatorCallbackStub()
42 {
43 HILOG_DEBUG();
44 memberFuncMap_.clear();
45 }
46
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)47 int AccessibilityElementOperatorCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
48 MessageParcel &reply, MessageOption &option)
49 {
50 HILOG_DEBUG("cmd = %{public}u, flags= %{public}d", code, option.GetFlags());
51 std::u16string descriptor = AccessibilityElementOperatorCallbackStub::GetDescriptor();
52 std::u16string remoteDescriptor = data.ReadInterfaceToken();
53 if (descriptor != remoteDescriptor) {
54 HILOG_ERROR("AccessibilityElementOperatorCallbackStub::OnRemoteRequest"
55 "local descriptor is not equal to remote");
56 return ERR_INVALID_STATE;
57 }
58
59 auto memFunc = memberFuncMap_.find(code);
60 if (memFunc != memberFuncMap_.end()) {
61 auto func = memFunc->second;
62 if (func != nullptr) {
63 return (this->*func)(data, reply);
64 }
65 }
66 HILOG_WARN("AccessibilityElementOperatorCallbackStub::OnRemoteRequest, default case, need check.");
67 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
68 }
69
HandleSetSearchElementInfoByAccessibilityIdResult(MessageParcel & data,MessageParcel & reply)70 ErrCode AccessibilityElementOperatorCallbackStub::HandleSetSearchElementInfoByAccessibilityIdResult(
71 MessageParcel &data, MessageParcel &reply)
72 {
73 HILOG_DEBUG();
74 std::vector<AccessibilityElementInfo> infos {};
75 int32_t accessibilityInfosize = data.ReadInt32();
76 ContainerSecurityVerify(data, accessibilityInfosize, infos.max_size());
77 for (int32_t i = 0; i < accessibilityInfosize; i++) {
78 sptr<AccessibilityElementInfoParcel> accessibilityInfo =
79 data.ReadStrongParcelable<AccessibilityElementInfoParcel>();
80 if (!accessibilityInfo) {
81 HILOG_ERROR("ReadStrongParcelable<accessibilityInfo> failed");
82 return TRANSACTION_ERR;
83 }
84 infos.emplace_back(*accessibilityInfo);
85 }
86 int32_t requestId = data.ReadInt32();
87
88 SetSearchElementInfoByAccessibilityIdResult(infos, requestId);
89 return NO_ERROR;
90 }
91
HandleSetSearchElementInfoByTextResult(MessageParcel & data,MessageParcel & reply)92 ErrCode AccessibilityElementOperatorCallbackStub::HandleSetSearchElementInfoByTextResult(
93 MessageParcel &data, MessageParcel &reply)
94 {
95 HILOG_DEBUG();
96 std::vector<AccessibilityElementInfo> infos {};
97 int32_t accessibilityInfosize = data.ReadInt32();
98 ContainerSecurityVerify(data, accessibilityInfosize, infos.max_size());
99 for (int32_t i = 0; i < accessibilityInfosize; i++) {
100 sptr<AccessibilityElementInfoParcel> accessibilityInfo =
101 data.ReadStrongParcelable<AccessibilityElementInfoParcel>();
102 if (!accessibilityInfo) {
103 HILOG_ERROR("ReadStrongParcelable<accessibilityInfo> failed");
104 return TRANSACTION_ERR;
105 }
106 infos.emplace_back(*accessibilityInfo);
107 }
108 int32_t requestId = data.ReadInt32();
109
110 SetSearchElementInfoByTextResult(infos, requestId);
111
112 return NO_ERROR;
113 }
114
HandleSetFindFocusedElementInfoResult(MessageParcel & data,MessageParcel & reply)115 ErrCode AccessibilityElementOperatorCallbackStub::HandleSetFindFocusedElementInfoResult(MessageParcel &data,
116 MessageParcel &reply)
117 {
118 HILOG_DEBUG();
119 sptr<AccessibilityElementInfoParcel> info = data.ReadStrongParcelable<AccessibilityElementInfoParcel>();
120 if (!info) {
121 HILOG_ERROR("ReadStrongParcelable<AccessibilityElementInfo> failed");
122 return TRANSACTION_ERR;
123 }
124
125 int32_t requestId = data.ReadInt32();
126
127 SetFindFocusedElementInfoResult(*info, requestId);
128
129 return NO_ERROR;
130 }
131
HandleSetFocusMoveSearchResult(MessageParcel & data,MessageParcel & reply)132 ErrCode AccessibilityElementOperatorCallbackStub::HandleSetFocusMoveSearchResult(MessageParcel &data,
133 MessageParcel &reply)
134 {
135 HILOG_DEBUG();
136 sptr<AccessibilityElementInfoParcel> info = data.ReadStrongParcelable<AccessibilityElementInfoParcel>();
137 if (!info) {
138 HILOG_ERROR("ReadStrongParcelable<AccessibilityElementInfo> failed");
139 return TRANSACTION_ERR;
140 }
141
142 int32_t requestId = data.ReadInt32();
143
144 SetFocusMoveSearchResult(*info, requestId);
145
146 return NO_ERROR;
147 }
148
HandleSetExecuteActionResult(MessageParcel & data,MessageParcel & reply)149 ErrCode AccessibilityElementOperatorCallbackStub::HandleSetExecuteActionResult(MessageParcel &data,
150 MessageParcel &reply)
151 {
152 HILOG_DEBUG();
153
154 bool succeeded = data.ReadBool();
155 int32_t requestId = data.ReadInt32();
156
157 SetExecuteActionResult(succeeded, requestId);
158
159 return NO_ERROR;
160 }
161 } // namespace Accessibility
162 } // namespace OHOS