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_
13 #include "base/basictypes.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"
21 class PersistentPrefStore;
26 class DictionaryValue;
31 namespace user_prefs {
32 class PrefRegistrySyncable;
33 } // namespace user_prefs
35 // Intercepts preference values as they are loaded from disk and verifies them
36 // using a PrefHashStore. Keeps the PrefHashStore contents up to date as values
38 class PrefHashFilter : public PrefFilter {
40 enum EnforcementLevel {
45 enum PrefTrackingStrategy {
46 // Atomic preferences are tracked as a whole.
47 TRACKING_STRATEGY_ATOMIC,
48 // Split preferences are dictionaries for which each top-level entry is
49 // tracked independently. Note: preferences using this strategy must be kept
50 // in sync with TrackedSplitPreferences in histograms.xml.
51 TRACKING_STRATEGY_SPLIT,
54 struct TrackedPreferenceMetadata {
57 EnforcementLevel enforcement_level;
58 PrefTrackingStrategy strategy;
61 // Constructs a PrefHashFilter tracking the specified |tracked_preferences|
62 // using |pref_hash_store| to check/store hashes.
63 // |reporting_ids_count| is the count of all possible IDs (possibly greater
64 // than |tracked_preferences.size()|).
66 scoped_ptr<PrefHashStore> pref_hash_store,
67 const std::vector<TrackedPreferenceMetadata>& tracked_preferences,
68 size_t reporting_ids_count);
70 virtual ~PrefHashFilter();
72 // Registers required user preferences.
73 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
75 // Retrieves the time of the last reset event, if any, for the provided user
76 // preferences. If no reset has occurred, Returns a null |Time|.
77 static base::Time GetResetTime(PrefService* user_prefs);
79 // Clears the time of the last reset event, if any, for the provided user
81 static void ClearResetTime(PrefService* user_prefs);
83 // Initializes the PrefHashStore with hashes of the tracked preferences in
85 void Initialize(const PrefStore& pref_store);
87 // Migrates protected values from |source| to |destination|. Values are
88 // migrated if they are protected according to this filter's configuration,
89 // the corresponding key has no value in |destination|, and the value in
90 // |source| is trusted according to this filter's PrefHashStore. Regardless of
91 // the state of |destination| or the trust status, the protected values will
92 // be removed from |source|.
93 void MigrateValues(PersistentPrefStore* source,
94 PersistentPrefStore* destination);
96 // PrefFilter implementation.
97 virtual void FilterOnLoad(base::DictionaryValue* pref_store_contents)
99 virtual void FilterUpdate(const std::string& path) OVERRIDE;
100 virtual void FilterSerializeData(
101 const base::DictionaryValue* pref_store_contents) OVERRIDE;
104 // A map of paths to TrackedPreferences; this map owns this individual
105 // TrackedPreference objects.
106 typedef base::ScopedPtrHashMap<std::string, TrackedPreference>
107 TrackedPreferencesMap;
108 // A map from changed paths to their corresponding TrackedPreferences (which
109 // aren't owned by this map).
110 typedef std::map<std::string, const TrackedPreference*> ChangedPathsMap;
112 scoped_ptr<PrefHashStore> pref_hash_store_;
114 TrackedPreferencesMap tracked_paths_;
116 // The set of all paths whose value has changed since the last call to
117 // FilterSerializeData.
118 ChangedPathsMap changed_paths_;
120 DISALLOW_COPY_AND_ASSIGN(PrefHashFilter);
123 #endif // CHROME_BROWSER_PREFS_PREF_HASH_FILTER_H_