Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Side by Side Diff: chrome/browser/prefs/tracked/tracked_preferences_migration.cc

Issue 324493002: Move preference MACs to the protected preference stores. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove some now unnecessary changes. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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();
gab 2014/06/13 01:57:44 Merge the above 2 statements into 1: const base::
erikwright (departed) 2014/06/16 20:51:27 Done.
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());
gab 2014/06/13 01:57:44 inline this below as it was before
erikwright (departed) 2014/06/16 20:51:27 Done.
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
gab 2014/06/13 01:57:44 nit: rm empty line
erikwright (departed) 2014/06/16 20:51:27 Done.
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 // The callbacks bound below will own this TrackedPreferencesMigrator by 220 // The callbacks bound below will own this TrackedPreferencesMigrator by
162 // reference. 221 // reference.
163 unprotected_pref_filter->InterceptNextFilterOnLoad( 222 unprotected_pref_filter->InterceptNextFilterOnLoad(
164 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, 223 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad,
165 this, 224 this,
166 UNPROTECTED_PREF_FILTER)); 225 UNPROTECTED_PREF_FILTER));
167 protected_pref_filter->InterceptNextFilterOnLoad( 226 protected_pref_filter->InterceptNextFilterOnLoad(
168 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad, 227 base::Bind(&TrackedPreferencesMigrator::InterceptFilterOnLoad,
169 this, 228 this,
170 PROTECTED_PREF_FILTER)); 229 PROTECTED_PREF_FILTER));
(...skipping 23 matching lines...) Expand all
194 void TrackedPreferencesMigrator::MigrateIfReady() { 253 void TrackedPreferencesMigrator::MigrateIfReady() {
195 // Wait for both stores to have been read before proceeding. 254 // Wait for both stores to have been read before proceeding.
196 if (!protected_prefs_ || !unprotected_prefs_) 255 if (!protected_prefs_ || !unprotected_prefs_)
197 return; 256 return;
198 257
199 bool protected_prefs_need_cleanup = false; 258 bool protected_prefs_need_cleanup = false;
200 bool unprotected_prefs_altered = false; 259 bool unprotected_prefs_altered = false;
201 MigratePrefsFromOldToNewStore(unprotected_pref_names_, 260 MigratePrefsFromOldToNewStore(unprotected_pref_names_,
202 protected_prefs_.get(), 261 protected_prefs_.get(),
203 unprotected_prefs_.get(), 262 unprotected_prefs_.get(),
263 unprotected_pref_hash_store_.get(),
204 &protected_prefs_need_cleanup, 264 &protected_prefs_need_cleanup,
205 &unprotected_prefs_altered); 265 &unprotected_prefs_altered,
266 legacy_pref_hash_store_.get());
206 bool unprotected_prefs_need_cleanup = false; 267 bool unprotected_prefs_need_cleanup = false;
207 bool protected_prefs_altered = false; 268 bool protected_prefs_altered = false;
208 MigratePrefsFromOldToNewStore(protected_pref_names_, 269 MigratePrefsFromOldToNewStore(protected_pref_names_,
209 unprotected_prefs_.get(), 270 unprotected_prefs_.get(),
210 protected_prefs_.get(), 271 protected_prefs_.get(),
272 protected_pref_hash_store_.get(),
211 &unprotected_prefs_need_cleanup, 273 &unprotected_prefs_need_cleanup,
212 &protected_prefs_altered); 274 &protected_prefs_altered,
275 legacy_pref_hash_store_.get());
276
277
278
gab 2014/06/13 01:57:44 nit: rm 2/3 empty lines
erikwright (departed) 2014/06/16 20:51:27 Done.
279 if (!unprotected_prefs_altered && !protected_prefs_altered) {
gab 2014/06/13 01:57:44 So you will always cleanup the hashes in the next
erikwright (departed) 2014/06/16 20:51:27 Yes it was intentional, and your understanding of
280 CleanupMigratedHashes(unprotected_pref_names_,
281 protected_pref_hash_store_.get(),
282 protected_prefs_.get());
283 CleanupMigratedHashes(protected_pref_names_,
284 unprotected_pref_hash_store_.get(),
285 unprotected_prefs_.get());
286 legacy_pref_hash_store_->Reset();
gab 2014/06/13 01:57:44 One impact of doing this is that we will lose our
erikwright (departed) 2014/06/16 20:51:27 I believe that one of the side effects of this CL
gab 2014/06/17 02:00:05 This doesn't matter, an uninitialized store is al
erikwright (departed) 2014/06/17 17:54:05 Sure, but then probably we should not bother writi
gab 2014/06/17 18:24:30 True, but not trusting it in these scenarios no ma
287 }
213 288
214 // Hand the processed prefs back to their respective filters. 289 // Hand the processed prefs back to their respective filters.
215 finalize_unprotected_filter_on_load_.Run(unprotected_prefs_.Pass(), 290 finalize_unprotected_filter_on_load_.Run(unprotected_prefs_.Pass(),
216 unprotected_prefs_altered); 291 unprotected_prefs_altered);
217 finalize_protected_filter_on_load_.Run(protected_prefs_.Pass(), 292 finalize_protected_filter_on_load_.Run(protected_prefs_.Pass(),
218 protected_prefs_altered); 293 protected_prefs_altered);
219 294
220 if (unprotected_prefs_need_cleanup) { 295 if (unprotected_prefs_need_cleanup) {
221 // Schedule a cleanup of the |protected_pref_names_| from the unprotected 296 // Schedule a cleanup of the |protected_pref_names_| from the unprotected
222 // prefs once the protected prefs were successfully written to disk (or 297 // prefs once the protected prefs were successfully written to disk (or
(...skipping 10 matching lines...) Expand all
233 // prefs once the unprotected prefs were successfully written to disk (or 308 // prefs once the unprotected prefs were successfully written to disk (or
234 // do it immediately if |!unprotected_prefs_altered|). 309 // do it immediately if |!unprotected_prefs_altered|).
235 ScheduleSourcePrefStoreCleanup( 310 ScheduleSourcePrefStoreCleanup(
236 register_on_successful_unprotected_store_write_callback_, 311 register_on_successful_unprotected_store_write_callback_,
237 protected_store_cleaner_, 312 protected_store_cleaner_,
238 unprotected_pref_names_, 313 unprotected_pref_names_,
239 unprotected_prefs_altered); 314 unprotected_prefs_altered);
240 } 315 }
241 } 316 }
242 317
318
gab 2014/06/13 01:57:44 nit: rm empty line
erikwright (departed) 2014/06/16 20:51:27 Done.
243 } // namespace 319 } // namespace
244 320
245 void SetupTrackedPreferencesMigration( 321 void SetupTrackedPreferencesMigration(
246 const std::set<std::string>& unprotected_pref_names, 322 const std::set<std::string>& unprotected_pref_names,
247 const std::set<std::string>& protected_pref_names, 323 const std::set<std::string>& protected_pref_names,
248 const base::Callback<void(const std::string& key)>& 324 const base::Callback<void(const std::string& key)>&
249 unprotected_store_cleaner, 325 unprotected_store_cleaner,
250 const base::Callback<void(const std::string& key)>& protected_store_cleaner, 326 const base::Callback<void(const std::string& key)>& protected_store_cleaner,
251 const base::Callback<void(const base::Closure&)>& 327 const base::Callback<void(const base::Closure&)>&
252 register_on_successful_unprotected_store_write_callback, 328 register_on_successful_unprotected_store_write_callback,
253 const base::Callback<void(const base::Closure&)>& 329 const base::Callback<void(const base::Closure&)>&
254 register_on_successful_protected_store_write_callback, 330 register_on_successful_protected_store_write_callback,
331 scoped_ptr<PrefHashStore> unprotected_pref_hash_store,
332 scoped_ptr<PrefHashStore> protected_pref_hash_store,
333 scoped_ptr<HashStoreContents> legacy_pref_hash_store,
255 InterceptablePrefFilter* unprotected_pref_filter, 334 InterceptablePrefFilter* unprotected_pref_filter,
256 InterceptablePrefFilter* protected_pref_filter) { 335 InterceptablePrefFilter* protected_pref_filter) {
257 scoped_refptr<TrackedPreferencesMigrator> prefs_migrator( 336 scoped_refptr<TrackedPreferencesMigrator> prefs_migrator(
258 new TrackedPreferencesMigrator( 337 new TrackedPreferencesMigrator(
259 unprotected_pref_names, 338 unprotected_pref_names,
260 protected_pref_names, 339 protected_pref_names,
261 unprotected_store_cleaner, 340 unprotected_store_cleaner,
262 protected_store_cleaner, 341 protected_store_cleaner,
263 register_on_successful_unprotected_store_write_callback, 342 register_on_successful_unprotected_store_write_callback,
264 register_on_successful_protected_store_write_callback, 343 register_on_successful_protected_store_write_callback,
344 unprotected_pref_hash_store.Pass(),
345 protected_pref_hash_store.Pass(),
346 legacy_pref_hash_store.Pass(),
265 unprotected_pref_filter, 347 unprotected_pref_filter,
266 protected_pref_filter)); 348 protected_pref_filter));
267 } 349 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698