| OLD | NEW |
| 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 "components/user_prefs/tracked/tracked_preferences_migration.h" | 5 #include "components/user_prefs/tracked/tracked_preferences_migration.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" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 const base::Callback<void(const std::string& key)>& | 29 const base::Callback<void(const std::string& key)>& |
| 30 unprotected_store_cleaner, | 30 unprotected_store_cleaner, |
| 31 const base::Callback<void(const std::string& key)>& | 31 const base::Callback<void(const std::string& key)>& |
| 32 protected_store_cleaner, | 32 protected_store_cleaner, |
| 33 const base::Callback<void(const base::Closure&)>& | 33 const base::Callback<void(const base::Closure&)>& |
| 34 register_on_successful_unprotected_store_write_callback, | 34 register_on_successful_unprotected_store_write_callback, |
| 35 const base::Callback<void(const base::Closure&)>& | 35 const base::Callback<void(const base::Closure&)>& |
| 36 register_on_successful_protected_store_write_callback, | 36 register_on_successful_protected_store_write_callback, |
| 37 std::unique_ptr<PrefHashStore> unprotected_pref_hash_store, | 37 std::unique_ptr<PrefHashStore> unprotected_pref_hash_store, |
| 38 std::unique_ptr<PrefHashStore> protected_pref_hash_store, | 38 std::unique_ptr<PrefHashStore> protected_pref_hash_store, |
| 39 std::unique_ptr<HashStoreContents> legacy_pref_hash_store, | |
| 40 InterceptablePrefFilter* unprotected_pref_filter, | 39 InterceptablePrefFilter* unprotected_pref_filter, |
| 41 InterceptablePrefFilter* protected_pref_filter); | 40 InterceptablePrefFilter* protected_pref_filter); |
| 42 | 41 |
| 43 private: | 42 private: |
| 44 friend class base::RefCounted<TrackedPreferencesMigrator>; | 43 friend class base::RefCounted<TrackedPreferencesMigrator>; |
| 45 | 44 |
| 46 enum PrefFilterID { | 45 enum PrefFilterID { |
| 47 UNPROTECTED_PREF_FILTER, | 46 UNPROTECTED_PREF_FILTER, |
| 48 PROTECTED_PREF_FILTER | 47 PROTECTED_PREF_FILTER |
| 49 }; | 48 }; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 72 const base::Callback<void(const base::Closure&)> | 71 const base::Callback<void(const base::Closure&)> |
| 73 register_on_successful_protected_store_write_callback_; | 72 register_on_successful_protected_store_write_callback_; |
| 74 | 73 |
| 75 InterceptablePrefFilter::FinalizeFilterOnLoadCallback | 74 InterceptablePrefFilter::FinalizeFilterOnLoadCallback |
| 76 finalize_unprotected_filter_on_load_; | 75 finalize_unprotected_filter_on_load_; |
| 77 InterceptablePrefFilter::FinalizeFilterOnLoadCallback | 76 InterceptablePrefFilter::FinalizeFilterOnLoadCallback |
| 78 finalize_protected_filter_on_load_; | 77 finalize_protected_filter_on_load_; |
| 79 | 78 |
| 80 std::unique_ptr<PrefHashStore> unprotected_pref_hash_store_; | 79 std::unique_ptr<PrefHashStore> unprotected_pref_hash_store_; |
| 81 std::unique_ptr<PrefHashStore> protected_pref_hash_store_; | 80 std::unique_ptr<PrefHashStore> protected_pref_hash_store_; |
| 82 std::unique_ptr<HashStoreContents> legacy_pref_hash_store_; | |
| 83 | 81 |
| 84 std::unique_ptr<base::DictionaryValue> unprotected_prefs_; | 82 std::unique_ptr<base::DictionaryValue> unprotected_prefs_; |
| 85 std::unique_ptr<base::DictionaryValue> protected_prefs_; | 83 std::unique_ptr<base::DictionaryValue> protected_prefs_; |
| 86 | 84 |
| 87 DISALLOW_COPY_AND_ASSIGN(TrackedPreferencesMigrator); | 85 DISALLOW_COPY_AND_ASSIGN(TrackedPreferencesMigrator); |
| 88 }; | 86 }; |
| 89 | 87 |
| 90 // Invokes |store_cleaner| for every |keys_to_clean|. | 88 // Invokes |store_cleaner| for every |keys_to_clean|. |
| 91 void CleanupPrefStore( | 89 void CleanupPrefStore( |
| 92 const base::Callback<void(const std::string& key)>& store_cleaner, | 90 const base::Callback<void(const std::string& key)>& store_cleaner, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 } | 132 } |
| 135 | 133 |
| 136 // Copies the value of each pref in |pref_names| which is set in |old_store|, | 134 // Copies the value of each pref in |pref_names| which is set in |old_store|, |
| 137 // but not in |new_store| into |new_store|. Sets |old_store_needs_cleanup| to | 135 // but not in |new_store| into |new_store|. Sets |old_store_needs_cleanup| to |
| 138 // true if any old duplicates remain in |old_store| and sets |new_store_altered| | 136 // true if any old duplicates remain in |old_store| and sets |new_store_altered| |
| 139 // to true if any value was copied to |new_store|. | 137 // to true if any value was copied to |new_store|. |
| 140 void MigratePrefsFromOldToNewStore(const std::set<std::string>& pref_names, | 138 void MigratePrefsFromOldToNewStore(const std::set<std::string>& pref_names, |
| 141 base::DictionaryValue* old_store, | 139 base::DictionaryValue* old_store, |
| 142 base::DictionaryValue* new_store, | 140 base::DictionaryValue* new_store, |
| 143 PrefHashStore* new_hash_store, | 141 PrefHashStore* new_hash_store, |
| 144 HashStoreContents* legacy_hash_store, | |
| 145 bool* old_store_needs_cleanup, | 142 bool* old_store_needs_cleanup, |
| 146 bool* new_store_altered, | 143 bool* new_store_altered) { |
| 147 bool* used_legacy_pref_hashes) { | |
| 148 const base::DictionaryValue* old_hash_store_contents = | 144 const base::DictionaryValue* old_hash_store_contents = |
| 149 DictionaryHashStoreContents(old_store).GetContents(); | 145 DictionaryHashStoreContents(old_store).GetContents(); |
| 150 const base::DictionaryValue* legacy_hash_store_contents = | |
| 151 legacy_hash_store->GetContents(); | |
| 152 std::unique_ptr<PrefHashStoreTransaction> new_hash_store_transaction( | 146 std::unique_ptr<PrefHashStoreTransaction> new_hash_store_transaction( |
| 153 new_hash_store->BeginTransaction(std::unique_ptr<HashStoreContents>( | 147 new_hash_store->BeginTransaction(std::unique_ptr<HashStoreContents>( |
| 154 new DictionaryHashStoreContents(new_store)))); | 148 new DictionaryHashStoreContents(new_store)))); |
| 155 | 149 |
| 156 for (std::set<std::string>::const_iterator it = pref_names.begin(); | 150 for (std::set<std::string>::const_iterator it = pref_names.begin(); |
| 157 it != pref_names.end(); | 151 it != pref_names.end(); |
| 158 ++it) { | 152 ++it) { |
| 159 const std::string& pref_name = *it; | 153 const std::string& pref_name = *it; |
| 160 const base::Value* value_in_old_store = NULL; | 154 const base::Value* value_in_old_store = NULL; |
| 161 | 155 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 179 new_store->Set(pref_name, value_in_old_store->DeepCopy()); | 173 new_store->Set(pref_name, value_in_old_store->DeepCopy()); |
| 180 migrated_value = true; | 174 migrated_value = true; |
| 181 *new_store_altered = true; | 175 *new_store_altered = true; |
| 182 } | 176 } |
| 183 } | 177 } |
| 184 | 178 |
| 185 if (destination_hash_missing || migrated_value) { | 179 if (destination_hash_missing || migrated_value) { |
| 186 const base::Value* old_hash = NULL; | 180 const base::Value* old_hash = NULL; |
| 187 if (old_hash_store_contents) | 181 if (old_hash_store_contents) |
| 188 old_hash_store_contents->Get(pref_name, &old_hash); | 182 old_hash_store_contents->Get(pref_name, &old_hash); |
| 189 if (!old_hash && legacy_hash_store_contents) { | |
| 190 legacy_hash_store_contents->Get(pref_name, &old_hash); | |
| 191 if (old_hash) | |
| 192 *used_legacy_pref_hashes = true; | |
| 193 } | |
| 194 if (old_hash) { | 183 if (old_hash) { |
| 195 new_hash_store_transaction->ImportHash(pref_name, old_hash); | 184 new_hash_store_transaction->ImportHash(pref_name, old_hash); |
| 196 *new_store_altered = true; | 185 *new_store_altered = true; |
| 197 } else if (!destination_hash_missing) { | 186 } else if (!destination_hash_missing) { |
| 198 // Do not allow values to be migrated without MACs if the destination | 187 // Do not allow values to be migrated without MACs if the destination |
| 199 // already has a MAC (http://crbug.com/414554). Remove the migrated | 188 // already has a MAC (http://crbug.com/414554). Remove the migrated |
| 200 // value in order to provide the same no-op behaviour as if the pref was | 189 // value in order to provide the same no-op behaviour as if the pref was |
| 201 // added to the wrong file when there was already a value for | 190 // added to the wrong file when there was already a value for |
| 202 // |pref_name| in |new_store|. | 191 // |pref_name| in |new_store|. |
| 203 new_store->Remove(pref_name, NULL); | 192 new_store->Remove(pref_name, NULL); |
| 204 *new_store_altered = true; | 193 *new_store_altered = true; |
| 205 } | 194 } |
| 206 } | 195 } |
| 207 } | 196 } |
| 208 } | 197 } |
| 209 | 198 |
| 210 TrackedPreferencesMigrator::TrackedPreferencesMigrator( | 199 TrackedPreferencesMigrator::TrackedPreferencesMigrator( |
| 211 const std::set<std::string>& unprotected_pref_names, | 200 const std::set<std::string>& unprotected_pref_names, |
| 212 const std::set<std::string>& protected_pref_names, | 201 const std::set<std::string>& protected_pref_names, |
| 213 const base::Callback<void(const std::string& key)>& | 202 const base::Callback<void(const std::string& key)>& |
| 214 unprotected_store_cleaner, | 203 unprotected_store_cleaner, |
| 215 const base::Callback<void(const std::string& key)>& protected_store_cleaner, | 204 const base::Callback<void(const std::string& key)>& protected_store_cleaner, |
| 216 const base::Callback<void(const base::Closure&)>& | 205 const base::Callback<void(const base::Closure&)>& |
| 217 register_on_successful_unprotected_store_write_callback, | 206 register_on_successful_unprotected_store_write_callback, |
| 218 const base::Callback<void(const base::Closure&)>& | 207 const base::Callback<void(const base::Closure&)>& |
| 219 register_on_successful_protected_store_write_callback, | 208 register_on_successful_protected_store_write_callback, |
| 220 std::unique_ptr<PrefHashStore> unprotected_pref_hash_store, | 209 std::unique_ptr<PrefHashStore> unprotected_pref_hash_store, |
| 221 std::unique_ptr<PrefHashStore> protected_pref_hash_store, | 210 std::unique_ptr<PrefHashStore> protected_pref_hash_store, |
| 222 std::unique_ptr<HashStoreContents> legacy_pref_hash_store, | |
| 223 InterceptablePrefFilter* unprotected_pref_filter, | 211 InterceptablePrefFilter* unprotected_pref_filter, |
| 224 InterceptablePrefFilter* protected_pref_filter) | 212 InterceptablePrefFilter* protected_pref_filter) |
| 225 : unprotected_pref_names_(unprotected_pref_names), | 213 : unprotected_pref_names_(unprotected_pref_names), |
| 226 protected_pref_names_(protected_pref_names), | 214 protected_pref_names_(protected_pref_names), |
| 227 unprotected_store_cleaner_(unprotected_store_cleaner), | 215 unprotected_store_cleaner_(unprotected_store_cleaner), |
| 228 protected_store_cleaner_(protected_store_cleaner), | 216 protected_store_cleaner_(protected_store_cleaner), |
| 229 register_on_successful_unprotected_store_write_callback_( | 217 register_on_successful_unprotected_store_write_callback_( |
| 230 register_on_successful_unprotected_store_write_callback), | 218 register_on_successful_unprotected_store_write_callback), |
| 231 register_on_successful_protected_store_write_callback_( | 219 register_on_successful_protected_store_write_callback_( |
| 232 register_on_successful_protected_store_write_callback), | 220 register_on_successful_protected_store_write_callback), |
| 233 unprotected_pref_hash_store_(std::move(unprotected_pref_hash_store)), | 221 unprotected_pref_hash_store_(std::move(unprotected_pref_hash_store)), |
| 234 protected_pref_hash_store_(std::move(protected_pref_hash_store)), | 222 protected_pref_hash_store_(std::move(protected_pref_hash_store)) { |
| 235 legacy_pref_hash_store_(std::move(legacy_pref_hash_store)) { | |
| 236 // The callbacks bound below will own this TrackedPreferencesMigrator by | 223 // The callbacks bound below will own this TrackedPreferencesMigrator by |
| 237 // reference. | 224 // reference. |
| 238 unprotected_pref_filter->InterceptNextFilterOnLoad( | 225 unprotected_pref_filter->InterceptNextFilterOnLoad( |
| 239 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, | 226 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, |
| 240 this, | 227 this, |
| 241 UNPROTECTED_PREF_FILTER)); | 228 UNPROTECTED_PREF_FILTER)); |
| 242 protected_pref_filter->InterceptNextFilterOnLoad( | 229 protected_pref_filter->InterceptNextFilterOnLoad( |
| 243 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, | 230 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, |
| 244 this, | 231 this, |
| 245 PROTECTED_PREF_FILTER)); | 232 PROTECTED_PREF_FILTER)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 264 } | 251 } |
| 265 | 252 |
| 266 MigrateIfReady(); | 253 MigrateIfReady(); |
| 267 } | 254 } |
| 268 | 255 |
| 269 void TrackedPreferencesMigrator::MigrateIfReady() { | 256 void TrackedPreferencesMigrator::MigrateIfReady() { |
| 270 // Wait for both stores to have been read before proceeding. | 257 // Wait for both stores to have been read before proceeding. |
| 271 if (!protected_prefs_ || !unprotected_prefs_) | 258 if (!protected_prefs_ || !unprotected_prefs_) |
| 272 return; | 259 return; |
| 273 | 260 |
| 274 bool used_legacy_pref_hashes = false; | |
| 275 bool protected_prefs_need_cleanup = false; | 261 bool protected_prefs_need_cleanup = false; |
| 276 bool unprotected_prefs_altered = false; | 262 bool unprotected_prefs_altered = false; |
| 277 MigratePrefsFromOldToNewStore(unprotected_pref_names_, | 263 MigratePrefsFromOldToNewStore( |
| 278 protected_prefs_.get(), | 264 unprotected_pref_names_, protected_prefs_.get(), unprotected_prefs_.get(), |
| 279 unprotected_prefs_.get(), | 265 unprotected_pref_hash_store_.get(), &protected_prefs_need_cleanup, |
| 280 unprotected_pref_hash_store_.get(), | 266 &unprotected_prefs_altered); |
| 281 legacy_pref_hash_store_.get(), | |
| 282 &protected_prefs_need_cleanup, | |
| 283 &unprotected_prefs_altered, | |
| 284 &used_legacy_pref_hashes); | |
| 285 bool unprotected_prefs_need_cleanup = false; | 267 bool unprotected_prefs_need_cleanup = false; |
| 286 bool protected_prefs_altered = false; | 268 bool protected_prefs_altered = false; |
| 287 MigratePrefsFromOldToNewStore(protected_pref_names_, | 269 MigratePrefsFromOldToNewStore( |
| 288 unprotected_prefs_.get(), | 270 protected_pref_names_, unprotected_prefs_.get(), protected_prefs_.get(), |
| 289 protected_prefs_.get(), | 271 protected_pref_hash_store_.get(), &unprotected_prefs_need_cleanup, |
| 290 protected_pref_hash_store_.get(), | 272 &protected_prefs_altered); |
| 291 legacy_pref_hash_store_.get(), | |
| 292 &unprotected_prefs_need_cleanup, | |
| 293 &protected_prefs_altered, | |
| 294 &used_legacy_pref_hashes); | |
| 295 UMA_HISTOGRAM_BOOLEAN("Settings.MigratedHashesFromLocalState", | |
| 296 used_legacy_pref_hashes); | |
| 297 | 273 |
| 298 if (!unprotected_prefs_altered && !protected_prefs_altered) { | 274 if (!unprotected_prefs_altered && !protected_prefs_altered) { |
| 299 // Clean up any MACs that might have been previously migrated from the | 275 // Clean up any MACs that might have been previously migrated from the |
| 300 // various stores. It's safe to leave them behind for a little while as they | 276 // various stores. It's safe to leave them behind for a little while as they |
| 301 // will be ignored unless the corresponding value is _also_ present. The | 277 // will be ignored unless the corresponding value is _also_ present. The |
| 302 // cleanup must be deferred until the MACs have been written to their target | 278 // cleanup must be deferred until the MACs have been written to their target |
| 303 // stores, and doing so in a subsequent launch is easier than within the | 279 // stores, and doing so in a subsequent launch is easier than within the |
| 304 // same process. | 280 // same process. |
| 305 CleanupMigratedHashes(unprotected_pref_names_, | 281 CleanupMigratedHashes(unprotected_pref_names_, |
| 306 protected_pref_hash_store_.get(), | 282 protected_pref_hash_store_.get(), |
| 307 protected_prefs_.get()); | 283 protected_prefs_.get()); |
| 308 CleanupMigratedHashes(protected_pref_names_, | 284 CleanupMigratedHashes(protected_pref_names_, |
| 309 unprotected_pref_hash_store_.get(), | 285 unprotected_pref_hash_store_.get(), |
| 310 unprotected_prefs_.get()); | 286 unprotected_prefs_.get()); |
| 311 legacy_pref_hash_store_->Reset(); | |
| 312 } | 287 } |
| 313 | 288 |
| 314 // Hand the processed prefs back to their respective filters. | 289 // Hand the processed prefs back to their respective filters. |
| 315 finalize_unprotected_filter_on_load_.Run(std::move(unprotected_prefs_), | 290 finalize_unprotected_filter_on_load_.Run(std::move(unprotected_prefs_), |
| 316 unprotected_prefs_altered); | 291 unprotected_prefs_altered); |
| 317 finalize_protected_filter_on_load_.Run(std::move(protected_prefs_), | 292 finalize_protected_filter_on_load_.Run(std::move(protected_prefs_), |
| 318 protected_prefs_altered); | 293 protected_prefs_altered); |
| 319 | 294 |
| 320 if (unprotected_prefs_need_cleanup) { | 295 if (unprotected_prefs_need_cleanup) { |
| 321 // Schedule a cleanup of the |protected_pref_names_| from the unprotected | 296 // Schedule a cleanup of the |protected_pref_names_| from the unprotected |
| (...skipping 25 matching lines...) Expand all Loading... |
| 347 const std::set<std::string>& protected_pref_names, | 322 const std::set<std::string>& protected_pref_names, |
| 348 const base::Callback<void(const std::string& key)>& | 323 const base::Callback<void(const std::string& key)>& |
| 349 unprotected_store_cleaner, | 324 unprotected_store_cleaner, |
| 350 const base::Callback<void(const std::string& key)>& protected_store_cleaner, | 325 const base::Callback<void(const std::string& key)>& protected_store_cleaner, |
| 351 const base::Callback<void(const base::Closure&)>& | 326 const base::Callback<void(const base::Closure&)>& |
| 352 register_on_successful_unprotected_store_write_callback, | 327 register_on_successful_unprotected_store_write_callback, |
| 353 const base::Callback<void(const base::Closure&)>& | 328 const base::Callback<void(const base::Closure&)>& |
| 354 register_on_successful_protected_store_write_callback, | 329 register_on_successful_protected_store_write_callback, |
| 355 std::unique_ptr<PrefHashStore> unprotected_pref_hash_store, | 330 std::unique_ptr<PrefHashStore> unprotected_pref_hash_store, |
| 356 std::unique_ptr<PrefHashStore> protected_pref_hash_store, | 331 std::unique_ptr<PrefHashStore> protected_pref_hash_store, |
| 357 std::unique_ptr<HashStoreContents> legacy_pref_hash_store, | |
| 358 InterceptablePrefFilter* unprotected_pref_filter, | 332 InterceptablePrefFilter* unprotected_pref_filter, |
| 359 InterceptablePrefFilter* protected_pref_filter) { | 333 InterceptablePrefFilter* protected_pref_filter) { |
| 360 scoped_refptr<TrackedPreferencesMigrator> prefs_migrator( | 334 scoped_refptr<TrackedPreferencesMigrator> prefs_migrator( |
| 361 new TrackedPreferencesMigrator( | 335 new TrackedPreferencesMigrator( |
| 362 unprotected_pref_names, protected_pref_names, | 336 unprotected_pref_names, protected_pref_names, |
| 363 unprotected_store_cleaner, protected_store_cleaner, | 337 unprotected_store_cleaner, protected_store_cleaner, |
| 364 register_on_successful_unprotected_store_write_callback, | 338 register_on_successful_unprotected_store_write_callback, |
| 365 register_on_successful_protected_store_write_callback, | 339 register_on_successful_protected_store_write_callback, |
| 366 std::move(unprotected_pref_hash_store), | 340 std::move(unprotected_pref_hash_store), |
| 367 std::move(protected_pref_hash_store), | 341 std::move(protected_pref_hash_store), unprotected_pref_filter, |
| 368 std::move(legacy_pref_hash_store), unprotected_pref_filter, | |
| 369 protected_pref_filter)); | 342 protected_pref_filter)); |
| 370 } | 343 } |
| OLD | NEW |