• 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 #ifndef ACCESSIBILITY_CONFIG_OBSERVER_H
17 #define ACCESSIBILITY_CONFIG_OBSERVER_H
18 
19 #include <map>
20 #include <string>
21 #include <uv.h>
22 #include "accessibility_config.h"
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 
26 namespace OHOS {
27 namespace Accessibility {
28 napi_handle_scope TmpOpenScope(napi_env env);
29 } // namespace Accessibility
30 } // namespace OHOS
31 
32 const uint32_t CONFIG_START_WORK_ARGS_SIZE = 2;
33 
34 struct NAccessibilityConfigObserver {
35 public:
NAccessibilityConfigObserverNAccessibilityConfigObserver36     NAccessibilityConfigObserver(napi_env env, napi_ref callback, OHOS::AccessibilityConfig::CONFIG_ID id)
37         : env_(env), handlerRef_(callback), configId_(id) {};
38     void OnConfigChanged(const OHOS::AccessibilityConfig::ConfigValue& value);
39     void OnConfigChangedExtra(const OHOS::AccessibilityConfig::ConfigValue& value);
40 
41     void OnDaltonizationColorFilterConfigChanged();
42     void NotifyStateChanged2JS(bool enabled);
43     int NotifyStateChanged(uv_work_t *work);
44     void NotifyPropertyChanged2JS(const OHOS::AccessibilityConfig::CaptionProperty &caption);
45     int NotifyPropertyChanged(uv_work_t *work);
46     void NotifyStringChanged2JS(const std::string& value);
47     void NotifyStringVectorChanged2JS(std::vector<std::string> value);
48     int NotifyStringChanged(uv_work_t *work);
49     int NotifyStringVectorChanged(uv_work_t *work);
50     void NotifyIntChanged2JS(int32_t value);
51     int NotifyIntChanged(uv_work_t *work);
52     void NotifyUintChanged2JS(uint32_t value);
53     int NotifyUintChanged(uv_work_t *work);
54     void NotifyFloatChanged2JS(float value);
55     int NotifyFloatChanged(uv_work_t *work);
56 
57     napi_env env_ = nullptr;
58     napi_ref handlerRef_ = nullptr;
59     OHOS::AccessibilityConfig::CONFIG_ID configId_ = OHOS::AccessibilityConfig::CONFIG_ID::CONFIG_ID_MAX;
60 };
61 
62 class NAccessibilityConfigObserverImpl : public OHOS::AccessibilityConfig::AccessibilityConfigObserver,
63     public std::enable_shared_from_this<NAccessibilityConfigObserverImpl> {
64 public:
65     NAccessibilityConfigObserverImpl() = default;
66     virtual void OnConfigChanged(
67         const OHOS::AccessibilityConfig::CONFIG_ID id, const OHOS::AccessibilityConfig::ConfigValue& value) override;
68     void SubscribeToFramework();
69     void SubscribeObserver(napi_env env, OHOS::AccessibilityConfig::CONFIG_ID id, napi_value observer);
70     void UnsubscribeObserver(napi_env env, OHOS::AccessibilityConfig::CONFIG_ID id, napi_value observer);
71     void UnsubscribeObservers(OHOS::AccessibilityConfig::CONFIG_ID id);
72 
73 private:
74     std::mutex mutex_;
75     std::vector<std::shared_ptr<NAccessibilityConfigObserver>> observers_ = {};
76 };
77 #endif // ACCESSIBILITY_CONFIG_OBSERVER_H