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
) {
23 // Watch the preference that indicates sync is managed so we can take
24 // appropriate action.
25 pref_sync_managed_
.Init(
28 base::Bind(&SyncPrefs::OnSyncManagedPrefChanged
, base::Unretained(this)));
31 SyncPrefs::SyncPrefs() : pref_service_(NULL
) {}
33 SyncPrefs::~SyncPrefs() { DCHECK(CalledOnValidThread()); }
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();
67 RegisterDataTypePreferredPref(registry
, it
.Get(), false);
70 registry
->RegisterBooleanPref(prefs::kSyncManaged
, false);
71 registry
->RegisterStringPref(prefs::kSyncEncryptionBootstrapToken
,
73 registry
->RegisterStringPref(prefs::kSyncKeystoreEncryptionBootstrapToken
,
75 #if defined(OS_CHROMEOS)
76 registry
->RegisterStringPref(prefs::kSyncSpareBootstrapToken
, "");
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 SetSyncRequested(true);
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::IsSyncRequested() const {
137 DCHECK(CalledOnValidThread());
138 // IsSyncRequested is the inverse of the old SuppressStart pref.
139 // Since renaming a pref value is hard, here we still use the old one.
140 return !pref_service_
->GetBoolean(prefs::kSyncSuppressStart
);
143 void SyncPrefs::SetSyncRequested(bool is_requested
) {
144 DCHECK(CalledOnValidThread());
145 // See IsSyncRequested for why we use this pref and !is_requested.
146 pref_service_
->SetBoolean(prefs::kSyncSuppressStart
, !is_requested
);
149 base::Time
SyncPrefs::GetLastSyncedTime() const {
150 DCHECK(CalledOnValidThread());
151 return base::Time::FromInternalValue(
152 pref_service_
->GetInt64(prefs::kSyncLastSyncedTime
));
155 void SyncPrefs::SetLastSyncedTime(base::Time time
) {
156 DCHECK(CalledOnValidThread());
157 pref_service_
->SetInt64(prefs::kSyncLastSyncedTime
, time
.ToInternalValue());
160 base::Time
SyncPrefs::GetLastPollTime() const {
161 DCHECK(CalledOnValidThread());
162 return base::Time::FromInternalValue(
163 pref_service_
->GetInt64(prefs::kSyncLastSyncedTime
));
166 void SyncPrefs::SetLastPollTime(base::Time time
) {
167 DCHECK(CalledOnValidThread());
168 pref_service_
->SetInt64(prefs::kSyncLastPollTime
, time
.ToInternalValue());
171 bool SyncPrefs::HasKeepEverythingSynced() const {
172 DCHECK(CalledOnValidThread());
173 return pref_service_
->GetBoolean(prefs::kSyncKeepEverythingSynced
);
176 void SyncPrefs::SetKeepEverythingSynced(bool keep_everything_synced
) {
177 DCHECK(CalledOnValidThread());
178 pref_service_
->SetBoolean(prefs::kSyncKeepEverythingSynced
,
179 keep_everything_synced
);
182 syncer::ModelTypeSet
SyncPrefs::GetPreferredDataTypes(
183 syncer::ModelTypeSet registered_types
) const {
184 DCHECK(CalledOnValidThread());
186 if (pref_service_
->GetBoolean(prefs::kSyncKeepEverythingSynced
)) {
187 return registered_types
;
190 syncer::ModelTypeSet preferred_types
;
191 for (syncer::ModelTypeSet::Iterator it
= registered_types
.First(); it
.Good();
193 if (GetDataTypePreferred(it
.Get())) {
194 preferred_types
.Put(it
.Get());
197 return ResolvePrefGroups(registered_types
, preferred_types
);
200 void SyncPrefs::SetPreferredDataTypes(syncer::ModelTypeSet registered_types
,
201 syncer::ModelTypeSet preferred_types
) {
202 DCHECK(CalledOnValidThread());
203 DCHECK(registered_types
.HasAll(preferred_types
));
204 preferred_types
= ResolvePrefGroups(registered_types
, preferred_types
);
205 for (syncer::ModelTypeSet::Iterator i
= registered_types
.First(); i
.Good();
207 SetDataTypePreferred(i
.Get(), preferred_types
.Has(i
.Get()));
211 bool SyncPrefs::IsManaged() const {
212 DCHECK(CalledOnValidThread());
213 return pref_service_
->GetBoolean(prefs::kSyncManaged
);
216 std::string
SyncPrefs::GetEncryptionBootstrapToken() const {
217 DCHECK(CalledOnValidThread());
218 return pref_service_
->GetString(prefs::kSyncEncryptionBootstrapToken
);
221 void SyncPrefs::SetEncryptionBootstrapToken(const std::string
& token
) {
222 DCHECK(CalledOnValidThread());
223 pref_service_
->SetString(prefs::kSyncEncryptionBootstrapToken
, token
);
226 std::string
SyncPrefs::GetKeystoreEncryptionBootstrapToken() const {
227 DCHECK(CalledOnValidThread());
228 return pref_service_
->GetString(prefs::kSyncKeystoreEncryptionBootstrapToken
);
231 void SyncPrefs::SetKeystoreEncryptionBootstrapToken(const std::string
& token
) {
232 DCHECK(CalledOnValidThread());
233 pref_service_
->SetString(prefs::kSyncKeystoreEncryptionBootstrapToken
, token
);
236 std::string
SyncPrefs::GetSyncSessionsGUID() const {
237 DCHECK(CalledOnValidThread());
238 return pref_service_
->GetString(prefs::kSyncSessionsGUID
);
241 void SyncPrefs::SetSyncSessionsGUID(const std::string
& guid
) {
242 DCHECK(CalledOnValidThread());
243 pref_service_
->SetString(prefs::kSyncSessionsGUID
, guid
);
247 const char* SyncPrefs::GetPrefNameForDataType(syncer::ModelType data_type
) {
249 case syncer::BOOKMARKS
:
250 return prefs::kSyncBookmarks
;
251 case syncer::PASSWORDS
:
252 return prefs::kSyncPasswords
;
253 case syncer::PREFERENCES
:
254 return prefs::kSyncPreferences
;
255 case syncer::AUTOFILL
:
256 return prefs::kSyncAutofill
;
257 case syncer::AUTOFILL_PROFILE
:
258 return prefs::kSyncAutofillProfile
;
259 case syncer::AUTOFILL_WALLET_DATA
:
260 return prefs::kSyncAutofillWallet
;
261 case syncer::AUTOFILL_WALLET_METADATA
:
262 return prefs::kSyncAutofillWalletMetadata
;
264 return prefs::kSyncThemes
;
265 case syncer::TYPED_URLS
:
266 return prefs::kSyncTypedUrls
;
267 case syncer::EXTENSION_SETTINGS
:
268 return prefs::kSyncExtensionSettings
;
269 case syncer::EXTENSIONS
:
270 return prefs::kSyncExtensions
;
271 case syncer::APP_LIST
:
272 return prefs::kSyncAppList
;
273 case syncer::APP_SETTINGS
:
274 return prefs::kSyncAppSettings
;
276 return prefs::kSyncApps
;
277 case syncer::SEARCH_ENGINES
:
278 return prefs::kSyncSearchEngines
;
279 case syncer::SESSIONS
:
280 return prefs::kSyncSessions
;
281 case syncer::APP_NOTIFICATIONS
:
282 return prefs::kSyncAppNotifications
;
283 case syncer::HISTORY_DELETE_DIRECTIVES
:
284 return prefs::kSyncHistoryDeleteDirectives
;
285 case syncer::SYNCED_NOTIFICATIONS
:
286 return prefs::kSyncSyncedNotifications
;
287 case syncer::SYNCED_NOTIFICATION_APP_INFO
:
288 return prefs::kSyncSyncedNotificationAppInfo
;
289 case syncer::DICTIONARY
:
290 return prefs::kSyncDictionary
;
291 case syncer::FAVICON_IMAGES
:
292 return prefs::kSyncFaviconImages
;
293 case syncer::FAVICON_TRACKING
:
294 return prefs::kSyncFaviconTracking
;
295 case syncer::SUPERVISED_USER_SETTINGS
:
296 return prefs::kSyncSupervisedUserSettings
;
297 case syncer::PROXY_TABS
:
298 return prefs::kSyncTabs
;
299 case syncer::PRIORITY_PREFERENCES
:
300 return prefs::kSyncPriorityPreferences
;
301 case syncer::SUPERVISED_USERS
:
302 return prefs::kSyncSupervisedUsers
;
303 case syncer::ARTICLES
:
304 return prefs::kSyncArticles
;
305 case syncer::SUPERVISED_USER_SHARED_SETTINGS
:
306 return prefs::kSyncSupervisedUserSharedSettings
;
307 case syncer::SUPERVISED_USER_WHITELISTS
:
308 return prefs::kSyncSupervisedUserWhitelists
;
309 case syncer::DEVICE_INFO
:
310 return prefs::kSyncDeviceInfo
;
311 case syncer::WIFI_CREDENTIALS
:
312 return prefs::kSyncWifiCredentials
;
316 NOTREACHED() << "Type is " << data_type
;
320 #if defined(OS_CHROMEOS)
321 std::string
SyncPrefs::GetSpareBootstrapToken() const {
322 DCHECK(CalledOnValidThread());
323 return pref_service_
->GetString(prefs::kSyncSpareBootstrapToken
);
326 void SyncPrefs::SetSpareBootstrapToken(const std::string
& token
) {
327 DCHECK(CalledOnValidThread());
328 pref_service_
->SetString(prefs::kSyncSpareBootstrapToken
, token
);
332 int SyncPrefs::GetRemainingRollbackTries() const {
333 return pref_service_
->GetInteger(prefs::kSyncRemainingRollbackTries
);
336 void SyncPrefs::SetRemainingRollbackTries(int times
) {
337 pref_service_
->SetInteger(prefs::kSyncRemainingRollbackTries
, times
);
340 void SyncPrefs::OnSyncManagedPrefChanged() {
341 DCHECK(CalledOnValidThread());
342 FOR_EACH_OBSERVER(SyncPrefObserver
,
343 sync_pref_observers_
,
344 OnSyncManagedPrefChange(*pref_sync_managed_
));
347 void SyncPrefs::SetManagedForTest(bool is_managed
) {
348 DCHECK(CalledOnValidThread());
349 pref_service_
->SetBoolean(prefs::kSyncManaged
, is_managed
);
352 void SyncPrefs::RegisterPrefGroups() {
353 pref_groups_
[syncer::APPS
].Put(syncer::APP_NOTIFICATIONS
);
354 pref_groups_
[syncer::APPS
].Put(syncer::APP_SETTINGS
);
355 pref_groups_
[syncer::APPS
].Put(syncer::APP_LIST
);
357 pref_groups_
[syncer::AUTOFILL
].Put(syncer::AUTOFILL_PROFILE
);
358 pref_groups_
[syncer::AUTOFILL
].Put(syncer::AUTOFILL_WALLET_DATA
);
359 pref_groups_
[syncer::AUTOFILL
].Put(syncer::AUTOFILL_WALLET_METADATA
);
361 pref_groups_
[syncer::EXTENSIONS
].Put(syncer::EXTENSION_SETTINGS
);
363 pref_groups_
[syncer::PREFERENCES
].Put(syncer::DICTIONARY
);
364 pref_groups_
[syncer::PREFERENCES
].Put(syncer::PRIORITY_PREFERENCES
);
365 pref_groups_
[syncer::PREFERENCES
].Put(syncer::SEARCH_ENGINES
);
367 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::HISTORY_DELETE_DIRECTIVES
);
368 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::SESSIONS
);
369 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::FAVICON_IMAGES
);
370 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::FAVICON_TRACKING
);
372 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::SESSIONS
);
373 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::FAVICON_IMAGES
);
374 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::FAVICON_TRACKING
);
376 // TODO(zea): put favicons in the bookmarks group as well once it handles
381 void SyncPrefs::RegisterDataTypePreferredPref(
382 user_prefs::PrefRegistrySyncable
* registry
,
383 syncer::ModelType type
,
385 const char* pref_name
= GetPrefNameForDataType(type
);
390 registry
->RegisterBooleanPref(pref_name
, is_preferred
);
393 bool SyncPrefs::GetDataTypePreferred(syncer::ModelType type
) const {
394 DCHECK(CalledOnValidThread());
395 const char* pref_name
= GetPrefNameForDataType(type
);
401 // Device info is always enabled.
402 if (pref_name
== prefs::kSyncDeviceInfo
)
405 if (type
== syncer::PROXY_TABS
&&
406 pref_service_
->GetUserPrefValue(pref_name
) == NULL
&&
407 pref_service_
->IsUserModifiablePreference(pref_name
)) {
408 // If there is no tab sync preference yet (i.e. newly enabled type),
409 // default to the session sync preference value.
410 pref_name
= GetPrefNameForDataType(syncer::SESSIONS
);
413 return pref_service_
->GetBoolean(pref_name
);
416 void SyncPrefs::SetDataTypePreferred(syncer::ModelType type
,
418 DCHECK(CalledOnValidThread());
419 const char* pref_name
= GetPrefNameForDataType(type
);
425 // Device info is always preferred.
426 if (type
== syncer::DEVICE_INFO
)
429 pref_service_
->SetBoolean(pref_name
, is_preferred
);
432 syncer::ModelTypeSet
SyncPrefs::ResolvePrefGroups(
433 syncer::ModelTypeSet registered_types
,
434 syncer::ModelTypeSet types
) const {
435 DCHECK(registered_types
.HasAll(types
));
436 syncer::ModelTypeSet types_with_groups
= types
;
437 for (PrefGroupsMap::const_iterator i
= pref_groups_
.begin();
438 i
!= pref_groups_
.end();
440 if (types
.Has(i
->first
))
441 types_with_groups
.PutAll(i
->second
);
443 types_with_groups
.RetainAll(registered_types
);
444 return types_with_groups
;
447 base::Time
SyncPrefs::GetFirstSyncTime() const {
448 return base::Time::FromInternalValue(
449 pref_service_
->GetInt64(prefs::kSyncFirstSyncTime
));
452 void SyncPrefs::SetFirstSyncTime(base::Time time
) {
453 pref_service_
->SetInt64(prefs::kSyncFirstSyncTime
, time
.ToInternalValue());
456 void SyncPrefs::ClearFirstSyncTime() {
457 pref_service_
->ClearPref(prefs::kSyncFirstSyncTime
);
460 bool SyncPrefs::IsPassphrasePrompted() const {
461 return pref_service_
->GetBoolean(prefs::kSyncPassphrasePrompted
);
464 void SyncPrefs::SetPassphrasePrompted(bool value
) {
465 pref_service_
->SetBoolean(prefs::kSyncPassphrasePrompted
, value
);
468 int SyncPrefs::GetMemoryPressureWarningCount() const {
469 return pref_service_
->GetInteger(prefs::kSyncMemoryPressureWarningCount
);
472 void SyncPrefs::SetMemoryPressureWarningCount(int value
) {
473 pref_service_
->SetInteger(prefs::kSyncMemoryPressureWarningCount
, value
);
476 bool SyncPrefs::DidSyncShutdownCleanly() const {
477 return pref_service_
->GetBoolean(prefs::kSyncShutdownCleanly
);
480 void SyncPrefs::SetCleanShutdown(bool value
) {
481 pref_service_
->SetBoolean(prefs::kSyncShutdownCleanly
, value
);
484 } // namespace sync_driver