Chromium Code Reviews| 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 "chrome/browser/prefs/tracked/tracked_preferences_migration.h" | 5 #include "chrome/browser/prefs/tracked/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/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
|
gab
2014/06/17 02:00:06
Remove this include which is now in the header.
erikwright (departed)
2014/06/17 19:07:24
Done.
| |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/prefs/interceptable_pref_filter.h" | 13 #include "chrome/browser/prefs/interceptable_pref_filter.h" |
| 14 #include "chrome/browser/prefs/pref_hash_store.h" | |
| 15 #include "chrome/browser/prefs/pref_hash_store_transaction.h" | |
| 16 #include "chrome/browser/prefs/tracked/dictionary_hash_store_contents.h" | |
| 17 #include "chrome/browser/prefs/tracked/hash_store_contents.h" | |
| 14 | 18 |
| 15 namespace { | 19 namespace { |
| 16 | 20 |
| 17 class TrackedPreferencesMigrator | 21 class TrackedPreferencesMigrator |
| 18 : public base::RefCounted<TrackedPreferencesMigrator> { | 22 : public base::RefCounted<TrackedPreferencesMigrator> { |
| 19 public: | 23 public: |
| 20 TrackedPreferencesMigrator( | 24 TrackedPreferencesMigrator( |
| 21 const std::set<std::string>& unprotected_pref_names, | 25 const std::set<std::string>& unprotected_pref_names, |
| 22 const std::set<std::string>& protected_pref_names, | 26 const std::set<std::string>& protected_pref_names, |
| 23 const base::Callback<void(const std::string& key)>& | 27 const base::Callback<void(const std::string& key)>& |
| 24 unprotected_store_cleaner, | 28 unprotected_store_cleaner, |
| 25 const base::Callback<void(const std::string& key)>& | 29 const base::Callback<void(const std::string& key)>& |
| 26 protected_store_cleaner, | 30 protected_store_cleaner, |
| 27 const base::Callback<void(const base::Closure&)>& | 31 const base::Callback<void(const base::Closure&)>& |
| 28 register_on_successful_unprotected_store_write_callback, | 32 register_on_successful_unprotected_store_write_callback, |
| 29 const base::Callback<void(const base::Closure&)>& | 33 const base::Callback<void(const base::Closure&)>& |
| 30 register_on_successful_protected_store_write_callback, | 34 register_on_successful_protected_store_write_callback, |
| 35 scoped_ptr<PrefHashStore> unprotected_pref_hash_store, | |
| 36 scoped_ptr<PrefHashStore> protected_pref_hash_store, | |
| 37 scoped_ptr<HashStoreContents> legacy_pref_hash_store, | |
| 31 InterceptablePrefFilter* unprotected_pref_filter, | 38 InterceptablePrefFilter* unprotected_pref_filter, |
| 32 InterceptablePrefFilter* protected_pref_filter); | 39 InterceptablePrefFilter* protected_pref_filter); |
| 33 | 40 |
| 34 private: | 41 private: |
| 35 friend class base::RefCounted<TrackedPreferencesMigrator>; | 42 friend class base::RefCounted<TrackedPreferencesMigrator>; |
| 36 | 43 |
| 37 enum PrefFilterID { | 44 enum PrefFilterID { |
| 38 UNPROTECTED_PREF_FILTER, | 45 UNPROTECTED_PREF_FILTER, |
| 39 PROTECTED_PREF_FILTER | 46 PROTECTED_PREF_FILTER |
| 40 }; | 47 }; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 61 const base::Callback<void(const base::Closure&)> | 68 const base::Callback<void(const base::Closure&)> |
| 62 register_on_successful_unprotected_store_write_callback_; | 69 register_on_successful_unprotected_store_write_callback_; |
| 63 const base::Callback<void(const base::Closure&)> | 70 const base::Callback<void(const base::Closure&)> |
| 64 register_on_successful_protected_store_write_callback_; | 71 register_on_successful_protected_store_write_callback_; |
| 65 | 72 |
| 66 InterceptablePrefFilter::FinalizeFilterOnLoadCallback | 73 InterceptablePrefFilter::FinalizeFilterOnLoadCallback |
| 67 finalize_unprotected_filter_on_load_; | 74 finalize_unprotected_filter_on_load_; |
| 68 InterceptablePrefFilter::FinalizeFilterOnLoadCallback | 75 InterceptablePrefFilter::FinalizeFilterOnLoadCallback |
| 69 finalize_protected_filter_on_load_; | 76 finalize_protected_filter_on_load_; |
| 70 | 77 |
| 78 scoped_ptr<PrefHashStore> unprotected_pref_hash_store_; | |
| 79 scoped_ptr<PrefHashStore> protected_pref_hash_store_; | |
| 80 scoped_ptr<HashStoreContents> legacy_pref_hash_store_; | |
| 81 | |
| 71 scoped_ptr<base::DictionaryValue> unprotected_prefs_; | 82 scoped_ptr<base::DictionaryValue> unprotected_prefs_; |
| 72 scoped_ptr<base::DictionaryValue> protected_prefs_; | 83 scoped_ptr<base::DictionaryValue> protected_prefs_; |
| 73 | 84 |
| 74 DISALLOW_COPY_AND_ASSIGN(TrackedPreferencesMigrator); | 85 DISALLOW_COPY_AND_ASSIGN(TrackedPreferencesMigrator); |
| 75 }; | 86 }; |
| 76 | 87 |
| 77 // Invokes |store_cleaner| for every |keys_to_clean|. | 88 // Invokes |store_cleaner| for every |keys_to_clean|. |
| 78 void CleanupPrefStore( | 89 void CleanupPrefStore( |
| 79 const base::Callback<void(const std::string& key)>& store_cleaner, | 90 const base::Callback<void(const std::string& key)>& store_cleaner, |
| 80 const std::set<std::string>& keys_to_clean) { | 91 const std::set<std::string>& keys_to_clean) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 102 } else { | 113 } else { |
| 103 CleanupPrefStore(source_store_cleaner, keys_to_clean); | 114 CleanupPrefStore(source_store_cleaner, keys_to_clean); |
| 104 } | 115 } |
| 105 } | 116 } |
| 106 | 117 |
| 107 // Copies the value of each pref in |pref_names| which is set in |old_store|, | 118 // Copies the value of each pref in |pref_names| which is set in |old_store|, |
| 108 // but not in |new_store| into |new_store|. Sets |old_store_needs_cleanup| to | 119 // but not in |new_store| into |new_store|. Sets |old_store_needs_cleanup| to |
| 109 // true if any old duplicates remain in |old_store| and sets |new_store_altered| | 120 // true if any old duplicates remain in |old_store| and sets |new_store_altered| |
| 110 // to true if any value was copied to |new_store|. | 121 // to true if any value was copied to |new_store|. |
| 111 void MigratePrefsFromOldToNewStore(const std::set<std::string>& pref_names, | 122 void MigratePrefsFromOldToNewStore(const std::set<std::string>& pref_names, |
| 112 const base::DictionaryValue* old_store, | 123 base::DictionaryValue* old_store, |
| 113 base::DictionaryValue* new_store, | 124 base::DictionaryValue* new_store, |
| 125 PrefHashStore* new_hash_store, | |
| 114 bool* old_store_needs_cleanup, | 126 bool* old_store_needs_cleanup, |
| 115 bool* new_store_altered) { | 127 bool* new_store_altered, |
| 128 HashStoreContents* legacy_hash_store) { | |
|
gab
2014/06/17 02:00:06
Personal preference: put this param between |new_h
erikwright (departed)
2014/06/17 19:07:24
Done.
| |
| 129 const base::DictionaryValue* old_hash_store_contents = | |
| 130 DictionaryHashStoreContents(old_store).GetContents(); | |
| 131 const base::DictionaryValue* legacy_hash_store_contents = | |
| 132 legacy_hash_store->GetContents(); | |
| 133 scoped_ptr<PrefHashStoreTransaction> new_hash_store_transaction( | |
| 134 new_hash_store->BeginTransaction(scoped_ptr<HashStoreContents>( | |
| 135 new DictionaryHashStoreContents(new_store)))); | |
| 136 | |
| 116 for (std::set<std::string>::const_iterator it = pref_names.begin(); | 137 for (std::set<std::string>::const_iterator it = pref_names.begin(); |
| 117 it != pref_names.end(); ++it) { | 138 it != pref_names.end(); |
| 118 const std::string& pref_name = *it; | 139 ++it) { |
| 140 const std::string& pref_name = *it; | |
| 141 const base::Value* value_in_old_store = NULL; | |
| 119 | 142 |
| 120 const base::Value* value_in_old_store = NULL; | 143 // If the destination does not have a hash for this pref we will |
| 121 if (!old_store->Get(pref_name, &value_in_old_store)) | 144 // unconditionally attempt to move it. |
| 122 continue; | 145 bool destination_hash_missing = |
| 146 !new_hash_store_transaction->HasHash(pref_name); | |
| 147 // If we migrate the value we will also attempt to migrate the hash. | |
| 148 bool migrated_value = false; | |
| 149 if (old_store->Get(pref_name, &value_in_old_store)) { | |
| 150 // Whether this value ends up being copied below or was left behind by a | |
| 151 // previous incomplete migration, it should be cleaned up. | |
| 152 *old_store_needs_cleanup = true; | |
| 123 | 153 |
| 124 // Whether this value ends up being copied below or was left behind by a | 154 if (!new_store->Get(pref_name, NULL)) { |
| 125 // previous incomplete migration, it should be cleaned up. | 155 // Copy the value from |old_store| to |new_store| rather than moving it |
| 126 *old_store_needs_cleanup = true; | 156 // to avoid data loss should |old_store| be flushed to disk without |
| 157 // |new_store| having equivalently been successfully flushed to disk | |
| 158 // (e.g., on crash or in cases where |new_store| is read-only following | |
| 159 // a read error on startup). | |
| 160 new_store->Set(pref_name, value_in_old_store->DeepCopy()); | |
| 161 migrated_value = true; | |
| 162 *new_store_altered = true; | |
| 163 } | |
| 164 } | |
| 127 | 165 |
| 128 if (new_store->Get(pref_name, NULL)) | 166 if (destination_hash_missing || migrated_value) { |
| 129 continue; | 167 const base::Value* old_hash = NULL; |
| 130 | 168 if (old_hash_store_contents) |
| 131 // Copy the value from |old_store| to |new_store| rather than moving it to | 169 old_hash_store_contents->Get(pref_name, &old_hash); |
| 132 // avoid data loss should |old_store| be flushed to disk without |new_store| | 170 if (!old_hash && legacy_hash_store_contents) |
| 133 // having equivalently been successfully flushed to disk (e.g., on crash or | 171 legacy_hash_store_contents->Get(pref_name, &old_hash); |
|
gab
2014/06/17 02:00:06
I would like to log an UMA stat if we migrate any
| |
| 134 // in cases where |new_store| is read-only following a read error on | 172 if (old_hash || !destination_hash_missing) { |
|
gab
2014/06/17 02:00:06
I think this would be less subtle (and more readab
erikwright (departed)
2014/06/17 19:07:24
Done.
| |
| 135 // startup). | 173 new_hash_store_transaction->ImportHash(pref_name, old_hash); |
| 136 new_store->Set(pref_name, value_in_old_store->DeepCopy()); | 174 *new_store_altered = true; |
| 137 *new_store_altered = true; | 175 } |
| 176 } | |
| 138 } | 177 } |
| 139 } | 178 } |
| 140 | 179 |
| 180 void CleanupMigratedHashes(const std::set<std::string>& migrated_pref_names, | |
|
gab
2014/06/17 02:00:06
Move this method just below ScheduleSourcePrefStor
erikwright (departed)
2014/06/17 19:07:24
Done.
| |
| 181 PrefHashStore* origin_pref_hash_store, | |
| 182 base::DictionaryValue* origin_pref_store) { | |
| 183 scoped_ptr<PrefHashStoreTransaction> transaction( | |
| 184 origin_pref_hash_store->BeginTransaction(scoped_ptr<HashStoreContents>( | |
| 185 new DictionaryHashStoreContents(origin_pref_store)))); | |
| 186 for (std::set<std::string>::const_iterator it = migrated_pref_names.begin(); | |
| 187 it != migrated_pref_names.end(); | |
| 188 ++it) { | |
| 189 transaction->ClearHash(*it); | |
| 190 } | |
| 191 } | |
| 192 | |
| 141 TrackedPreferencesMigrator::TrackedPreferencesMigrator( | 193 TrackedPreferencesMigrator::TrackedPreferencesMigrator( |
| 142 const std::set<std::string>& unprotected_pref_names, | 194 const std::set<std::string>& unprotected_pref_names, |
| 143 const std::set<std::string>& protected_pref_names, | 195 const std::set<std::string>& protected_pref_names, |
| 144 const base::Callback<void(const std::string& key)>& | 196 const base::Callback<void(const std::string& key)>& |
| 145 unprotected_store_cleaner, | 197 unprotected_store_cleaner, |
| 146 const base::Callback<void(const std::string& key)>& protected_store_cleaner, | 198 const base::Callback<void(const std::string& key)>& protected_store_cleaner, |
| 147 const base::Callback<void(const base::Closure&)>& | 199 const base::Callback<void(const base::Closure&)>& |
| 148 register_on_successful_unprotected_store_write_callback, | 200 register_on_successful_unprotected_store_write_callback, |
| 149 const base::Callback<void(const base::Closure&)>& | 201 const base::Callback<void(const base::Closure&)>& |
| 150 register_on_successful_protected_store_write_callback, | 202 register_on_successful_protected_store_write_callback, |
| 203 scoped_ptr<PrefHashStore> unprotected_pref_hash_store, | |
| 204 scoped_ptr<PrefHashStore> protected_pref_hash_store, | |
| 205 scoped_ptr<HashStoreContents> legacy_pref_hash_store, | |
| 151 InterceptablePrefFilter* unprotected_pref_filter, | 206 InterceptablePrefFilter* unprotected_pref_filter, |
| 152 InterceptablePrefFilter* protected_pref_filter) | 207 InterceptablePrefFilter* protected_pref_filter) |
| 153 : unprotected_pref_names_(unprotected_pref_names), | 208 : unprotected_pref_names_(unprotected_pref_names), |
| 154 protected_pref_names_(protected_pref_names), | 209 protected_pref_names_(protected_pref_names), |
| 155 unprotected_store_cleaner_(unprotected_store_cleaner), | 210 unprotected_store_cleaner_(unprotected_store_cleaner), |
| 156 protected_store_cleaner_(protected_store_cleaner), | 211 protected_store_cleaner_(protected_store_cleaner), |
| 157 register_on_successful_unprotected_store_write_callback_( | 212 register_on_successful_unprotected_store_write_callback_( |
| 158 register_on_successful_unprotected_store_write_callback), | 213 register_on_successful_unprotected_store_write_callback), |
| 159 register_on_successful_protected_store_write_callback_( | 214 register_on_successful_protected_store_write_callback_( |
| 160 register_on_successful_protected_store_write_callback) { | 215 register_on_successful_protected_store_write_callback), |
| 216 unprotected_pref_hash_store_(unprotected_pref_hash_store.Pass()), | |
| 217 protected_pref_hash_store_(protected_pref_hash_store.Pass()), | |
| 218 legacy_pref_hash_store_(legacy_pref_hash_store.Pass()) { | |
| 161 // The callbacks bound below will own this TrackedPreferencesMigrator by | 219 // The callbacks bound below will own this TrackedPreferencesMigrator by |
| 162 // reference. | 220 // reference. |
| 163 unprotected_pref_filter->InterceptNextFilterOnLoad( | 221 unprotected_pref_filter->InterceptNextFilterOnLoad( |
| 164 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, | 222 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, |
| 165 this, | 223 this, |
| 166 UNPROTECTED_PREF_FILTER)); | 224 UNPROTECTED_PREF_FILTER)); |
| 167 protected_pref_filter->InterceptNextFilterOnLoad( | 225 protected_pref_filter->InterceptNextFilterOnLoad( |
| 168 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, | 226 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, |
| 169 this, | 227 this, |
| 170 PROTECTED_PREF_FILTER)); | 228 PROTECTED_PREF_FILTER)); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 194 void TrackedPreferencesMigrator::MigrateIfReady() { | 252 void TrackedPreferencesMigrator::MigrateIfReady() { |
| 195 // Wait for both stores to have been read before proceeding. | 253 // Wait for both stores to have been read before proceeding. |
| 196 if (!protected_prefs_ || !unprotected_prefs_) | 254 if (!protected_prefs_ || !unprotected_prefs_) |
| 197 return; | 255 return; |
| 198 | 256 |
| 199 bool protected_prefs_need_cleanup = false; | 257 bool protected_prefs_need_cleanup = false; |
| 200 bool unprotected_prefs_altered = false; | 258 bool unprotected_prefs_altered = false; |
| 201 MigratePrefsFromOldToNewStore(unprotected_pref_names_, | 259 MigratePrefsFromOldToNewStore(unprotected_pref_names_, |
| 202 protected_prefs_.get(), | 260 protected_prefs_.get(), |
| 203 unprotected_prefs_.get(), | 261 unprotected_prefs_.get(), |
| 262 unprotected_pref_hash_store_.get(), | |
| 204 &protected_prefs_need_cleanup, | 263 &protected_prefs_need_cleanup, |
| 205 &unprotected_prefs_altered); | 264 &unprotected_prefs_altered, |
| 265 legacy_pref_hash_store_.get()); | |
| 206 bool unprotected_prefs_need_cleanup = false; | 266 bool unprotected_prefs_need_cleanup = false; |
| 207 bool protected_prefs_altered = false; | 267 bool protected_prefs_altered = false; |
| 208 MigratePrefsFromOldToNewStore(protected_pref_names_, | 268 MigratePrefsFromOldToNewStore(protected_pref_names_, |
| 209 unprotected_prefs_.get(), | 269 unprotected_prefs_.get(), |
| 210 protected_prefs_.get(), | 270 protected_prefs_.get(), |
| 271 protected_pref_hash_store_.get(), | |
| 211 &unprotected_prefs_need_cleanup, | 272 &unprotected_prefs_need_cleanup, |
| 212 &protected_prefs_altered); | 273 &protected_prefs_altered, |
| 274 legacy_pref_hash_store_.get()); | |
| 275 | |
| 276 if (!unprotected_prefs_altered && !protected_prefs_altered) { | |
| 277 // Clean up any MACs that might have been previously migrated from the | |
| 278 // various stores. It's safe to leave them behind for a little while as they | |
| 279 // will be ignored unless the corresponding value is _also_ present. The | |
| 280 // cleanup must be deferred until the MACs have been written to their target | |
| 281 // stores, and doing so in a subsequent launch is easier than within the | |
| 282 // same process. | |
| 283 CleanupMigratedHashes(unprotected_pref_names_, | |
| 284 protected_pref_hash_store_.get(), | |
| 285 protected_prefs_.get()); | |
| 286 CleanupMigratedHashes(protected_pref_names_, | |
| 287 unprotected_pref_hash_store_.get(), | |
| 288 unprotected_prefs_.get()); | |
| 289 legacy_pref_hash_store_->Reset(); | |
| 290 } | |
| 213 | 291 |
| 214 // Hand the processed prefs back to their respective filters. | 292 // Hand the processed prefs back to their respective filters. |
| 215 finalize_unprotected_filter_on_load_.Run(unprotected_prefs_.Pass(), | 293 finalize_unprotected_filter_on_load_.Run(unprotected_prefs_.Pass(), |
| 216 unprotected_prefs_altered); | 294 unprotected_prefs_altered); |
| 217 finalize_protected_filter_on_load_.Run(protected_prefs_.Pass(), | 295 finalize_protected_filter_on_load_.Run(protected_prefs_.Pass(), |
| 218 protected_prefs_altered); | 296 protected_prefs_altered); |
| 219 | 297 |
| 220 if (unprotected_prefs_need_cleanup) { | 298 if (unprotected_prefs_need_cleanup) { |
| 221 // Schedule a cleanup of the |protected_pref_names_| from the unprotected | 299 // Schedule a cleanup of the |protected_pref_names_| from the unprotected |
| 222 // prefs once the protected prefs were successfully written to disk (or | 300 // prefs once the protected prefs were successfully written to disk (or |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 245 void SetupTrackedPreferencesMigration( | 323 void SetupTrackedPreferencesMigration( |
| 246 const std::set<std::string>& unprotected_pref_names, | 324 const std::set<std::string>& unprotected_pref_names, |
| 247 const std::set<std::string>& protected_pref_names, | 325 const std::set<std::string>& protected_pref_names, |
| 248 const base::Callback<void(const std::string& key)>& | 326 const base::Callback<void(const std::string& key)>& |
| 249 unprotected_store_cleaner, | 327 unprotected_store_cleaner, |
| 250 const base::Callback<void(const std::string& key)>& protected_store_cleaner, | 328 const base::Callback<void(const std::string& key)>& protected_store_cleaner, |
| 251 const base::Callback<void(const base::Closure&)>& | 329 const base::Callback<void(const base::Closure&)>& |
| 252 register_on_successful_unprotected_store_write_callback, | 330 register_on_successful_unprotected_store_write_callback, |
| 253 const base::Callback<void(const base::Closure&)>& | 331 const base::Callback<void(const base::Closure&)>& |
| 254 register_on_successful_protected_store_write_callback, | 332 register_on_successful_protected_store_write_callback, |
| 333 scoped_ptr<PrefHashStore> unprotected_pref_hash_store, | |
| 334 scoped_ptr<PrefHashStore> protected_pref_hash_store, | |
| 335 scoped_ptr<HashStoreContents> legacy_pref_hash_store, | |
| 255 InterceptablePrefFilter* unprotected_pref_filter, | 336 InterceptablePrefFilter* unprotected_pref_filter, |
| 256 InterceptablePrefFilter* protected_pref_filter) { | 337 InterceptablePrefFilter* protected_pref_filter) { |
| 257 scoped_refptr<TrackedPreferencesMigrator> prefs_migrator( | 338 scoped_refptr<TrackedPreferencesMigrator> prefs_migrator( |
| 258 new TrackedPreferencesMigrator( | 339 new TrackedPreferencesMigrator( |
| 259 unprotected_pref_names, | 340 unprotected_pref_names, |
| 260 protected_pref_names, | 341 protected_pref_names, |
| 261 unprotected_store_cleaner, | 342 unprotected_store_cleaner, |
| 262 protected_store_cleaner, | 343 protected_store_cleaner, |
| 263 register_on_successful_unprotected_store_write_callback, | 344 register_on_successful_unprotected_store_write_callback, |
| 264 register_on_successful_protected_store_write_callback, | 345 register_on_successful_protected_store_write_callback, |
| 346 unprotected_pref_hash_store.Pass(), | |
| 347 protected_pref_hash_store.Pass(), | |
| 348 legacy_pref_hash_store.Pass(), | |
| 265 unprotected_pref_filter, | 349 unprotected_pref_filter, |
| 266 protected_pref_filter)); | 350 protected_pref_filter)); |
| 267 } | 351 } |
| OLD | NEW |