Upstream version 5.34.104.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
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"
20
21 class PrefStore;
22
23 namespace base {
24 class DictionaryValue;
25 class Value;
26 }  // namespace base
27
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
30 // are changed.
31 class PrefHashFilter : public PrefFilter {
32  public:
33   enum EnforcementLevel {
34     NO_ENFORCEMENT,
35     ENFORCE_ON_LOAD
36   };
37
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,
45   };
46
47   struct TrackedPreferenceMetadata {
48     size_t reporting_id;
49     const char* name;
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;
54   };
55
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);
68
69   virtual ~PrefHashFilter();
70
71   // Initializes the PrefHashStore with hashes of the tracked preferences in
72   // |pref_store|.
73   void Initialize(const PrefStore& pref_store);
74
75   // PrefFilter implementation.
76   virtual void FilterOnLoad(base::DictionaryValue* pref_store_contents)
77       OVERRIDE;
78   virtual void FilterUpdate(const std::string& path) OVERRIDE;
79   virtual void FilterSerializeData(
80       const base::DictionaryValue* pref_store_contents) OVERRIDE;
81
82  private:
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;
90
91   scoped_ptr<PrefHashStore> pref_hash_store_;
92
93   TrackedPreferencesMap tracked_paths_;
94
95   // The set of all paths whose value has changed since the last call to
96   // FilterSerializeData.
97   ChangedPathsMap changed_paths_;
98
99   base::Closure reset_callback_;
100
101   DISALLOW_COPY_AND_ASSIGN(PrefHashFilter);
102 };
103
104 #endif  // CHROME_BROWSER_PREFS_PREF_HASH_FILTER_H_