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