• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2025 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 #include "napi_accessibility_system_ability_client.h"
17 
18 #include <uv.h>
19 #include "accessibility_state_event.h"
20 #include "hilog_wrapper.h"
21 #include "accessibility_utils.h"
22 #include "api_reporter_helper.h"
23 
24 using namespace OHOS;
25 using namespace OHOS::Accessibility;
26 using namespace OHOS::AccessibilityNapi;
27 
28 namespace OHOS {
29 namespace Accessibility {
TmpOpenScope(napi_env env)30 napi_handle_scope TmpOpenScope(napi_env env)
31 {
32     napi_handle_scope scope = nullptr;
33     NAPI_CALL(env, napi_open_handle_scope(env, &scope));
34     return scope;
35 }
36 } // namespace Accessibility
37 } // namespace OHOS
38 
39 std::shared_ptr<StateListenerImpl> NAccessibilityClient::accessibilityStateListeners_ =
40     std::make_shared<StateListenerImpl>(AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED);
41 std::shared_ptr<StateListenerImpl> NAccessibilityClient::touchGuideStateListeners_ =
42     std::make_shared<StateListenerImpl>(AccessibilityStateEventType::EVENT_TOUCH_GUIDE_STATE_CHANGED);
43 std::shared_ptr<StateListenerImpl> NAccessibilityClient::screenReaderStateListeners_ =
44     std::make_shared<StateListenerImpl>(AccessibilityStateEventType::EVENT_SCREEN_READER_STATE_CHANGED);
45 std::shared_ptr<NAccessibilityConfigObserverImpl> NAccessibilityClient::captionListeners_ =
46     std::make_shared<NAccessibilityConfigObserverImpl>();
47 constexpr int32_t REPORTER_THRESHOLD_VALUE = 3000;
48 
49 napi_ref NAccessibilityClient::aaConsRef_;
50 napi_ref NAccessibilityClient::aaStyleConsRef_;
51 
52 #define ACCESSIBILITY_NAPI_ASSERT(env, cond, errCode) \
53 do { \
54     if (!(cond)) { \
55         napi_value err = CreateBusinessError(env, errCode); \
56         napi_throw(env, err); \
57         napi_value res = nullptr; \
58         napi_get_undefined(env, &res); \
59         return res; \
60     } \
61 } while (0)
62 
IsScreenReaderOpenSync(napi_env env,napi_callback_info info)63 napi_value NAccessibilityClient::IsScreenReaderOpenSync(napi_env env, napi_callback_info info)
64 {
65     HILOG_INFO();
66     size_t argc = ARGS_SIZE_ONE;
67     napi_value argv = nullptr;
68     napi_get_cb_info(env, info, &argc, &argv, nullptr, nullptr);
69     ACCESSIBILITY_NAPI_ASSERT(env, argc == ARGS_SIZE_ZERO, OHOS::Accessibility::RET_ERR_INVALID_PARAM);
70 
71     auto asaClient = AccessibilitySystemAbilityClient::GetInstance();
72     ACCESSIBILITY_NAPI_ASSERT(env, asaClient != nullptr, OHOS::Accessibility::RET_ERR_NULLPTR);
73     bool status = false;
74     auto ret = asaClient->IsScreenReaderEnabled(status);
75     ACCESSIBILITY_NAPI_ASSERT(env, ret == RET_OK, OHOS::Accessibility::RET_ERR_FAILED);
76     napi_value result = nullptr;
77     napi_get_boolean(env, status, &result);
78     return result;
79 }
80 
IsOpenAccessibilitySync(napi_env env,napi_callback_info info)81 napi_value NAccessibilityClient::IsOpenAccessibilitySync(napi_env env, napi_callback_info info)
82 {
83 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
84     ApiReportHelper reporter("NAccessibilityClient.IsOpenAccessibilitySync");
85 #endif // ACCESSIBILITY_EMULATOR_DEFINED
86     HILOG_INFO();
87     size_t argc = ARGS_SIZE_ONE;
88     napi_value argv = nullptr;
89     napi_get_cb_info(env, info, &argc, &argv, nullptr, nullptr);
90     ACCESSIBILITY_NAPI_ASSERT(env, argc == ARGS_SIZE_ZERO, OHOS::Accessibility::RET_ERR_INVALID_PARAM);
91 
92     auto asaClient = AccessibilitySystemAbilityClient::GetInstance();
93     ACCESSIBILITY_NAPI_ASSERT(env, asaClient != nullptr, OHOS::Accessibility::RET_ERR_NULLPTR);
94     bool status = false;
95     auto ret = asaClient->IsEnabled(status);
96     ACCESSIBILITY_NAPI_ASSERT(env, ret == RET_OK, OHOS::Accessibility::RET_ERR_FAILED);
97     napi_value result = nullptr;
98     napi_get_boolean(env, status, &result);
99     return result;
100 }
101 
IsOpenAccessibility(napi_env env,napi_callback_info info)102 napi_value NAccessibilityClient::IsOpenAccessibility(napi_env env, napi_callback_info info)
103 {
104 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
105     ApiReportHelper reporter("NAccessibilityClient.IsOpenAccessibility");
106 #endif // ACCESSIBILITY_EMULATOR_DEFINED
107     HILOG_INFO();
108     size_t argc = ARGS_SIZE_ONE;
109     napi_value argv = nullptr;
110     NAccessibilitySystemAbilityClient* callbackInfo = new(std::nothrow) NAccessibilitySystemAbilityClient();
111     if (callbackInfo == nullptr) {
112         HILOG_ERROR("Failed to create callbackInfo.");
113         return nullptr;
114     }
115     napi_get_cb_info(env, info, &argc, &argv, nullptr, nullptr);
116 
117     napi_value promise = nullptr;
118     if (argc > 0 && CheckJsFunction(env, argv)) {
119         HILOG_DEBUG("IsOpenAccessibility callback mode");
120         napi_create_reference(env, argv, 1, &callbackInfo->callback_);
121         napi_get_undefined(env, &promise);
122     } else {
123         HILOG_DEBUG("IsOpenAccessibility promise mode");
124         napi_create_promise(env, &callbackInfo->deferred_, &promise);
125     }
126     napi_value resource = nullptr;
127     napi_create_string_utf8(env, "IsOpenAccessibility", NAPI_AUTO_LENGTH, &resource);
128 
129     auto ret = napi_create_async_work(env, nullptr, resource,
130         // Execute async to call c++ function
131         [](napi_env env, void* data) {
132             NAccessibilitySystemAbilityClient* callbackInfo = static_cast<NAccessibilitySystemAbilityClient*>(data);
133             auto asaClient = AccessibilitySystemAbilityClient::GetInstance();
134             if (asaClient) {
135                 callbackInfo->ret_ = asaClient->IsEnabled(callbackInfo->enabled_);
136                 HILOG_INFO("IsOpenAccessibility Executing enabled[%{public}d]", callbackInfo->enabled_);
137             }
138         },
139         // Execute the complete function
140         [](napi_env env, napi_status status, void* data) {
141             Completefunction(env, "IsOpenAccessibility", data);
142         },
143         reinterpret_cast<void*>(callbackInfo), &callbackInfo->work_);
144     if (ret != napi_ok) {
145         delete callbackInfo;
146         callbackInfo = nullptr;
147         HILOG_ERROR("failed to create async work.");
148         return nullptr;
149     }
150     napi_queue_async_work_with_qos(env, callbackInfo->work_, napi_qos_user_initiated);
151     return promise;
152 }
153 
IsOpenTouchExplorationSync(napi_env env,napi_callback_info info)154 napi_value NAccessibilityClient::IsOpenTouchExplorationSync(napi_env env, napi_callback_info info)
155 {
156 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
157     ApiReportHelper reporter("NAccessibilityClient.IsOpenTouchExplorationSync");
158 #endif // ACCESSIBILITY_EMULATOR_DEFINED
159     HILOG_INFO();
160     size_t argc = ARGS_SIZE_ONE;
161     napi_value argv = nullptr;
162     napi_get_cb_info(env, info, &argc, &argv, nullptr, nullptr);
163     ACCESSIBILITY_NAPI_ASSERT(env, argc == ARGS_SIZE_ZERO, OHOS::Accessibility::RET_ERR_INVALID_PARAM);
164 
165     auto asaClient = AccessibilitySystemAbilityClient::GetInstance();
166     ACCESSIBILITY_NAPI_ASSERT(env, asaClient != nullptr, OHOS::Accessibility::RET_ERR_NULLPTR);
167     bool status = false;
168     auto ret = asaClient->IsTouchExplorationEnabled(status);
169     ACCESSIBILITY_NAPI_ASSERT(env, ret == RET_OK, OHOS::Accessibility::RET_ERR_FAILED);
170     napi_value result = nullptr;
171     napi_get_boolean(env, status, &result);
172     return result;
173 }
174 
IsOpenTouchExploration(napi_env env,napi_callback_info info)175 napi_value NAccessibilityClient::IsOpenTouchExploration(napi_env env, napi_callback_info info)
176 {
177 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
178     ApiReportHelper reporter("NAccessibilityClient.IsOpenTouchExploration");
179 #endif // ACCESSIBILITY_EMULATOR_DEFINED
180     HILOG_INFO();
181     NAccessibilitySystemAbilityClient* callbackInfo = new(std::nothrow) NAccessibilitySystemAbilityClient();
182     if (callbackInfo == nullptr) {
183         HILOG_ERROR("Failed to create callbackInfo.");
184         return nullptr;
185     }
186     size_t argc = ARGS_SIZE_ONE;
187     napi_value argv = nullptr;
188     napi_get_cb_info(env, info, &argc, &argv, nullptr, nullptr);
189 
190     napi_value promise = nullptr;
191     if (argc > 0 && CheckJsFunction(env, argv)) {
192         HILOG_DEBUG("IsOpenTouchExploration callback mode");
193         napi_create_reference(env, argv, 1, &callbackInfo->callback_);
194         napi_get_undefined(env, &promise);
195     } else {
196         HILOG_DEBUG("IsOpenTouchExploration promise mode");
197         napi_create_promise(env, &callbackInfo->deferred_, &promise);
198     }
199     napi_value resource = nullptr;
200     napi_create_string_utf8(env, "IsOpenTouchExploration", NAPI_AUTO_LENGTH, &resource);
201 
202     auto ret = napi_create_async_work(env, nullptr, resource,
203         // Execute async to call c++ function
204         [](napi_env env, void* data) {
205             NAccessibilitySystemAbilityClient* callbackInfo = static_cast<NAccessibilitySystemAbilityClient*>(data);
206             auto asaClient = AccessibilitySystemAbilityClient::GetInstance();
207             if (asaClient) {
208                 callbackInfo->ret_ = asaClient->IsTouchExplorationEnabled(callbackInfo->touchEnabled_);
209                 HILOG_INFO("IsOpenTouchExploration Executing touchEnabled[%{public}d]", callbackInfo->touchEnabled_);
210             }
211         },
212         // Execute the complete function
213         [](napi_env env, napi_status status, void* data) {
214             Completefunction(env, "IsOpenTouchExploration", data);
215         },
216         reinterpret_cast<void*>(callbackInfo), &callbackInfo->work_);
217     if (ret != napi_ok) {
218         delete callbackInfo;
219         callbackInfo = nullptr;
220         HILOG_ERROR("failed to create async work.");
221         return nullptr;
222     }
223     napi_queue_async_work_with_qos(env, callbackInfo->work_, napi_qos_user_initiated);
224     return promise;
225 }
226 
Completefunction(napi_env env,std::string type,void * data)227 void NAccessibilityClient::Completefunction(napi_env env, std::string type, void* data)
228 {
229     NAccessibilitySystemAbilityClient* callbackInfo = static_cast<NAccessibilitySystemAbilityClient*>(data);
230     napi_value result[ARGS_SIZE_TWO] = {0};
231     napi_value callback = 0;
232     napi_value undefined = 0;
233     napi_get_undefined(env, &undefined);
234 
235     if (type == "IsOpenAccessibility") {
236         NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, callbackInfo->enabled_, &result[PARAM1]));
237         HILOG_INFO("IsOpenAccessibility completed enabled[%{public}d]", callbackInfo->enabled_);
238     } else if (type == "IsOpenTouchExploration") {
239         NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, callbackInfo->touchEnabled_, &result[PARAM1]));
240         HILOG_INFO("IsOpenTouchExploration completed touchEnabled_[%{public}d]", callbackInfo->touchEnabled_);
241     } else {
242         napi_delete_async_work(env, callbackInfo->work_);
243         delete callbackInfo;
244         callbackInfo = nullptr;
245         return;
246     }
247     result[PARAM0] = CreateBusinessError(env, callbackInfo->ret_);
248     if (callbackInfo->callback_) {
249         napi_get_reference_value(env, callbackInfo->callback_, &callback);
250         napi_value returnVal = nullptr;
251         napi_call_function(env, undefined, callback, ARGS_SIZE_TWO, result, &returnVal);
252         napi_delete_reference(env, callbackInfo->callback_);
253     } else {
254         if (callbackInfo->ret_ == OHOS::Accessibility::RET_OK) {
255             HILOG_DEBUG("Completefunction callbackInfo->ret_ is RET_OK");
256             napi_resolve_deferred(env, callbackInfo->deferred_, result[PARAM1]);
257         } else {
258             HILOG_DEBUG("Completefunction callbackInfo->ret_ is not RET_OK");
259             napi_reject_deferred(env, callbackInfo->deferred_, result[PARAM0]);
260         }
261     }
262     napi_delete_async_work(env, callbackInfo->work_);
263     delete callbackInfo;
264     callbackInfo = nullptr;
265 }
266 
GetAbilityListExecute(napi_env env,void * data)267 void NAccessibilityClient::GetAbilityListExecute(napi_env env, void* data)
268 {
269     NAccessibilitySystemAbilityClient* callbackInfo = static_cast<NAccessibilitySystemAbilityClient*>(data);
270     auto asaClient = AccessibilitySystemAbilityClient::GetInstance();
271     if (asaClient) {
272         callbackInfo->ret_ = asaClient->GetAbilityList(callbackInfo->abilityTypes_,
273             callbackInfo->stateTypes_, callbackInfo->abilityList_);
274     }
275 }
276 
GetAbilityListComplete(napi_env env,napi_status status,void * data)277 void NAccessibilityClient::GetAbilityListComplete(napi_env env, napi_status status, void* data)
278 {
279     NAccessibilitySystemAbilityClient* callbackInfo = static_cast<NAccessibilitySystemAbilityClient*>(data);
280     napi_value result[ARGS_SIZE_TWO] = {0};
281     napi_value callback = 0;
282     napi_value undefined = 0;
283     napi_get_undefined(env, &undefined);
284     napi_create_array(env, &result[PARAM1]);
285     ConvertAccessibleAbilityInfosToJS(env, result[PARAM1], callbackInfo->abilityList_);
286     result[PARAM0] = CreateBusinessError(env, callbackInfo->ret_);
287     if (callbackInfo->callback_) {
288         napi_get_reference_value(env, callbackInfo->callback_, &callback);
289         napi_value returnVal = nullptr;
290         napi_call_function(env, undefined, callback, ARGS_SIZE_TWO, result, &returnVal);
291         napi_delete_reference(env, callbackInfo->callback_);
292     } else {
293         if (callbackInfo->ret_ == OHOS::Accessibility::RET_OK) {
294             HILOG_DEBUG("GetAbilityListComplete callbackInfo->ret_ is RET_OK");
295             napi_resolve_deferred(env, callbackInfo->deferred_, result[PARAM1]);
296         } else {
297             HILOG_DEBUG("GetAbilityListComplete callbackInfo->ret_ is not RET_OK");
298             napi_reject_deferred(env, callbackInfo->deferred_, result[PARAM0]);
299         }
300     }
301     napi_delete_async_work(env, callbackInfo->work_);
302     delete callbackInfo;
303     callbackInfo = nullptr;
304 }
305 
GetAbilityList(napi_env env,napi_callback_info info)306 napi_value NAccessibilityClient::GetAbilityList(napi_env env, napi_callback_info info)
307 {
308 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
309     ApiReportHelper reporter("NAccessibilityClient.GetAbilityList");
310 #endif // ACCESSIBILITY_EMULATOR_DEFINED
311     NAccessibilitySystemAbilityClient* callbackInfo = new(std::nothrow) NAccessibilitySystemAbilityClient();
312     if (callbackInfo == nullptr) {
313         HILOG_ERROR("Failed to create callbackInfo.");
314         return nullptr;
315     }
316 
317     size_t argc = 3;
318     napi_value parameters[3] = {0};
319     napi_get_cb_info(env, info, &argc, parameters, nullptr, nullptr);
320 
321     std::string abilityTypes = GetStringFromNAPI(env, parameters[0]);
322     std::string stateTypes = GetStringFromNAPI(env, parameters[1]);
323     HILOG_INFO("abilityTypes[%{public}s] stateTypes[%{public}s]", abilityTypes.c_str(), stateTypes.c_str());
324 
325     callbackInfo->abilityTypes_ = ConvertStringToAccessibilityAbilityTypes(abilityTypes);
326     callbackInfo->stateTypes_ = ConvertStringToAbilityStateType(stateTypes);
327 
328     napi_value promise = nullptr;
329 
330     if (argc > ARGS_SIZE_TWO && CheckJsFunction(env, parameters[ARGS_SIZE_TWO])) {
331         napi_create_reference(env, parameters[ARGS_SIZE_TWO], 1, &callbackInfo->callback_);
332         napi_get_undefined(env, &promise);
333     } else {
334         napi_create_promise(env, &callbackInfo->deferred_, &promise);
335     }
336     napi_value resource = nullptr;
337     napi_create_string_utf8(env, "GetAbilityList", NAPI_AUTO_LENGTH, &resource);
338 
339     auto ret = napi_create_async_work(env, nullptr, resource,
340         // Execute async to call c++ function
341         NAccessibilityClient::GetAbilityListExecute,
342         // Execute the complete function
343         NAccessibilityClient::GetAbilityListComplete,
344         reinterpret_cast<void*>(callbackInfo),
345         &callbackInfo->work_);
346     if (ret != napi_ok) {
347         delete callbackInfo;
348         callbackInfo = nullptr;
349         HILOG_ERROR("failed to create async work.");
350         return nullptr;
351     }
352     napi_queue_async_work_with_qos(env, callbackInfo->work_, napi_qos_user_initiated);
353     return promise;
354 }
355 
GetAccessibilityExtensionList(napi_env env,napi_callback_info info)356 napi_value NAccessibilityClient::GetAccessibilityExtensionList(napi_env env, napi_callback_info info)
357 {
358 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
359     ApiReportHelper reporter("NAccessibilityClient.GetAccessibilityExtensionList");
360 #endif // ACCESSIBILITY_EMULATOR_DEFINED
361     NAccessibilitySystemAbilityClient* callbackInfo = new(std::nothrow) NAccessibilitySystemAbilityClient();
362     if (callbackInfo == nullptr) {
363         HILOG_ERROR("Failed to create callbackInfo.");
364         napi_value err = CreateBusinessError(env, OHOS::Accessibility::RET_ERR_NULLPTR);
365         napi_throw(env, err);
366         return nullptr;
367     }
368 
369     size_t argc = ARGS_SIZE_THREE;
370     napi_value parameters[ARGS_SIZE_THREE] = {0};
371     napi_get_cb_info(env, info, &argc, parameters, nullptr, nullptr);
372 
373     OHOS::Accessibility::RetError errCode = OHOS::Accessibility::RET_OK;
374     do {
375         if (argc < ARGS_SIZE_THREE - 1) {
376             HILOG_ERROR("argc is invalid: %{public}zu", argc);
377             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
378             break;
379         }
380 
381         // parse ability type
382         std::string abilityTypes = "";
383         std::string stateTypes = "";
384         if (!ParseString(env, abilityTypes, parameters[PARAM0]) ||
385             !ParseString(env, stateTypes, parameters[PARAM1])) {
386             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
387             break;
388         }
389 
390         HILOG_INFO("abilityTypes = %{private}s", abilityTypes.c_str());
391         if (CheckAbilityType(abilityTypes)) {
392             callbackInfo->abilityTypes_ = ConvertStringToAccessibilityAbilityTypes(abilityTypes);
393         } else {
394             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
395             break;
396         }
397         // parse ability state
398         HILOG_INFO("stateTypes = %{private}s", stateTypes.c_str());
399         if (CheckStateType(stateTypes)) {
400             callbackInfo->stateTypes_ = ConvertStringToAbilityStateType(stateTypes);
401         } else {
402             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
403         }
404     } while (0);
405 
406     if (errCode == OHOS::Accessibility::RET_ERR_INVALID_PARAM) {
407         delete callbackInfo;
408         callbackInfo = nullptr;
409         napi_value err = CreateBusinessError(env, errCode);
410         HILOG_ERROR("invalid param");
411         napi_throw(env, err);
412         return nullptr;
413     }
414 
415     return GetAccessibilityExtensionListAsync(env, argc, parameters, callbackInfo);
416 }
417 
GetAccessibilityExtensionListAsync(napi_env env,size_t argc,napi_value * parameters,NAccessibilitySystemAbilityClient * callbackInfo)418 napi_value NAccessibilityClient::GetAccessibilityExtensionListAsync(
419     napi_env env, size_t argc, napi_value* parameters, NAccessibilitySystemAbilityClient* callbackInfo)
420 {
421     napi_value promise = nullptr;
422     if (argc > ARGS_SIZE_THREE - 1 && CheckJsFunction(env, parameters[PARAM2])) {
423         napi_create_reference(env, parameters[PARAM2], 1, &callbackInfo->callback_);
424         napi_get_undefined(env, &promise);
425     } else {
426         napi_create_promise(env, &callbackInfo->deferred_, &promise);
427     }
428     napi_value resource = nullptr;
429     napi_create_string_utf8(env, "GetAccessibilityExtensionList", NAPI_AUTO_LENGTH, &resource);
430 
431     auto ret = napi_create_async_work(env, nullptr, resource,
432         // Execute async to call c++ function
433         NAccessibilityClient::GetAbilityListExecute,
434         // Execute the complete function
435         NAccessibilityClient::GetAbilityListComplete,
436         reinterpret_cast<void*>(callbackInfo),
437         &callbackInfo->work_);
438     if (ret != napi_ok) {
439         delete callbackInfo;
440         callbackInfo = nullptr;
441         HILOG_ERROR("failed to create async work.");
442         return nullptr;
443     }
444     napi_queue_async_work_with_qos(env, callbackInfo->work_, napi_qos_user_initiated);
445     return promise;
446 }
447 
GetAccessibilityExtensionListSync(napi_env env,napi_callback_info info)448 napi_value NAccessibilityClient::GetAccessibilityExtensionListSync(napi_env env, napi_callback_info info)
449 {
450 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
451     ApiReportHelper reporter("NAccessibilityClient.GetAccessibilityExtensionListSync");
452 #endif // ACCESSIBILITY_EMULATOR_DEFINED
453     size_t argc = ARGS_SIZE_THREE;
454     napi_value parameters[ARGS_SIZE_THREE] = {0};
455     napi_get_cb_info(env, info, &argc, parameters, nullptr, nullptr);
456 
457     OHOS::Accessibility::RetError errCode = OHOS::Accessibility::RET_OK;
458     OHOS::Accessibility::AbilityStateType stateTypes = OHOS::Accessibility::ABILITY_STATE_INVALID;
459     uint32_t abilityTypes = 0;
460 
461     do {
462         if (argc < ARGS_SIZE_THREE - 1) {
463             HILOG_ERROR("argc is invalid: %{public}zu", argc);
464             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
465             break;
466         }
467 
468         // parse ability type
469         std::string abilityTypeStr = "";
470         std::string stateTypeStr = "";
471         if (!ParseString(env, abilityTypeStr, parameters[PARAM0]) ||
472             !ParseString(env, stateTypeStr, parameters[PARAM1])) {
473             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
474             break;
475         }
476 
477         HILOG_DEBUG("abilityTypeStr = %{private}s", abilityTypeStr.c_str());
478         if (CheckAbilityType(abilityTypeStr)) {
479             abilityTypes = ConvertStringToAccessibilityAbilityTypes(abilityTypeStr);
480         } else {
481             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
482             break;
483         }
484 
485         // parse ability state
486         HILOG_DEBUG("stateTypeStr = %{private}s", stateTypeStr.c_str());
487         if (CheckStateType(stateTypeStr)) {
488             stateTypes = ConvertStringToAbilityStateType(stateTypeStr);
489         } else {
490             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
491         }
492     } while (0);
493 
494     std::vector<OHOS::Accessibility::AccessibilityAbilityInfo> abilityList {};
495     if (errCode == OHOS::Accessibility::RET_OK) {
496         auto asaClient = AccessibilitySystemAbilityClient::GetInstance();
497         if (asaClient) {
498             errCode = asaClient->GetAbilityList(abilityTypes, stateTypes, abilityList);
499         }
500     }
501 
502     ACCESSIBILITY_NAPI_ASSERT(env, errCode == OHOS::Accessibility::RET_OK,
503         OHOS::Accessibility::RET_ERR_INVALID_PARAM);
504 
505     napi_value resultAbilityList = nullptr;
506     napi_create_array(env, &resultAbilityList);
507     ConvertAccessibleAbilityInfosToJS(env, resultAbilityList, abilityList);
508     return resultAbilityList;
509 }
510 
CheckAbilityType(const std::string & abilityType)511 bool NAccessibilityClient::CheckAbilityType(const std::string& abilityType)
512 {
513     if (std::strcmp(abilityType.c_str(), "audible") == 0 ||
514         std::strcmp(abilityType.c_str(), "generic") == 0 ||
515         std::strcmp(abilityType.c_str(), "haptic") == 0 ||
516         std::strcmp(abilityType.c_str(), "spoken") == 0 ||
517         std::strcmp(abilityType.c_str(), "visual") == 0 ||
518         std::strcmp(abilityType.c_str(), "all") == 0) {
519         return true;
520     } else {
521         return false;
522     }
523 }
524 
CheckStateType(const std::string & stateType)525 bool NAccessibilityClient::CheckStateType(const std::string& stateType)
526 {
527     if (std::strcmp(stateType.c_str(), "enable") == 0 ||
528         std::strcmp(stateType.c_str(), "disable") == 0 ||
529         std::strcmp(stateType.c_str(), "install") == 0) {
530         return true;
531     } else {
532         return false;
533     }
534 }
535 
SendEventExecute(napi_env env,void * data)536 void NAccessibilityClient::SendEventExecute(napi_env env, void* data)
537 {
538     NAccessibilitySystemAbilityClient* callbackInfo = static_cast<NAccessibilitySystemAbilityClient*>(data);
539     auto asaClient = AccessibilitySystemAbilityClient::GetInstance();
540     if (callbackInfo->result_ && asaClient) {
541         callbackInfo->ret_ = asaClient->SendEvent(callbackInfo->eventInfo_);
542     }
543     HILOG_INFO("SendEvent result[%{public}d]", callbackInfo->ret_);
544 }
545 
SendEventComplete(napi_env env,napi_status status,void * data)546 void NAccessibilityClient::SendEventComplete(napi_env env, napi_status status, void* data)
547 {
548     NAccessibilitySystemAbilityClient* callbackInfo = static_cast<NAccessibilitySystemAbilityClient*>(data);
549     napi_value result[ARGS_SIZE_TWO] = {0};
550     napi_value callback = 0;
551     napi_value undefined = 0;
552     napi_value ret = 0;
553     napi_get_undefined(env, &undefined);
554     napi_get_undefined(env, &ret);
555     result[PARAM0] = CreateBusinessError(env, callbackInfo->ret_);
556     result[PARAM1] = ret;
557     if (callbackInfo->callback_) {
558         napi_get_reference_value(env, callbackInfo->callback_, &callback);
559         napi_value returnVal = nullptr;
560         napi_call_function(env, undefined, callback, ARGS_SIZE_TWO, result, &returnVal);
561         napi_delete_reference(env, callbackInfo->callback_);
562     } else {
563         if (callbackInfo->ret_ == OHOS::Accessibility::RET_OK) {
564             HILOG_DEBUG("SendEventComplete callbackInfo->ret_ is RET_OK");
565             napi_resolve_deferred(env, callbackInfo->deferred_, result[PARAM1]);
566         } else {
567             HILOG_DEBUG("SendEventComplete callbackInfo->ret_ is not RET_OK");
568             napi_reject_deferred(env, callbackInfo->deferred_, result[PARAM0]);
569         }
570     }
571     napi_delete_async_work(env, callbackInfo->work_);
572     delete callbackInfo;
573     callbackInfo = nullptr;
574 }
575 
SendEvent(napi_env env,napi_callback_info info)576 napi_value NAccessibilityClient::SendEvent(napi_env env, napi_callback_info info)
577 {
578 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
579     ApiReportHelper reporter("NAccessibilityClient.SendEvent", REPORTER_THRESHOLD_VALUE);
580 #endif // ACCESSIBILITY_EMULATOR_DEFINED
581     HILOG_INFO();
582     NAccessibilitySystemAbilityClient* callbackInfo = new(std::nothrow) NAccessibilitySystemAbilityClient();
583     if (callbackInfo == nullptr) {
584         HILOG_ERROR("Failed to create callbackInfo.");
585         return nullptr;
586     }
587 
588     size_t argc = ARGS_SIZE_TWO;
589     napi_value parameters[ARGS_SIZE_TWO] = {0};
590     napi_get_cb_info(env, info, &argc, parameters, nullptr, nullptr);
591     callbackInfo->result_ = ConvertEventInfoJSToNAPI(env, parameters[0], callbackInfo->eventInfo_);
592 
593     napi_value promise = nullptr;
594 
595     if (argc > ARGS_SIZE_ONE && CheckJsFunction(env, parameters[1])) {
596         HILOG_DEBUG("SendEvent callback mode");
597         napi_create_reference(env, parameters[1], 1, &callbackInfo->callback_);
598     } else {
599         HILOG_DEBUG("SendEvent promise mode");
600         napi_create_promise(env, &callbackInfo->deferred_, &promise);
601     }
602     napi_value resource = nullptr;
603     napi_create_string_utf8(env, "SendEvent", NAPI_AUTO_LENGTH, &resource);
604 
605     auto ret = napi_create_async_work(env, nullptr, resource,
606         NAccessibilityClient::SendEventExecute,
607         NAccessibilityClient::SendEventComplete,
608         reinterpret_cast<void*>(callbackInfo),
609         &callbackInfo->work_);
610     if (ret != napi_ok) {
611         delete callbackInfo;
612         callbackInfo = nullptr;
613         HILOG_ERROR("failed to create async work.");
614         return nullptr;
615     }
616     napi_queue_async_work_with_qos(env, callbackInfo->work_, napi_qos_user_initiated);
617 
618     return promise;
619 }
620 
SendAccessibilityEvent(napi_env env,napi_callback_info info)621 napi_value NAccessibilityClient::SendAccessibilityEvent(napi_env env, napi_callback_info info)
622 {
623 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
624     ApiReportHelper reporter("NAccessibilityClient.SendAccessibilityEvent", REPORTER_THRESHOLD_VALUE);
625 #endif // ACCESSIBILITY_EMULATOR_DEFINED
626     HILOG_INFO();
627     NAccessibilitySystemAbilityClient* callbackInfo = new(std::nothrow) NAccessibilitySystemAbilityClient();
628     if (callbackInfo == nullptr) {
629         HILOG_ERROR("Failed to create callbackInfo.");
630         napi_value err = CreateBusinessError(env, OHOS::Accessibility::RET_ERR_NULLPTR);
631         napi_throw(env, err);
632         return nullptr;
633     }
634 
635     size_t argc = ARGS_SIZE_TWO;
636     napi_value parameters[ARGS_SIZE_TWO] = {0};
637     napi_get_cb_info(env, info, &argc, parameters, nullptr, nullptr);
638 
639     OHOS::Accessibility::RetError errCode = OHOS::Accessibility::RET_OK;
640     if (argc < ARGS_SIZE_TWO - 1) {
641         HILOG_ERROR("argc is invalid: %{public}zu", argc);
642         errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
643     }
644 
645     if (errCode == OHOS::Accessibility::RET_OK) {
646         callbackInfo->result_ = ConvertEventInfoJSToNAPI(env, parameters[0], callbackInfo->eventInfo_);
647         if (!callbackInfo->result_) {
648             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
649         }
650     }
651 
652     if (errCode == OHOS::Accessibility::RET_ERR_INVALID_PARAM) {
653         delete callbackInfo;
654         callbackInfo = nullptr;
655         napi_value err = CreateBusinessError(env, errCode);
656         HILOG_ERROR("SendAccessibilityEvent invalid param");
657         napi_throw(env, err);
658         return nullptr;
659     }
660 
661     napi_value promise = nullptr;
662     if (argc > ARGS_SIZE_TWO - 1 && CheckJsFunction(env, parameters[PARAM1])) {
663         napi_create_reference(env, parameters[PARAM1], 1, &callbackInfo->callback_);
664         napi_get_undefined(env, &promise);
665     } else {
666         HILOG_DEBUG("SendEvent promise mode");
667         napi_create_promise(env, &callbackInfo->deferred_, &promise);
668     }
669     napi_value resource = nullptr;
670     napi_create_string_utf8(env, "SendAccessibilityEvent", NAPI_AUTO_LENGTH, &resource);
671 
672     napi_create_async_work(env, nullptr, resource,
673         NAccessibilityClient::SendEventExecute,
674         NAccessibilityClient::SendEventComplete,
675         reinterpret_cast<void*>(callbackInfo),
676         &callbackInfo->work_);
677     napi_queue_async_work_with_qos(env, callbackInfo->work_, napi_qos_user_initiated);
678 
679     return promise;
680 }
681 
SubscribeState(napi_env env,napi_callback_info info)682 napi_value NAccessibilityClient::SubscribeState(napi_env env, napi_callback_info info)
683 {
684     HILOG_INFO();
685 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
686     ApiReportHelper reporter("NAccessibilityClient.SubscribeState");
687 #endif // ACCESSIBILITY_EMULATOR_DEFINED
688     size_t argc = ARGS_SIZE_TWO;
689     napi_value args[ARGS_SIZE_TWO] = {0};
690     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
691     if (status != napi_ok) {
692         HILOG_ERROR("SubscribeState Failed to get event type");
693         napi_value err = CreateBusinessError(env, OHOS::Accessibility::RET_ERR_FAILED);
694         napi_throw(env, err);
695         return nullptr;
696     }
697 
698     OHOS::Accessibility::RetError errCode = OHOS::Accessibility::RET_OK;
699     if (argc < ARGS_SIZE_TWO) {
700         HILOG_ERROR("SubscribeState argc is invalid: %{public}zu", argc);
701         errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
702     }
703     uint32_t type = AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED;
704     GetAccessibilityStateEventType(env, args, errCode, type);
705     if (errCode == OHOS::Accessibility::RET_OK) {
706         napi_valuetype valueType = napi_null;
707         napi_typeof(env, args[PARAM1], &valueType);
708         if (valueType != napi_function) {
709             HILOG_ERROR("args[PARAM1] format is wrong");
710             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
711         }
712     }
713 
714     if (errCode == OHOS::Accessibility::RET_ERR_INVALID_PARAM) {
715         napi_value err = CreateBusinessError(env, errCode);
716         HILOG_ERROR("SubscribeState invalid param");
717         napi_throw(env, err);
718         return nullptr;
719     }
720 
721     switch (type) {
722         case AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED:
723 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
724             reporter.setApiName("NAccessibilityClient.SubscribeState.accessibilityStateChange");
725 #endif // ACCESSIBILITY_EMULATOR_DEFINED
726             accessibilityStateListeners_->SubscribeObserver(env, args[PARAM1]);
727             break;
728         case AccessibilityStateEventType::EVENT_TOUCH_GUIDE_STATE_CHANGED:
729 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
730             reporter.setApiName("NAccessibilityClient.SubscribeState.touchGuideStateChange");
731 #endif // ACCESSIBILITY_EMULATOR_DEFINED
732             touchGuideStateListeners_->SubscribeObserver(env, args[PARAM1]);
733             break;
734         case AccessibilityStateEventType::EVENT_SCREEN_READER_STATE_CHANGED:
735 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
736             reporter.setApiName("NAccessibilityClient.SubscribeState.screenReaderStateChange");
737 #endif // ACCESSIBILITY_EMULATOR_DEFINED
738             screenReaderStateListeners_->SubscribeObserver(env, args[PARAM1]);
739             break;
740         default:
741             break;
742     }
743     return nullptr;
744 }
745 
GetAccessibilityStateEventType(napi_env env,napi_value * args,OHOS::Accessibility::RetError & errCode,uint32_t & type)746 void NAccessibilityClient::GetAccessibilityStateEventType(
747     napi_env env, napi_value* args, OHOS::Accessibility::RetError& errCode, uint32_t& type)
748 {
749     if (errCode == OHOS::Accessibility::RET_OK) {
750         std::string eventType = "";
751         if (!ParseString(env, eventType, args[PARAM0])) {
752             HILOG_ERROR("eventType type parse failed");
753             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
754         } else {
755             if (std::strcmp(eventType.c_str(), "accessibilityStateChange") == 0) {
756                 type = AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED;
757             } else if (std::strcmp(eventType.c_str(), "touchGuideStateChange") == 0) {
758                 type = AccessibilityStateEventType::EVENT_TOUCH_GUIDE_STATE_CHANGED;
759             } else if (std::strcmp(eventType.c_str(), "screenReaderStateChange") == 0) {
760                 type = AccessibilityStateEventType::EVENT_SCREEN_READER_STATE_CHANGED;
761             } else {
762                 HILOG_ERROR("SubscribeState eventType[%{public}s] is error", eventType.c_str());
763                 errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
764             }
765         }
766     }
767 }
768 
UnsubscribeState(napi_env env,napi_callback_info info)769 napi_value NAccessibilityClient::UnsubscribeState(napi_env env, napi_callback_info info)
770 {
771     HILOG_INFO();
772 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
773     ApiReportHelper reporter("NAccessibilityClient.UnsubscribeState");
774 #endif // ACCESSIBILITY_EMULATOR_DEFINED
775     size_t argc = ARGS_SIZE_TWO;
776     napi_value args[ARGS_SIZE_TWO] = {0};
777     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
778 
779     OHOS::Accessibility::RetError errCode = OHOS::Accessibility::RET_OK;
780     if (argc < ARGS_SIZE_TWO - 1) {
781         HILOG_ERROR("UnsubscribeState argc is invalid: %{public}zu", argc);
782         errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
783     }
784 
785     uint32_t type = AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED;
786     GetAccessibilityStateEventType(env, args, errCode, type);
787 
788     if (errCode == OHOS::Accessibility::RET_ERR_INVALID_PARAM) {
789         napi_value err = CreateBusinessError(env, errCode);
790         HILOG_ERROR("invalid param");
791         napi_throw(env, err);
792         return nullptr;
793     }
794     switch (type) {
795         case AccessibilityStateEventType::EVENT_ACCESSIBILITY_STATE_CHANGED:
796 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
797             reporter.setApiName("NAccessibilityClient.UnsubscribeState.accessibilityStateChange");
798 #endif // ACCESSIBILITY_EMULATOR_DEFINED
799             if (argc >= ARGS_SIZE_TWO && CheckJsFunction(env, args[PARAM1])) {
800                 accessibilityStateListeners_->UnsubscribeObserver(env, args[PARAM1]);
801             } else {
802                 accessibilityStateListeners_->UnsubscribeObservers();
803             }
804             break;
805         case AccessibilityStateEventType::EVENT_TOUCH_GUIDE_STATE_CHANGED:
806 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
807             reporter.setApiName("NAccessibilityClient.UnsubscribeState.touchGuideStateChange");
808 #endif // ACCESSIBILITY_EMULATOR_DEFINED
809             if (argc >= ARGS_SIZE_TWO && CheckJsFunction(env, args[PARAM1])) {
810                 touchGuideStateListeners_->UnsubscribeObserver(env, args[PARAM1]);
811             } else {
812                 touchGuideStateListeners_->UnsubscribeObservers();
813             }
814             break;
815         case AccessibilityStateEventType::EVENT_SCREEN_READER_STATE_CHANGED:
816 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
817             reporter.setApiName("NAccessibilityClient.UnsubscribeState.screenReaderStateChange");
818 #endif // ACCESSIBILITY_EMULATOR_DEFINED
819             if (argc >= ARGS_SIZE_TWO && CheckJsFunction(env, args[PARAM1])) {
820                 screenReaderStateListeners_->UnsubscribeObserver(env, args[PARAM1]);
821             } else {
822                 screenReaderStateListeners_->UnsubscribeObservers();
823             }
824             break;
825         default:
826             break;
827     }
828 
829     return nullptr;
830 }
831 
NotifyJS(napi_env env,bool state,napi_ref handlerRef)832 void StateListener::NotifyJS(napi_env env, bool state, napi_ref handlerRef)
833 {
834     HILOG_INFO("state = [%{public}s]", state ? "true" : "false");
835 
836     std::shared_ptr<StateCallbackInfo> callbackInfo = std::make_shared<StateCallbackInfo>();
837     if (callbackInfo == nullptr) {
838         HILOG_ERROR("Failed to create callbackInfo");
839         return;
840     }
841     callbackInfo->state_ = state;
842     callbackInfo->env_ = env;
843     callbackInfo->ref_ = handlerRef;
844     auto task = [callbackInfo]() {
845         if (callbackInfo == nullptr) {
846             return;
847         }
848 
849         napi_env tmpEnv = callbackInfo->env_;
850         auto closeScope = [tmpEnv](napi_handle_scope scope) {
851             napi_close_handle_scope(tmpEnv, scope);
852         };
853         std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scope(
854             OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
855         napi_value handler = nullptr;
856         napi_value callResult = nullptr;
857         napi_value jsEvent = nullptr;
858         napi_get_boolean(callbackInfo->env_, callbackInfo->state_, &jsEvent);
859         napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
860         napi_value undefined = nullptr;
861         napi_get_undefined(callbackInfo->env_, &undefined);
862         napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
863     };
864     if (napi_send_event(env, task, napi_eprio_high) != napi_status::napi_ok) {
865         HILOG_ERROR("failed to send event");
866     }
867 }
868 
OnStateChanged(const bool state)869 void StateListener::OnStateChanged(const bool state)
870 {
871     NotifyJS(env_, state, handlerRef_);
872 }
873 
DefineJSCaptionsManager(napi_env env)874 void NAccessibilityClient::DefineJSCaptionsManager(napi_env env)
875 {
876     napi_property_descriptor captionsManagerDesc[] = {
877         DECLARE_NAPI_GETTER_SETTER("enabled",
878             NAccessibilityClient::GetCaptionStateEnabled, NAccessibilityClient::SetCaptionStateEnabled),
879         DECLARE_NAPI_GETTER_SETTER("style",
880             NAccessibilityClient::GetCaptionStyle, NAccessibilityClient::SetCaptionStyle),
881         DECLARE_NAPI_FUNCTION("on", NAccessibilityClient::RegisterCaptionStateCallback),
882         DECLARE_NAPI_FUNCTION("off", NAccessibilityClient::DeregisterCaptionStateCallback),
883     };
884 
885     napi_value aaCons = nullptr;
886     NAPI_CALL_RETURN_VOID(env,
887         napi_define_class(env,
888             "CaptionsManager",
889             NAPI_AUTO_LENGTH,
890             NAccessibilityClient::AccessibleAbilityConstructor,
891             nullptr,
892             sizeof(captionsManagerDesc) / sizeof(captionsManagerDesc[0]),
893             captionsManagerDesc,
894             &aaCons));
895 
896     napi_create_reference(env, aaCons, 1, &NAccessibilityClient::aaConsRef_);
897 }
898 
AccessibleAbilityConstructor(napi_env env,napi_callback_info info)899 napi_value NAccessibilityClient::AccessibleAbilityConstructor(napi_env env, napi_callback_info info)
900 {
901     napi_value jsthis = nullptr;
902     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr));
903     return jsthis;
904 }
905 
906 
GetCaptionsManager(napi_env env,napi_callback_info info)907 napi_value NAccessibilityClient::GetCaptionsManager(napi_env env, napi_callback_info info)
908 {
909     HILOG_INFO();
910 #ifdef ACCESSIBILITY_EMULATOR_DEFINED
911     ApiReportHelper reporter("NAccessibilityClient.GetCaptionsManager");
912 #endif // ACCESSIBILITY_EMULATOR_DEFINED
913     napi_value result = 0;
914     napi_value aaCons = nullptr;
915     napi_get_reference_value(env, NAccessibilityClient::aaConsRef_, &aaCons);
916     napi_new_instance(env, aaCons, 0, nullptr, &result);
917     return result;
918 }
919 
SetCaptionStateEnabled(napi_env env,napi_callback_info info)920 napi_value NAccessibilityClient::SetCaptionStateEnabled(napi_env env, napi_callback_info info)
921 {
922     HILOG_INFO();
923     size_t argc = ARGS_SIZE_ONE;
924     napi_value parameters[ARGS_SIZE_ONE] = {0};
925     napi_get_cb_info(env, info, &argc, parameters, nullptr, nullptr);
926     if (argc >= ARGS_SIZE_ONE) {
927         bool captionState = false;
928         OHOS::Accessibility::RetError ret = OHOS::Accessibility::RET_OK;
929         if (!ParseBool(env, captionState, parameters[PARAM0])) {
930             ret = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
931         } else {
932             HILOG_INFO("captionState = %{public}s", captionState ? "True" : "False");
933 
934             auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
935             ret = instance.SetCaptionsState(captionState);
936         }
937         if (ret != OHOS::Accessibility::RET_OK) {
938             napi_value err = CreateBusinessError(env, ret);
939             napi_throw(env, err);
940         }
941     } else {
942         HILOG_ERROR("SetCaptionStateEnabled argc size Error");
943         napi_value err = CreateBusinessError(env, OHOS::Accessibility::RET_ERR_INVALID_PARAM);
944         napi_throw(env, err);
945     }
946 
947     napi_value undefined = nullptr;
948     napi_get_undefined(env, &undefined);
949     return undefined;
950 }
951 
GetCaptionStateEnabled(napi_env env,napi_callback_info info)952 napi_value NAccessibilityClient::GetCaptionStateEnabled(napi_env env, napi_callback_info info)
953 {
954     HILOG_INFO();
955     napi_value captionStateEnabled = nullptr;
956 
957     auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
958     bool captionState = false;
959     instance.GetCaptionsState(captionState);
960     napi_get_boolean(env, captionState, &captionStateEnabled);
961 
962     HILOG_INFO("captionState = %{public}s", captionState ? "True" : "False");
963 
964     return captionStateEnabled;
965 }
966 
SetCaptionStyle(napi_env env,napi_callback_info info)967 napi_value NAccessibilityClient::SetCaptionStyle(napi_env env, napi_callback_info info)
968 {
969     HILOG_INFO();
970     size_t argc = ARGS_SIZE_ONE;
971     napi_value parameters[ARGS_SIZE_ONE] = {0};
972     napi_get_cb_info(env, info, &argc, parameters, nullptr, nullptr);
973     if (argc >= ARGS_SIZE_ONE) {
974         OHOS::AccessibilityConfig::CaptionProperty captionProperty = {};
975         OHOS::Accessibility::RetError ret = OHOS::Accessibility::RET_OK;
976         if (!ConvertObjToCaptionProperty(env, parameters[PARAM0], &captionProperty)) {
977             ret = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
978         } else {
979             auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
980             ret = instance.SetCaptionsProperty(captionProperty);
981         }
982         if (ret != OHOS::Accessibility::RET_OK) {
983             napi_value err = CreateBusinessError(env, ret);
984             napi_throw(env, err);
985         }
986     } else {
987         HILOG_ERROR("SetCaptionStyle argc size Error");
988         napi_value err = CreateBusinessError(env, OHOS::Accessibility::RET_ERR_INVALID_PARAM);
989         napi_throw(env, err);
990     }
991 
992     napi_value undefined = nullptr;
993     napi_get_undefined(env, &undefined);
994     return undefined;
995 }
996 
GetCaptionStyle(napi_env env,napi_callback_info info)997 napi_value NAccessibilityClient::GetCaptionStyle(napi_env env, napi_callback_info info)
998 {
999     HILOG_INFO();
1000     napi_value captionStyle = nullptr;
1001     napi_get_reference_value(env, NAccessibilityClient::aaStyleConsRef_, &captionStyle);
1002 
1003     return captionStyle;
1004 }
1005 
RegisterCaptionStateCallback(napi_env env,napi_callback_info info)1006 napi_value NAccessibilityClient::RegisterCaptionStateCallback(napi_env env, napi_callback_info info)
1007 {
1008     HILOG_INFO();
1009 
1010     size_t argc = ARGS_SIZE_TWO;
1011     napi_value args[ARGS_SIZE_TWO] = {0};
1012     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1013     if (status != napi_ok) {
1014         HILOG_ERROR("RegisterCaptionStateCallback Failed to get event type");
1015         napi_value err = CreateBusinessError(env, OHOS::Accessibility::RET_ERR_FAILED);
1016         napi_throw(env, err);
1017         return nullptr;
1018     }
1019 
1020     OHOS::Accessibility::RetError errCode = OHOS::Accessibility::RET_OK;
1021     if (argc < ARGS_SIZE_TWO) {
1022         HILOG_ERROR("RegisterCaptionStateCallback argc is invalid: %{public}zu", argc);
1023         errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
1024     }
1025 
1026     OHOS::AccessibilityConfig::CONFIG_ID type = OHOS::AccessibilityConfig::CONFIG_ID_MAX;
1027     if (errCode == OHOS::Accessibility::RET_OK) {
1028         std::string eventType = "";
1029         if (!ParseString(env, eventType, args[PARAM0])) {
1030             HILOG_ERROR("eventType type parse failed");
1031             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
1032         } else {
1033             if (std::strcmp(eventType.c_str(), "enableChange") == 0) {
1034                 type = OHOS::AccessibilityConfig::CONFIG_CAPTION_STATE;
1035             } else if (std::strcmp(eventType.c_str(), "styleChange") == 0) {
1036                 type =  OHOS::AccessibilityConfig::CONFIG_CAPTION_STYLE;
1037             } else {
1038                 HILOG_ERROR("SubscribeState eventType[%{public}s] is error", eventType.c_str());
1039                 errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
1040             }
1041         }
1042     }
1043 
1044     if (errCode == OHOS::Accessibility::RET_OK) {
1045         napi_valuetype valueType = napi_null;
1046         napi_typeof(env, args[PARAM1], &valueType);
1047         if (valueType != napi_function) {
1048             HILOG_ERROR("RegisterCaptionStateCallback args[PARAM1] format is wrong");
1049             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
1050         }
1051     }
1052 
1053     if (errCode == OHOS::Accessibility::RET_ERR_INVALID_PARAM) {
1054         napi_value err = CreateBusinessError(env, errCode);
1055         HILOG_ERROR("invalid param");
1056         napi_throw(env, err);
1057         return nullptr;
1058     }
1059 
1060     captionListeners_->SubscribeObserver(env, type, args[PARAM1]);
1061 
1062     return nullptr;
1063 }
1064 
DeregisterCaptionStateCallback(napi_env env,napi_callback_info info)1065 napi_value NAccessibilityClient::DeregisterCaptionStateCallback(napi_env env, napi_callback_info info)
1066 {
1067     HILOG_INFO();
1068     size_t argc = ARGS_SIZE_TWO;
1069     napi_value args[ARGS_SIZE_TWO] = {0};
1070     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1071 
1072     OHOS::Accessibility::RetError errCode = OHOS::Accessibility::RET_OK;
1073     if (argc < ARGS_SIZE_TWO - 1) {
1074         HILOG_ERROR("DeregisterCaptionStateCallback argc is invalid: %{public}zu", argc);
1075         errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
1076     }
1077 
1078     OHOS::AccessibilityConfig::CONFIG_ID type = OHOS::AccessibilityConfig::CONFIG_ID_MAX;
1079     if (errCode == OHOS::Accessibility::RET_OK) {
1080         std::string eventType = "";
1081         if (!ParseString(env, eventType, args[PARAM0])) {
1082             HILOG_ERROR("eventType type parse failed");
1083             errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
1084         } else {
1085             if (std::strcmp(eventType.c_str(), "enableChange") == 0) {
1086                 type = OHOS::AccessibilityConfig::CONFIG_CAPTION_STATE;
1087             } else if (std::strcmp(eventType.c_str(), "styleChange") == 0) {
1088                 type =  OHOS::AccessibilityConfig::CONFIG_CAPTION_STYLE;
1089             } else {
1090                 HILOG_ERROR("SubscribeState eventType[%{public}s] is error", eventType.c_str());
1091                 errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
1092             }
1093         }
1094     }
1095 
1096     if (errCode == OHOS::Accessibility::RET_ERR_INVALID_PARAM) {
1097         napi_value err = CreateBusinessError(env, errCode);
1098         HILOG_ERROR("DeregisterCaptionStateCallback invalid param");
1099         napi_throw(env, err);
1100         return nullptr;
1101     }
1102 
1103     if (argc >= ARGS_SIZE_TWO && CheckJsFunction(env, args[PARAM1])) {
1104         captionListeners_->UnsubscribeObserver(env, type, args[PARAM1]);
1105     } else {
1106         captionListeners_->UnsubscribeObservers(type);
1107     }
1108 
1109     return nullptr;
1110 }
1111 
DefineJSCaptionsStyle(napi_env env)1112 void NAccessibilityClient::DefineJSCaptionsStyle(napi_env env)
1113 {
1114     napi_property_descriptor captionsStyleDesc[] = {
1115         DECLARE_NAPI_GETTER_SETTER("fontFamily",
1116             NAccessibilityClient::GetCaptionsFontFamily, NAccessibilityClient::SetCaptionsFontFamily),
1117         DECLARE_NAPI_GETTER_SETTER("fontScale",
1118             NAccessibilityClient::GetCaptionsFontScale, NAccessibilityClient::SetCaptionsFontScale),
1119         DECLARE_NAPI_GETTER_SETTER("fontColor",
1120             NAccessibilityClient::GetCaptionFrontColor, NAccessibilityClient::SetCaptionFrontColor),
1121         DECLARE_NAPI_GETTER_SETTER("fontEdgeType",
1122             NAccessibilityClient::GetCaptionFontEdgeType, NAccessibilityClient::SetCaptionFontEdgeType),
1123         DECLARE_NAPI_GETTER_SETTER("backgroundColor",
1124             NAccessibilityClient::GetCaptionBackgroundColor, NAccessibilityClient::SetCaptionBackgroundColor),
1125         DECLARE_NAPI_GETTER_SETTER("windowColor",
1126             NAccessibilityClient::GetCaptionWindowColor, NAccessibilityClient::SetCaptionWindowColor),
1127     };
1128 
1129     napi_value aaStyleCons = nullptr;
1130     napi_value captionStyle = nullptr;
1131 
1132     NAPI_CALL_RETURN_VOID(env,
1133         napi_define_class(env,
1134             "CaptionsStyle",
1135             NAPI_AUTO_LENGTH,
1136             NAccessibilityClient::AccessibleAbilityConstructorStyle,
1137             nullptr,
1138             sizeof(captionsStyleDesc) / sizeof(captionsStyleDesc[0]),
1139             captionsStyleDesc,
1140             &aaStyleCons));
1141 
1142     napi_new_instance(env, aaStyleCons, 0, nullptr, &captionStyle);
1143     napi_create_reference(env, captionStyle, 1, &NAccessibilityClient::aaStyleConsRef_);
1144 }
1145 
AccessibleAbilityConstructorStyle(napi_env env,napi_callback_info info)1146 napi_value NAccessibilityClient::AccessibleAbilityConstructorStyle(napi_env env, napi_callback_info info)
1147 {
1148     HILOG_INFO();
1149     napi_value jsthis = nullptr;
1150     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr));
1151     return jsthis;
1152 }
1153 
GetCaptionsFontFamily(napi_env env,napi_callback_info info)1154 napi_value NAccessibilityClient::GetCaptionsFontFamily(napi_env env, napi_callback_info info)
1155 {
1156     HILOG_INFO();
1157     napi_value returnValue = nullptr;
1158     auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
1159     OHOS::AccessibilityConfig::CaptionProperty captionProperty = {};
1160     instance.GetCaptionsProperty(captionProperty);
1161     napi_create_string_utf8(env, captionProperty.GetFontFamily().c_str(), NAPI_AUTO_LENGTH, &returnValue);
1162     return returnValue;
1163 }
1164 
SetCaptionsFontFamily(napi_env env,napi_callback_info info)1165 napi_value NAccessibilityClient::SetCaptionsFontFamily(napi_env env, napi_callback_info info)
1166 {
1167     HILOG_INFO();
1168     size_t argc = ARGS_SIZE_ONE;
1169     napi_value parameters[ARGS_SIZE_ONE] = {0};
1170     napi_get_cb_info(env, info, &argc, parameters, nullptr, nullptr);
1171     if (argc >= ARGS_SIZE_ONE) {
1172         // Get input FontFamily
1173         std::string fontFamily = "";
1174         OHOS::Accessibility::RetError ret = OHOS::Accessibility::RET_OK;
1175         if (ParseString(env, fontFamily, parameters[PARAM0]) && fontFamily.length() > 0) {
1176             HILOG_INFO("FontFamily = %{public}s", fontFamily.c_str());
1177             auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
1178             OHOS::AccessibilityConfig::CaptionProperty captionProperty {};
1179             instance.GetCaptionsProperty(captionProperty);
1180             // Change the input info and then set the CaptionProperty
1181             captionProperty.SetFontFamily(std::string(fontFamily));
1182             ret = instance.SetCaptionsProperty(captionProperty);
1183         } else {
1184             ret = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
1185         }
1186         if (ret != OHOS::Accessibility::RET_OK) {
1187             napi_value err = CreateBusinessError(env, ret);
1188             napi_throw(env, err);
1189         }
1190     } else {
1191         HILOG_ERROR("SetCaptionsFontFamily argc size Error");
1192         napi_value err = CreateBusinessError(env, OHOS::Accessibility::RET_ERR_INVALID_PARAM);
1193         napi_throw(env, err);
1194     }
1195     napi_value undefined = nullptr;
1196     napi_get_undefined(env, &undefined);
1197     return undefined;
1198 }
1199 
GetCaptionsFontScale(napi_env env,napi_callback_info info)1200 napi_value NAccessibilityClient::GetCaptionsFontScale(napi_env env, napi_callback_info info)
1201 {
1202     HILOG_INFO();
1203     napi_value returnValue = nullptr;
1204     auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
1205     OHOS::AccessibilityConfig::CaptionProperty captionProperty = {};
1206     instance.GetCaptionsProperty(captionProperty);
1207     napi_create_int32(env, captionProperty.GetFontScale(), &returnValue);
1208     return returnValue;
1209 }
1210 
SetCaptionsFontScale(napi_env env,napi_callback_info info)1211 napi_value NAccessibilityClient::SetCaptionsFontScale(napi_env env, napi_callback_info info)
1212 {
1213     HILOG_INFO();
1214     size_t argc = ARGS_SIZE_ONE;
1215     napi_value parameters[ARGS_SIZE_ONE] = {0};
1216     napi_get_cb_info(env, info, &argc, parameters, nullptr, nullptr);
1217     if (argc >= ARGS_SIZE_ONE) {
1218         // Get input FontScale
1219         int32_t num = 100;
1220         OHOS::Accessibility::RetError ret = OHOS::Accessibility::RET_OK;
1221         if (ParseInt32(env, num, parameters[PARAM0])) {
1222             HILOG_INFO("FontScale = %{public}d", num);
1223             auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
1224             OHOS::AccessibilityConfig::CaptionProperty captionProperty {};
1225             instance.GetCaptionsProperty(captionProperty);
1226             // Change the input info and then set the CaptionProperty
1227             captionProperty.SetFontScale(num);
1228             ret = instance.SetCaptionsProperty(captionProperty);
1229         } else {
1230             ret = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
1231         }
1232         if (ret != OHOS::Accessibility::RET_OK) {
1233             napi_value err = CreateBusinessError(env, ret);
1234             napi_throw(env, err);
1235         }
1236     } else {
1237         HILOG_ERROR("SetCaptionsFontScale argc size Error");
1238         napi_value err = CreateBusinessError(env, OHOS::Accessibility::RET_ERR_INVALID_PARAM);
1239         napi_throw(env, err);
1240     }
1241     napi_value undefined = nullptr;
1242     napi_get_undefined(env, &undefined);
1243     return undefined;
1244 }
1245 
GetCaptionFrontColor(napi_env env,napi_callback_info info)1246 napi_value NAccessibilityClient::GetCaptionFrontColor(napi_env env, napi_callback_info info)
1247 {
1248     HILOG_INFO();
1249     napi_value returnValue = nullptr;
1250     auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
1251     OHOS::AccessibilityConfig::CaptionProperty captionProperty = {};
1252     instance.GetCaptionsProperty(captionProperty);
1253     uint32_t color = captionProperty.GetFontColor();
1254     std::string colorStr = ConvertColorToString(color);
1255     napi_create_string_utf8(env, colorStr.c_str(), NAPI_AUTO_LENGTH, &returnValue);
1256     return returnValue;
1257 }
1258 
SetCaptionFrontColor(napi_env env,napi_callback_info info)1259 napi_value NAccessibilityClient::SetCaptionFrontColor(napi_env env, napi_callback_info info)
1260 {
1261     HILOG_INFO();
1262     size_t argc = ARGS_SIZE_ONE;
1263     napi_value parameters[ARGS_SIZE_ONE] = {0};
1264     napi_get_cb_info(env, info, &argc, parameters, nullptr, nullptr);
1265     if (argc >= ARGS_SIZE_ONE) {
1266         HILOG_DEBUG("SetCaptionFrontColor argc > 1");
1267         uint32_t color = GetColorValue(env, parameters[PARAM0]);
1268         auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
1269         OHOS::AccessibilityConfig::CaptionProperty captionProperty {};
1270         instance.GetCaptionsProperty(captionProperty);
1271         // Change the input info and then set the CaptionProperty
1272         captionProperty.SetFontColor(color);
1273         OHOS::Accessibility::RetError ret = instance.SetCaptionsProperty(captionProperty);
1274         if (ret != OHOS::Accessibility::RET_OK) {
1275             napi_value err = CreateBusinessError(env, ret);
1276             napi_throw(env, err);
1277         }
1278     } else {
1279         HILOG_ERROR("SetCaptionFrontColor argc size Error");
1280         napi_value err = CreateBusinessError(env, OHOS::Accessibility::RET_ERR_INVALID_PARAM);
1281         napi_throw(env, err);
1282     }
1283     napi_value undefined = nullptr;
1284     napi_get_undefined(env, &undefined);
1285     return undefined;
1286 }
1287 
GetCaptionFontEdgeType(napi_env env,napi_callback_info info)1288 napi_value NAccessibilityClient::GetCaptionFontEdgeType(napi_env env, napi_callback_info info)
1289 {
1290     HILOG_INFO();
1291     napi_value returnValue = nullptr;
1292     auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
1293     OHOS::AccessibilityConfig::CaptionProperty captionProperty = {};
1294     instance.GetCaptionsProperty(captionProperty);
1295     napi_create_string_utf8(env, captionProperty.GetFontEdgeType().c_str(), NAPI_AUTO_LENGTH, &returnValue);
1296     return returnValue;
1297 }
1298 
SetCaptionFontEdgeType(napi_env env,napi_callback_info info)1299 napi_value NAccessibilityClient::SetCaptionFontEdgeType(napi_env env, napi_callback_info info)
1300 {
1301     HILOG_INFO();
1302     size_t argc = ARGS_SIZE_ONE;
1303     napi_value parameters[ARGS_SIZE_ONE] = {0};
1304 
1305     napi_get_cb_info(env, info, &argc, parameters, nullptr, nullptr);
1306     if (argc >= ARGS_SIZE_ONE) {
1307         // Get input FontEdgeType
1308         std::string fontEdgeType = "";
1309         OHOS::Accessibility::RetError ret = OHOS::Accessibility::RET_OK;
1310         if (ParseString(env, fontEdgeType, parameters[PARAM0]) && fontEdgeType.length() > 0) {
1311             HILOG_INFO("fontEdgeType = %{public}s", fontEdgeType.c_str());
1312             auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
1313             OHOS::AccessibilityConfig::CaptionProperty captionProperty {};
1314             instance.GetCaptionsProperty(captionProperty);
1315             // Change the input info and then set the CaptionProperty
1316             captionProperty.SetFontEdgeType(std::string(fontEdgeType));
1317             ret = instance.SetCaptionsProperty(captionProperty);
1318         } else {
1319             ret = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
1320         }
1321         if (ret != OHOS::Accessibility::RET_OK) {
1322             napi_value err = CreateBusinessError(env, ret);
1323             napi_throw(env, err);
1324         }
1325     } else {
1326         HILOG_ERROR("SetCaptionFontEdgeType argc size Error");
1327         napi_value err = CreateBusinessError(env, OHOS::Accessibility::RET_ERR_INVALID_PARAM);
1328         napi_throw(env, err);
1329     }
1330     napi_value undefined = nullptr;
1331     napi_get_undefined(env, &undefined);
1332     return undefined;
1333 }
1334 
GetCaptionBackgroundColor(napi_env env,napi_callback_info info)1335 napi_value NAccessibilityClient::GetCaptionBackgroundColor(napi_env env, napi_callback_info info)
1336 {
1337     HILOG_INFO();
1338     napi_value returnValue = nullptr;
1339     auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
1340     OHOS::AccessibilityConfig::CaptionProperty captionProperty = {};
1341     instance.GetCaptionsProperty(captionProperty);
1342     uint32_t color = captionProperty.GetBackgroundColor();
1343     std::string colorStr = ConvertColorToString(color);
1344     napi_create_string_utf8(env, colorStr.c_str(), NAPI_AUTO_LENGTH, &returnValue);
1345     return returnValue;
1346 }
1347 
SetCaptionBackgroundColor(napi_env env,napi_callback_info info)1348 napi_value NAccessibilityClient::SetCaptionBackgroundColor(napi_env env, napi_callback_info info)
1349 {
1350     HILOG_INFO();
1351     size_t argc = ARGS_SIZE_ONE;
1352     napi_value parameters[ARGS_SIZE_ONE] = {0};
1353     napi_get_cb_info(env, info, &argc, parameters, nullptr, nullptr);
1354     if (argc >= ARGS_SIZE_ONE) {
1355         HILOG_DEBUG("SetCaptionBackgroundColor argc > 1");
1356         uint32_t color = GetColorValue(env, parameters[PARAM0]);
1357         auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
1358         OHOS::AccessibilityConfig::CaptionProperty captionProperty {};
1359         instance.GetCaptionsProperty(captionProperty);
1360         // Change the input info and then set the CaptionProperty
1361         captionProperty.SetBackgroundColor(color);
1362         OHOS::Accessibility::RetError ret = instance.SetCaptionsProperty(captionProperty);
1363         if (ret != OHOS::Accessibility::RET_OK) {
1364             napi_value err = CreateBusinessError(env, ret);
1365             napi_throw(env, err);
1366         }
1367     } else {
1368         HILOG_ERROR("SetCaptionBackgroundColor argc size Error");
1369         napi_value err = CreateBusinessError(env, OHOS::Accessibility::RET_ERR_INVALID_PARAM);
1370         napi_throw(env, err);
1371     }
1372     napi_value undefined = nullptr;
1373     napi_get_undefined(env, &undefined);
1374     return undefined;
1375 }
1376 
GetCaptionWindowColor(napi_env env,napi_callback_info info)1377 napi_value NAccessibilityClient::GetCaptionWindowColor(napi_env env, napi_callback_info info)
1378 {
1379     HILOG_INFO();
1380     napi_value returnValue = nullptr;
1381     auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
1382     OHOS::AccessibilityConfig::CaptionProperty captionProperty = {};
1383     instance.GetCaptionsProperty(captionProperty);
1384     uint32_t color = captionProperty.GetWindowColor();
1385     std::string colorStr = ConvertColorToString(color);
1386     napi_create_string_utf8(env, colorStr.c_str(), NAPI_AUTO_LENGTH, &returnValue);
1387     return returnValue;
1388 }
1389 
SetCaptionWindowColor(napi_env env,napi_callback_info info)1390 napi_value NAccessibilityClient::SetCaptionWindowColor(napi_env env, napi_callback_info info)
1391 {
1392     HILOG_INFO();
1393     size_t argc = ARGS_SIZE_ONE;
1394     napi_value parameters[ARGS_SIZE_ONE] = {0};
1395     napi_get_cb_info(env, info, &argc, parameters, nullptr, nullptr);
1396     if (argc >= ARGS_SIZE_ONE) {
1397         HILOG_DEBUG("SetCaptionWindowColor argc > 1");
1398         uint32_t color = GetColorValue(env, parameters[PARAM0]);
1399         auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
1400         OHOS::AccessibilityConfig::CaptionProperty captionProperty {};
1401         instance.GetCaptionsProperty(captionProperty);
1402         // Change the input info and then set the CaptionProperty
1403         captionProperty.SetWindowColor(color);
1404         OHOS::Accessibility::RetError ret = instance.SetCaptionsProperty(captionProperty);
1405         if (ret != OHOS::Accessibility::RET_OK) {
1406             napi_value err = CreateBusinessError(env, ret);
1407             napi_throw(env, err);
1408         }
1409     } else {
1410         HILOG_ERROR("SetCaptionWindowColor argc size Error");
1411         napi_value err = CreateBusinessError(env, OHOS::Accessibility::RET_ERR_INVALID_PARAM);
1412         napi_throw(env, err);
1413     }
1414     napi_value undefined = nullptr;
1415     napi_get_undefined(env, &undefined);
1416     return undefined;
1417 }
1418 
SubscribeToFramework()1419 void StateListenerImpl::SubscribeToFramework()
1420 {
1421     auto asaClient = AccessibilitySystemAbilityClient::GetInstance();
1422     if (asaClient) {
1423         asaClient->SubscribeStateObserver(shared_from_this(), type_);
1424     }
1425 }
1426 
UnsubscribeFromFramework()1427 void StateListenerImpl::UnsubscribeFromFramework()
1428 {
1429     HILOG_INFO("UnsubscribeFromFramework");
1430     auto asaClient = AccessibilitySystemAbilityClient::GetInstance();
1431     if (asaClient) {
1432         asaClient->UnsubscribeStateObserver(shared_from_this(), type_);
1433     }
1434 }
1435 
OnStateChanged(const bool state)1436 void StateListenerImpl::OnStateChanged(const bool state)
1437 {
1438     HILOG_INFO();
1439     std::lock_guard<ffrt::mutex> lock(mutex_);
1440     for (auto &observer : observers_) {
1441         observer->OnStateChanged(state);
1442     }
1443 }
1444 
DeleteObserverReference(napi_env env,std::shared_ptr<StateListener> observer)1445 void StateListenerImpl::DeleteObserverReference(napi_env env, std::shared_ptr<StateListener> observer)
1446 {
1447     if (observer == nullptr) {
1448         return;
1449     }
1450     std::shared_ptr<AccessibilityCallbackInfo> callbackInfo =
1451         std::make_shared<AccessibilityCallbackInfo>();
1452     if (callbackInfo == nullptr) {
1453         HILOG_ERROR("failed to create callbackInfo");
1454         return;
1455     }
1456     callbackInfo->env_ = observer->env_;
1457     callbackInfo->ref_ = observer->handlerRef_;
1458     auto task = [callbackInfo]() {
1459         if (callbackInfo == nullptr) {
1460             return;
1461         }
1462         napi_env tmpEnv = callbackInfo->env_;
1463         auto closeScope = [tmpEnv](napi_handle_scope scope) {
1464             napi_close_handle_scope(tmpEnv, scope);
1465         };
1466         std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scope(
1467             OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
1468         napi_delete_reference(tmpEnv, callbackInfo->ref_);
1469     };
1470     if (napi_send_event(env, task, napi_eprio_high) != napi_status::napi_ok) {
1471         HILOG_ERROR("failed to send event");
1472     }
1473 }
1474 
SubscribeObserver(napi_env env,napi_value observer)1475 void StateListenerImpl::SubscribeObserver(napi_env env, napi_value observer)
1476 {
1477     HILOG_INFO();
1478     std::lock_guard<ffrt::mutex> lock(mutex_);
1479     for (auto iter = observers_.begin(); iter != observers_.end();) {
1480         if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->handlerRef_)) {
1481             HILOG_DEBUG("SubscribeObserver Observer exist");
1482             return;
1483         } else {
1484             iter++;
1485         }
1486     }
1487 
1488     napi_ref ref;
1489     napi_create_reference(env, observer, 1, &ref);
1490     std::shared_ptr<StateListener> stateListener = std::make_shared<StateListener>(env, ref);
1491 
1492     observers_.emplace_back(stateListener);
1493     HILOG_INFO("observer size%{public}zu", observers_.size());
1494 }
1495 
UnsubscribeObserver(napi_env env,napi_value observer)1496 void StateListenerImpl::UnsubscribeObserver(napi_env env, napi_value observer)
1497 {
1498     HILOG_INFO();
1499     std::lock_guard<ffrt::mutex> lock(mutex_);
1500     for (auto iter = observers_.begin(); iter != observers_.end();) {
1501         if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->handlerRef_)) {
1502             DeleteObserverReference(env, *iter);
1503             observers_.erase(iter);
1504             return;
1505         } else {
1506             iter++;
1507         }
1508     }
1509 }
1510 
UnsubscribeObservers()1511 void StateListenerImpl::UnsubscribeObservers()
1512 {
1513     HILOG_INFO();
1514     std::lock_guard<ffrt::mutex> lock(mutex_);
1515     for (auto iter = observers_.begin(); iter != observers_.end();) {
1516         DeleteObserverReference((*iter)->env_, *iter);
1517         observers_.erase(iter);
1518     }
1519     observers_.clear();
1520 }