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

Side by Side Diff: components/prefs_tracker/tracked_preferences_migration.cc

Issue 1227973003: Componentize //chrome/browser/prefs/tracked. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: modified folder to prefs_tracker Created 5 years, 5 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/tracked/tracked_preferences_migration.h" 5 #include "components/prefs_tracker/tracked_preferences_migration.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "chrome/browser/prefs/tracked/dictionary_hash_store_contents.h" 13 #include "components/prefs_tracker/dictionary_hash_store_contents.h"
14 #include "chrome/browser/prefs/tracked/hash_store_contents.h" 14 #include "components/prefs_tracker/hash_store_contents.h"
15 #include "chrome/browser/prefs/tracked/interceptable_pref_filter.h" 15 #include "components/prefs_tracker/interceptable_pref_filter.h"
16 #include "chrome/browser/prefs/tracked/pref_hash_store.h" 16 #include "components/prefs_tracker/pref_hash_store.h"
17 #include "chrome/browser/prefs/tracked/pref_hash_store_transaction.h" 17 #include "components/prefs_tracker/pref_hash_store_transaction.h"
18 18
19 namespace { 19 namespace {
20 20
21 class TrackedPreferencesMigrator 21 class TrackedPreferencesMigrator
22 : public base::RefCounted<TrackedPreferencesMigrator> { 22 : public base::RefCounted<TrackedPreferencesMigrator> {
23 public: 23 public:
24 TrackedPreferencesMigrator( 24 TrackedPreferencesMigrator(
25 const std::set<std::string>& unprotected_pref_names, 25 const std::set<std::string>& unprotected_pref_names,
26 const std::set<std::string>& protected_pref_names, 26 const std::set<std::string>& protected_pref_names,
27 const base::Callback<void(const std::string& key)>& 27 const base::Callback<void(const std::string& key)>&
28 unprotected_store_cleaner, 28 unprotected_store_cleaner,
29 const base::Callback<void(const std::string& key)>& 29 const base::Callback<void(const std::string& key)>&
30 protected_store_cleaner, 30 protected_store_cleaner,
31 const base::Callback<void(const base::Closure&)>& 31 const base::Callback<void(const base::Closure&)>&
32 register_on_successful_unprotected_store_write_callback, 32 register_on_successful_unprotected_store_write_callback,
33 const base::Callback<void(const base::Closure&)>& 33 const base::Callback<void(const base::Closure&)>&
34 register_on_successful_protected_store_write_callback, 34 register_on_successful_protected_store_write_callback,
35 scoped_ptr<PrefHashStore> unprotected_pref_hash_store, 35 scoped_ptr<PrefHashStore> unprotected_pref_hash_store,
36 scoped_ptr<PrefHashStore> protected_pref_hash_store, 36 scoped_ptr<PrefHashStore> protected_pref_hash_store,
37 scoped_ptr<HashStoreContents> legacy_pref_hash_store, 37 scoped_ptr<HashStoreContents> legacy_pref_hash_store,
38 InterceptablePrefFilter* unprotected_pref_filter, 38 InterceptablePrefFilter* unprotected_pref_filter,
39 InterceptablePrefFilter* protected_pref_filter); 39 InterceptablePrefFilter* protected_pref_filter);
40 40
41 private: 41 private:
42 friend class base::RefCounted<TrackedPreferencesMigrator>; 42 friend class base::RefCounted<TrackedPreferencesMigrator>;
43 43
44 enum PrefFilterID { 44 enum PrefFilterID { UNPROTECTED_PREF_FILTER, PROTECTED_PREF_FILTER };
45 UNPROTECTED_PREF_FILTER,
46 PROTECTED_PREF_FILTER
47 };
48 45
49 ~TrackedPreferencesMigrator(); 46 ~TrackedPreferencesMigrator();
50 47
51 // Stores the data coming in from the filter identified by |id| into this 48 // Stores the data coming in from the filter identified by |id| into this
52 // class and then calls MigrateIfReady(); 49 // class and then calls MigrateIfReady();
53 void InterceptFilterOnLoad( 50 void InterceptFilterOnLoad(
54 PrefFilterID id, 51 PrefFilterID id,
55 const InterceptablePrefFilter::FinalizeFilterOnLoadCallback& 52 const InterceptablePrefFilter::FinalizeFilterOnLoadCallback&
56 finalize_filter_on_load, 53 finalize_filter_on_load,
57 scoped_ptr<base::DictionaryValue> prefs); 54 scoped_ptr<base::DictionaryValue> prefs);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 114
118 // Removes hashes for |migrated_pref_names| from |origin_pref_store| using 115 // Removes hashes for |migrated_pref_names| from |origin_pref_store| using
119 // the configuration/implementation in |origin_pref_hash_store|. 116 // the configuration/implementation in |origin_pref_hash_store|.
120 void CleanupMigratedHashes(const std::set<std::string>& migrated_pref_names, 117 void CleanupMigratedHashes(const std::set<std::string>& migrated_pref_names,
121 PrefHashStore* origin_pref_hash_store, 118 PrefHashStore* origin_pref_hash_store,
122 base::DictionaryValue* origin_pref_store) { 119 base::DictionaryValue* origin_pref_store) {
123 scoped_ptr<PrefHashStoreTransaction> transaction( 120 scoped_ptr<PrefHashStoreTransaction> transaction(
124 origin_pref_hash_store->BeginTransaction(scoped_ptr<HashStoreContents>( 121 origin_pref_hash_store->BeginTransaction(scoped_ptr<HashStoreContents>(
125 new DictionaryHashStoreContents(origin_pref_store)))); 122 new DictionaryHashStoreContents(origin_pref_store))));
126 for (std::set<std::string>::const_iterator it = migrated_pref_names.begin(); 123 for (std::set<std::string>::const_iterator it = migrated_pref_names.begin();
127 it != migrated_pref_names.end(); 124 it != migrated_pref_names.end(); ++it) {
128 ++it) {
129 transaction->ClearHash(*it); 125 transaction->ClearHash(*it);
130 } 126 }
131 } 127 }
132 128
133 // Copies the value of each pref in |pref_names| which is set in |old_store|, 129 // Copies the value of each pref in |pref_names| which is set in |old_store|,
134 // but not in |new_store| into |new_store|. Sets |old_store_needs_cleanup| to 130 // but not in |new_store| into |new_store|. Sets |old_store_needs_cleanup| to
135 // true if any old duplicates remain in |old_store| and sets |new_store_altered| 131 // true if any old duplicates remain in |old_store| and sets |new_store_altered|
136 // to true if any value was copied to |new_store|. 132 // to true if any value was copied to |new_store|.
137 void MigratePrefsFromOldToNewStore(const std::set<std::string>& pref_names, 133 void MigratePrefsFromOldToNewStore(const std::set<std::string>& pref_names,
138 base::DictionaryValue* old_store, 134 base::DictionaryValue* old_store,
139 base::DictionaryValue* new_store, 135 base::DictionaryValue* new_store,
140 PrefHashStore* new_hash_store, 136 PrefHashStore* new_hash_store,
141 HashStoreContents* legacy_hash_store, 137 HashStoreContents* legacy_hash_store,
142 bool* old_store_needs_cleanup, 138 bool* old_store_needs_cleanup,
143 bool* new_store_altered, 139 bool* new_store_altered,
144 bool* used_legacy_pref_hashes) { 140 bool* used_legacy_pref_hashes) {
145 const base::DictionaryValue* old_hash_store_contents = 141 const base::DictionaryValue* old_hash_store_contents =
146 DictionaryHashStoreContents(old_store).GetContents(); 142 DictionaryHashStoreContents(old_store).GetContents();
147 const base::DictionaryValue* legacy_hash_store_contents = 143 const base::DictionaryValue* legacy_hash_store_contents =
148 legacy_hash_store->GetContents(); 144 legacy_hash_store->GetContents();
149 scoped_ptr<PrefHashStoreTransaction> new_hash_store_transaction( 145 scoped_ptr<PrefHashStoreTransaction> new_hash_store_transaction(
150 new_hash_store->BeginTransaction(scoped_ptr<HashStoreContents>( 146 new_hash_store->BeginTransaction(scoped_ptr<HashStoreContents>(
151 new DictionaryHashStoreContents(new_store)))); 147 new DictionaryHashStoreContents(new_store))));
152 148
153 for (std::set<std::string>::const_iterator it = pref_names.begin(); 149 for (std::set<std::string>::const_iterator it = pref_names.begin();
154 it != pref_names.end(); 150 it != pref_names.end(); ++it) {
155 ++it) { 151 const std::string& pref_name = *it;
156 const std::string& pref_name = *it; 152 const base::Value* value_in_old_store = NULL;
157 const base::Value* value_in_old_store = NULL;
158 153
159 // If the destination does not have a hash for this pref we will 154 // If the destination does not have a hash for this pref we will
160 // unconditionally attempt to move it. 155 // unconditionally attempt to move it.
161 bool destination_hash_missing = 156 bool destination_hash_missing =
162 !new_hash_store_transaction->HasHash(pref_name); 157 !new_hash_store_transaction->HasHash(pref_name);
163 // If we migrate the value we will also attempt to migrate the hash. 158 // If we migrate the value we will also attempt to migrate the hash.
164 bool migrated_value = false; 159 bool migrated_value = false;
165 if (old_store->Get(pref_name, &value_in_old_store)) { 160 if (old_store->Get(pref_name, &value_in_old_store)) {
166 // Whether this value ends up being copied below or was left behind by a 161 // Whether this value ends up being copied below or was left behind by a
167 // previous incomplete migration, it should be cleaned up. 162 // previous incomplete migration, it should be cleaned up.
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 register_on_successful_unprotected_store_write_callback_( 221 register_on_successful_unprotected_store_write_callback_(
227 register_on_successful_unprotected_store_write_callback), 222 register_on_successful_unprotected_store_write_callback),
228 register_on_successful_protected_store_write_callback_( 223 register_on_successful_protected_store_write_callback_(
229 register_on_successful_protected_store_write_callback), 224 register_on_successful_protected_store_write_callback),
230 unprotected_pref_hash_store_(unprotected_pref_hash_store.Pass()), 225 unprotected_pref_hash_store_(unprotected_pref_hash_store.Pass()),
231 protected_pref_hash_store_(protected_pref_hash_store.Pass()), 226 protected_pref_hash_store_(protected_pref_hash_store.Pass()),
232 legacy_pref_hash_store_(legacy_pref_hash_store.Pass()) { 227 legacy_pref_hash_store_(legacy_pref_hash_store.Pass()) {
233 // The callbacks bound below will own this TrackedPreferencesMigrator by 228 // The callbacks bound below will own this TrackedPreferencesMigrator by
234 // reference. 229 // reference.
235 unprotected_pref_filter->InterceptNextFilterOnLoad( 230 unprotected_pref_filter->InterceptNextFilterOnLoad(
236 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, 231 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, this,
237 this,
238 UNPROTECTED_PREF_FILTER)); 232 UNPROTECTED_PREF_FILTER));
239 protected_pref_filter->InterceptNextFilterOnLoad( 233 protected_pref_filter->InterceptNextFilterOnLoad(
240 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, 234 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, this,
241 this,
242 PROTECTED_PREF_FILTER)); 235 PROTECTED_PREF_FILTER));
243 } 236 }
244 237
245 TrackedPreferencesMigrator::~TrackedPreferencesMigrator() {} 238 TrackedPreferencesMigrator::~TrackedPreferencesMigrator() {
239 }
246 240
247 void TrackedPreferencesMigrator::InterceptFilterOnLoad( 241 void TrackedPreferencesMigrator::InterceptFilterOnLoad(
248 PrefFilterID id, 242 PrefFilterID id,
249 const InterceptablePrefFilter::FinalizeFilterOnLoadCallback& 243 const InterceptablePrefFilter::FinalizeFilterOnLoadCallback&
250 finalize_filter_on_load, 244 finalize_filter_on_load,
251 scoped_ptr<base::DictionaryValue> prefs) { 245 scoped_ptr<base::DictionaryValue> prefs) {
252 switch (id) { 246 switch (id) {
253 case UNPROTECTED_PREF_FILTER: 247 case UNPROTECTED_PREF_FILTER:
254 finalize_unprotected_filter_on_load_ = finalize_filter_on_load; 248 finalize_unprotected_filter_on_load_ = finalize_filter_on_load;
255 unprotected_prefs_ = prefs.Pass(); 249 unprotected_prefs_ = prefs.Pass();
256 break; 250 break;
257 case PROTECTED_PREF_FILTER: 251 case PROTECTED_PREF_FILTER:
258 finalize_protected_filter_on_load_ = finalize_filter_on_load; 252 finalize_protected_filter_on_load_ = finalize_filter_on_load;
259 protected_prefs_ = prefs.Pass(); 253 protected_prefs_ = prefs.Pass();
260 break; 254 break;
261 } 255 }
262 256
263 MigrateIfReady(); 257 MigrateIfReady();
264 } 258 }
265 259
266 void TrackedPreferencesMigrator::MigrateIfReady() { 260 void TrackedPreferencesMigrator::MigrateIfReady() {
267 // Wait for both stores to have been read before proceeding. 261 // Wait for both stores to have been read before proceeding.
268 if (!protected_prefs_ || !unprotected_prefs_) 262 if (!protected_prefs_ || !unprotected_prefs_)
269 return; 263 return;
270 264
271 bool used_legacy_pref_hashes = false; 265 bool used_legacy_pref_hashes = false;
272 bool protected_prefs_need_cleanup = false; 266 bool protected_prefs_need_cleanup = false;
273 bool unprotected_prefs_altered = false; 267 bool unprotected_prefs_altered = false;
274 MigratePrefsFromOldToNewStore(unprotected_pref_names_, 268 MigratePrefsFromOldToNewStore(
275 protected_prefs_.get(), 269 unprotected_pref_names_, protected_prefs_.get(), unprotected_prefs_.get(),
276 unprotected_prefs_.get(), 270 unprotected_pref_hash_store_.get(), legacy_pref_hash_store_.get(),
277 unprotected_pref_hash_store_.get(), 271 &protected_prefs_need_cleanup, &unprotected_prefs_altered,
278 legacy_pref_hash_store_.get(), 272 &used_legacy_pref_hashes);
279 &protected_prefs_need_cleanup,
280 &unprotected_prefs_altered,
281 &used_legacy_pref_hashes);
282 bool unprotected_prefs_need_cleanup = false; 273 bool unprotected_prefs_need_cleanup = false;
283 bool protected_prefs_altered = false; 274 bool protected_prefs_altered = false;
284 MigratePrefsFromOldToNewStore(protected_pref_names_, 275 MigratePrefsFromOldToNewStore(
gab 2015/07/23 21:41:16 I think this is also easier to read (easier to see
Jitu( very slow this week) 2015/07/24 09:09:23 Done.
285 unprotected_prefs_.get(), 276 protected_pref_names_, unprotected_prefs_.get(), protected_prefs_.get(),
286 protected_prefs_.get(), 277 protected_pref_hash_store_.get(), legacy_pref_hash_store_.get(),
287 protected_pref_hash_store_.get(), 278 &unprotected_prefs_need_cleanup, &protected_prefs_altered,
288 legacy_pref_hash_store_.get(), 279 &used_legacy_pref_hashes);
289 &unprotected_prefs_need_cleanup,
290 &protected_prefs_altered,
291 &used_legacy_pref_hashes);
292 UMA_HISTOGRAM_BOOLEAN("Settings.MigratedHashesFromLocalState", 280 UMA_HISTOGRAM_BOOLEAN("Settings.MigratedHashesFromLocalState",
293 used_legacy_pref_hashes); 281 used_legacy_pref_hashes);
294 282
295 if (!unprotected_prefs_altered && !protected_prefs_altered) { 283 if (!unprotected_prefs_altered && !protected_prefs_altered) {
296 // Clean up any MACs that might have been previously migrated from the 284 // Clean up any MACs that might have been previously migrated from the
297 // various stores. It's safe to leave them behind for a little while as they 285 // various stores. It's safe to leave them behind for a little while as they
298 // will be ignored unless the corresponding value is _also_ present. The 286 // will be ignored unless the corresponding value is _also_ present. The
299 // cleanup must be deferred until the MACs have been written to their target 287 // cleanup must be deferred until the MACs have been written to their target
300 // stores, and doing so in a subsequent launch is easier than within the 288 // stores, and doing so in a subsequent launch is easier than within the
301 // same process. 289 // same process.
(...skipping 11 matching lines...) Expand all
313 unprotected_prefs_altered); 301 unprotected_prefs_altered);
314 finalize_protected_filter_on_load_.Run(protected_prefs_.Pass(), 302 finalize_protected_filter_on_load_.Run(protected_prefs_.Pass(),
315 protected_prefs_altered); 303 protected_prefs_altered);
316 304
317 if (unprotected_prefs_need_cleanup) { 305 if (unprotected_prefs_need_cleanup) {
318 // Schedule a cleanup of the |protected_pref_names_| from the unprotected 306 // Schedule a cleanup of the |protected_pref_names_| from the unprotected
319 // prefs once the protected prefs were successfully written to disk (or 307 // prefs once the protected prefs were successfully written to disk (or
320 // do it immediately if |!protected_prefs_altered|). 308 // do it immediately if |!protected_prefs_altered|).
321 ScheduleSourcePrefStoreCleanup( 309 ScheduleSourcePrefStoreCleanup(
322 register_on_successful_protected_store_write_callback_, 310 register_on_successful_protected_store_write_callback_,
323 unprotected_store_cleaner_, 311 unprotected_store_cleaner_, protected_pref_names_,
324 protected_pref_names_,
325 protected_prefs_altered); 312 protected_prefs_altered);
326 } 313 }
327 314
328 if (protected_prefs_need_cleanup) { 315 if (protected_prefs_need_cleanup) {
329 // Schedule a cleanup of the |unprotected_pref_names_| from the protected 316 // Schedule a cleanup of the |unprotected_pref_names_| from the protected
330 // prefs once the unprotected prefs were successfully written to disk (or 317 // prefs once the unprotected prefs were successfully written to disk (or
331 // do it immediately if |!unprotected_prefs_altered|). 318 // do it immediately if |!unprotected_prefs_altered|).
332 ScheduleSourcePrefStoreCleanup( 319 ScheduleSourcePrefStoreCleanup(
333 register_on_successful_unprotected_store_write_callback_, 320 register_on_successful_unprotected_store_write_callback_,
334 protected_store_cleaner_, 321 protected_store_cleaner_, unprotected_pref_names_,
335 unprotected_pref_names_,
336 unprotected_prefs_altered); 322 unprotected_prefs_altered);
337 } 323 }
338 } 324 }
339 325
340 } // namespace 326 } // namespace
341 327
342 void SetupTrackedPreferencesMigration( 328 void SetupTrackedPreferencesMigration(
343 const std::set<std::string>& unprotected_pref_names, 329 const std::set<std::string>& unprotected_pref_names,
344 const std::set<std::string>& protected_pref_names, 330 const std::set<std::string>& protected_pref_names,
345 const base::Callback<void(const std::string& key)>& 331 const base::Callback<void(const std::string& key)>&
346 unprotected_store_cleaner, 332 unprotected_store_cleaner,
347 const base::Callback<void(const std::string& key)>& protected_store_cleaner, 333 const base::Callback<void(const std::string& key)>& protected_store_cleaner,
348 const base::Callback<void(const base::Closure&)>& 334 const base::Callback<void(const base::Closure&)>&
349 register_on_successful_unprotected_store_write_callback, 335 register_on_successful_unprotected_store_write_callback,
350 const base::Callback<void(const base::Closure&)>& 336 const base::Callback<void(const base::Closure&)>&
351 register_on_successful_protected_store_write_callback, 337 register_on_successful_protected_store_write_callback,
352 scoped_ptr<PrefHashStore> unprotected_pref_hash_store, 338 scoped_ptr<PrefHashStore> unprotected_pref_hash_store,
353 scoped_ptr<PrefHashStore> protected_pref_hash_store, 339 scoped_ptr<PrefHashStore> protected_pref_hash_store,
354 scoped_ptr<HashStoreContents> legacy_pref_hash_store, 340 scoped_ptr<HashStoreContents> legacy_pref_hash_store,
355 InterceptablePrefFilter* unprotected_pref_filter, 341 InterceptablePrefFilter* unprotected_pref_filter,
356 InterceptablePrefFilter* protected_pref_filter) { 342 InterceptablePrefFilter* protected_pref_filter) {
357 scoped_refptr<TrackedPreferencesMigrator> prefs_migrator( 343 scoped_refptr<TrackedPreferencesMigrator> prefs_migrator(
358 new TrackedPreferencesMigrator( 344 new TrackedPreferencesMigrator(
359 unprotected_pref_names, 345 unprotected_pref_names, protected_pref_names,
360 protected_pref_names, 346 unprotected_store_cleaner, protected_store_cleaner,
361 unprotected_store_cleaner,
362 protected_store_cleaner,
363 register_on_successful_unprotected_store_write_callback, 347 register_on_successful_unprotected_store_write_callback,
364 register_on_successful_protected_store_write_callback, 348 register_on_successful_protected_store_write_callback,
365 unprotected_pref_hash_store.Pass(), 349 unprotected_pref_hash_store.Pass(), protected_pref_hash_store.Pass(),
366 protected_pref_hash_store.Pass(), 350 legacy_pref_hash_store.Pass(), unprotected_pref_filter,
367 legacy_pref_hash_store.Pass(),
368 unprotected_pref_filter,
369 protected_pref_filter)); 351 protected_pref_filter));
370 } 352 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698