1// Copyright 2014 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_PROFILE_PREF_STORE_MANAGER_H_
6#define CHROME_BROWSER_PREFS_PROFILE_PREF_STORE_MANAGER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/files/file_path.h"
12#include "base/macros.h"
13#include "base/memory/ref_counted.h"
14#include "base/memory/scoped_ptr.h"
15#include "chrome/browser/prefs/pref_hash_filter.h"
16
17class PersistentPrefStore;
18class PrefHashStore;
19class PrefService;
20class TrackedPreferenceValidationDelegate;
21
22namespace base {
23class DictionaryValue;
24class SequencedTaskRunner;
25}  // namespace base
26
27namespace user_prefs {
28class PrefRegistrySyncable;
29}  // namespace user_prefs
30
31class PrefRegistrySimple;
32
33// Provides a facade through which the user preference store may be accessed and
34// managed.
35class ProfilePrefStoreManager {
36 public:
37  // Instantiates a ProfilePrefStoreManager with the configuration required to
38  // manage the user preferences of the profile at |profile_path|.
39  // |tracking_configuration| is used for preference tracking.
40  // |reporting_ids_count| is the count of all possible tracked preference IDs
41  // (possibly greater than |tracking_configuration.size()|).
42  // |seed| and |device_id| are used to track preference value changes and must
43  // be the same on each launch in order to verify loaded preference values.
44  ProfilePrefStoreManager(
45      const base::FilePath& profile_path,
46      const std::vector<PrefHashFilter::TrackedPreferenceMetadata>&
47          tracking_configuration,
48      size_t reporting_ids_count,
49      const std::string& seed,
50      const std::string& device_id,
51      PrefService* local_state);
52
53  ~ProfilePrefStoreManager();
54
55  static const bool kPlatformSupportsPreferenceTracking;
56
57  // Register local state prefs used by the profile preferences system.
58  static void RegisterPrefs(PrefRegistrySimple* registry);
59
60  // Register user prefs used by the profile preferences system.
61  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
62
63  // Determines the user preferences filename for the profile at |profile_path|.
64  static base::FilePath GetPrefFilePathFromProfilePath(
65      const base::FilePath& profile_path);
66
67  // Deletes stored hashes for all profiles from |local_state|.
68  static void ResetAllPrefHashStores(PrefService* local_state);
69
70  // Retrieves the time of the last preference reset event, if any, for
71  // |pref_service|. Assumes that |pref_service| is backed by a PrefStore that
72  // was built by ProfilePrefStoreManager.
73  // If no reset has occurred, returns a null |Time|.
74  static base::Time GetResetTime(PrefService* pref_service);
75
76  // Clears the time of the last preference reset event, if any, for
77  // |pref_service|. Assumes that |pref_service| is backed by a PrefStore that
78  // was built by ProfilePrefStoreManager.
79  static void ClearResetTime(PrefService* pref_service);
80
81  // Creates a PersistentPrefStore providing access to the user preferences of
82  // the managed profile. If |on_reset| is provided, it will be invoked if a
83  // reset occurs as a result of loading the profile's prefs.
84  // An optional |validation_delegate| will be notified
85  // of the status of each tracked preference as they are checked.
86  PersistentPrefStore* CreateProfilePrefStore(
87      const scoped_refptr<base::SequencedTaskRunner>& io_task_runner,
88      const base::Closure& on_reset_on_load,
89      TrackedPreferenceValidationDelegate* validation_delegate);
90
91  // Initializes the preferences for the managed profile with the preference
92  // values in |master_prefs|. Acts synchronously, including blocking IO.
93  // Returns true on success.
94  bool InitializePrefsFromMasterPrefs(
95      const base::DictionaryValue& master_prefs);
96
97  // Creates a single-file PrefStore as was used in M34 and earlier. Used only
98  // for testing migration.
99  PersistentPrefStore* CreateDeprecatedCombinedProfilePrefStore(
100      const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
101
102 private:
103  // Returns a PrefHashStore for the managed profile. Should only be called
104  // if |kPlatformSupportsPreferenceTracking|. |use_super_mac| determines
105  // whether the returned object will calculate, store, and validate super MACs
106  // (and, by extension, accept non-null newly protected preferences as
107  // TrustedInitialized).
108  scoped_ptr<PrefHashStore> GetPrefHashStore(bool use_super_mac);
109
110  const base::FilePath profile_path_;
111  const std::vector<PrefHashFilter::TrackedPreferenceMetadata>
112      tracking_configuration_;
113  const size_t reporting_ids_count_;
114  const std::string seed_;
115  const std::string device_id_;
116  PrefService* local_state_;
117
118  DISALLOW_COPY_AND_ASSIGN(ProfilePrefStoreManager);
119};
120
121#endif  // CHROME_BROWSER_PREFS_PROFILE_PREF_STORE_MANAGER_H_
122