OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "components/sync_driver/sync_prefs.h" | |
6 | |
7 #include "base/base64.h" | |
8 #include "base/logging.h" | |
9 #include "base/strings/string_number_conversions.h" | |
10 #include "base/values.h" | |
11 #include "build/build_config.h" | |
12 #include "components/pref_registry/pref_registry_syncable.h" | |
13 #include "components/prefs/pref_member.h" | |
14 #include "components/prefs/pref_service.h" | |
15 #include "components/sync_driver/pref_names.h" | |
16 | |
17 namespace sync_driver { | |
18 | |
19 SyncPrefObserver::~SyncPrefObserver() {} | |
20 | |
21 SyncPrefs::SyncPrefs(PrefService* pref_service) : pref_service_(pref_service) { | |
22 DCHECK(pref_service); | |
23 RegisterPrefGroups(); | |
24 // Watch the preference that indicates sync is managed so we can take | |
25 // appropriate action. | |
26 pref_sync_managed_.Init( | |
27 prefs::kSyncManaged, | |
28 pref_service_, | |
29 base::Bind(&SyncPrefs::OnSyncManagedPrefChanged, base::Unretained(this))); | |
30 } | |
31 | |
32 SyncPrefs::SyncPrefs() : pref_service_(NULL) {} | |
33 | |
34 SyncPrefs::~SyncPrefs() { DCHECK(CalledOnValidThread()); } | |
35 | |
36 // static | |
37 void SyncPrefs::RegisterProfilePrefs( | |
38 user_prefs::PrefRegistrySyncable* registry) { | |
39 registry->RegisterBooleanPref(prefs::kSyncFirstSetupComplete, false); | |
40 registry->RegisterBooleanPref(prefs::kSyncSuppressStart, false); | |
41 registry->RegisterInt64Pref(prefs::kSyncLastSyncedTime, 0); | |
42 registry->RegisterInt64Pref(prefs::kSyncLastPollTime, 0); | |
43 registry->RegisterInt64Pref(prefs::kSyncFirstSyncTime, 0); | |
44 | |
45 // All datatypes are on by default, but this gets set explicitly | |
46 // when you configure sync (when turning it on), in | |
47 // ProfileSyncService::OnUserChoseDatatypes. | |
48 registry->RegisterBooleanPref(prefs::kSyncKeepEverythingSynced, true); | |
49 | |
50 syncer::ModelTypeSet user_types = syncer::UserTypes(); | |
51 | |
52 // Include proxy types as well, as they can be individually selected, | |
53 // although they don't have sync representations. | |
54 user_types.PutAll(syncer::ProxyTypes()); | |
55 | |
56 // Treat bookmarks and device info specially. | |
57 RegisterDataTypePreferredPref(registry, syncer::BOOKMARKS, true); | |
58 RegisterDataTypePreferredPref(registry, syncer::DEVICE_INFO, true); | |
59 user_types.Remove(syncer::BOOKMARKS); | |
60 user_types.Remove(syncer::DEVICE_INFO); | |
61 | |
62 // All types are set to off by default, which forces a configuration to | |
63 // explicitly enable them. GetPreferredTypes() will ensure that any new | |
64 // implicit types are enabled when their pref group is, or via | |
65 // KeepEverythingSynced. | |
66 for (syncer::ModelTypeSet::Iterator it = user_types.First(); it.Good(); | |
67 it.Inc()) { | |
68 RegisterDataTypePreferredPref(registry, it.Get(), false); | |
69 } | |
70 | |
71 registry->RegisterBooleanPref(prefs::kSyncManaged, false); | |
72 registry->RegisterStringPref(prefs::kSyncEncryptionBootstrapToken, | |
73 std::string()); | |
74 registry->RegisterStringPref(prefs::kSyncKeystoreEncryptionBootstrapToken, | |
75 std::string()); | |
76 #if defined(OS_CHROMEOS) | |
77 registry->RegisterStringPref(prefs::kSyncSpareBootstrapToken, ""); | |
78 #endif | |
79 | |
80 registry->RegisterBooleanPref(prefs::kSyncHasAuthError, false); | |
81 registry->RegisterStringPref(prefs::kSyncSessionsGUID, std::string()); | |
82 registry->RegisterBooleanPref(prefs::kSyncPassphrasePrompted, false); | |
83 registry->RegisterIntegerPref(prefs::kSyncMemoryPressureWarningCount, -1); | |
84 registry->RegisterBooleanPref(prefs::kSyncShutdownCleanly, false); | |
85 registry->RegisterDictionaryPref(prefs::kSyncInvalidationVersions); | |
86 registry->RegisterStringPref(prefs::kSyncLastRunVersion, std::string()); | |
87 registry->RegisterBooleanPref( | |
88 prefs::kSyncPassphraseEncryptionTransitionInProgress, false); | |
89 registry->RegisterStringPref(prefs::kSyncNigoriStateForPassphraseTransition, | |
90 std::string()); | |
91 } | |
92 | |
93 void SyncPrefs::AddSyncPrefObserver(SyncPrefObserver* sync_pref_observer) { | |
94 DCHECK(CalledOnValidThread()); | |
95 sync_pref_observers_.AddObserver(sync_pref_observer); | |
96 } | |
97 | |
98 void SyncPrefs::RemoveSyncPrefObserver(SyncPrefObserver* sync_pref_observer) { | |
99 DCHECK(CalledOnValidThread()); | |
100 sync_pref_observers_.RemoveObserver(sync_pref_observer); | |
101 } | |
102 | |
103 void SyncPrefs::ClearPreferences() { | |
104 DCHECK(CalledOnValidThread()); | |
105 pref_service_->ClearPref(prefs::kSyncLastSyncedTime); | |
106 pref_service_->ClearPref(prefs::kSyncLastPollTime); | |
107 pref_service_->ClearPref(prefs::kSyncFirstSetupComplete); | |
108 pref_service_->ClearPref(prefs::kSyncEncryptionBootstrapToken); | |
109 pref_service_->ClearPref(prefs::kSyncKeystoreEncryptionBootstrapToken); | |
110 pref_service_->ClearPref(prefs::kSyncPassphrasePrompted); | |
111 pref_service_->ClearPref(prefs::kSyncMemoryPressureWarningCount); | |
112 pref_service_->ClearPref(prefs::kSyncShutdownCleanly); | |
113 pref_service_->ClearPref(prefs::kSyncInvalidationVersions); | |
114 pref_service_->ClearPref(prefs::kSyncLastRunVersion); | |
115 pref_service_->ClearPref( | |
116 prefs::kSyncPassphraseEncryptionTransitionInProgress); | |
117 pref_service_->ClearPref(prefs::kSyncNigoriStateForPassphraseTransition); | |
118 | |
119 // TODO(nick): The current behavior does not clear | |
120 // e.g. prefs::kSyncBookmarks. Is that really what we want? | |
121 } | |
122 | |
123 bool SyncPrefs::IsFirstSetupComplete() const { | |
124 DCHECK(CalledOnValidThread()); | |
125 return pref_service_->GetBoolean(prefs::kSyncFirstSetupComplete); | |
126 } | |
127 | |
128 void SyncPrefs::SetFirstSetupComplete() { | |
129 DCHECK(CalledOnValidThread()); | |
130 pref_service_->SetBoolean(prefs::kSyncFirstSetupComplete, true); | |
131 } | |
132 | |
133 bool SyncPrefs::SyncHasAuthError() const { | |
134 DCHECK(CalledOnValidThread()); | |
135 return pref_service_->GetBoolean(prefs::kSyncHasAuthError); | |
136 } | |
137 | |
138 void SyncPrefs::SetSyncAuthError(bool error) { | |
139 DCHECK(CalledOnValidThread()); | |
140 pref_service_->SetBoolean(prefs::kSyncHasAuthError, error); | |
141 } | |
142 | |
143 bool SyncPrefs::IsSyncRequested() const { | |
144 DCHECK(CalledOnValidThread()); | |
145 // IsSyncRequested is the inverse of the old SuppressStart pref. | |
146 // Since renaming a pref value is hard, here we still use the old one. | |
147 return !pref_service_->GetBoolean(prefs::kSyncSuppressStart); | |
148 } | |
149 | |
150 void SyncPrefs::SetSyncRequested(bool is_requested) { | |
151 DCHECK(CalledOnValidThread()); | |
152 // See IsSyncRequested for why we use this pref and !is_requested. | |
153 pref_service_->SetBoolean(prefs::kSyncSuppressStart, !is_requested); | |
154 } | |
155 | |
156 base::Time SyncPrefs::GetLastSyncedTime() const { | |
157 DCHECK(CalledOnValidThread()); | |
158 return base::Time::FromInternalValue( | |
159 pref_service_->GetInt64(prefs::kSyncLastSyncedTime)); | |
160 } | |
161 | |
162 void SyncPrefs::SetLastSyncedTime(base::Time time) { | |
163 DCHECK(CalledOnValidThread()); | |
164 pref_service_->SetInt64(prefs::kSyncLastSyncedTime, time.ToInternalValue()); | |
165 } | |
166 | |
167 base::Time SyncPrefs::GetLastPollTime() const { | |
168 DCHECK(CalledOnValidThread()); | |
169 return base::Time::FromInternalValue( | |
170 pref_service_->GetInt64(prefs::kSyncLastSyncedTime)); | |
171 } | |
172 | |
173 void SyncPrefs::SetLastPollTime(base::Time time) { | |
174 DCHECK(CalledOnValidThread()); | |
175 pref_service_->SetInt64(prefs::kSyncLastPollTime, time.ToInternalValue()); | |
176 } | |
177 | |
178 bool SyncPrefs::HasKeepEverythingSynced() const { | |
179 DCHECK(CalledOnValidThread()); | |
180 return pref_service_->GetBoolean(prefs::kSyncKeepEverythingSynced); | |
181 } | |
182 | |
183 void SyncPrefs::SetKeepEverythingSynced(bool keep_everything_synced) { | |
184 DCHECK(CalledOnValidThread()); | |
185 pref_service_->SetBoolean(prefs::kSyncKeepEverythingSynced, | |
186 keep_everything_synced); | |
187 } | |
188 | |
189 syncer::ModelTypeSet SyncPrefs::GetPreferredDataTypes( | |
190 syncer::ModelTypeSet registered_types) const { | |
191 DCHECK(CalledOnValidThread()); | |
192 | |
193 if (pref_service_->GetBoolean(prefs::kSyncKeepEverythingSynced)) { | |
194 return registered_types; | |
195 } | |
196 | |
197 syncer::ModelTypeSet preferred_types; | |
198 for (syncer::ModelTypeSet::Iterator it = registered_types.First(); it.Good(); | |
199 it.Inc()) { | |
200 if (GetDataTypePreferred(it.Get())) { | |
201 preferred_types.Put(it.Get()); | |
202 } | |
203 } | |
204 return ResolvePrefGroups(registered_types, preferred_types); | |
205 } | |
206 | |
207 void SyncPrefs::SetPreferredDataTypes(syncer::ModelTypeSet registered_types, | |
208 syncer::ModelTypeSet preferred_types) { | |
209 DCHECK(CalledOnValidThread()); | |
210 preferred_types = ResolvePrefGroups(registered_types, preferred_types); | |
211 DCHECK(registered_types.HasAll(preferred_types)); | |
212 for (syncer::ModelTypeSet::Iterator i = registered_types.First(); i.Good(); | |
213 i.Inc()) { | |
214 SetDataTypePreferred(i.Get(), preferred_types.Has(i.Get())); | |
215 } | |
216 } | |
217 | |
218 bool SyncPrefs::IsManaged() const { | |
219 DCHECK(CalledOnValidThread()); | |
220 return pref_service_->GetBoolean(prefs::kSyncManaged); | |
221 } | |
222 | |
223 std::string SyncPrefs::GetEncryptionBootstrapToken() const { | |
224 DCHECK(CalledOnValidThread()); | |
225 return pref_service_->GetString(prefs::kSyncEncryptionBootstrapToken); | |
226 } | |
227 | |
228 void SyncPrefs::SetEncryptionBootstrapToken(const std::string& token) { | |
229 DCHECK(CalledOnValidThread()); | |
230 pref_service_->SetString(prefs::kSyncEncryptionBootstrapToken, token); | |
231 } | |
232 | |
233 std::string SyncPrefs::GetKeystoreEncryptionBootstrapToken() const { | |
234 DCHECK(CalledOnValidThread()); | |
235 return pref_service_->GetString(prefs::kSyncKeystoreEncryptionBootstrapToken); | |
236 } | |
237 | |
238 void SyncPrefs::SetKeystoreEncryptionBootstrapToken(const std::string& token) { | |
239 DCHECK(CalledOnValidThread()); | |
240 pref_service_->SetString(prefs::kSyncKeystoreEncryptionBootstrapToken, token); | |
241 } | |
242 | |
243 std::string SyncPrefs::GetSyncSessionsGUID() const { | |
244 DCHECK(CalledOnValidThread()); | |
245 return pref_service_->GetString(prefs::kSyncSessionsGUID); | |
246 } | |
247 | |
248 void SyncPrefs::SetSyncSessionsGUID(const std::string& guid) { | |
249 DCHECK(CalledOnValidThread()); | |
250 pref_service_->SetString(prefs::kSyncSessionsGUID, guid); | |
251 } | |
252 | |
253 // static | |
254 const char* SyncPrefs::GetPrefNameForDataType(syncer::ModelType data_type) { | |
255 switch (data_type) { | |
256 case syncer::BOOKMARKS: | |
257 return prefs::kSyncBookmarks; | |
258 case syncer::PASSWORDS: | |
259 return prefs::kSyncPasswords; | |
260 case syncer::PREFERENCES: | |
261 return prefs::kSyncPreferences; | |
262 case syncer::AUTOFILL: | |
263 return prefs::kSyncAutofill; | |
264 case syncer::AUTOFILL_PROFILE: | |
265 return prefs::kSyncAutofillProfile; | |
266 case syncer::AUTOFILL_WALLET_DATA: | |
267 return prefs::kSyncAutofillWallet; | |
268 case syncer::AUTOFILL_WALLET_METADATA: | |
269 return prefs::kSyncAutofillWalletMetadata; | |
270 case syncer::THEMES: | |
271 return prefs::kSyncThemes; | |
272 case syncer::TYPED_URLS: | |
273 return prefs::kSyncTypedUrls; | |
274 case syncer::EXTENSION_SETTINGS: | |
275 return prefs::kSyncExtensionSettings; | |
276 case syncer::EXTENSIONS: | |
277 return prefs::kSyncExtensions; | |
278 case syncer::APP_LIST: | |
279 return prefs::kSyncAppList; | |
280 case syncer::APP_SETTINGS: | |
281 return prefs::kSyncAppSettings; | |
282 case syncer::APPS: | |
283 return prefs::kSyncApps; | |
284 case syncer::SEARCH_ENGINES: | |
285 return prefs::kSyncSearchEngines; | |
286 case syncer::SESSIONS: | |
287 return prefs::kSyncSessions; | |
288 case syncer::APP_NOTIFICATIONS: | |
289 return prefs::kSyncAppNotifications; | |
290 case syncer::HISTORY_DELETE_DIRECTIVES: | |
291 return prefs::kSyncHistoryDeleteDirectives; | |
292 case syncer::SYNCED_NOTIFICATIONS: | |
293 return prefs::kSyncSyncedNotifications; | |
294 case syncer::SYNCED_NOTIFICATION_APP_INFO: | |
295 return prefs::kSyncSyncedNotificationAppInfo; | |
296 case syncer::DICTIONARY: | |
297 return prefs::kSyncDictionary; | |
298 case syncer::FAVICON_IMAGES: | |
299 return prefs::kSyncFaviconImages; | |
300 case syncer::FAVICON_TRACKING: | |
301 return prefs::kSyncFaviconTracking; | |
302 case syncer::SUPERVISED_USER_SETTINGS: | |
303 return prefs::kSyncSupervisedUserSettings; | |
304 case syncer::PROXY_TABS: | |
305 return prefs::kSyncTabs; | |
306 case syncer::PRIORITY_PREFERENCES: | |
307 return prefs::kSyncPriorityPreferences; | |
308 case syncer::SUPERVISED_USERS: | |
309 return prefs::kSyncSupervisedUsers; | |
310 case syncer::ARTICLES: | |
311 return prefs::kSyncArticles; | |
312 case syncer::SUPERVISED_USER_SHARED_SETTINGS: | |
313 return prefs::kSyncSupervisedUserSharedSettings; | |
314 case syncer::SUPERVISED_USER_WHITELISTS: | |
315 return prefs::kSyncSupervisedUserWhitelists; | |
316 case syncer::DEVICE_INFO: | |
317 return prefs::kSyncDeviceInfo; | |
318 case syncer::WIFI_CREDENTIALS: | |
319 return prefs::kSyncWifiCredentials; | |
320 case syncer::ARC_PACKAGE: | |
321 return prefs::kSyncArcPackage; | |
322 default: | |
323 break; | |
324 } | |
325 NOTREACHED() << "Type is " << data_type; | |
326 return NULL; | |
327 } | |
328 | |
329 #if defined(OS_CHROMEOS) | |
330 std::string SyncPrefs::GetSpareBootstrapToken() const { | |
331 DCHECK(CalledOnValidThread()); | |
332 return pref_service_->GetString(prefs::kSyncSpareBootstrapToken); | |
333 } | |
334 | |
335 void SyncPrefs::SetSpareBootstrapToken(const std::string& token) { | |
336 DCHECK(CalledOnValidThread()); | |
337 pref_service_->SetString(prefs::kSyncSpareBootstrapToken, token); | |
338 } | |
339 #endif | |
340 | |
341 void SyncPrefs::OnSyncManagedPrefChanged() { | |
342 DCHECK(CalledOnValidThread()); | |
343 FOR_EACH_OBSERVER(SyncPrefObserver, | |
344 sync_pref_observers_, | |
345 OnSyncManagedPrefChange(*pref_sync_managed_)); | |
346 } | |
347 | |
348 void SyncPrefs::SetManagedForTest(bool is_managed) { | |
349 DCHECK(CalledOnValidThread()); | |
350 pref_service_->SetBoolean(prefs::kSyncManaged, is_managed); | |
351 } | |
352 | |
353 void SyncPrefs::RegisterPrefGroups() { | |
354 pref_groups_[syncer::APPS].Put(syncer::APP_NOTIFICATIONS); | |
355 pref_groups_[syncer::APPS].Put(syncer::APP_SETTINGS); | |
356 pref_groups_[syncer::APPS].Put(syncer::APP_LIST); | |
357 | |
358 pref_groups_[syncer::AUTOFILL].Put(syncer::AUTOFILL_PROFILE); | |
359 pref_groups_[syncer::AUTOFILL].Put(syncer::AUTOFILL_WALLET_DATA); | |
360 pref_groups_[syncer::AUTOFILL].Put(syncer::AUTOFILL_WALLET_METADATA); | |
361 | |
362 pref_groups_[syncer::EXTENSIONS].Put(syncer::EXTENSION_SETTINGS); | |
363 | |
364 pref_groups_[syncer::PREFERENCES].Put(syncer::DICTIONARY); | |
365 pref_groups_[syncer::PREFERENCES].Put(syncer::PRIORITY_PREFERENCES); | |
366 pref_groups_[syncer::PREFERENCES].Put(syncer::SEARCH_ENGINES); | |
367 | |
368 pref_groups_[syncer::TYPED_URLS].Put(syncer::HISTORY_DELETE_DIRECTIVES); | |
369 pref_groups_[syncer::TYPED_URLS].Put(syncer::SESSIONS); | |
370 pref_groups_[syncer::TYPED_URLS].Put(syncer::FAVICON_IMAGES); | |
371 pref_groups_[syncer::TYPED_URLS].Put(syncer::FAVICON_TRACKING); | |
372 | |
373 pref_groups_[syncer::PROXY_TABS].Put(syncer::SESSIONS); | |
374 pref_groups_[syncer::PROXY_TABS].Put(syncer::FAVICON_IMAGES); | |
375 pref_groups_[syncer::PROXY_TABS].Put(syncer::FAVICON_TRACKING); | |
376 | |
377 // TODO(zea): put favicons in the bookmarks group as well once it handles | |
378 // those favicons. | |
379 } | |
380 | |
381 // static | |
382 void SyncPrefs::RegisterDataTypePreferredPref( | |
383 user_prefs::PrefRegistrySyncable* registry, | |
384 syncer::ModelType type, | |
385 bool is_preferred) { | |
386 const char* pref_name = GetPrefNameForDataType(type); | |
387 if (!pref_name) { | |
388 NOTREACHED(); | |
389 return; | |
390 } | |
391 registry->RegisterBooleanPref(pref_name, is_preferred); | |
392 } | |
393 | |
394 bool SyncPrefs::GetDataTypePreferred(syncer::ModelType type) const { | |
395 DCHECK(CalledOnValidThread()); | |
396 const char* pref_name = GetPrefNameForDataType(type); | |
397 if (!pref_name) { | |
398 NOTREACHED(); | |
399 return false; | |
400 } | |
401 | |
402 // Device info is always enabled. | |
403 if (pref_name == prefs::kSyncDeviceInfo) | |
404 return true; | |
405 | |
406 if (type == syncer::PROXY_TABS && | |
407 pref_service_->GetUserPrefValue(pref_name) == NULL && | |
408 pref_service_->IsUserModifiablePreference(pref_name)) { | |
409 // If there is no tab sync preference yet (i.e. newly enabled type), | |
410 // default to the session sync preference value. | |
411 pref_name = GetPrefNameForDataType(syncer::SESSIONS); | |
412 } | |
413 | |
414 return pref_service_->GetBoolean(pref_name); | |
415 } | |
416 | |
417 void SyncPrefs::SetDataTypePreferred(syncer::ModelType type, | |
418 bool is_preferred) { | |
419 DCHECK(CalledOnValidThread()); | |
420 const char* pref_name = GetPrefNameForDataType(type); | |
421 if (!pref_name) { | |
422 NOTREACHED(); | |
423 return; | |
424 } | |
425 | |
426 // Device info is always preferred. | |
427 if (type == syncer::DEVICE_INFO) | |
428 return; | |
429 | |
430 pref_service_->SetBoolean(pref_name, is_preferred); | |
431 } | |
432 | |
433 syncer::ModelTypeSet SyncPrefs::ResolvePrefGroups( | |
434 syncer::ModelTypeSet registered_types, | |
435 syncer::ModelTypeSet types) const { | |
436 syncer::ModelTypeSet types_with_groups = types; | |
437 for (PrefGroupsMap::const_iterator i = pref_groups_.begin(); | |
438 i != pref_groups_.end(); | |
439 ++i) { | |
440 if (types.Has(i->first)) | |
441 types_with_groups.PutAll(i->second); | |
442 } | |
443 types_with_groups.RetainAll(registered_types); | |
444 return types_with_groups; | |
445 } | |
446 | |
447 base::Time SyncPrefs::GetFirstSyncTime() const { | |
448 return base::Time::FromInternalValue( | |
449 pref_service_->GetInt64(prefs::kSyncFirstSyncTime)); | |
450 } | |
451 | |
452 void SyncPrefs::SetFirstSyncTime(base::Time time) { | |
453 pref_service_->SetInt64(prefs::kSyncFirstSyncTime, time.ToInternalValue()); | |
454 } | |
455 | |
456 void SyncPrefs::ClearFirstSyncTime() { | |
457 pref_service_->ClearPref(prefs::kSyncFirstSyncTime); | |
458 } | |
459 | |
460 bool SyncPrefs::IsPassphrasePrompted() const { | |
461 return pref_service_->GetBoolean(prefs::kSyncPassphrasePrompted); | |
462 } | |
463 | |
464 void SyncPrefs::SetPassphrasePrompted(bool value) { | |
465 pref_service_->SetBoolean(prefs::kSyncPassphrasePrompted, value); | |
466 } | |
467 | |
468 int SyncPrefs::GetMemoryPressureWarningCount() const { | |
469 return pref_service_->GetInteger(prefs::kSyncMemoryPressureWarningCount); | |
470 } | |
471 | |
472 void SyncPrefs::SetMemoryPressureWarningCount(int value) { | |
473 pref_service_->SetInteger(prefs::kSyncMemoryPressureWarningCount, value); | |
474 } | |
475 | |
476 bool SyncPrefs::DidSyncShutdownCleanly() const { | |
477 return pref_service_->GetBoolean(prefs::kSyncShutdownCleanly); | |
478 } | |
479 | |
480 void SyncPrefs::SetCleanShutdown(bool value) { | |
481 pref_service_->SetBoolean(prefs::kSyncShutdownCleanly, value); | |
482 } | |
483 | |
484 void SyncPrefs::GetInvalidationVersions( | |
485 std::map<syncer::ModelType, int64_t>* invalidation_versions) const { | |
486 const base::DictionaryValue* invalidation_dictionary = | |
487 pref_service_->GetDictionary(prefs::kSyncInvalidationVersions); | |
488 syncer::ModelTypeSet protocol_types = syncer::ProtocolTypes(); | |
489 for (auto iter = protocol_types.First(); iter.Good(); iter.Inc()) { | |
490 std::string key = syncer::ModelTypeToString(iter.Get()); | |
491 std::string version_str; | |
492 if (!invalidation_dictionary->GetString(key, &version_str)) | |
493 continue; | |
494 int64_t version = 0; | |
495 if (!base::StringToInt64(version_str, &version)) | |
496 continue; | |
497 (*invalidation_versions)[iter.Get()] = version; | |
498 } | |
499 } | |
500 | |
501 void SyncPrefs::UpdateInvalidationVersions( | |
502 const std::map<syncer::ModelType, int64_t>& invalidation_versions) { | |
503 std::unique_ptr<base::DictionaryValue> invalidation_dictionary( | |
504 new base::DictionaryValue()); | |
505 for (const auto& map_iter : invalidation_versions) { | |
506 std::string version_str = base::Int64ToString(map_iter.second); | |
507 invalidation_dictionary->SetString( | |
508 syncer::ModelTypeToString(map_iter.first), version_str); | |
509 } | |
510 pref_service_->Set(prefs::kSyncInvalidationVersions, | |
511 *invalidation_dictionary); | |
512 } | |
513 | |
514 std::string SyncPrefs::GetLastRunVersion() const { | |
515 return pref_service_->GetString(prefs::kSyncLastRunVersion); | |
516 } | |
517 | |
518 void SyncPrefs::SetLastRunVersion(const std::string& current_version) { | |
519 pref_service_->SetString(prefs::kSyncLastRunVersion, current_version); | |
520 } | |
521 | |
522 void SyncPrefs::SetPassphraseEncryptionTransitionInProgress(bool value) { | |
523 pref_service_->SetBoolean( | |
524 prefs::kSyncPassphraseEncryptionTransitionInProgress, value); | |
525 } | |
526 | |
527 bool SyncPrefs::GetPassphraseEncryptionTransitionInProgress() const { | |
528 return pref_service_->GetBoolean( | |
529 prefs::kSyncPassphraseEncryptionTransitionInProgress); | |
530 } | |
531 | |
532 void SyncPrefs::SetSavedNigoriStateForPassphraseEncryptionTransition( | |
533 const syncer::SyncEncryptionHandler::NigoriState& nigori_state) { | |
534 std::string encoded; | |
535 base::Base64Encode(nigori_state.nigori_specifics.SerializeAsString(), | |
536 &encoded); | |
537 pref_service_->SetString(prefs::kSyncNigoriStateForPassphraseTransition, | |
538 encoded); | |
539 } | |
540 | |
541 std::unique_ptr<syncer::SyncEncryptionHandler::NigoriState> | |
542 SyncPrefs::GetSavedNigoriStateForPassphraseEncryptionTransition() const { | |
543 const std::string encoded = | |
544 pref_service_->GetString(prefs::kSyncNigoriStateForPassphraseTransition); | |
545 std::string decoded; | |
546 if (!base::Base64Decode(encoded, &decoded)) | |
547 return std::unique_ptr<syncer::SyncEncryptionHandler::NigoriState>(); | |
548 | |
549 std::unique_ptr<syncer::SyncEncryptionHandler::NigoriState> result( | |
550 new syncer::SyncEncryptionHandler::NigoriState()); | |
551 if (!result->nigori_specifics.ParseFromString(decoded)) | |
552 return std::unique_ptr<syncer::SyncEncryptionHandler::NigoriState>(); | |
553 return result; | |
554 } | |
555 | |
556 } // namespace sync_driver | |
OLD | NEW |