Clarify and update GN build flags docs.
[chromium-blink-merge.git] / components / sync_driver / sync_prefs.cc
blobfe465e8fcc50032dcc2bf37c7cd367e79fd70055
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);
81 registry->RegisterStringPref(prefs::kSyncSessionsGUID, std::string());
83 registry->RegisterIntegerPref(prefs::kSyncRemainingRollbackTries, 0);
85 registry->RegisterBooleanPref(prefs::kSyncPassphrasePrompted, false);
87 registry->RegisterIntegerPref(prefs::kSyncMemoryPressureWarningCount, -1);
89 registry->RegisterBooleanPref(prefs::kSyncShutdownCleanly, false);
92 void SyncPrefs::AddSyncPrefObserver(SyncPrefObserver* sync_pref_observer) {
93 DCHECK(CalledOnValidThread());
94 sync_pref_observers_.AddObserver(sync_pref_observer);
97 void SyncPrefs::RemoveSyncPrefObserver(SyncPrefObserver* sync_pref_observer) {
98 DCHECK(CalledOnValidThread());
99 sync_pref_observers_.RemoveObserver(sync_pref_observer);
102 void SyncPrefs::ClearPreferences() {
103 DCHECK(CalledOnValidThread());
104 pref_service_->ClearPref(prefs::kSyncLastSyncedTime);
105 pref_service_->ClearPref(prefs::kSyncLastPollTime);
106 pref_service_->ClearPref(prefs::kSyncHasSetupCompleted);
107 pref_service_->ClearPref(prefs::kSyncEncryptionBootstrapToken);
108 pref_service_->ClearPref(prefs::kSyncKeystoreEncryptionBootstrapToken);
109 pref_service_->ClearPref(prefs::kSyncPassphrasePrompted);
111 // TODO(nick): The current behavior does not clear
112 // e.g. prefs::kSyncBookmarks. Is that really what we want?
115 bool SyncPrefs::HasSyncSetupCompleted() const {
116 DCHECK(CalledOnValidThread());
117 return pref_service_->GetBoolean(prefs::kSyncHasSetupCompleted);
120 void SyncPrefs::SetSyncSetupCompleted() {
121 DCHECK(CalledOnValidThread());
122 pref_service_->SetBoolean(prefs::kSyncHasSetupCompleted, true);
123 SetStartSuppressed(false);
126 bool SyncPrefs::SyncHasAuthError() const {
127 DCHECK(CalledOnValidThread());
128 return pref_service_->GetBoolean(prefs::kSyncHasAuthError);
131 void SyncPrefs::SetSyncAuthError(bool error) {
132 DCHECK(CalledOnValidThread());
133 pref_service_->SetBoolean(prefs::kSyncHasAuthError, error);
136 bool SyncPrefs::IsStartSuppressed() const {
137 DCHECK(CalledOnValidThread());
138 return pref_service_->GetBoolean(prefs::kSyncSuppressStart);
141 void SyncPrefs::SetStartSuppressed(bool is_suppressed) {
142 DCHECK(CalledOnValidThread());
143 pref_service_->SetBoolean(prefs::kSyncSuppressStart, is_suppressed);
146 base::Time SyncPrefs::GetLastSyncedTime() const {
147 DCHECK(CalledOnValidThread());
148 return base::Time::FromInternalValue(
149 pref_service_->GetInt64(prefs::kSyncLastSyncedTime));
152 void SyncPrefs::SetLastSyncedTime(base::Time time) {
153 DCHECK(CalledOnValidThread());
154 pref_service_->SetInt64(prefs::kSyncLastSyncedTime, time.ToInternalValue());
157 base::Time SyncPrefs::GetLastPollTime() const {
158 DCHECK(CalledOnValidThread());
159 return base::Time::FromInternalValue(
160 pref_service_->GetInt64(prefs::kSyncLastSyncedTime));
163 void SyncPrefs::SetLastPollTime(base::Time time) {
164 DCHECK(CalledOnValidThread());
165 pref_service_->SetInt64(prefs::kSyncLastPollTime, time.ToInternalValue());
168 bool SyncPrefs::HasKeepEverythingSynced() const {
169 DCHECK(CalledOnValidThread());
170 return pref_service_->GetBoolean(prefs::kSyncKeepEverythingSynced);
173 void SyncPrefs::SetKeepEverythingSynced(bool keep_everything_synced) {
174 DCHECK(CalledOnValidThread());
175 pref_service_->SetBoolean(prefs::kSyncKeepEverythingSynced,
176 keep_everything_synced);
179 syncer::ModelTypeSet SyncPrefs::GetPreferredDataTypes(
180 syncer::ModelTypeSet registered_types) const {
181 DCHECK(CalledOnValidThread());
183 if (pref_service_->GetBoolean(prefs::kSyncKeepEverythingSynced)) {
184 return registered_types;
187 syncer::ModelTypeSet preferred_types;
188 for (syncer::ModelTypeSet::Iterator it = registered_types.First(); it.Good();
189 it.Inc()) {
190 if (GetDataTypePreferred(it.Get())) {
191 preferred_types.Put(it.Get());
194 return ResolvePrefGroups(registered_types, preferred_types);
197 void SyncPrefs::SetPreferredDataTypes(syncer::ModelTypeSet registered_types,
198 syncer::ModelTypeSet preferred_types) {
199 DCHECK(CalledOnValidThread());
200 DCHECK(registered_types.HasAll(preferred_types));
201 preferred_types = ResolvePrefGroups(registered_types, preferred_types);
202 for (syncer::ModelTypeSet::Iterator i = registered_types.First(); i.Good();
203 i.Inc()) {
204 SetDataTypePreferred(i.Get(), preferred_types.Has(i.Get()));
208 bool SyncPrefs::IsManaged() const {
209 DCHECK(CalledOnValidThread());
210 return pref_service_->GetBoolean(prefs::kSyncManaged);
213 std::string SyncPrefs::GetEncryptionBootstrapToken() const {
214 DCHECK(CalledOnValidThread());
215 return pref_service_->GetString(prefs::kSyncEncryptionBootstrapToken);
218 void SyncPrefs::SetEncryptionBootstrapToken(const std::string& token) {
219 DCHECK(CalledOnValidThread());
220 pref_service_->SetString(prefs::kSyncEncryptionBootstrapToken, token);
223 std::string SyncPrefs::GetKeystoreEncryptionBootstrapToken() const {
224 DCHECK(CalledOnValidThread());
225 return pref_service_->GetString(prefs::kSyncKeystoreEncryptionBootstrapToken);
228 void SyncPrefs::SetKeystoreEncryptionBootstrapToken(const std::string& token) {
229 DCHECK(CalledOnValidThread());
230 pref_service_->SetString(prefs::kSyncKeystoreEncryptionBootstrapToken, token);
233 std::string SyncPrefs::GetSyncSessionsGUID() const {
234 DCHECK(CalledOnValidThread());
235 return pref_service_->GetString(prefs::kSyncSessionsGUID);
238 void SyncPrefs::SetSyncSessionsGUID(const std::string& guid) {
239 DCHECK(CalledOnValidThread());
240 pref_service_->SetString(prefs::kSyncSessionsGUID, guid);
243 // static
244 const char* SyncPrefs::GetPrefNameForDataType(syncer::ModelType data_type) {
245 switch (data_type) {
246 case syncer::BOOKMARKS:
247 return prefs::kSyncBookmarks;
248 case syncer::PASSWORDS:
249 return prefs::kSyncPasswords;
250 case syncer::PREFERENCES:
251 return prefs::kSyncPreferences;
252 case syncer::AUTOFILL:
253 return prefs::kSyncAutofill;
254 case syncer::AUTOFILL_PROFILE:
255 return prefs::kSyncAutofillProfile;
256 case syncer::AUTOFILL_WALLET_DATA:
257 return prefs::kSyncAutofillWallet;
258 case syncer::THEMES:
259 return prefs::kSyncThemes;
260 case syncer::TYPED_URLS:
261 return prefs::kSyncTypedUrls;
262 case syncer::EXTENSION_SETTINGS:
263 return prefs::kSyncExtensionSettings;
264 case syncer::EXTENSIONS:
265 return prefs::kSyncExtensions;
266 case syncer::APP_LIST:
267 return prefs::kSyncAppList;
268 case syncer::APP_SETTINGS:
269 return prefs::kSyncAppSettings;
270 case syncer::APPS:
271 return prefs::kSyncApps;
272 case syncer::SEARCH_ENGINES:
273 return prefs::kSyncSearchEngines;
274 case syncer::SESSIONS:
275 return prefs::kSyncSessions;
276 case syncer::APP_NOTIFICATIONS:
277 return prefs::kSyncAppNotifications;
278 case syncer::HISTORY_DELETE_DIRECTIVES:
279 return prefs::kSyncHistoryDeleteDirectives;
280 case syncer::SYNCED_NOTIFICATIONS:
281 return prefs::kSyncSyncedNotifications;
282 case syncer::SYNCED_NOTIFICATION_APP_INFO:
283 return prefs::kSyncSyncedNotificationAppInfo;
284 case syncer::DICTIONARY:
285 return prefs::kSyncDictionary;
286 case syncer::FAVICON_IMAGES:
287 return prefs::kSyncFaviconImages;
288 case syncer::FAVICON_TRACKING:
289 return prefs::kSyncFaviconTracking;
290 case syncer::SUPERVISED_USER_SETTINGS:
291 return prefs::kSyncSupervisedUserSettings;
292 case syncer::PROXY_TABS:
293 return prefs::kSyncTabs;
294 case syncer::PRIORITY_PREFERENCES:
295 return prefs::kSyncPriorityPreferences;
296 case syncer::SUPERVISED_USERS:
297 return prefs::kSyncSupervisedUsers;
298 case syncer::ARTICLES:
299 return prefs::kSyncArticles;
300 case syncer::SUPERVISED_USER_SHARED_SETTINGS:
301 return prefs::kSyncSupervisedUserSharedSettings;
302 case syncer::SUPERVISED_USER_WHITELISTS:
303 return prefs::kSyncSupervisedUserWhitelists;
304 case syncer::DEVICE_INFO:
305 return prefs::kSyncDeviceInfo;
306 case syncer::WIFI_CREDENTIALS:
307 return prefs::kSyncWifiCredentials;
308 default:
309 break;
311 NOTREACHED() << "Type is " << data_type;
312 return NULL;
315 #if defined(OS_CHROMEOS)
316 std::string SyncPrefs::GetSpareBootstrapToken() const {
317 DCHECK(CalledOnValidThread());
318 return pref_service_->GetString(prefs::kSyncSpareBootstrapToken);
321 void SyncPrefs::SetSpareBootstrapToken(const std::string& token) {
322 DCHECK(CalledOnValidThread());
323 pref_service_->SetString(prefs::kSyncSpareBootstrapToken, token);
325 #endif
327 int SyncPrefs::GetRemainingRollbackTries() const {
328 return pref_service_->GetInteger(prefs::kSyncRemainingRollbackTries);
331 void SyncPrefs::SetRemainingRollbackTries(int times) {
332 pref_service_->SetInteger(prefs::kSyncRemainingRollbackTries, times);
335 void SyncPrefs::OnSyncManagedPrefChanged() {
336 DCHECK(CalledOnValidThread());
337 FOR_EACH_OBSERVER(SyncPrefObserver,
338 sync_pref_observers_,
339 OnSyncManagedPrefChange(*pref_sync_managed_));
342 void SyncPrefs::SetManagedForTest(bool is_managed) {
343 DCHECK(CalledOnValidThread());
344 pref_service_->SetBoolean(prefs::kSyncManaged, is_managed);
347 void SyncPrefs::RegisterPrefGroups() {
348 pref_groups_[syncer::APPS].Put(syncer::APP_NOTIFICATIONS);
349 pref_groups_[syncer::APPS].Put(syncer::APP_SETTINGS);
350 pref_groups_[syncer::APPS].Put(syncer::APP_LIST);
352 pref_groups_[syncer::AUTOFILL].Put(syncer::AUTOFILL_PROFILE);
353 pref_groups_[syncer::AUTOFILL].Put(syncer::AUTOFILL_WALLET_DATA);
355 pref_groups_[syncer::EXTENSIONS].Put(syncer::EXTENSION_SETTINGS);
357 pref_groups_[syncer::PREFERENCES].Put(syncer::DICTIONARY);
358 pref_groups_[syncer::PREFERENCES].Put(syncer::PRIORITY_PREFERENCES);
359 pref_groups_[syncer::PREFERENCES].Put(syncer::SEARCH_ENGINES);
361 pref_groups_[syncer::TYPED_URLS].Put(syncer::HISTORY_DELETE_DIRECTIVES);
362 pref_groups_[syncer::TYPED_URLS].Put(syncer::SESSIONS);
363 pref_groups_[syncer::TYPED_URLS].Put(syncer::FAVICON_IMAGES);
364 pref_groups_[syncer::TYPED_URLS].Put(syncer::FAVICON_TRACKING);
366 pref_groups_[syncer::PROXY_TABS].Put(syncer::SESSIONS);
367 pref_groups_[syncer::PROXY_TABS].Put(syncer::FAVICON_IMAGES);
368 pref_groups_[syncer::PROXY_TABS].Put(syncer::FAVICON_TRACKING);
370 // TODO(zea): put favicons in the bookmarks group as well once it handles
371 // those favicons.
374 // static
375 void SyncPrefs::RegisterDataTypePreferredPref(
376 user_prefs::PrefRegistrySyncable* registry,
377 syncer::ModelType type,
378 bool is_preferred) {
379 const char* pref_name = GetPrefNameForDataType(type);
380 if (!pref_name) {
381 NOTREACHED();
382 return;
384 registry->RegisterBooleanPref(pref_name, is_preferred);
387 bool SyncPrefs::GetDataTypePreferred(syncer::ModelType type) const {
388 DCHECK(CalledOnValidThread());
389 const char* pref_name = GetPrefNameForDataType(type);
390 if (!pref_name) {
391 NOTREACHED();
392 return false;
395 // Device info is always enabled.
396 if (pref_name == prefs::kSyncDeviceInfo)
397 return true;
399 if (type == syncer::PROXY_TABS &&
400 pref_service_->GetUserPrefValue(pref_name) == NULL &&
401 pref_service_->IsUserModifiablePreference(pref_name)) {
402 // If there is no tab sync preference yet (i.e. newly enabled type),
403 // default to the session sync preference value.
404 pref_name = GetPrefNameForDataType(syncer::SESSIONS);
407 return pref_service_->GetBoolean(pref_name);
410 void SyncPrefs::SetDataTypePreferred(syncer::ModelType type,
411 bool is_preferred) {
412 DCHECK(CalledOnValidThread());
413 const char* pref_name = GetPrefNameForDataType(type);
414 if (!pref_name) {
415 NOTREACHED();
416 return;
419 // Device info is always preferred.
420 if (type == syncer::DEVICE_INFO)
421 return;
423 pref_service_->SetBoolean(pref_name, is_preferred);
426 syncer::ModelTypeSet SyncPrefs::ResolvePrefGroups(
427 syncer::ModelTypeSet registered_types,
428 syncer::ModelTypeSet types) const {
429 DCHECK(registered_types.HasAll(types));
430 syncer::ModelTypeSet types_with_groups = types;
431 for (PrefGroupsMap::const_iterator i = pref_groups_.begin();
432 i != pref_groups_.end();
433 ++i) {
434 if (types.Has(i->first))
435 types_with_groups.PutAll(i->second);
437 types_with_groups.RetainAll(registered_types);
438 return types_with_groups;
441 base::Time SyncPrefs::GetFirstSyncTime() const {
442 return base::Time::FromInternalValue(
443 pref_service_->GetInt64(prefs::kSyncFirstSyncTime));
446 void SyncPrefs::SetFirstSyncTime(base::Time time) {
447 pref_service_->SetInt64(prefs::kSyncFirstSyncTime, time.ToInternalValue());
450 void SyncPrefs::ClearFirstSyncTime() {
451 pref_service_->ClearPref(prefs::kSyncFirstSyncTime);
454 bool SyncPrefs::IsPassphrasePrompted() const {
455 return pref_service_->GetBoolean(prefs::kSyncPassphrasePrompted);
458 void SyncPrefs::SetPassphrasePrompted(bool value) {
459 pref_service_->SetBoolean(prefs::kSyncPassphrasePrompted, value);
462 int SyncPrefs::GetMemoryPressureWarningCount() const {
463 return pref_service_->GetInteger(prefs::kSyncMemoryPressureWarningCount);
466 void SyncPrefs::SetMemoryPressureWarningCount(int value) {
467 pref_service_->SetInteger(prefs::kSyncMemoryPressureWarningCount, value);
470 bool SyncPrefs::DidSyncShutdownCleanly() const {
471 return pref_service_->GetBoolean(prefs::kSyncShutdownCleanly);
474 void SyncPrefs::SetCleanShutdown(bool value) {
475 pref_service_->SetBoolean(prefs::kSyncShutdownCleanly, value);
478 } // namespace sync_driver