| 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 | // | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_COMMON_FAKE_PREFS_H_ | 
 | 18 | #define UPDATE_ENGINE_COMMON_FAKE_PREFS_H_ | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 19 |  | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 20 | #include <functional> | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 21 | #include <map> | 
 | 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 Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 25 |  | 
| Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 26 | #include <base/macros.h> | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 27 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #include "update_engine/common/prefs_interface.h" | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 29 |  | 
 | 30 | namespace chromeos_update_engine { | 
 | 31 |  | 
 | 32 | // Implements a fake preference store by storing the value associated with | 
 | 33 | // a key in a std::map, suitable for testing. It doesn't allow to set a value on | 
 | 34 | // a key with a different type than the previously set type. This enforces the | 
 | 35 | // type of a given key to be fixed. Also the class checks that the Get*() | 
 | 36 | // methods aren't called on a key set with a different type. | 
 | 37 |  | 
 | 38 | class FakePrefs : public PrefsInterface { | 
 | 39 |  public: | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 40 |   FakePrefs() = default; | 
 | 41 |   ~FakePrefs(); | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 42 |  | 
 | 43 |   // PrefsInterface methods. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 44 |   bool GetString(std::string_view key, std::string* value) const override; | 
 | 45 |   bool SetString(std::string_view key, std::string_view value) override; | 
 | 46 |   bool GetInt64(std::string_view key, int64_t* value) const override; | 
 | 47 |   bool SetInt64(std::string_view key, const int64_t value) override; | 
 | 48 |   bool GetBoolean(std::string_view key, bool* value) const override; | 
 | 49 |   bool SetBoolean(std::string_view key, const bool value) override; | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 50 |  | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 51 |   bool Exists(std::string_view key) const override; | 
 | 52 |   bool Delete(std::string_view key) override; | 
 | 53 |   bool Delete(std::string_view key, | 
| Vyshu Khota | 4c5413d | 2020-11-04 16:17:25 -0800 | [diff] [blame] | 54 |               const std::vector<std::string>& nss) override; | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 55 |  | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 56 |   bool GetSubKeys(std::string_view ns, | 
| Jae Hoon Kim | 29a80e0 | 2020-05-11 20:18:49 -0700 | [diff] [blame] | 57 |                   std::vector<std::string>* keys) const override; | 
 | 58 |  | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 59 |   void AddObserver(std::string_view key, ObserverInterface* observer) override; | 
 | 60 |   void RemoveObserver(std::string_view key, | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 61 |                       ObserverInterface* observer) override; | 
| Daniel Zheng | 5eece04 | 2023-05-17 14:44:10 -0700 | [diff] [blame] | 62 |   bool StartTransaction() override { return false; } | 
 | 63 |   bool CancelTransaction() override { return false; } | 
 | 64 |   bool SubmitTransaction() override { return false; } | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 65 |  | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 66 |  private: | 
 | 67 |   enum class PrefType { | 
 | 68 |     kString, | 
 | 69 |     kInt64, | 
 | 70 |     kBool, | 
 | 71 |   }; | 
 | 72 |   struct PrefValue { | 
 | 73 |     std::string as_str; | 
 | 74 |     int64_t as_int64; | 
 | 75 |     bool as_bool; | 
 | 76 |   }; | 
 | 77 |  | 
 | 78 |   struct PrefTypeValue { | 
 | 79 |     PrefType type; | 
 | 80 |     PrefValue value; | 
 | 81 |   }; | 
 | 82 |  | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 83 |   // Class to store compile-time type-dependent constants. | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 84 |   template <typename T> | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 85 |   class PrefConsts { | 
 | 86 |    public: | 
 | 87 |     // The PrefType associated with T. | 
 | 88 |     static FakePrefs::PrefType const type; | 
 | 89 |  | 
 | 90 |     // The data member pointer to PrefValue associated with T. | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 91 |     static T FakePrefs::PrefValue::*const member; | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 92 |   }; | 
 | 93 |  | 
 | 94 |   // Returns a string representation of the PrefType useful for logging. | 
 | 95 |   static std::string GetTypeName(PrefType type); | 
 | 96 |  | 
 | 97 |   // Checks that the |key| is either not present or has the given |type|. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 98 |   void CheckKeyType(std::string_view key, PrefType type) const; | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 99 |  | 
 | 100 |   // Helper function to set a value of the passed |key|. It sets the type based | 
 | 101 |   // on the template parameter T. | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 102 |   template <typename T> | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 103 |   void SetValue(std::string_view key, T value); | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 104 |  | 
 | 105 |   // Helper function to get a value from the map checking for invalid calls. | 
 | 106 |   // The function fails the test if you attempt to read a value  defined as a | 
 | 107 |   // different type. Returns whether the get succeeded. | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 108 |   template <typename T> | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 109 |   bool GetValue(std::string_view key, T* value) const; | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 110 |  | 
 | 111 |   // Container for all the key/value pairs. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 112 |   std::map<std::string, PrefTypeValue, std::less<>> values_; | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 113 |  | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 114 |   // The registered observers watching for changes. | 
| Kelvin Zhang | 1c86a92 | 2021-05-13 10:30:48 -0400 | [diff] [blame] | 115 |   std::map<std::string, std::vector<ObserverInterface*>, std::less<>> | 
 | 116 |       observers_; | 
| Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 117 |  | 
| Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 118 |   DISALLOW_COPY_AND_ASSIGN(FakePrefs); | 
 | 119 | }; | 
 | 120 |  | 
 | 121 | }  // namespace chromeos_update_engine | 
 | 122 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 123 | #endif  // UPDATE_ENGINE_COMMON_FAKE_PREFS_H_ |