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