• 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     if (loop == nullptr || work == nullptr) {
113         HILOG_ERROR("loop or work is nullptr.");
114         return RET_ERR_FAILED;
115     }
116     int ret = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {},
117         [](uv_work_t *work, int status) {
118             StateCallbackInfo *callbackInfo = static_cast<StateCallbackInfo*>(work->data);
119             napi_env env = callbackInfo->env_;
120             auto closeScope = [env](napi_handle_scope scope) {
121                 napi_close_handle_scope(env, scope);
122             };
123             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
124                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
125             napi_value jsEvent = nullptr;
126             napi_create_object(callbackInfo->env_, &jsEvent);
127             if (jsEvent == nullptr) {
128                 HILOG_ERROR("napi_create_object fail.");
129                 return;
130             }
131             napi_get_boolean(callbackInfo->env_, callbackInfo->state_, &jsEvent);
132 
133             napi_value handler = nullptr;
134             napi_value callResult = nullptr;
135             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
136             napi_value undefined = nullptr;
137             napi_get_undefined(callbackInfo->env_, &undefined);
138             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
139             int32_t result;
140             napi_get_value_int32(callbackInfo->env_, callResult, &result);
141             HILOG_INFO("NotifyStateChangedJS napi_call_function result[%{public}d]", result);
142             delete callbackInfo;
143             callbackInfo = nullptr;
144             delete work;
145             work = nullptr;
146         },
147         uv_qos_default);
148     return ret;
149 }
150 
NotifyPropertyChanged(uv_work_t * work)151 int NAccessibilityConfigObserver::NotifyPropertyChanged(uv_work_t *work)
152 {
153     uv_loop_s *loop = nullptr;
154     napi_get_uv_event_loop(env_, &loop);
155     if (loop == nullptr || work == nullptr) {
156         HILOG_ERROR("loop or work is nullptr.");
157         return RET_ERR_FAILED;
158     }
159     int ret = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {},
160         [](uv_work_t *work, int status) {
161             CaptionCallbackInfo *callbackInfo = static_cast<CaptionCallbackInfo*>(work->data);
162             napi_env env = callbackInfo->env_;
163             auto closeScope = [env](napi_handle_scope scope) {
164                 napi_close_handle_scope(env, scope);
165             };
166             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
167                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
168             napi_value jsEvent = nullptr;
169             napi_create_object(callbackInfo->env_, &jsEvent);
170             if (jsEvent == nullptr) {
171                 HILOG_ERROR("napi_create_object fail.");
172                 return;
173             }
174             ConvertCaptionPropertyToJS(callbackInfo->env_, jsEvent, callbackInfo->caption_);
175 
176             napi_value handler = nullptr;
177             napi_value callResult = nullptr;
178             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
179             napi_value undefined = nullptr;
180             napi_get_undefined(callbackInfo->env_, &undefined);
181             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
182             int32_t result;
183             napi_get_value_int32(callbackInfo->env_, callResult, &result);
184             HILOG_INFO("NotifyPropertyChangedJS napi_call_function result[%{public}d]", result);
185             delete callbackInfo;
186             callbackInfo = nullptr;
187             delete work;
188             work = nullptr;
189         },
190         uv_qos_default);
191     return ret;
192 }
193 
NotifyStringChanged(uv_work_t * work)194 int NAccessibilityConfigObserver::NotifyStringChanged(uv_work_t *work)
195 {
196     uv_loop_s *loop = nullptr;
197     napi_get_uv_event_loop(env_, &loop);
198     if (loop == nullptr || work == nullptr) {
199         HILOG_ERROR("loop or work is nullptr.");
200         return RET_ERR_FAILED;
201     }
202     int ret = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {},
203         [](uv_work_t *work, int status) {
204             StateCallbackInfo *callbackInfo = static_cast<StateCallbackInfo*>(work->data);
205             napi_env env = callbackInfo->env_;
206             auto closeScope = [env](napi_handle_scope scope) {
207                 napi_close_handle_scope(env, scope);
208             };
209             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
210                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
211             napi_value jsEvent = nullptr;
212             napi_create_string_utf8(callbackInfo->env_, callbackInfo->stringValue_.c_str(),
213                 callbackInfo->stringValue_.length(), &jsEvent);
214             napi_value handler = nullptr;
215             napi_value callResult = nullptr;
216             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
217             napi_value undefined = nullptr;
218             napi_get_undefined(callbackInfo->env_, &undefined);
219             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
220             size_t result;
221             const uint32_t BUF_SIZE = 1024;
222             char buf[BUF_SIZE] = {0};
223             napi_get_value_string_utf8(callbackInfo->env_, callResult, buf, BUF_SIZE, &result);
224             HILOG_INFO("NotifyStringChanged2JSInner napi_call_function result[%{public}zu]", result);
225             delete callbackInfo;
226             callbackInfo = nullptr;
227             delete work;
228             work = nullptr;
229         },
230         uv_qos_default);
231     return ret;
232 }
233 
NotifyStringVectorChanged(uv_work_t * work)234 int NAccessibilityConfigObserver::NotifyStringVectorChanged(uv_work_t *work)
235 {
236     uv_loop_s *loop = nullptr;
237     napi_get_uv_event_loop(env_, &loop);
238     if (loop == nullptr || work == nullptr) {
239         HILOG_ERROR("loop or work is nullptr.");
240         return RET_ERR_FAILED;
241     }
242     int ret = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {},
243         [](uv_work_t *work, int status) {
244             StateCallbackInfo *callbackInfo = static_cast<StateCallbackInfo*>(work->data);
245             napi_env env = callbackInfo->env_;
246             auto closeScope = [env](napi_handle_scope scope) {
247                 napi_close_handle_scope(env, scope);
248             };
249             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
250                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
251             napi_value jsEvent = nullptr;
252             napi_create_array(callbackInfo->env_, &jsEvent);
253             ConvertStringVecToJS(callbackInfo->env_, jsEvent, callbackInfo->stringVector_);
254 
255             napi_value handler = nullptr;
256             napi_value callResult = nullptr;
257             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
258             napi_value undefined = nullptr;
259             napi_get_undefined(callbackInfo->env_, &undefined);
260             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
261             size_t result;
262             const uint32_t BUF_SIZE = 1024;
263             char buf[BUF_SIZE] = {0};
264             napi_get_value_string_utf8(callbackInfo->env_, callResult, buf, BUF_SIZE, &result);
265             HILOG_DEBUG("NotifyStringVectorChanged napi_call_function result[%{public}zu]", result);
266             delete callbackInfo;
267             callbackInfo = nullptr;
268             delete work;
269             work = nullptr;
270         },
271         uv_qos_default);
272     return ret;
273 }
274 
NotifyIntChanged(uv_work_t * work)275 int NAccessibilityConfigObserver::NotifyIntChanged(uv_work_t *work)
276 {
277     uv_loop_s *loop = nullptr;
278     napi_get_uv_event_loop(env_, &loop);
279     if (loop == nullptr || work == nullptr) {
280         HILOG_ERROR("loop or work is nullptr.");
281         return RET_ERR_FAILED;
282     }
283     int ret = uv_queue_work_with_qos(
284         loop,
285         work,
286         [](uv_work_t *work) {},
287         [](uv_work_t *work, int status) {
288             StateCallbackInfo *callbackInfo = static_cast<StateCallbackInfo*>(work->data);
289             napi_env env = callbackInfo->env_;
290             auto closeScope = [env](napi_handle_scope scope) {
291                 napi_close_handle_scope(env, scope);
292             };
293             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
294                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
295             napi_value jsEvent = nullptr;
296             napi_create_int32(callbackInfo->env_, callbackInfo->int32Value_, &jsEvent);
297 
298             napi_value handler = nullptr;
299             napi_value callResult = nullptr;
300             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
301             napi_value undefined = nullptr;
302             napi_get_undefined(callbackInfo->env_, &undefined);
303             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
304             int32_t result;
305             napi_get_value_int32(callbackInfo->env_, callResult, &result);
306             HILOG_INFO("NotifyIntChanged2JSInner napi_call_function result[%{public}d]", result);
307             delete callbackInfo;
308             callbackInfo = nullptr;
309             delete work;
310             work = nullptr;
311         },
312         uv_qos_default);
313     return ret;
314 }
315 
NotifyUintChanged(uv_work_t * work)316 int NAccessibilityConfigObserver::NotifyUintChanged(uv_work_t *work)
317 {
318     uv_loop_s *loop = nullptr;
319     napi_get_uv_event_loop(env_, &loop);
320     if (loop == nullptr || work == nullptr) {
321         HILOG_ERROR("loop or work is nullptr.");
322         return RET_ERR_FAILED;
323     }
324     int ret = uv_queue_work_with_qos(
325         loop,
326         work,
327         [](uv_work_t *work) {},
328         [](uv_work_t *work, int status) {
329             StateCallbackInfo *callbackInfo = static_cast<StateCallbackInfo*>(work->data);
330             napi_env env = callbackInfo->env_;
331             auto closeScope = [env](napi_handle_scope scope) {
332                 napi_close_handle_scope(env, scope);
333             };
334             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
335                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
336             napi_value jsEvent = nullptr;
337             napi_create_uint32(callbackInfo->env_, callbackInfo->uint32Value_, &jsEvent);
338 
339             napi_value handler = nullptr;
340             napi_value callResult = nullptr;
341             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
342             napi_value undefined = nullptr;
343             napi_get_undefined(callbackInfo->env_, &undefined);
344             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
345             uint32_t result;
346             napi_get_value_uint32(callbackInfo->env_, callResult, &result);
347             HILOG_INFO("NotifyUintChanged2JSInner napi_call_function result[%{public}d]", result);
348             delete callbackInfo;
349             callbackInfo = nullptr;
350             delete work;
351             work = nullptr;
352         },
353         uv_qos_default);
354     return ret;
355 }
356 
NotifyFloatChanged(uv_work_t * work)357 int NAccessibilityConfigObserver::NotifyFloatChanged(uv_work_t *work)
358 {
359     uv_loop_s *loop = nullptr;
360     napi_get_uv_event_loop(env_, &loop);
361     if (loop == nullptr || work == nullptr) {
362         HILOG_ERROR("loop or work is nullptr.");
363         return RET_ERR_FAILED;
364     }
365     int ret = uv_queue_work_with_qos(
366         loop,
367         work,
368         [](uv_work_t *work) {},
369         [](uv_work_t *work, int status) {
370             StateCallbackInfo *callbackInfo = static_cast<StateCallbackInfo*>(work->data);
371             napi_env env = callbackInfo->env_;
372             auto closeScope = [env](napi_handle_scope scope) {
373                 napi_close_handle_scope(env, scope);
374             };
375             std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scopes(
376                 OHOS::Accessibility::TmpOpenScope(callbackInfo->env_), closeScope);
377             napi_value jsEvent = nullptr;
378             napi_create_double(callbackInfo->env_, double(callbackInfo->floatValue_), &jsEvent);
379 
380             napi_value handler = nullptr;
381             napi_value callResult = nullptr;
382             napi_get_reference_value(callbackInfo->env_, callbackInfo->ref_, &handler);
383             napi_value undefined = nullptr;
384             napi_get_undefined(callbackInfo->env_, &undefined);
385             napi_call_function(callbackInfo->env_, undefined, handler, 1, &jsEvent, &callResult);
386             int32_t result;
387             napi_get_value_int32(callbackInfo->env_, callResult, &result);
388             HILOG_INFO("NotifyFloatChanged2JSInner napi_call_function result[%{public}d]", result);
389             delete callbackInfo;
390             callbackInfo = nullptr;
391             delete work;
392             work = nullptr;
393         },
394         uv_qos_default);
395     return ret;
396 }
397 
NotifyStateChanged2JS(bool enabled)398 void NAccessibilityConfigObserver::NotifyStateChanged2JS(bool enabled)
399 {
400     HILOG_INFO("id = [%{public}d] enabled = [%{public}s]", static_cast<int32_t>(configId_), enabled ? "true" : "false");
401 
402     StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo();
403     if (!callbackInfo) {
404         HILOG_ERROR("Failed to create callbackInfo.");
405         return;
406     }
407     callbackInfo->state_ = enabled;
408     callbackInfo->env_ = env_;
409     callbackInfo->ref_ = handlerRef_;
410     uv_work_t *work = new(std::nothrow) uv_work_t;
411     if (!work) {
412         HILOG_ERROR("NotifyStateChanged2JS Failed to create work.");
413         delete callbackInfo;
414         callbackInfo = nullptr;
415         return;
416     }
417     work->data = static_cast<void*>(callbackInfo);
418     int ret = NotifyStateChanged(work);
419     if (ret != 0) {
420         HILOG_ERROR("Failed to execute NotifyStateChanged2JS work queue");
421         delete callbackInfo;
422         callbackInfo = nullptr;
423         delete work;
424         work = nullptr;
425     }
426 }
427 
NotifyPropertyChanged2JS(const OHOS::AccessibilityConfig::CaptionProperty & caption)428 void NAccessibilityConfigObserver::NotifyPropertyChanged2JS(const OHOS::AccessibilityConfig::CaptionProperty &caption)
429 {
430     HILOG_INFO("id = [%{public}d]", static_cast<int32_t>(configId_));
431 
432     CaptionCallbackInfo *callbackInfo = new(std::nothrow) CaptionCallbackInfo();
433     if (!callbackInfo) {
434         HILOG_ERROR("Failed to create callbackInfo.");
435         return;
436     }
437     callbackInfo->caption_ = caption;
438     callbackInfo->env_ = env_;
439     callbackInfo->ref_ = handlerRef_;
440     uv_work_t *work = new(std::nothrow) uv_work_t;
441     if (!work) {
442         HILOG_ERROR("NotifyPropertyChanged2JS Failed to create work.");
443         delete callbackInfo;
444         callbackInfo = nullptr;
445         return;
446     }
447     work->data = static_cast<void*>(callbackInfo);
448     int ret = NotifyPropertyChanged(work);
449     if (ret != 0) {
450         HILOG_ERROR("Failed to execute NotifyPropertyChanged2JS work queue");
451         delete callbackInfo;
452         callbackInfo = nullptr;
453         delete work;
454         work = nullptr;
455     }
456 }
457 
NotifyStringChanged2JS(const std::string & value)458 void NAccessibilityConfigObserver::NotifyStringChanged2JS(const std::string& value)
459 {
460     HILOG_INFO("value = [%{public}s]", value.c_str());
461 
462     StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo();
463     if (!callbackInfo) {
464         HILOG_ERROR("Failed to create callbackInfo.");
465         return;
466     }
467     callbackInfo->stringValue_ = value;
468     callbackInfo->env_ = env_;
469     callbackInfo->ref_ = handlerRef_;
470     uv_work_t *work = new(std::nothrow) uv_work_t;
471     if (!work) {
472         HILOG_ERROR("NotifyStringChanged2JS Failed to create work.");
473         delete callbackInfo;
474         callbackInfo = nullptr;
475         return;
476     }
477     work->data = static_cast<void*>(callbackInfo);
478     int ret = NotifyStringChanged(work);
479     if (ret != 0) {
480         HILOG_ERROR("Failed to execute NotifyStringChanged2JS work queue");
481         delete callbackInfo;
482         callbackInfo = nullptr;
483         delete work;
484         work = nullptr;
485     }
486 }
487 
NotifyStringVectorChanged2JS(std::vector<std::string> value)488 void NAccessibilityConfigObserver::NotifyStringVectorChanged2JS(std::vector<std::string> value)
489 {
490     HILOG_DEBUG();
491 
492     StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo();
493     if (!callbackInfo) {
494         HILOG_ERROR("Failed to create callbackInfo.");
495         return;
496     }
497     callbackInfo->stringVector_ = value;
498     callbackInfo->env_ = env_;
499     callbackInfo->ref_ = handlerRef_;
500     uv_work_t *work = new(std::nothrow) uv_work_t;
501     if (!work) {
502         HILOG_ERROR("NotifyStringVectorChanged2JS Failed to create work.");
503         delete callbackInfo;
504         callbackInfo = nullptr;
505         return;
506     }
507     work->data = static_cast<void*>(callbackInfo);
508     int ret = NotifyStringVectorChanged(work);
509     if (ret != 0) {
510         HILOG_ERROR("Failed to execute NotifyStringVectorChanged2JS work queue");
511         delete callbackInfo;
512         callbackInfo = nullptr;
513         delete work;
514         work = nullptr;
515     }
516 }
517 
NotifyIntChanged2JS(int32_t value)518 void NAccessibilityConfigObserver::NotifyIntChanged2JS(int32_t value)
519 {
520     HILOG_INFO("id = [%{public}d] value = [%{public}d]", static_cast<int32_t>(configId_), value);
521 
522     StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo();
523     if (!callbackInfo) {
524         HILOG_ERROR("Failed to create callbackInfo.");
525         return;
526     }
527     callbackInfo->int32Value_ = value;
528     callbackInfo->env_ = env_;
529     callbackInfo->ref_ = handlerRef_;
530     uv_work_t *work = new(std::nothrow) uv_work_t;
531     if (!work) {
532         HILOG_ERROR("NotifyIntChanged2JS Failed to create work.");
533         delete callbackInfo;
534         callbackInfo = nullptr;
535         return;
536     }
537     work->data = static_cast<void*>(callbackInfo);
538     int ret = NotifyIntChanged(work);
539     if (ret != 0) {
540         HILOG_ERROR("Failed to execute NotifyIntChanged2JS work queue");
541         delete callbackInfo;
542         callbackInfo = nullptr;
543         delete work;
544         work = nullptr;
545     }
546 }
547 
NotifyUintChanged2JS(uint32_t value)548 void NAccessibilityConfigObserver::NotifyUintChanged2JS(uint32_t value)
549 {
550     HILOG_INFO("id = [%{public}d] value = [%{public}u]", static_cast<int32_t>(configId_), value);
551 
552     StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo();
553     if (!callbackInfo) {
554         HILOG_ERROR("Failed to create callbackInfo.");
555         return;
556     }
557     callbackInfo->uint32Value_ = value;
558     callbackInfo->env_ = env_;
559     callbackInfo->ref_ = handlerRef_;
560     uv_work_t *work = new(std::nothrow) uv_work_t;
561     if (!work) {
562         HILOG_ERROR("NotifyUintChanged2JS Failed to create work.");
563         delete callbackInfo;
564         callbackInfo = nullptr;
565         return;
566     }
567     work->data = static_cast<void*>(callbackInfo);
568     int ret = NotifyUintChanged(work);
569     if (ret != 0) {
570         HILOG_ERROR("Failed to execute NotifyUintChanged2JS work queue");
571         delete callbackInfo;
572         callbackInfo = nullptr;
573         delete work;
574         work = nullptr;
575     }
576 }
577 
NotifyFloatChanged2JS(float value)578 void NAccessibilityConfigObserver::NotifyFloatChanged2JS(float value)
579 {
580     HILOG_INFO("id = [%{public}d] value = [%{public}f]", static_cast<int32_t>(configId_), value);
581 
582     StateCallbackInfo *callbackInfo = new(std::nothrow) StateCallbackInfo();
583     if (!callbackInfo) {
584         HILOG_ERROR("Failed to create callbackInfo.");
585         return;
586     }
587     callbackInfo->floatValue_ = value;
588     callbackInfo->env_ = env_;
589     callbackInfo->ref_ = handlerRef_;
590     uv_work_t *work = new(std::nothrow) uv_work_t;
591     if (!work) {
592         HILOG_ERROR("NotifyFloatChanged2JS Failed to create work.");
593         delete callbackInfo;
594         callbackInfo = nullptr;
595         return;
596     }
597     work->data = static_cast<void*>(callbackInfo);
598     int ret = NotifyFloatChanged(work);
599     if (ret != 0) {
600         HILOG_ERROR("Failed to execute NotifyFloatChanged2JS work queue");
601         delete callbackInfo;
602         callbackInfo = nullptr;
603         delete work;
604         work = nullptr;
605     }
606 }
607 
SubscribeToFramework()608 void NAccessibilityConfigObserverImpl::SubscribeToFramework()
609 {
610     auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
611     for (int32_t index = 0; index < static_cast<int32_t>(CONFIG_ID_MAX); index ++) {
612         instance.SubscribeConfigObserver(static_cast<CONFIG_ID>(index), shared_from_this(), false);
613     }
614 }
615 
OnConfigChanged(const OHOS::AccessibilityConfig::CONFIG_ID id,const OHOS::AccessibilityConfig::ConfigValue & value)616 void NAccessibilityConfigObserverImpl::OnConfigChanged(
617     const OHOS::AccessibilityConfig::CONFIG_ID id, const OHOS::AccessibilityConfig::ConfigValue& value)
618 {
619     HILOG_INFO();
620     std::lock_guard<std::mutex> lock(mutex_);
621     for (auto &observer : observers_) {
622         if (observer && observer->configId_ == id) {
623             observer->OnConfigChanged(value);
624         }
625     }
626 }
627 
SubscribeObserver(napi_env env,OHOS::AccessibilityConfig::CONFIG_ID id,napi_value observer)628 void NAccessibilityConfigObserverImpl::SubscribeObserver(napi_env env,
629     OHOS::AccessibilityConfig::CONFIG_ID id, napi_value observer)
630 {
631     HILOG_INFO();
632     std::lock_guard<std::mutex> lock(mutex_);
633     for (auto iter = observers_.begin(); iter != observers_.end();) {
634         if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->handlerRef_)) {
635             HILOG_DEBUG("SubscribeObserver Observer exist");
636             return;
637         } else {
638             iter++;
639         }
640     }
641 
642     napi_ref handler = nullptr;
643     napi_create_reference(env, observer, 1, &handler);
644     std::shared_ptr<NAccessibilityConfigObserver> observerPtr =
645         std::make_shared<NAccessibilityConfigObserver>(env, handler, id);
646 
647     observers_.emplace_back(observerPtr);
648 }
649 
UnsubscribeObserver(napi_env env,OHOS::AccessibilityConfig::CONFIG_ID id,napi_value observer)650 void NAccessibilityConfigObserverImpl::UnsubscribeObserver(napi_env env,
651     OHOS::AccessibilityConfig::CONFIG_ID id, napi_value observer)
652 {
653     HILOG_INFO();
654     std::lock_guard<std::mutex> lock(mutex_);
655     for (auto iter = observers_.begin(); iter != observers_.end();) {
656         if ((*iter)->configId_ == id) {
657             if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->handlerRef_)) {
658                 observers_.erase(iter);
659                 return;
660             } else {
661                 iter++;
662             }
663         } else {
664             iter++;
665         }
666     }
667 }
668 
UnsubscribeObservers(OHOS::AccessibilityConfig::CONFIG_ID id)669 void NAccessibilityConfigObserverImpl::UnsubscribeObservers(OHOS::AccessibilityConfig::CONFIG_ID id)
670 {
671     HILOG_INFO();
672     std::lock_guard<std::mutex> lock(mutex_);
673     for (auto iter = observers_.begin(); iter != observers_.end();) {
674         if ((*iter)->configId_ == id) {
675             iter = observers_.erase(iter);
676         } else {
677             iter++;
678         }
679     }
680 }
681