| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_COMMON_PREFS_INTERFACE_H_ | 
|  | 18 | #define UPDATE_ENGINE_COMMON_PREFS_INTERFACE_H_ | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 19 |  | 
| Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 20 | #include <stdint.h> | 
|  | 21 |  | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 22 | #include <string> | 
| Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 23 | #include <vector> | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 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: | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 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|. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 40 | virtual void OnPrefSet(std::string_view key) = 0; | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 41 |  | 
|  | 42 | // Called when the observed |key| is deleted. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 43 | virtual void OnPrefDeleted(std::string_view key) = 0; | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 44 | }; | 
|  | 45 |  | 
|  | 46 | virtual ~PrefsInterface() = default; | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 47 |  | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 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). | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 51 | virtual bool GetString(std::string_view key, std::string* value) const = 0; | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 52 |  | 
|  | 53 | // Associates |key| with a string |value|. Returns true on success, | 
|  | 54 | // false otherwise. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 55 | virtual bool SetString(std::string_view key, std::string_view value) = 0; | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 56 |  | 
| Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 57 | // Gets an int64_t |value| associated with |key|. Returns true on | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 58 | // success, false on failure (including when the |key| is not | 
|  | 59 | // present in the store). | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 60 | virtual bool GetInt64(std::string_view key, int64_t* value) const = 0; | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 61 |  | 
| Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 62 | // Associates |key| with an int64_t |value|. Returns true on success, | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 63 | // false otherwise. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 64 | virtual bool SetInt64(std::string_view key, const int64_t value) = 0; | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 65 |  | 
| Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 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). | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 69 | virtual bool GetBoolean(std::string_view key, bool* value) const = 0; | 
| Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 70 |  | 
|  | 71 | // Associates |key| with a boolean |value|. Returns true on success, | 
|  | 72 | // false otherwise. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 73 | virtual bool SetBoolean(std::string_view key, const bool value) = 0; | 
| Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 74 |  | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 75 | // Returns true if the setting exists (i.e. a file with the given key | 
|  | 76 | // exists in the prefs directory) | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 77 | virtual bool Exists(std::string_view key) const = 0; | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 78 |  | 
|  | 79 | // Returns true if successfully deleted the file corresponding to | 
|  | 80 | // this key. Calling with non-existent keys does nothing. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 81 | virtual bool Delete(std::string_view key) = 0; | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 82 |  | 
| Vyshu Khota | 4c5413d | 2020-11-04 16:17:25 -0800 | [diff] [blame] | 83 | // Deletes the pref key from platform and given namespace subdirectories. | 
|  | 84 | // Keys are matched against end of pref keys in each namespace. | 
|  | 85 | // Returns true if all deletes were successful. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 86 | virtual bool Delete(std::string_view pref_key, | 
| Vyshu Khota | 4c5413d | 2020-11-04 16:17:25 -0800 | [diff] [blame] | 87 | const std::vector<std::string>& nss) = 0; | 
|  | 88 |  | 
| Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 89 | // Creates a key which is part of a sub preference. | 
| Jae Hoon Kim | c1f3692 | 2020-05-11 18:20:18 -0700 | [diff] [blame] | 90 | static std::string CreateSubKey(const std::vector<std::string>& ns_with_key); | 
| Andrew | 065d78d | 2020-04-07 15:43:07 -0700 | [diff] [blame] | 91 |  | 
| Jae Hoon Kim | 29a80e0 | 2020-05-11 20:18:49 -0700 | [diff] [blame] | 92 | // Returns a list of keys within the namespace. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 93 | virtual bool GetSubKeys(std::string_view ns, | 
| Jae Hoon Kim | 29a80e0 | 2020-05-11 20:18:49 -0700 | [diff] [blame] | 94 | std::vector<std::string>* keys) const = 0; | 
|  | 95 |  | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 96 | // Add an observer to watch whenever the given |key| is modified. The | 
|  | 97 | // OnPrefSet() and OnPrefDelete() methods will be called whenever any of the | 
|  | 98 | // Set*() methods or the Delete() method are called on the given key, | 
|  | 99 | // respectively. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 100 | virtual void AddObserver(std::string_view key, | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 101 | ObserverInterface* observer) = 0; | 
|  | 102 |  | 
|  | 103 | // Remove an observer added with AddObserver(). The observer won't be called | 
|  | 104 | // anymore for future Set*() and Delete() method calls. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 105 | virtual void RemoveObserver(std::string_view key, | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 106 | ObserverInterface* observer) = 0; | 
| Vyshu Khota | 4c5413d | 2020-11-04 16:17:25 -0800 | [diff] [blame] | 107 |  | 
| Daniel Zheng | 5eece04 | 2023-05-17 14:44:10 -0700 | [diff] [blame] | 108 | // create tmp_prefs to write to during checkpointing. Flush will replace prefs | 
|  | 109 | // with tmp_prefs | 
|  | 110 | virtual bool StartTransaction() = 0; | 
|  | 111 |  | 
|  | 112 | // cancel any pending transaction by deleting tmp_prefs | 
|  | 113 | virtual bool CancelTransaction() = 0; | 
|  | 114 |  | 
|  | 115 | // swap prefs with tmp_prefs checkpointing will use new tmp prefs to resume | 
|  | 116 | // updates, otherwise we fall back on old prefs | 
|  | 117 | virtual bool SubmitTransaction() = 0; | 
|  | 118 |  | 
| Vyshu Khota | 4c5413d | 2020-11-04 16:17:25 -0800 | [diff] [blame] | 119 | protected: | 
|  | 120 | // Key separator used to create sub key and get file names, | 
|  | 121 | static const char kKeySeparator = '/'; | 
| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 122 | }; | 
|  | 123 |  | 
|  | 124 | }  // namespace chromeos_update_engine | 
|  | 125 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 126 | #endif  // UPDATE_ENGINE_COMMON_PREFS_INTERFACE_H_ |