• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef PREFERENCES_BASE_H
17 #define PREFERENCES_BASE_H
18 
19 #include <any>
20 #include <condition_variable>
21 #include <list>
22 #include <map>
23 #include <memory>
24 #include <mutex>
25 #include <set>
26 #include <string>
27 #include <vector>
28 #include <shared_mutex>
29 
30 #include "preferences.h"
31 #include "preferences_observer.h"
32 
33 namespace OHOS {
34 template <typename T> class sptr;
35 class Uri;
36 namespace NativePreferences {
37 class ExecutorPool;
38 class DataPreferencesObserverStub;
39 /**
40  * The function class of the preference. Various operations on preferences instances are provided in this class.
41  */
42 class PreferencesBase : public Preferences {
43 public:
44     PreferencesBase(const Options &options);
45     ~PreferencesBase();
46 
47     PreferencesValue Get(const std::string &key, const PreferencesValue &defValue) override;
48 
49     int Put(const std::string &key, const PreferencesValue &value) override;
50 
51     int GetInt(const std::string &key, const int &defValue) override;
52 
53     std::string GetString(const std::string &key, const std::string &defValue) override;
54 
55     bool GetBool(const std::string &key, const bool &defValue) override;
56 
57     float GetFloat(const std::string &key, const float &defValue) override;
58 
59     double GetDouble(const std::string &key, const double &defValue) override;
60 
61     int64_t GetLong(const std::string &key, const int64_t &defValue) override;
62 
63     std::map<std::string, PreferencesValue> GetAll() override;
64 
65     bool HasKey(const std::string &key) override;
66 
67     int PutInt(const std::string &key, int value) override;
68 
69     int PutString(const std::string &key, const std::string &value) override;
70 
71     int PutBool(const std::string &key, bool value) override;
72 
73     int PutLong(const std::string &key, int64_t value) override;
74 
75     int PutFloat(const std::string &key, float value) override;
76 
77     int PutDouble(const std::string &key, double value) override;
78 
79     int Delete(const std::string &key) override;
80 
81     int Clear() override;
82 
83     void Flush() override;
84 
85     int FlushSync() override;
86 
87     int RegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver,
88         RegisterMode mode = RegisterMode::LOCAL_CHANGE) override;
89 
90     int UnRegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver,
91         RegisterMode mode = RegisterMode::LOCAL_CHANGE) override;
92 
93     int RegisterDataObserver(std::shared_ptr<PreferencesObserver> preferencesObserver,
94         const std::vector<std::string> &keys = {}) override;
95 
96     int UnRegisterDataObserver(std::shared_ptr<PreferencesObserver> preferencesObserver,
97         const std::vector<std::string> &keys = {}) override;
98 
99     std::string GetGroupId() const override;
100 
101     int CloseDb() override;
102 
Close()103     virtual int Close()
104     {
105         return E_OK;
106     }
107 
IsClose(const std::string & name)108     virtual bool IsClose(const std::string &name)
109     {
110         return false;
111     }
112 
113     std::pair<int, PreferencesValue> GetValue(const std::string &key, const PreferencesValue &defValue) override;
114 
115     std::pair<int, std::map<std::string, PreferencesValue>> GetAllData() override;
116 
117     std::string GetBundleName() const override;
118 
119     virtual std::unordered_map<std::string, PreferencesValue> GetAllDatas() override;
120 
121 protected:
122     Uri MakeUri(const std::string &key = "");
123     struct WeakPtrCompare {
operatorWeakPtrCompare124         bool operator()(const std::weak_ptr<PreferencesObserver> &a, const std::weak_ptr<PreferencesObserver> &b) const
125         {
126             return a.owner_before(b);
127         }
128     };
129     using DataObserverMap = std::map<std::weak_ptr<PreferencesObserver>, std::set<std::string>, WeakPtrCompare>;
130     std::mutex mutex_;
131     std::shared_mutex obseverMetux_;
132     std::condition_variable cond_;
133 
134     std::vector<std::weak_ptr<PreferencesObserver>> localObservers_;
135     std::vector<sptr<DataPreferencesObserverStub>> multiProcessObservers_;
136     DataObserverMap dataObserversMap_;
137 
138     const Options options_;
139 
140     static ExecutorPool executorPool_;
141 };
142 } // End of namespace NativePreferences
143 } // End of namespace OHOS
144 #endif // End of #ifndef PREFERENCES_H