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(
39 prefs::kSyncHasSetupCompleted
,
41 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
42 registry
->RegisterBooleanPref(
43 prefs::kSyncSuppressStart
,
45 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
46 registry
->RegisterInt64Pref(
47 prefs::kSyncLastSyncedTime
,
49 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
50 registry
->RegisterInt64Pref(
51 prefs::kSyncFirstSyncTime
,
53 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
55 // All datatypes are on by default, but this gets set explicitly
56 // when you configure sync (when turning it on), in
57 // ProfileSyncService::OnUserChoseDatatypes.
58 registry
->RegisterBooleanPref(
59 prefs::kSyncKeepEverythingSynced
,
61 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
63 syncer::ModelTypeSet user_types
= syncer::UserTypes();
65 // Include proxy types as well, as they can be individually selected,
66 // although they don't have sync representations.
67 user_types
.PutAll(syncer::ProxyTypes());
69 // Treat bookmarks and device info specially.
70 RegisterDataTypePreferredPref(registry
, syncer::BOOKMARKS
, true);
71 RegisterDataTypePreferredPref(registry
, syncer::DEVICE_INFO
, true);
72 user_types
.Remove(syncer::BOOKMARKS
);
73 user_types
.Remove(syncer::DEVICE_INFO
);
75 // All types are set to off by default, which forces a configuration to
76 // explicitly enable them. GetPreferredTypes() will ensure that any new
77 // implicit types are enabled when their pref group is, or via
78 // KeepEverythingSynced.
79 for (syncer::ModelTypeSet::Iterator it
= user_types
.First(); it
.Good();
81 RegisterDataTypePreferredPref(registry
, it
.Get(), false);
84 registry
->RegisterBooleanPref(
87 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
88 registry
->RegisterStringPref(
89 prefs::kSyncEncryptionBootstrapToken
,
91 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
92 registry
->RegisterStringPref(
93 prefs::kSyncKeystoreEncryptionBootstrapToken
,
95 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
96 #if defined(OS_CHROMEOS)
97 registry
->RegisterStringPref(
98 prefs::kSyncSpareBootstrapToken
,
100 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
103 registry
->RegisterBooleanPref(
104 prefs::kSyncHasAuthError
,
106 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
108 registry
->RegisterStringPref(
109 prefs::kSyncSessionsGUID
,
111 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
113 registry
->RegisterIntegerPref(
114 prefs::kSyncRemainingRollbackTries
, 0,
115 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
118 void SyncPrefs::AddSyncPrefObserver(SyncPrefObserver
* sync_pref_observer
) {
119 DCHECK(CalledOnValidThread());
120 sync_pref_observers_
.AddObserver(sync_pref_observer
);
123 void SyncPrefs::RemoveSyncPrefObserver(SyncPrefObserver
* sync_pref_observer
) {
124 DCHECK(CalledOnValidThread());
125 sync_pref_observers_
.RemoveObserver(sync_pref_observer
);
128 void SyncPrefs::ClearPreferences() {
129 DCHECK(CalledOnValidThread());
130 pref_service_
->ClearPref(prefs::kSyncLastSyncedTime
);
131 pref_service_
->ClearPref(prefs::kSyncHasSetupCompleted
);
132 pref_service_
->ClearPref(prefs::kSyncEncryptionBootstrapToken
);
133 pref_service_
->ClearPref(prefs::kSyncKeystoreEncryptionBootstrapToken
);
135 // TODO(nick): The current behavior does not clear
136 // e.g. prefs::kSyncBookmarks. Is that really what we want?
139 bool SyncPrefs::HasSyncSetupCompleted() const {
140 DCHECK(CalledOnValidThread());
141 return pref_service_
->GetBoolean(prefs::kSyncHasSetupCompleted
);
144 void SyncPrefs::SetSyncSetupCompleted() {
145 DCHECK(CalledOnValidThread());
146 pref_service_
->SetBoolean(prefs::kSyncHasSetupCompleted
, true);
147 SetStartSuppressed(false);
150 bool SyncPrefs::SyncHasAuthError() const {
151 DCHECK(CalledOnValidThread());
152 return pref_service_
->GetBoolean(prefs::kSyncHasAuthError
);
155 void SyncPrefs::SetSyncAuthError(bool error
) {
156 DCHECK(CalledOnValidThread());
157 pref_service_
->SetBoolean(prefs::kSyncHasAuthError
, error
);
160 bool SyncPrefs::IsStartSuppressed() const {
161 DCHECK(CalledOnValidThread());
162 return pref_service_
->GetBoolean(prefs::kSyncSuppressStart
);
165 void SyncPrefs::SetStartSuppressed(bool is_suppressed
) {
166 DCHECK(CalledOnValidThread());
167 pref_service_
->SetBoolean(prefs::kSyncSuppressStart
, is_suppressed
);
170 base::Time
SyncPrefs::GetLastSyncedTime() const {
171 DCHECK(CalledOnValidThread());
172 return base::Time::FromInternalValue(
173 pref_service_
->GetInt64(prefs::kSyncLastSyncedTime
));
176 void SyncPrefs::SetLastSyncedTime(base::Time time
) {
177 DCHECK(CalledOnValidThread());
178 pref_service_
->SetInt64(prefs::kSyncLastSyncedTime
, time
.ToInternalValue());
181 bool SyncPrefs::HasKeepEverythingSynced() const {
182 DCHECK(CalledOnValidThread());
183 return pref_service_
->GetBoolean(prefs::kSyncKeepEverythingSynced
);
186 void SyncPrefs::SetKeepEverythingSynced(bool keep_everything_synced
) {
187 DCHECK(CalledOnValidThread());
188 pref_service_
->SetBoolean(prefs::kSyncKeepEverythingSynced
,
189 keep_everything_synced
);
192 syncer::ModelTypeSet
SyncPrefs::GetPreferredDataTypes(
193 syncer::ModelTypeSet registered_types
) const {
194 DCHECK(CalledOnValidThread());
196 if (pref_service_
->GetBoolean(prefs::kSyncKeepEverythingSynced
)) {
197 return registered_types
;
200 syncer::ModelTypeSet preferred_types
;
201 for (syncer::ModelTypeSet::Iterator it
= registered_types
.First(); it
.Good();
203 if (GetDataTypePreferred(it
.Get())) {
204 preferred_types
.Put(it
.Get());
207 return ResolvePrefGroups(registered_types
, preferred_types
);
210 void SyncPrefs::SetPreferredDataTypes(syncer::ModelTypeSet registered_types
,
211 syncer::ModelTypeSet preferred_types
) {
212 DCHECK(CalledOnValidThread());
213 DCHECK(registered_types
.HasAll(preferred_types
));
214 preferred_types
= ResolvePrefGroups(registered_types
, preferred_types
);
215 for (syncer::ModelTypeSet::Iterator i
= registered_types
.First(); i
.Good();
217 SetDataTypePreferred(i
.Get(), preferred_types
.Has(i
.Get()));
221 bool SyncPrefs::IsManaged() const {
222 DCHECK(CalledOnValidThread());
223 return pref_service_
->GetBoolean(prefs::kSyncManaged
);
226 std::string
SyncPrefs::GetEncryptionBootstrapToken() const {
227 DCHECK(CalledOnValidThread());
228 return pref_service_
->GetString(prefs::kSyncEncryptionBootstrapToken
);
231 void SyncPrefs::SetEncryptionBootstrapToken(const std::string
& token
) {
232 DCHECK(CalledOnValidThread());
233 pref_service_
->SetString(prefs::kSyncEncryptionBootstrapToken
, token
);
236 std::string
SyncPrefs::GetKeystoreEncryptionBootstrapToken() const {
237 DCHECK(CalledOnValidThread());
238 return pref_service_
->GetString(prefs::kSyncKeystoreEncryptionBootstrapToken
);
241 void SyncPrefs::SetKeystoreEncryptionBootstrapToken(const std::string
& token
) {
242 DCHECK(CalledOnValidThread());
243 pref_service_
->SetString(prefs::kSyncKeystoreEncryptionBootstrapToken
, token
);
246 std::string
SyncPrefs::GetSyncSessionsGUID() const {
247 DCHECK(CalledOnValidThread());
248 return pref_service_
->GetString(prefs::kSyncSessionsGUID
);
251 void SyncPrefs::SetSyncSessionsGUID(const std::string
& guid
) {
252 DCHECK(CalledOnValidThread());
253 pref_service_
->SetString(prefs::kSyncSessionsGUID
, guid
);
257 const char* SyncPrefs::GetPrefNameForDataType(syncer::ModelType data_type
) {
259 case syncer::BOOKMARKS
:
260 return prefs::kSyncBookmarks
;
261 case syncer::PASSWORDS
:
262 return prefs::kSyncPasswords
;
263 case syncer::PREFERENCES
:
264 return prefs::kSyncPreferences
;
265 case syncer::AUTOFILL
:
266 return prefs::kSyncAutofill
;
267 case syncer::AUTOFILL_PROFILE
:
268 return prefs::kSyncAutofillWallet
;
269 case syncer::AUTOFILL_WALLET_DATA
:
270 return prefs::kSyncAutofillProfile
;
272 return prefs::kSyncThemes
;
273 case syncer::TYPED_URLS
:
274 return prefs::kSyncTypedUrls
;
275 case syncer::EXTENSION_SETTINGS
:
276 return prefs::kSyncExtensionSettings
;
277 case syncer::EXTENSIONS
:
278 return prefs::kSyncExtensions
;
279 case syncer::APP_LIST
:
280 return prefs::kSyncAppList
;
281 case syncer::APP_SETTINGS
:
282 return prefs::kSyncAppSettings
;
284 return prefs::kSyncApps
;
285 case syncer::SEARCH_ENGINES
:
286 return prefs::kSyncSearchEngines
;
287 case syncer::SESSIONS
:
288 return prefs::kSyncSessions
;
289 case syncer::APP_NOTIFICATIONS
:
290 return prefs::kSyncAppNotifications
;
291 case syncer::HISTORY_DELETE_DIRECTIVES
:
292 return prefs::kSyncHistoryDeleteDirectives
;
293 case syncer::SYNCED_NOTIFICATIONS
:
294 return prefs::kSyncSyncedNotifications
;
295 case syncer::SYNCED_NOTIFICATION_APP_INFO
:
296 return prefs::kSyncSyncedNotificationAppInfo
;
297 case syncer::DICTIONARY
:
298 return prefs::kSyncDictionary
;
299 case syncer::FAVICON_IMAGES
:
300 return prefs::kSyncFaviconImages
;
301 case syncer::FAVICON_TRACKING
:
302 return prefs::kSyncFaviconTracking
;
303 case syncer::SUPERVISED_USER_SETTINGS
:
304 return prefs::kSyncSupervisedUserSettings
;
305 case syncer::PROXY_TABS
:
306 return prefs::kSyncTabs
;
307 case syncer::PRIORITY_PREFERENCES
:
308 return prefs::kSyncPriorityPreferences
;
309 case syncer::SUPERVISED_USERS
:
310 return prefs::kSyncSupervisedUsers
;
311 case syncer::ARTICLES
:
312 return prefs::kSyncArticles
;
313 case syncer::SUPERVISED_USER_SHARED_SETTINGS
:
314 return prefs::kSyncSupervisedUserSharedSettings
;
315 case syncer::SUPERVISED_USER_WHITELISTS
:
316 return prefs::kSyncSupervisedUserWhitelists
;
317 case syncer::DEVICE_INFO
:
318 return prefs::kSyncDeviceInfo
;
319 case syncer::WIFI_CREDENTIALS
:
320 return prefs::kSyncWifiCredentials
;
324 NOTREACHED() << "Type is " << data_type
;
328 #if defined(OS_CHROMEOS)
329 std::string
SyncPrefs::GetSpareBootstrapToken() const {
330 DCHECK(CalledOnValidThread());
331 return pref_service_
->GetString(prefs::kSyncSpareBootstrapToken
);
334 void SyncPrefs::SetSpareBootstrapToken(const std::string
& token
) {
335 DCHECK(CalledOnValidThread());
336 pref_service_
->SetString(prefs::kSyncSpareBootstrapToken
, token
);
340 int SyncPrefs::GetRemainingRollbackTries() const {
341 return pref_service_
->GetInteger(prefs::kSyncRemainingRollbackTries
);
344 void SyncPrefs::SetRemainingRollbackTries(int times
) {
345 pref_service_
->SetInteger(prefs::kSyncRemainingRollbackTries
, times
);
348 void SyncPrefs::OnSyncManagedPrefChanged() {
349 DCHECK(CalledOnValidThread());
350 FOR_EACH_OBSERVER(SyncPrefObserver
,
351 sync_pref_observers_
,
352 OnSyncManagedPrefChange(*pref_sync_managed_
));
355 void SyncPrefs::SetManagedForTest(bool is_managed
) {
356 DCHECK(CalledOnValidThread());
357 pref_service_
->SetBoolean(prefs::kSyncManaged
, is_managed
);
360 void SyncPrefs::RegisterPrefGroups() {
361 pref_groups_
[syncer::APPS
].Put(syncer::APP_NOTIFICATIONS
);
362 pref_groups_
[syncer::APPS
].Put(syncer::APP_SETTINGS
);
363 pref_groups_
[syncer::APPS
].Put(syncer::APP_LIST
);
365 pref_groups_
[syncer::AUTOFILL
].Put(syncer::AUTOFILL_PROFILE
);
366 pref_groups_
[syncer::AUTOFILL
].Put(syncer::AUTOFILL_WALLET_DATA
);
368 pref_groups_
[syncer::EXTENSIONS
].Put(syncer::EXTENSION_SETTINGS
);
370 pref_groups_
[syncer::PREFERENCES
].Put(syncer::DICTIONARY
);
371 pref_groups_
[syncer::PREFERENCES
].Put(syncer::PRIORITY_PREFERENCES
);
372 pref_groups_
[syncer::PREFERENCES
].Put(syncer::SEARCH_ENGINES
);
374 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::HISTORY_DELETE_DIRECTIVES
);
375 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::SESSIONS
);
376 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::FAVICON_IMAGES
);
377 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::FAVICON_TRACKING
);
379 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::SESSIONS
);
380 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::FAVICON_IMAGES
);
381 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::FAVICON_TRACKING
);
383 // TODO(zea): put favicons in the bookmarks group as well once it handles
388 void SyncPrefs::RegisterDataTypePreferredPref(
389 user_prefs::PrefRegistrySyncable
* registry
,
390 syncer::ModelType type
,
392 const char* pref_name
= GetPrefNameForDataType(type
);
397 registry
->RegisterBooleanPref(
400 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
403 bool SyncPrefs::GetDataTypePreferred(syncer::ModelType type
) const {
404 DCHECK(CalledOnValidThread());
405 const char* pref_name
= GetPrefNameForDataType(type
);
411 // Device info is always enabled.
412 if (pref_name
== prefs::kSyncDeviceInfo
)
415 if (type
== syncer::PROXY_TABS
&&
416 pref_service_
->GetUserPrefValue(pref_name
) == NULL
&&
417 pref_service_
->IsUserModifiablePreference(pref_name
)) {
418 // If there is no tab sync preference yet (i.e. newly enabled type),
419 // default to the session sync preference value.
420 pref_name
= GetPrefNameForDataType(syncer::SESSIONS
);
423 return pref_service_
->GetBoolean(pref_name
);
426 void SyncPrefs::SetDataTypePreferred(syncer::ModelType type
,
428 DCHECK(CalledOnValidThread());
429 const char* pref_name
= GetPrefNameForDataType(type
);
435 // Device info is always preferred.
436 if (type
== syncer::DEVICE_INFO
)
439 pref_service_
->SetBoolean(pref_name
, is_preferred
);
442 syncer::ModelTypeSet
SyncPrefs::ResolvePrefGroups(
443 syncer::ModelTypeSet registered_types
,
444 syncer::ModelTypeSet types
) const {
445 DCHECK(registered_types
.HasAll(types
));
446 syncer::ModelTypeSet types_with_groups
= types
;
447 for (PrefGroupsMap::const_iterator i
= pref_groups_
.begin();
448 i
!= pref_groups_
.end();
450 if (types
.Has(i
->first
))
451 types_with_groups
.PutAll(i
->second
);
453 types_with_groups
.RetainAll(registered_types
);
454 return types_with_groups
;
457 base::Time
SyncPrefs::GetFirstSyncTime() const {
458 return base::Time::FromInternalValue(
459 pref_service_
->GetInt64(prefs::kSyncFirstSyncTime
));
462 void SyncPrefs::SetFirstSyncTime(base::Time time
) {
463 pref_service_
->SetInt64(prefs::kSyncFirstSyncTime
, time
.ToInternalValue());
466 void SyncPrefs::ClearFirstSyncTime() {
467 pref_service_
->ClearPref(prefs::kSyncFirstSyncTime
);
470 } // namespace sync_driver