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 "services/preferences/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" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "components/user_prefs/tracked/dictionary_hash_store_contents.h" | 15 #include "services/preferences/tracked/dictionary_hash_store_contents.h" |
16 #include "components/user_prefs/tracked/hash_store_contents.h" | 16 #include "services/preferences/tracked/hash_store_contents.h" |
17 #include "components/user_prefs/tracked/interceptable_pref_filter.h" | 17 #include "services/preferences/tracked/interceptable_pref_filter.h" |
18 #include "components/user_prefs/tracked/pref_hash_store.h" | 18 #include "services/preferences/tracked/pref_hash_store.h" |
19 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" | 19 #include "services/preferences/tracked/pref_hash_store_transaction.h" |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 class TrackedPreferencesMigrator | 23 class TrackedPreferencesMigrator |
24 : public base::RefCounted<TrackedPreferencesMigrator> { | 24 : public base::RefCounted<TrackedPreferencesMigrator> { |
25 public: | 25 public: |
26 TrackedPreferencesMigrator( | 26 TrackedPreferencesMigrator( |
27 const std::set<std::string>& unprotected_pref_names, | 27 const std::set<std::string>& unprotected_pref_names, |
28 const std::set<std::string>& protected_pref_names, | 28 const std::set<std::string>& protected_pref_names, |
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 InterceptablePrefFilter* unprotected_pref_filter, | 39 InterceptablePrefFilter* unprotected_pref_filter, |
40 InterceptablePrefFilter* protected_pref_filter); | 40 InterceptablePrefFilter* protected_pref_filter); |
41 | 41 |
42 private: | 42 private: |
43 friend class base::RefCounted<TrackedPreferencesMigrator>; | 43 friend class base::RefCounted<TrackedPreferencesMigrator>; |
44 | 44 |
45 enum PrefFilterID { | 45 enum PrefFilterID { UNPROTECTED_PREF_FILTER, PROTECTED_PREF_FILTER }; |
46 UNPROTECTED_PREF_FILTER, | |
47 PROTECTED_PREF_FILTER | |
48 }; | |
49 | 46 |
50 ~TrackedPreferencesMigrator(); | 47 ~TrackedPreferencesMigrator(); |
51 | 48 |
52 // Stores the data coming in from the filter identified by |id| into this | 49 // Stores the data coming in from the filter identified by |id| into this |
53 // class and then calls MigrateIfReady(); | 50 // class and then calls MigrateIfReady(); |
54 void InterceptFilterOnLoad( | 51 void InterceptFilterOnLoad( |
55 PrefFilterID id, | 52 PrefFilterID id, |
56 const InterceptablePrefFilter::FinalizeFilterOnLoadCallback& | 53 const InterceptablePrefFilter::FinalizeFilterOnLoadCallback& |
57 finalize_filter_on_load, | 54 finalize_filter_on_load, |
58 std::unique_ptr<base::DictionaryValue> prefs); | 55 std::unique_ptr<base::DictionaryValue> prefs); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 DictionaryHashStoreContents dictionary_contents(origin_pref_store); | 120 DictionaryHashStoreContents dictionary_contents(origin_pref_store); |
124 std::unique_ptr<PrefHashStoreTransaction> transaction( | 121 std::unique_ptr<PrefHashStoreTransaction> transaction( |
125 origin_pref_hash_store->BeginTransaction(&dictionary_contents)); | 122 origin_pref_hash_store->BeginTransaction(&dictionary_contents)); |
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 bool* old_store_needs_cleanup, | 137 bool* old_store_needs_cleanup, |
142 bool* new_store_altered) { | 138 bool* new_store_altered) { |
143 const base::DictionaryValue* old_hash_store_contents = | 139 const base::DictionaryValue* old_hash_store_contents = |
144 DictionaryHashStoreContents(old_store).GetContents(); | 140 DictionaryHashStoreContents(old_store).GetContents(); |
145 DictionaryHashStoreContents dictionary_contents(new_store); | 141 DictionaryHashStoreContents dictionary_contents(new_store); |
146 std::unique_ptr<PrefHashStoreTransaction> new_hash_store_transaction( | 142 std::unique_ptr<PrefHashStoreTransaction> new_hash_store_transaction( |
147 new_hash_store->BeginTransaction(&dictionary_contents)); | 143 new_hash_store->BeginTransaction(&dictionary_contents)); |
148 | 144 |
149 for (std::set<std::string>::const_iterator it = pref_names.begin(); | 145 for (std::set<std::string>::const_iterator it = pref_names.begin(); |
150 it != pref_names.end(); | 146 it != pref_names.end(); ++it) { |
151 ++it) { | 147 const std::string& pref_name = *it; |
152 const std::string& pref_name = *it; | 148 const base::Value* value_in_old_store = NULL; |
153 const base::Value* value_in_old_store = NULL; | |
154 | 149 |
155 // If the destination does not have a hash for this pref we will | 150 // If the destination does not have a hash for this pref we will |
156 // unconditionally attempt to move it. | 151 // unconditionally attempt to move it. |
157 bool destination_hash_missing = | 152 bool destination_hash_missing = |
158 !new_hash_store_transaction->HasHash(pref_name); | 153 !new_hash_store_transaction->HasHash(pref_name); |
159 // If we migrate the value we will also attempt to migrate the hash. | 154 // If we migrate the value we will also attempt to migrate the hash. |
160 bool migrated_value = false; | 155 bool migrated_value = false; |
161 if (old_store->Get(pref_name, &value_in_old_store)) { | 156 if (old_store->Get(pref_name, &value_in_old_store)) { |
162 // Whether this value ends up being copied below or was left behind by a | 157 // Whether this value ends up being copied below or was left behind by a |
163 // previous incomplete migration, it should be cleaned up. | 158 // previous incomplete migration, it should be cleaned up. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 protected_store_cleaner_(protected_store_cleaner), | 210 protected_store_cleaner_(protected_store_cleaner), |
216 register_on_successful_unprotected_store_write_callback_( | 211 register_on_successful_unprotected_store_write_callback_( |
217 register_on_successful_unprotected_store_write_callback), | 212 register_on_successful_unprotected_store_write_callback), |
218 register_on_successful_protected_store_write_callback_( | 213 register_on_successful_protected_store_write_callback_( |
219 register_on_successful_protected_store_write_callback), | 214 register_on_successful_protected_store_write_callback), |
220 unprotected_pref_hash_store_(std::move(unprotected_pref_hash_store)), | 215 unprotected_pref_hash_store_(std::move(unprotected_pref_hash_store)), |
221 protected_pref_hash_store_(std::move(protected_pref_hash_store)) { | 216 protected_pref_hash_store_(std::move(protected_pref_hash_store)) { |
222 // The callbacks bound below will own this TrackedPreferencesMigrator by | 217 // The callbacks bound below will own this TrackedPreferencesMigrator by |
223 // reference. | 218 // reference. |
224 unprotected_pref_filter->InterceptNextFilterOnLoad( | 219 unprotected_pref_filter->InterceptNextFilterOnLoad( |
225 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, | 220 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, this, |
226 this, | |
227 UNPROTECTED_PREF_FILTER)); | 221 UNPROTECTED_PREF_FILTER)); |
228 protected_pref_filter->InterceptNextFilterOnLoad( | 222 protected_pref_filter->InterceptNextFilterOnLoad( |
229 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, | 223 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, this, |
230 this, | |
231 PROTECTED_PREF_FILTER)); | 224 PROTECTED_PREF_FILTER)); |
232 } | 225 } |
233 | 226 |
234 TrackedPreferencesMigrator::~TrackedPreferencesMigrator() {} | 227 TrackedPreferencesMigrator::~TrackedPreferencesMigrator() {} |
235 | 228 |
236 void TrackedPreferencesMigrator::InterceptFilterOnLoad( | 229 void TrackedPreferencesMigrator::InterceptFilterOnLoad( |
237 PrefFilterID id, | 230 PrefFilterID id, |
238 const InterceptablePrefFilter::FinalizeFilterOnLoadCallback& | 231 const InterceptablePrefFilter::FinalizeFilterOnLoadCallback& |
239 finalize_filter_on_load, | 232 finalize_filter_on_load, |
240 std::unique_ptr<base::DictionaryValue> prefs) { | 233 std::unique_ptr<base::DictionaryValue> prefs) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 unprotected_prefs_altered); | 283 unprotected_prefs_altered); |
291 finalize_protected_filter_on_load_.Run(std::move(protected_prefs_), | 284 finalize_protected_filter_on_load_.Run(std::move(protected_prefs_), |
292 protected_prefs_altered); | 285 protected_prefs_altered); |
293 | 286 |
294 if (unprotected_prefs_need_cleanup) { | 287 if (unprotected_prefs_need_cleanup) { |
295 // Schedule a cleanup of the |protected_pref_names_| from the unprotected | 288 // Schedule a cleanup of the |protected_pref_names_| from the unprotected |
296 // prefs once the protected prefs were successfully written to disk (or | 289 // prefs once the protected prefs were successfully written to disk (or |
297 // do it immediately if |!protected_prefs_altered|). | 290 // do it immediately if |!protected_prefs_altered|). |
298 ScheduleSourcePrefStoreCleanup( | 291 ScheduleSourcePrefStoreCleanup( |
299 register_on_successful_protected_store_write_callback_, | 292 register_on_successful_protected_store_write_callback_, |
300 unprotected_store_cleaner_, | 293 unprotected_store_cleaner_, protected_pref_names_, |
301 protected_pref_names_, | |
302 protected_prefs_altered); | 294 protected_prefs_altered); |
303 } | 295 } |
304 | 296 |
305 if (protected_prefs_need_cleanup) { | 297 if (protected_prefs_need_cleanup) { |
306 // Schedule a cleanup of the |unprotected_pref_names_| from the protected | 298 // Schedule a cleanup of the |unprotected_pref_names_| from the protected |
307 // prefs once the unprotected prefs were successfully written to disk (or | 299 // prefs once the unprotected prefs were successfully written to disk (or |
308 // do it immediately if |!unprotected_prefs_altered|). | 300 // do it immediately if |!unprotected_prefs_altered|). |
309 ScheduleSourcePrefStoreCleanup( | 301 ScheduleSourcePrefStoreCleanup( |
310 register_on_successful_unprotected_store_write_callback_, | 302 register_on_successful_unprotected_store_write_callback_, |
311 protected_store_cleaner_, | 303 protected_store_cleaner_, unprotected_pref_names_, |
312 unprotected_pref_names_, | |
313 unprotected_prefs_altered); | 304 unprotected_prefs_altered); |
314 } | 305 } |
315 } | 306 } |
316 | 307 |
317 } // namespace | 308 } // namespace |
318 | 309 |
319 void SetupTrackedPreferencesMigration( | 310 void SetupTrackedPreferencesMigration( |
320 const std::set<std::string>& unprotected_pref_names, | 311 const std::set<std::string>& unprotected_pref_names, |
321 const std::set<std::string>& protected_pref_names, | 312 const std::set<std::string>& protected_pref_names, |
322 const base::Callback<void(const std::string& key)>& | 313 const base::Callback<void(const std::string& key)>& |
(...skipping 10 matching lines...) Expand all Loading... |
333 scoped_refptr<TrackedPreferencesMigrator> prefs_migrator( | 324 scoped_refptr<TrackedPreferencesMigrator> prefs_migrator( |
334 new TrackedPreferencesMigrator( | 325 new TrackedPreferencesMigrator( |
335 unprotected_pref_names, protected_pref_names, | 326 unprotected_pref_names, protected_pref_names, |
336 unprotected_store_cleaner, protected_store_cleaner, | 327 unprotected_store_cleaner, protected_store_cleaner, |
337 register_on_successful_unprotected_store_write_callback, | 328 register_on_successful_unprotected_store_write_callback, |
338 register_on_successful_protected_store_write_callback, | 329 register_on_successful_protected_store_write_callback, |
339 std::move(unprotected_pref_hash_store), | 330 std::move(unprotected_pref_hash_store), |
340 std::move(protected_pref_hash_store), unprotected_pref_filter, | 331 std::move(protected_pref_hash_store), unprotected_pref_filter, |
341 protected_pref_filter)); | 332 protected_pref_filter)); |
342 } | 333 } |
OLD | NEW |