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