prefs.h revision 39910dcd1d68987ccee7c3031dc269233a8490bb
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// 16 17#ifndef UPDATE_ENGINE_COMMON_PREFS_H_ 18#define UPDATE_ENGINE_COMMON_PREFS_H_ 19 20#include <map> 21#include <string> 22#include <vector> 23 24#include <base/files/file_path.h> 25 26#include "gtest/gtest_prod.h" // for FRIEND_TEST 27#include "update_engine/common/prefs_interface.h" 28 29namespace chromeos_update_engine { 30 31// Implements a preference store by storing the value associated with 32// a key in a separate file named after the key under a preference 33// store directory. 34 35class Prefs : public PrefsInterface { 36 public: 37 Prefs() = default; 38 39 // Initializes the store by associating this object with |prefs_dir| 40 // as the preference store directory. Returns true on success, false 41 // otherwise. 42 bool Init(const base::FilePath& prefs_dir); 43 44 // PrefsInterface methods. 45 bool GetString(const std::string& key, std::string* value) const override; 46 bool SetString(const std::string& key, const std::string& value) override; 47 bool GetInt64(const std::string& key, int64_t* value) const override; 48 bool SetInt64(const std::string& key, const int64_t value) override; 49 bool GetBoolean(const std::string& key, bool* value) const override; 50 bool SetBoolean(const std::string& key, const bool value) override; 51 52 bool Exists(const std::string& key) const override; 53 bool Delete(const std::string& key) override; 54 55 void AddObserver(const std::string& key, 56 ObserverInterface* observer) override; 57 void RemoveObserver(const std::string& key, 58 ObserverInterface* observer) override; 59 60 private: 61 FRIEND_TEST(PrefsTest, GetFileNameForKey); 62 FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter); 63 FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty); 64 65 // Sets |filename| to the full path to the file containing the data 66 // associated with |key|. Returns true on success, false otherwise. 67 bool GetFileNameForKey(const std::string& key, 68 base::FilePath* filename) const; 69 70 // Preference store directory. 71 base::FilePath prefs_dir_; 72 73 // The registered observers watching for changes. 74 std::map<std::string, std::vector<ObserverInterface*>> observers_; 75 76 DISALLOW_COPY_AND_ASSIGN(Prefs); 77}; 78 79} // namespace chromeos_update_engine 80 81#endif // UPDATE_ENGINE_COMMON_PREFS_H_ 82