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 void AddObserver(PrefStore::Observer
* observer
) override
;
34 void RemoveObserver(PrefStore::Observer
* observer
) override
;
35 bool HasObservers() const override
;
36 bool IsInitializationComplete() const override
;
37 bool GetValue(const std::string
& key
,
38 const base::Value
** result
) const override
;
40 // Methods of PersistentPrefStore.
41 bool GetMutableValue(const std::string
& key
, base::Value
** result
) override
;
42 void SetValue(const std::string
& key
, base::Value
* value
) override
;
43 void SetValueSilently(const std::string
& key
, base::Value
* value
) override
;
44 void RemoveValue(const std::string
& key
) override
;
45 bool ReadOnly() const override
;
46 PrefReadError
GetReadError() const override
;
47 PrefReadError
ReadPrefs() override
;
48 void ReadPrefsAsync(ReadErrorDelegate
* delegate
) override
;
49 void CommitPendingWrite() override
;
50 void ReportValueChanged(const std::string
& key
) override
;
52 // Methods of PrefStore::Observer.
53 void OnPrefValueChanged(const std::string
& key
) override
;
54 void OnInitializationCompleted(bool succeeded
) override
;
56 void RegisterOverlayPref(const std::string
& key
);
57 void RegisterOverlayPref(const std::string
& overlay_key
,
58 const std::string
& underlay_key
);
61 ~OverlayUserPrefStore() override
;
64 typedef std::map
<std::string
, std::string
> NamesMap
;
66 const std::string
& GetOverlayKey(const std::string
& underlay_key
) const;
67 const std::string
& GetUnderlayKey(const std::string
& overlay_key
) const;
69 // Returns true if |key| corresponds to a preference that shall be stored in
70 // an in-memory PrefStore that is not persisted to disk.
71 bool ShallBeStoredInOverlay(const std::string
& key
) const;
73 ObserverList
<PrefStore::Observer
, true> observers_
;
74 PrefValueMap overlay_
;
75 scoped_refptr
<PersistentPrefStore
> underlay_
;
76 NamesMap overlay_to_underlay_names_map_
;
77 NamesMap underlay_to_overlay_names_map_
;
79 DISALLOW_COPY_AND_ASSIGN(OverlayUserPrefStore
);
82 #endif // BASE_PREFS_OVERLAY_USER_PREF_STORE_H_