| 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..6e432a57b471981eb2571d3f125a98f90e991fa6
|
| --- /dev/null
|
| +++ b/chrome/browser/prefs/tracked/tracked_preferences_migration.cc
|
| @@ -0,0 +1,192 @@
|
| +// 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 {
|
| + 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();
|
| +
|
| + 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(
|
| + 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,
|
| + JsonPrefStore* protected_pref_store)
|
| + : unprotected_pref_names_(unprotected_pref_names),
|
| + protected_pref_names_(protected_pref_names),
|
| + read_only_(false) {
|
| + // The callbacks bound below will own this TrackedPreferencesMigrator by
|
| + // 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* value_in_old_store = NULL;
|
| + 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, &value_in_old_store)) {
|
| + *old_store_needs_cleanup = true;
|
| + if (!new_store->Get(pref_name, NULL)) {
|
| + // 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, value_in_old_store->DeepCopy());
|
| + *new_store_altered = true;
|
| + }
|
| + }
|
| + }
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +void SetupTrackedPreferencesMigration(
|
| + 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));
|
| +}
|
|
|