1fc223305Sopenharmony_ci/*
2fc223305Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3fc223305Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fc223305Sopenharmony_ci * you may not use this file except in compliance with the License.
5fc223305Sopenharmony_ci * You may obtain a copy of the License at
6fc223305Sopenharmony_ci *
7fc223305Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fc223305Sopenharmony_ci *
9fc223305Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fc223305Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fc223305Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fc223305Sopenharmony_ci * See the License for the specific language governing permissions and
13fc223305Sopenharmony_ci * limitations under the License.
14fc223305Sopenharmony_ci */
15fc223305Sopenharmony_ci#ifndef PREFERENCES_IMPL_H
16fc223305Sopenharmony_ci#define PREFERENCES_IMPL_H
17fc223305Sopenharmony_ci
18fc223305Sopenharmony_ci#include <string>
19fc223305Sopenharmony_ci
20fc223305Sopenharmony_ci#include "preferences.h"
21fc223305Sopenharmony_ci#include "preferences_interface.h"
22fc223305Sopenharmony_ci#include "ffi_remote_data.h"
23fc223305Sopenharmony_ci#include "ability_context.h"
24fc223305Sopenharmony_ci#include "preferences_observer.h"
25fc223305Sopenharmony_ci
26fc223305Sopenharmony_cinamespace OHOS {
27fc223305Sopenharmony_cinamespace Preferences {
28fc223305Sopenharmony_ciusing RegisterMode = NativePreferences::PreferencesObserver::RegisterMode;
29fc223305Sopenharmony_ci
30fc223305Sopenharmony_ciclass CJPreferencesObserver : public OHOS::NativePreferences::PreferencesObserver {
31fc223305Sopenharmony_cipublic:
32fc223305Sopenharmony_ci    CJPreferencesObserver(std::function<void(std::string)> *callback,
33fc223305Sopenharmony_ci        const std::function<void(std::string)>& callbackRef);
34fc223305Sopenharmony_ci    void OnChange(const std::string &key) override;
35fc223305Sopenharmony_ci    std::function<void(std::string)> *m_callback;
36fc223305Sopenharmony_ci    std::function<void(std::string)> m_callbackRef;
37fc223305Sopenharmony_ci};
38fc223305Sopenharmony_ci
39fc223305Sopenharmony_ciclass PreferencesImpl : public OHOS::FFI::FFIData {
40fc223305Sopenharmony_cipublic:
41fc223305Sopenharmony_ci    explicit PreferencesImpl(OHOS::AbilityRuntime::Context* context,
42fc223305Sopenharmony_ci        const std::string& name, const std::string& dataGroupId, int32_t* errCode);
43fc223305Sopenharmony_ci
44fc223305Sopenharmony_ci    static int32_t DeletePreferences(OHOS::AbilityRuntime::Context* context, const std::string &name,
45fc223305Sopenharmony_ci        const std::string &dataGroupId);
46fc223305Sopenharmony_ci
47fc223305Sopenharmony_ci    static int32_t RemovePreferencesFromCache(OHOS::AbilityRuntime::Context* context, const std::string &name,
48fc223305Sopenharmony_ci        const std::string &dataGroupId);
49fc223305Sopenharmony_ci
50fc223305Sopenharmony_ci    int32_t Delete(const std::string &key);
51fc223305Sopenharmony_ci
52fc223305Sopenharmony_ci    bool HasKey(const std::string &key);
53fc223305Sopenharmony_ci
54fc223305Sopenharmony_ci    bool HasRegisteredObserver(std::function<void(std::string)> *callback, RegisterMode mode);
55fc223305Sopenharmony_ci
56fc223305Sopenharmony_ci    int32_t RegisterObserver(const std::string &mode, std::function<void(std::string)> *callback,
57fc223305Sopenharmony_ci        const std::function<void(std::string)>& callbackRef);
58fc223305Sopenharmony_ci
59fc223305Sopenharmony_ci    int32_t UnRegisterObserver(const std::string &mode, std::function<void(std::string)> *callback);
60fc223305Sopenharmony_ci
61fc223305Sopenharmony_ci    int32_t UnRegisteredAllObservers(const std::string &mode);
62fc223305Sopenharmony_ci
63fc223305Sopenharmony_ci    RegisterMode ConvertToRegisterMode(const std::string &mode);
64fc223305Sopenharmony_ci
65fc223305Sopenharmony_ci    ValueType Get(const std::string &key, const ValueType &defValue);
66fc223305Sopenharmony_ci
67fc223305Sopenharmony_ci    int32_t Put(const std::string &key, const ValueType &value);
68fc223305Sopenharmony_ci
69fc223305Sopenharmony_ci    ValueTypes GetAll();
70fc223305Sopenharmony_ci
71fc223305Sopenharmony_ci    void Flush();
72fc223305Sopenharmony_ci
73fc223305Sopenharmony_ci    void Clear();
74fc223305Sopenharmony_ci
75fc223305Sopenharmony_ci    OHOS::FFI::RuntimeType* GetRuntimeType() override;
76fc223305Sopenharmony_ci
77fc223305Sopenharmony_ciprivate:
78fc223305Sopenharmony_ci    friend class OHOS::FFI::TypeBase;
79fc223305Sopenharmony_ci    friend class OHOS::FFI::RuntimeType;
80fc223305Sopenharmony_ci    static OHOS::FFI::RuntimeType* GetClassType();
81fc223305Sopenharmony_ci    static constexpr char strChange[] = "change";
82fc223305Sopenharmony_ci    static constexpr char strMultiProcessChange[] = "multiProcessChange";
83fc223305Sopenharmony_ci    std::shared_ptr<NativePreferences::Preferences> preferences;
84fc223305Sopenharmony_ci    std::mutex listMutex_ {};
85fc223305Sopenharmony_ci    std::list<std::shared_ptr<CJPreferencesObserver>> localObservers_;
86fc223305Sopenharmony_ci    std::list<std::shared_ptr<CJPreferencesObserver>> multiProcessObservers_;
87fc223305Sopenharmony_ci};
88fc223305Sopenharmony_ci}
89fc223305Sopenharmony_ci} // namespace OHOS::Preferences
90fc223305Sopenharmony_ci
91fc223305Sopenharmony_ci#endif // PREFERENCES_IMPL_h