xref: /aosp_15_r20/system/update_engine/common/prefs_interface.h (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2011 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_INTERFACE_H_
18*5a923131SAndroid Build Coastguard Worker #define UPDATE_ENGINE_COMMON_PREFS_INTERFACE_H_
19*5a923131SAndroid Build Coastguard Worker 
20*5a923131SAndroid Build Coastguard Worker #include <stdint.h>
21*5a923131SAndroid Build Coastguard Worker 
22*5a923131SAndroid Build Coastguard Worker #include <string>
23*5a923131SAndroid Build Coastguard Worker #include <vector>
24*5a923131SAndroid Build Coastguard Worker 
25*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
26*5a923131SAndroid Build Coastguard Worker 
27*5a923131SAndroid Build Coastguard Worker // The prefs interface allows access to a persistent preferences
28*5a923131SAndroid Build Coastguard Worker // store. The two reasons for providing this as an interface are
29*5a923131SAndroid Build Coastguard Worker // testing as well as easier switching to a new implementation in the
30*5a923131SAndroid Build Coastguard Worker // future, if necessary.
31*5a923131SAndroid Build Coastguard Worker 
32*5a923131SAndroid Build Coastguard Worker class PrefsInterface {
33*5a923131SAndroid Build Coastguard Worker  public:
34*5a923131SAndroid Build Coastguard Worker   // Observer class to be notified about key value changes.
35*5a923131SAndroid Build Coastguard Worker   class ObserverInterface {
36*5a923131SAndroid Build Coastguard Worker    public:
37*5a923131SAndroid Build Coastguard Worker     virtual ~ObserverInterface() = default;
38*5a923131SAndroid Build Coastguard Worker 
39*5a923131SAndroid Build Coastguard Worker     // Called when the value is set for the observed |key|.
40*5a923131SAndroid Build Coastguard Worker     virtual void OnPrefSet(std::string_view key) = 0;
41*5a923131SAndroid Build Coastguard Worker 
42*5a923131SAndroid Build Coastguard Worker     // Called when the observed |key| is deleted.
43*5a923131SAndroid Build Coastguard Worker     virtual void OnPrefDeleted(std::string_view key) = 0;
44*5a923131SAndroid Build Coastguard Worker   };
45*5a923131SAndroid Build Coastguard Worker 
46*5a923131SAndroid Build Coastguard Worker   virtual ~PrefsInterface() = default;
47*5a923131SAndroid Build Coastguard Worker 
48*5a923131SAndroid Build Coastguard Worker   // Gets a string |value| associated with |key|. Returns true on
49*5a923131SAndroid Build Coastguard Worker   // success, false on failure (including when the |key| is not
50*5a923131SAndroid Build Coastguard Worker   // present in the store).
51*5a923131SAndroid Build Coastguard Worker   virtual bool GetString(std::string_view key, std::string* value) const = 0;
52*5a923131SAndroid Build Coastguard Worker 
53*5a923131SAndroid Build Coastguard Worker   // Associates |key| with a string |value|. Returns true on success,
54*5a923131SAndroid Build Coastguard Worker   // false otherwise.
55*5a923131SAndroid Build Coastguard Worker   virtual bool SetString(std::string_view key, std::string_view value) = 0;
56*5a923131SAndroid Build Coastguard Worker 
57*5a923131SAndroid Build Coastguard Worker   // Gets an int64_t |value| associated with |key|. Returns true on
58*5a923131SAndroid Build Coastguard Worker   // success, false on failure (including when the |key| is not
59*5a923131SAndroid Build Coastguard Worker   // present in the store).
60*5a923131SAndroid Build Coastguard Worker   virtual bool GetInt64(std::string_view key, int64_t* value) const = 0;
61*5a923131SAndroid Build Coastguard Worker 
62*5a923131SAndroid Build Coastguard Worker   // Associates |key| with an int64_t |value|. Returns true on success,
63*5a923131SAndroid Build Coastguard Worker   // false otherwise.
64*5a923131SAndroid Build Coastguard Worker   virtual bool SetInt64(std::string_view key, const int64_t value) = 0;
65*5a923131SAndroid Build Coastguard Worker 
66*5a923131SAndroid Build Coastguard Worker   // Gets a boolean |value| associated with |key|. Returns true on
67*5a923131SAndroid Build Coastguard Worker   // success, false on failure (including when the |key| is not
68*5a923131SAndroid Build Coastguard Worker   // present in the store).
69*5a923131SAndroid Build Coastguard Worker   virtual bool GetBoolean(std::string_view key, bool* value) const = 0;
70*5a923131SAndroid Build Coastguard Worker 
71*5a923131SAndroid Build Coastguard Worker   // Associates |key| with a boolean |value|. Returns true on success,
72*5a923131SAndroid Build Coastguard Worker   // false otherwise.
73*5a923131SAndroid Build Coastguard Worker   virtual bool SetBoolean(std::string_view key, const bool value) = 0;
74*5a923131SAndroid Build Coastguard Worker 
75*5a923131SAndroid Build Coastguard Worker   // Returns true if the setting exists (i.e. a file with the given key
76*5a923131SAndroid Build Coastguard Worker   // exists in the prefs directory)
77*5a923131SAndroid Build Coastguard Worker   virtual bool Exists(std::string_view key) const = 0;
78*5a923131SAndroid Build Coastguard Worker 
79*5a923131SAndroid Build Coastguard Worker   // Returns true if successfully deleted the file corresponding to
80*5a923131SAndroid Build Coastguard Worker   // this key. Calling with non-existent keys does nothing.
81*5a923131SAndroid Build Coastguard Worker   virtual bool Delete(std::string_view key) = 0;
82*5a923131SAndroid Build Coastguard Worker 
83*5a923131SAndroid Build Coastguard Worker   // Deletes the pref key from platform and given namespace subdirectories.
84*5a923131SAndroid Build Coastguard Worker   // Keys are matched against end of pref keys in each namespace.
85*5a923131SAndroid Build Coastguard Worker   // Returns true if all deletes were successful.
86*5a923131SAndroid Build Coastguard Worker   virtual bool Delete(std::string_view pref_key,
87*5a923131SAndroid Build Coastguard Worker                       const std::vector<std::string>& nss) = 0;
88*5a923131SAndroid Build Coastguard Worker 
89*5a923131SAndroid Build Coastguard Worker   // Creates a key which is part of a sub preference.
90*5a923131SAndroid Build Coastguard Worker   static std::string CreateSubKey(const std::vector<std::string>& ns_with_key);
91*5a923131SAndroid Build Coastguard Worker 
92*5a923131SAndroid Build Coastguard Worker   // Returns a list of keys within the namespace.
93*5a923131SAndroid Build Coastguard Worker   virtual bool GetSubKeys(std::string_view ns,
94*5a923131SAndroid Build Coastguard Worker                           std::vector<std::string>* keys) const = 0;
95*5a923131SAndroid Build Coastguard Worker 
96*5a923131SAndroid Build Coastguard Worker   // Add an observer to watch whenever the given |key| is modified. The
97*5a923131SAndroid Build Coastguard Worker   // OnPrefSet() and OnPrefDelete() methods will be called whenever any of the
98*5a923131SAndroid Build Coastguard Worker   // Set*() methods or the Delete() method are called on the given key,
99*5a923131SAndroid Build Coastguard Worker   // respectively.
100*5a923131SAndroid Build Coastguard Worker   virtual void AddObserver(std::string_view key,
101*5a923131SAndroid Build Coastguard Worker                            ObserverInterface* observer) = 0;
102*5a923131SAndroid Build Coastguard Worker 
103*5a923131SAndroid Build Coastguard Worker   // Remove an observer added with AddObserver(). The observer won't be called
104*5a923131SAndroid Build Coastguard Worker   // anymore for future Set*() and Delete() method calls.
105*5a923131SAndroid Build Coastguard Worker   virtual void RemoveObserver(std::string_view key,
106*5a923131SAndroid Build Coastguard Worker                               ObserverInterface* observer) = 0;
107*5a923131SAndroid Build Coastguard Worker 
108*5a923131SAndroid Build Coastguard Worker   // create tmp_prefs to write to during checkpointing. Flush will replace prefs
109*5a923131SAndroid Build Coastguard Worker   // with tmp_prefs
110*5a923131SAndroid Build Coastguard Worker   virtual bool StartTransaction() = 0;
111*5a923131SAndroid Build Coastguard Worker 
112*5a923131SAndroid Build Coastguard Worker   // cancel any pending transaction by deleting tmp_prefs
113*5a923131SAndroid Build Coastguard Worker   virtual bool CancelTransaction() = 0;
114*5a923131SAndroid Build Coastguard Worker 
115*5a923131SAndroid Build Coastguard Worker   // swap prefs with tmp_prefs checkpointing will use new tmp prefs to resume
116*5a923131SAndroid Build Coastguard Worker   // updates, otherwise we fall back on old prefs
117*5a923131SAndroid Build Coastguard Worker   virtual bool SubmitTransaction() = 0;
118*5a923131SAndroid Build Coastguard Worker 
119*5a923131SAndroid Build Coastguard Worker  protected:
120*5a923131SAndroid Build Coastguard Worker   // Key separator used to create sub key and get file names,
121*5a923131SAndroid Build Coastguard Worker   static const char kKeySeparator = '/';
122*5a923131SAndroid Build Coastguard Worker };
123*5a923131SAndroid Build Coastguard Worker 
124*5a923131SAndroid Build Coastguard Worker }  // namespace chromeos_update_engine
125*5a923131SAndroid Build Coastguard Worker 
126*5a923131SAndroid Build Coastguard Worker #endif  // UPDATE_ENGINE_COMMON_PREFS_INTERFACE_H_
127