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