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/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/containers/scoped_ptr_hash_map.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/prefs/pref_filter.h"
18 #include "chrome/browser/prefs/pref_hash_store.h"
19 #include "chrome/browser/prefs/tracked/tracked_preference.h"
24 class DictionaryValue;
28 // Intercepts preference values as they are loaded from disk and verifies them
29 // using a PrefHashStore. Keeps the PrefHashStore contents up to date as values
31 class PrefHashFilter : public PrefFilter {
33 enum EnforcementLevel {
38 enum PrefTrackingStrategy {
39 // Atomic preferences are tracked as a whole.
40 TRACKING_STRATEGY_ATOMIC,
41 // Split preferences are dictionaries for which each top-level entry is
42 // tracked independently. Note: preferences using this strategy must be kept
43 // in sync with TrackedSplitPreferences in histograms.xml.
44 TRACKING_STRATEGY_SPLIT,
47 struct TrackedPreferenceMetadata {
50 // This preference will not be enforced above this level no matter what the
51 // |enforcement_level| is set to.
52 EnforcementLevel max_enforcement_level;
53 PrefTrackingStrategy strategy;
56 // Constructs a PrefHashFilter tracking the specified |tracked_preferences|
57 // using |pref_hash_store| to check/store hashes.
58 // |reporting_ids_count| is the count of all possible IDs (possibly greater
59 // than |tracked_preferences_size|). |enforcement_level| determines when this
60 // filter will enforce factory defaults upon detecting an untrusted preference
61 // value. |reset_callback| is called when a reset event occurs.
62 PrefHashFilter(scoped_ptr<PrefHashStore> pref_hash_store,
63 const TrackedPreferenceMetadata tracked_preferences[],
64 size_t tracked_preferences_size,
65 size_t reporting_ids_count,
66 EnforcementLevel enforcement_level,
67 const base::Closure& reset_callback);
69 virtual ~PrefHashFilter();
71 // Initializes the PrefHashStore with hashes of the tracked preferences in
73 void Initialize(const PrefStore& pref_store);
75 // PrefFilter implementation.
76 virtual void FilterOnLoad(base::DictionaryValue* pref_store_contents)
78 virtual void FilterUpdate(const std::string& path) OVERRIDE;
79 virtual void FilterSerializeData(
80 const base::DictionaryValue* pref_store_contents) OVERRIDE;
83 // A map of paths to TrackedPreferences; this map owns this individual
84 // TrackedPreference objects.
85 typedef base::ScopedPtrHashMap<std::string, TrackedPreference>
86 TrackedPreferencesMap;
87 // A map from changed paths to their corresponding TrackedPreferences (which
88 // aren't owned by this map).
89 typedef std::map<std::string, const TrackedPreference*> ChangedPathsMap;
91 scoped_ptr<PrefHashStore> pref_hash_store_;
93 TrackedPreferencesMap tracked_paths_;
95 // The set of all paths whose value has changed since the last call to
96 // FilterSerializeData.
97 ChangedPathsMap changed_paths_;
99 base::Closure reset_callback_;
101 DISALLOW_COPY_AND_ASSIGN(PrefHashFilter);
104 #endif // CHROME_BROWSER_PREFS_PREF_HASH_FILTER_H_