Home | History | Annotate | Download | only in common
      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 
     29 namespace chromeos_update_engine {
     30 
     31 // Implements a preference store by storing the value associated with a key
     32 // in a given storage passed during construction.
     33 class PrefsBase : public PrefsInterface {
     34  public:
     35   // Storage interface used to set and retrieve keys.
     36   class StorageInterface {
     37    public:
     38     StorageInterface() = default;
     39     virtual ~StorageInterface() = default;
     40 
     41     // Get the key named |key| and store its value in the referenced |value|.
     42     // Returns whether the operation succeeded.
     43     virtual bool GetKey(const std::string& key, std::string* value) const = 0;
     44 
     45     // Set the value of the key named |key| to |value| regardless of the
     46     // previous value. Returns whether the operation succeeded.
     47     virtual bool SetKey(const std::string& key, const std::string& value) = 0;
     48 
     49     // Returns whether the key named |key| exists.
     50     virtual bool KeyExists(const std::string& key) const = 0;
     51 
     52     // Deletes the value associated with the key name |key|. Returns whether the
     53     // key was deleted.
     54     virtual bool DeleteKey(const std::string& key) = 0;
     55 
     56    private:
     57     DISALLOW_COPY_AND_ASSIGN(StorageInterface);
     58   };
     59 
     60   explicit PrefsBase(StorageInterface* storage) : storage_(storage) {}
     61 
     62   // PrefsInterface methods.
     63   bool GetString(const std::string& key, std::string* value) const override;
     64   bool SetString(const std::string& key, const std::string& value) override;
     65   bool GetInt64(const std::string& key, int64_t* value) const override;
     66   bool SetInt64(const std::string& key, const int64_t value) override;
     67   bool GetBoolean(const std::string& key, bool* value) const override;
     68   bool SetBoolean(const std::string& key, const bool value) override;
     69 
     70   bool Exists(const std::string& key) const override;
     71   bool Delete(const std::string& key) override;
     72 
     73   void AddObserver(const std::string& key,
     74                    ObserverInterface* observer) override;
     75   void RemoveObserver(const std::string& key,
     76                       ObserverInterface* observer) override;
     77 
     78  private:
     79   // The registered observers watching for changes.
     80   std::map<std::string, std::vector<ObserverInterface*>> observers_;
     81 
     82   // The concrete implementation of the storage used for the keys.
     83   StorageInterface* storage_;
     84 
     85   DISALLOW_COPY_AND_ASSIGN(PrefsBase);
     86 };
     87 
     88 // Implements a preference store by storing the value associated with
     89 // a key in a separate file named after the key under a preference
     90 // store directory.
     91 
     92 class Prefs : public PrefsBase {
     93  public:
     94   Prefs() : PrefsBase(&file_storage_) {}
     95 
     96   // Initializes the store by associating this object with |prefs_dir|
     97   // as the preference store directory. Returns true on success, false
     98   // otherwise.
     99   bool Init(const base::FilePath& prefs_dir);
    100 
    101  private:
    102   FRIEND_TEST(PrefsTest, GetFileNameForKey);
    103   FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter);
    104   FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty);
    105 
    106   class FileStorage : public PrefsBase::StorageInterface {
    107    public:
    108     FileStorage() = default;
    109 
    110     bool Init(const base::FilePath& prefs_dir);
    111 
    112     // PrefsBase::StorageInterface overrides.
    113     bool GetKey(const std::string& key, std::string* value) const override;
    114     bool SetKey(const std::string& key, const std::string& value) override;
    115     bool KeyExists(const std::string& key) const override;
    116     bool DeleteKey(const std::string& key) override;
    117 
    118    private:
    119     FRIEND_TEST(PrefsTest, GetFileNameForKey);
    120     FRIEND_TEST(PrefsTest, GetFileNameForKeyBadCharacter);
    121     FRIEND_TEST(PrefsTest, GetFileNameForKeyEmpty);
    122 
    123     // Sets |filename| to the full path to the file containing the data
    124     // associated with |key|. Returns true on success, false otherwise.
    125     bool GetFileNameForKey(const std::string& key,
    126                            base::FilePath* filename) const;
    127 
    128     // Preference store directory.
    129     base::FilePath prefs_dir_;
    130   };
    131 
    132   // The concrete file storage implementation.
    133   FileStorage file_storage_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(Prefs);
    136 };
    137 
    138 // Implements a preference store in memory. The stored values are lost when the
    139 // object is destroyed.
    140 
    141 class MemoryPrefs : public PrefsBase {
    142  public:
    143   MemoryPrefs() : PrefsBase(&mem_storage_) {}
    144 
    145  private:
    146   class MemoryStorage : public PrefsBase::StorageInterface {
    147    public:
    148     MemoryStorage() = default;
    149 
    150     // PrefsBase::StorageInterface overrides.
    151     bool GetKey(const std::string& key, std::string* value) const override;
    152     bool SetKey(const std::string& key, const std::string& value) override;
    153     bool KeyExists(const std::string& key) const override;
    154     bool DeleteKey(const std::string& key) override;
    155 
    156    private:
    157     // The std::map holding the values in memory.
    158     std::map<std::string, std::string> values_;
    159   };
    160 
    161   // The concrete memory storage implementation.
    162   MemoryStorage mem_storage_;
    163 
    164   DISALLOW_COPY_AND_ASSIGN(MemoryPrefs);
    165 };
    166 }  // namespace chromeos_update_engine
    167 
    168 #endif  // UPDATE_ENGINE_COMMON_PREFS_H_
    169