1 /* 2 * Copyright (c) 2024 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 #ifndef PREFERENCES_IMPL_H 16 #define PREFERENCES_IMPL_H 17 18 #include <string> 19 20 #include "preferences.h" 21 #include "preferences_interface.h" 22 #include "ffi_remote_data.h" 23 #include "ability_context.h" 24 #include "preferences_observer.h" 25 26 namespace OHOS { 27 namespace Preferences { 28 using RegisterMode = NativePreferences::PreferencesObserver::RegisterMode; 29 30 class CJPreferencesObserver : public OHOS::NativePreferences::PreferencesObserver { 31 public: 32 CJPreferencesObserver(std::function<void(std::string)> *callback, 33 const std::function<void(std::string)>& callbackRef); 34 void OnChange(const std::string &key) override; 35 std::function<void(std::string)> *m_callback; 36 std::function<void(std::string)> m_callbackRef; 37 }; 38 39 class PreferencesImpl : public OHOS::FFI::FFIData { 40 public: 41 explicit PreferencesImpl(OHOS::AbilityRuntime::Context* context, 42 const std::string& name, const std::string& dataGroupId, int32_t* errCode); 43 44 static int32_t DeletePreferences(OHOS::AbilityRuntime::Context* context, const std::string &name, 45 const std::string &dataGroupId); 46 47 static int32_t RemovePreferencesFromCache(OHOS::AbilityRuntime::Context* context, const std::string &name, 48 const std::string &dataGroupId); 49 50 int32_t Delete(const std::string &key); 51 52 bool HasKey(const std::string &key); 53 54 bool HasRegisteredObserver(std::function<void(std::string)> *callback, RegisterMode mode); 55 56 int32_t RegisterObserver(const std::string &mode, std::function<void(std::string)> *callback, 57 const std::function<void(std::string)>& callbackRef); 58 59 int32_t UnRegisterObserver(const std::string &mode, std::function<void(std::string)> *callback); 60 61 int32_t UnRegisteredAllObservers(const std::string &mode); 62 63 RegisterMode ConvertToRegisterMode(const std::string &mode); 64 65 ValueType Get(const std::string &key, const ValueType &defValue); 66 67 int32_t Put(const std::string &key, const ValueType &value); 68 69 ValueTypes GetAll(); 70 71 void Flush(); 72 73 void Clear(); 74 75 OHOS::FFI::RuntimeType* GetRuntimeType() override; 76 77 private: 78 friend class OHOS::FFI::TypeBase; 79 friend class OHOS::FFI::RuntimeType; 80 static OHOS::FFI::RuntimeType* GetClassType(); 81 static constexpr char strChange[] = "change"; 82 static constexpr char strMultiProcessChange[] = "multiProcessChange"; 83 std::shared_ptr<NativePreferences::Preferences> preferences; 84 std::mutex listMutex_ {}; 85 std::list<std::shared_ptr<CJPreferencesObserver>> localObservers_; 86 std::list<std::shared_ptr<CJPreferencesObserver>> multiProcessObservers_; 87 }; 88 } 89 } // namespace OHOS::Preferences 90 91 #endif // PREFERENCES_IMPL_h