xref: /aosp_15_r20/system/update_engine/common/prefs.h (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2012 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker 
17*5a923131SAndroid Build Coastguard Worker #ifndef UPDATE_ENGINE_COMMON_PREFS_H_
18*5a923131SAndroid Build Coastguard Worker #define UPDATE_ENGINE_COMMON_PREFS_H_
19*5a923131SAndroid Build Coastguard Worker 
20*5a923131SAndroid Build Coastguard Worker #include <functional>
21*5a923131SAndroid Build Coastguard Worker #include <map>
22*5a923131SAndroid Build Coastguard Worker #include <string>
23*5a923131SAndroid Build Coastguard Worker #include <string_view>
24*5a923131SAndroid Build Coastguard Worker #include <vector>
25*5a923131SAndroid Build Coastguard Worker 
26*5a923131SAndroid Build Coastguard Worker #include <base/files/file_path.h>
27*5a923131SAndroid Build Coastguard Worker 
28*5a923131SAndroid Build Coastguard Worker #include "gtest/gtest_prod.h"  // for FRIEND_TEST
29*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/prefs_interface.h"
30*5a923131SAndroid Build Coastguard Worker 
31*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
32*5a923131SAndroid Build Coastguard Worker 
33*5a923131SAndroid Build Coastguard Worker // Implements a preference store by storing the value associated with a key
34*5a923131SAndroid Build Coastguard Worker // in a given storage passed during construction.
35*5a923131SAndroid Build Coastguard Worker class PrefsBase : public PrefsInterface {
36*5a923131SAndroid Build Coastguard Worker  public:
37*5a923131SAndroid Build Coastguard Worker   // Storage interface used to set and retrieve keys.
38*5a923131SAndroid Build Coastguard Worker   class StorageInterface {
39*5a923131SAndroid Build Coastguard Worker    public:
40*5a923131SAndroid Build Coastguard Worker     StorageInterface() = default;
41*5a923131SAndroid Build Coastguard Worker     virtual ~StorageInterface() = default;
42*5a923131SAndroid Build Coastguard Worker 
43*5a923131SAndroid Build Coastguard Worker     // Get the key named |key| and store its value in the referenced |value|.
44*5a923131SAndroid Build Coastguard Worker     // Returns whether the operation succeeded.
45*5a923131SAndroid Build Coastguard Worker     virtual bool GetKey(std::string_view key, std::string* value) const = 0;
46*5a923131SAndroid Build Coastguard Worker 
47*5a923131SAndroid Build Coastguard Worker     // Get the keys stored within the namespace. If there are no keys in the
48*5a923131SAndroid Build Coastguard Worker     // namespace, |keys| will be empty. Returns whether the operation succeeded.
49*5a923131SAndroid Build Coastguard Worker     virtual bool GetSubKeys(std::string_view ns,
50*5a923131SAndroid Build Coastguard Worker                             std::vector<std::string>* keys) const = 0;
51*5a923131SAndroid Build Coastguard Worker 
52*5a923131SAndroid Build Coastguard Worker     // Set the value of the key named |key| to |value| regardless of the
53*5a923131SAndroid Build Coastguard Worker     // previous value. Returns whether the operation succeeded.
54*5a923131SAndroid Build Coastguard Worker     virtual bool SetKey(std::string_view key, std::string_view value) = 0;
55*5a923131SAndroid Build Coastguard Worker 
56*5a923131SAndroid Build Coastguard Worker     // Returns whether the key named |key| exists.
57*5a923131SAndroid Build Coastguard Worker     virtual bool KeyExists(std::string_view key) const = 0;
58*5a923131SAndroid Build Coastguard Worker 
59*5a923131SAndroid Build Coastguard Worker     // Deletes the value associated with the key name |key|. Returns whether the
60*5a923131SAndroid Build Coastguard Worker     // key was deleted.
61*5a923131SAndroid Build Coastguard Worker     virtual bool DeleteKey(std::string_view key) = 0;
62*5a923131SAndroid Build Coastguard Worker 
63*5a923131SAndroid Build Coastguard Worker     // Makes a copy of prefs directory called prefs_tmp, which is modified
64*5a923131SAndroid Build Coastguard Worker     // during update_engine checkpointing
CreateTemporaryPrefs()65*5a923131SAndroid Build Coastguard Worker     virtual bool CreateTemporaryPrefs() { return false; }
66*5a923131SAndroid Build Coastguard Worker 
67*5a923131SAndroid Build Coastguard Worker     // Deletes prefs_tmp directory
DeleteTemporaryPrefs()68*5a923131SAndroid Build Coastguard Worker     virtual bool DeleteTemporaryPrefs() { return false; }
69*5a923131SAndroid Build Coastguard Worker 
70*5a923131SAndroid Build Coastguard Worker     // Replaces prefs with prefs_tmp to make update_engine checkpointing more
71*5a923131SAndroid Build Coastguard Worker     // atomic
SwapPrefs()72*5a923131SAndroid Build Coastguard Worker     virtual bool SwapPrefs() { return false; }
73*5a923131SAndroid Build Coastguard Worker 
74*5a923131SAndroid Build Coastguard Worker    private:
75*5a923131SAndroid Build Coastguard Worker     DISALLOW_COPY_AND_ASSIGN(StorageInterface);
76*5a923131SAndroid Build Coastguard Worker   };
77*5a923131SAndroid Build Coastguard Worker 
PrefsBase(StorageInterface * storage)78*5a923131SAndroid Build Coastguard Worker   explicit PrefsBase(StorageInterface* storage) : storage_(storage) {}
79*5a923131SAndroid Build Coastguard Worker 
80*5a923131SAndroid Build Coastguard Worker   // PrefsInterface methods.
81*5a923131SAndroid Build Coastguard Worker   bool GetString(std::string_view key, std::string* value) const override;
82*5a923131SAndroid Build Coastguard Worker   bool SetString(std::string_view key, std::string_view value) override;
83*5a923131SAndroid Build Coastguard Worker   bool GetInt64(std::string_view key, int64_t* value) const override;
84*5a923131SAndroid Build Coastguard Worker   bool SetInt64(std::string_view key, const int64_t value) override;
85*5a923131SAndroid Build Coastguard Worker   bool GetBoolean(std::string_view key, bool* value) const override;
86*5a923131SAndroid Build Coastguard Worker   bool SetBoolean(std::string_view key, const bool value) override;
87*5a923131SAndroid Build Coastguard Worker   bool StartTransaction() override;
88*5a923131SAndroid Build Coastguard Worker   bool CancelTransaction() override;
89*5a923131SAndroid Build Coastguard Worker   bool SubmitTransaction() override;
90*5a923131SAndroid Build Coastguard Worker 
91*5a923131SAndroid Build Coastguard Worker   bool Exists(std::string_view key) const override;
92*5a923131SAndroid Build Coastguard Worker   bool Delete(std::string_view key) override;
93*5a923131SAndroid Build Coastguard Worker   bool Delete(std::string_view pref_key,
94*5a923131SAndroid Build Coastguard Worker               const std::vector<std::string>& nss) override;
95*5a923131SAndroid Build Coastguard Worker 
96*5a923131SAndroid Build Coastguard Worker   bool GetSubKeys(std::string_view ns,
97*5a923131SAndroid Build Coastguard Worker                   std::vector<std::string>* keys) const override;
98*5a923131SAndroid Build Coastguard Worker 
99*5a923131SAndroid Build Coastguard Worker   void AddObserver(std::string_view key, ObserverInterface* observer) override;
100*5a923131SAndroid Build Coastguard Worker   void RemoveObserver(std::string_view key,
101*5a923131SAndroid Build Coastguard Worker                       ObserverInterface* observer) override;
102*5a923131SAndroid Build Coastguard Worker 
103*5a923131SAndroid Build Coastguard Worker  private:
104*5a923131SAndroid Build Coastguard Worker   // The registered observers watching for changes.
105*5a923131SAndroid Build Coastguard Worker   std::map<std::string, std::vector<ObserverInterface*>, std::less<>>
106*5a923131SAndroid Build Coastguard Worker       observers_;
107*5a923131SAndroid Build Coastguard Worker 
108*5a923131SAndroid Build Coastguard Worker   // The concrete implementation of the storage used for the keys.
109*5a923131SAndroid Build Coastguard Worker   StorageInterface* storage_;
110*5a923131SAndroid Build Coastguard Worker 
111*5a923131SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(PrefsBase);
112*5a923131SAndroid Build Coastguard Worker };
113*5a923131SAndroid Build Coastguard Worker 
114*5a923131SAndroid Build Coastguard Worker // Implements a preference store by storing the value associated with
115*5a923131SAndroid Build Coastguard Worker // a key in a separate file named after the key under a preference
116*5a923131SAndroid Build Coastguard Worker // store directory.
117*5a923131SAndroid Build Coastguard Worker 
118*5a923131SAndroid Build Coastguard Worker class Prefs : public PrefsBase {
119*5a923131SAndroid Build Coastguard Worker  public:
Prefs()120*5a923131SAndroid Build Coastguard Worker   Prefs() : PrefsBase(&file_storage_) {}
121*5a923131SAndroid Build Coastguard Worker 
122*5a923131SAndroid Build Coastguard Worker   // Initializes the store by associating this object with |prefs_dir|
123*5a923131SAndroid Build Coastguard Worker   // as the preference store directory. Returns true on success, false
124*5a923131SAndroid Build Coastguard Worker   // otherwise.
125*5a923131SAndroid Build Coastguard Worker   bool Init(const base::FilePath& prefs_dir);
126*5a923131SAndroid Build Coastguard Worker 
127*5a923131SAndroid Build Coastguard Worker  private:
128*5a923131SAndroid Build Coastguard Worker   FRIEND_TEST(PrefsTest, GetFileNameForKey);
129*5a923131SAndroid Build Coastguard Worker   FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter);
130*5a923131SAndroid Build Coastguard Worker   FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty);
131*5a923131SAndroid Build Coastguard Worker 
132*5a923131SAndroid Build Coastguard Worker   class FileStorage : public PrefsBase::StorageInterface {
133*5a923131SAndroid Build Coastguard Worker    public:
134*5a923131SAndroid Build Coastguard Worker     FileStorage() = default;
135*5a923131SAndroid Build Coastguard Worker 
136*5a923131SAndroid Build Coastguard Worker     bool Init(const base::FilePath& prefs_dir);
137*5a923131SAndroid Build Coastguard Worker 
138*5a923131SAndroid Build Coastguard Worker     // PrefsBase::StorageInterface overrides.
139*5a923131SAndroid Build Coastguard Worker     bool GetKey(std::string_view key, std::string* value) const override;
140*5a923131SAndroid Build Coastguard Worker     bool GetSubKeys(std::string_view ns,
141*5a923131SAndroid Build Coastguard Worker                     std::vector<std::string>* keys) const override;
142*5a923131SAndroid Build Coastguard Worker     bool SetKey(std::string_view key, std::string_view value) override;
143*5a923131SAndroid Build Coastguard Worker     bool KeyExists(std::string_view key) const override;
144*5a923131SAndroid Build Coastguard Worker     bool DeleteKey(std::string_view key) override;
145*5a923131SAndroid Build Coastguard Worker     bool CreateTemporaryPrefs() override;
146*5a923131SAndroid Build Coastguard Worker     bool DeleteTemporaryPrefs() override;
147*5a923131SAndroid Build Coastguard Worker     bool SwapPrefs() override;
148*5a923131SAndroid Build Coastguard Worker 
149*5a923131SAndroid Build Coastguard Worker    private:
150*5a923131SAndroid Build Coastguard Worker     FRIEND_TEST(PrefsTest, GetFileNameForKey);
151*5a923131SAndroid Build Coastguard Worker     FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter);
152*5a923131SAndroid Build Coastguard Worker     FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty);
153*5a923131SAndroid Build Coastguard Worker 
154*5a923131SAndroid Build Coastguard Worker     // Sets |filename| to the full path to the file containing the data
155*5a923131SAndroid Build Coastguard Worker     // associated with |key|. Returns true on success, false otherwise.
156*5a923131SAndroid Build Coastguard Worker     bool GetFileNameForKey(std::string_view key,
157*5a923131SAndroid Build Coastguard Worker                            base::FilePath* filename) const;
158*5a923131SAndroid Build Coastguard Worker 
159*5a923131SAndroid Build Coastguard Worker     // Returns path of prefs_tmp used during update_engine checkpointing
160*5a923131SAndroid Build Coastguard Worker     std::string GetTemporaryDir() const;
161*5a923131SAndroid Build Coastguard Worker 
162*5a923131SAndroid Build Coastguard Worker     // Preference store directory.
163*5a923131SAndroid Build Coastguard Worker     base::FilePath prefs_dir_;
164*5a923131SAndroid Build Coastguard Worker   };
165*5a923131SAndroid Build Coastguard Worker 
166*5a923131SAndroid Build Coastguard Worker   // The concrete file storage implementation.
167*5a923131SAndroid Build Coastguard Worker   FileStorage file_storage_;
168*5a923131SAndroid Build Coastguard Worker 
169*5a923131SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(Prefs);
170*5a923131SAndroid Build Coastguard Worker };
171*5a923131SAndroid Build Coastguard Worker 
172*5a923131SAndroid Build Coastguard Worker // Implements a preference store in memory. The stored values are lost when the
173*5a923131SAndroid Build Coastguard Worker // object is destroyed.
174*5a923131SAndroid Build Coastguard Worker 
175*5a923131SAndroid Build Coastguard Worker class MemoryPrefs : public PrefsBase {
176*5a923131SAndroid Build Coastguard Worker  public:
MemoryPrefs()177*5a923131SAndroid Build Coastguard Worker   MemoryPrefs() : PrefsBase(&mem_storage_) {}
178*5a923131SAndroid Build Coastguard Worker 
179*5a923131SAndroid Build Coastguard Worker  private:
180*5a923131SAndroid Build Coastguard Worker   class MemoryStorage : public PrefsBase::StorageInterface {
181*5a923131SAndroid Build Coastguard Worker    public:
182*5a923131SAndroid Build Coastguard Worker     MemoryStorage() = default;
183*5a923131SAndroid Build Coastguard Worker 
184*5a923131SAndroid Build Coastguard Worker     // PrefsBase::StorageInterface overrides.
185*5a923131SAndroid Build Coastguard Worker     bool GetKey(std::string_view, std::string* value) const override;
186*5a923131SAndroid Build Coastguard Worker     bool GetSubKeys(std::string_view ns,
187*5a923131SAndroid Build Coastguard Worker                     std::vector<std::string>* keys) const override;
188*5a923131SAndroid Build Coastguard Worker     bool SetKey(std::string_view key, std::string_view value) override;
189*5a923131SAndroid Build Coastguard Worker     bool KeyExists(std::string_view key) const override;
190*5a923131SAndroid Build Coastguard Worker     bool DeleteKey(std::string_view key) override;
191*5a923131SAndroid Build Coastguard Worker 
192*5a923131SAndroid Build Coastguard Worker    private:
193*5a923131SAndroid Build Coastguard Worker     // The std::map holding the values in memory.
194*5a923131SAndroid Build Coastguard Worker     std::map<std::string, std::string, std::less<>> values_;
195*5a923131SAndroid Build Coastguard Worker   };
196*5a923131SAndroid Build Coastguard Worker 
197*5a923131SAndroid Build Coastguard Worker   // The concrete memory storage implementation.
198*5a923131SAndroid Build Coastguard Worker   MemoryStorage mem_storage_;
199*5a923131SAndroid Build Coastguard Worker 
200*5a923131SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(MemoryPrefs);
201*5a923131SAndroid Build Coastguard Worker };
202*5a923131SAndroid Build Coastguard Worker }  // namespace chromeos_update_engine
203*5a923131SAndroid Build Coastguard Worker 
204*5a923131SAndroid Build Coastguard Worker #endif  // UPDATE_ENGINE_COMMON_PREFS_H_
205