Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 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_H_ |
| 18 | #define UPDATE_ENGINE_COMMON_PREFS_H_ |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 19 | |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 20 | #include <map> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 21 | #include <string> |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 22 | #include <vector> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 23 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 24 | #include <base/files/file_path.h> |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 25 | |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 26 | #include "gtest/gtest_prod.h" // for FRIEND_TEST |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 27 | #include "update_engine/common/prefs_interface.h" |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 28 | |
| 29 | namespace chromeos_update_engine { |
| 30 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 31 | // Implements a preference store by storing the value associated with a key |
| 32 | // in a given storage passed during construction. |
| 33 | class PrefsBase : public PrefsInterface { |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 34 | public: |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 35 | // Storage interface used to set and retrieve keys. |
| 36 | class StorageInterface { |
| 37 | public: |
| 38 | StorageInterface() = default; |
| 39 | virtual ~StorageInterface() = default; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 40 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 41 | // Get the key named |key| and store its value in the referenced |value|. |
| 42 | // Returns whether the operation succeeded. |
| 43 | virtual bool GetKey(const std::string& key, std::string* value) const = 0; |
| 44 | |
Jae Hoon Kim | 29a80e0 | 2020-05-11 20:18:49 -0700 | [diff] [blame] | 45 | // Get the keys stored within the namespace. If there are no keys in the |
| 46 | // namespace, |keys| will be empty. Returns whether the operation succeeded. |
| 47 | virtual bool GetSubKeys(const std::string& ns, |
| 48 | std::vector<std::string>* keys) const = 0; |
| 49 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 50 | // Set the value of the key named |key| to |value| regardless of the |
| 51 | // previous value. Returns whether the operation succeeded. |
| 52 | virtual bool SetKey(const std::string& key, const std::string& value) = 0; |
| 53 | |
| 54 | // Returns whether the key named |key| exists. |
| 55 | virtual bool KeyExists(const std::string& key) const = 0; |
| 56 | |
| 57 | // Deletes the value associated with the key name |key|. Returns whether the |
| 58 | // key was deleted. |
| 59 | virtual bool DeleteKey(const std::string& key) = 0; |
| 60 | |
| 61 | private: |
| 62 | DISALLOW_COPY_AND_ASSIGN(StorageInterface); |
| 63 | }; |
| 64 | |
| 65 | explicit PrefsBase(StorageInterface* storage) : storage_(storage) {} |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 66 | |
| 67 | // PrefsInterface methods. |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 68 | bool GetString(const std::string& key, std::string* value) const override; |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 69 | bool SetString(const std::string& key, const std::string& value) override; |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 70 | bool GetInt64(const std::string& key, int64_t* value) const override; |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 71 | bool SetInt64(const std::string& key, const int64_t value) override; |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 72 | bool GetBoolean(const std::string& key, bool* value) const override; |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 73 | bool SetBoolean(const std::string& key, const bool value) override; |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 74 | |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 75 | bool Exists(const std::string& key) const override; |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 76 | bool Delete(const std::string& key) override; |
Vyshu Khota | 4c5413d | 2020-11-04 16:17:25 -0800 | [diff] [blame] | 77 | bool Delete(const std::string& pref_key, |
| 78 | const std::vector<std::string>& nss) override; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 79 | |
Jae Hoon Kim | 29a80e0 | 2020-05-11 20:18:49 -0700 | [diff] [blame] | 80 | bool GetSubKeys(const std::string& ns, |
| 81 | std::vector<std::string>* keys) const override; |
| 82 | |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 83 | void AddObserver(const std::string& key, |
| 84 | ObserverInterface* observer) override; |
| 85 | void RemoveObserver(const std::string& key, |
| 86 | ObserverInterface* observer) override; |
| 87 | |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 88 | private: |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 89 | // The registered observers watching for changes. |
| 90 | std::map<std::string, std::vector<ObserverInterface*>> observers_; |
| 91 | |
| 92 | // The concrete implementation of the storage used for the keys. |
| 93 | StorageInterface* storage_; |
| 94 | |
| 95 | DISALLOW_COPY_AND_ASSIGN(PrefsBase); |
| 96 | }; |
| 97 | |
| 98 | // Implements a preference store by storing the value associated with |
| 99 | // a key in a separate file named after the key under a preference |
| 100 | // store directory. |
| 101 | |
| 102 | class Prefs : public PrefsBase { |
| 103 | public: |
| 104 | Prefs() : PrefsBase(&file_storage_) {} |
| 105 | |
| 106 | // Initializes the store by associating this object with |prefs_dir| |
| 107 | // as the preference store directory. Returns true on success, false |
| 108 | // otherwise. |
| 109 | bool Init(const base::FilePath& prefs_dir); |
| 110 | |
| 111 | private: |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 112 | FRIEND_TEST(PrefsTest, GetFileNameForKey); |
| 113 | FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter); |
| 114 | FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty); |
| 115 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 116 | class FileStorage : public PrefsBase::StorageInterface { |
| 117 | public: |
| 118 | FileStorage() = default; |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 119 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 120 | bool Init(const base::FilePath& prefs_dir); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 121 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 122 | // PrefsBase::StorageInterface overrides. |
| 123 | bool GetKey(const std::string& key, std::string* value) const override; |
Jae Hoon Kim | 29a80e0 | 2020-05-11 20:18:49 -0700 | [diff] [blame] | 124 | bool GetSubKeys(const std::string& ns, |
| 125 | std::vector<std::string>* keys) const override; |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 126 | bool SetKey(const std::string& key, const std::string& value) override; |
| 127 | bool KeyExists(const std::string& key) const override; |
| 128 | bool DeleteKey(const std::string& key) override; |
| 129 | |
| 130 | private: |
| 131 | FRIEND_TEST(PrefsTest, GetFileNameForKey); |
| 132 | FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter); |
| 133 | FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty); |
| 134 | |
| 135 | // Sets |filename| to the full path to the file containing the data |
| 136 | // associated with |key|. Returns true on success, false otherwise. |
| 137 | bool GetFileNameForKey(const std::string& key, |
| 138 | base::FilePath* filename) const; |
| 139 | |
| 140 | // Preference store directory. |
| 141 | base::FilePath prefs_dir_; |
| 142 | }; |
| 143 | |
| 144 | // The concrete file storage implementation. |
| 145 | FileStorage file_storage_; |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 146 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 147 | DISALLOW_COPY_AND_ASSIGN(Prefs); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 150 | // Implements a preference store in memory. The stored values are lost when the |
| 151 | // object is destroyed. |
| 152 | |
| 153 | class MemoryPrefs : public PrefsBase { |
| 154 | public: |
| 155 | MemoryPrefs() : PrefsBase(&mem_storage_) {} |
| 156 | |
| 157 | private: |
| 158 | class MemoryStorage : public PrefsBase::StorageInterface { |
| 159 | public: |
| 160 | MemoryStorage() = default; |
| 161 | |
| 162 | // PrefsBase::StorageInterface overrides. |
| 163 | bool GetKey(const std::string& key, std::string* value) const override; |
Jae Hoon Kim | 29a80e0 | 2020-05-11 20:18:49 -0700 | [diff] [blame] | 164 | bool GetSubKeys(const std::string& ns, |
| 165 | std::vector<std::string>* keys) const override; |
Alex Deymo | a0284ac | 2016-07-22 12:51:41 -0700 | [diff] [blame] | 166 | bool SetKey(const std::string& key, const std::string& value) override; |
| 167 | bool KeyExists(const std::string& key) const override; |
| 168 | bool DeleteKey(const std::string& key) override; |
| 169 | |
| 170 | private: |
| 171 | // The std::map holding the values in memory. |
| 172 | std::map<std::string, std::string> values_; |
| 173 | }; |
| 174 | |
| 175 | // The concrete memory storage implementation. |
| 176 | MemoryStorage mem_storage_; |
| 177 | |
| 178 | DISALLOW_COPY_AND_ASSIGN(MemoryPrefs); |
| 179 | }; |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 180 | } // namespace chromeos_update_engine |
| 181 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 182 | #endif // UPDATE_ENGINE_COMMON_PREFS_H_ |