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_ == nullptr) {
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 == nullptr) {
68         HILOG_ERROR("FindFocusedElementInfo Failed to create elementOperator.");
69         return RET_ERR_NULLPTR;
70     }
71     ffrt::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     ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
90     if (wait != ffrt::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     elementInfo.SetMainWindowId(windowId);
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 
GetCursorPosition(int32_t accessibilityWindowId,int64_t elementId,int32_t & position)119 RetError AccessibleAbilityChannelClient::GetCursorPosition(
120     int32_t accessibilityWindowId, int64_t elementId, int32_t &position)
121 {
122     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "GetCursorPosition");
123     if (proxy_ == nullptr) {
124         HILOG_ERROR("GetCursorPosition Failed to connect to aams [channelId:%{public}d]",
125             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 == nullptr) {
133         HILOG_ERROR("GetCursorPosition Failed to create elementOperator.");
134         return RET_ERR_NULLPTR;
135     }
136     ffrt::future<void> promiseFuture = elementOperator->promise_.get_future();
137 
138     RetError ret = proxy_->GetCursorPosition(accessibilityWindowId, elementId, requestId, elementOperator);
139     if (ret != RET_OK) {
140         HILOG_ERROR("ExecuteAction failed. ret[%{public}d]", ret);
141         return ret;
142     }
143 
144     ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
145     if (wait != ffrt::future_status::ready) {
146         HILOG_ERROR("GetCursorPosition Failed to wait result");
147         return RET_ERR_TIME_OUT;
148     }
149 
150     position = elementOperator->CursorPosition_;
151     HILOG_INFO("position%{public}d", position);
152     return RET_OK;
153 }
154 
ExecuteAction(int32_t accessibilityWindowId,int64_t elementId,int32_t action,const std::map<std::string,std::string> & actionArguments)155 RetError AccessibleAbilityChannelClient::ExecuteAction(int32_t accessibilityWindowId,
156     int64_t elementId, int32_t action, const std::map<std::string, std::string> &actionArguments)
157 {
158     HILOG_DEBUG("execute action:%{public}d, elementId:%{public}" PRId64 "", action, elementId);
159     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "ExecuteAction");
160     if (proxy_ == nullptr) {
161         HILOG_ERROR("ExecuteAction Failed to connect to aams [channelId:%{public}d]", channelId_);
162         return RET_ERR_SAMGR;
163     }
164     if (action == ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS &&
165         accessibilityFocusedElementId_ != INVALID_WINDOW_ID) {
166         ExecuteAction(accessibilityFocusedWindowId_, accessibilityFocusedElementId_,
167             ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS, actionArguments);
168     }
169 
170     int32_t requestId = GenerateRequestId();
171     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
172         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
173     if (elementOperator == nullptr) {
174         HILOG_ERROR("ExecuteAction Failed to create elementOperator.");
175         return RET_ERR_NULLPTR;
176     }
177     ffrt::future<void> promiseFuture = elementOperator->promise_.get_future();
178 
179     RetError ret = proxy_->ExecuteAction(accessibilityWindowId,
180         elementId, action, actionArguments, requestId, elementOperator);
181     if (ret != RET_OK) {
182         HILOG_ERROR("ExecuteAction failed. ret[%{public}d]", ret);
183         return ret;
184     }
185 
186     ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
187     if (wait != ffrt::future_status::ready) {
188         HILOG_ERROR("Failed to wait result");
189         return RET_ERR_TIME_OUT;
190     }
191     HILOG_INFO("action:[%{public}d], executeActionResult_[%{public}d]", action, elementOperator->executeActionResult_);
192 
193     if (elementOperator->executeActionResult_) {
194         switch (action) {
195             case ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS:
196                 accessibilityFocusedWindowId_ = accessibilityWindowId;
197                 accessibilityFocusedElementId_ = elementId;
198                 HILOG_INFO("Set windowId:%{public}d, elementId:%{public}" PRId64 "", accessibilityWindowId, elementId);
199                 break;
200             case ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS:
201                 accessibilityFocusedWindowId_ = INVALID_WINDOW_ID;
202                 accessibilityFocusedElementId_ = INVALID_WINDOW_ID;
203                 HILOG_INFO("Clear accessibility focused window id");
204                 break;
205             default:
206                 break;
207         }
208     }
209     return elementOperator->executeActionResult_ ? RET_OK : RET_ERR_PERFORM_ACTION_FAILED_BY_ACE;
210 }
211 
EnableScreenCurtain(bool isEnable)212 RetError AccessibleAbilityChannelClient::EnableScreenCurtain(bool isEnable)
213 {
214     HILOG_INFO("[channelId:%{public}d]", channelId_);
215     if (proxy_ == nullptr) {
216         HILOG_ERROR("EnableScreenCurtain Failed to connect to aams [channelId:%{public}d]", channelId_);
217         return RET_ERR_SAMGR;
218     }
219     return  proxy_->EnableScreenCurtain(isEnable);
220 }
221 
SearchElementInfosByAccessibilityId(int32_t accessibilityWindowId,int64_t elementId,int32_t mode,std::vector<AccessibilityElementInfo> & elementInfos,int32_t treeId,bool isFilter)222 RetError AccessibleAbilityChannelClient::SearchElementInfosByAccessibilityId(int32_t accessibilityWindowId,
223     int64_t elementId, int32_t mode, std::vector<AccessibilityElementInfo> &elementInfos, int32_t treeId,
224     bool isFilter)
225 {
226     int32_t requestId = GenerateRequestId();
227     HILOG_DEBUG("channelId:%{public}d, elementId:%{public}" PRId64 ", windowId:%{public}d, requestId:%{public}d",
228         channelId_, elementId, accessibilityWindowId, requestId);
229     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "SearchElementById");
230     if (proxy_ == nullptr) {
231         HILOG_ERROR("SearchElementInfosByAccessibilityId Failed to connect to aams [channelId:%{public}d]",
232             channelId_);
233         return RET_ERR_SAMGR;
234     }
235 
236     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
237         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
238     if (elementOperator == nullptr) {
239         HILOG_ERROR("SearchElementInfosByAccessibilityId Failed to create elementOperator.");
240         return RET_ERR_NULLPTR;
241     }
242     ffrt::future<void> promiseFuture = elementOperator->promise_.get_future();
243     ElementBasicInfo elementBasicInfo {};
244     elementBasicInfo.windowId = accessibilityWindowId;
245     elementBasicInfo.treeId = treeId;
246     elementBasicInfo.elementId = elementId;
247 
248     RetError ret = proxy_->SearchElementInfoByAccessibilityId(elementBasicInfo, requestId,
249         elementOperator, mode, isFilter);
250     if (ret != RET_OK) {
251         HILOG_ERROR("SearchElementInfosByAccessibilityId windowId :[%{pubic}d] Failed to wait result, Time out",
252             accessibilityWindowId);
253         return ret;
254     }
255 
256     ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
257     if (wait != ffrt::future_status::ready) {
258         HILOG_ERROR("SearchElementInfosByAccessibilityId Failed to wait result");
259         return RET_ERR_TIME_OUT;
260     }
261 
262     for (auto &info : elementOperator->elementInfosResult_) {
263         if (info.GetAccessibilityId() == AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
264             HILOG_ERROR("SearchElementInfosByAccessibilityId The elementInfo from ace is wrong");
265             return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE;
266         }
267     }
268     HILOG_DEBUG("Get result successfully from ace. size[%{public}zu]", elementOperator->elementInfosResult_.size());
269     elementInfos = elementOperator->elementInfosResult_;
270     if (!elementInfos.empty()) {
271         for (auto &element : elementInfos) {
272             element.SetMainWindowId(accessibilityWindowId);
273         }
274     }
275     return RET_OK;
276 }
277 
GetWindow(const int32_t windowId,AccessibilityWindowInfo & windowInfo)278 RetError AccessibleAbilityChannelClient::GetWindow(const int32_t windowId, AccessibilityWindowInfo &windowInfo)
279 {
280     HILOG_DEBUG("[channelId:%{public}d] [windowId:%{public}d]", channelId_, windowId);
281     HITRACE_METER(HITRACE_TAG_ACCESSIBILITY_MANAGER);
282     if (proxy_ == nullptr) {
283         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
284         return RET_ERR_SAMGR;
285     }
286     return proxy_->GetWindow(windowId, windowInfo);
287 }
288 
GetWindows(std::vector<AccessibilityWindowInfo> & windows)289 RetError AccessibleAbilityChannelClient::GetWindows(std::vector<AccessibilityWindowInfo> &windows)
290 {
291     HILOG_DEBUG("[channelId:%{public}d]", channelId_);
292     HITRACE_METER(HITRACE_TAG_ACCESSIBILITY_MANAGER);
293     if (proxy_) {
294         return proxy_->GetWindows(windows);
295     } else {
296         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
297         return RET_ERR_SAMGR;
298     }
299 }
300 
GetWindows(const uint64_t displayId,std::vector<AccessibilityWindowInfo> & windows) const301 RetError AccessibleAbilityChannelClient::GetWindows(const uint64_t displayId,
302     std::vector<AccessibilityWindowInfo> &windows) const
303 {
304     HILOG_DEBUG("[channelId:%{public}d] [displayId:%{public}" PRIu64 "]", channelId_, displayId);
305     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "GetWindowsByDisplayId");
306     if (proxy_) {
307         return proxy_->GetWindowsByDisplayId(displayId, windows);
308     } else {
309         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
310         return RET_ERR_SAMGR;
311     }
312 }
313 
SearchElementInfosByText(int32_t accessibilityWindowId,int64_t elementId,const std::string & text,std::vector<AccessibilityElementInfo> & elementInfos)314 RetError AccessibleAbilityChannelClient::SearchElementInfosByText(int32_t accessibilityWindowId,
315     int64_t elementId, const std::string &text, std::vector<AccessibilityElementInfo> &elementInfos)
316 {
317     HILOG_DEBUG("[channelId:%{public}d]", channelId_);
318     HITRACE_METER_NAME(HITRACE_TAG_ACCESSIBILITY_MANAGER, "SearchElementByText");
319     if (proxy_ == nullptr) {
320         HILOG_ERROR("SearchElementInfosByText Failed to connect to aams [channelId:%{public}d]",
321             channelId_);
322         return RET_ERR_SAMGR;
323     }
324 
325     int32_t requestId = GenerateRequestId();
326     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
327         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
328     if (elementOperator == nullptr) {
329         HILOG_ERROR("SearchElementInfosByText Failed to create elementOperator.");
330         return RET_ERR_NULLPTR;
331     }
332     ffrt::future<void> promiseFuture = elementOperator->promise_.get_future();
333 
334     RetError ret = proxy_->SearchElementInfosByText(accessibilityWindowId,
335         elementId, text, requestId, elementOperator);
336     if (ret != RET_OK) {
337         HILOG_ERROR("SearchElementInfosByText failed. ret[%{public}d]", ret);
338         return ret;
339     }
340 
341     ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
342     if (wait != ffrt::future_status::ready) {
343         HILOG_ERROR("SearchElementInfosByText Failed to wait result");
344         return RET_ERR_TIME_OUT;
345     }
346 
347     for (auto &info : elementOperator->elementInfosResult_) {
348         if (info.GetAccessibilityId() == AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
349             HILOG_ERROR("SearchElementInfosByText The elementInfo from ace is wrong");
350             return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE;
351         }
352     }
353     HILOG_INFO("Get result successfully from ace. size[%{public}zu]", elementOperator->elementInfosResult_.size());
354     elementInfos = elementOperator->elementInfosResult_;
355     return RET_OK;
356 }
357 
FocusMoveSearch(int32_t accessibilityWindowId,int64_t elementId,int32_t direction,AccessibilityElementInfo & elementInfo)358 RetError AccessibleAbilityChannelClient::FocusMoveSearch(int32_t accessibilityWindowId,
359     int64_t elementId, int32_t direction, AccessibilityElementInfo &elementInfo)
360 {
361     HILOG_DEBUG("[channelId:%{public}d]", channelId_);
362     if (proxy_ == nullptr) {
363         HILOG_ERROR("FocusMoveSearch Failed to connect to aams [channelId:%{public}d]", channelId_);
364         return RET_ERR_SAMGR;
365     }
366 
367     int32_t requestId = GenerateRequestId();
368     sptr<AccessibilityElementOperatorCallbackImpl> elementOperator =
369         new(std::nothrow) AccessibilityElementOperatorCallbackImpl();
370     if (elementOperator == nullptr) {
371         HILOG_ERROR("FocusMoveSearch Failed to create elementOperator.");
372         return RET_ERR_NULLPTR;
373     }
374     ffrt::future<void> promiseFuture = elementOperator->promise_.get_future();
375 
376     RetError ret = proxy_->FocusMoveSearch(accessibilityWindowId, elementId, direction, requestId, elementOperator);
377     if (ret != RET_OK) {
378         HILOG_ERROR("FocusMoveSearch failed. ret[%{public}d]", ret);
379         return ret;
380     }
381 
382     ffrt::future_status wait = promiseFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
383     if (wait != ffrt::future_status::ready) {
384         HILOG_ERROR("FocusMoveSearch Failed to wait result");
385         return RET_ERR_TIME_OUT;
386     }
387 
388     if (elementOperator->accessibilityInfoResult_.GetAccessibilityId() ==
389         AccessibilityElementInfo::UNDEFINED_ACCESSIBILITY_ID) {
390         HILOG_ERROR("FocusMoveSearch The elementInfo from ace is wrong");
391         return RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE;
392     }
393 
394     HILOG_INFO("Get result successfully from ace");
395     elementInfo = elementOperator->accessibilityInfoResult_;
396     return RET_OK;
397 }
398 
SetTargetBundleName(const std::vector<std::string> & targetBundleNames)399 RetError AccessibleAbilityChannelClient::SetTargetBundleName(const std::vector<std::string> &targetBundleNames)
400 {
401     HILOG_INFO("[channelId:%{public}d]", channelId_);
402     if (proxy_) {
403         return proxy_->SetTargetBundleName(targetBundleNames);
404     } else {
405         HILOG_ERROR("Failed to connect to aams [channelId:%{public}d]", channelId_);
406         return RET_ERR_SAMGR;
407     }
408 }
409 } // namespace Accessibility
410 } // namespace OHOS
411