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 accessibility 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 IsEnabled(bool &isEnabled) override; 87 88 /** 89 * @brief Checks whether touch exploration 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 IsTouchExplorationEnabled(bool &isEnabled) override; 94 95 /** 96 * @brief Queries the list of accessibility abilities. 97 * @param accessibilityAbilityTypes Indicates the accessibility type specified by AccessibilityAbilityTypes. 98 * @param stateType Indicates the accessibility ability status. 99 * 1 indicates that the ability is enabled; 100 * 2 indicates that the ability is disabled; 101 * 3 indicates that the ability has been installed. 102 * @param infos accessibility ability infos by specified types. 103 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 104 */ 105 virtual RetError GetAbilityList(const uint32_t accessibilityAbilityTypes, const AbilityStateType stateType, 106 std::vector<AccessibilityAbilityInfo> &infos) override; 107 108 /** 109 * @brief Sends an accessibility event. 110 * @param eventType Identifies the accessibility event specified by AccessibilityEventInfo. 111 * @param componentId Indicates the ID of the component to be associated with the event. 112 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 113 */ 114 virtual RetError SendEvent(const EventType eventType, const int64_t componentId) override; 115 116 /** 117 * @brief Sends information about an accessibility event. 118 * @param event Indicates the accessibility event information specified by AccessibilityEventInfo. 119 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 120 */ 121 virtual RetError SendEvent(const AccessibilityEventInfo &event) override; 122 123 /** 124 * @brief Subscribes to the specified type of accessibility status change events. 125 * @param observer Indicates the observer for listening to status events, which is specified 126 * by AccessibilityStateObserver. 127 * @param eventType Indicates the status type, which is specified by AccessibilityStateEventType. 128 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 129 */ 130 virtual RetError SubscribeStateObserver(const std::shared_ptr<AccessibilityStateObserver> &observer, 131 const uint32_t eventType) override; 132 133 /** 134 * @brief Unsubscribe the specified type of accessibility status change events. 135 * @param observer Indicates the registered accessibility status event observer. 136 * @param eventType Indicates the status type, which is specified by AccessibilityStateEventType. 137 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 138 */ 139 virtual RetError UnsubscribeStateObserver(const std::shared_ptr<AccessibilityStateObserver> &observer, 140 const uint32_t eventType) override; 141 142 /** 143 * @brief Get enabled abilities. 144 * @param enabledAbilities The infos of enabled abilities. 145 * @return Return RET_OK if successful, otherwise refer to the RetError for the failure. 146 */ 147 virtual RetError GetEnabledAbilities(std::vector<std::string> &enabledAbilities) override; 148 149 /** 150 * @brief Clean the AAMS object data. 151 * @param remote The object access to AAMS. 152 */ 153 void ResetService(const wptr<IRemoteObject> &remote); 154 155 /** 156 * @brief Receive the state notify from AAMS and send it to the observer registered. 157 * @param stateType The state type and value. 158 * state type: Refer to AccessibilityStateEventType. 159 * value: STATE_ACCESSIBILITY_ENABLED/STATE_EXPLORATION_ENABLED/ 160 * STATE_ACCESSIBILITY_DISABLED/STATE_EXPLORATION_DISABLED 161 */ 162 void OnAccessibleAbilityManagerStateChanged(const uint32_t stateType); 163 164 /** 165 * @brief Set the accessibility state. 166 * @param stateType The state type and value. 167 * state type: Refer to AccessibilityStateEventType. 168 * value: STATE_ACCESSIBILITY_ENABLED/STATE_EXPLORATION_ENABLED/ 169 * STATE_ACCESSIBILITY_DISABLED/STATE_EXPLORATION_DISABLED 170 */ 171 void SetAccessibilityState(const uint32_t stateType); 172 173 /** 174 * @brief Set the element information by accessibility id to AA. 175 * @param infos The element info searched by accessibility id. 176 * @param requestId The request id from AA, it is used to match with request and response. 177 */ 178 virtual void SetSearchElementInfoByAccessibilityIdResult(const std::list<AccessibilityElementInfo> &infos, 179 const int32_t requestId) override; 180 181 /** 182 * @brief Set the element information matched with text to AA. 183 * @param infos The element information searched matched with text. 184 * @param requestId The request id from AA, it is used to match with request and response. 185 */ 186 virtual void SetSearchElementInfoByTextResult(const std::list<AccessibilityElementInfo> &infos, 187 const int32_t requestId) override; 188 189 /** 190 * @brief Set the element information matched with focus type to AA. 191 * @param info The element information searched matched with focus type. 192 * @param requestId The request id from AA, it is used to match with request and response. 193 */ 194 virtual void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info, 195 const int32_t requestId) override; 196 197 /** 198 * @brief Set the element information by focus direction to AA. 199 * @param info The element information searched by focus direction. 200 * @param requestId The request id from AA, it is used to match with request and response. 201 */ 202 virtual void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId) override; 203 204 /** 205 * @brief Set the result of action executed to AA. 206 * @param succeeded True: The action is executed successfully; otherwise is false. 207 * @param requestId The request id from AA, it is used to match with request and response. 208 */ 209 virtual void SetExecuteActionResult(const bool succeeded, const int32_t requestId) override; 210 211 /** 212 * @brief Set the result of cursor position to AA. 213 * @param cursorPosition The cursorPosition to be returned. 214 * @param requestId The request id from AA, it is used to match with request and response. 215 */ 216 virtual void SetCursorPositionResult(const int32_t cursorPosition, const int32_t requestId) override; 217 218 virtual uint32_t GetAccessibilityState() override; 219 virtual void SetFindAccessibilityNodeInfoResult(const AccessibilityElementInfo elementInfo, 220 const int32_t requestId, const int32_t requestCode) override; 221 virtual void SetFindAccessibilityNodeInfosResult(const std::list<AccessibilityElementInfo> elementInfos, 222 const int32_t requestId, const int32_t requestCode) override; 223 virtual void SetPerformActionResult(const bool succeeded, const int32_t requestId) override; 224 virtual RetError GetFocusedWindowId(int32_t &focusedWindowId) override; 225 226 bool LoadAccessibilityService(); 227 void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject); 228 void LoadSystemAbilityFail(); 229 230 private: 231 class AccessibleAbilityManagerStateObserverImpl : public AccessibleAbilityManagerStateObserverStub { 232 public: AccessibleAbilityManagerStateObserverImpl(AccessibilitySystemAbilityClientImpl & client)233 explicit AccessibleAbilityManagerStateObserverImpl(AccessibilitySystemAbilityClientImpl &client) 234 : client_(client) {} 235 ~AccessibleAbilityManagerStateObserverImpl() = default; 236 OnStateChanged(const uint32_t stateType)237 virtual void OnStateChanged(const uint32_t stateType) override 238 { 239 if (clientDeleted_ == false) { 240 client_.OnAccessibleAbilityManagerStateChanged(stateType); 241 } 242 } OnClientDeleted()243 void OnClientDeleted() 244 { 245 clientDeleted_ = true; 246 } 247 private: 248 AccessibilitySystemAbilityClientImpl &client_; 249 std::atomic<bool> clientDeleted_ = false; 250 }; 251 252 class DeathRecipient : public IRemoteObject::DeathRecipient { 253 public: DeathRecipient(AccessibilitySystemAbilityClientImpl & client)254 explicit DeathRecipient(AccessibilitySystemAbilityClientImpl &client) : client_(client) {} 255 ~DeathRecipient() = default; 256 DISALLOW_COPY_AND_MOVE(DeathRecipient); 257 OnRemoteDied(const wptr<IRemoteObject> & remote)258 void OnRemoteDied(const wptr<IRemoteObject> &remote) 259 { 260 client_.ResetService(remote); 261 } 262 private: 263 AccessibilitySystemAbilityClientImpl &client_; 264 }; 265 266 class AccessibilityLoadCallback : public SystemAbilityLoadCallbackStub { 267 public: 268 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, 269 const sptr<IRemoteObject> &remoteObject) override; 270 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; 271 }; 272 273 /** 274 * @brief Connect to AAMS Service. 275 * @return success : true, failed : false. 276 */ 277 bool ConnectToService(); 278 279 void Init(); 280 281 /** 282 * @brief Notify the state is changed. 283 * @param stateType The state type and value. 284 * state type: Refer to AccessibilityStateEventType. 285 * @param value The value be changed. 286 */ 287 void NotifyStateChanged(uint32_t eventType, bool value); 288 289 /** 290 * @brief Check the event type is valid or not. 291 * @param eventType The data of event type. 292 * @return True: The data of event type is valid; otherwise is not. 293 */ 294 bool CheckEventType(EventType eventType); 295 static void OnParameterChanged(const char *key, const char *value, void *context); 296 void ReregisterElementOperator(); 297 298 uint32_t state_{0}; 299 ffrt::mutex mutex_; 300 StateArray stateArray_; 301 StateObserversArray stateObserversArray_; 302 303 std::map<int32_t, sptr<AccessibilityElementOperatorImpl>> elementOperators_; 304 std::map<int32_t, std::map<int32_t, sptr<AccessibilityElementOperatorImpl>>> cardElementOperators_; 305 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 306 sptr<IAccessibleAbilityManagerService> serviceProxy_ = nullptr; 307 sptr<AccessibleAbilityManagerStateObserverImpl> stateObserver_ = nullptr; 308 309 ffrt::condition_variable proxyConVar_; 310 ffrt::mutex conVarMutex_; // mutex for proxyConVar 311 }; 312 } // namespace Accessibility 313 } // namespace OHOS 314 #endif // ACCESSIBILITY_SYSTEM_ABILITY_CLIENT_IMPL_H