Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(199)

Side by Side Diff: chrome/browser/prefs/profile_pref_store_manager.cc

Issue 2745563005: Pref service: add support for tracked prefs. (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/prefs/profile_pref_store_manager.h" 5 #include "chrome/browser/prefs/profile_pref_store_manager.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/json/json_file_value_serializer.h" 12 #include "base/json/json_file_value_serializer.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/metrics/histogram_macros.h" 15 #include "base/metrics/histogram_macros.h"
16 #include "base/sequenced_task_runner.h" 16 #include "base/sequenced_task_runner.h"
17 #include "build/build_config.h" 17 #include "build/build_config.h"
18 #include "chrome/common/chrome_constants.h" 18 #include "chrome/common/chrome_constants.h"
19 #include "components/pref_registry/pref_registry_syncable.h" 19 #include "components/pref_registry/pref_registry_syncable.h"
20 #include "components/prefs/json_pref_store.h" 20 #include "components/prefs/json_pref_store.h"
21 #include "components/prefs/persistent_pref_store.h" 21 #include "components/prefs/persistent_pref_store.h"
22 #include "components/prefs/pref_registry_simple.h" 22 #include "components/prefs/pref_registry_simple.h"
23 #include "components/user_prefs/tracked/pref_hash_store_impl.h" 23 #include "components/user_prefs/tracked/pref_hash_store_impl.h"
24 #include "components/user_prefs/tracked/segregated_pref_store.h" 24 #include "components/user_prefs/tracked/segregated_pref_store.h"
25 #include "components/user_prefs/tracked/tracked_preferences_migration.h" 25 #include "components/user_prefs/tracked/tracked_preferences_migration.h"
26 #include "services/preferences/public/cpp/tracked_persistent_pref_store_factory. h"
27 #include "services/preferences/public/interfaces/preferences_configuration.mojom .h"
26 28
27 #if defined(OS_WIN) 29 #if defined(OS_WIN)
28 #include "chrome/install_static/install_util.h" 30 #include "chrome/install_static/install_util.h"
29 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h" 31 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h"
30 #endif 32 #endif
31 33
32 namespace { 34 namespace {
33 35
34 void RemoveValueSilently(const base::WeakPtr<JsonPrefStore> pref_store,
35 const std::string& key) {
36 if (pref_store) {
37 pref_store->RemoveValueSilently(
38 key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
39 }
40 }
41
42 #if defined(OS_WIN) 36 #if defined(OS_WIN)
43 // Forces a different registry key to be used for storing preference validation 37 // Forces a different registry key to be used for storing preference validation
44 // MACs. See |SetPreferenceValidationRegistryPathForTesting|. 38 // MACs. See |SetPreferenceValidationRegistryPathForTesting|.
45 const base::string16* g_preference_validation_registry_path_for_testing = 39 const base::string16* g_preference_validation_registry_path_for_testing =
46 nullptr; 40 nullptr;
47 #endif // OS_WIN 41 #endif // OS_WIN
48 42
49 } // namespace 43 } // namespace
50 44
51 // Preference tracking and protection is not required on platforms where other 45 // Preference tracking and protection is not required on platforms where other
52 // apps do not have access to chrome's persistent storage. 46 // apps do not have access to chrome's persistent storage.
53 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = 47 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking =
54 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) 48 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
55 false; 49 false;
56 #else 50 #else
57 true; 51 true;
58 #endif 52 #endif
59 53
60 ProfilePrefStoreManager::ProfilePrefStoreManager( 54 ProfilePrefStoreManager::ProfilePrefStoreManager(
61 const base::FilePath& profile_path, 55 const base::FilePath& profile_path,
62 const std::vector<PrefHashFilter::TrackedPreferenceMetadata>& 56 const std::vector<PrefHashFilter::TrackedPreferenceMetadata>&
63 tracking_configuration, 57 tracking_configuration,
64 size_t reporting_ids_count, 58 size_t reporting_ids_count,
65 const std::string& seed, 59 const std::string& seed,
66 const std::string& legacy_device_id, 60 const std::string& legacy_device_id)
67 PrefService* local_state)
68 : profile_path_(profile_path), 61 : profile_path_(profile_path),
69 tracking_configuration_(tracking_configuration), 62 tracking_configuration_(tracking_configuration),
70 reporting_ids_count_(reporting_ids_count), 63 reporting_ids_count_(reporting_ids_count),
71 seed_(seed), 64 seed_(seed),
72 legacy_device_id_(legacy_device_id), 65 legacy_device_id_(legacy_device_id) {}
73 local_state_(local_state) {}
74 66
75 ProfilePrefStoreManager::~ProfilePrefStoreManager() {} 67 ProfilePrefStoreManager::~ProfilePrefStoreManager() {}
76 68
77 // static 69 // static
78 void ProfilePrefStoreManager::RegisterProfilePrefs( 70 void ProfilePrefStoreManager::RegisterProfilePrefs(
79 user_prefs::PrefRegistrySyncable* registry) { 71 user_prefs::PrefRegistrySyncable* registry) {
80 PrefHashFilter::RegisterProfilePrefs(registry); 72 PrefHashFilter::RegisterProfilePrefs(registry);
81 } 73 }
82 74
83 // static 75 // static
84 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { 76 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) {
85 return PrefHashFilter::GetResetTime(pref_service); 77 return PrefHashFilter::GetResetTime(pref_service);
86 } 78 }
87 79
88 // static 80 // static
89 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { 81 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) {
90 PrefHashFilter::ClearResetTime(pref_service); 82 PrefHashFilter::ClearResetTime(pref_service);
91 } 83 }
92 84
93 #if defined(OS_WIN) 85 #if defined(OS_WIN)
94 // static 86 // static
95 void ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting( 87 void ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting(
96 const base::string16* path) { 88 const base::string16* path) {
97 DCHECK(!path->empty()); 89 DCHECK(!path->empty());
98 g_preference_validation_registry_path_for_testing = path; 90 g_preference_validation_registry_path_for_testing = path;
99 } 91 }
100 #endif // OS_WIN 92 #endif // OS_WIN
101 93
102 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( 94 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore(
103 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, 95 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
104 const base::Closure& on_reset_on_load, 96 const base::Closure& on_reset_on_load,
105 prefs::mojom::TrackedPreferenceValidationDelegate* validation_delegate) { 97 prefs::mojom::TrackedPreferenceValidationDelegate* validation_delegate) {
106 std::unique_ptr<PrefFilter> pref_filter;
107 if (!kPlatformSupportsPreferenceTracking) { 98 if (!kPlatformSupportsPreferenceTracking) {
108 return new JsonPrefStore(profile_path_.Append(chrome::kPreferencesFilename), 99 return new JsonPrefStore(profile_path_.Append(chrome::kPreferencesFilename),
109 io_task_runner.get(), 100 std::move(io_task_runner), nullptr);
110 std::unique_ptr<PrefFilter>());
111 } 101 }
112 102 return prefs::CreateTrackedPersistentPrefStore(
113 std::vector<PrefHashFilter::TrackedPreferenceMetadata> 103 std::move(io_task_runner),
114 unprotected_configuration; 104 profile_path_.Append(chrome::kPreferencesFilename),
115 std::vector<PrefHashFilter::TrackedPreferenceMetadata>
116 protected_configuration;
117 std::set<std::string> protected_pref_names;
118 std::set<std::string> unprotected_pref_names;
119 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::const_iterator
120 it = tracking_configuration_.begin();
121 it != tracking_configuration_.end();
122 ++it) {
123 if (it->enforcement_level >
124 PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT) {
125 protected_configuration.push_back(*it);
126 protected_pref_names.insert(it->name);
127 } else {
128 unprotected_configuration.push_back(*it);
129 unprotected_pref_names.insert(it->name);
130 }
131 }
132
133 std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter(
134 new PrefHashFilter(GetPrefHashStore(false),
135 GetExternalVerificationPrefHashStorePair(),
136 unprotected_configuration, base::Closure(),
137 validation_delegate, reporting_ids_count_, false));
138 std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter(
139 GetPrefHashStore(true), GetExternalVerificationPrefHashStorePair(),
140 protected_configuration, on_reset_on_load, validation_delegate,
141 reporting_ids_count_, true));
142
143 PrefHashFilter* raw_unprotected_pref_hash_filter =
144 unprotected_pref_hash_filter.get();
145 PrefHashFilter* raw_protected_pref_hash_filter =
146 protected_pref_hash_filter.get();
147
148 scoped_refptr<JsonPrefStore> unprotected_pref_store(new JsonPrefStore(
149 profile_path_.Append(chrome::kPreferencesFilename), io_task_runner.get(),
150 std::move(unprotected_pref_hash_filter)));
151 scoped_refptr<JsonPrefStore> protected_pref_store(new JsonPrefStore(
152 profile_path_.Append(chrome::kSecurePreferencesFilename), 105 profile_path_.Append(chrome::kSecurePreferencesFilename),
153 io_task_runner.get(), std::move(protected_pref_hash_filter))); 106 tracking_configuration_, reporting_ids_count_, seed_, legacy_device_id_,
154 107 #if defined(OS_WIN)
155 SetupTrackedPreferencesMigration( 108 g_preference_validation_registry_path_for_testing
156 unprotected_pref_names, protected_pref_names, 109 ? *g_preference_validation_registry_path_for_testing
157 base::Bind(&RemoveValueSilently, unprotected_pref_store->AsWeakPtr()), 110 : install_static::GetRegistryPath(),
158 base::Bind(&RemoveValueSilently, protected_pref_store->AsWeakPtr()), 111 #else
159 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, 112 base::string16(),
160 unprotected_pref_store->AsWeakPtr()), 113 #endif
161 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, 114 validation_delegate, on_reset_on_load);
gab 2017/03/20 16:36:26 I'm not a fan of this mumbo-jumbo passing of argum
Sam McNally 2017/03/20 23:45:41 Done. The struct doesn't quite match what this cla
162 protected_pref_store->AsWeakPtr()),
163 GetPrefHashStore(false), GetPrefHashStore(true),
164 raw_unprotected_pref_hash_filter, raw_protected_pref_hash_filter);
165
166 return new SegregatedPrefStore(unprotected_pref_store, protected_pref_store,
167 protected_pref_names);
168 } 115 }
169 116
170 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( 117 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs(
171 std::unique_ptr<base::DictionaryValue> master_prefs) { 118 std::unique_ptr<base::DictionaryValue> master_prefs) {
172 // Create the profile directory if it doesn't exist yet (very possible on 119 // Create the profile directory if it doesn't exist yet (very possible on
173 // first run). 120 // first run).
174 if (!base::CreateDirectory(profile_path_)) 121 if (!base::CreateDirectory(profile_path_))
175 return false; 122 return false;
176 123
177 if (kPlatformSupportsPreferenceTracking) { 124 if (kPlatformSupportsPreferenceTracking) {
178 PrefHashFilter(GetPrefHashStore(false), 125 PrefHashFilter(
gab 2017/03/20 16:36:26 I find it weird to have classes like PrefHashFilte
Sam McNally 2017/03/20 23:45:41 That's the plan. The tracked-pref implementation s
179 GetExternalVerificationPrefHashStorePair(), 126 base::MakeUnique<PrefHashStoreImpl>(seed_, legacy_device_id_, false),
180 tracking_configuration_, base::Closure(), NULL, 127 GetExternalVerificationPrefHashStorePair(), tracking_configuration_,
181 reporting_ids_count_, false) 128 base::Closure(), NULL, reporting_ids_count_, false)
182 .Initialize(master_prefs.get()); 129 .Initialize(master_prefs.get());
183 } 130 }
184 131
185 // This will write out to a single combined file which will be immediately 132 // This will write out to a single combined file which will be immediately
186 // migrated to two files on load. 133 // migrated to two files on load.
187 JSONFileValueSerializer serializer( 134 JSONFileValueSerializer serializer(
188 profile_path_.Append(chrome::kPreferencesFilename)); 135 profile_path_.Append(chrome::kPreferencesFilename));
189 136
190 // Call Serialize (which does IO) on the main thread, which would _normally_ 137 // Call Serialize (which does IO) on the main thread, which would _normally_
191 // be verboten. In this case however, we require this IO to synchronously 138 // be verboten. In this case however, we require this IO to synchronously
192 // complete before Chrome can start (as master preferences seed the Local 139 // complete before Chrome can start (as master preferences seed the Local
193 // State and Preferences files). This won't trip ThreadIORestrictions as they 140 // State and Preferences files). This won't trip ThreadIORestrictions as they
194 // won't have kicked in yet on the main thread. 141 // won't have kicked in yet on the main thread.
195 bool success = serializer.Serialize(*master_prefs); 142 bool success = serializer.Serialize(*master_prefs);
196 143
197 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); 144 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success);
198 return success; 145 return success;
199 } 146 }
200 147
201 std::unique_ptr<PrefHashStore> ProfilePrefStoreManager::GetPrefHashStore(
202 bool use_super_mac) {
203 DCHECK(kPlatformSupportsPreferenceTracking);
204
205 return std::unique_ptr<PrefHashStore>(
206 new PrefHashStoreImpl(seed_, legacy_device_id_, use_super_mac));
207 }
208
209 std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>> 148 std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>>
210 ProfilePrefStoreManager::GetExternalVerificationPrefHashStorePair() { 149 ProfilePrefStoreManager::GetExternalVerificationPrefHashStorePair() {
211 DCHECK(kPlatformSupportsPreferenceTracking); 150 DCHECK(kPlatformSupportsPreferenceTracking);
212 #if defined(OS_WIN) 151 #if defined(OS_WIN)
213 return std::make_pair( 152 return std::make_pair(
214 base::MakeUnique<PrefHashStoreImpl>( 153 base::MakeUnique<PrefHashStoreImpl>(
215 "ChromeRegistryHashStoreValidationSeed", legacy_device_id_, 154 "ChromeRegistryHashStoreValidationSeed", legacy_device_id_,
216 false /* use_super_mac */), 155 false /* use_super_mac */),
217 g_preference_validation_registry_path_for_testing 156 g_preference_validation_registry_path_for_testing
218 ? base::MakeUnique<RegistryHashStoreContentsWin>( 157 ? base::MakeUnique<RegistryHashStoreContentsWin>(
219 *g_preference_validation_registry_path_for_testing, 158 *g_preference_validation_registry_path_for_testing,
220 profile_path_.BaseName().LossyDisplayName()) 159 profile_path_.BaseName().LossyDisplayName())
221 : base::MakeUnique<RegistryHashStoreContentsWin>( 160 : base::MakeUnique<RegistryHashStoreContentsWin>(
222 install_static::GetRegistryPath(), 161 install_static::GetRegistryPath(),
223 profile_path_.BaseName().LossyDisplayName())); 162 profile_path_.BaseName().LossyDisplayName()));
224 #else 163 #else
225 return std::make_pair(nullptr, nullptr); 164 return std::make_pair(nullptr, nullptr);
226 #endif 165 #endif
227 } 166 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/profile_pref_store_manager.h ('k') | chrome/browser/prefs/profile_pref_store_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698