[Eraser strings] Remove unused Supervised User infobar and corresponding strings
[chromium-blink-merge.git] / components / sync_driver / sync_prefs.cc
blob3fe3391d6d00bb020e85db3199c20e42302ba53a
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.
5 #include "components/sync_driver/sync_prefs.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_member.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/values.h"
12 #include "build/build_config.h"
13 #include "components/pref_registry/pref_registry_syncable.h"
14 #include "components/sync_driver/pref_names.h"
16 namespace sync_driver {
18 SyncPrefObserver::~SyncPrefObserver() {}
20 SyncPrefs::SyncPrefs(PrefService* pref_service) : pref_service_(pref_service) {
21 DCHECK(pref_service);
22 RegisterPrefGroups();
23 // Watch the preference that indicates sync is managed so we can take
24 // appropriate action.
25 pref_sync_managed_.Init(
26 prefs::kSyncManaged,
27 pref_service_,
28 base::Bind(&SyncPrefs::OnSyncManagedPrefChanged, base::Unretained(this)));
31 SyncPrefs::SyncPrefs() : pref_service_(NULL) {}
33 SyncPrefs::~SyncPrefs() { DCHECK(CalledOnValidThread()); }
35 // static
36 void SyncPrefs::RegisterProfilePrefs(
37 user_prefs::PrefRegistrySyncable* registry) {
38 registry->RegisterBooleanPref(prefs::kSyncHasSetupCompleted, false);
39 registry->RegisterBooleanPref(prefs::kSyncSuppressStart, false);
40 registry->RegisterInt64Pref(prefs::kSyncLastSyncedTime, 0);
41 registry->RegisterInt64Pref(prefs::kSyncLastPollTime, 0);
42 registry->RegisterInt64Pref(prefs::kSyncFirstSyncTime, 0);
44 // All datatypes are on by default, but this gets set explicitly
45 // when you configure sync (when turning it on), in
46 // ProfileSyncService::OnUserChoseDatatypes.
47 registry->RegisterBooleanPref(prefs::kSyncKeepEverythingSynced, true);
49 syncer::ModelTypeSet user_types = syncer::UserTypes();
51 // Include proxy types as well, as they can be individually selected,
52 // although they don't have sync representations.
53 user_types.PutAll(syncer::ProxyTypes());
55 // Treat bookmarks and device info specially.
56 RegisterDataTypePreferredPref(registry, syncer::BOOKMARKS, true);
57 RegisterDataTypePreferredPref(registry, syncer::DEVICE_INFO, true);
58 user_types.Remove(syncer::BOOKMARKS);
59 user_types.Remove(syncer::DEVICE_INFO);
61 // All types are set to off by default, which forces a configuration to
62 // explicitly enable them. GetPreferredTypes() will ensure that any new
63 // implicit types are enabled when their pref group is, or via
64 // KeepEverythingSynced.
65 for (syncer::ModelTypeSet::Iterator it = user_types.First(); it.Good();
66 it.Inc()) {
67 RegisterDataTypePreferredPref(registry, it.Get(), false);
70 registry->RegisterBooleanPref(prefs::kSyncManaged, false);
71 registry->RegisterStringPref(prefs::kSyncEncryptionBootstrapToken,
72 std::string());
73 registry->RegisterStringPref(prefs::kSyncKeystoreEncryptionBootstrapToken,
74 std::string());
75 #if defined(OS_CHROMEOS)
76 registry->RegisterStringPref(prefs::kSyncSpareBootstrapToken, "");
77 #endif
79 registry->RegisterBooleanPref(prefs::kSyncHasAuthError, false);
80 registry->RegisterStringPref(prefs::kSyncSessionsGUID, std::string());
81 registry->RegisterIntegerPref(prefs::kSyncRemainingRollbackTries, 0);
82 registry->RegisterBooleanPref(prefs::kSyncPassphrasePrompted, false);
83 registry->RegisterIntegerPref(prefs::kSyncMemoryPressureWarningCount, -1);
84 registry->RegisterBooleanPref(prefs::kSyncShutdownCleanly, false);
85 registry->RegisterDictionaryPref(prefs::kSyncInvalidationVersions);
88 void SyncPrefs::AddSyncPrefObserver(SyncPrefObserver* sync_pref_observer) {
89 DCHECK(CalledOnValidThread());
90 sync_pref_observers_.AddObserver(sync_pref_observer);
93 void SyncPrefs::RemoveSyncPrefObserver(SyncPrefObserver* sync_pref_observer) {
94 DCHECK(CalledOnValidThread());
95 sync_pref_observers_.RemoveObserver(sync_pref_observer);
98 void SyncPrefs::ClearPreferences() {
99 DCHECK(CalledOnValidThread());
100 pref_service_->ClearPref(prefs::kSyncLastSyncedTime);
101 pref_service_->ClearPref(prefs::kSyncLastPollTime);
102 pref_service_->ClearPref(prefs::kSyncHasSetupCompleted);
103 pref_service_->ClearPref(prefs::kSyncEncryptionBootstrapToken);
104 pref_service_->ClearPref(prefs::kSyncKeystoreEncryptionBootstrapToken);
105 pref_service_->ClearPref(prefs::kSyncPassphrasePrompted);
106 pref_service_->ClearPref(prefs::kSyncMemoryPressureWarningCount);
107 pref_service_->ClearPref(prefs::kSyncShutdownCleanly);
108 pref_service_->ClearPref(prefs::kSyncInvalidationVersions);
110 // TODO(nick): The current behavior does not clear
111 // e.g. prefs::kSyncBookmarks. Is that really what we want?
114 bool SyncPrefs::HasSyncSetupCompleted() const {
115 DCHECK(CalledOnValidThread());
116 return pref_service_->GetBoolean(prefs::kSyncHasSetupCompleted);
119 void SyncPrefs::SetSyncSetupCompleted() {
120 DCHECK(CalledOnValidThread());
121 pref_service_->SetBoolean(prefs::kSyncHasSetupCompleted, true);
122 SetSyncRequested(true);
125 bool SyncPrefs::SyncHasAuthError() const {
126 DCHECK(CalledOnValidThread());
127 return pref_service_->GetBoolean(prefs::kSyncHasAuthError);
130 void SyncPrefs::SetSyncAuthError(bool error) {
131 DCHECK(CalledOnValidThread());
132 pref_service_->SetBoolean(prefs::kSyncHasAuthError, error);
135 bool SyncPrefs::IsSyncRequested() const {
136 DCHECK(CalledOnValidThread());
137 // IsSyncRequested is the inverse of the old SuppressStart pref.
138 // Since renaming a pref value is hard, here we still use the old one.
139 return !pref_service_->GetBoolean(prefs::kSyncSuppressStart);
142 void SyncPrefs::SetSyncRequested(bool is_requested) {
143 DCHECK(CalledOnValidThread());
144 // See IsSyncRequested for why we use this pref and !is_requested.
145 pref_service_->SetBoolean(prefs::kSyncSuppressStart, !is_requested);
148 base::Time SyncPrefs::GetLastSyncedTime() const {
149 DCHECK(CalledOnValidThread());
150 return base::Time::FromInternalValue(
151 pref_service_->GetInt64(prefs::kSyncLastSyncedTime));
154 void SyncPrefs::SetLastSyncedTime(base::Time time) {
155 DCHECK(CalledOnValidThread());
156 pref_service_->SetInt64(prefs::kSyncLastSyncedTime, time.ToInternalValue());
159 base::Time SyncPrefs::GetLastPollTime() const {
160 DCHECK(CalledOnValidThread());
161 return base::Time::FromInternalValue(
162 pref_service_->GetInt64(prefs::kSyncLastSyncedTime));
165 void SyncPrefs::SetLastPollTime(base::Time time) {
166 DCHECK(CalledOnValidThread());
167 pref_service_->SetInt64(prefs::kSyncLastPollTime, time.ToInternalValue());
170 bool SyncPrefs::HasKeepEverythingSynced() const {
171 DCHECK(CalledOnValidThread());
172 return pref_service_->GetBoolean(prefs::kSyncKeepEverythingSynced);
175 void SyncPrefs::SetKeepEverythingSynced(bool keep_everything_synced) {
176 DCHECK(CalledOnValidThread());
177 pref_service_->SetBoolean(prefs::kSyncKeepEverythingSynced,
178 keep_everything_synced);
181 syncer::ModelTypeSet SyncPrefs::GetPreferredDataTypes(
182 syncer::ModelTypeSet registered_types) const {
183 DCHECK(CalledOnValidThread());
185 if (pref_service_->GetBoolean(prefs::kSyncKeepEverythingSynced)) {
186 return registered_types;
189 syncer::ModelTypeSet preferred_types;
190 for (syncer::ModelTypeSet::Iterator it = registered_types.First(); it.Good();
191 it.Inc()) {
192 if (GetDataTypePreferred(it.Get())) {
193 preferred_types.Put(it.Get());
196 return ResolvePrefGroups(registered_types, preferred_types);
199 void SyncPrefs::SetPreferredDataTypes(syncer::ModelTypeSet registered_types,
200 syncer::ModelTypeSet preferred_types) {
201 DCHECK(CalledOnValidThread());
202 preferred_types = ResolvePrefGroups(registered_types, preferred_types);
203 DCHECK(registered_types.HasAll(preferred_types));
204 for (syncer::ModelTypeSet::Iterator i = registered_types.First(); i.Good();
205 i.Inc()) {
206 SetDataTypePreferred(i.Get(), preferred_types.Has(i.Get()));
210 bool SyncPrefs::IsManaged() const {
211 DCHECK(CalledOnValidThread());
212 return pref_service_->GetBoolean(prefs::kSyncManaged);
215 std::string SyncPrefs::GetEncryptionBootstrapToken() const {
216 DCHECK(CalledOnValidThread());
217 return pref_service_->GetString(prefs::kSyncEncryptionBootstrapToken);
220 void SyncPrefs::SetEncryptionBootstrapToken(const std::string& token) {
221 DCHECK(CalledOnValidThread());
222 pref_service_->SetString(prefs::kSyncEncryptionBootstrapToken, token);
225 std::string SyncPrefs::GetKeystoreEncryptionBootstrapToken() const {
226 DCHECK(CalledOnValidThread());
227 return pref_service_->GetString(prefs::kSyncKeystoreEncryptionBootstrapToken);
230 void SyncPrefs::SetKeystoreEncryptionBootstrapToken(const std::string& token) {
231 DCHECK(CalledOnValidThread());
232 pref_service_->SetString(prefs::kSyncKeystoreEncryptionBootstrapToken, token);
235 std::string SyncPrefs::GetSyncSessionsGUID() const {
236 DCHECK(CalledOnValidThread());
237 return pref_service_->GetString(prefs::kSyncSessionsGUID);
240 void SyncPrefs::SetSyncSessionsGUID(const std::string& guid) {
241 DCHECK(CalledOnValidThread());
242 pref_service_->SetString(prefs::kSyncSessionsGUID, guid);
245 // static
246 const char* SyncPrefs::GetPrefNameForDataType(syncer::ModelType data_type) {
247 switch (data_type) {
248 case syncer::BOOKMARKS:
249 return prefs::kSyncBookmarks;
250 case syncer::PASSWORDS:
251 return prefs::kSyncPasswords;
252 case syncer::PREFERENCES:
253 return prefs::kSyncPreferences;
254 case syncer::AUTOFILL:
255 return prefs::kSyncAutofill;
256 case syncer::AUTOFILL_PROFILE:
257 return prefs::kSyncAutofillProfile;
258 case syncer::AUTOFILL_WALLET_DATA:
259 return prefs::kSyncAutofillWallet;
260 case syncer::AUTOFILL_WALLET_METADATA:
261 return prefs::kSyncAutofillWalletMetadata;
262 case syncer::THEMES:
263 return prefs::kSyncThemes;
264 case syncer::TYPED_URLS:
265 return prefs::kSyncTypedUrls;
266 case syncer::EXTENSION_SETTINGS:
267 return prefs::kSyncExtensionSettings;
268 case syncer::EXTENSIONS:
269 return prefs::kSyncExtensions;
270 case syncer::APP_LIST:
271 return prefs::kSyncAppList;
272 case syncer::APP_SETTINGS:
273 return prefs::kSyncAppSettings;
274 case syncer::APPS:
275 return prefs::kSyncApps;
276 case syncer::SEARCH_ENGINES:
277 return prefs::kSyncSearchEngines;
278 case syncer::SESSIONS:
279 return prefs::kSyncSessions;
280 case syncer::APP_NOTIFICATIONS:
281 return prefs::kSyncAppNotifications;
282 case syncer::HISTORY_DELETE_DIRECTIVES:
283 return prefs::kSyncHistoryDeleteDirectives;
284 case syncer::SYNCED_NOTIFICATIONS:
285 return prefs::kSyncSyncedNotifications;
286 case syncer::SYNCED_NOTIFICATION_APP_INFO:
287 return prefs::kSyncSyncedNotificationAppInfo;
288 case syncer::DICTIONARY:
289 return prefs::kSyncDictionary;
290 case syncer::FAVICON_IMAGES:
291 return prefs::kSyncFaviconImages;
292 case syncer::FAVICON_TRACKING:
293 return prefs::kSyncFaviconTracking;
294 case syncer::SUPERVISED_USER_SETTINGS:
295 return prefs::kSyncSupervisedUserSettings;
296 case syncer::PROXY_TABS:
297 return prefs::kSyncTabs;
298 case syncer::PRIORITY_PREFERENCES:
299 return prefs::kSyncPriorityPreferences;
300 case syncer::SUPERVISED_USERS:
301 return prefs::kSyncSupervisedUsers;
302 case syncer::ARTICLES:
303 return prefs::kSyncArticles;
304 case syncer::SUPERVISED_USER_SHARED_SETTINGS:
305 return prefs::kSyncSupervisedUserSharedSettings;
306 case syncer::SUPERVISED_USER_WHITELISTS:
307 return prefs::kSyncSupervisedUserWhitelists;
308 case syncer::DEVICE_INFO:
309 return prefs::kSyncDeviceInfo;
310 case syncer::WIFI_CREDENTIALS:
311 return prefs::kSyncWifiCredentials;
312 default:
313 break;
315 NOTREACHED() << "Type is " << data_type;
316 return NULL;
319 #if defined(OS_CHROMEOS)
320 std::string SyncPrefs::GetSpareBootstrapToken() const {
321 DCHECK(CalledOnValidThread());
322 return pref_service_->GetString(prefs::kSyncSpareBootstrapToken);
325 void SyncPrefs::SetSpareBootstrapToken(const std::string& token) {
326 DCHECK(CalledOnValidThread());
327 pref_service_->SetString(prefs::kSyncSpareBootstrapToken, token);
329 #endif
331 int SyncPrefs::GetRemainingRollbackTries() const {
332 return pref_service_->GetInteger(prefs::kSyncRemainingRollbackTries);
335 void SyncPrefs::SetRemainingRollbackTries(int times) {
336 pref_service_->SetInteger(prefs::kSyncRemainingRollbackTries, times);
339 void SyncPrefs::OnSyncManagedPrefChanged() {
340 DCHECK(CalledOnValidThread());
341 FOR_EACH_OBSERVER(SyncPrefObserver,
342 sync_pref_observers_,
343 OnSyncManagedPrefChange(*pref_sync_managed_));
346 void SyncPrefs::SetManagedForTest(bool is_managed) {
347 DCHECK(CalledOnValidThread());
348 pref_service_->SetBoolean(prefs::kSyncManaged, is_managed);
351 void SyncPrefs::RegisterPrefGroups() {
352 pref_groups_[syncer::APPS].Put(syncer::APP_NOTIFICATIONS);
353 pref_groups_[syncer::APPS].Put(syncer::APP_SETTINGS);
354 pref_groups_[syncer::APPS].Put(syncer::APP_LIST);
356 pref_groups_[syncer::AUTOFILL].Put(syncer::AUTOFILL_PROFILE);
357 pref_groups_[syncer::AUTOFILL].Put(syncer::AUTOFILL_WALLET_DATA);
358 pref_groups_[syncer::AUTOFILL].Put(syncer::AUTOFILL_WALLET_METADATA);
360 pref_groups_[syncer::EXTENSIONS].Put(syncer::EXTENSION_SETTINGS);
362 pref_groups_[syncer::PREFERENCES].Put(syncer::DICTIONARY);
363 pref_groups_[syncer::PREFERENCES].Put(syncer::PRIORITY_PREFERENCES);
364 pref_groups_[syncer::PREFERENCES].Put(syncer::SEARCH_ENGINES);
366 pref_groups_[syncer::TYPED_URLS].Put(syncer::HISTORY_DELETE_DIRECTIVES);
367 pref_groups_[syncer::TYPED_URLS].Put(syncer::SESSIONS);
368 pref_groups_[syncer::TYPED_URLS].Put(syncer::FAVICON_IMAGES);
369 pref_groups_[syncer::TYPED_URLS].Put(syncer::FAVICON_TRACKING);
371 pref_groups_[syncer::PROXY_TABS].Put(syncer::SESSIONS);
372 pref_groups_[syncer::PROXY_TABS].Put(syncer::FAVICON_IMAGES);
373 pref_groups_[syncer::PROXY_TABS].Put(syncer::FAVICON_TRACKING);
375 // TODO(zea): put favicons in the bookmarks group as well once it handles
376 // those favicons.
379 // static
380 void SyncPrefs::RegisterDataTypePreferredPref(
381 user_prefs::PrefRegistrySyncable* registry,
382 syncer::ModelType type,
383 bool is_preferred) {
384 const char* pref_name = GetPrefNameForDataType(type);
385 if (!pref_name) {
386 NOTREACHED();
387 return;
389 registry->RegisterBooleanPref(pref_name, is_preferred);
392 bool SyncPrefs::GetDataTypePreferred(syncer::ModelType type) const {
393 DCHECK(CalledOnValidThread());
394 const char* pref_name = GetPrefNameForDataType(type);
395 if (!pref_name) {
396 NOTREACHED();
397 return false;
400 // Device info is always enabled.
401 if (pref_name == prefs::kSyncDeviceInfo)
402 return true;
404 if (type == syncer::PROXY_TABS &&
405 pref_service_->GetUserPrefValue(pref_name) == NULL &&
406 pref_service_->IsUserModifiablePreference(pref_name)) {
407 // If there is no tab sync preference yet (i.e. newly enabled type),
408 // default to the session sync preference value.
409 pref_name = GetPrefNameForDataType(syncer::SESSIONS);
412 return pref_service_->GetBoolean(pref_name);
415 void SyncPrefs::SetDataTypePreferred(syncer::ModelType type,
416 bool is_preferred) {
417 DCHECK(CalledOnValidThread());
418 const char* pref_name = GetPrefNameForDataType(type);
419 if (!pref_name) {
420 NOTREACHED();
421 return;
424 // Device info is always preferred.
425 if (type == syncer::DEVICE_INFO)
426 return;
428 pref_service_->SetBoolean(pref_name, is_preferred);
431 syncer::ModelTypeSet SyncPrefs::ResolvePrefGroups(
432 syncer::ModelTypeSet registered_types,
433 syncer::ModelTypeSet types) const {
434 syncer::ModelTypeSet types_with_groups = types;
435 for (PrefGroupsMap::const_iterator i = pref_groups_.begin();
436 i != pref_groups_.end();
437 ++i) {
438 if (types.Has(i->first))
439 types_with_groups.PutAll(i->second);
441 types_with_groups.RetainAll(registered_types);
442 return types_with_groups;
445 base::Time SyncPrefs::GetFirstSyncTime() const {
446 return base::Time::FromInternalValue(
447 pref_service_->GetInt64(prefs::kSyncFirstSyncTime));
450 void SyncPrefs::SetFirstSyncTime(base::Time time) {
451 pref_service_->SetInt64(prefs::kSyncFirstSyncTime, time.ToInternalValue());
454 void SyncPrefs::ClearFirstSyncTime() {
455 pref_service_->ClearPref(prefs::kSyncFirstSyncTime);
458 bool SyncPrefs::IsPassphrasePrompted() const {
459 return pref_service_->GetBoolean(prefs::kSyncPassphrasePrompted);
462 void SyncPrefs::SetPassphrasePrompted(bool value) {
463 pref_service_->SetBoolean(prefs::kSyncPassphrasePrompted, value);
466 int SyncPrefs::GetMemoryPressureWarningCount() const {
467 return pref_service_->GetInteger(prefs::kSyncMemoryPressureWarningCount);
470 void SyncPrefs::SetMemoryPressureWarningCount(int value) {
471 pref_service_->SetInteger(prefs::kSyncMemoryPressureWarningCount, value);
474 bool SyncPrefs::DidSyncShutdownCleanly() const {
475 return pref_service_->GetBoolean(prefs::kSyncShutdownCleanly);
478 void SyncPrefs::SetCleanShutdown(bool value) {
479 pref_service_->SetBoolean(prefs::kSyncShutdownCleanly, value);
482 void SyncPrefs::GetInvalidationVersions(
483 std::map<syncer::ModelType, int64>* invalidation_versions) const {
484 const base::DictionaryValue* invalidation_dictionary =
485 pref_service_->GetDictionary(prefs::kSyncInvalidationVersions);
486 syncer::ModelTypeSet protocol_types = syncer::ProtocolTypes();
487 for (auto iter = protocol_types.First(); iter.Good(); iter.Inc()) {
488 std::string key = syncer::ModelTypeToString(iter.Get());
489 std::string version_str;
490 if (!invalidation_dictionary->GetString(key, &version_str))
491 continue;
492 int64 version = 0;
493 if (!base::StringToInt64(version_str, &version))
494 continue;
495 (*invalidation_versions)[iter.Get()] = version;
499 void SyncPrefs::UpdateInvalidationVersions(
500 const std::map<syncer::ModelType, int64>& invalidation_versions) {
501 scoped_ptr<base::DictionaryValue> invalidation_dictionary(
502 new base::DictionaryValue());
503 for (const auto& map_iter : invalidation_versions) {
504 std::string version_str = base::Int64ToString(map_iter.second);
505 invalidation_dictionary->SetString(
506 syncer::ModelTypeToString(map_iter.first), version_str);
508 pref_service_->Set(prefs::kSyncInvalidationVersions,
509 *invalidation_dictionary);
512 } // namespace sync_driver