1 // 2 // Copyright (C) 2011 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef UPDATE_ENGINE_COMMON_PREFS_INTERFACE_H_ 18 #define UPDATE_ENGINE_COMMON_PREFS_INTERFACE_H_ 19 20 #include <stdint.h> 21 22 #include <string> 23 #include <vector> 24 25 namespace chromeos_update_engine { 26 27 // The prefs interface allows access to a persistent preferences 28 // store. The two reasons for providing this as an interface are 29 // testing as well as easier switching to a new implementation in the 30 // future, if necessary. 31 32 class PrefsInterface { 33 public: 34 // Observer class to be notified about key value changes. 35 class ObserverInterface { 36 public: 37 virtual ~ObserverInterface() = default; 38 39 // Called when the value is set for the observed |key|. 40 virtual void OnPrefSet(const std::string& key) = 0; 41 42 // Called when the observed |key| is deleted. 43 virtual void OnPrefDeleted(const std::string& key) = 0; 44 }; 45 46 virtual ~PrefsInterface() = default; 47 48 // Gets a string |value| associated with |key|. Returns true on 49 // success, false on failure (including when the |key| is not 50 // present in the store). 51 virtual bool GetString(const std::string& key, std::string* value) const = 0; 52 53 // Associates |key| with a string |value|. Returns true on success, 54 // false otherwise. 55 virtual bool SetString(const std::string& key, const std::string& value) = 0; 56 57 // Gets an int64_t |value| associated with |key|. Returns true on 58 // success, false on failure (including when the |key| is not 59 // present in the store). 60 virtual bool GetInt64(const std::string& key, int64_t* value) const = 0; 61 62 // Associates |key| with an int64_t |value|. Returns true on success, 63 // false otherwise. 64 virtual bool SetInt64(const std::string& key, const int64_t value) = 0; 65 66 // Gets a boolean |value| associated with |key|. Returns true on 67 // success, false on failure (including when the |key| is not 68 // present in the store). 69 virtual bool GetBoolean(const std::string& key, bool* value) const = 0; 70 71 // Associates |key| with a boolean |value|. Returns true on success, 72 // false otherwise. 73 virtual bool SetBoolean(const std::string& key, const bool value) = 0; 74 75 // Returns true if the setting exists (i.e. a file with the given key 76 // exists in the prefs directory) 77 virtual bool Exists(const std::string& key) const = 0; 78 79 // Returns true if successfully deleted the file corresponding to 80 // this key. Calling with non-existent keys does nothing. 81 virtual bool Delete(const std::string& key) = 0; 82 83 // Creates a key which is part of a sub preference. 84 static std::string CreateSubKey(const std::vector<std::string>& ns_with_key); 85 86 // Returns a list of keys within the namespace. 87 virtual bool GetSubKeys(const std::string& ns, 88 std::vector<std::string>* keys) const = 0; 89 90 // Add an observer to watch whenever the given |key| is modified. The 91 // OnPrefSet() and OnPrefDelete() methods will be called whenever any of the 92 // Set*() methods or the Delete() method are called on the given key, 93 // respectively. 94 virtual void AddObserver(const std::string& key, 95 ObserverInterface* observer) = 0; 96 97 // Remove an observer added with AddObserver(). The observer won't be called 98 // anymore for future Set*() and Delete() method calls. 99 virtual void RemoveObserver(const std::string& key, 100 ObserverInterface* observer) = 0; 101 }; 102 103 } // namespace chromeos_update_engine 104 105 #endif // UPDATE_ENGINE_COMMON_PREFS_INTERFACE_H_ 106