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