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