| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_PREFS_OVERLAY_USER_PREF_STORE_H_ | 5 // TODO(brettw) remove this forwarding header when prefs is completely moved to |
| 6 #define BASE_PREFS_OVERLAY_USER_PREF_STORE_H_ | 6 // components. |
| 7 | 7 #include "components/prefs/overlay_user_pref_store.h" |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/observer_list.h" | |
| 16 #include "base/prefs/base_prefs_export.h" | |
| 17 #include "base/prefs/persistent_pref_store.h" | |
| 18 #include "base/prefs/pref_value_map.h" | |
| 19 | |
| 20 // PersistentPrefStore that directs all write operations into an in-memory | |
| 21 // PrefValueMap. Read operations are first answered by the PrefValueMap. | |
| 22 // If the PrefValueMap does not contain a value for the requested key, | |
| 23 // the look-up is passed on to an underlying PersistentPrefStore |underlay_|. | |
| 24 class BASE_PREFS_EXPORT OverlayUserPrefStore : public PersistentPrefStore, | |
| 25 public PrefStore::Observer { | |
| 26 public: | |
| 27 explicit OverlayUserPrefStore(PersistentPrefStore* underlay); | |
| 28 | |
| 29 // Returns true if a value has been set for the |key| in this | |
| 30 // OverlayUserPrefStore, i.e. if it potentially overrides a value | |
| 31 // from the |underlay_|. | |
| 32 virtual bool IsSetInOverlay(const std::string& key) const; | |
| 33 | |
| 34 // Methods of PrefStore. | |
| 35 void AddObserver(PrefStore::Observer* observer) override; | |
| 36 void RemoveObserver(PrefStore::Observer* observer) override; | |
| 37 bool HasObservers() const override; | |
| 38 bool IsInitializationComplete() const override; | |
| 39 bool GetValue(const std::string& key, | |
| 40 const base::Value** result) const override; | |
| 41 | |
| 42 // Methods of PersistentPrefStore. | |
| 43 bool GetMutableValue(const std::string& key, base::Value** result) override; | |
| 44 void SetValue(const std::string& key, | |
| 45 scoped_ptr<base::Value> value, | |
| 46 uint32_t flags) override; | |
| 47 void SetValueSilently(const std::string& key, | |
| 48 scoped_ptr<base::Value> value, | |
| 49 uint32_t flags) override; | |
| 50 void RemoveValue(const std::string& key, uint32_t flags) override; | |
| 51 bool ReadOnly() const override; | |
| 52 PrefReadError GetReadError() const override; | |
| 53 PrefReadError ReadPrefs() override; | |
| 54 void ReadPrefsAsync(ReadErrorDelegate* delegate) override; | |
| 55 void CommitPendingWrite() override; | |
| 56 void SchedulePendingLossyWrites() override; | |
| 57 void ReportValueChanged(const std::string& key, uint32_t flags) override; | |
| 58 | |
| 59 // Methods of PrefStore::Observer. | |
| 60 void OnPrefValueChanged(const std::string& key) override; | |
| 61 void OnInitializationCompleted(bool succeeded) override; | |
| 62 | |
| 63 void RegisterOverlayPref(const std::string& key); | |
| 64 void RegisterOverlayPref(const std::string& overlay_key, | |
| 65 const std::string& underlay_key); | |
| 66 | |
| 67 protected: | |
| 68 ~OverlayUserPrefStore() override; | |
| 69 | |
| 70 private: | |
| 71 typedef std::map<std::string, std::string> NamesMap; | |
| 72 | |
| 73 const std::string& GetOverlayKey(const std::string& underlay_key) const; | |
| 74 const std::string& GetUnderlayKey(const std::string& overlay_key) const; | |
| 75 | |
| 76 // Returns true if |key| corresponds to a preference that shall be stored in | |
| 77 // an in-memory PrefStore that is not persisted to disk. | |
| 78 bool ShallBeStoredInOverlay(const std::string& key) const; | |
| 79 | |
| 80 base::ObserverList<PrefStore::Observer, true> observers_; | |
| 81 PrefValueMap overlay_; | |
| 82 scoped_refptr<PersistentPrefStore> underlay_; | |
| 83 NamesMap overlay_to_underlay_names_map_; | |
| 84 NamesMap underlay_to_overlay_names_map_; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(OverlayUserPrefStore); | |
| 87 }; | |
| 88 | |
| 89 #endif // BASE_PREFS_OVERLAY_USER_PREF_STORE_H_ | |
| OLD | NEW |