• 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 "accessible_ability_channel_client.h"
17 
18 #include <cinttypes>
19 #include <hitrace_meter.h>
20 
21 #include "accessibility_element_operator_callback_impl.h"
22 #include "hilog_wrapper.h"
23 
24 namespace OHOS {
25 namespace Accessibility {
26 namespace {
27     constexpr uint32_t TIME_OUT_OPERATOR = 5000;
28     constexpr int32_t REQUEST_ID_MAX = 0x0000FFFF;
29 } // namespace
30 
GenerateRequestId()31 int32_t AccessibleAbilityChannelClient::GenerateRequestId()
32 {
33     int32_t requestId = requestId_++;
34     requestId = requestId % REQUEST_ID_MAX;
35 
36     return requestId;
37 }
38 
GetRemote()39 sptr<IRemoteObject> AccessibleAbilityChannelClient::GetRemote()
40 {
41     return proxy_->AsObject();
42 }
43 
SetOnKeyPressEventResult(const bool handled,const int32_t sequence)44 void AccessibleAbilityChannelClient::SetOnKeyPressEventResult(const bool handled, const int32_t sequence)
45 {
46     HILOG_INFO("[channelId:%{public}d]", channelId_);
47     if (proxy_) {
48         proxy_->SetOnKeyPressEventResult(handled, sequence);
49     } else {
50         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
51     }
52 }
53 
FindFocusedElementInfo(int32_t accessibilityWindowId,int32_t elementId,int32_t focusType,AccessibilityElementInfo & elementInfo)54 RetError AccessibleAbilityChannelClient::FindFocusedElementInfo(int32_t accessibilityWindowId,
55     int32_t elementId, int32_t focusType, AccessibilityElementInfo &elementInfo)
56 {
57     HILOG_DEBUG("[channelId:%{public}d]", channelId_);
58     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "FindFocusedElement");
59     if (!proxy_) {
60         HILOG_ERROR("FindFocusedElementInfo Failed to connect to aams [channelId:%{public}d]",
61             channelId_);
62         return RET_ERR_SAMGR;
63     }
64 
65     int32_t requestId = GenerateRequestId();
66     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
67         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
68     if (!elementOperator) {
69         HILOG_ERROR("FindFocusedElementInfo Failed to create elementOperator.");
70         return RET_ERR_NULLPTR;
71     }
72     std::future<void> promiseFuture = elementOperator->promise_.get_future();
73 
74     int32_t windowId = accessibilityWindowId;
75     if (accessibilityWindowId == ANY_WINDOW_ID && focusType == FOCUS_TYPE_ACCESSIBILITY &&
76         accessibilityFocusedWindowId_ != INVALID_WINDOW_ID) {
77         windowId = accessibilityFocusedWindowId_;
78         HILOG_INFO("Convert into accessibility focused window id[%{public}d]", windowId);
79     }
80 
81     RetError ret = proxy_->FindFocusedElementInfo(windowId,
82         elementId, focusType, requestId, elementOperator);
83     if (ret != RET_OK) {
84         HILOG_ERROR("FindFocusedElementInfo failed. ret[%{public}d]", ret);
85         return ret;
86     }
87     HILOG_DEBUG("channelId:%{public}d, windowId:%{public}d, elementId:%{public}d, focusType:%{public}d",
88         channelId_, windowId, elementId, focusType);
89 
90     std::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
91     if (wait != std::future_status::ready) {
92         HILOG_ERROR("FindFocusedElementInfo Failed to wait result");
93         return RET_ERR_TIME_OUT;
94     }
95 
96     if (elementOperator->accessibilityInfoResult_.GetAccessibilityId() ==
97         AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
98         HILOG_ERROR("FindFocusedElementInfo The elementInfo from ace is wrong");
99         return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE;
100     }
101     HILOG_INFO("Get result successfully from ace.");
102 
103     elementInfo = elementOperator->accessibilityInfoResult_;
104     return RET_OK;
105 }
106 
SendSimulateGesture(const std::shared_ptr<AccessibilityGestureInjectPath> & gesturePath)107 RetError AccessibleAbilityChannelClient::SendSimulateGesture(
108     const std::shared_ptr<AccessibilityGestureInjectPath> &gesturePath)
109 {
110     HILOG_INFO("[channelId:%{public}d]", channelId_);
111     if (proxy_) {
112         return proxy_->SendSimulateGesture(gesturePath);
113     } else {
114         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
115         return RET_ERR_SAMGR;
116     }
117 }
118 
ExecuteAction(int32_t accessibilityWindowId,int32_t elementId,int32_t action,const std::map<std::string,std::string> & actionArguments)119 RetError AccessibleAbilityChannelClient::ExecuteAction(int32_t accessibilityWindowId,
120     int32_t elementId, int32_t action, const std::map<std::string, std::string> &actionArguments)
121 {
122     HILOG_DEBUG("[channelId:%{public}d]", channelId_);
123     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "ExecuteAction");
124     if (!proxy_) {
125         HILOG_ERROR("ExecuteAction Failed to connect to aams [channelId:%{public}d]", channelId_);
126         return RET_ERR_SAMGR;
127     }
128 
129     int32_t requestId = GenerateRequestId();
130     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
131         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
132     if (!elementOperator) {
133         HILOG_ERROR("ExecuteAction Failed to create elementOperator.");
134         return RET_ERR_NULLPTR;
135     }
136     std::future<void> promiseFuture = elementOperator->promise_.get_future();
137 
138     RetError ret = proxy_->ExecuteAction(accessibilityWindowId,
139         elementId, action, actionArguments, requestId, elementOperator);
140     if (ret != RET_OK) {
141         HILOG_ERROR("ExecuteAction failed. ret[%{public}d]", ret);
142         return ret;
143     }
144 
145     std::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
146     if (wait != std::future_status::ready) {
147         HILOG_ERROR("Failed to wait result");
148         return RET_ERR_TIME_OUT;
149     }
150     HILOG_INFO("Get result successfully from ace. executeActionResult_[%{public}d]",
151         elementOperator->executeActionResult_);
152 
153     if (elementOperator->executeActionResult_) {
154         switch (action) {
155             case ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS:
156                 accessibilityFocusedWindowId_ = accessibilityWindowId;
157                 HILOG_INFO("Set accessibility focused window id[%{public}d]", accessibilityFocusedWindowId_);
158                 break;
159             case ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS:
160                 accessibilityFocusedWindowId_ = INVALID_WINDOW_ID;
161                 HILOG_INFO("Clear accessibility focused window id");
162                 break;
163             default:
164                 break;
165         }
166     }
167     return elementOperator->executeActionResult_ ? RET_OK : RET_ERR_PERFORM_ACTION_FAILED_BY_ACE;
168 }
169 
SearchElementInfosByAccessibilityId(int32_t accessibilityWindowId,int32_t elementId,int32_t mode,std::vector<AccessibilityElementInfo> & elementInfos)170 RetError AccessibleAbilityChannelClient::SearchElementInfosByAccessibilityId(int32_t accessibilityWindowId,
171     int32_t elementId, int32_t mode, std::vector<AccessibilityElementInfo> &elementInfos)
172 {
173     HILOG_DEBUG("[channelId:%{public}d] [windowId:%{public}d]", channelId_, accessibilityWindowId);
174     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "SearchElementById");
175     if (!proxy_) {
176         HILOG_ERROR("SearchElementInfosByAccessibilityId Failed to connect to aams [channelId:%{public}d]",
177             channelId_);
178         return RET_ERR_SAMGR;
179     }
180 
181     int32_t requestId = GenerateRequestId();
182     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
183         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
184     if (!elementOperator) {
185         HILOG_ERROR("SearchElementInfosByAccessibilityId Failed to create elementOperator.");
186         return RET_ERR_NULLPTR;
187     }
188     std::future<void> promiseFuture = elementOperator->promise_.get_future();
189 
190     RetError ret = proxy_->SearchElementInfoByAccessibilityId(accessibilityWindowId, elementId, requestId,
191         elementOperator, mode);
192     if (ret != RET_OK) {
193         HILOG_ERROR("SearchElementInfosByAccessibilityId failed. ret[%{public}d]", ret);
194         return ret;
195     }
196 
197     std::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
198     if (wait != std::future_status::ready) {
199         HILOG_ERROR("SearchElementInfosByAccessibilityId Failed to wait result");
200         return RET_ERR_TIME_OUT;
201     }
202 
203     for (auto &info : elementOperator->elementInfosResult_) {
204         if (info.GetAccessibilityId() == AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
205             HILOG_ERROR("SearchElementInfosByAccessibilityId The elementInfo from ace is wrong");
206             return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE;
207         }
208     }
209     HILOG_DEBUG("Get result successfully from ace. size[%{public}zu]", elementOperator->elementInfosResult_.size());
210     elementInfos = elementOperator->elementInfosResult_;
211     return RET_OK;
212 }
213 
GetWindow(const int32_t windowId,AccessibilityWindowInfo & windowInfo)214 RetError AccessibleAbilityChannelClient::GetWindow(const int32_t windowId, AccessibilityWindowInfo &windowInfo)
215 {
216     HILOG_DEBUG("[channelId:%{public}d] [windowId:%{public}d]", channelId_, windowId);
217     HITRACE_METER(HITRACE_TAG_ACCESSIBILITY_MANAGER);
218     if (!proxy_) {
219         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
220         return RET_ERR_SAMGR;
221     }
222     return proxy_->GetWindow(windowId, windowInfo);
223 }
224 
GetWindows(std::vector<AccessibilityWindowInfo> & windows)225 RetError AccessibleAbilityChannelClient::GetWindows(std::vector<AccessibilityWindowInfo> &windows)
226 {
227     HILOG_DEBUG("[channelId:%{public}d]", channelId_);
228     HITRACE_METER(HITRACE_TAG_ACCESSIBILITY_MANAGER);
229     if (proxy_) {
230         return proxy_->GetWindows(windows);
231     } else {
232         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
233         return RET_ERR_SAMGR;
234     }
235 }
236 
GetWindows(const uint64_t displayId,std::vector<AccessibilityWindowInfo> & windows) const237 RetError AccessibleAbilityChannelClient::GetWindows(const uint64_t displayId,
238     std::vector<AccessibilityWindowInfo> &windows) const
239 {
240     HILOG_DEBUG("[channelId:%{public}d] [displayId:%{public}" PRIu64 "]", channelId_, displayId);
241     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "GetWindowsByDisplayId");
242     if (proxy_) {
243         return proxy_->GetWindowsByDisplayId(displayId, windows);
244     } else {
245         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
246         return RET_ERR_SAMGR;
247     }
248 }
249 
SearchElementInfosByText(int32_t accessibilityWindowId,int32_t elementId,const std::string & text,std::vector<AccessibilityElementInfo> & elementInfos)250 RetError AccessibleAbilityChannelClient::SearchElementInfosByText(int32_t accessibilityWindowId,
251     int32_t elementId, const std::string &text, std::vector<AccessibilityElementInfo> &elementInfos)
252 {
253     HILOG_DEBUG("[channelId:%{public}d]", channelId_);
254     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "SearchElementByText");
255     if (!proxy_) {
256         HILOG_ERROR("SearchElementInfosByText Failed to connect to aams [channelId:%{public}d]",
257             channelId_);
258         return RET_ERR_SAMGR;
259     }
260 
261     int32_t requestId = GenerateRequestId();
262     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
263         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
264     if (!elementOperator) {
265         HILOG_ERROR("SearchElementInfosByText Failed to create elementOperator.");
266         return RET_ERR_NULLPTR;
267     }
268     std::future<void> promiseFuture = elementOperator->promise_.get_future();
269 
270     RetError ret = proxy_->SearchElementInfosByText(accessibilityWindowId,
271         elementId, text, requestId, elementOperator);
272     if (ret != RET_OK) {
273         HILOG_ERROR("SearchElementInfosByText failed. ret[%{public}d]", ret);
274         return ret;
275     }
276 
277     std::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
278     if (wait != std::future_status::ready) {
279         HILOG_ERROR("SearchElementInfosByText Failed to wait result");
280         return RET_ERR_TIME_OUT;
281     }
282 
283     for (auto &info : elementOperator->elementInfosResult_) {
284         if (info.GetAccessibilityId() == AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
285             HILOG_ERROR("SearchElementInfosByText The elementInfo from ace is wrong");
286             return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE;
287         }
288     }
289     HILOG_INFO("Get result successfully from ace. size[%{public}zu]", elementOperator->elementInfosResult_.size());
290     elementInfos = elementOperator->elementInfosResult_;
291     return RET_OK;
292 }
293 
FocusMoveSearch(int32_t accessibilityWindowId,int32_t elementId,int32_t direction,AccessibilityElementInfo & elementInfo)294 RetError AccessibleAbilityChannelClient::FocusMoveSearch(int32_t accessibilityWindowId,
295     int32_t elementId, int32_t direction, AccessibilityElementInfo &elementInfo)
296 {
297     HILOG_DEBUG("[channelId:%{public}d]", channelId_);
298     if (!proxy_) {
299         HILOG_ERROR("FocusMoveSearch Failed to connect to aams [channelId:%{public}d]", channelId_);
300         return RET_ERR_SAMGR;
301     }
302 
303     int32_t requestId = GenerateRequestId();
304     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
305         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
306     if (!elementOperator) {
307         HILOG_ERROR("FocusMoveSearch Failed to create elementOperator.");
308         return RET_ERR_NULLPTR;
309     }
310     std::future<void> promiseFuture = elementOperator->promise_.get_future();
311 
312     RetError ret = proxy_->FocusMoveSearch(accessibilityWindowId, elementId, direction, requestId, elementOperator);
313     if (ret != RET_OK) {
314         HILOG_ERROR("FocusMoveSearch failed. ret[%{public}d]", ret);
315         return ret;
316     }
317 
318     std::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
319     if (wait != std::future_status::ready) {
320         HILOG_ERROR("FocusMoveSearch Failed to wait result");
321         return RET_ERR_TIME_OUT;
322     }
323 
324     if (elementOperator->accessibilityInfoResult_.GetAccessibilityId() ==
325         AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
326         HILOG_ERROR("FocusMoveSearch The elementInfo from ace is wrong");
327         return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE;
328     }
329 
330     HILOG_INFO("Get result successfully from ace");
331     elementInfo = elementOperator->accessibilityInfoResult_;
332     return RET_OK;
333 }
334 
SetTargetBundleName(const std::vector<std::string> & targetBundleNames)335 RetError AccessibleAbilityChannelClient::SetTargetBundleName(const std::vector<std::string> &targetBundleNames)
336 {
337     HILOG_INFO("[channelId:%{public}d]", channelId_);
338     if (proxy_) {
339         return proxy_->SetTargetBundleName(targetBundleNames);
340     } else {
341         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
342         return RET_ERR_SAMGR;
343     }
344 }
345 } // namespace Accessibility
346 } // namespace OHOS