1*5a923131SAndroid Build Coastguard Worker // 2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2012 The Android Open Source Project 3*5a923131SAndroid Build Coastguard Worker // 4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); 5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License. 6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at 7*5a923131SAndroid Build Coastguard Worker // 8*5a923131SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0 9*5a923131SAndroid Build Coastguard Worker // 10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, 12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and 14*5a923131SAndroid Build Coastguard Worker // limitations under the License. 15*5a923131SAndroid Build Coastguard Worker // 16*5a923131SAndroid Build Coastguard Worker 17*5a923131SAndroid Build Coastguard Worker #ifndef UPDATE_ENGINE_COMMON_FAKE_PREFS_H_ 18*5a923131SAndroid Build Coastguard Worker #define UPDATE_ENGINE_COMMON_FAKE_PREFS_H_ 19*5a923131SAndroid Build Coastguard Worker 20*5a923131SAndroid Build Coastguard Worker #include <functional> 21*5a923131SAndroid Build Coastguard Worker #include <map> 22*5a923131SAndroid Build Coastguard Worker #include <string> 23*5a923131SAndroid Build Coastguard Worker #include <string_view> 24*5a923131SAndroid Build Coastguard Worker #include <vector> 25*5a923131SAndroid Build Coastguard Worker 26*5a923131SAndroid Build Coastguard Worker #include <android-base/macros.h> 27*5a923131SAndroid Build Coastguard Worker 28*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/prefs_interface.h" 29*5a923131SAndroid Build Coastguard Worker 30*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine { 31*5a923131SAndroid Build Coastguard Worker 32*5a923131SAndroid Build Coastguard Worker // Implements a fake preference store by storing the value associated with 33*5a923131SAndroid Build Coastguard Worker // a key in a std::map, suitable for testing. It doesn't allow to set a value on 34*5a923131SAndroid Build Coastguard Worker // a key with a different type than the previously set type. This enforces the 35*5a923131SAndroid Build Coastguard Worker // type of a given key to be fixed. Also the class checks that the Get*() 36*5a923131SAndroid Build Coastguard Worker // methods aren't called on a key set with a different type. 37*5a923131SAndroid Build Coastguard Worker 38*5a923131SAndroid Build Coastguard Worker class FakePrefs : public PrefsInterface { 39*5a923131SAndroid Build Coastguard Worker public: 40*5a923131SAndroid Build Coastguard Worker FakePrefs() = default; 41*5a923131SAndroid Build Coastguard Worker ~FakePrefs(); 42*5a923131SAndroid Build Coastguard Worker 43*5a923131SAndroid Build Coastguard Worker // PrefsInterface methods. 44*5a923131SAndroid Build Coastguard Worker bool GetString(std::string_view key, std::string* value) const override; 45*5a923131SAndroid Build Coastguard Worker bool SetString(std::string_view key, std::string_view value) override; 46*5a923131SAndroid Build Coastguard Worker bool GetInt64(std::string_view key, int64_t* value) const override; 47*5a923131SAndroid Build Coastguard Worker bool SetInt64(std::string_view key, const int64_t value) override; 48*5a923131SAndroid Build Coastguard Worker bool GetBoolean(std::string_view key, bool* value) const override; 49*5a923131SAndroid Build Coastguard Worker bool SetBoolean(std::string_view key, const bool value) override; 50*5a923131SAndroid Build Coastguard Worker 51*5a923131SAndroid Build Coastguard Worker bool Exists(std::string_view key) const override; 52*5a923131SAndroid Build Coastguard Worker bool Delete(std::string_view key) override; 53*5a923131SAndroid Build Coastguard Worker bool Delete(std::string_view key, 54*5a923131SAndroid Build Coastguard Worker const std::vector<std::string>& nss) override; 55*5a923131SAndroid Build Coastguard Worker 56*5a923131SAndroid Build Coastguard Worker bool GetSubKeys(std::string_view ns, 57*5a923131SAndroid Build Coastguard Worker std::vector<std::string>* keys) const override; 58*5a923131SAndroid Build Coastguard Worker 59*5a923131SAndroid Build Coastguard Worker void AddObserver(std::string_view key, ObserverInterface* observer) override; 60*5a923131SAndroid Build Coastguard Worker void RemoveObserver(std::string_view key, 61*5a923131SAndroid Build Coastguard Worker ObserverInterface* observer) override; StartTransaction()62*5a923131SAndroid Build Coastguard Worker bool StartTransaction() override { return false; } CancelTransaction()63*5a923131SAndroid Build Coastguard Worker bool CancelTransaction() override { return false; } SubmitTransaction()64*5a923131SAndroid Build Coastguard Worker bool SubmitTransaction() override { return false; } 65*5a923131SAndroid Build Coastguard Worker 66*5a923131SAndroid Build Coastguard Worker private: 67*5a923131SAndroid Build Coastguard Worker enum class PrefType { 68*5a923131SAndroid Build Coastguard Worker kString, 69*5a923131SAndroid Build Coastguard Worker kInt64, 70*5a923131SAndroid Build Coastguard Worker kBool, 71*5a923131SAndroid Build Coastguard Worker }; 72*5a923131SAndroid Build Coastguard Worker struct PrefValue { 73*5a923131SAndroid Build Coastguard Worker std::string as_str; 74*5a923131SAndroid Build Coastguard Worker int64_t as_int64; 75*5a923131SAndroid Build Coastguard Worker bool as_bool; 76*5a923131SAndroid Build Coastguard Worker }; 77*5a923131SAndroid Build Coastguard Worker 78*5a923131SAndroid Build Coastguard Worker struct PrefTypeValue { 79*5a923131SAndroid Build Coastguard Worker PrefType type; 80*5a923131SAndroid Build Coastguard Worker PrefValue value; 81*5a923131SAndroid Build Coastguard Worker }; 82*5a923131SAndroid Build Coastguard Worker 83*5a923131SAndroid Build Coastguard Worker // Class to store compile-time type-dependent constants. 84*5a923131SAndroid Build Coastguard Worker template <typename T> 85*5a923131SAndroid Build Coastguard Worker class PrefConsts { 86*5a923131SAndroid Build Coastguard Worker public: 87*5a923131SAndroid Build Coastguard Worker // The PrefType associated with T. 88*5a923131SAndroid Build Coastguard Worker static FakePrefs::PrefType const type; 89*5a923131SAndroid Build Coastguard Worker 90*5a923131SAndroid Build Coastguard Worker // The data member pointer to PrefValue associated with T. 91*5a923131SAndroid Build Coastguard Worker static T FakePrefs::PrefValue::*const member; 92*5a923131SAndroid Build Coastguard Worker }; 93*5a923131SAndroid Build Coastguard Worker 94*5a923131SAndroid Build Coastguard Worker // Returns a string representation of the PrefType useful for logging. 95*5a923131SAndroid Build Coastguard Worker static std::string GetTypeName(PrefType type); 96*5a923131SAndroid Build Coastguard Worker 97*5a923131SAndroid Build Coastguard Worker // Checks that the |key| is either not present or has the given |type|. 98*5a923131SAndroid Build Coastguard Worker void CheckKeyType(std::string_view key, PrefType type) const; 99*5a923131SAndroid Build Coastguard Worker 100*5a923131SAndroid Build Coastguard Worker // Helper function to set a value of the passed |key|. It sets the type based 101*5a923131SAndroid Build Coastguard Worker // on the template parameter T. 102*5a923131SAndroid Build Coastguard Worker template <typename T> 103*5a923131SAndroid Build Coastguard Worker void SetValue(std::string_view key, T value); 104*5a923131SAndroid Build Coastguard Worker 105*5a923131SAndroid Build Coastguard Worker // Helper function to get a value from the map checking for invalid calls. 106*5a923131SAndroid Build Coastguard Worker // The function fails the test if you attempt to read a value defined as a 107*5a923131SAndroid Build Coastguard Worker // different type. Returns whether the get succeeded. 108*5a923131SAndroid Build Coastguard Worker template <typename T> 109*5a923131SAndroid Build Coastguard Worker bool GetValue(std::string_view key, T* value) const; 110*5a923131SAndroid Build Coastguard Worker 111*5a923131SAndroid Build Coastguard Worker // Container for all the key/value pairs. 112*5a923131SAndroid Build Coastguard Worker std::map<std::string, PrefTypeValue, std::less<>> values_; 113*5a923131SAndroid Build Coastguard Worker 114*5a923131SAndroid Build Coastguard Worker // The registered observers watching for changes. 115*5a923131SAndroid Build Coastguard Worker std::map<std::string, std::vector<ObserverInterface*>, std::less<>> 116*5a923131SAndroid Build Coastguard Worker observers_; 117*5a923131SAndroid Build Coastguard Worker 118*5a923131SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(FakePrefs); 119*5a923131SAndroid Build Coastguard Worker }; 120*5a923131SAndroid Build Coastguard Worker 121*5a923131SAndroid Build Coastguard Worker } // namespace chromeos_update_engine 122*5a923131SAndroid Build Coastguard Worker 123*5a923131SAndroid Build Coastguard Worker #endif // UPDATE_ENGINE_COMMON_FAKE_PREFS_H_ 124