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 ACCESSIBILITY_SYSTEM_ABILITY_CLIENT_IMPL_H 17 #define ACCESSIBILITY_SYSTEM_ABILITY_CLIENT_IMPL_H 18 19 #include <array> 20 #include "accessibility_element_operator_impl.h" 21 #include "accessibility_system_ability_client.h" 22 #include "accessible_ability_manager_state_observer_stub.h" 23 #include "ffrt.h" 24 #include "ffrt_inner.h" 25 #include "i_accessible_ability_manager_service.h" 26 #include "refbase.h" 27 #include "system_ability_load_callback_stub.h" 28 #include "system_ability_status_change_stub.h" 29 30 namespace OHOS { 31 namespace Accessibility { 32 using StateArray = std::array<bool, AccessibilityStateEventType::EVENT_TYPE_MAX>; 33 using StateObserverVector = std::vector<std::shared_ptr<AccessibilityStateObserver>>; 34 using StateObserversArray = std::array<StateObserverVector, AccessibilityStateEventType::EVENT_TYPE_MAX>; 35 class AccessibilitySystemAbilityClientImpl 36 : public AccessibilitySystemAbilityClient, public AccessibilityElementOperatorCallback { 37 public: 38 /** 39 * @brief Construct. 40 */ 41 AccessibilitySystemAbilityClientImpl(); 42 43 /** 44 * @brief Deconstruct. 45 */ 46 ~AccessibilitySystemAbilityClientImpl(); 47 48 /** 49 * @brief Register the element operator, so the AA can get node info from ACE. 50 * @param windowId Window ID 51 * @param operation The callback object. 52 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 53 */ 54 virtual RetError RegisterElementOperator(const int32_t windowId, 55 const std::shared_ptr<AccessibilityElementOperator> &operation) override; 56 57 /** 58 * @brief Register the element operator, so the AA can get node info from ACE. 59 * @param parameter The Register parameters. 60 * @param operation The callback object. 61 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 62 */ 63 virtual RetError RegisterElementOperator(Registration parameter, 64 const std::shared_ptr<AccessibilityElementOperator> &operation) override; 65 66 /** 67 * @brief Deregister the element operator. 68 * @param windowId Window ID 69 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 70 */ 71 virtual RetError DeregisterElementOperator(const int32_t windowId) override; 72 73 /** 74 * @brief Deregister the element operator. 75 * @param windowId Window ID 76 * @param treeId Tree ID 77 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 78 */ 79 virtual RetError DeregisterElementOperator(const int32_t windowId, const int32_t treeId) override; 80 81 /** 82 * @brief Checks whether screenreader ability is enabled. 83 * @param isEnabled true: enabled; false: disabled 84 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 85 */ 86 virtual RetError IsScreenReaderEnabled(bool &isEnabled) override; 87 88 /** 89 * @brief Checks whether accessibility ability is enabled. 90 * @param isEnabled true: enabled; false: disabled 91 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 92 */ 93 virtual RetError IsEnabled(bool &isEnabled) override; 94 95 /** 96 * @brief Checks whether touch exploration ability is enabled. 97 * @param isEnabled true: enabled; false: disabled 98 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 99 */ 100 virtual RetError IsTouchExplorationEnabled(bool &isEnabled) override; 101 102 /** 103 * @brief Queries the list of accessibility abilities. 104 * @param accessibilityAbilityTypes Indicates the accessibility type specified by AccessibilityAbilityTypes. 105 * @param stateType Indicates the accessibility ability status. 106 * 1 indicates that the ability is enabled; 107 * 2 indicates that the ability is disabled; 108 * 3 indicates that the ability has been installed. 109 * @param infos accessibility ability infos by specified types. 110 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 111 */ 112 virtual RetError GetAbilityList(const uint32_t accessibilityAbilityTypes, const AbilityStateType stateType, 113 std::vector<AccessibilityAbilityInfo> &infos) override; 114 115 /** 116 * @brief Sends an accessibility event. 117 * @param eventType Identifies the accessibility event specified by AccessibilityEventInfo. 118 * @param componentId Indicates the ID of the component to be associated with the event. 119 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 120 */ 121 virtual RetError SendEvent(const EventType eventType, const int64_t componentId) override; 122 123 /** 124 * @brief Sends information about an accessibility event. 125 * @param event Indicates the accessibility event information specified by AccessibilityEventInfo. 126 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 127 */ 128 virtual RetError SendEvent(const AccessibilityEventInfo &event) override; 129 130 /** 131 * @brief Subscribes to the specified type of accessibility status change events. 132 * @param observer Indicates the observer for listening to status events, which is specified 133 * by AccessibilityStateObserver. 134 * @param eventType Indicates the status type, which is specified by AccessibilityStateEventType. 135 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 136 */ 137 virtual RetError SubscribeStateObserver(const std::shared_ptr<AccessibilityStateObserver> &observer, 138 const uint32_t eventType) override; 139 140 /** 141 * @brief Unsubscribe the specified type of accessibility status change events. 142 * @param observer Indicates the registered accessibility status event observer. 143 * @param eventType Indicates the status type, which is specified by AccessibilityStateEventType. 144 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 145 */ 146 virtual RetError UnsubscribeStateObserver(const std::shared_ptr<AccessibilityStateObserver> &observer, 147 const uint32_t eventType) override; 148 149 /** 150 * @brief Get enabled abilities. 151 * @param enabledAbilities The infos of enabled abilities. 152 * @return Return RET_OK if successful, otherwise refer to the RetError for the failure. 153 */ 154 virtual RetError GetEnabledAbilities(std::vector<std::string> &enabledAbilities) override; 155 156 /** 157 * @brief Clean the AAMS object data. 158 * @param remote The object access to AAMS. 159 */ 160 void ResetService(const wptr<IRemoteObject> &remote); 161 162 /** 163 * @brief Receive the state notify from AAMS and send it to the observer registered. 164 * @param stateType The state type and value. 165 * state type: Refer to AccessibilityStateEventType. 166 * value: STATE_ACCESSIBILITY_ENABLED/STATE_EXPLORATION_ENABLED/ 167 * STATE_ACCESSIBILITY_DISABLED/STATE_EXPLORATION_DISABLED 168 */ 169 void OnAccessibleAbilityManagerStateChanged(const uint32_t stateType); 170 171 /** 172 * @brief Set the accessibility state. 173 * @param stateType The state type and value. 174 * state type: Refer to AccessibilityStateEventType. 175 * value: STATE_ACCESSIBILITY_ENABLED/STATE_EXPLORATION_ENABLED/ 176 * STATE_ACCESSIBILITY_DISABLED/STATE_EXPLORATION_DISABLED 177 */ 178 void SetAccessibilityState(const uint32_t stateType); 179 180 /** 181 * @brief Set the element information by accessibility id to AA. 182 * @param infos The element info searched by accessibility id. 183 * @param requestId The request id from AA, it is used to match with request and response. 184 */ 185 virtual void SetSearchElementInfoByAccessibilityIdResult(const std::list<AccessibilityElementInfo> &infos, 186 const int32_t requestId) override; 187 188 /** 189 * @brief Set the element information by window id to AA. 190 * @param infos The element info searched by window id. 191 * @param requestId The request id from AA, it is used to match with request and response. 192 */ 193 virtual void SetSearchDefaultFocusByWindowIdResult(const std::list<AccessibilityElementInfo> &infos, 194 const int32_t requestId) override; 195 196 /** 197 * @brief Set the element information matched with text to AA. 198 * @param infos The element information searched matched with text. 199 * @param requestId The request id from AA, it is used to match with request and response. 200 */ 201 virtual void SetSearchElementInfoByTextResult(const std::list<AccessibilityElementInfo> &infos, 202 const int32_t requestId) override; 203 204 /** 205 * @brief Set the element information matched with focus type to AA. 206 * @param info The element information searched matched with focus type. 207 * @param requestId The request id from AA, it is used to match with request and response. 208 */ 209 virtual void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info, 210 const int32_t requestId) override; 211 212 /** 213 * @brief Set the element information by focus direction to AA. 214 * @param info The element information searched by focus direction. 215 * @param requestId The request id from AA, it is used to match with request and response. 216 */ 217 virtual void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId) override; 218 219 /** 220 * @brief Set the result of action executed to AA. 221 * @param succeeded True: The action is executed successfully; otherwise is false. 222 * @param requestId The request id from AA, it is used to match with request and response. 223 */ 224 virtual void SetExecuteActionResult(const bool succeeded, const int32_t requestId) override; 225 226 /** 227 * @brief Set the result of cursor position to AA. 228 * @param cursorPosition The cursorPosition to be returned. 229 * @param requestId The request id from AA, it is used to match with request and response. 230 */ 231 virtual void SetCursorPositionResult(const int32_t cursorPosition, const int32_t requestId) override; 232 233 virtual uint32_t GetAccessibilityState() override; 234 virtual void SetFindAccessibilityNodeInfoResult(const AccessibilityElementInfo elementInfo, 235 const int32_t requestId, const int32_t requestCode) override; 236 virtual void SetFindAccessibilityNodeInfosResult(const std::list<AccessibilityElementInfo> elementInfos, 237 const int32_t requestId, const int32_t requestCode) override; 238 virtual void SetPerformActionResult(const bool succeeded, const int32_t requestId) override; 239 virtual RetError GetFocusedWindowId(int32_t &focusedWindowId) override; 240 241 bool LoadAccessibilityService(); 242 void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject); 243 void LoadSystemAbilityFail(); 244 245 private: 246 class AccessibleAbilityManagerStateObserverImpl : public AccessibleAbilityManagerStateObserverStub { 247 public: AccessibleAbilityManagerStateObserverImpl(AccessibilitySystemAbilityClientImpl & client)248 explicit AccessibleAbilityManagerStateObserverImpl(AccessibilitySystemAbilityClientImpl &client) 249 : client_(client) {} 250 ~AccessibleAbilityManagerStateObserverImpl() = default; 251 OnStateChanged(const uint32_t stateType)252 virtual void OnStateChanged(const uint32_t stateType) override 253 { 254 if (clientDeleted_ == false) { 255 client_.OnAccessibleAbilityManagerStateChanged(stateType); 256 } 257 } OnClientDeleted()258 void OnClientDeleted() 259 { 260 clientDeleted_ = true; 261 } 262 private: 263 AccessibilitySystemAbilityClientImpl &client_; 264 std::atomic<bool> clientDeleted_ = false; 265 }; 266 267 class DeathRecipient : public IRemoteObject::DeathRecipient { 268 public: DeathRecipient(AccessibilitySystemAbilityClientImpl & client)269 explicit DeathRecipient(AccessibilitySystemAbilityClientImpl &client) : client_(client) {} 270 ~DeathRecipient() = default; 271 DISALLOW_COPY_AND_MOVE(DeathRecipient); 272 OnRemoteDied(const wptr<IRemoteObject> & remote)273 void OnRemoteDied(const wptr<IRemoteObject> &remote) 274 { 275 client_.ResetService(remote); 276 } 277 private: 278 AccessibilitySystemAbilityClientImpl &client_; 279 }; 280 281 class AccessibilityLoadCallback : public SystemAbilityLoadCallbackStub { 282 public: 283 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, 284 const sptr<IRemoteObject> &remoteObject) override; 285 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; 286 }; 287 288 /** 289 * @brief Connect to AAMS Service. 290 * @return success : true, failed : false. 291 */ 292 bool ConnectToService(); 293 294 void Init(); 295 296 /** 297 * @brief Notify the state is changed. 298 * @param stateType The state type and value. 299 * state type: Refer to AccessibilityStateEventType. 300 * @param value The value be changed. 301 */ 302 void NotifyStateChanged(uint32_t eventType, bool value); 303 304 /** 305 * @brief Check the event type is valid or not. 306 * @param eventType The data of event type. 307 * @return True: The data of event type is valid; otherwise is not. 308 */ 309 bool CheckEventType(EventType eventType); 310 static void OnParameterChanged(const char *key, const char *value, void *context); 311 void ReregisterElementOperator(); 312 313 uint32_t state_{0}; 314 ffrt::mutex mutex_; 315 StateArray stateArray_; 316 StateObserversArray stateObserversArray_; 317 318 std::map<int32_t, sptr<AccessibilityElementOperatorImpl>> elementOperators_; 319 std::map<int32_t, std::map<int32_t, sptr<AccessibilityElementOperatorImpl>>> cardElementOperators_; 320 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 321 sptr<IAccessibleAbilityManagerService> serviceProxy_ = nullptr; 322 sptr<AccessibleAbilityManagerStateObserverImpl> stateObserver_ = nullptr; 323 324 ffrt::condition_variable proxyConVar_; 325 ffrt::mutex conVarMutex_; // mutex for proxyConVar 326 }; 327 } // namespace Accessibility 328 } // namespace OHOS 329 #endif // ACCESSIBILITY_SYSTEM_ABILITY_CLIENT_IMPL_H