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