f3945571cb9362d4b49ccc99f7c0d5d6bff8387f
[platform/framework/web/crosswalk.git] / src / chrome / browser / prefs / pref_hash_filter.h
1 // Copyright 2013 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 CHROME_BROWSER_PREFS_PREF_HASH_FILTER_H_
6 #define CHROME_BROWSER_PREFS_PREF_HASH_FILTER_H_
7
8 #include <map>
9 #include <set>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/containers/scoped_ptr_hash_map.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "chrome/browser/prefs/interceptable_pref_filter.h"
17 #include "chrome/browser/prefs/tracked/tracked_preference.h"
18
19 class PrefHashStore;
20 class PrefService;
21 class PrefStore;
22 class TrackedPreferenceValidationDelegate;
23
24 namespace base {
25 class DictionaryValue;
26 class Time;
27 class Value;
28 }  // namespace base
29
30 namespace user_prefs {
31 class PrefRegistrySyncable;
32 }  // namespace user_prefs
33
34 // Intercepts preference values as they are loaded from disk and verifies them
35 // using a PrefHashStore. Keeps the PrefHashStore contents up to date as values
36 // are changed.
37 class PrefHashFilter : public InterceptablePrefFilter {
38  public:
39   enum EnforcementLevel {
40     NO_ENFORCEMENT,
41     ENFORCE_ON_LOAD
42   };
43
44   enum PrefTrackingStrategy {
45     // Atomic preferences are tracked as a whole.
46     TRACKING_STRATEGY_ATOMIC,
47     // Split preferences are dictionaries for which each top-level entry is
48     // tracked independently. Note: preferences using this strategy must be kept
49     // in sync with TrackedSplitPreferences in histograms.xml.
50     TRACKING_STRATEGY_SPLIT,
51   };
52
53   struct TrackedPreferenceMetadata {
54     size_t reporting_id;
55     const char* name;
56     EnforcementLevel enforcement_level;
57     PrefTrackingStrategy strategy;
58   };
59
60   // Constructs a PrefHashFilter tracking the specified |tracked_preferences|
61   // using |pref_hash_store| to check/store hashes. An optional |delegate| is
62   // notified of the status of each preference as it is checked.
63   // |reporting_ids_count| is the count of all possible IDs (possibly greater
64   // than |tracked_preferences.size()|). If |report_super_mac_validity| is true,
65   // the state of the super MAC will be reported via UMA during
66   // FinalizeFilterOnLoad.
67   PrefHashFilter(
68       scoped_ptr<PrefHashStore> pref_hash_store,
69       const std::vector<TrackedPreferenceMetadata>& tracked_preferences,
70       TrackedPreferenceValidationDelegate* delegate,
71       size_t reporting_ids_count,
72       bool report_super_mac_validity);
73
74   virtual ~PrefHashFilter();
75
76   // Registers required user preferences.
77   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
78
79   // Retrieves the time of the last reset event, if any, for the provided user
80   // preferences. If no reset has occurred, Returns a null |Time|.
81   static base::Time GetResetTime(PrefService* user_prefs);
82
83   // Clears the time of the last reset event, if any, for the provided user
84   // preferences.
85   static void ClearResetTime(PrefService* user_prefs);
86
87   // Initializes the PrefHashStore with hashes of the tracked preferences in
88   // |pref_store_contents|. |pref_store_contents| will be the |storage| passed
89   // to PrefHashStore::BeginTransaction().
90   void Initialize(base::DictionaryValue* pref_store_contents);
91
92   // PrefFilter remaining implementation.
93   virtual void FilterUpdate(const std::string& path) OVERRIDE;
94   virtual void FilterSerializeData(
95       base::DictionaryValue* pref_store_contents) OVERRIDE;
96
97  private:
98   // InterceptablePrefFilter implementation.
99   virtual void FinalizeFilterOnLoad(
100       const PostFilterOnLoadCallback& post_filter_on_load_callback,
101       scoped_ptr<base::DictionaryValue> pref_store_contents,
102       bool prefs_altered) OVERRIDE;
103
104   // Callback to be invoked only once (and subsequently reset) on the next
105   // FilterOnLoad event. It will be allowed to modify the |prefs| handed to
106   // FilterOnLoad before handing them back to this PrefHashFilter.
107   FilterOnLoadInterceptor filter_on_load_interceptor_;
108
109   // A map of paths to TrackedPreferences; this map owns this individual
110   // TrackedPreference objects.
111   typedef base::ScopedPtrHashMap<std::string, TrackedPreference>
112       TrackedPreferencesMap;
113   // A map from changed paths to their corresponding TrackedPreferences (which
114   // aren't owned by this map).
115   typedef std::map<std::string, const TrackedPreference*> ChangedPathsMap;
116
117   scoped_ptr<PrefHashStore> pref_hash_store_;
118
119   TrackedPreferencesMap tracked_paths_;
120
121   // The set of all paths whose value has changed since the last call to
122   // FilterSerializeData.
123   ChangedPathsMap changed_paths_;
124
125   // Whether to report the validity of the super MAC at load time (via UMA).
126   bool report_super_mac_validity_;
127
128   DISALLOW_COPY_AND_ASSIGN(PrefHashFilter);
129 };
130
131 #endif  // CHROME_BROWSER_PREFS_PREF_HASH_FILTER_H_