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.
5 #ifndef CHROME_BROWSER_PREFS_PREF_HASH_FILTER_H_
6 #define CHROME_BROWSER_PREFS_PREF_HASH_FILTER_H_
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"
22 class TrackedPreferenceValidationDelegate;
25 class DictionaryValue;
30 namespace user_prefs {
31 class PrefRegistrySyncable;
32 } // namespace user_prefs
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
37 class PrefHashFilter : public InterceptablePrefFilter {
39 enum EnforcementLevel {
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,
53 struct TrackedPreferenceMetadata {
56 EnforcementLevel enforcement_level;
57 PrefTrackingStrategy strategy;
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.
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);
74 virtual ~PrefHashFilter();
76 // Registers required user preferences.
77 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
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);
83 // Clears the time of the last reset event, if any, for the provided user
85 static void ClearResetTime(PrefService* user_prefs);
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);
92 // PrefFilter remaining implementation.
93 virtual void FilterUpdate(const std::string& path) OVERRIDE;
94 virtual void FilterSerializeData(
95 base::DictionaryValue* pref_store_contents) OVERRIDE;
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;
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_;
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;
117 scoped_ptr<PrefHashStore> pref_hash_store_;
119 TrackedPreferencesMap tracked_paths_;
121 // The set of all paths whose value has changed since the last call to
122 // FilterSerializeData.
123 ChangedPathsMap changed_paths_;
125 // Whether to report the validity of the super MAC at load time (via UMA).
126 bool report_super_mac_validity_;
128 DISALLOW_COPY_AND_ASSIGN(PrefHashFilter);
131 #endif // CHROME_BROWSER_PREFS_PREF_HASH_FILTER_H_