• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
24 
25 constexpr int32_t SINGLE_TRANSMIT = -2;
26 constexpr int32_t MULTI_TRANSMIT_FINISH = -1;
27 
WriteData(std::vector<AccessibilityElementInfo> & infos)28 void StoreElementData::WriteData(std::vector<AccessibilityElementInfo> &infos)
29 {
30     HILOG_DEBUG();
31     std::lock_guard<std::mutex> lock(mutex_);
32     storeData_.insert(storeData_.end(), infos.begin(), infos.end());
33 }
34 
Clear()35 void StoreElementData::Clear()
36 {
37     HILOG_DEBUG();
38     std::lock_guard<std::mutex> lock(mutex_);
39     storeData_.clear();
40 }
41 
ReadData()42 std::vector<AccessibilityElementInfo> StoreElementData::ReadData()
43 {
44     HILOG_DEBUG();
45     std::lock_guard<std::mutex> lock(mutex_);
46     return storeData_;
47 }
48 
Size()49 size_t StoreElementData::Size()
50 {
51     HILOG_DEBUG();
52     std::lock_guard<std::mutex> lock(mutex_);
53     return storeData_.size();
54 }
55 
AccessibilityElementOperatorCallbackStub()56 AccessibilityElementOperatorCallbackStub::AccessibilityElementOperatorCallbackStub()
57 {
58     memberFuncMap_[static_cast<uint32_t>(
59         AccessibilityInterfaceCode::SET_RESULT_BY_ACCESSIBILITY_ID)] =
60         &AccessibilityElementOperatorCallbackStub::HandleSetSearchElementInfoByAccessibilityIdResult;
61     memberFuncMap_[static_cast<uint32_t>(AccessibilityInterfaceCode::SET_RESULT_BY_TEXT)] =
62         &AccessibilityElementOperatorCallbackStub::HandleSetSearchElementInfoByTextResult;
63     memberFuncMap_[static_cast<uint32_t>(
64         AccessibilityInterfaceCode::SET_RESULT_FOCUSED_INFO)] =
65         &AccessibilityElementOperatorCallbackStub::HandleSetFindFocusedElementInfoResult;
66     memberFuncMap_[static_cast<uint32_t>(AccessibilityInterfaceCode::SET_RESULT_FOCUS_MOVE)] =
67         &AccessibilityElementOperatorCallbackStub::HandleSetFocusMoveSearchResult;
68     memberFuncMap_[static_cast<uint32_t>(
69         AccessibilityInterfaceCode::SET_RESULT_PERFORM_ACTION)] =
70         &AccessibilityElementOperatorCallbackStub::HandleSetExecuteActionResult;
71 }
72 
73 StoreElementData AccessibilityElementOperatorCallbackStub::storeElementData;
74 
~AccessibilityElementOperatorCallbackStub()75 AccessibilityElementOperatorCallbackStub::~AccessibilityElementOperatorCallbackStub()
76 {
77     HILOG_DEBUG();
78     memberFuncMap_.clear();
79 }
80 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)81 int AccessibilityElementOperatorCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
82     MessageParcel &reply, MessageOption &option)
83 {
84     HILOG_DEBUG("cmd = %{public}u, flags= %{public}d", code, option.GetFlags());
85     std::u16string descriptor = AccessibilityElementOperatorCallbackStub::GetDescriptor();
86     std::u16string remoteDescriptor = data.ReadInterfaceToken();
87     if (descriptor != remoteDescriptor) {
88         HILOG_ERROR("AccessibilityElementOperatorCallbackStub::OnRemoteRequest"
89             "local descriptor is not equal to remote");
90         return ERR_INVALID_STATE;
91     }
92 
93     auto memFunc = memberFuncMap_.find(code);
94     if (memFunc != memberFuncMap_.end()) {
95         auto func = memFunc->second;
96         if (func != nullptr) {
97             return (this->*func)(data, reply);
98         }
99     }
100     HILOG_WARN("AccessibilityElementOperatorCallbackStub::OnRemoteRequest, default case, need check.");
101     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
102 }
103 
HandleSetSearchElementInfoByAccessibilityIdResult(MessageParcel & data,MessageParcel & reply)104 ErrCode AccessibilityElementOperatorCallbackStub::HandleSetSearchElementInfoByAccessibilityIdResult(
105     MessageParcel &data, MessageParcel &reply)
106 {
107     HILOG_DEBUG();
108     int32_t flag = data.ReadInt32();
109     if (flag == SINGLE_TRANSMIT || flag == 0) {
110         storeElementData.Clear();
111     }
112 
113     int32_t accessibilityInfosize = data.ReadInt32();
114     std::vector<AccessibilityElementInfo> tmpData;
115     ContainerSecurityVerify(data, accessibilityInfosize, tmpData.max_size());
116     for (int32_t i = 0; i < accessibilityInfosize; i++) {
117         sptr<AccessibilityElementInfoParcel> accessibilityInfo =
118             data.ReadStrongParcelable<AccessibilityElementInfoParcel>();
119         if (!accessibilityInfo) {
120             HILOG_ERROR("ReadStrongParcelable<accessibilityInfo> failed");
121             storeElementData.Clear();
122             reply.WriteInt32(RET_ERR_FAILED);
123             return TRANSACTION_ERR;
124         }
125         tmpData.push_back(*accessibilityInfo);
126     }
127 
128     storeElementData.WriteData(tmpData); // get all data and push once
129     int32_t requestId = data.ReadInt32();
130     if (flag == SINGLE_TRANSMIT || flag == MULTI_TRANSMIT_FINISH) {
131         reply.WriteInt32(0);
132         HILOG_DEBUG("infos size %{public}zu, requestId %{public}d", storeElementData.Size(), requestId);
133         SetSearchElementInfoByAccessibilityIdResult(storeElementData.ReadData(), requestId);
134         return NO_ERROR;
135     }
136 
137     reply.WriteInt32(0);
138     return NO_ERROR;
139 }
140 
HandleSetSearchElementInfoByTextResult(MessageParcel & data,MessageParcel & reply)141 ErrCode AccessibilityElementOperatorCallbackStub::HandleSetSearchElementInfoByTextResult(
142     MessageParcel &data, MessageParcel &reply)
143 {
144     HILOG_DEBUG();
145     std::vector<AccessibilityElementInfo> infos {};
146     int32_t accessibilityInfosize = data.ReadInt32();
147     ContainerSecurityVerify(data, accessibilityInfosize, infos.max_size());
148     for (int32_t i = 0; i < accessibilityInfosize; i++) {
149         sptr<AccessibilityElementInfoParcel> accessibilityInfo =
150             data.ReadStrongParcelable<AccessibilityElementInfoParcel>();
151         if (!accessibilityInfo) {
152             HILOG_ERROR("ReadStrongParcelable<accessibilityInfo> failed");
153             return TRANSACTION_ERR;
154         }
155         infos.emplace_back(*accessibilityInfo);
156     }
157     int32_t requestId = data.ReadInt32();
158 
159     SetSearchElementInfoByTextResult(infos, requestId);
160 
161     return NO_ERROR;
162 }
163 
HandleSetFindFocusedElementInfoResult(MessageParcel & data,MessageParcel & reply)164 ErrCode AccessibilityElementOperatorCallbackStub::HandleSetFindFocusedElementInfoResult(MessageParcel &data,
165     MessageParcel &reply)
166 {
167     HILOG_DEBUG();
168     sptr<AccessibilityElementInfoParcel> info = data.ReadStrongParcelable<AccessibilityElementInfoParcel>();
169     if (!info) {
170         HILOG_ERROR("ReadStrongParcelable<AccessibilityElementInfo> failed");
171         return TRANSACTION_ERR;
172     }
173 
174     int32_t requestId = data.ReadInt32();
175 
176     SetFindFocusedElementInfoResult(*info, requestId);
177 
178     return NO_ERROR;
179 }
180 
HandleSetFocusMoveSearchResult(MessageParcel & data,MessageParcel & reply)181 ErrCode AccessibilityElementOperatorCallbackStub::HandleSetFocusMoveSearchResult(MessageParcel &data,
182     MessageParcel &reply)
183 {
184     HILOG_DEBUG();
185     sptr<AccessibilityElementInfoParcel> info = data.ReadStrongParcelable<AccessibilityElementInfoParcel>();
186     if (!info) {
187         HILOG_ERROR("ReadStrongParcelable<AccessibilityElementInfo> failed");
188         return TRANSACTION_ERR;
189     }
190 
191     int32_t requestId = data.ReadInt32();
192 
193     SetFocusMoveSearchResult(*info, requestId);
194 
195     return NO_ERROR;
196 }
197 
HandleSetExecuteActionResult(MessageParcel & data,MessageParcel & reply)198 ErrCode AccessibilityElementOperatorCallbackStub::HandleSetExecuteActionResult(MessageParcel &data,
199     MessageParcel &reply)
200 {
201     HILOG_DEBUG();
202 
203     bool succeeded = data.ReadBool();
204     int32_t requestId = data.ReadInt32();
205 
206     SetExecuteActionResult(succeeded, requestId);
207 
208     return NO_ERROR;
209 }
210 } // namespace Accessibility
211 } // namespace OHOS