| 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..552a089712ed1eb473f6ff8ab4cfc0ecb74d85aa
|
| --- /dev/null
|
| +++ b/chrome/browser/prefs/tracked/tracked_preferences_migration.cc
|
| @@ -0,0 +1,239 @@
|
| +// 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/memory/weak_ptr.h"
|
| +#include "base/prefs/json_pref_store.h"
|
| +#include "base/values.h"
|
| +
|
| +namespace {
|
| +
|
| +class TrackedPreferencesMigrator
|
| + : public base::RefCounted<TrackedPreferencesMigrator> {
|
| + public:
|
| + // 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);
|
| +
|
| + private:
|
| + friend class base::RefCounted<TrackedPreferencesMigrator>;
|
| +
|
| + enum PrefStoreID {
|
| + UNPROTECTED_PREFERENCE_STORE,
|
| + PROTECTED_PREFERENCE_STORE
|
| + };
|
| +
|
| + ~TrackedPreferencesMigrator();
|
| +
|
| + // 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,
|
| + const JsonPrefStore::FinalizePrefsReadCallback& finalize_prefs_read);
|
| +
|
| + // Proceeds with migration if both |unprotected_prefs_| and |protected_prefs_|
|
| + // have been set.
|
| + void MigrateIfReady();
|
| +
|
| + 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_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(TrackedPreferencesMigrator);
|
| +};
|
| +
|
| +// Silently removes all |keys_to_clean| from |pref_store| if the weak pointer is
|
| +// still valid.
|
| +void CleanupPrefStore(const base::WeakPtr<JsonPrefStore>& pref_store,
|
| + const std::set<std::string>& keys_to_clean) {
|
| + if (pref_store) {
|
| + for (std::set<std::string>::const_iterator it = keys_to_clean.begin();
|
| + it != keys_to_clean.end(); ++it) {
|
| + // Silently clean the values as they have already been copied to their
|
| + // new authoritative store and we don't want to alert PrefStore observers
|
| + // about the irrelevant removal of this value which no one should be
|
| + // reading anymore.
|
| + pref_store->RemoveValueSilently(*it);
|
| + }
|
| + }
|
| +}
|
| +
|
| +// If |wait_for_commit_to_destination_store|: schedules a cleanup of the
|
| +// |keys_to_clean| from the |source_pref_store| once the
|
| +// |destination_pref_store| they were migrated to was successfully written to
|
| +// disk. Otherwise, executes the cleanup right away.
|
| +void ScheduleSourcePrefStoreCleanup(
|
| + const base::WeakPtr<JsonPrefStore>& source_pref_store,
|
| + const std::set<std::string>& keys_to_clean,
|
| + bool wait_for_commit_to_destination_store,
|
| + JsonPrefStore* destination_pref_store) {
|
| + DCHECK(source_pref_store);
|
| + DCHECK(destination_pref_store);
|
| + DCHECK(!keys_to_clean.empty());
|
| + if (wait_for_commit_to_destination_store) {
|
| + destination_pref_store->RegisterOnNextSuccessfulWriteCallback(
|
| + base::Bind(&CleanupPrefStore, source_pref_store, keys_to_clean));
|
| + } else {
|
| + CleanupPrefStore(source_pref_store, keys_to_clean);
|
| + }
|
| +}
|
| +
|
| +// 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|.
|
| +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) {
|
| + for (std::set<std::string>::const_iterator it = pref_names.begin();
|
| + it != pref_names.end(); ++it) {
|
| + const std::string& pref_name = *it;
|
| +
|
| + const base::Value* value_in_old_store = NULL;
|
| + if (!old_store->Get(pref_name, &value_in_old_store))
|
| + continue;
|
| +
|
| + // Whether this value ends up being copied below or was left behind by a
|
| + // previous incomplete migration, it should be cleaned up.
|
| + *old_store_needs_cleanup = true;
|
| +
|
| + if (new_store->Get(pref_name, NULL))
|
| + continue;
|
| +
|
| + // 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 (e.g., on crash or
|
| + // in cases where |new_store| is read-only following a read error on
|
| + // startup).
|
| + new_store->Set(pref_name, value_in_old_store->DeepCopy());
|
| + *new_store_altered = true;
|
| + }
|
| +}
|
| +
|
| +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) {
|
| + // 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,
|
| + const JsonPrefStore::FinalizePrefsReadCallback& finalize_prefs_read) {
|
| + 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);
|
| +
|
| + // Hand the processed prefs back to their respective JsonPrefStore and keep
|
| + // weak pointers to those stores to make a best effort attempt at cleaning up
|
| + // duplicate values once those values' destination store was successfully
|
| + // written to disk.
|
| + base::WeakPtr<JsonPrefStore> unprotected_pref_store =
|
| + finalize_unprotected_prefs_read_.Run(unprotected_prefs_.Pass(),
|
| + unprotected_prefs_altered);
|
| + base::WeakPtr<JsonPrefStore> protected_pref_store =
|
| + finalize_protected_prefs_read_.Run(protected_prefs_.Pass(),
|
| + protected_prefs_altered);
|
| + DCHECK(unprotected_pref_store);
|
| + DCHECK(protected_pref_store);
|
| +
|
| + if (unprotected_prefs_need_cleanup) {
|
| + // Cleanup the |protected_pref_names_| from the |unprotected_pref_store|
|
| + // once they were successfully written to the |protected_pref_store|.
|
| + ScheduleSourcePrefStoreCleanup(unprotected_pref_store,
|
| + protected_pref_names_,
|
| + protected_prefs_altered,
|
| + protected_pref_store.get());
|
| + }
|
| +
|
| + if (protected_prefs_need_cleanup) {
|
| + // Cleanup the |unprotected_pref_names_| from the |protected_pref_store|
|
| + // once they were successfully written to the |unprotected_pref_store|.
|
| + ScheduleSourcePrefStoreCleanup(protected_pref_store,
|
| + unprotected_pref_names_,
|
| + unprotected_prefs_altered,
|
| + unprotected_pref_store.get());
|
| + }
|
| +}
|
| +
|
| +} // 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));
|
| +}
|
|
|