• 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.h"
17 #include "accessible_ability_manager_service.h"
18 #include "accessibility_window_connection.h"
19 #include "accessibility_window_manager.h"
20 #include "accessible_ability_connection.h"
21 #include "hilog_wrapper.h"
22 #include <cinttypes>
23 #include "transaction/rs_interfaces.h"
24 #ifdef OHOS_BUILD_ENABLE_POWER_MANAGER
25 #include "accessibility_power_manager.h"
26 #endif
27 #include "utils.h"
28 
29 namespace OHOS {
30 namespace Accessibility {
31 namespace {
32     constexpr int32_t WINDOW_ID_INVALID = -1;
33     constexpr int64_t ELEMENT_ID_INVALID = -1;
34     MMI::InputManager* inputManager_ = MMI::InputManager::GetInstance();
35     std::map<int32_t, std::pair<bool, std::pair<int32_t, int32_t>>> accessibleKeyCodeTable = {
36         {ActionType::ACCESSIBILITY_ACTION_HOME,
37             {false, {MMI::KeyEvent::KEYCODE_META_LEFT, MMI::KeyEvent::KEYCODE_D}}},
38         {ActionType::ACCESSIBILITY_ACTION_RECENTTASK,
39             {false, {MMI::KeyEvent::KEYCODE_META_LEFT, MMI::KeyEvent::KEYCODE_TAB}}},
40         {ActionType::ACCESSIBILITY_ACTION_BACK,
41             {true, {MMI::KeyEvent::KEYCODE_BACK, MMI::KeyEvent::KEYCODE_BACK}}},
42         {ActionType::ACCESSIBILITY_ACTION_NOTIFICATIONCENTER,
43             {true, {MMI::KeyEvent::KEYCODE_CALL_NOTIFICATION_CENTER, MMI::KeyEvent::KEYCODE_CALL_NOTIFICATION_CENTER}}},
44         {ActionType::ACCESSIBILITY_ACTION_CONTROLCENTER,
45             {true, {MMI::KeyEvent::KEYCODE_CALL_CONTROL_CENTER, MMI::KeyEvent::KEYCODE_CALL_CONTROL_CENTER}}}};
46 } // namespace
47 
AccessibleAbilityChannel(const int32_t accountId,const std::string & clientName)48 AccessibleAbilityChannel::AccessibleAbilityChannel(const int32_t accountId, const std::string &clientName)
49     : clientName_(clientName), accountId_(accountId)
50 {
51     eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(
52         Singleton<AccessibleAbilityManagerService>::GetInstance().GetChannelRunner());
53 }
54 
SearchElementInfoByAccessibilityId(const ElementBasicInfo elementBasicInfo,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback,const int32_t mode,bool isFilter,bool systemApi)55 RetError AccessibleAbilityChannel::SearchElementInfoByAccessibilityId(const ElementBasicInfo elementBasicInfo,
56     const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback,
57     const int32_t mode, bool isFilter, bool systemApi)
58 {
59     int32_t treeId = elementBasicInfo.treeId;
60     int32_t windowId = elementBasicInfo.windowId;
61     int64_t elementId = elementBasicInfo.elementId;
62     HILOG_DEBUG("elementId:%{public}" PRId64 " winId: %{public}d treeId: %{public}d", elementId, windowId, treeId);
63     if (systemApi && !Singleton<AccessibleAbilityManagerService>::GetInstance().CheckPermission(
64         OHOS_PERMISSION_ACCESSIBILITY_EXTENSION_ABILITY)) {
65         HILOG_WARN("SearchElementInfoByAccessibilityId permission denied.");
66         return RET_ERR_NO_PERMISSION;
67     }
68     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
69 
70     if (eventHandler_ == nullptr || callback == nullptr) {
71         HILOG_ERROR("eventHandler_ exist: %{public}d, callback exist: %{public}d.", eventHandler_ != nullptr,
72             callback != nullptr);
73         return RET_ERR_NULLPTR;
74     }
75 
76     int32_t accountId = accountId_;
77     std::string clientName = clientName_;
78     std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
79     ffrt::future syncFuture = syncPromise->get_future();
80     eventHandler_->PostTask([accountId, clientName, syncPromise, windowId, elementId, treeId, requestId,
81         callback, mode, isFilter]() {
82         HILOG_DEBUG("search element accountId[%{public}d], name[%{public}s]", accountId, clientName.c_str());
83         sptr<IAccessibilityElementOperator> elementOperator = nullptr;
84         RetError ret = GetElementOperator(accountId, windowId, FOCUS_TYPE_INVALID, clientName,
85             elementOperator, treeId);
86         if (ret != RET_OK || !CheckWinFromAwm(windowId)) {
87             HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", windowId);
88             std::vector<AccessibilityElementInfo> infos = {};
89             callback->SetSearchElementInfoByAccessibilityIdResult(infos, requestId);
90             syncPromise->set_value(ret);
91             return;
92         }
93 
94         auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
95         if (windowId == SCENE_BOARD_WINDOW_ID && awm.IsInnerWindowRootElement(elementId)) {
96             std::vector<AccessibilityElementInfo> infos = {};
97             callback->SetSearchElementInfoByAccessibilityIdResult(infos, requestId);
98             HILOG_DEBUG("IsInnerWindowRootElement elementId: %{public}" PRId64 "", elementId);
99         } else {
100             int64_t realElementId = awm.GetSceneBoardElementId(windowId, elementId);
101             Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(windowId, treeId,
102                 requestId, callback);
103             RetError ret = elementOperator->SearchElementInfoByAccessibilityId(realElementId, requestId,
104                 callback, mode, isFilter);
105             if (ret != RET_OK) {
106                 HILOG_ERROR("SearchElementInfoByAccessibilityId IPC Failed.");
107                 std::vector<AccessibilityElementInfo> infos = {};
108                 callback->SetSearchElementInfoByAccessibilityIdResult(infos, requestId);
109                 syncPromise->set_value(ret);
110                 return;
111             }
112             HILOG_DEBUG("AccessibleAbilityChannel::SearchElementInfoByAccessibilityId successfully");
113         }
114         syncPromise->set_value(RET_OK);
115         }, "SearchElementInfoByAccessibilityId");
116 
117     ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
118     if (wait != ffrt::future_status::ready) {
119         Singleton<AccessibleAbilityManagerService>::GetInstance().RemoveRequestId(requestId);
120         HILOG_ERROR("Failed to wait SearchElementInfoByAccessibilityId result");
121         return RET_ERR_TIME_OUT;
122     }
123     return syncFuture.get();
124 }
125 
SearchDefaultFocusedByWindowId(const ElementBasicInfo elementBasicInfo,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback,const int32_t mode,bool isFilter)126 RetError AccessibleAbilityChannel::SearchDefaultFocusedByWindowId(const ElementBasicInfo elementBasicInfo,
127     const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback,
128     const int32_t mode, bool isFilter)
129 {
130     int32_t windowId = elementBasicInfo.windowId;
131     int64_t elementId = elementBasicInfo.elementId;
132     int32_t treeId = elementBasicInfo.treeId;
133     HILOG_DEBUG("elementId:%{public}" PRId64 " winId: %{public}d treeId: %{public}d", elementId, windowId, treeId);
134     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
135 
136     if (eventHandler_ == nullptr || callback == nullptr) {
137         HILOG_ERROR("eventHandler_ exist: %{public}d, callback exist: %{public}d.", eventHandler_ != nullptr,
138             callback != nullptr);
139         return RET_ERR_NULLPTR;
140     }
141 
142     int32_t accountId = accountId_;
143     std::string clientName = clientName_;
144     std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
145     ffrt::future syncFuture = syncPromise->get_future();
146     eventHandler_->PostTask([accountId, clientName, syncPromise, windowId, elementId, treeId, requestId,
147         callback, mode, isFilter]() {
148         HILOG_DEBUG("search element accountId[%{public}d], name[%{public}s]", accountId, clientName.c_str());
149         sptr<IAccessibilityElementOperator> elementOperator = nullptr;
150         RetError ret = GetElementOperator(accountId, windowId, FOCUS_TYPE_INVALID, clientName,
151             elementOperator, treeId);
152         if (ret != RET_OK || !CheckWinFromAwm(windowId)) {
153             HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", windowId);
154             std::vector<AccessibilityElementInfo> infos = {};
155             callback->SetSearchDefaultFocusByWindowIdResult(infos, requestId);
156             syncPromise->set_value(ret);
157             return;
158         }
159 
160         auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
161         if (windowId == SCENE_BOARD_WINDOW_ID && awm.IsInnerWindowRootElement(elementId)) {
162             std::vector<AccessibilityElementInfo> infos = {};
163             callback->SetSearchDefaultFocusByWindowIdResult(infos, requestId);
164         } else {
165             Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(windowId, treeId,
166                 requestId, callback);
167             elementOperator->SearchDefaultFocusedByWindowId(windowId, requestId, callback, mode, isFilter);
168             HILOG_DEBUG("AccessibleAbilityChannel::SearchElementInfoByAccessibilityId successfully");
169         }
170         syncPromise->set_value(RET_OK);
171         }, "SearchElementInfoByAccessibilityId");
172 
173     ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
174     if (wait != ffrt::future_status::ready) {
175         Singleton<AccessibleAbilityManagerService>::GetInstance().RemoveRequestId(requestId);
176         HILOG_ERROR("Failed to wait SearchElementInfoByAccessibilityId result");
177         return RET_ERR_TIME_OUT;
178     }
179     return RET_OK;
180 }
181 
SearchElementInfosByText(const int32_t accessibilityWindowId,const int64_t elementId,const std::string & text,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback,bool systemApi)182 RetError AccessibleAbilityChannel::SearchElementInfosByText(const int32_t accessibilityWindowId,
183     const int64_t elementId, const std::string &text, const int32_t requestId,
184     const sptr<IAccessibilityElementOperatorCallback> &callback, bool systemApi)
185 {
186     HILOG_DEBUG("SearchElementInfosByText :channel SearchElementInfo elementId: %{public}" PRId64 " winId: %{public}d",
187         elementId, accessibilityWindowId);
188     if (systemApi && !Singleton<AccessibleAbilityManagerService>::GetInstance().CheckPermission(
189         OHOS_PERMISSION_ACCESSIBILITY_EXTENSION_ABILITY)) {
190         return RET_ERR_NO_PERMISSION;
191     }
192     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
193     if (eventHandler_ == nullptr) {
194         HILOG_ERROR("eventHandler_ is nullptr.");
195         return RET_ERR_NULLPTR;
196     }
197     if (callback == nullptr) {
198         HILOG_ERROR("callback is nullptr.");
199         return RET_ERR_NULLPTR;
200     }
201     int32_t treeId = AccessibleAbilityManagerService::GetTreeIdBySplitElementId(elementId);
202     HILOG_DEBUG("SearchElementInfosByText :channel SearchElementInfo treeId: %{public}d", treeId);
203     int32_t accountId = accountId_;
204     std::string clientName = clientName_;
205     std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
206     ffrt::future syncFuture = syncPromise->get_future();
207     eventHandler_->PostTask([accountId, clientName, syncPromise, accessibilityWindowId, elementId, treeId, text,
208         requestId, callback]() {
209         HILOG_DEBUG("accountId[%{public}d], name[%{public}s]", accountId, clientName.c_str());
210         sptr<IAccessibilityElementOperator> elementOperator = nullptr;
211         RetError ret = GetElementOperator(accountId, accessibilityWindowId, FOCUS_TYPE_INVALID,
212             clientName, elementOperator, treeId);
213         if (ret != RET_OK) {
214             HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", accessibilityWindowId);
215             std::vector<AccessibilityElementInfo> infos = {};
216             callback->SetSearchElementInfoByAccessibilityIdResult(infos, requestId);
217             syncPromise->set_value(ret);
218             return;
219         }
220         auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
221         int64_t realElementId = awm.GetSceneBoardElementId(accessibilityWindowId, elementId);
222         Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(accessibilityWindowId, treeId,
223             requestId, callback);
224         elementOperator->SearchElementInfosByText(realElementId, text, requestId, callback);
225         syncPromise->set_value(RET_OK);
226         }, "SearchElementInfosByText");
227     ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
228     if (wait != ffrt::future_status::ready) {
229         Singleton<AccessibleAbilityManagerService>::GetInstance().RemoveRequestId(requestId);
230         HILOG_ERROR("Failed to wait SearchElementInfosByText result");
231         return RET_ERR_TIME_OUT;
232     }
233     return syncFuture.get();
234 }
235 
FindFocusedElementInfo(const int32_t accessibilityWindowId,const int64_t elementId,const int32_t focusType,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback,bool systemApi)236 RetError AccessibleAbilityChannel::FindFocusedElementInfo(const int32_t accessibilityWindowId,
237     const int64_t elementId, const int32_t focusType, const int32_t requestId,
238     const sptr<IAccessibilityElementOperatorCallback> &callback, bool systemApi)
239 {
240     HILOG_DEBUG("channel FindFocusedElementInfo elementId: %{public}" PRId64 " winId: %{public}d",
241         elementId, accessibilityWindowId);
242     if (systemApi && !Singleton<AccessibleAbilityManagerService>::GetInstance().CheckPermission(
243         OHOS_PERMISSION_ACCESSIBILITY_EXTENSION_ABILITY)) {
244         return RET_ERR_NO_PERMISSION;
245     }
246     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
247     if (eventHandler_ == nullptr) {
248         HILOG_ERROR("eventHandler_ is nullptr.");
249         return RET_ERR_NULLPTR;
250     }
251     if (callback == nullptr) {
252         HILOG_ERROR("callback is nullptr.");
253         return RET_ERR_NULLPTR;
254     }
255     std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
256     ffrt::future syncFuture = syncPromise->get_future();
257     int32_t treeId = AccessibleAbilityManagerService::GetTreeIdBySplitElementId(elementId);
258     HILOG_DEBUG("FindFocusedElementInfo :channel FindFocusedElementInfo treeId: %{public}d", treeId);
259     int32_t accountId = accountId_;
260     std::string clientName = clientName_;
261     eventHandler_->PostTask([accountId, clientName, syncPromise, accessibilityWindowId, elementId, treeId,
262         focusType, requestId, callback]() {
263         HILOG_DEBUG("accountId[%{public}d], name[%{public}s]", accountId, clientName.c_str());
264         sptr<IAccessibilityElementOperator> elementOperator = nullptr;
265         RetError ret = GetElementOperator(accountId, accessibilityWindowId, focusType,
266             clientName, elementOperator, treeId);
267         if (ret != RET_OK) {
268             HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", accessibilityWindowId);
269             std::vector<AccessibilityElementInfo> infos = {};
270             callback->SetSearchElementInfoByAccessibilityIdResult(infos, requestId);
271             syncPromise->set_value(ret);
272             return;
273         }
274         auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
275         int64_t realElementId = awm.GetSceneBoardElementId(accessibilityWindowId, elementId);
276         Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(accessibilityWindowId, treeId,
277             requestId, callback);
278         elementOperator->FindFocusedElementInfo(realElementId, focusType, requestId, callback);
279         syncPromise->set_value(RET_OK);
280         }, "FindFocusedElementInfo");
281     ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
282     if (wait != ffrt::future_status::ready) {
283         Singleton<AccessibleAbilityManagerService>::GetInstance().RemoveRequestId(requestId);
284         HILOG_ERROR("Failed to wait FindFocusedElementInfo result");
285         return RET_ERR_TIME_OUT;
286     }
287     return syncFuture.get();
288 }
289 
FocusMoveSearch(const int32_t accessibilityWindowId,const int64_t elementId,const int32_t direction,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback,bool systemApi)290 RetError AccessibleAbilityChannel::FocusMoveSearch(const int32_t accessibilityWindowId, const int64_t elementId,
291     const int32_t direction, const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback,
292     bool systemApi)
293 {
294     HILOG_DEBUG("FocusMoveSearch :channel FocusMoveSearch elementId: %{public}" PRId64 " winId: %{public}d",
295         elementId, accessibilityWindowId);
296     if (systemApi && !Singleton<AccessibleAbilityManagerService>::GetInstance().CheckPermission(
297         OHOS_PERMISSION_ACCESSIBILITY_EXTENSION_ABILITY)) {
298         return RET_ERR_NO_PERMISSION;
299     }
300     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
301     if (eventHandler_ == nullptr) {
302         HILOG_ERROR("eventHandler_ is nullptr.");
303         return RET_ERR_NULLPTR;
304     }
305     if (callback == nullptr) {
306         HILOG_ERROR("callback is nullptr.");
307         return RET_ERR_NULLPTR;
308     }
309     std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
310     ffrt::future syncFuture = syncPromise->get_future();
311     int32_t treeId = AccessibleAbilityManagerService::GetTreeIdBySplitElementId(elementId);
312     HILOG_DEBUG("FocusMoveSearch :channel FocusMoveSearch treeId: %{public}d", treeId);
313     int32_t accountId = accountId_;
314     std::string clientName = clientName_;
315     eventHandler_->PostTask([accountId, clientName, syncPromise, accessibilityWindowId,
316         elementId, treeId, direction, requestId, callback]() {
317         HILOG_DEBUG("accountId[%{public}d], name[%{public}s]", accountId, clientName.c_str());
318         sptr<IAccessibilityElementOperator> elementOperator = nullptr;
319         RetError ret = GetElementOperator(accountId, accessibilityWindowId, FOCUS_TYPE_INVALID,
320             clientName, elementOperator, treeId);
321         if (ret != RET_OK) {
322             HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", accessibilityWindowId);
323             std::vector<AccessibilityElementInfo> infos = {};
324             callback->SetSearchElementInfoByAccessibilityIdResult(infos, requestId);
325             syncPromise->set_value(ret);
326             return;
327         }
328         auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
329         int64_t realElementId = awm.GetSceneBoardElementId(accessibilityWindowId, elementId);
330         Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(accessibilityWindowId, treeId,
331             requestId, callback);
332         elementOperator->FocusMoveSearch(realElementId, direction, requestId, callback);
333         syncPromise->set_value(RET_OK);
334         }, "FocusMoveSearch");
335     ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
336     if (wait != ffrt::future_status::ready) {
337         Singleton<AccessibleAbilityManagerService>::GetInstance().RemoveRequestId(requestId);
338         HILOG_ERROR("Failed to wait FocusMoveSearch result");
339         return RET_ERR_TIME_OUT;
340     }
341     return syncFuture.get();
342 }
343 
SetKeyCodeToMmi(std::shared_ptr<MMI::KeyEvent> & keyEvent,const bool isPress,const int32_t keyCode)344 void AccessibleAbilityChannel::SetKeyCodeToMmi(std::shared_ptr<MMI::KeyEvent>& keyEvent, const bool isPress,
345     const int32_t keyCode)
346 {
347     HILOG_DEBUG();
348     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
349     if (keyEvent == nullptr) {
350         HILOG_ERROR("KeyEvent is nullptr");
351         return;
352     }
353     MMI::KeyEvent::KeyItem item;
354     item.SetPressed(isPress);
355     item.SetKeyCode(keyCode);
356     keyEvent->AddKeyItem(item);
357     keyEvent->SetKeyCode(keyCode);
358 }
359 
TransmitActionToMmi(const int32_t action)360 RetError AccessibleAbilityChannel::TransmitActionToMmi(const int32_t action)
361 {
362     HILOG_DEBUG("The action is %{public}d", action);
363     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
364     std::shared_ptr<MMI::KeyEvent> keyEventUp = MMI::KeyEvent::Create();
365     std::shared_ptr<MMI::KeyEvent> keyEventDown = MMI::KeyEvent::Create();
366     if (keyEventUp == nullptr || keyEventDown == nullptr) {
367         HILOG_ERROR("KeyEvent is nullptr");
368         return RET_ERR_NULLPTR;
369     }
370 
371     if (!inputManager_) {
372         HILOG_ERROR("inputManager_ is nullptr");
373         return RET_ERR_NULLPTR;
374     }
375 
376     HILOG_INFO("Transmit keycode to MMI");
377 
378     if (accessibleKeyCodeTable.at(action).first) {
379         SetKeyCodeToMmi(keyEventDown, true, accessibleKeyCodeTable.at(action).second.first);
380         SetKeyCodeToMmi(keyEventUp, false, accessibleKeyCodeTable.at(action).second.first);
381     } else {
382         SetKeyCodeToMmi(keyEventDown, true, accessibleKeyCodeTable.at(action).second.first);
383         SetKeyCodeToMmi(keyEventUp, false, accessibleKeyCodeTable.at(action).second.first);
384         SetKeyCodeToMmi(keyEventDown, true, accessibleKeyCodeTable.at(action).second.second);
385     }
386     keyEventDown->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
387     keyEventUp->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP);
388     inputManager_->SimulateInputEvent(keyEventDown);
389     inputManager_->SimulateInputEvent(keyEventUp);
390 
391     return RET_OK;
392 }
393 
EnableScreenCurtain(bool isEnable)394 RetError AccessibleAbilityChannel::EnableScreenCurtain(bool isEnable)
395 {
396     HILOG_DEBUG();
397     auto& aams = Singleton<AccessibleAbilityManagerService>::GetInstance();
398     aams.PostDelayUnloadTask();
399     return aams.SetCurtainScreenUsingStatus(isEnable);
400 }
401 
HoldRunningLock()402 RetError AccessibleAbilityChannel::HoldRunningLock()
403 {
404     HILOG_DEBUG();
405     if (!Singleton<AccessibleAbilityManagerService>::GetInstance().CheckPermission(
406         OHOS_PERMISSION_ACCESSIBILITY_EXTENSION_ABILITY)) {
407         HILOG_WARN("HoldRunningLock permission denied.");
408         return RET_ERR_NO_PERMISSION;
409     }
410 #ifdef OHOS_BUILD_ENABLE_POWER_MANAGER
411     std::string bundleName = "";
412     if (!Utils::GetBundleNameByCallingUid(bundleName)) {
413         return RET_ERR_FAILED;
414     }
415     if (!Singleton<AccessibilityPowerManager>::GetInstance().HoldRunningLock(bundleName)) {
416         HILOG_ERROR("Failed to hold running lock.");
417         return RET_ERR_FAILED;
418     }
419 #endif
420     return RET_OK;
421 }
422 
UnholdRunningLock()423 RetError AccessibleAbilityChannel::UnholdRunningLock()
424 {
425     HILOG_DEBUG();
426     if (!Singleton<AccessibleAbilityManagerService>::GetInstance().CheckPermission(
427         OHOS_PERMISSION_ACCESSIBILITY_EXTENSION_ABILITY)) {
428         HILOG_WARN("HoldRunningLock permission denied.");
429         return RET_ERR_NO_PERMISSION;
430     }
431 #ifdef OHOS_BUILD_ENABLE_POWER_MANAGER
432     std::string bundleName = "";
433     if (!Utils::GetBundleNameByCallingUid(bundleName)) {
434         return RET_ERR_FAILED;
435     }
436     auto &powerManager = Singleton<AccessibilityPowerManager>::GetInstance();
437     if (!powerManager.UnholdRunningLock(bundleName)) {
438         HILOG_ERROR("Failed to unhold running lock.");
439         return RET_ERR_FAILED;
440     }
441 #endif
442     return RET_OK;
443 }
444 
ExecuteAction(const int32_t accessibilityWindowId,const int64_t elementId,const int32_t action,const std::map<std::string,std::string> & actionArguments,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)445 RetError AccessibleAbilityChannel::ExecuteAction(const int32_t accessibilityWindowId, const int64_t elementId,
446     const int32_t action, const std::map<std::string, std::string> &actionArguments, const int32_t requestId,
447     const sptr<IAccessibilityElementOperatorCallback> &callback)
448 {
449     HILOG_DEBUG("ExecuteAction elementId:%{public}" PRId64 " winId:%{public}d, action:%{public}d, requestId:%{public}d",
450         elementId, accessibilityWindowId, action, requestId);
451     if (actionArguments.find("sysapi_check_perm") != actionArguments.end()) {
452         if (!Singleton<AccessibleAbilityManagerService>::GetInstance().CheckPermission(
453             OHOS_PERMISSION_ACCESSIBILITY_EXTENSION_ABILITY)) {
454             HILOG_WARN("system api permission denied.");
455             return RET_ERR_NO_PERMISSION;
456         }
457     }
458 
459     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
460     if (eventHandler_ == nullptr || callback == nullptr) {
461         HILOG_ERROR("eventHandler_ exist: %{public}d, callback exist: %{public}d.", eventHandler_ != nullptr,
462             callback != nullptr);
463         return RET_ERR_NULLPTR;
464     }
465 
466     if (accessibleKeyCodeTable.find(action) != accessibleKeyCodeTable.end()) {
467         RetError ret = TransmitActionToMmi(action);
468         if (ret != RET_OK) {
469             HILOG_ERROR("Transmit Action To Mmi failed!");
470             callback->SetExecuteActionResult(false, requestId);
471             return RET_ERR_FAILED;
472         }
473         callback->SetExecuteActionResult(true, requestId);
474         return RET_OK;
475     }
476     return ExecuteActionAsync(accessibilityWindowId, elementId, action, actionArguments, requestId, callback);
477 }
478 
ExecuteActionAsync(const int32_t accessibilityWindowId,const int64_t elementId,const int32_t action,const std::map<std::string,std::string> & actionArguments,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)479 RetError AccessibleAbilityChannel::ExecuteActionAsync(const int32_t accessibilityWindowId, const int64_t elementId,
480     const int32_t action, const std::map<std::string, std::string> &actionArguments, const int32_t requestId,
481     const sptr<IAccessibilityElementOperatorCallback> &callback)
482 {
483     SetFocusWindowIdAndElementId(accessibilityWindowId, elementId, action);
484     std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
485     ffrt::future syncFuture = syncPromise->get_future();
486     int32_t treeId = AccessibleAbilityManagerService::GetTreeIdBySplitElementId(elementId);
487     int32_t accountId = accountId_;
488     std::string clientName = clientName_;
489     eventHandler_->PostTask([accountId, clientName, syncPromise, accessibilityWindowId, elementId, treeId, action,
490         actionArguments, requestId, callback]() {
491         sptr<IAccessibilityElementOperator> elementOperator = nullptr;
492         RetError ret = GetElementOperator(accountId, accessibilityWindowId, FOCUS_TYPE_INVALID, clientName,
493             elementOperator, treeId);
494         if (ret != RET_OK) {
495             HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", accessibilityWindowId);
496             syncPromise->set_value(ret);
497             return;
498         }
499 
500         auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
501         int64_t realElementId = awm.GetSceneBoardElementId(accessibilityWindowId, elementId);
502         Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(accessibilityWindowId, treeId,
503             requestId, callback);
504         elementOperator->ExecuteAction(realElementId, action, actionArguments, requestId, callback);
505         syncPromise->set_value(RET_OK);
506         }, "ExecuteAction");
507 
508     ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_1000MS));
509     if (wait != ffrt::future_status::ready) {
510         Singleton<AccessibleAbilityManagerService>::GetInstance().RemoveRequestId(requestId);
511         HILOG_ERROR("Failed to wait ExecuteAction result");
512         return RET_ERR_TIME_OUT;
513     }
514     return syncFuture.get();
515 }
516 
SetFocusWindowIdAndElementId(const int32_t accessibilityWindowId,const int64_t elementId,const int32_t action)517 void AccessibleAbilityChannel::SetFocusWindowIdAndElementId(const int32_t accessibilityWindowId,
518     const int64_t elementId, const int32_t action)
519 {
520     auto& awm = Singleton<AccessibleAbilityManagerService>::GetInstance();
521     if ((action == ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS)) {
522         awm.SetFocusWindowId(accessibilityWindowId);
523         awm.SetFocusElementId(elementId);
524     } else if (action == ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS) {
525         awm.SetFocusWindowId(WINDOW_ID_INVALID);
526         awm.SetFocusElementId(ELEMENT_ID_INVALID);
527     }
528 }
529 
GetWindow(const int32_t windowId,AccessibilityWindowInfo & windowInfo)530 RetError AccessibleAbilityChannel::GetWindow(const int32_t windowId, AccessibilityWindowInfo &windowInfo)
531 {
532     HILOG_DEBUG("windowId:%{public}d", windowId);
533     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
534     if (eventHandler_== nullptr) {
535         HILOG_ERROR("eventHandler_ is nullptr.");
536         return RET_ERR_NULLPTR;
537     }
538 
539     int32_t accountId = accountId_;
540     std::string clientName = clientName_;
541     std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
542     std::shared_ptr<AccessibilityWindowInfo> tmpWindowInfo = std::make_shared<AccessibilityWindowInfo>(windowInfo);
543     ffrt::future syncFuture = syncPromise->get_future();
544     eventHandler_->PostTask([accountId, clientName, windowId, tmpWindowInfo, syncPromise]() {
545         HILOG_DEBUG("windowId:%{public}d", windowId);
546         sptr<AccessibleAbilityConnection> clientConnection = GetConnection(accountId, clientName);
547         if (!clientConnection) {
548             HILOG_ERROR("There is no client connection");
549             syncPromise->set_value(RET_ERR_NO_CONNECTION);
550             return;
551         }
552         if (!(clientConnection->GetAbilityInfo().GetCapabilityValues() & Capability::CAPABILITY_RETRIEVE)) {
553             HILOG_ERROR("AccessibleAbilityChannel::GetWindow failed: no capability");
554             syncPromise->set_value(RET_ERR_NO_CAPABILITY);
555             return;
556         }
557 
558         if (Singleton<AccessibilityWindowManager>::GetInstance().GetAccessibilityWindow(windowId, *tmpWindowInfo)) {
559             syncPromise->set_value(RET_OK);
560         } else {
561             syncPromise->set_value(RET_ERR_NO_WINDOW_CONNECTION);
562         }
563         }, "GetWindow");
564 
565     ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
566     if (wait != ffrt::future_status::ready) {
567         HILOG_ERROR("Failed to wait GetWindow result");
568         return RET_ERR_TIME_OUT;
569     }
570 
571     windowInfo = *tmpWindowInfo;
572     return syncFuture.get();
573 }
574 
GetWindows(std::vector<AccessibilityWindowInfo> & windows,bool systemApi)575 RetError AccessibleAbilityChannel::GetWindows(std::vector<AccessibilityWindowInfo> &windows, bool systemApi)
576 {
577     HILOG_DEBUG();
578     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
579 #ifdef OHOS_BUILD_ENABLE_DISPLAY_MANAGER
580     uint64_t displayId = Singleton<AccessibilityDisplayManager>::GetInstance().GetDefaultDisplayId();
581     HILOG_DEBUG("default display id is %{public}" PRIu64 "", displayId);
582     return GetWindows(displayId, windows, systemApi);
583 #else
584     HILOG_DEBUG("not support display manager");
585     return GetWindows(0, windows, systemApi);
586 #endif
587 }
588 
GetWindowsByDisplayId(const uint64_t displayId,std::vector<AccessibilityWindowInfo> & windows,bool systemApi)589 RetError AccessibleAbilityChannel::GetWindowsByDisplayId(const uint64_t displayId,
590     std::vector<AccessibilityWindowInfo> &windows, bool systemApi)
591 {
592     HILOG_DEBUG();
593     return GetWindows(displayId, windows, systemApi);
594 }
595 
GetWindows(uint64_t displayId,std::vector<AccessibilityWindowInfo> & windows,bool systemApi) const596 RetError AccessibleAbilityChannel::GetWindows(uint64_t displayId, std::vector<AccessibilityWindowInfo> &windows,
597     bool systemApi) const
598 {
599     if (systemApi && !Singleton<AccessibleAbilityManagerService>::GetInstance().CheckPermission(
600         OHOS_PERMISSION_ACCESSIBILITY_EXTENSION_ABILITY)) {
601         return RET_ERR_NO_PERMISSION;
602     }
603     if (eventHandler_== nullptr) {
604         HILOG_ERROR("eventHandler_ is nullptr.");
605         return RET_ERR_NULLPTR;
606     }
607     std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
608     auto tmpWindows = std::make_shared<std::vector<AccessibilityWindowInfo>>(windows);
609     int32_t accountId = accountId_;
610     std::string clientName = clientName_;
611     ffrt::future syncFuture = syncPromise->get_future();
612     eventHandler_->PostTask([accountId, clientName, displayId, tmpWindows, syncPromise]() {
613         HILOG_DEBUG();
614         sptr<AccessibleAbilityConnection> clientConnection = GetConnection(accountId, clientName);
615         if (!clientConnection) {
616             HILOG_ERROR("There is no client connection");
617             syncPromise->set_value(RET_ERR_NO_CONNECTION);
618             return;
619         }
620         if (!(clientConnection->GetAbilityInfo().GetCapabilityValues() & Capability::CAPABILITY_RETRIEVE)) {
621             HILOG_ERROR("GetWindows failed: no capability");
622             syncPromise->set_value(RET_ERR_NO_CAPABILITY);
623             return;
624         }
625         std::vector<AccessibilityWindowInfo> windowInfos =
626             Singleton<AccessibilityWindowManager>::GetInstance().GetAccessibilityWindows();
627 #ifdef OHOS_BUILD_ENABLE_DISPLAY_MANAGER
628         for (auto &window : windowInfos) {
629             if (window.GetDisplayId() == displayId) {
630                 tmpWindows->emplace_back(window);
631             }
632         }
633 #else
634         for (auto &window : windowInfos) {
635             tmpWindows->emplace_back(window);
636         }
637 #endif
638         syncPromise->set_value(RET_OK);
639         }, "GetWindows");
640     ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
641     if (wait != ffrt::future_status::ready) {
642         HILOG_ERROR("Failed to wait GetWindows result");
643         return RET_ERR_TIME_OUT;
644     }
645     windows = *tmpWindows;
646     return syncFuture.get();
647 }
648 
SetOnKeyPressEventResult(const bool handled,const int32_t sequence)649 void AccessibleAbilityChannel::SetOnKeyPressEventResult(const bool handled, const int32_t sequence)
650 {
651     HILOG_DEBUG();
652     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
653     if (eventHandler_== nullptr) {
654         HILOG_ERROR("eventHandler_ is nullptr.");
655         return;
656     }
657 
658     int32_t accountId = accountId_;
659     std::string clientName = clientName_;
660     eventHandler_->PostTask([accountId, clientName, handled, sequence]() {
661         sptr<KeyEventFilter> keyEventFilter =
662             Singleton<AccessibleAbilityManagerService>::GetInstance().GetKeyEventFilter();
663         if (!keyEventFilter) {
664             return;
665         }
666 
667         sptr<AccessibleAbilityConnection> clientConnection = GetConnection(accountId, clientName);
668         if (!clientConnection) {
669             HILOG_ERROR("There is no client connection");
670             return;
671         }
672         keyEventFilter->SetServiceOnKeyEventResult(*clientConnection, handled, sequence);
673         }, "SetOnKeyPressEventResult");
674 }
675 
GetCursorPosition(const int32_t accessibilityWindowId,const int64_t elementId,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)676 RetError AccessibleAbilityChannel::GetCursorPosition(const int32_t accessibilityWindowId, const int64_t elementId,
677     const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
678 {
679     HILOG_DEBUG("GetCursorPosition :channel GetCursorPosition elementId: %{public}" PRId64 " winId: %{public}d",
680         elementId, accessibilityWindowId);
681     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
682     if (eventHandler_== nullptr) {
683         HILOG_ERROR("eventHandler_ is nullptr.");
684         return RET_ERR_NULLPTR;
685     }
686     std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
687     ffrt::future syncFuture = syncPromise->get_future();
688     int32_t treeId = AccessibleAbilityManagerService::GetTreeIdBySplitElementId(elementId);
689     HILOG_DEBUG("GetCursorPosition :channel GetCursorPosition treeId: %{public}d", treeId);
690     int32_t accountId = accountId_;
691     std::string clientName = clientName_;
692     eventHandler_->PostTask([accountId, clientName, syncPromise, accessibilityWindowId, elementId, treeId,
693         requestId, callback]() {
694         HILOG_DEBUG("accountId[%{public}d], name[%{public}s]", accountId, clientName.c_str());
695         sptr<IAccessibilityElementOperator> elementOperator = nullptr;
696         RetError ret = GetElementOperator(accountId, accessibilityWindowId, FOCUS_TYPE_INVALID, clientName,
697             elementOperator, treeId);
698         if (ret != RET_OK) {
699             HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", accessibilityWindowId);
700             syncPromise->set_value(ret);
701             return;
702         }
703 
704         auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
705         int64_t realElementId = awm.GetSceneBoardElementId(accessibilityWindowId, elementId);
706         Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(accessibilityWindowId, treeId,
707             requestId, callback);
708         elementOperator->GetCursorPosition(realElementId, requestId, callback);
709         syncPromise->set_value(RET_OK);
710         }, "GetCursorPosition");
711 
712     ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
713     if (wait != ffrt::future_status::ready) {
714         Singleton<AccessibleAbilityManagerService>::GetInstance().RemoveRequestId(requestId);
715         HILOG_ERROR("Failed to wait GetCursorPosition result");
716         return RET_ERR_TIME_OUT;
717     }
718     return syncFuture.get();
719 }
720 
SendSimulateGesture(const std::shared_ptr<AccessibilityGestureInjectPath> & gesturePath)721 RetError AccessibleAbilityChannel::SendSimulateGesture(
722     const std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath)
723 {
724     HILOG_INFO();
725     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
726     if (eventHandler_== nullptr) {
727         HILOG_ERROR("eventHandler_ is nullptr");
728         return RET_ERR_NULLPTR;
729     }
730 
731     int32_t accountId = accountId_;
732     std::string clientName = clientName_;
733     std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
734     ffrt::future syncFuture = syncPromise->get_future();
735     eventHandler_->PostTask([accountId, clientName, gesturePath, syncPromise]() {
736         HILOG_DEBUG();
737         sptr<AccessibleAbilityConnection> clientConnection = GetConnection(accountId, clientName);
738         if (!clientConnection) {
739             HILOG_ERROR("There is no client connection");
740             syncPromise->set_value(RET_ERR_NO_CONNECTION);
741             return;
742         }
743 
744         if (!(clientConnection->GetAbilityInfo().GetCapabilityValues() & Capability::CAPABILITY_GESTURE)) {
745             HILOG_ERROR("AccessibleAbilityChannel::SendSimulateGesture failed: no capability");
746             syncPromise->set_value(RET_ERR_NO_CAPABILITY);
747             return;
748         }
749 
750         sptr<TouchEventInjector> touchEventInjector =
751             Singleton<AccessibleAbilityManagerService>::GetInstance().GetTouchEventInjector();
752         if (!touchEventInjector) {
753             HILOG_ERROR("touchEventInjector is null");
754             syncPromise->set_value(RET_ERR_NO_INJECTOR);
755             return;
756         }
757         touchEventInjector->InjectEvents(gesturePath);
758         syncPromise->set_value(RET_OK);
759         }, "SendSimulateGesture");
760 
761     ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
762     if (wait != ffrt::future_status::ready) {
763         HILOG_ERROR("Failed to wait SendSimulateGesture result");
764         return RET_ERR_TIME_OUT;
765     }
766     return syncFuture.get();
767 }
768 
SetTargetBundleName(const std::vector<std::string> & targetBundleNames)769 RetError AccessibleAbilityChannel::SetTargetBundleName(const std::vector<std::string> &targetBundleNames)
770 {
771     HILOG_DEBUG();
772     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
773     if (eventHandler_== nullptr) {
774         HILOG_ERROR("eventHandler_ is nullptr");
775         return RET_ERR_NULLPTR;
776     }
777 
778     int32_t accountId = accountId_;
779     std::string clientName = clientName_;
780     std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
781     ffrt::future syncFuture = syncPromise->get_future();
782     eventHandler_->PostTask([accountId, clientName, targetBundleNames, syncPromise]() {
783         HILOG_DEBUG();
784         sptr<AccessibleAbilityConnection> clientConnection = GetConnection(accountId, clientName);
785         if (!clientConnection) {
786             HILOG_ERROR("There is no client connection");
787             syncPromise->set_value(RET_ERR_NO_CONNECTION);
788             return;
789         }
790 
791         clientConnection->SetAbilityInfoTargetBundleName(targetBundleNames);
792         syncPromise->set_value(RET_OK);
793         }, "SetTargetBundleName");
794 
795     ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
796     if (wait != ffrt::future_status::ready) {
797         HILOG_ERROR("Failed to wait SetTargetBundleName result");
798         return RET_ERR_TIME_OUT;
799     }
800     return syncFuture.get();
801 }
802 
GetConnection(int32_t accountId,const std::string & clientName)803 sptr<AccessibleAbilityConnection> AccessibleAbilityChannel::GetConnection(
804     int32_t accountId, const std::string &clientName)
805 {
806     HILOG_DEBUG();
807     sptr<AccessibilityAccountData> accountData =
808         Singleton<AccessibleAbilityManagerService>::GetInstance().GetAccountData(accountId);
809     if (accountData == nullptr) {
810         HILOG_ERROR("accountData is nullptr");
811         return nullptr;
812     }
813 
814     HILOG_DEBUG("accountId[%{public}d] clientName[%{public}s]", accountId, clientName.c_str());
815     return accountData->GetAccessibleAbilityConnection(clientName);
816 }
817 
GetElementOperator(int32_t accountId,int32_t windowId,int32_t focusType,const std::string & clientName,sptr<IAccessibilityElementOperator> & elementOperator,const int32_t treeId)818 RetError AccessibleAbilityChannel::GetElementOperator(
819     int32_t accountId, int32_t windowId, int32_t focusType, const std::string &clientName,
820     sptr<IAccessibilityElementOperator> &elementOperator, const int32_t treeId)
821 {
822     HILOG_DEBUG();
823     elementOperator = nullptr;
824     sptr<AccessibleAbilityConnection> clientConnection = GetConnection(accountId, clientName);
825     if (!clientConnection) {
826         HILOG_ERROR("There is no client connection");
827         return RET_ERR_NO_CONNECTION;
828     }
829     if (!(clientConnection->GetAbilityInfo().GetCapabilityValues() & Capability::CAPABILITY_RETRIEVE)) {
830         HILOG_ERROR("the client has no retieve capability");
831         return RET_ERR_NO_CAPABILITY;
832     }
833 
834     sptr<AccessibilityAccountData> accountData =
835         Singleton<AccessibleAbilityManagerService>::GetInstance().GetAccountData(accountId);
836     if (!accountData) {
837         HILOG_ERROR("accountData is nullptr");
838         return RET_ERR_NULLPTR;
839     }
840     int32_t realId = Singleton<AccessibilityWindowManager>::GetInstance().ConvertToRealWindowId(windowId, focusType);
841     sptr<AccessibilityWindowConnection> connection =  nullptr;
842     connection = accountData->GetAccessibilityWindowConnection(realId);
843     if (connection == nullptr) {
844         HILOG_ERROR("windowId[%{public}d] has no connection", realId);
845         return RET_ERR_NO_WINDOW_CONNECTION;
846     }
847     if (treeId <= 0) {
848         elementOperator = connection->GetProxy();
849     } else {
850         elementOperator = connection->GetCardProxy(treeId);
851     }
852     if (!elementOperator) {
853         HILOG_ERROR("The proxy of window connection is nullptr");
854         return RET_ERR_NULLPTR;
855     }
856     return RET_OK;
857 }
858 
CheckWinFromAwm(const int32_t windowId)859 bool AccessibleAbilityChannel::CheckWinFromAwm(const int32_t windowId)
860 {
861     std::vector<AccessibilityWindowInfo> windowsFromAwm =
862         Singleton<AccessibilityWindowManager>::GetInstance().GetAccessibilityWindows();
863     if (!windowsFromAwm.empty()) {
864         for (const auto& window: windowsFromAwm) {
865             if (windowId == window.GetWindowId()) {
866                 return true;
867             }
868         }
869     }
870     return false;
871 }
872 
SetIsRegisterDisconnectCallback(bool isRegister)873 RetError AccessibleAbilityChannel::SetIsRegisterDisconnectCallback(bool isRegister)
874 {
875     HILOG_INFO();
876     sptr<AccessibleAbilityConnection> clientConnection = GetConnection(accountId_, clientName_);
877     if (clientConnection == nullptr) {
878         HILOG_ERROR("GetConnection failed, clientName: %{public}s", clientName_.c_str());
879         return RET_ERR_NULLPTR;
880     }
881     clientConnection->SetIsRegisterDisconnectCallback(isRegister);
882     return RET_OK;
883 }
884 
NotifyDisconnect()885 RetError AccessibleAbilityChannel::NotifyDisconnect()
886 {
887     HILOG_INFO();
888     sptr<AccessibilityAccountData> accountData =
889         Singleton<AccessibleAbilityManagerService>::GetInstance().GetAccountData(accountId_);
890     if (accountData == nullptr) {
891         HILOG_ERROR("accountData is nullptr");
892         return RET_ERR_NULLPTR;
893     }
894     sptr<AccessibleAbilityConnection> clientConnection = accountData->GetWaitDisConnectAbility(clientName_);
895     if (clientConnection == nullptr) {
896         HILOG_ERROR("GetConnection failed, clientName: %{public}s", clientName_.c_str());
897         return RET_ERR_NULLPTR;
898     }
899     accountData->RemoveWaitDisconnectAbility(clientName_);
900     clientConnection->NotifyDisconnect();
901     return RET_OK;
902 }
903 
ConfigureEvents(const std::vector<uint32_t> needEvents)904 RetError AccessibleAbilityChannel::ConfigureEvents(const std::vector<uint32_t> needEvents)
905 {
906     HILOG_INFO();
907     RetError ret = Singleton<AccessibleAbilityManagerService>::GetInstance().UpdateUITestConfigureEvents(needEvents);
908     if (ret != RET_OK) {
909         HILOG_ERROR("Configure Events failed!");
910         return RET_ERR_FAILED;
911     }
912     return RET_OK;
913 }
914 
SearchElementInfoBySpecificProperty(const ElementBasicInfo elementBasicInfo,const SpecificPropertyParam & param,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)915 void AccessibleAbilityChannel::SearchElementInfoBySpecificProperty(const ElementBasicInfo elementBasicInfo,
916     const SpecificPropertyParam& param, const int32_t requestId,
917     const sptr<IAccessibilityElementOperatorCallback> &callback)
918 {
919     int32_t treeId = elementBasicInfo.treeId;
920     int32_t windowId = elementBasicInfo.windowId;
921     int64_t elementId = elementBasicInfo.elementId;
922     HILOG_DEBUG("elementId:%{public}" PRId64 " winId: %{public}d treeId: %{public}d "
923         "propertyTarget:%{public}s propertyType:%{public}u",
924         elementId, windowId, treeId, param.propertyTarget.c_str(), static_cast<uint32_t>(param.propertyType));
925     Singleton<AccessibleAbilityManagerService>::GetInstance().PostDelayUnloadTask();
926 
927     if (eventHandler_ == nullptr || callback == nullptr) {
928         HILOG_ERROR("eventHandler_ exist: %{public}d, callback exist: %{public}d.", eventHandler_ != nullptr,
929             callback != nullptr);
930         return;
931     }
932 
933     int32_t accountId = accountId_;
934     std::string clientName = clientName_;
935     std::shared_ptr<ffrt::promise<RetError>> syncPromise = std::make_shared<ffrt::promise<RetError>>();
936     ffrt::future syncFuture = syncPromise->get_future();
937     eventHandler_->PostTask([accountId, clientName, syncPromise, windowId, elementId, treeId, requestId,
938         callback, param]() {
939         HILOG_DEBUG("search element accountId[%{public}d], name[%{public}s]", accountId, clientName.c_str());
940         sptr<IAccessibilityElementOperator> elementOperator = nullptr;
941         RetError ret = GetElementOperator(accountId, windowId, FOCUS_TYPE_INVALID, clientName,
942             elementOperator, treeId);
943         if (ret != RET_OK || !CheckWinFromAwm(windowId)) {
944             HILOG_ERROR("Get elementOperator failed! accessibilityWindowId[%{public}d]", windowId);
945             std::list<AccessibilityElementInfo> infos = {};
946             std::list<AccessibilityElementInfo> treeInfos = {};
947             callback->SetSearchElementInfoBySpecificPropertyResult(infos, treeInfos, requestId);
948             syncPromise->set_value(ret);
949             return;
950         }
951 
952         auto& awm = Singleton<AccessibilityWindowManager>::GetInstance();
953         if (windowId == SCENE_BOARD_WINDOW_ID && awm.IsInnerWindowRootElement(elementId)) {
954             std::list<AccessibilityElementInfo> infos = {};
955             std::list<AccessibilityElementInfo> treeInfos = {};
956             callback->SetSearchElementInfoBySpecificPropertyResult(infos, treeInfos, requestId);
957             HILOG_DEBUG("IsInnerWindowRootElement elementId: %{public}" PRId64 "", elementId);
958         } else {
959             int64_t realElementId = awm.GetSceneBoardElementId(windowId, elementId);
960             Singleton<AccessibleAbilityManagerService>::GetInstance().AddRequestId(windowId, treeId,
961                 requestId, callback);
962             elementOperator->SearchElementInfoBySpecificProperty(realElementId, param, requestId, callback);
963             HILOG_DEBUG("AccessibleAbilityChannel::SearchElementInfosBySpecificProperty successfully");
964         }
965         syncPromise->set_value(RET_OK);
966         }, "SearchElementInfosBySpecificProperty");
967 
968     ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIME_OUT_OPERATOR));
969     if (wait != ffrt::future_status::ready) {
970         Singleton<AccessibleAbilityManagerService>::GetInstance().RemoveRequestId(requestId);
971         HILOG_ERROR("Failed to wait SearchElementInfosBySpecificProperty result");
972         return;
973     }
974 }
975 } // namespace Accessibility
976 } // namespace OHOS