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