• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "accessibility_utils.h"
17 #include "accessibility_config_observer.h"
18 
19 #include <uv.h>
20 
21 #include "hilog_wrapper.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 
25 using namespace OHOS;
26 using namespace OHOS::Accessibility;
27 using namespace OHOS::AccessibilityNapi;
28 using namespace OHOS::AccessibilityConfig;
29 
30 namespace OHOS {
31 namespace Accessibility {
TmpOpenScope(napi_env env)32 napi_handle_scope TmpOpenScope(napi_env env)
33 {
34     napi_handle_scope scope = nullptr;
35     NAPI_CALL(env, napi_open_handle_scope(env, &scope));
36     return scope;
37 }
38 } // namespace Accessibility
39 } // namespace OHOS
40 
OnConfigChangedExtra(const ConfigValue & value)41 void NAccessibilityConfigObserver::OnConfigChangedExtra(const ConfigValue &value)
42 {
43     HILOG_INFO("id = [%{public}d]", static_cast<int32_t>(configId_));
44     if (configId_ == CONFIG_CONTENT_TIMEOUT) {
45         NotifyIntChanged2JS(static_cast<int32_t>(value.contentTimeout));
46     } else if (configId_ == CONFIG_BRIGHTNESS_DISCOUNT) {
47         NotifyFloatChanged2JS(value.brightnessDiscount);
48     } else if (configId_ == CONFIG_AUDIO_BALANCE) {
49         NotifyFloatChanged2JS(value.audioBalance);
50     } else if (configId_ ==  CONFIG_HIGH_CONTRAST_TEXT) {
51         NotifyStateChanged2JS(value.highContrastText);
52     } else if (configId_ ==  CONFIG_DALTONIZATION_STATE) {
53         NotifyStateChanged2JS(value.daltonizationState);
54     } else if (configId_ == CONFIG_INVERT_COLOR) {
55         NotifyStateChanged2JS(value.invertColor);
56     } else if (configId_ == CONFIG_ANIMATION_OFF) {
57         NotifyStateChanged2JS(value.animationOff);
58     } else if (configId_ == CONFIG_AUDIO_MONO) {
59         NotifyStateChanged2JS(value.audioMono);
60     } else if (configId_ == CONIFG_CLICK_RESPONSE_TIME) {
61         NotifyStringChanged2JS(ConvertClickResponseTimeTypeToString(value.clickResponseTime));
62     } else if (configId_ == CONFIG_IGNORE_REPEAT_CLICK_TIME) {
63         NotifyStringChanged2JS(ConvertIgnoreRepeatClickTimeTypeToString(value.ignoreRepeatClickTime));
64     } else if (configId_ == CONFIG_IGNORE_REPEAT_CLICK_STATE) {
65         NotifyStateChanged2JS(value.ignoreRepeatClickState);
66     }
67 }
68 
OnConfigChanged(const ConfigValue & value)69 void NAccessibilityConfigObserver::OnConfigChanged(const ConfigValue &value)
70 {
71     HILOG_INFO("id = [%{public}d]", static_cast<int32_t>(configId_));
72     if (configId_ == CONFIG_CAPTION_STATE) {
73         NotifyStateChanged2JS(value.captionState);
74     } else if (configId_ == CONFIG_CAPTION_STYLE) {
75         NotifyPropertyChanged2JS(value.captionStyle);
76     } else if (configId_ == CONFIG_SCREEN_MAGNIFICATION) {
77         NotifyStateChanged2JS(value.screenMagnifier);
78     } else if (configId_ == CONFIG_MOUSE_KEY) {
79         NotifyStateChanged2JS(value.mouseKey);
80     } else if (configId_ == CONFIG_SHORT_KEY) {
81         NotifyStateChanged2JS(value.shortkey);
82     } else if (configId_ == CONFIG_SHORT_KEY_TARGET) {
83         NotifyStringChanged2JS(value.shortkey_target);
84     } else if (configId_ == CONFIG_SHORT_KEY_MULTI_TARGET) {
85         NotifyStringVectorChanged2JS(value.shortkeyMultiTarget);
86     } else if (configId_ == CONFIG_MOUSE_AUTOCLICK) {
87         NotifyIntChanged2JS(value.mouseAutoClick);
88     } else if (configId_ == CONFIG_DALTONIZATION_COLOR_FILTER) {
89         OnDaltonizationColorFilterConfigChanged();
90     } else {
91         OnConfigChangedExtra(value);
92     }
93 }
94 
OnDaltonizationColorFilterConfigChanged()95 void NAccessibilityConfigObserver::OnDaltonizationColorFilterConfigChanged()
96 {
97     auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
98     DALTONIZATION_TYPE type = Normal;
99     RetError ret = instance.GetDaltonizationColorFilter(type);
100     if (ret == RET_OK) {
101         NotifyStringChanged2JS(ConvertDaltonizationTypeToString(type));
102     } else {
103         NotifyStringChanged2JS(ConvertDaltonizationTypeToString(type));
104         HILOG_ERROR("get DaltonizationColorFilter failed: %{public}d", ret);
105     }
106 }
107 
NotifyStateChanged(uv_work_t * work)108 int NAccessibilityConfigObserver::NotifyStateChanged(uv_work_t *work)
109 {
110     uv_loop_s *loop = nullptr;
111     napi_get_uv_event_loop(env_, &loop);
112     int ret = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {},
113         [](uv_work_t *work, int status) {
114             StateCallbackInfo *callbackInfo = static_cast<StateCallbackInfo*>(work->data);
115             auto closeScope = [callbackInfo](napi_handle_scope scope) {
116                 napi_close_handle_scope(callbackInfo->env_, scope);
117             };
118             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
119                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
120             napi_value jsEvent;
121             napi_create_object(callbackInfo->env_, &jsEvent);
122             napi_get_boolean(callbackInfo->env_, callbackInfo->state_, &jsEvent);
123 
124             napi_value handler = nullptr;
125             napi_value callResult = nullptr;
126             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
127             napi_value undefined = nullptr;
128             napi_get_undefined(callbackInfo->env_, &undefined);
129             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
130             int32_t result;
131             napi_get_value_int32(callbackInfo->env_, callResult, &result);
132             HILOG_INFO("NotifyStateChangedJS napi_call_function result[%{public}d]", result);
133             delete callbackInfo;
134             callbackInfo = nullptr;
135             delete work;
136             work = nullptr;
137         },
138         uv_qos_default);
139     return ret;
140 }
141 
NotifyPropertyChanged(uv_work_t * work)142 int NAccessibilityConfigObserver::NotifyPropertyChanged(uv_work_t *work)
143 {
144     uv_loop_s *loop = nullptr;
145     napi_get_uv_event_loop(env_, &loop);
146     int ret = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {},
147         [](uv_work_t *work, int status) {
148             CaptionCallbackInfo *callbackInfo = static_cast<CaptionCallbackInfo*>(work->data);
149             auto closeScope = [callbackInfo](napi_handle_scope scope) {
150                 napi_close_handle_scope(callbackInfo->env_, scope);
151             };
152             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
153                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
154             napi_value jsEvent;
155             napi_create_object(callbackInfo->env_, &jsEvent);
156             ConvertCaptionPropertyToJS(callbackInfo->env_, jsEvent, callbackInfo->caption_);
157 
158             napi_value handler = nullptr;
159             napi_value callResult = nullptr;
160             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
161             napi_value undefined = nullptr;
162             napi_get_undefined(callbackInfo->env_, &undefined);
163             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
164             int32_t result;
165             napi_get_value_int32(callbackInfo->env_, callResult, &result);
166             HILOG_INFO("NotifyPropertyChangedJS napi_call_function result[%{public}d]", result);
167             delete callbackInfo;
168             callbackInfo = nullptr;
169             delete work;
170             work = nullptr;
171         },
172         uv_qos_default);
173     return ret;
174 }
175 
NotifyStringChanged(uv_work_t * work)176 int NAccessibilityConfigObserver::NotifyStringChanged(uv_work_t *work)
177 {
178     uv_loop_s *loop = nullptr;
179     napi_get_uv_event_loop(env_, &loop);
180     int ret = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {},
181         [](uv_work_t *work, int status) {
182             StateCallbackInfo *callbackInfo = static_cast<StateCallbackInfo*>(work->data);
183             auto closeScope = [callbackInfo](napi_handle_scope scope) {
184                 napi_close_handle_scope(callbackInfo->env_, scope);
185             };
186             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
187                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
188             napi_value jsEvent;
189             napi_create_string_utf8(callbackInfo->env_, callbackInfo->stringValue_.c_str(),
190                 callbackInfo->stringValue_.length(), &jsEvent);
191             napi_value handler = nullptr;
192             napi_value callResult = nullptr;
193             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
194             napi_value undefined = nullptr;
195             napi_get_undefined(callbackInfo->env_, &undefined);
196             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
197             size_t result;
198             const uint32_t BUF_SIZE = 1024;
199             char buf[BUF_SIZE] = {0};
200             napi_get_value_string_utf8(callbackInfo->env_, callResult, buf, BUF_SIZE, &result);
201             HILOG_INFO("NotifyStringChanged2JSInner napi_call_function result[%{public}zu]", result);
202             delete callbackInfo;
203             callbackInfo = nullptr;
204             delete work;
205             work = nullptr;
206         },
207         uv_qos_default);
208     return ret;
209 }
210 
NotifyStringVectorChanged(uv_work_t * work)211 int NAccessibilityConfigObserver::NotifyStringVectorChanged(uv_work_t *work)
212 {
213     uv_loop_s *loop = nullptr;
214     napi_get_uv_event_loop(env_, &loop);
215     int ret = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {},
216         [](uv_work_t *work, int status) {
217             StateCallbackInfo *callbackInfo = static_cast<StateCallbackInfo*>(work->data);
218             auto closeScope = [callbackInfo](napi_handle_scope scope) {
219                 napi_close_handle_scope(callbackInfo->env_, scope);
220             };
221             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
222                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
223             napi_value jsEvent;
224             napi_create_array(callbackInfo->env_, &jsEvent);
225             ConvertStringVecToJS(callbackInfo->env_, jsEvent, callbackInfo->stringVector_);
226 
227             napi_value handler = nullptr;
228             napi_value callResult = nullptr;
229             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
230             napi_value undefined = nullptr;
231             napi_get_undefined(callbackInfo->env_, &undefined);
232             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
233             size_t result;
234             const uint32_t BUF_SIZE = 1024;
235             char buf[BUF_SIZE] = {0};
236             napi_get_value_string_utf8(callbackInfo->env_, callResult, buf, BUF_SIZE, &result);
237             HILOG_DEBUG("NotifyStringVectorChanged napi_call_function result[%{public}zu]", result);
238             delete callbackInfo;
239             callbackInfo = nullptr;
240             delete work;
241             work = nullptr;
242         },
243         uv_qos_default);
244     return ret;
245 }
246 
NotifyIntChanged(uv_work_t * work)247 int NAccessibilityConfigObserver::NotifyIntChanged(uv_work_t *work)
248 {
249     uv_loop_s *loop = nullptr;
250     napi_get_uv_event_loop(env_, &loop);
251     int ret = uv_queue_work_with_qos(
252         loop,
253         work,
254         [](uv_work_t *work) {},
255         [](uv_work_t *work, int status) {
256             StateCallbackInfo *callbackInfo = static_cast<StateCallbackInfo*>(work->data);
257             auto closeScope = [callbackInfo](napi_handle_scope scope) {
258                 napi_close_handle_scope(callbackInfo->env_, scope);
259             };
260             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
261                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
262             napi_value jsEvent;
263             napi_create_int32(callbackInfo->env_, callbackInfo->int32Value_, &jsEvent);
264 
265             napi_value handler = nullptr;
266             napi_value callResult = nullptr;
267             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
268             napi_value undefined = nullptr;
269             napi_get_undefined(callbackInfo->env_, &undefined);
270             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
271             int32_t result;
272             napi_get_value_int32(callbackInfo->env_, callResult, &result);
273             HILOG_INFO("NotifyIntChanged2JSInner napi_call_function result[%{public}d]", result);
274             delete callbackInfo;
275             callbackInfo = nullptr;
276             delete work;
277             work = nullptr;
278         },
279         uv_qos_default);
280     return ret;
281 }
282 
NotifyUintChanged(uv_work_t * work)283 int NAccessibilityConfigObserver::NotifyUintChanged(uv_work_t *work)
284 {
285     uv_loop_s *loop = nullptr;
286     napi_get_uv_event_loop(env_, &loop);
287     int ret = uv_queue_work_with_qos(
288         loop,
289         work,
290         [](uv_work_t *work) {},
291         [](uv_work_t *work, int status) {
292             StateCallbackInfo *callbackInfo = static_cast<StateCallbackInfo*>(work->data);
293             auto closeScope = [callbackInfo](napi_handle_scope scope) {
294                 napi_close_handle_scope(callbackInfo->env_, scope);
295             };
296             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
297                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
298             napi_value jsEvent;
299             napi_create_uint32(callbackInfo->env_, callbackInfo->uint32Value_, &jsEvent);
300 
301             napi_value handler = nullptr;
302             napi_value callResult = nullptr;
303             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
304             napi_value undefined = nullptr;
305             napi_get_undefined(callbackInfo->env_, &undefined);
306             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
307             uint32_t result;
308             napi_get_value_uint32(callbackInfo->env_, callResult, &result);
309             HILOG_INFO("NotifyUintChanged2JSInner napi_call_function result[%{public}d]", result);
310             delete callbackInfo;
311             callbackInfo = nullptr;
312             delete work;
313             work = nullptr;
314         },
315         uv_qos_default);
316     return ret;
317 }
318 
NotifyFloatChanged(uv_work_t * work)319 int NAccessibilityConfigObserver::NotifyFloatChanged(uv_work_t *work)
320 {
321     uv_loop_s *loop = nullptr;
322     napi_get_uv_event_loop(env_, &loop);
323     int ret = uv_queue_work_with_qos(
324         loop,
325         work,
326         [](uv_work_t *work) {},
327         [](uv_work_t *work, int status) {
328             StateCallbackInfo *callbackInfo = static_cast<StateCallbackInfo*>(work->data);
329             auto closeScope = [callbackInfo](napi_handle_scope scope) {
330                 napi_close_handle_scope(callbackInfo->env_, scope);
331             };
332             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
333                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
334             napi_value jsEvent;
335             napi_create_double(callbackInfo->env_, double(callbackInfo->floatValue_), &jsEvent);
336 
337             napi_value handler = nullptr;
338             napi_value callResult = nullptr;
339             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
340             napi_value undefined = nullptr;
341             napi_get_undefined(callbackInfo->env_, &undefined);
342             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
343             int32_t result;
344             napi_get_value_int32(callbackInfo->env_, callResult, &result);
345             HILOG_INFO("NotifyFloatChanged2JSInner napi_call_function result[%{public}d]", result);
346             delete callbackInfo;
347             callbackInfo = nullptr;
348             delete work;
349             work = nullptr;
350         },
351         uv_qos_default);
352     return ret;
353 }
354 
NotifyStateChanged2JS(bool enabled)355 void NAccessibilityConfigObserver::NotifyStateChanged2JS(bool enabled)
356 {
357     HILOG_INFO("id = [%{public}d] enabled = [%{public}s]", static_cast<int32_t>(configId_), enabled ? "true" : "false");
358 
359     StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo();
360     if (!callbackInfo) {
361         HILOG_ERROR("Failed to create callbackInfo.");
362         return;
363     }
364     callbackInfo->state_ = enabled;
365     callbackInfo->env_ = env_;
366     callbackInfo->ref_ = handlerRef_;
367     uv_work_t *work = new(std::nothrow) uv_work_t;
368     if (!work) {
369         HILOG_ERROR("NotifyStateChanged2JS Failed to create work.");
370         delete callbackInfo;
371         callbackInfo = nullptr;
372         return;
373     }
374     work->data = static_cast<void*>(callbackInfo);
375     int ret = NotifyStateChanged(work);
376     if (ret != 0) {
377         HILOG_ERROR("Failed to execute NotifyStateChanged2JS work queue");
378         delete callbackInfo;
379         callbackInfo = nullptr;
380         delete work;
381         work = nullptr;
382     }
383 }
384 
NotifyPropertyChanged2JS(const OHOS::AccessibilityConfig::CaptionProperty & caption)385 void NAccessibilityConfigObserver::NotifyPropertyChanged2JS(const OHOS::AccessibilityConfig::CaptionProperty &caption)
386 {
387     HILOG_INFO("id = [%{public}d]", static_cast<int32_t>(configId_));
388 
389     CaptionCallbackInfo *callbackInfo = new(std::nothrow) CaptionCallbackInfo();
390     if (!callbackInfo) {
391         HILOG_ERROR("Failed to create callbackInfo.");
392         return;
393     }
394     callbackInfo->caption_ = caption;
395     callbackInfo->env_ = env_;
396     callbackInfo->ref_ = handlerRef_;
397     uv_work_t *work = new(std::nothrow) uv_work_t;
398     if (!work) {
399         HILOG_ERROR("NotifyPropertyChanged2JS Failed to create work.");
400         delete callbackInfo;
401         callbackInfo = nullptr;
402         return;
403     }
404     work->data = static_cast<void*>(callbackInfo);
405     int ret = NotifyPropertyChanged(work);
406     if (ret != 0) {
407         HILOG_ERROR("Failed to execute NotifyPropertyChanged2JS work queue");
408         delete callbackInfo;
409         callbackInfo = nullptr;
410         delete work;
411         work = nullptr;
412     }
413 }
414 
NotifyStringChanged2JS(const std::string & value)415 void NAccessibilityConfigObserver::NotifyStringChanged2JS(const std::string& value)
416 {
417     HILOG_INFO("value = [%{public}s]", value.c_str());
418 
419     StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo();
420     if (!callbackInfo) {
421         HILOG_ERROR("Failed to create callbackInfo.");
422         return;
423     }
424     callbackInfo->stringValue_ = value;
425     callbackInfo->env_ = env_;
426     callbackInfo->ref_ = handlerRef_;
427     uv_work_t *work = new(std::nothrow) uv_work_t;
428     if (!work) {
429         HILOG_ERROR("NotifyStringChanged2JS Failed to create work.");
430         delete callbackInfo;
431         callbackInfo = nullptr;
432         return;
433     }
434     work->data = static_cast<void*>(callbackInfo);
435     int ret = NotifyStringChanged(work);
436     if (ret != 0) {
437         HILOG_ERROR("Failed to execute NotifyStringChanged2JS work queue");
438         delete callbackInfo;
439         callbackInfo = nullptr;
440         delete work;
441         work = nullptr;
442     }
443 }
444 
NotifyStringVectorChanged2JS(std::vector<std::string> value)445 void NAccessibilityConfigObserver::NotifyStringVectorChanged2JS(std::vector<std::string> value)
446 {
447     HILOG_DEBUG();
448 
449     StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo();
450     if (!callbackInfo) {
451         HILOG_ERROR("Failed to create callbackInfo.");
452         return;
453     }
454     callbackInfo->stringVector_ = value;
455     callbackInfo->env_ = env_;
456     callbackInfo->ref_ = handlerRef_;
457     uv_work_t *work = new(std::nothrow) uv_work_t;
458     if (!work) {
459         HILOG_ERROR("NotifyStringVectorChanged2JS Failed to create work.");
460         delete callbackInfo;
461         callbackInfo = nullptr;
462         return;
463     }
464     work->data = static_cast<void*>(callbackInfo);
465     int ret = NotifyStringVectorChanged(work);
466     if (ret != 0) {
467         HILOG_ERROR("Failed to execute NotifyStringVectorChanged2JS work queue");
468         delete callbackInfo;
469         callbackInfo = nullptr;
470         delete work;
471         work = nullptr;
472     }
473 }
474 
NotifyIntChanged2JS(int32_t value)475 void NAccessibilityConfigObserver::NotifyIntChanged2JS(int32_t value)
476 {
477     HILOG_INFO("id = [%{public}d] value = [%{public}d]", static_cast<int32_t>(configId_), value);
478 
479     StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo();
480     if (!callbackInfo) {
481         HILOG_ERROR("Failed to create callbackInfo.");
482         return;
483     }
484     callbackInfo->int32Value_ = value;
485     callbackInfo->env_ = env_;
486     callbackInfo->ref_ = handlerRef_;
487     uv_work_t *work = new(std::nothrow) uv_work_t;
488     if (!work) {
489         HILOG_ERROR("NotifyIntChanged2JS Failed to create work.");
490         delete callbackInfo;
491         callbackInfo = nullptr;
492         return;
493     }
494     work->data = static_cast<void*>(callbackInfo);
495     int ret = NotifyIntChanged(work);
496     if (ret != 0) {
497         HILOG_ERROR("Failed to execute NotifyIntChanged2JS work queue");
498         delete callbackInfo;
499         callbackInfo = nullptr;
500         delete work;
501         work = nullptr;
502     }
503 }
504 
NotifyUintChanged2JS(uint32_t value)505 void NAccessibilityConfigObserver::NotifyUintChanged2JS(uint32_t value)
506 {
507     HILOG_INFO("id = [%{public}d] value = [%{public}u]", static_cast<int32_t>(configId_), value);
508 
509     StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo();
510     if (!callbackInfo) {
511         HILOG_ERROR("Failed to create callbackInfo.");
512         return;
513     }
514     callbackInfo->uint32Value_ = value;
515     callbackInfo->env_ = env_;
516     callbackInfo->ref_ = handlerRef_;
517     uv_work_t *work = new(std::nothrow) uv_work_t;
518     if (!work) {
519         HILOG_ERROR("NotifyUintChanged2JS Failed to create work.");
520         delete callbackInfo;
521         callbackInfo = nullptr;
522         return;
523     }
524     work->data = static_cast<void*>(callbackInfo);
525     int ret = NotifyUintChanged(work);
526     if (ret != 0) {
527         HILOG_ERROR("Failed to execute NotifyUintChanged2JS work queue");
528         delete callbackInfo;
529         callbackInfo = nullptr;
530         delete work;
531         work = nullptr;
532     }
533 }
534 
NotifyFloatChanged2JS(float value)535 void NAccessibilityConfigObserver::NotifyFloatChanged2JS(float value)
536 {
537     HILOG_INFO("id = [%{public}d] value = [%{public}f]", static_cast<int32_t>(configId_), value);
538 
539     StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo();
540     if (!callbackInfo) {
541         HILOG_ERROR("Failed to create callbackInfo.");
542         return;
543     }
544     callbackInfo->floatValue_ = value;
545     callbackInfo->env_ = env_;
546     callbackInfo->ref_ = handlerRef_;
547     uv_work_t *work = new(std::nothrow) uv_work_t;
548     if (!work) {
549         HILOG_ERROR("NotifyFloatChanged2JS Failed to create work.");
550         delete callbackInfo;
551         callbackInfo = nullptr;
552         return;
553     }
554     work->data = static_cast<void*>(callbackInfo);
555     int ret = NotifyFloatChanged(work);
556     if (ret != 0) {
557         HILOG_ERROR("Failed to execute NotifyFloatChanged2JS work queue");
558         delete callbackInfo;
559         callbackInfo = nullptr;
560         delete work;
561         work = nullptr;
562     }
563 }
564 
SubscribeToFramework()565 void NAccessibilityConfigObserverImpl::SubscribeToFramework()
566 {
567     auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
568     for (int32_t index = 0; index < static_cast<int32_t>(CONFIG_ID_MAX); index ++) {
569         instance.SubscribeConfigObserver(static_cast<CONFIG_ID>(index), shared_from_this(), false);
570     }
571 }
572 
OnConfigChanged(const OHOS::AccessibilityConfig::CONFIG_ID id,const OHOS::AccessibilityConfig::ConfigValue & value)573 void NAccessibilityConfigObserverImpl::OnConfigChanged(
574     const OHOS::AccessibilityConfig::CONFIG_ID id, const OHOS::AccessibilityConfig::ConfigValue& value)
575 {
576     HILOG_INFO();
577     std::lock_guard<std::mutex> lock(mutex_);
578     for (auto &observer : observers_) {
579         if (observer && observer->configId_ == id) {
580             observer->OnConfigChanged(value);
581         }
582     }
583 }
584 
SubscribeObserver(napi_env env,OHOS::AccessibilityConfig::CONFIG_ID id,napi_value observer)585 void NAccessibilityConfigObserverImpl::SubscribeObserver(napi_env env,
586     OHOS::AccessibilityConfig::CONFIG_ID id, napi_value observer)
587 {
588     HILOG_INFO();
589     std::lock_guard<std::mutex> lock(mutex_);
590     for (auto iter = observers_.begin(); iter != observers_.end();) {
591         if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->handlerRef_)) {
592             HILOG_DEBUG("SubscribeObserver Observer exist");
593             return;
594         } else {
595             iter++;
596         }
597     }
598 
599     napi_ref handler = nullptr;
600     napi_create_reference(env, observer, 1, &handler);
601     std::shared_ptr<NAccessibilityConfigObserver> observerPtr =
602         std::make_shared<NAccessibilityConfigObserver>(env, handler, id);
603 
604     observers_.emplace_back(observerPtr);
605 }
606 
UnsubscribeObserver(napi_env env,OHOS::AccessibilityConfig::CONFIG_ID id,napi_value observer)607 void NAccessibilityConfigObserverImpl::UnsubscribeObserver(napi_env env,
608     OHOS::AccessibilityConfig::CONFIG_ID id, napi_value observer)
609 {
610     HILOG_INFO();
611     std::lock_guard<std::mutex> lock(mutex_);
612     for (auto iter = observers_.begin(); iter != observers_.end();) {
613         if ((*iter)->configId_ == id) {
614             if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->handlerRef_)) {
615                 observers_.erase(iter);
616                 return;
617             } else {
618                 iter++;
619             }
620         } else {
621             iter++;
622         }
623     }
624 }
625 
UnsubscribeObservers(OHOS::AccessibilityConfig::CONFIG_ID id)626 void NAccessibilityConfigObserverImpl::UnsubscribeObservers(OHOS::AccessibilityConfig::CONFIG_ID id)
627 {
628     HILOG_INFO();
629     std::lock_guard<std::mutex> lock(mutex_);
630     for (auto iter = observers_.begin(); iter != observers_.end();) {
631         if ((*iter)->configId_ == id) {
632             iter = observers_.erase(iter);
633         } else {
634             iter++;
635         }
636     }
637 }
638