| 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" |
| 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 base::DictionaryValue* unprotected_prefs_; | 82 base::DictionaryValue* unprotected_prefs_; |
| 72 base::DictionaryValue* protected_prefs_; | 83 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) { |
| 129 DictionaryHashStoreContents old_hash_store(old_store); |
| 130 const base::DictionaryValue* old_hash_store_contents = |
| 131 old_hash_store.GetContents(); |
| 132 const base::DictionaryValue* legacy_hash_store_contents = |
| 133 legacy_hash_store->GetContents(); |
| 134 scoped_ptr<PrefHashStoreTransaction> new_hash_store_transaction( |
| 135 new_hash_store->BeginTransaction(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 scoped_ptr<base::Value> value_copy(value_in_old_store->DeepCopy()); |
| 161 new_store->Set(pref_name, value_copy.release()); |
| 162 migrated_value = true; |
| 163 *new_store_altered = true; |
| 164 } |
| 165 } |
| 127 | 166 |
| 128 if (new_store->Get(pref_name, NULL)) | 167 if (destination_hash_missing || migrated_value) { |
| 129 continue; | 168 const base::Value* old_hash = NULL; |
| 130 | 169 if (old_hash_store_contents) |
| 131 // Copy the value from |old_store| to |new_store| rather than moving it to | 170 old_hash_store_contents->Get(pref_name, &old_hash); |
| 132 // avoid data loss should |old_store| be flushed to disk without |new_store| | 171 if (!old_hash && legacy_hash_store_contents) |
| 133 // having equivalently been successfully flushed to disk (e.g., on crash or | 172 legacy_hash_store_contents->Get(pref_name, &old_hash); |
| 134 // in cases where |new_store| is read-only following a read error on | 173 if (old_hash || !destination_hash_missing) { |
| 135 // startup). | 174 new_hash_store_transaction->ImportHash(pref_name, old_hash); |
| 136 new_store->Set(pref_name, value_in_old_store->DeepCopy()); | 175 *new_store_altered = true; |
| 137 *new_store_altered = true; | 176 } |
| 177 } |
| 138 } | 178 } |
| 139 } | 179 } |
| 140 | 180 |
| 181 void CleanupMigratedHashes(const std::set<std::string>& migrated_pref_names, |
| 182 PrefHashStore* origin_pref_hash_store, |
| 183 base::DictionaryValue* origin_pref_store) { |
| 184 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 185 origin_pref_hash_store->BeginTransaction(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 |
| 193 |
| 141 TrackedPreferencesMigrator::TrackedPreferencesMigrator( | 194 TrackedPreferencesMigrator::TrackedPreferencesMigrator( |
| 142 const std::set<std::string>& unprotected_pref_names, | 195 const std::set<std::string>& unprotected_pref_names, |
| 143 const std::set<std::string>& protected_pref_names, | 196 const std::set<std::string>& protected_pref_names, |
| 144 const base::Callback<void(const std::string& key)>& | 197 const base::Callback<void(const std::string& key)>& |
| 145 unprotected_store_cleaner, | 198 unprotected_store_cleaner, |
| 146 const base::Callback<void(const std::string& key)>& protected_store_cleaner, | 199 const base::Callback<void(const std::string& key)>& protected_store_cleaner, |
| 147 const base::Callback<void(const base::Closure&)>& | 200 const base::Callback<void(const base::Closure&)>& |
| 148 register_on_successful_unprotected_store_write_callback, | 201 register_on_successful_unprotected_store_write_callback, |
| 149 const base::Callback<void(const base::Closure&)>& | 202 const base::Callback<void(const base::Closure&)>& |
| 150 register_on_successful_protected_store_write_callback, | 203 register_on_successful_protected_store_write_callback, |
| 204 scoped_ptr<PrefHashStore> unprotected_pref_hash_store, |
| 205 scoped_ptr<PrefHashStore> protected_pref_hash_store, |
| 206 scoped_ptr<HashStoreContents> legacy_pref_hash_store, |
| 151 InterceptablePrefFilter* unprotected_pref_filter, | 207 InterceptablePrefFilter* unprotected_pref_filter, |
| 152 InterceptablePrefFilter* protected_pref_filter) | 208 InterceptablePrefFilter* protected_pref_filter) |
| 153 : unprotected_pref_names_(unprotected_pref_names), | 209 : unprotected_pref_names_(unprotected_pref_names), |
| 154 protected_pref_names_(protected_pref_names), | 210 protected_pref_names_(protected_pref_names), |
| 155 unprotected_store_cleaner_(unprotected_store_cleaner), | 211 unprotected_store_cleaner_(unprotected_store_cleaner), |
| 156 protected_store_cleaner_(protected_store_cleaner), | 212 protected_store_cleaner_(protected_store_cleaner), |
| 157 register_on_successful_unprotected_store_write_callback_( | 213 register_on_successful_unprotected_store_write_callback_( |
| 158 register_on_successful_unprotected_store_write_callback), | 214 register_on_successful_unprotected_store_write_callback), |
| 159 register_on_successful_protected_store_write_callback_( | 215 register_on_successful_protected_store_write_callback_( |
| 160 register_on_successful_protected_store_write_callback), | 216 register_on_successful_protected_store_write_callback), |
| 217 unprotected_pref_hash_store_(unprotected_pref_hash_store.Pass()), |
| 218 protected_pref_hash_store_(protected_pref_hash_store.Pass()), |
| 219 legacy_pref_hash_store_(legacy_pref_hash_store.Pass()), |
| 161 unprotected_prefs_(NULL), | 220 unprotected_prefs_(NULL), |
| 162 protected_prefs_(NULL) { | 221 protected_prefs_(NULL) { |
| 163 // The callbacks bound below will own this TrackedPreferencesMigrator by | 222 // The callbacks bound below will own this TrackedPreferencesMigrator by |
| 164 // reference. | 223 // reference. |
| 165 unprotected_pref_filter->InterceptNextFilterOnLoad( | 224 unprotected_pref_filter->InterceptNextFilterOnLoad( |
| 166 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, | 225 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, |
| 167 this, | 226 this, |
| 168 UNPROTECTED_PREF_FILTER)); | 227 UNPROTECTED_PREF_FILTER)); |
| 169 protected_pref_filter->InterceptNextFilterOnLoad( | 228 protected_pref_filter->InterceptNextFilterOnLoad( |
| 170 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, | 229 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 196 void TrackedPreferencesMigrator::MigrateIfReady() { | 255 void TrackedPreferencesMigrator::MigrateIfReady() { |
| 197 // Wait for both stores to have been read before proceeding. | 256 // Wait for both stores to have been read before proceeding. |
| 198 if (!protected_prefs_ || !unprotected_prefs_) | 257 if (!protected_prefs_ || !unprotected_prefs_) |
| 199 return; | 258 return; |
| 200 | 259 |
| 201 bool protected_prefs_need_cleanup = false; | 260 bool protected_prefs_need_cleanup = false; |
| 202 bool unprotected_prefs_altered = false; | 261 bool unprotected_prefs_altered = false; |
| 203 MigratePrefsFromOldToNewStore(unprotected_pref_names_, | 262 MigratePrefsFromOldToNewStore(unprotected_pref_names_, |
| 204 protected_prefs_, | 263 protected_prefs_, |
| 205 unprotected_prefs_, | 264 unprotected_prefs_, |
| 265 unprotected_pref_hash_store_.get(), |
| 206 &protected_prefs_need_cleanup, | 266 &protected_prefs_need_cleanup, |
| 207 &unprotected_prefs_altered); | 267 &unprotected_prefs_altered, |
| 268 legacy_pref_hash_store_.get()); |
| 208 bool unprotected_prefs_need_cleanup = false; | 269 bool unprotected_prefs_need_cleanup = false; |
| 209 bool protected_prefs_altered = false; | 270 bool protected_prefs_altered = false; |
| 210 MigratePrefsFromOldToNewStore(protected_pref_names_, | 271 MigratePrefsFromOldToNewStore(protected_pref_names_, |
| 211 unprotected_prefs_, | 272 unprotected_prefs_, |
| 212 protected_prefs_, | 273 protected_prefs_, |
| 274 protected_pref_hash_store_.get(), |
| 213 &unprotected_prefs_need_cleanup, | 275 &unprotected_prefs_need_cleanup, |
| 214 &protected_prefs_altered); | 276 &protected_prefs_altered, |
| 277 legacy_pref_hash_store_.get()); |
| 278 |
| 279 |
| 280 |
| 281 if (!unprotected_prefs_altered && !protected_prefs_altered) { |
| 282 CleanupMigratedHashes(unprotected_pref_names_, |
| 283 protected_pref_hash_store_.get(), |
| 284 protected_prefs_); |
| 285 CleanupMigratedHashes(protected_pref_names_, |
| 286 unprotected_pref_hash_store_.get(), |
| 287 unprotected_prefs_); |
| 288 legacy_pref_hash_store_->Reset(); |
| 289 } |
| 215 | 290 |
| 216 // Allow preference loading to proceed. | 291 // Allow preference loading to proceed. |
| 217 unprotected_prefs_ = NULL; | 292 unprotected_prefs_ = NULL; |
| 218 protected_prefs_ = NULL; | 293 protected_prefs_ = NULL; |
| 219 finalize_unprotected_filter_on_load_.Run(unprotected_prefs_altered); | 294 finalize_unprotected_filter_on_load_.Run(unprotected_prefs_altered); |
| 220 finalize_protected_filter_on_load_.Run(protected_prefs_altered); | 295 finalize_protected_filter_on_load_.Run(protected_prefs_altered); |
| 221 | 296 |
| 222 if (unprotected_prefs_need_cleanup) { | 297 if (unprotected_prefs_need_cleanup) { |
| 223 // Schedule a cleanup of the |protected_pref_names_| from the unprotected | 298 // Schedule a cleanup of the |protected_pref_names_| from the unprotected |
| 224 // prefs once the protected prefs were successfully written to disk (or | 299 // prefs once the protected prefs were successfully written to disk (or |
| (...skipping 10 matching lines...) Expand all Loading... |
| 235 // prefs once the unprotected prefs were successfully written to disk (or | 310 // prefs once the unprotected prefs were successfully written to disk (or |
| 236 // do it immediately if |!unprotected_prefs_altered|). | 311 // do it immediately if |!unprotected_prefs_altered|). |
| 237 ScheduleSourcePrefStoreCleanup( | 312 ScheduleSourcePrefStoreCleanup( |
| 238 register_on_successful_unprotected_store_write_callback_, | 313 register_on_successful_unprotected_store_write_callback_, |
| 239 protected_store_cleaner_, | 314 protected_store_cleaner_, |
| 240 unprotected_pref_names_, | 315 unprotected_pref_names_, |
| 241 unprotected_prefs_altered); | 316 unprotected_prefs_altered); |
| 242 } | 317 } |
| 243 } | 318 } |
| 244 | 319 |
| 320 |
| 245 } // namespace | 321 } // namespace |
| 246 | 322 |
| 247 void SetupTrackedPreferencesMigration( | 323 void SetupTrackedPreferencesMigration( |
| 248 const std::set<std::string>& unprotected_pref_names, | 324 const std::set<std::string>& unprotected_pref_names, |
| 249 const std::set<std::string>& protected_pref_names, | 325 const std::set<std::string>& protected_pref_names, |
| 250 const base::Callback<void(const std::string& key)>& | 326 const base::Callback<void(const std::string& key)>& |
| 251 unprotected_store_cleaner, | 327 unprotected_store_cleaner, |
| 252 const base::Callback<void(const std::string& key)>& protected_store_cleaner, | 328 const base::Callback<void(const std::string& key)>& protected_store_cleaner, |
| 253 const base::Callback<void(const base::Closure&)>& | 329 const base::Callback<void(const base::Closure&)>& |
| 254 register_on_successful_unprotected_store_write_callback, | 330 register_on_successful_unprotected_store_write_callback, |
| 255 const base::Callback<void(const base::Closure&)>& | 331 const base::Callback<void(const base::Closure&)>& |
| 256 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, |
| 257 InterceptablePrefFilter* unprotected_pref_filter, | 336 InterceptablePrefFilter* unprotected_pref_filter, |
| 258 InterceptablePrefFilter* protected_pref_filter) { | 337 InterceptablePrefFilter* protected_pref_filter) { |
| 259 scoped_refptr<TrackedPreferencesMigrator> prefs_migrator( | 338 scoped_refptr<TrackedPreferencesMigrator> prefs_migrator( |
| 260 new TrackedPreferencesMigrator( | 339 new TrackedPreferencesMigrator( |
| 261 unprotected_pref_names, | 340 unprotected_pref_names, |
| 262 protected_pref_names, | 341 protected_pref_names, |
| 263 unprotected_store_cleaner, | 342 unprotected_store_cleaner, |
| 264 protected_store_cleaner, | 343 protected_store_cleaner, |
| 265 register_on_successful_unprotected_store_write_callback, | 344 register_on_successful_unprotected_store_write_callback, |
| 266 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(), |
| 267 unprotected_pref_filter, | 349 unprotected_pref_filter, |
| 268 protected_pref_filter)); | 350 protected_pref_filter)); |
| 269 } | 351 } |
| OLD | NEW |