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