1 /* 2 * Copyright (c) 2021 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 PREFERENCES_JSKIT_NAPI_PREFERENCE_H 17 #define PREFERENCES_JSKIT_NAPI_PREFERENCE_H 18 19 #include <assert.h> 20 21 #include "napi/native_api.h" 22 #include "napi/native_common.h" 23 #include "napi/native_node_api.h" 24 #include "preferences.h" 25 #include "preferences_helper.h" 26 27 namespace OHOS { 28 namespace PreferencesJsKit { 29 class PreferencesProxy { 30 public: 31 static void Init(napi_env env, napi_value exports); 32 static napi_value New(napi_env env, napi_callback_info info); 33 static napi_status NewInstance(napi_env env, napi_value arg, napi_value *instance); 34 static void Destructor(napi_env env, void *nativeObject, void *finalize_hint); 35 36 private: 37 explicit PreferencesProxy(std::shared_ptr<OHOS::NativePreferences::Preferences> &value); 38 ~PreferencesProxy(); 39 40 static napi_value GetValue(napi_env env, napi_callback_info info); 41 static napi_value SetValue(napi_env env, napi_callback_info info); 42 static napi_value HasKey(napi_env env, napi_callback_info info); 43 static napi_value Delete(napi_env env, napi_callback_info info); 44 static napi_value Flush(napi_env env, napi_callback_info info); 45 static napi_value Clear(napi_env env, napi_callback_info info); 46 static napi_value RegisterObserver(napi_env env, napi_callback_info info); 47 static napi_value UnRegisterObserver(napi_env env, napi_callback_info info); 48 49 std::shared_ptr<OHOS::NativePreferences::Preferences> value_; 50 napi_env env_; 51 napi_ref wrapper_; 52 std::shared_ptr<OHOS::NativePreferences::PreferencesObserver> observer_; 53 }; 54 55 class PreferencesObserverImpl : public OHOS::NativePreferences::PreferencesObserver { 56 public: 57 PreferencesObserverImpl(napi_env env, napi_value callback); 58 virtual ~PreferencesObserverImpl(); 59 void OnChange(OHOS::NativePreferences::Preferences &preferences, const std::string &key) override; 60 61 private: 62 napi_env env_; 63 napi_ref observerRef; 64 }; 65 } // namespace PreferencesJsKit 66 } // namespace OHOS 67 68 #endif // PREFERENCES_JSKIT_NAPI_PREFERENCE_H 69