- add sources.
[platform/framework/web/crosswalk.git] / src / base / prefs / overlay_user_pref_store.h
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.
4
5 #ifndef BASE_PREFS_OVERLAY_USER_PREF_STORE_H_
6 #define BASE_PREFS_OVERLAY_USER_PREF_STORE_H_
7
8 #include <map>
9 #include <string>
10
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"
17
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 {
24  public:
25   explicit OverlayUserPrefStore(PersistentPrefStore* underlay);
26
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;
31
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;
39
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 void MarkNeedsEmptyValue(const std::string& key) OVERRIDE;
48   virtual bool ReadOnly() const OVERRIDE;
49   virtual PrefReadError GetReadError() const OVERRIDE;
50   virtual PrefReadError ReadPrefs() OVERRIDE;
51   virtual void ReadPrefsAsync(ReadErrorDelegate* delegate) OVERRIDE;
52   virtual void CommitPendingWrite() OVERRIDE;
53   virtual void ReportValueChanged(const std::string& key) OVERRIDE;
54
55   // Methods of PrefStore::Observer.
56   virtual void OnPrefValueChanged(const std::string& key) OVERRIDE;
57   virtual void OnInitializationCompleted(bool succeeded) OVERRIDE;
58
59   void RegisterOverlayPref(const std::string& key);
60   void RegisterOverlayPref(const std::string& overlay_key,
61                            const std::string& underlay_key);
62
63  protected:
64   virtual ~OverlayUserPrefStore();
65
66  private:
67   typedef std::map<std::string, std::string> NamesMap;
68
69   const std::string& GetOverlayKey(const std::string& underlay_key) const;
70   const std::string& GetUnderlayKey(const std::string& overlay_key) const;
71
72   // Returns true if |key| corresponds to a preference that shall be stored in
73   // an in-memory PrefStore that is not persisted to disk.
74   bool ShallBeStoredInOverlay(const std::string& key) const;
75
76   ObserverList<PrefStore::Observer, true> observers_;
77   PrefValueMap overlay_;
78   scoped_refptr<PersistentPrefStore> underlay_;
79   NamesMap overlay_to_underlay_names_map_;
80   NamesMap underlay_to_overlay_names_map_;
81
82   DISALLOW_COPY_AND_ASSIGN(OverlayUserPrefStore);
83 };
84
85 #endif  // BASE_PREFS_OVERLAY_USER_PREF_STORE_H_