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" |
(...skipping 29 matching lines...) Expand all Loading... |
40 }; | 40 }; |
41 | 41 |
42 ~TrackedPreferencesMigrator(); | 42 ~TrackedPreferencesMigrator(); |
43 | 43 |
44 // Stores the data coming in from the filter identified by |id| into this | 44 // Stores the data coming in from the filter identified by |id| into this |
45 // class and then calls MigrateIfReady(); | 45 // class and then calls MigrateIfReady(); |
46 void InterceptFilterOnLoad( | 46 void InterceptFilterOnLoad( |
47 PrefFilterID id, | 47 PrefFilterID id, |
48 const InterceptablePrefFilter::FinalizeFilterOnLoadCallback& | 48 const InterceptablePrefFilter::FinalizeFilterOnLoadCallback& |
49 finalize_filter_on_load, | 49 finalize_filter_on_load, |
50 scoped_ptr<base::DictionaryValue> prefs); | 50 base::DictionaryValue* prefs); |
51 | 51 |
52 // Proceeds with migration if both |unprotected_prefs_| and |protected_prefs_| | 52 // Proceeds with migration if both |unprotected_prefs_| and |protected_prefs_| |
53 // have been set. | 53 // have been set. |
54 void MigrateIfReady(); | 54 void MigrateIfReady(); |
55 | 55 |
56 const std::set<std::string> unprotected_pref_names_; | 56 const std::set<std::string> unprotected_pref_names_; |
57 const std::set<std::string> protected_pref_names_; | 57 const std::set<std::string> protected_pref_names_; |
58 | 58 |
59 const base::Callback<void(const std::string& key)> unprotected_store_cleaner_; | 59 const base::Callback<void(const std::string& key)> unprotected_store_cleaner_; |
60 const base::Callback<void(const std::string& key)> protected_store_cleaner_; | 60 const base::Callback<void(const std::string& key)> protected_store_cleaner_; |
61 const base::Callback<void(const base::Closure&)> | 61 const base::Callback<void(const base::Closure&)> |
62 register_on_successful_unprotected_store_write_callback_; | 62 register_on_successful_unprotected_store_write_callback_; |
63 const base::Callback<void(const base::Closure&)> | 63 const base::Callback<void(const base::Closure&)> |
64 register_on_successful_protected_store_write_callback_; | 64 register_on_successful_protected_store_write_callback_; |
65 | 65 |
66 InterceptablePrefFilter::FinalizeFilterOnLoadCallback | 66 InterceptablePrefFilter::FinalizeFilterOnLoadCallback |
67 finalize_unprotected_filter_on_load_; | 67 finalize_unprotected_filter_on_load_; |
68 InterceptablePrefFilter::FinalizeFilterOnLoadCallback | 68 InterceptablePrefFilter::FinalizeFilterOnLoadCallback |
69 finalize_protected_filter_on_load_; | 69 finalize_protected_filter_on_load_; |
70 | 70 |
71 scoped_ptr<base::DictionaryValue> unprotected_prefs_; | 71 base::DictionaryValue* unprotected_prefs_; |
72 scoped_ptr<base::DictionaryValue> protected_prefs_; | 72 base::DictionaryValue* protected_prefs_; |
73 | 73 |
74 DISALLOW_COPY_AND_ASSIGN(TrackedPreferencesMigrator); | 74 DISALLOW_COPY_AND_ASSIGN(TrackedPreferencesMigrator); |
75 }; | 75 }; |
76 | 76 |
77 // Invokes |store_cleaner| for every |keys_to_clean|. | 77 // Invokes |store_cleaner| for every |keys_to_clean|. |
78 void CleanupPrefStore( | 78 void CleanupPrefStore( |
79 const base::Callback<void(const std::string& key)>& store_cleaner, | 79 const base::Callback<void(const std::string& key)>& store_cleaner, |
80 const std::set<std::string>& keys_to_clean) { | 80 const std::set<std::string>& keys_to_clean) { |
81 for (std::set<std::string>::const_iterator it = keys_to_clean.begin(); | 81 for (std::set<std::string>::const_iterator it = keys_to_clean.begin(); |
82 it != keys_to_clean.end(); ++it) { | 82 it != keys_to_clean.end(); ++it) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 register_on_successful_protected_store_write_callback, | 150 register_on_successful_protected_store_write_callback, |
151 InterceptablePrefFilter* unprotected_pref_filter, | 151 InterceptablePrefFilter* unprotected_pref_filter, |
152 InterceptablePrefFilter* protected_pref_filter) | 152 InterceptablePrefFilter* protected_pref_filter) |
153 : unprotected_pref_names_(unprotected_pref_names), | 153 : unprotected_pref_names_(unprotected_pref_names), |
154 protected_pref_names_(protected_pref_names), | 154 protected_pref_names_(protected_pref_names), |
155 unprotected_store_cleaner_(unprotected_store_cleaner), | 155 unprotected_store_cleaner_(unprotected_store_cleaner), |
156 protected_store_cleaner_(protected_store_cleaner), | 156 protected_store_cleaner_(protected_store_cleaner), |
157 register_on_successful_unprotected_store_write_callback_( | 157 register_on_successful_unprotected_store_write_callback_( |
158 register_on_successful_unprotected_store_write_callback), | 158 register_on_successful_unprotected_store_write_callback), |
159 register_on_successful_protected_store_write_callback_( | 159 register_on_successful_protected_store_write_callback_( |
160 register_on_successful_protected_store_write_callback) { | 160 register_on_successful_protected_store_write_callback), |
| 161 unprotected_prefs_(NULL), |
| 162 protected_prefs_(NULL) { |
161 // The callbacks bound below will own this TrackedPreferencesMigrator by | 163 // The callbacks bound below will own this TrackedPreferencesMigrator by |
162 // reference. | 164 // reference. |
163 unprotected_pref_filter->InterceptNextFilterOnLoad( | 165 unprotected_pref_filter->InterceptNextFilterOnLoad( |
164 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, | 166 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, |
165 this, | 167 this, |
166 UNPROTECTED_PREF_FILTER)); | 168 UNPROTECTED_PREF_FILTER)); |
167 protected_pref_filter->InterceptNextFilterOnLoad( | 169 protected_pref_filter->InterceptNextFilterOnLoad( |
168 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, | 170 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, |
169 this, | 171 this, |
170 PROTECTED_PREF_FILTER)); | 172 PROTECTED_PREF_FILTER)); |
171 } | 173 } |
172 | 174 |
173 TrackedPreferencesMigrator::~TrackedPreferencesMigrator() {} | 175 TrackedPreferencesMigrator::~TrackedPreferencesMigrator() {} |
174 | 176 |
175 void TrackedPreferencesMigrator::InterceptFilterOnLoad( | 177 void TrackedPreferencesMigrator::InterceptFilterOnLoad( |
176 PrefFilterID id, | 178 PrefFilterID id, |
177 const InterceptablePrefFilter::FinalizeFilterOnLoadCallback& | 179 const InterceptablePrefFilter::FinalizeFilterOnLoadCallback& |
178 finalize_filter_on_load, | 180 finalize_filter_on_load, |
179 scoped_ptr<base::DictionaryValue> prefs) { | 181 base::DictionaryValue* prefs) { |
180 switch (id) { | 182 switch (id) { |
181 case UNPROTECTED_PREF_FILTER: | 183 case UNPROTECTED_PREF_FILTER: |
182 finalize_unprotected_filter_on_load_ = finalize_filter_on_load; | 184 finalize_unprotected_filter_on_load_ = finalize_filter_on_load; |
183 unprotected_prefs_ = prefs.Pass(); | 185 unprotected_prefs_ = prefs; |
184 break; | 186 break; |
185 case PROTECTED_PREF_FILTER: | 187 case PROTECTED_PREF_FILTER: |
186 finalize_protected_filter_on_load_ = finalize_filter_on_load; | 188 finalize_protected_filter_on_load_ = finalize_filter_on_load; |
187 protected_prefs_ = prefs.Pass(); | 189 protected_prefs_ = prefs; |
188 break; | 190 break; |
189 } | 191 } |
190 | 192 |
191 MigrateIfReady(); | 193 MigrateIfReady(); |
192 } | 194 } |
193 | 195 |
194 void TrackedPreferencesMigrator::MigrateIfReady() { | 196 void TrackedPreferencesMigrator::MigrateIfReady() { |
195 // Wait for both stores to have been read before proceeding. | 197 // Wait for both stores to have been read before proceeding. |
196 if (!protected_prefs_ || !unprotected_prefs_) | 198 if (!protected_prefs_ || !unprotected_prefs_) |
197 return; | 199 return; |
198 | 200 |
199 bool protected_prefs_need_cleanup = false; | 201 bool protected_prefs_need_cleanup = false; |
200 bool unprotected_prefs_altered = false; | 202 bool unprotected_prefs_altered = false; |
201 MigratePrefsFromOldToNewStore(unprotected_pref_names_, | 203 MigratePrefsFromOldToNewStore(unprotected_pref_names_, |
202 protected_prefs_.get(), | 204 protected_prefs_, |
203 unprotected_prefs_.get(), | 205 unprotected_prefs_, |
204 &protected_prefs_need_cleanup, | 206 &protected_prefs_need_cleanup, |
205 &unprotected_prefs_altered); | 207 &unprotected_prefs_altered); |
206 bool unprotected_prefs_need_cleanup = false; | 208 bool unprotected_prefs_need_cleanup = false; |
207 bool protected_prefs_altered = false; | 209 bool protected_prefs_altered = false; |
208 MigratePrefsFromOldToNewStore(protected_pref_names_, | 210 MigratePrefsFromOldToNewStore(protected_pref_names_, |
209 unprotected_prefs_.get(), | 211 unprotected_prefs_, |
210 protected_prefs_.get(), | 212 protected_prefs_, |
211 &unprotected_prefs_need_cleanup, | 213 &unprotected_prefs_need_cleanup, |
212 &protected_prefs_altered); | 214 &protected_prefs_altered); |
213 | 215 |
214 // Hand the processed prefs back to their respective filters. | 216 // Allow preference loading to proceed. |
215 finalize_unprotected_filter_on_load_.Run(unprotected_prefs_.Pass(), | 217 unprotected_prefs_ = NULL; |
216 unprotected_prefs_altered); | 218 protected_prefs_ = NULL; |
217 finalize_protected_filter_on_load_.Run(protected_prefs_.Pass(), | 219 finalize_unprotected_filter_on_load_.Run(unprotected_prefs_altered); |
218 protected_prefs_altered); | 220 finalize_protected_filter_on_load_.Run(protected_prefs_altered); |
219 | 221 |
220 if (unprotected_prefs_need_cleanup) { | 222 if (unprotected_prefs_need_cleanup) { |
221 // Schedule a cleanup of the |protected_pref_names_| from the unprotected | 223 // Schedule a cleanup of the |protected_pref_names_| from the unprotected |
222 // prefs once the protected prefs were successfully written to disk (or | 224 // prefs once the protected prefs were successfully written to disk (or |
223 // do it immediately if |!protected_prefs_altered|). | 225 // do it immediately if |!protected_prefs_altered|). |
224 ScheduleSourcePrefStoreCleanup( | 226 ScheduleSourcePrefStoreCleanup( |
225 register_on_successful_protected_store_write_callback_, | 227 register_on_successful_protected_store_write_callback_, |
226 unprotected_store_cleaner_, | 228 unprotected_store_cleaner_, |
227 protected_pref_names_, | 229 protected_pref_names_, |
228 protected_prefs_altered); | 230 protected_prefs_altered); |
(...skipping 29 matching lines...) Expand all Loading... |
258 new TrackedPreferencesMigrator( | 260 new TrackedPreferencesMigrator( |
259 unprotected_pref_names, | 261 unprotected_pref_names, |
260 protected_pref_names, | 262 protected_pref_names, |
261 unprotected_store_cleaner, | 263 unprotected_store_cleaner, |
262 protected_store_cleaner, | 264 protected_store_cleaner, |
263 register_on_successful_unprotected_store_write_callback, | 265 register_on_successful_unprotected_store_write_callback, |
264 register_on_successful_protected_store_write_callback, | 266 register_on_successful_protected_store_write_callback, |
265 unprotected_pref_filter, | 267 unprotected_pref_filter, |
266 protected_pref_filter)); | 268 protected_pref_filter)); |
267 } | 269 } |
OLD | NEW |