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