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

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

Issue 257003007: Introduce a new framework for back-and-forth tracked/protected preferences migration. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: git cl format Created 6 years, 7 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/prefs/tracked/tracked_preferences_migration.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/prefs/json_pref_store.h"
13 #include "base/values.h"
14
15 namespace {
16
17 class TrackedPreferencesMigrator
18 : public base::RefCounted<TrackedPreferencesMigrator> {
19 public:
20 enum PrefStoreID {
21 UNPROTECTED_PREFERENCE_STORE,
22 PROTECTED_PREFERENCE_STORE
23 };
24
25 // Constructs a TrackedPreferencesMigrator which sets itself up as the
26 // OnFileReadInterceptor for both |unprotected_pref_store| and
27 // |protected_pref_store|. It guarantees that the most recent pref value
28 // for |unprotected_pref_names| and |protected_pref_names| are in the
29 // appropriate JsonPrefStores before those stores even sees those values.
30 TrackedPreferencesMigrator(
31 const std::set<std::string>& unprotected_pref_names,
32 const std::set<std::string>& protected_pref_names,
33 JsonPrefStore* unprotected_pref_store,
34 JsonPrefStore* protected_pref_store);
35
36 ~TrackedPreferencesMigrator();
37
38 private:
39 // Stores the data coming in for the store identified by |id| into this class
40 // and then calls MigrateIfReady();
41 void OnFileRead(
42 PrefStoreID id,
43 scoped_ptr<base::DictionaryValue> prefs,
44 bool read_only,
45 const JsonPrefStore::FinalizePrefsReadCallback& finalize_prefs_read);
46
47 // Proceeds with migration if both |unprotected_prefs_| and |protected_prefs_|
48 // have been set.
49 void MigrateIfReady();
50
51 // Copies the value of each pref in |pref_names| which is set in |old_store|,
52 // but not in |new_store| into |new_store|. Sets |old_store_needs_cleanup| to
53 // true if any old duplicates remain in |old_store| and sets
54 // |new_store_altered| to true if any value was copied to |new_store|.
55 static void MigratePrefsFromOldToNewStore(
56 const std::set<std::string>& pref_names,
57 const base::DictionaryValue* old_store,
58 base::DictionaryValue* new_store,
59 bool* old_store_needs_cleanup,
60 bool* new_store_altered);
61
62 const std::set<std::string> unprotected_pref_names_;
63 const std::set<std::string> protected_pref_names_;
64
65 scoped_ptr<base::DictionaryValue> unprotected_prefs_;
66 scoped_ptr<base::DictionaryValue> protected_prefs_;
67
68 JsonPrefStore::FinalizePrefsReadCallback finalize_unprotected_prefs_read_;
69 JsonPrefStore::FinalizePrefsReadCallback finalize_protected_prefs_read_;
70
71 // Set to true if either store is reported to be read-only (in which case the
72 // migration will be copy only -- no deletions).
73 bool read_only_;
74
75 DISALLOW_COPY_AND_ASSIGN(TrackedPreferencesMigrator);
76 };
77
78 TrackedPreferencesMigrator::TrackedPreferencesMigrator(
79 const std::set<std::string>& unprotected_pref_names,
80 const std::set<std::string>& protected_pref_names,
81 JsonPrefStore* unprotected_pref_store,
82 JsonPrefStore* protected_pref_store)
83 : unprotected_pref_names_(unprotected_pref_names),
84 protected_pref_names_(protected_pref_names),
85 read_only_(false) {
86 // The callbacks bound below will own this TrackedPreferencesMigrator by
87 // reference.
88 unprotected_pref_store->InterceptNextFileRead(
89 base::Bind(&TrackedPreferencesMigrator::OnFileRead,
90 this,
91 UNPROTECTED_PREFERENCE_STORE));
92 protected_pref_store->InterceptNextFileRead(
93 base::Bind(&TrackedPreferencesMigrator::OnFileRead,
94 this,
95 PROTECTED_PREFERENCE_STORE));
96 }
97
98 TrackedPreferencesMigrator::~TrackedPreferencesMigrator() {}
99
100 void TrackedPreferencesMigrator::OnFileRead(
101 PrefStoreID id,
102 scoped_ptr<base::DictionaryValue> prefs,
103 bool read_only,
104 const JsonPrefStore::FinalizePrefsReadCallback& finalize_prefs_read) {
105 if (read_only)
106 read_only_ = true;
107
108 switch (id) {
109 case UNPROTECTED_PREFERENCE_STORE:
110 unprotected_prefs_ = prefs.Pass();
111 finalize_unprotected_prefs_read_ = finalize_prefs_read;
112 break;
113 case PROTECTED_PREFERENCE_STORE:
114 protected_prefs_ = prefs.Pass();
115 finalize_protected_prefs_read_ = finalize_prefs_read;
116 break;
117 }
118
119 MigrateIfReady();
120 }
121
122 void TrackedPreferencesMigrator::MigrateIfReady() {
123 // Wait for both stores to have been read before proceeding.
124 if (!protected_prefs_ || !unprotected_prefs_)
125 return;
126
127 bool protected_prefs_need_cleanup = false;
128 bool unprotected_prefs_altered = false;
129 MigratePrefsFromOldToNewStore(unprotected_pref_names_,
130 protected_prefs_.get(),
131 unprotected_prefs_.get(),
132 &protected_prefs_need_cleanup,
133 &unprotected_prefs_altered);
134 bool unprotected_prefs_need_cleanup = false;
135 bool protected_prefs_altered = false;
136 MigratePrefsFromOldToNewStore(protected_pref_names_,
137 unprotected_prefs_.get(),
138 protected_prefs_.get(),
139 &unprotected_prefs_need_cleanup,
140 &protected_prefs_altered);
141
142 if (!read_only_) {
143 // TODO(gab): Register callbacks to cleanup values from their old store when
144 // we get confirmation that the copies were successfully flushed out to
145 // their new respective stores.
146 }
147
148 // Finally, hand the prefs back to their respective JsonPrefStore.
149 finalize_unprotected_prefs_read_.Run(unprotected_prefs_.Pass(),
150 unprotected_prefs_altered);
151 finalize_protected_prefs_read_.Run(protected_prefs_.Pass(),
152 protected_prefs_altered);
153 }
154
155 // static
156 void TrackedPreferencesMigrator::MigratePrefsFromOldToNewStore(
157 const std::set<std::string>& pref_names,
158 const base::DictionaryValue* old_store,
159 base::DictionaryValue* new_store,
160 bool* old_store_needs_cleanup,
161 bool* new_store_altered) {
162 const base::Value* value_in_old_store = NULL;
163 for (std::set<std::string>::const_iterator it = pref_names.begin();
164 it != pref_names.end(); ++it) {
165 const std::string& pref_name = *it;
166 if (old_store->Get(pref_name, &value_in_old_store)) {
167 *old_store_needs_cleanup = true;
168 if (!new_store->Get(pref_name, NULL)) {
169 // Copy the value from |old_store| to |new_store| rather than moving it
170 // to avoid data loss should |old_store| be flushed to disk without
171 // |new_store| having equivalently been successfully flushed to disk
172 // on crash.
173 new_store->Set(pref_name, value_in_old_store->DeepCopy());
174 *new_store_altered = true;
175 }
176 }
177 }
178 }
179
180 } // namespace
181
182 void SetupTrackedPreferencesMigration(
183 const std::set<std::string>& unprotected_pref_names,
184 const std::set<std::string>& protected_pref_names,
185 JsonPrefStore* unprotected_pref_store,
186 JsonPrefStore* protected_pref_store) {
187 scoped_refptr<TrackedPreferencesMigrator> prefs_migrator(
188 new TrackedPreferencesMigrator(unprotected_pref_names,
189 protected_pref_names,
190 unprotected_pref_store,
191 protected_pref_store));
192 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698