Upstream version 7.35.139.0
[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 <string>
11 #include <vector>
12
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"
20
21 class PersistentPrefStore;
22 class PrefService;
23 class PrefStore;
24
25 namespace base {
26 class DictionaryValue;
27 class Time;
28 class Value;
29 }  // namespace base
30
31 namespace user_prefs {
32 class PrefRegistrySyncable;
33 }  // namespace user_prefs
34
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
37 // are changed.
38 class PrefHashFilter : public PrefFilter {
39  public:
40   enum EnforcementLevel {
41     NO_ENFORCEMENT,
42     ENFORCE_ON_LOAD
43   };
44
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,
52   };
53
54   struct TrackedPreferenceMetadata {
55     size_t reporting_id;
56     const char* name;
57     EnforcementLevel enforcement_level;
58     PrefTrackingStrategy strategy;
59   };
60
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()|).
65   PrefHashFilter(
66       scoped_ptr<PrefHashStore> pref_hash_store,
67       const std::vector<TrackedPreferenceMetadata>& tracked_preferences,
68       size_t reporting_ids_count);
69
70   virtual ~PrefHashFilter();
71
72   // Registers required user preferences.
73   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
74
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);
78
79   // Clears the time of the last reset event, if any, for the provided user
80   // preferences.
81   static void ClearResetTime(PrefService* user_prefs);
82
83   // Initializes the PrefHashStore with hashes of the tracked preferences in
84   // |pref_store|.
85   void Initialize(const PrefStore& pref_store);
86
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);
95
96   // PrefFilter implementation.
97   virtual void FilterOnLoad(base::DictionaryValue* pref_store_contents)
98       OVERRIDE;
99   virtual void FilterUpdate(const std::string& path) OVERRIDE;
100   virtual void FilterSerializeData(
101       const base::DictionaryValue* pref_store_contents) OVERRIDE;
102
103  private:
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;
111
112   scoped_ptr<PrefHashStore> pref_hash_store_;
113
114   TrackedPreferencesMap tracked_paths_;
115
116   // The set of all paths whose value has changed since the last call to
117   // FilterSerializeData.
118   ChangedPathsMap changed_paths_;
119
120   DISALLOW_COPY_AND_ASSIGN(PrefHashFilter);
121 };
122
123 #endif  // CHROME_BROWSER_PREFS_PREF_HASH_FILTER_H_