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
3 // found in the LICENSE file.
5 #ifndef BASE_PREFS_OVERLAY_USER_PREF_STORE_H_
6 #define BASE_PREFS_OVERLAY_USER_PREF_STORE_H_
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/observer_list.h"
14 #include "base/prefs/base_prefs_export.h"
15 #include "base/prefs/persistent_pref_store.h"
16 #include "base/prefs/pref_value_map.h"
18 // PersistentPrefStore that directs all write operations into an in-memory
19 // PrefValueMap. Read operations are first answered by the PrefValueMap.
20 // If the PrefValueMap does not contain a value for the requested key,
21 // the look-up is passed on to an underlying PersistentPrefStore |underlay_|.
22 class BASE_PREFS_EXPORT OverlayUserPrefStore
: public PersistentPrefStore
,
23 public PrefStore::Observer
{
25 explicit OverlayUserPrefStore(PersistentPrefStore
* underlay
);
27 // Returns true if a value has been set for the |key| in this
28 // OverlayUserPrefStore, i.e. if it potentially overrides a value
29 // from the |underlay_|.
30 virtual bool IsSetInOverlay(const std::string
& key
) const;
32 // Methods of PrefStore.
33 virtual void AddObserver(PrefStore::Observer
* observer
) OVERRIDE
;
34 virtual void RemoveObserver(PrefStore::Observer
* observer
) OVERRIDE
;
35 virtual bool HasObservers() const OVERRIDE
;
36 virtual bool IsInitializationComplete() const OVERRIDE
;
37 virtual bool GetValue(const std::string
& key
,
38 const base::Value
** result
) const OVERRIDE
;
40 // Methods of PersistentPrefStore.
41 virtual bool GetMutableValue(const std::string
& key
,
42 base::Value
** result
) OVERRIDE
;
43 virtual void SetValue(const std::string
& key
, base::Value
* value
) OVERRIDE
;
44 virtual void SetValueSilently(const std::string
& key
,
45 base::Value
* value
) OVERRIDE
;
46 virtual void RemoveValue(const std::string
& key
) OVERRIDE
;
47 virtual bool ReadOnly() const OVERRIDE
;
48 virtual PrefReadError
GetReadError() const OVERRIDE
;
49 virtual PrefReadError
ReadPrefs() OVERRIDE
;
50 virtual void ReadPrefsAsync(ReadErrorDelegate
* delegate
) OVERRIDE
;
51 virtual void CommitPendingWrite() OVERRIDE
;
52 virtual void ReportValueChanged(const std::string
& key
) OVERRIDE
;
54 // Methods of PrefStore::Observer.
55 virtual void OnPrefValueChanged(const std::string
& key
) OVERRIDE
;
56 virtual void OnInitializationCompleted(bool succeeded
) OVERRIDE
;
58 void RegisterOverlayPref(const std::string
& key
);
59 void RegisterOverlayPref(const std::string
& overlay_key
,
60 const std::string
& underlay_key
);
63 virtual ~OverlayUserPrefStore();
66 typedef std::map
<std::string
, std::string
> NamesMap
;
68 const std::string
& GetOverlayKey(const std::string
& underlay_key
) const;
69 const std::string
& GetUnderlayKey(const std::string
& overlay_key
) const;
71 // Returns true if |key| corresponds to a preference that shall be stored in
72 // an in-memory PrefStore that is not persisted to disk.
73 bool ShallBeStoredInOverlay(const std::string
& key
) const;
75 ObserverList
<PrefStore::Observer
, true> observers_
;
76 PrefValueMap overlay_
;
77 scoped_refptr
<PersistentPrefStore
> underlay_
;
78 NamesMap overlay_to_underlay_names_map_
;
79 NamesMap underlay_to_overlay_names_map_
;
81 DISALLOW_COPY_AND_ASSIGN(OverlayUserPrefStore
);
84 #endif // BASE_PREFS_OVERLAY_USER_PREF_STORE_H_