Index: chrome/browser/prefs/tracked/tracked_preferences_migration.cc |
diff --git a/chrome/browser/prefs/tracked/tracked_preferences_migration.cc b/chrome/browser/prefs/tracked/tracked_preferences_migration.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1ae0085bbf3ad359ac5ee199061849a0dd013fa1 |
--- /dev/null |
+++ b/chrome/browser/prefs/tracked/tracked_preferences_migration.cc |
@@ -0,0 +1,190 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/prefs/tracked/tracked_preferences_migration.h" |
+ |
+#include "base/bind.h" |
+#include "base/callback.h" |
+#include "base/macros.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/prefs/json_pref_store.h" |
+#include "base/values.h" |
+ |
+namespace { |
+ |
+class TrackedPreferencesMigrator |
+ : public base::RefCounted<TrackedPreferencesMigrator> { |
+ public: |
+ enum PrefStoreID { |
Bernhard Bauer
2014/04/29 16:38:53
This could be private, right?
gab
2014/04/29 20:12:14
Done.
|
+ UNPROTECTED_PREFERENCE_STORE, |
+ PROTECTED_PREFERENCE_STORE |
+ }; |
+ |
+ // Constructs a TrackedPreferencesMigrator which sets itself up as the |
+ // OnFileReadInterceptor for both |unprotected_pref_store| and |
+ // |protected_pref_store|. It guarantees that the most recent pref value |
+ // for |unprotected_pref_names| and |protected_pref_names| are in the |
+ // appropriate JsonPrefStores before those stores even sees those values. |
+ TrackedPreferencesMigrator( |
+ const std::set<std::string>& unprotected_pref_names, |
+ const std::set<std::string>& protected_pref_names, |
+ JsonPrefStore* unprotected_pref_store, |
+ JsonPrefStore* protected_pref_store); |
+ |
+ ~TrackedPreferencesMigrator(); |
Bernhard Bauer
2014/04/29 16:38:53
Destructors should be private for refcounted class
gab
2014/04/29 20:12:14
That's what I wanted to do, but it doesn't work be
Bernhard Bauer
2014/04/29 21:20:28
You'll need to friend the base class, i.e. base::R
gab
2014/04/29 22:36:05
Ah I see, done, thanks!
|
+ |
+ private: |
+ // Stores the data coming in for the store identified by |id| into this class |
+ // and then calls MigrateIfReady(); |
+ void OnFileRead( |
+ PrefStoreID id, |
+ scoped_ptr<base::DictionaryValue> prefs, |
+ bool read_only, |
+ const JsonPrefStore::FinalizePrefsReadCallback& finalize_prefs_read); |
+ |
+ // Proceeds with migration if both |unprotected_prefs_| and |protected_prefs_| |
+ // have been set. |
+ void MigrateIfReady(); |
+ |
+ // Copies the value of each pref in |pref_names| which is set in |old_store|, |
+ // but not in |new_store| into |new_store|. Sets |old_store_needs_cleanup| to |
+ // true if any old duplicates remain in |old_store| and sets |
+ // |new_store_altered| to true if any value was copied to |new_store|. |
+ static void MigratePrefsFromOldToNewStore( |
Bernhard Bauer
2014/04/29 15:13:04
I think this could live outside of the class if it
gab
2014/04/29 15:46:44
I debated both and went this way because it was sl
Bernhard Bauer
2014/04/29 16:38:53
Hm, but class declarations don't need to be groupe
gab
2014/04/29 20:12:14
Ok, done. I put the free-method between TrackedPre
|
+ const std::set<std::string>& pref_names, |
+ const base::DictionaryValue* old_store, |
+ base::DictionaryValue* new_store, |
+ bool* old_store_needs_cleanup, |
+ bool* new_store_altered); |
+ |
+ const std::set<std::string> unprotected_pref_names_; |
+ const std::set<std::string> protected_pref_names_; |
+ |
+ scoped_ptr<base::DictionaryValue> unprotected_prefs_; |
+ scoped_ptr<base::DictionaryValue> protected_prefs_; |
+ |
+ JsonPrefStore::FinalizePrefsReadCallback finalize_unprotected_prefs_read_; |
+ JsonPrefStore::FinalizePrefsReadCallback finalize_protected_prefs_read_; |
+ |
+ // Set to true if either store is reported to be read-only (in which case the |
+ // migration will be copy only -- no deletions). |
+ bool read_only_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TrackedPreferencesMigrator); |
+}; |
+ |
+TrackedPreferencesMigrator::TrackedPreferencesMigrator( |
+ const std::set<std::string>& unprotected_pref_names, |
+ const std::set<std::string>& protected_pref_names, |
+ JsonPrefStore* unprotected_pref_store, |
Bernhard Bauer
2014/04/29 15:13:04
Nit: Unindent by two spaces.
gab
2014/04/29 15:46:44
Done.
|
+ JsonPrefStore* protected_pref_store) |
+ : unprotected_pref_names_(unprotected_pref_names), |
Bernhard Bauer
2014/04/29 15:13:04
Unindent this line and the following ones too, by
gab
2014/04/29 15:46:44
I thought we wanted an extra 4 spaces here, but gi
|
+ protected_pref_names_(protected_pref_names), |
+ read_only_(false) { |
+ // The callbacks binded below will own this TrackedPreferencesMigrator by |
Bernhard Bauer
2014/04/29 15:13:04
Nit: "bound"
gab
2014/04/29 15:46:44
Done.
|
+ // reference. |
+ unprotected_pref_store->InterceptNextFileRead( |
+ base::Bind(&TrackedPreferencesMigrator::OnFileRead, this, |
+ UNPROTECTED_PREFERENCE_STORE)); |
+ protected_pref_store->InterceptNextFileRead( |
+ base::Bind(&TrackedPreferencesMigrator::OnFileRead, this, |
+ PROTECTED_PREFERENCE_STORE)); |
+} |
+ |
+TrackedPreferencesMigrator::~TrackedPreferencesMigrator() {} |
+ |
+void TrackedPreferencesMigrator::OnFileRead( |
+ PrefStoreID id, |
+ scoped_ptr<base::DictionaryValue> prefs, |
+ bool read_only, |
+ const JsonPrefStore::FinalizePrefsReadCallback& finalize_prefs_read) { |
+ if (read_only) |
+ read_only_ = true; |
+ |
+ switch (id) { |
+ case UNPROTECTED_PREFERENCE_STORE: |
+ unprotected_prefs_ = prefs.Pass(); |
+ finalize_unprotected_prefs_read_ = finalize_prefs_read; |
+ break; |
+ case PROTECTED_PREFERENCE_STORE: |
+ protected_prefs_ = prefs.Pass(); |
+ finalize_protected_prefs_read_ = finalize_prefs_read; |
+ break; |
+ } |
+ |
+ MigrateIfReady(); |
+} |
+ |
+void TrackedPreferencesMigrator::MigrateIfReady() { |
+ // Wait for both stores to have been read before proceeding. |
+ if (!protected_prefs_ || !unprotected_prefs_) |
+ return; |
+ |
+ bool protected_prefs_need_cleanup = false; |
+ bool unprotected_prefs_altered = false; |
+ MigratePrefsFromOldToNewStore(unprotected_pref_names_, |
+ protected_prefs_.get(), |
+ unprotected_prefs_.get(), |
+ &protected_prefs_need_cleanup, |
+ &unprotected_prefs_altered); |
+ bool unprotected_prefs_need_cleanup = false; |
+ bool protected_prefs_altered = false; |
+ MigratePrefsFromOldToNewStore(protected_pref_names_, |
+ unprotected_prefs_.get(), |
+ protected_prefs_.get(), |
+ &unprotected_prefs_need_cleanup, |
+ &protected_prefs_altered); |
+ |
+ if (!read_only_) { |
+ // TODO(gab): Register callbacks to cleanup values from their old store when |
+ // we get confirmation that the copies were successfully flushed out to |
+ // their new respective stores. |
+ } |
+ |
+ // Finally, hand the prefs back to their respective JsonPrefStore. |
+ finalize_unprotected_prefs_read_.Run(unprotected_prefs_.Pass(), |
+ unprotected_prefs_altered); |
+ finalize_protected_prefs_read_.Run(protected_prefs_.Pass(), |
+ protected_prefs_altered); |
+} |
+ |
+// static |
+void TrackedPreferencesMigrator::MigratePrefsFromOldToNewStore( |
+ const std::set<std::string>& pref_names, |
+ const base::DictionaryValue* old_store, |
+ base::DictionaryValue* new_store, |
+ bool* old_store_needs_cleanup, |
+ bool* new_store_altered) { |
+ const base::Value* tmp = NULL; |
Bernhard Bauer
2014/04/29 15:13:04
Could you come up with a better name for this vari
gab
2014/04/29 15:46:44
Done.
|
+ for (std::set<std::string>::const_iterator it = pref_names.begin(); |
+ it != pref_names.end(); ++it) { |
+ const std::string& pref_name = *it; |
+ if (old_store->Get(pref_name, &tmp)) { |
Bernhard Bauer
2014/04/29 15:13:04
You could probably invert the conditions here and
gab
2014/04/29 15:46:44
This nesting is required to set |old_store_needs_c
Bernhard Bauer
2014/04/29 16:38:53
Hm, wouldn't the following work?
if (!old_store
gab
2014/04/29 20:12:14
Not sure which one I prefer; I've heard of people
Bernhard Bauer
2014/04/29 21:20:28
Huh. I would have assumed that the compiler would
|
+ *old_store_needs_cleanup = true; |
+ if (!new_store->Get(pref_name, &tmp)) { |
+ // Copy the value from |old_store| to |new_store| rather than moving it |
+ // to avoid data loss should |old_store| be flushed to disk without |
+ // |new_store| having equivalently been successfully flushed to disk |
+ // on crash. |
+ new_store->Set(pref_name, tmp->DeepCopy()); |
+ *new_store_altered = true; |
+ } |
+ } |
+ } |
+} |
+ |
+} // namespace |
+ |
+void SetupTrackedPrefererencesMigration( |
+ const std::set<std::string>& unprotected_pref_names, |
+ const std::set<std::string>& protected_pref_names, |
+ JsonPrefStore* unprotected_pref_store, |
+ JsonPrefStore* protected_pref_store) { |
+ scoped_refptr<TrackedPreferencesMigrator> prefs_migrator( |
+ new TrackedPreferencesMigrator(unprotected_pref_names, |
+ protected_pref_names, |
+ unprotected_pref_store, |
+ protected_pref_store)); |
+} |