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 #ifndef ACCESSIBLE_ABILITY_CLIENT_H 17 #define ACCESSIBLE_ABILITY_CLIENT_H 18 19 #include <map> 20 #include <memory> 21 #include "accessibility_element_info.h" 22 #include "accessibility_event_info.h" 23 #include "accessibility_gesture_inject_path.h" 24 #include "accessibility_window_info.h" 25 #include "accessible_ability_listener.h" 26 #include "iremote_object.h" 27 #include "hilog_wrapper.h" 28 #include "napi/native_api.h" 29 #include "napi/native_node_api.h" 30 31 namespace OHOS { 32 namespace Accessibility { 33 struct DisconnectCallback { DisconnectCallbackDisconnectCallback34 DisconnectCallback(napi_env env, napi_ref ref) : handlerRef_(ref), env_(env) {}; ~DisconnectCallbackDisconnectCallback35 ~DisconnectCallback() {}; TmpOpenScopeDisconnectCallback36 static napi_handle_scope TmpOpenScope(napi_env env) 37 { 38 napi_handle_scope scope = nullptr; 39 NAPI_CALL(env, napi_open_handle_scope(env, &scope)); 40 return scope; 41 } 42 NotifyJSDisconnectCallback43 void NotifyJS() 44 { 45 HILOG_INFO(); 46 auto task = [this]() { 47 napi_env tmpEnv = env_; 48 auto closeScope = [tmpEnv](napi_handle_scope scope) { 49 napi_close_handle_scope(tmpEnv, scope); 50 }; 51 std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scope(TmpOpenScope(env_), closeScope); 52 napi_value handler = nullptr; 53 napi_value callResult = nullptr; 54 napi_value jsEvent = nullptr; 55 napi_get_reference_value(env_, handlerRef_, &handler); 56 napi_value undefined = nullptr; 57 napi_get_undefined(env_, &undefined); 58 napi_call_function(env_, undefined, handler, 0, &jsEvent, &callResult); 59 }; 60 if (napi_send_event(env_, task, napi_eprio_high) != napi_status::napi_ok) { 61 HILOG_ERROR("failed to send event"); 62 } 63 } 64 65 bool operator==(const DisconnectCallback& otherCallback) const 66 { 67 if (env_ != otherCallback.env_) { 68 return false; 69 } 70 napi_value item = nullptr; 71 napi_get_reference_value(env_, handlerRef_, &item); 72 napi_value otherItem = nullptr; 73 napi_get_reference_value(otherCallback.env_, otherCallback.handlerRef_, &otherItem); 74 bool equalFlag = false; 75 napi_status status = napi_strict_equals(env_, item, otherItem, &equalFlag); 76 if (status == napi_ok && equalFlag) { 77 return true; 78 } 79 return false; 80 } 81 82 napi_ref handlerRef_ = nullptr; 83 napi_env env_ = nullptr; 84 }; 85 86 class AccessibleAbilityClient : public virtual RefBase { 87 public: 88 /** 89 * @brief Destruct 90 */ 91 virtual ~AccessibleAbilityClient() = default; 92 93 /** 94 * @brief Gets an instance of AccessibleAbilityClient. 95 * @return Return an instance of AccessibleAbilityClient. 96 */ 97 static sptr<AccessibleAbilityClient> GetInstance(); 98 99 /** 100 * @brief Gets remote object. 101 * @return Remote object. 102 */ 103 virtual sptr<IRemoteObject> GetRemoteObject() = 0; 104 105 /** 106 * @brief Obtains elementInfo of 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 GetFocus( 112 const int32_t focusType, AccessibilityElementInfo& elementInfo, bool systemApi = false) = 0; 113 114 /** 115 * @brief Obtains elementInfo of focus. 116 * @param sourceInfo The source info to get focus. 117 * @param focusType The type of focus. It contains FOCUS_TYPE_INPUT and FOCUS_TYPE_ACCESSIBILITY. 118 * @param elementInfo The accessibilityElementInfo of focus. 119 * @return Return RET_OK if obtains elementInfo successfully, otherwise refer to the RetError for the failure. 120 */ 121 virtual RetError GetFocusByElementInfo(const AccessibilityElementInfo &sourceInfo, const int32_t focusType, 122 AccessibilityElementInfo &elementInfo) = 0; 123 124 /** 125 * @brief Sends simulate gestures to the screen. 126 * @param gesturePath The gesture which need to send. 127 * @return Return RET_OK if the gesture sends successfully, otherwise refer to the RetError for the failure. 128 */ 129 virtual RetError InjectGesture(const std::shared_ptr<AccessibilityGestureInjectPath> &gesturePath) = 0; 130 131 /** 132 * @brief Obtains elementInfo of the accessible root node. 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 GetRoot(AccessibilityElementInfo &elementInfo, bool systemApi = false) = 0; 137 138 /** 139 * @brief Obtains elementInfo of the accessible root node. 140 * @param windowInfo The source window info to get root. 141 * @param elementInfo The elementInfo of the accessible root node. 142 * @return Return RET_OK if obtains elementInfo successfully, otherwise refer to the RetError for the failure. 143 */ 144 virtual RetError GetRootByWindow(const AccessibilityWindowInfo &windowInfo, 145 AccessibilityElementInfo &elementInfo, bool systemApi = false) = 0; 146 147 /** 148 * @brief Obtains elementInfos of the accessible root node in batchs. 149 * @param elementInfos ElementInfos of the accessible root node and its recursive subnodes. 150 * @return Return RET_OK if obtains elementInfos successfully, otherwise refer to the RetError for the failure. 151 */ 152 virtual RetError GetRootBatch(std::vector<AccessibilityElementInfo>& elementInfos) = 0; 153 154 /** 155 * @brief Obtains elementInfos of the accessible root node in batchs. 156 * @param windowInfo The source window info to get root. 157 * @param elementInfos ElementInfos of the accessible root node and its recursive subnodes. 158 * @param isFilter Indicates whether to filter nodes. 159 * @param needCut Indicates whether to remove invisible nodes. 160 * @return Return RET_OK if obtains elementInfos successfully, otherwise refer to the RetError for the failure. 161 */ 162 virtual RetError GetRootByWindowBatch(const AccessibilityWindowInfo &windowInfo, 163 std::vector<AccessibilityElementInfo>& elementInfos, bool isFilter = false, bool needCut = false) = 0; 164 165 /** 166 * @brief Get the window information related with the event 167 * @param windowId The window id. 168 * @param windowInfo The window information. 169 * @return Return RET_OK if obtains windowInfo successfully, otherwise refer to the RetError for the failure. 170 */ 171 virtual RetError GetWindow(const int32_t windowId, AccessibilityWindowInfo &windowInfo) = 0; 172 173 /** 174 * @brief Obtains the list of interactive windows on the device, in the layers they are visible to users. 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(std::vector<AccessibilityWindowInfo> &windows, bool systemApi = false) = 0; 179 180 /** 181 * @brief Obtains the list of interactive windows on the device, in the layers they are visible to users. 182 * @param displayId the id of display 183 * @param windows The information of windows. 184 * @return Return RET_OK if obtains windowInfo successfully, otherwise refer to the RetError for the failure. 185 */ 186 virtual RetError GetWindows( 187 const uint64_t displayId, std::vector<AccessibilityWindowInfo>& windows, bool systemApi = false) = 0; 188 189 /** 190 * @brief Gets the next focused node in the specified direction of the currently focused node. 191 * @param elementInfo The source info to get next info. 192 * @param direction Indicates the direction to obtain the next focused node. Refer to FocusMoveDirection 193 * @param nextElementInfo The info of next element. 194 * @return Return RET_OK if gets next elementInfo successfully, otherwise refer to the RetError for the failure. 195 */ 196 virtual RetError GetNext(const AccessibilityElementInfo &elementInfo, const FocusMoveDirection direction, 197 AccessibilityElementInfo &nextElementInfo, bool systemApi = false) = 0; 198 199 /** 200 * @brief Get the child node information by child index. 201 * @param index The index of the child. 202 * @param parent The parent info to get child. 203 * @param child The element info of child. 204 * @return Return RET_OK if gets child elementInfo successfully, otherwise refer to the RetError for the failure. 205 */ 206 virtual RetError GetChildElementInfo(const int32_t index, const AccessibilityElementInfo &parent, 207 AccessibilityElementInfo &child) = 0; 208 209 /** 210 * @brief Get the children node information 211 * @param parent The parent info to get children. 212 * @param children The element info of children. 213 * @return Return RET_OK if gets children elementInfo successfully, otherwise refer to the RetError for the failure. 214 */ 215 virtual RetError GetChildren(const AccessibilityElementInfo &parent, 216 std::vector<AccessibilityElementInfo> &children, bool systemApi = false) = 0; 217 218 /** 219 * @brief Searches for node information based on the specified content. 220 * @param elementInfo The source info. 221 * @param text specified content 222 * @param elementInfos The element infos of specified content. 223 * @return Return RET_OK if gets elementInfos successfully, otherwise refer to the RetError for the failure. 224 */ 225 virtual RetError GetByContent(const AccessibilityElementInfo &elementInfo, const std::string &text, 226 std::vector<AccessibilityElementInfo> &elementInfos, bool systemApi = false) = 0; 227 228 /** 229 * @brief Get the node information related with the event 230 * @param eventInfo The source info to get source. 231 * @param elementInfo The element info of source. 232 * @return Return RET_OK if gets elementInfos successfully, otherwise refer to the RetError for the failure. 233 */ 234 virtual RetError GetSource(const AccessibilityEventInfo &eventInfo, AccessibilityElementInfo &elementInfo) = 0; 235 236 /** 237 * @brief Get Parent node information 238 * @param child The child element info to get parent. 239 * @param parent The parent element info. 240 * @return Return RET_OK if gets info successfully, otherwise refer to the RetError for the failure. 241 */ 242 virtual RetError GetParentElementInfo( 243 const AccessibilityElementInfo& child, AccessibilityElementInfo& parent, bool systemApi = false) = 0; 244 245 /** 246 * @brief Get node information based on element id in active window. 247 * @param elementId The target element id. 248 * @param windowId The target window id. 249 * @param targetElementInfo The element info of specified content. 250 * @return Return RET_OK if gets info successfully, otherwise refer to the RetError for the failure. 251 */ 252 virtual RetError GetByElementId(const int64_t elementId, const int32_t windowId, 253 AccessibilityElementInfo &targetElementInfo, bool systemApi = false) = 0; 254 255 /** 256 * @brief Get node information based on inspectorKey in active window. 257 * @param inspectorKey The target inspectorKey. 258 * @param elementInfo The element info of specified content. 259 * @return Return RET_OK if gets info successfully, otherwise refer to the RetError for the failure. 260 */ 261 virtual RetError SearchElementInfoByInspectorKey(const std::string &inspectorKey, 262 AccessibilityElementInfo &elementInfo) = 0; 263 264 /** 265 * @brief Executes a specified action. 266 * @param elementInfo The source info to execute action. 267 * @param action: the action type 268 * @param actionArguments: The parameter for action type. such as: 269 * action: ACCESSIBILITY_ACTION_NEXT_HTML_ITEM, 270 * actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType) 271 * action: ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM, 272 * actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType) 273 * action: ACCESSIBILITY_ACTION_NEXT_TEXT, 274 * actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX) 275 * action: ACCESSIBILITY_ACTION_PREVIOUS_TEXT, 276 * actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX) 277 * action: ACCESSIBILITY_ACTION_SET_SELECTION, 278 * actionArguments({ACTION_ARGU_SELECT_TEXT_START,"1"(start location)}, 279 * {ACTION_ARGU_SELECT_TEXT_END,"10"(end location)}) 280 * action: ACCESSIBILITY_ACTION_SET_TEXT, 281 * actionArguments(ACTION_ARGU_SET_TEXT,"the text of setted") 282 * @return Return RET_OK if performs action succeed, otherwise refer to the RetError for the failure. 283 */ 284 virtual RetError ExecuteAction(const AccessibilityElementInfo &elementInfo, const ActionType action, 285 const std::map<std::string, std::string> &actionArguments) = 0; 286 287 /** 288 * @brief Enable status value. 289 * @param isEnable flag Screen Curtain. 290 * @return Return RET_OK if registers listener successfully, otherwise refer to the RetError for the failure. 291 */ 292 virtual RetError EnableScreenCurtain(bool isEnable) = 0; 293 294 /** 295 * @brief To return the result of cursor position. 296 * @param elementInfo The source info to cursor position. 297 * @param position: The position of the cursor to get. 298 */ 299 virtual RetError GetCursorPosition(const AccessibilityElementInfo &elementInfo, int32_t &position) = 0; 300 301 /** 302 * @brief Check the permission of the extension ability. 303 * @return Return RET_OK if the ability has the permission. 304 */ 305 virtual RetError CheckExtensionAbilityPermission() = 0; 306 307 /** 308 * @brief Register ability listener. 309 * @param listener The listener to add. 310 * @return Return RET_OK if registers listener successfully, otherwise refer to the RetError for the failure. 311 */ 312 virtual RetError RegisterAbilityListener(const std::shared_ptr<AccessibleAbilityListener> &listener) = 0; 313 314 /** 315 * @brief Set target bundle names. 316 * @param targetBundleNames The target bundle name 317 * @return Return RET_OK if sets target bundle names successfully, otherwise refer to the RetError for the failure. 318 */ 319 virtual RetError SetTargetBundleName(const std::vector<std::string> &targetBundleNames) = 0; 320 321 /** 322 * @brief Set cache mode. 323 * The mode is used for functions: GetRoot, GetRootByWindow, GetChildElementInfo, 324 * GetChildren, GetSource, GetParentElementInfo. 325 * @param cacheMode The cache mode. It includes: 326 * PREFETCH_PREDECESSORS: cache the parent node info also. 327 * PREFETCH_SIBLINGS: cache the sister/brothers node info also. 328 * PREFETCH_CHILDREN: cache the child node info also. 329 * otherwise: no cache. 330 * @return Return RET_OK if sets cache mode successfully, otherwise refer to the RetError for the failure. 331 */ 332 virtual RetError SetCacheMode(const int32_t cacheMode) = 0; 333 334 /** 335 * @brief Find the node information by accessibility ID. 336 * @param accessibilityWindowId The window id that the component belongs to. 337 * @param elementId: The unique id of the component ID. 338 * @param mode PREFETCH_PREDECESSORS: Need to make the parent node info also. 339 * PREFETCH_SIBLINGS: Need to make the sister/brothers node info also. 340 * PREFETCH_CHILDREN: Need to make the child node info also. 341 * otherwise: Make the node information by elementId only. 342 * @param info[out] The components information matched conditions searched. 343 * @return Return RET_OK if search element info successfully, otherwise refer to the RetError for the failure. 344 */ 345 virtual RetError SearchElementInfoByAccessibilityId(const int32_t windowId, const int64_t elementId, 346 const uint32_t mode, AccessibilityElementInfo &info, bool isFilter = false) = 0; 347 348 /** 349 * @brief Search all child nodes 350 * @param windowId The target window id. 351 * @param elementId The target element id. 352 * @param elementInfos The element infos of specified content. 353 * @return Return RET_OK if gets all child nodes successfully, otherwise refer to the RetError for the failure. 354 */ 355 virtual RetError GetElements(const int32_t windowId, const int64_t elementId, 356 std::vector<AccessibilityElementInfo> &elementInfos) = 0; 357 358 /** 359 * @brief Search Default Focused ElementId 360 * @param windowId The target window id. 361 * @param elementInfos The element infos of specified content. 362 * @return Return RET_OK if gets all Default Focused successfully, otherwise refer to the RetError for the failure. 363 */ 364 virtual RetError GetDefaultFocusedElementIds(const int32_t windowId, 365 std::vector<AccessibilityElementInfo> &elementInfos) = 0; 366 367 /** 368 * @brief Hold running lock to prevent screen turning off automatically. 369 * @param null. 370 * @return Return RET_OK if hold running lock successfully, otherwise refer to the RetError for the failure. 371 */ 372 virtual RetError HoldRunningLock() = 0; 373 374 /** 375 * @brief Unhold running lock to prevent screen turning off automatically 376 * @param null. 377 * @return Return RET_OK if Unhold running lock successfully, otherwise refer to the RetError for the failure. 378 */ 379 virtual RetError UnholdRunningLock() = 0; 380 381 /** 382 * @brief Register Disconnect Callback. 383 * @param callback The callback. 384 * @return Return RET_OK if Register Callback successfully, otherwise refer to the RetError for the failure. 385 */ 386 virtual RetError RegisterDisconnectCallback(std::shared_ptr<DisconnectCallback> &callback) = 0; 387 388 /** 389 * @brief UnRegister Disconnect Callback. 390 * @return Return RET_OK if unRegister callback successfully, otherwise refer to the RetError for the failure. 391 */ 392 virtual RetError UnRegisterDisconnectCallback(std::shared_ptr<DisconnectCallback> &callback) = 0; 393 394 /** 395 * @brief Notify disconnect. 396 * @return Return RET_OK if notifyDisconnect successfully, otherwise refer to the RetError for the failure. 397 */ 398 virtual RetError NotifyDisconnect() = 0; 399 400 /** 401 * @brief Config need events. 402 * @param needEvents The need events. 403 * @return Return RET_OK if config need events successfully, otherwise refer to the RetError for the failure. 404 */ 405 virtual RetError ConfigureEvents(std::vector<uint32_t> needEvents) = 0; 406 }; 407 } // namespace Accessibility 408 } // namespace OHOS 409 #endif // ACCESSIBLE_ABILITY_CLIENT_H