summaryrefslogtreecommitdiff
path: root/chromium/components/user_prefs/tracked/pref_hash_filter.h
blob: 07c92db9506180b5c7da529ba8dd5f7201c2ff21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_USER_PREFS_TRACKED_PREF_HASH_FILTER_H_
#define COMPONENTS_USER_PREFS_TRACKED_PREF_HASH_FILTER_H_

#include <stddef.h>

#include <map>
#include <memory>
#include <set>
#include <vector>

#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/containers/scoped_ptr_hash_map.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/optional.h"
#include "components/user_prefs/tracked/hash_store_contents.h"
#include "components/user_prefs/tracked/interceptable_pref_filter.h"
#include "components/user_prefs/tracked/tracked_preference.h"

class PrefHashStore;
class PrefService;
class PrefStore;
class TrackedPreferenceValidationDelegate;

namespace base {
class DictionaryValue;
class Time;
class Value;
}  // namespace base

namespace user_prefs {
class PrefRegistrySyncable;
}  // namespace user_prefs

// Intercepts preference values as they are loaded from disk and verifies them
// using a PrefHashStore. Keeps the PrefHashStore contents up to date as values
// are changed.
class PrefHashFilter : public InterceptablePrefFilter {
 public:
  enum EnforcementLevel { NO_ENFORCEMENT, ENFORCE_ON_LOAD };

  enum PrefTrackingStrategy {
    // Atomic preferences are tracked as a whole.
    TRACKING_STRATEGY_ATOMIC,
    // Split preferences are dictionaries for which each top-level entry is
    // tracked independently. Note: preferences using this strategy must be kept
    // in sync with TrackedSplitPreferences in histograms.xml.
    TRACKING_STRATEGY_SPLIT,
  };

  enum ValueType {
    VALUE_IMPERSONAL,
    // The preference value may contain personal information.
    VALUE_PERSONAL,
  };

  struct TrackedPreferenceMetadata {
    size_t reporting_id;
    const char* name;
    EnforcementLevel enforcement_level;
    PrefTrackingStrategy strategy;
    ValueType value_type;
  };

  using StoreContentsPair = std::pair<std::unique_ptr<PrefHashStore>,
                                      std::unique_ptr<HashStoreContents>>;

  // Constructs a PrefHashFilter tracking the specified |tracked_preferences|
  // using |pref_hash_store| to check/store hashes. An optional |delegate| is
  // notified of the status of each preference as it is checked.
  // If |on_reset_on_load| is provided, it will be invoked if a reset occurs in
  // FilterOnLoad.
  // |reporting_ids_count| is the count of all possible IDs (possibly greater
  // than |tracked_preferences.size()|). If |report_super_mac_validity| is true,
  // the state of the super MAC will be reported via UMA during
  // FinalizeFilterOnLoad.
  // |external_validation_hash_store_pair_| will be used (if non-null) to
  // perform extra validations without triggering resets.
  PrefHashFilter(
      std::unique_ptr<PrefHashStore> pref_hash_store,
      StoreContentsPair external_validation_hash_store_pair_,
      const std::vector<TrackedPreferenceMetadata>& tracked_preferences,
      const base::Closure& on_reset_on_load,
      TrackedPreferenceValidationDelegate* delegate,
      size_t reporting_ids_count,
      bool report_super_mac_validity);

  ~PrefHashFilter() override;

  // Registers required user preferences.
  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

  // Retrieves the time of the last reset event, if any, for the provided user
  // preferences. If no reset has occurred, Returns a null |Time|.
  static base::Time GetResetTime(PrefService* user_prefs);

  // Clears the time of the last reset event, if any, for the provided user
  // preferences.
  static void ClearResetTime(PrefService* user_prefs);

  // Initializes the PrefHashStore with hashes of the tracked preferences in
  // |pref_store_contents|. |pref_store_contents| will be the |storage| passed
  // to PrefHashStore::BeginTransaction().
  void Initialize(base::DictionaryValue* pref_store_contents);

  // PrefFilter remaining implementation.
  void FilterUpdate(const std::string& path) override;
  OnWriteCallbackPair FilterSerializeData(
      base::DictionaryValue* pref_store_contents) override;

 private:
  // InterceptablePrefFilter implementation.
  void FinalizeFilterOnLoad(
      const PostFilterOnLoadCallback& post_filter_on_load_callback,
      std::unique_ptr<base::DictionaryValue> pref_store_contents,
      bool prefs_altered) override;

  // Helper function to generate FilterSerializeData()'s pre-write and
  // post-write callbacks. The returned callbacks are thread-safe.
  OnWriteCallbackPair GetOnWriteSynchronousCallbacks(
      base::DictionaryValue* pref_store_contents);

  // Clears the MACs contained in |external_validation_hash_store_contents|
  // which are present in |paths_to_clear|.
  static void ClearFromExternalStore(
      HashStoreContents* external_validation_hash_store_contents,
      const base::DictionaryValue* changed_paths_and_macs);

  // Flushes the MACs contained in |changed_paths_and_mac| to
  // external_hash_store_contents if |write_success|, otherwise discards the
  // changes.
  static void FlushToExternalStore(
      std::unique_ptr<HashStoreContents> external_hash_store_contents,
      std::unique_ptr<base::DictionaryValue> changed_paths_and_macs,
      bool write_success);

  // Callback to be invoked only once (and subsequently reset) on the next
  // FilterOnLoad event. It will be allowed to modify the |prefs| handed to
  // FilterOnLoad before handing them back to this PrefHashFilter.
  FilterOnLoadInterceptor filter_on_load_interceptor_;

  // A map of paths to TrackedPreferences; this map owns this individual
  // TrackedPreference objects.
  typedef base::ScopedPtrHashMap<std::string,
                                 std::unique_ptr<TrackedPreference>>
      TrackedPreferencesMap;

  // A map from changed paths to their corresponding TrackedPreferences (which
  // aren't owned by this map).
  typedef std::map<std::string, const TrackedPreference*> ChangedPathsMap;

  std::unique_ptr<PrefHashStore> pref_hash_store_;

  // A store and contents on which to perform extra validations without
  // triggering resets.
  // Will be null if the platform does not support external validation.
  const base::Optional<StoreContentsPair> external_validation_hash_store_pair_;

  // Invoked if a reset occurs in a call to FilterOnLoad.
  const base::Closure on_reset_on_load_;

  TrackedPreferencesMap tracked_paths_;

  // The set of all paths whose value has changed since the last call to
  // FilterSerializeData.
  ChangedPathsMap changed_paths_;

  // Whether to report the validity of the super MAC at load time (via UMA).
  bool report_super_mac_validity_;

  DISALLOW_COPY_AND_ASSIGN(PrefHashFilter);
};

#endif  // COMPONENTS_PREFS_TRACKED_PREF_HASH_FILTER_H_