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 <list> 22 23 #include "js_observer.h" 24 #include "napi/native_api.h" 25 #include "napi/native_common.h" 26 #include "napi/native_node_api.h" 27 #include "napi_preferences_observer.h" 28 #include "preferences.h" 29 #include "preferences_helper.h" 30 31 namespace OHOS { 32 namespace PreferencesJsKit { 33 class PreferencesProxy { 34 public: 35 static void Init(napi_env env, napi_value exports); 36 static napi_value New(napi_env env, napi_callback_info info); 37 static napi_status NewInstance( 38 napi_env env, std::shared_ptr<OHOS::NativePreferences::Preferences> value, napi_value *instance); 39 static void Destructor(napi_env env, void *nativeObject, void *finalize_hint); 40 41 private: 42 explicit PreferencesProxy(); 43 ~PreferencesProxy(); 44 45 static napi_value GetValue(napi_env env, napi_callback_info info); 46 static napi_value SetValue(napi_env env, napi_callback_info info); 47 static napi_value HasKey(napi_env env, napi_callback_info info); 48 static napi_value Delete(napi_env env, napi_callback_info info); 49 static napi_value Flush(napi_env env, napi_callback_info info); 50 static napi_value Clear(napi_env env, napi_callback_info info); 51 static napi_value RegisterObserver(napi_env env, napi_callback_info info); 52 static napi_value UnRegisterObserver(napi_env env, napi_callback_info info); 53 static napi_value GetAll(napi_env env, napi_callback_info info); 54 55 bool HasRegisteredObserver(napi_value callback); 56 void RegisteredObserver(napi_value callback); 57 void UnRegisteredObserver(napi_value callback); 58 59 std::shared_ptr<OHOS::NativePreferences::Preferences> value_; 60 napi_env env_; 61 62 std::mutex listMutex_ {}; 63 std::list<std::shared_ptr<JSPreferencesObserver>> dataObserver_; 64 std::shared_ptr<UvQueue> uvQueue_; 65 }; 66 } // namespace PreferencesJsKit 67 } // namespace OHOS 68 69 #endif // PREFERENCES_JSKIT_NAPI_PREFERENCE_H 70