1 /* 2 * Copyright (C) 2022-2023 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 #ifndef ACCESSIBLE_ABILITY_CLIENT_IMPL_H 17 #define ACCESSIBLE_ABILITY_CLIENT_IMPL_H 18 19 #include <atomic> 20 #include <deque> 21 #include <memory> 22 #include "accessible_ability_channel_client.h" 23 #include "accessible_ability_client.h" 24 #include "accessible_ability_client_stub.h" 25 #include "ffrt.h" 26 #include "ffrt_inner.h" 27 #include "i_accessible_ability_manager_service.h" 28 #include "refbase.h" 29 #include "system_ability_load_callback_stub.h" 30 #include "system_ability_status_change_stub.h" 31 #include "rwlock.h" 32 #include "safe_map.h" 33 34 namespace OHOS { 35 namespace Accessibility { 36 37 constexpr int32_t SCENE_BOARD_WINDOW_ID = 1; // default scene board window id 1 38 constexpr int32_t INVALID_SCENE_BOARD_INNER_WINDOW_ID = -1; // invalid scene board window id -1 39 constexpr int64_t INVALID_SCENE_BOARD_ELEMENT_ID = -1; // invalid scene board element id -1 40 constexpr int32_t MAX_CACHE_WINDOW_SIZE = 5; 41 42 class AccessibleAbilityClientImpl : public AccessibleAbilityClient, public AccessibleAbilityClientStub { 43 public: 44 /** 45 * @brief The constructor of AccessibleAbilityClientImpl. 46 */ 47 AccessibleAbilityClientImpl(); 48 49 /** 50 * @brief The deconstructor of AccessibleAbilityClientImpl. 51 */ 52 ~AccessibleAbilityClientImpl(); 53 54 /** 55 * @brief Get the implement of accessibility ability client. 56 */ 57 static sptr<AccessibleAbilityClientImpl> GetAbilityClientImplement(); 58 59 /** 60 * @brief Gets remote object. 61 * @return Remote object. 62 */ 63 virtual sptr<IRemoteObject> GetRemoteObject() override; 64 65 /** 66 * @brief Register ability listener. 67 * @param listener The listener to add. 68 * @return Return RET_OK if registers listener successfully, otherwise refer to the RetError for the failure. 69 */ 70 virtual RetError RegisterAbilityListener(const std::shared_ptr<AccessibleAbilityListener> &listener) override; 71 72 /** 73 * @brief Init accessible ability. 74 * @param channel The object of IAccessibleAbilityChannel. 75 * @param channelId The id of channel. 76 */ 77 virtual void Init(const sptr<IAccessibleAbilityChannel> &channel, const int32_t channelId) override; 78 79 /** 80 * @brief Disconnect accessible ability. 81 * @param channelId The id of channel. 82 */ 83 virtual void Disconnect(const int32_t channelId) override; 84 85 /** 86 * @brief Called when an accessibility event occurs. 87 * @param eventInfo The information of accessible event. 88 */ 89 virtual void OnAccessibilityEvent(const AccessibilityEventInfo &eventInfo) override; 90 91 /** 92 * @brief Called when a key event occurs. 93 * @param keyEvent Indicates the key event to send. 94 * @param sequence The sequence of the key event. 95 */ 96 virtual void OnKeyPressEvent(const MMI::KeyEvent &keyEvent, const int32_t sequence) override; 97 98 /** 99 * @brief Obtains elementInfo of focus. 100 * @param focusType The type of focus. It contains FOCUS_TYPE_INPUT and FOCUS_TYPE_ACCESSIBILITY. 101 * @param elementInfo The accessibilityElementInfo of focus. 102 * @return Return RET_OK if obtains elementInfo successfully, otherwise refer to the RetError for the failure. 103 */ 104 virtual RetError GetFocus(const int32_t focusType, AccessibilityElementInfo &elementInfo) override; 105 106 /** 107 * @brief Obtains elementInfo of focus. 108 * @param sourceInfo The source info to get focus. 109 * @param focusType The type of focus. It contains FOCUS_TYPE_INPUT and FOCUS_TYPE_ACCESSIBILITY. 110 * @param elementInfo The accessibilityElementInfo of focus. 111 * @return Return RET_OK if obtains elementInfo successfully, otherwise refer to the RetError for the failure. 112 */ 113 virtual RetError GetFocusByElementInfo(const AccessibilityElementInfo &sourceInfo, const int32_t focusType, 114 AccessibilityElementInfo &elementInfo) override; 115 116 /** 117 * @brief Sends simulate gestures to the screen. 118 * @param gesturePath The gesture which need to send. 119 * @return Return RET_OK if the gesture sends successfully, otherwise refer to the RetError for the failure. 120 */ 121 virtual RetError InjectGesture(const std::shared_ptr<AccessibilityGestureInjectPath> &gesturePath) override; 122 123 /** 124 * @brief Obtains elementInfo of the accessible root node. 125 * @param elementInfo The elementInfo of the accessible root node. 126 * @return Return RET_OK if obtains elementInfo successfully, otherwise refer to the RetError for the failure. 127 */ 128 virtual RetError GetRoot(AccessibilityElementInfo &elementInfo) override; 129 130 /** 131 * @brief Obtains elementInfo of the accessible root node. 132 * @param windowInfo The source window info to get root. 133 * @param elementInfo The elementInfo of the accessible root node. 134 * @return Return RET_OK if obtains elementInfo successfully, otherwise refer to the RetError for the failure. 135 */ 136 virtual RetError GetRootByWindow(const AccessibilityWindowInfo &windowInfo, 137 AccessibilityElementInfo &elementInfo) override; 138 139 /** 140 * @brief Obtains elementInfos of the accessible root node in batchs. 141 * @param elementInfos ElementInfos of the accessible root node and its recursive subnodes. 142 * @return Return RET_OK if obtains elementInfos successfully, otherwise refer to the RetError for the failure. 143 */ 144 virtual RetError GetRootBatch(std::vector<AccessibilityElementInfo>& elementInfos) override; 145 146 /** 147 * @brief Obtains elementInfos of the accessible root node in batchs. 148 * @param windowInfo The source window info to get root. 149 * @param elementInfos ElementInfos of the accessible root node and its recursive subnodes. 150 * @param isFilter Indicates whether to filter nodes. 151 * @param needCut Indicates whether to remove invisible nodes. 152 * @return Return RET_OK if obtains elementInfos successfully, otherwise refer to the RetError for the failure. 153 */ 154 virtual RetError GetRootByWindowBatch(const AccessibilityWindowInfo &windowInfo, 155 std::vector<AccessibilityElementInfo>& elementInfos, bool isFilter, bool needCut) override; 156 157 /** 158 * @brief Get the window information related with the event 159 * @param windowId The window id. 160 * @param windowInfo The window information. 161 * @return Return RET_OK if obtains windowInfo successfully, otherwise refer to the RetError for the failure. 162 */ 163 virtual RetError GetWindow(const int32_t windowId, AccessibilityWindowInfo &windowInfo) override; 164 165 /** 166 * @brief Obtains the list of interactive windows on the device, in the layers they are visible to users. 167 * @param windows The information of windows. 168 * @return Return RET_OK if obtains windowInfo successfully, otherwise refer to the RetError for the failure. 169 */ 170 virtual RetError GetWindows(std::vector<AccessibilityWindowInfo> &windows) override; 171 172 /** 173 * @brief Obtains the list of interactive windows on the device, in the layers they are visible to users. 174 * @param displayId the id of display 175 * @param windows The information of windows. 176 * @return Return RET_OK if obtains windowInfo successfully, otherwise refer to the RetError for the failure. 177 */ 178 virtual RetError GetWindows(const uint64_t displayId, std::vector<AccessibilityWindowInfo> &windows) override; 179 180 /** 181 * @brief Gets the next focused node in the specified direction of the currently focused node. 182 * @param elementInfo The source info to get next info. 183 * @param direction Indicates the direction to obtain the next focused node. Refer to FocusMoveDirection 184 * @param nextElementInfo The info of next element. 185 * @return Return RET_OK if gets next elementInfo successfully, otherwise refer to the RetError for the failure. 186 */ 187 virtual RetError GetNext(const AccessibilityElementInfo &elementInfo, const FocusMoveDirection direction, 188 AccessibilityElementInfo &nextElementInfo) override; 189 190 /** 191 * @brief Get the child node information by childId 192 * @param index The index of the child. 193 * @param parent The parent info to get child. 194 * @param child The element info of child. 195 * @return Return RET_OK if gets child elementInfo successfully, otherwise refer to the RetError for the failure. 196 */ 197 virtual RetError GetChildElementInfo(const int32_t index, const AccessibilityElementInfo &parent, 198 AccessibilityElementInfo &child) override; 199 200 /** 201 * @brief Get the child node information 202 * @param parent The parent info to get child. 203 * @param children The element info of children. 204 * @return Return RET_OK if gets child elementInfo successfully, otherwise refer to the RetError for the failure. 205 */ 206 virtual RetError GetChildren(const AccessibilityElementInfo &parent, 207 std::vector<AccessibilityElementInfo> &children) override; 208 209 /** 210 * @brief Searches for node information based on the specified content. 211 * @param elementInfo The source info. 212 * @param text specified content 213 * @param elementInfos The element infos of specified content. 214 * @return Return RET_OK if gets elementInfos successfully, otherwise refer to the RetError for the failure. 215 */ 216 virtual RetError GetByContent(const AccessibilityElementInfo &elementInfo, const std::string &text, 217 std::vector<AccessibilityElementInfo> &elementInfos) override; 218 219 /** 220 * @brief Get the node information related with the event 221 * @param eventInfo The source info to get source. 222 * @param elementInfo The element info of source. 223 * @return Return RET_OK if gets elementInfos successfully, otherwise refer to the RetError for the failure. 224 */ 225 virtual RetError GetSource(const AccessibilityEventInfo &eventInfo, 226 AccessibilityElementInfo &elementInfo) override; 227 228 /** 229 * @brief Get Parent node information 230 * @param child The child element info to get parent. 231 * @param parent The parent element info. 232 * @return Return RET_OK if gets info successfully, otherwise refer to the RetError for the failure. 233 */ 234 virtual RetError GetParentElementInfo(const AccessibilityElementInfo &child, 235 AccessibilityElementInfo &parent) override; 236 237 /** 238 * @brief Get node information based on element id in active window. 239 * @param elementId The target element id. 240 * @param windowId The target window id. 241 * @param targetElementInfo The element info of specified content. 242 * @return Return RET_OK if gets info successfully, otherwise refer to the RetError for the failure. 243 */ 244 virtual RetError GetByElementId(const int64_t elementId, const int32_t windowId, 245 AccessibilityElementInfo &targetElementInfo) override; 246 247 /** 248 * @brief Get node information based on inspectorKey in active window. 249 * @param inspectorKey The target inspectorKey. 250 * @param elementInfo The element info of specified content. 251 * @return Return RET_OK if gets info successfully, otherwise refer to the RetError for the failure. 252 */ 253 virtual RetError SearchElementInfoByInspectorKey(const std::string &inspectorKey, 254 AccessibilityElementInfo &elementInfo) override; 255 256 /** 257 * @brief Executes a specified action. 258 * @param elementInfo The source info to execute action. 259 * @param action: the action type 260 * @param actionArguments: The parameter for action type. such as: 261 * action: ACCESSIBILITY_ACTION_NEXT_HTML_ITEM, 262 * actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType) 263 * action: ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM, 264 * actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType) 265 * action: ACCESSIBILITY_ACTION_NEXT_TEXT, 266 * actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX) 267 * action: ACCESSIBILITY_ACTION_PREVIOUS_TEXT, 268 * actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX) 269 * action: ACCESSIBILITY_ACTION_SET_SELECTION, 270 * actionArguments({ACTION_ARGU_SELECT_TEXT_START,"1"(start location)}, 271 * {ACTION_ARGU_SELECT_TEXT_END,"10"(end location)}) 272 * action: ACCESSIBILITY_ACTION_SET_TEXT, 273 * actionArguments(ACTION_ARGU_SET_TEXT,"the text of setted") 274 * @return Return RET_OK if performs action succeed, otherwise refer to the RetError for the failure. 275 */ 276 virtual RetError ExecuteAction(const AccessibilityElementInfo &elementInfo, const ActionType action, 277 const std::map<std::string, std::string> &actionArguments) override; 278 279 /** 280 * @brief Curtain screen ability. 281 * @param isEnable Flag bits for opening or closing. 282 */ 283 virtual RetError EnableScreenCurtain(bool isEnable) override; 284 285 /** 286 * @brief To return the result of cursor position. 287 * @param elementInfo The source info to cursor position. 288 * @param position: The position of the cursor to get. 289 * @return Return RET_OK if performs action succeed, otherwise refer to the RetError for the failure. 290 */ 291 virtual RetError GetCursorPosition(const AccessibilityElementInfo &elementInfo, int32_t &position) override; 292 293 /** 294 * @brief Set target bundle names. 295 * @param targetBundleNames The target bundle name 296 * @return Return RET_OK if sets target bundle names successfully, otherwise refer to the RetError for the failure. 297 */ 298 virtual RetError SetTargetBundleName(const std::vector<std::string> &targetBundleNames) override; 299 300 /** 301 * @brief Set cache mode. 302 * The mode is used for functions: GetRoot, GetRootByWindow, GetChildElementInfo, 303 * GetChildren, GetSource, GetParentElementInfo. 304 * @param cacheMode The cache mode. It includes: 305 * PREFETCH_PREDECESSORS: cache the parent node info also. 306 * PREFETCH_SIBLINGS: cache the sister/brothers node info also. 307 * PREFETCH_CHILDREN: cache the child node info also. 308 * otherwise: no cache. 309 * @return Return RET_OK if sets cache mode successfully, otherwise refer to the RetError for the failure. 310 */ 311 virtual RetError SetCacheMode(const int32_t cacheMode) override; 312 313 /** 314 * @brief Search all child nodes 315 * @param windowId The target window id. 316 * @param elementId The target element id. 317 * @param elementInfos The element infos of specified content. 318 * @return Return RET_OK if gets all child nodes successfully, otherwise refer to the RetError for the failure. 319 */ 320 virtual RetError GetElements(const int32_t windowId, const int64_t elementId, 321 std::vector<AccessibilityElementInfo> &elementInfos) override; 322 323 /** 324 * @brief Search all child nodes 325 * @param windowId The target window id. 326 * @param elementInfos The element infos of specified content. 327 * @return Return RET_OK if gets all child nodes successfully, otherwise refer to the RetError for the failure. 328 */ 329 virtual RetError GetDefaultFocusedElementIds(const int32_t windowId, 330 std::vector<AccessibilityElementInfo> &elementInfos) override; 331 332 /** 333 * @brief Clean data. 334 * @param remote The object access to AAMS. 335 */ 336 void ResetAAClient(const wptr<IRemoteObject> &remote); 337 338 /** 339 * @brief Notify AA client and clean data when service is died. 340 * @param remote The object access to AAMS. 341 */ 342 void NotifyServiceDied(const wptr<IRemoteObject> &remote); 343 344 /** 345 * @brief Connect to AAMS. For UI test. 346 * @return Return RET_OK if the command of connection is sent successfully, 347 * otherwise refer to the RetError for the failure. 348 */ 349 RetError Connect(); 350 351 /** 352 * @brief disconnect to AAMS. For UI test. 353 * @return Return RET_OK if the command of disconnect is sent successfully, 354 * otherwise refer to the RetError for the failure. 355 */ 356 RetError Disconnect(); 357 358 /** 359 * @brief Set connection state. 360 * @param state Connnection state. 361 */ 362 void SetConnectionState(bool state); 363 364 /** 365 * @brief Find the node information by accessibility ID. 366 * @param accessibilityWindowId The window id that the component belongs to. 367 * @param elementId: The unique id of the component ID. 368 * @param mode PREFETCH_PREDECESSORS: Need to make the parent node info also. 369 * PREFETCH_SIBLINGS: Need to make the sister/brothers node info also. 370 * PREFETCH_CHILDREN: Need to make the child node info also. 371 * otherwise: Make the node information by elementId only. 372 * @param info[out] The components information matched conditions searched. 373 * @return Return RET_OK if search element info successfully, otherwise refer to the RetError for the failure. 374 */ 375 RetError SearchElementInfoByAccessibilityId(const int32_t windowId, const int64_t elementId, 376 const uint32_t mode, AccessibilityElementInfo &info, bool isFilter = false) override; 377 378 void AddWindowElementMapByWMS(int32_t windowId, int64_t elementId); 379 void AddWindowElementMapByAce(int32_t windowId, int64_t elementId); 380 RetError GetElementInfoFromCache(int32_t windowId, int64_t elementId, 381 std::vector<AccessibilityElementInfo> &elementInfos); 382 RetError SearchElementInfoRecursive(int32_t windowId, int64_t elementId, uint32_t mode, 383 std::vector<AccessibilityElementInfo> &elementInfos, bool isFilter = false); 384 RetError SearchElementInfoRecursiveByWinid(const int32_t windowId, const int64_t elementId, 385 uint32_t mode, std::vector<AccessibilityElementInfo> &elementInfos, int32_t treeId, bool isFilter = false, 386 uint64_t parentIndex = 0); 387 RetError SearchElementInfoRecursiveByContent(const int32_t windowId, const int64_t elementId, 388 uint32_t mode, std::vector<AccessibilityElementInfo> &elementInfos, const std::string text, int32_t treeId, 389 bool isFilter = false); 390 void RemoveCacheData(const AccessibilityEventInfo &eventInfo); 391 void AddCacheByWMS(int32_t windowId, int64_t elementId, std::vector<AccessibilityElementInfo>& elementInfos); 392 void AddCacheByAce(int32_t windowId, int64_t elementId, std::vector<AccessibilityElementInfo>& elementInfos); 393 void SortElementInfosIfNecessary(std::vector<AccessibilityElementInfo> &elementInfos); 394 395 bool LoadAccessibilityService(); 396 void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject); 397 void LoadSystemAbilityFail(); 398 RetError GetChildrenWork(const int32_t windowId, std::vector<int64_t> childIds, 399 std::vector<AccessibilityElementInfo> &children); 400 401 private: 402 class ElementCacheInfo { 403 public: 404 ElementCacheInfo() = default; 405 ~ElementCacheInfo() = default; 406 void RemoveElementByWindowId(const int32_t windowId); 407 bool GetElementByWindowId(const int32_t windowId, const int64_t elementId, 408 std::vector<AccessibilityElementInfo>& elementInfos); 409 bool GetElementByWindowIdBFS(const int64_t elementId, std::vector<AccessibilityElementInfo>& elementInfos, 410 std::map<int32_t, std::shared_ptr<AccessibilityElementInfo>>& cache); 411 void AddElementCache(int32_t windowId, const std::vector<AccessibilityElementInfo>& elementInfos); 412 bool IsExistWindowId(int32_t windowId); 413 private: 414 std::map<int32_t, std::map<int32_t, std::shared_ptr<AccessibilityElementInfo>>> elementCache_; 415 std::deque<int32_t> windowIdSet_; 416 ffrt::mutex elementCacheMutex_; 417 }; 418 419 class SceneBoardWindowElementMap { 420 public: 421 SceneBoardWindowElementMap() = default; 422 ~SceneBoardWindowElementMap() = default; 423 bool IsExistWindowId(int32_t windowId); 424 void AddWindowElementIdPair(int32_t windowId, int64_t elementId); 425 std::vector<int32_t> GetWindowIdList(); 426 int32_t GetWindowIdByElementId(int64_t elementId); 427 void RemovePairByWindowIdList(std::vector<int32_t>& windowIdList); 428 void RemovePairByWindowId(int32_t windowId); 429 private: 430 std::map<int32_t, int64_t> windowElementMap_; 431 ffrt::mutex mapMutex_; 432 }; 433 434 class AccessibleAbilityDeathRecipient final : public IRemoteObject::DeathRecipient { 435 public: AccessibleAbilityDeathRecipient(AccessibleAbilityClientImpl & client)436 AccessibleAbilityDeathRecipient(AccessibleAbilityClientImpl &client) : client_(client) {} 437 ~AccessibleAbilityDeathRecipient() = default; 438 DISALLOW_COPY_AND_MOVE(AccessibleAbilityDeathRecipient); 439 440 void OnRemoteDied(const wptr<IRemoteObject> &remote); 441 private: 442 AccessibleAbilityClientImpl &client_; 443 }; 444 445 class AccessibilityServiceDeathRecipient final : public IRemoteObject::DeathRecipient { 446 public: AccessibilityServiceDeathRecipient(AccessibleAbilityClientImpl & client)447 AccessibilityServiceDeathRecipient(AccessibleAbilityClientImpl &client) : client_(client) {} 448 ~AccessibilityServiceDeathRecipient() = default; 449 DISALLOW_COPY_AND_MOVE(AccessibilityServiceDeathRecipient); 450 451 void OnRemoteDied(const wptr<IRemoteObject> &remote); 452 private: 453 AccessibleAbilityClientImpl &client_; 454 }; 455 456 class AccessibilityLoadCallback : public SystemAbilityLoadCallbackStub { 457 public: 458 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, 459 const sptr<IRemoteObject> &remoteObject) override; 460 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; 461 }; 462 463 bool GetCacheElementInfo(const int32_t windowId, 464 const int64_t elementId, AccessibilityElementInfo &elementInfo); 465 void SetCacheElementInfo(const int32_t windowId, 466 const std::vector<OHOS::Accessibility::AccessibilityElementInfo> &elementInfos); 467 RetError SearchElementInfoByElementId(const int32_t windowId, const int64_t elementId, 468 const uint32_t mode, AccessibilityElementInfo &info, int32_t treeId); 469 RetError SearchElementInfoFromAce(const int32_t windowId, const int64_t elementId, 470 const uint32_t mode, AccessibilityElementInfo &info); 471 bool InitAccessibilityServiceProxy(); 472 static void OnParameterChanged(const char *key, const char *value, void *context); 473 bool CheckServiceProxy(); // should be used in mutex 474 RetError CheckConnection(); // should be used in mutex, to check isConnected_ and channelClient_ 475 476 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 477 sptr<IRemoteObject::DeathRecipient> accessibilityServiceDeathRecipient_ = nullptr; 478 sptr<IAccessibleAbilityManagerService> serviceProxy_ = nullptr; 479 std::shared_ptr<AccessibleAbilityListener> listener_ = nullptr; 480 std::shared_ptr<AccessibleAbilityChannelClient> channelClient_ = nullptr; 481 uint32_t cacheMode_ = 0; 482 int32_t cacheWindowId_ = -1; 483 SafeMap<int64_t, AccessibilityElementInfo> cacheElementInfos_; 484 ffrt::mutex mutex_; 485 std::atomic<bool> isConnected_ = false; 486 // used for query element info in batch 487 ElementCacheInfo elementCacheInfo_; 488 SceneBoardWindowElementMap windowElementMap_; 489 490 ffrt::condition_variable proxyConVar_; 491 ffrt::mutex conVarMutex_; 492 Utils::RWLock rwLock_; 493 }; 494 } // namespace Accessibility 495 } // namespace OHOS 496 #endif // ACCESSIBLE_ABILITY_CLIENT_IMPL_H