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 #include <set> 23 24 #include "js_observer.h" 25 #include "js_proxy.h" 26 #include "js_common_utils.h" 27 #include "napi/native_api.h" 28 #include "napi/native_common.h" 29 #include "napi/native_node_api.h" 30 #include "napi_preferences_observer.h" 31 #include "preferences.h" 32 #include "preferences_helper.h" 33 34 namespace OHOS::Sendable::JSPreferences { 35 using RegisterMode = NativePreferences::PreferencesObserver::RegisterMode; 36 using JSPreferencesObserver = PreferencesJsKit::JSPreferencesObserver; 37 using UvQueue = PreferencesJsKit::UvQueue; 38 using Preferences = NativePreferences::Preferences; 39 template<typename T> 40 using JSProxy = OHOS::JSProxy::JSProxy<T>; 41 class PreferencesProxy : public JSProxy<Preferences> { 42 public: 43 static void Init(napi_env env, napi_value exports); 44 static napi_value New(napi_env env, napi_callback_info info); 45 static napi_status NewInstance(napi_env env, std::shared_ptr<Preferences> value, napi_value *instance); 46 static void Destructor(napi_env env, void *nativeObject, void *finalize_hint); 47 48 private: 49 static constexpr char STR_CHANGE[] = "change"; 50 static constexpr char STR_MULTI_PRECESS_CHANGE[] = "multiProcessChange"; 51 static constexpr const char *STR_DATA_CHANGE = "dataChange"; 52 using Observer = NativePreferences::PreferencesObserver; 53 using JSObserverImpl = JSPreferencesObserver; 54 explicit PreferencesProxy(); 55 ~PreferencesProxy(); 56 57 static napi_value GetValue(napi_env env, napi_callback_info info); 58 static napi_value SetValue(napi_env env, napi_callback_info info); 59 static napi_value HasKey(napi_env env, napi_callback_info info); 60 static napi_value Delete(napi_env env, napi_callback_info info); 61 static napi_value Flush(napi_env env, napi_callback_info info); 62 static napi_value Clear(napi_env env, napi_callback_info info); 63 static napi_value RegisterObserver(napi_env env, napi_callback_info info); 64 static napi_value UnregisterObserver(napi_env env, napi_callback_info info); 65 static napi_value GetAll(napi_env env, napi_callback_info info); 66 67 static std::pair<PreferencesProxy *, std::weak_ptr<Preferences>> GetSelfInstance(napi_env env, napi_value self); 68 69 static RegisterMode ConvertToRegisterMode(const std::string &mode); 70 71 int RegisteredObserver(napi_env env, napi_value callback, RegisterMode mode, const std::vector<std::string> &keys); 72 int UnregisteredObserver( 73 napi_env env, napi_value callback, RegisterMode mode, const std::vector<std::string> &keys); 74 75 class JSObservers { 76 public: 77 JSObservers(napi_env env, PreferencesProxy *proxy); 78 ~JSObservers(); 79 int Subscribe(napi_value callback, RegisterMode mode, const std::vector<std::string> &keys); 80 int Unsubscribe(napi_value callback, RegisterMode mode, const std::vector<std::string> &keys); 81 private: 82 static void CleanEnv(void *obj); 83 84 std::shared_ptr<UvQueue> uvQueue_; 85 std::list<std::shared_ptr<JSObserverImpl>> observers_[Observer::CHANGE_BUTT]; 86 napi_env env_ = nullptr; 87 PreferencesProxy *proxy_ = nullptr; 88 }; 89 std::mutex mutex_{}; 90 std::map<napi_env, JSObservers> observers_; 91 }; 92 } // namespace OHOS::Sendable::Preferences 93 #endif // PREFERENCES_JSKIT_NAPI_PREFERENCE_H 94