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 specially.
70 RegisterDataTypePreferredPref(registry
, syncer::BOOKMARKS
, true);
71 user_types
.Remove(syncer::BOOKMARKS
);
73 // These two prefs are set from sync experiment to enable enhanced bookmarks.
74 registry
->RegisterIntegerPref(
75 prefs::kEnhancedBookmarksExperimentEnabled
,
77 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
79 registry
->RegisterStringPref(
80 prefs::kEnhancedBookmarksExtensionId
,
82 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
84 // All types are set to off by default, which forces a configuration to
85 // explicitly enable them. GetPreferredTypes() will ensure that any new
86 // implicit types are enabled when their pref group is, or via
87 // KeepEverythingSynced.
88 for (syncer::ModelTypeSet::Iterator it
= user_types
.First(); it
.Good();
90 RegisterDataTypePreferredPref(registry
, it
.Get(), false);
93 registry
->RegisterBooleanPref(
96 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
97 registry
->RegisterStringPref(
98 prefs::kSyncEncryptionBootstrapToken
,
100 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
101 registry
->RegisterStringPref(
102 prefs::kSyncKeystoreEncryptionBootstrapToken
,
104 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
105 #if defined(OS_CHROMEOS)
106 registry
->RegisterStringPref(
107 prefs::kSyncSpareBootstrapToken
,
109 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
112 registry
->RegisterBooleanPref(
113 prefs::kSyncHasAuthError
,
115 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
117 registry
->RegisterStringPref(
118 prefs::kSyncSessionsGUID
,
120 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
122 // We will start prompting people about new data types after the launch of
123 // SESSIONS - all previously launched data types are treated as if they are
124 // already acknowledged.
125 syncer::ModelTypeSet model_set
;
126 model_set
.Put(syncer::BOOKMARKS
);
127 model_set
.Put(syncer::PREFERENCES
);
128 model_set
.Put(syncer::PASSWORDS
);
129 model_set
.Put(syncer::AUTOFILL_PROFILE
);
130 model_set
.Put(syncer::AUTOFILL
);
131 model_set
.Put(syncer::THEMES
);
132 model_set
.Put(syncer::EXTENSIONS
);
133 model_set
.Put(syncer::NIGORI
);
134 model_set
.Put(syncer::SEARCH_ENGINES
);
135 model_set
.Put(syncer::APPS
);
136 model_set
.Put(syncer::APP_LIST
);
137 model_set
.Put(syncer::TYPED_URLS
);
138 model_set
.Put(syncer::SESSIONS
);
139 model_set
.Put(syncer::ARTICLES
);
140 registry
->RegisterListPref(prefs::kSyncAcknowledgedSyncTypes
,
141 syncer::ModelTypeSetToValue(model_set
),
142 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
144 registry
->RegisterIntegerPref(
145 prefs::kSyncRemainingRollbackTries
, 0,
146 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
149 void SyncPrefs::AddSyncPrefObserver(SyncPrefObserver
* sync_pref_observer
) {
150 DCHECK(CalledOnValidThread());
151 sync_pref_observers_
.AddObserver(sync_pref_observer
);
154 void SyncPrefs::RemoveSyncPrefObserver(SyncPrefObserver
* sync_pref_observer
) {
155 DCHECK(CalledOnValidThread());
156 sync_pref_observers_
.RemoveObserver(sync_pref_observer
);
159 void SyncPrefs::ClearPreferences() {
160 DCHECK(CalledOnValidThread());
161 pref_service_
->ClearPref(prefs::kSyncLastSyncedTime
);
162 pref_service_
->ClearPref(prefs::kSyncHasSetupCompleted
);
163 pref_service_
->ClearPref(prefs::kSyncEncryptionBootstrapToken
);
164 pref_service_
->ClearPref(prefs::kSyncKeystoreEncryptionBootstrapToken
);
166 // TODO(nick): The current behavior does not clear
167 // e.g. prefs::kSyncBookmarks. Is that really what we want?
170 bool SyncPrefs::HasSyncSetupCompleted() const {
171 DCHECK(CalledOnValidThread());
172 return pref_service_
->GetBoolean(prefs::kSyncHasSetupCompleted
);
175 void SyncPrefs::SetSyncSetupCompleted() {
176 DCHECK(CalledOnValidThread());
177 pref_service_
->SetBoolean(prefs::kSyncHasSetupCompleted
, true);
178 SetStartSuppressed(false);
181 bool SyncPrefs::SyncHasAuthError() const {
182 DCHECK(CalledOnValidThread());
183 return pref_service_
->GetBoolean(prefs::kSyncHasAuthError
);
186 void SyncPrefs::SetSyncAuthError(bool error
) {
187 DCHECK(CalledOnValidThread());
188 pref_service_
->SetBoolean(prefs::kSyncHasAuthError
, error
);
191 bool SyncPrefs::IsStartSuppressed() const {
192 DCHECK(CalledOnValidThread());
193 return pref_service_
->GetBoolean(prefs::kSyncSuppressStart
);
196 void SyncPrefs::SetStartSuppressed(bool is_suppressed
) {
197 DCHECK(CalledOnValidThread());
198 pref_service_
->SetBoolean(prefs::kSyncSuppressStart
, is_suppressed
);
201 base::Time
SyncPrefs::GetLastSyncedTime() const {
202 DCHECK(CalledOnValidThread());
203 return base::Time::FromInternalValue(
204 pref_service_
->GetInt64(prefs::kSyncLastSyncedTime
));
207 void SyncPrefs::SetLastSyncedTime(base::Time time
) {
208 DCHECK(CalledOnValidThread());
209 pref_service_
->SetInt64(prefs::kSyncLastSyncedTime
, time
.ToInternalValue());
212 bool SyncPrefs::HasKeepEverythingSynced() const {
213 DCHECK(CalledOnValidThread());
214 return pref_service_
->GetBoolean(prefs::kSyncKeepEverythingSynced
);
217 void SyncPrefs::SetKeepEverythingSynced(bool keep_everything_synced
) {
218 DCHECK(CalledOnValidThread());
219 pref_service_
->SetBoolean(prefs::kSyncKeepEverythingSynced
,
220 keep_everything_synced
);
223 syncer::ModelTypeSet
SyncPrefs::GetPreferredDataTypes(
224 syncer::ModelTypeSet registered_types
) const {
225 DCHECK(CalledOnValidThread());
227 if (pref_service_
->GetBoolean(prefs::kSyncKeepEverythingSynced
)) {
228 return registered_types
;
231 syncer::ModelTypeSet preferred_types
;
232 for (syncer::ModelTypeSet::Iterator it
= registered_types
.First(); it
.Good();
234 if (GetDataTypePreferred(it
.Get())) {
235 preferred_types
.Put(it
.Get());
238 return ResolvePrefGroups(registered_types
, preferred_types
);
241 void SyncPrefs::SetPreferredDataTypes(syncer::ModelTypeSet registered_types
,
242 syncer::ModelTypeSet preferred_types
) {
243 DCHECK(CalledOnValidThread());
244 DCHECK(registered_types
.HasAll(preferred_types
));
245 preferred_types
= ResolvePrefGroups(registered_types
, preferred_types
);
246 for (syncer::ModelTypeSet::Iterator i
= registered_types
.First(); i
.Good();
248 SetDataTypePreferred(i
.Get(), preferred_types
.Has(i
.Get()));
252 bool SyncPrefs::IsManaged() const {
253 DCHECK(CalledOnValidThread());
254 return pref_service_
->GetBoolean(prefs::kSyncManaged
);
257 std::string
SyncPrefs::GetEncryptionBootstrapToken() const {
258 DCHECK(CalledOnValidThread());
259 return pref_service_
->GetString(prefs::kSyncEncryptionBootstrapToken
);
262 void SyncPrefs::SetEncryptionBootstrapToken(const std::string
& token
) {
263 DCHECK(CalledOnValidThread());
264 pref_service_
->SetString(prefs::kSyncEncryptionBootstrapToken
, token
);
267 std::string
SyncPrefs::GetKeystoreEncryptionBootstrapToken() const {
268 DCHECK(CalledOnValidThread());
269 return pref_service_
->GetString(prefs::kSyncKeystoreEncryptionBootstrapToken
);
272 void SyncPrefs::SetKeystoreEncryptionBootstrapToken(const std::string
& token
) {
273 DCHECK(CalledOnValidThread());
274 pref_service_
->SetString(prefs::kSyncKeystoreEncryptionBootstrapToken
, token
);
277 std::string
SyncPrefs::GetSyncSessionsGUID() const {
278 DCHECK(CalledOnValidThread());
279 return pref_service_
->GetString(prefs::kSyncSessionsGUID
);
282 void SyncPrefs::SetSyncSessionsGUID(const std::string
& guid
) {
283 DCHECK(CalledOnValidThread());
284 pref_service_
->SetString(prefs::kSyncSessionsGUID
, guid
);
288 const char* SyncPrefs::GetPrefNameForDataType(syncer::ModelType data_type
) {
290 case syncer::BOOKMARKS
:
291 return prefs::kSyncBookmarks
;
292 case syncer::PASSWORDS
:
293 return prefs::kSyncPasswords
;
294 case syncer::PREFERENCES
:
295 return prefs::kSyncPreferences
;
296 case syncer::AUTOFILL
:
297 return prefs::kSyncAutofill
;
298 case syncer::AUTOFILL_PROFILE
:
299 return prefs::kSyncAutofillProfile
;
301 return prefs::kSyncThemes
;
302 case syncer::TYPED_URLS
:
303 return prefs::kSyncTypedUrls
;
304 case syncer::EXTENSION_SETTINGS
:
305 return prefs::kSyncExtensionSettings
;
306 case syncer::EXTENSIONS
:
307 return prefs::kSyncExtensions
;
308 case syncer::APP_LIST
:
309 return prefs::kSyncAppList
;
310 case syncer::APP_SETTINGS
:
311 return prefs::kSyncAppSettings
;
313 return prefs::kSyncApps
;
314 case syncer::SEARCH_ENGINES
:
315 return prefs::kSyncSearchEngines
;
316 case syncer::SESSIONS
:
317 return prefs::kSyncSessions
;
318 case syncer::APP_NOTIFICATIONS
:
319 return prefs::kSyncAppNotifications
;
320 case syncer::HISTORY_DELETE_DIRECTIVES
:
321 return prefs::kSyncHistoryDeleteDirectives
;
322 case syncer::SYNCED_NOTIFICATIONS
:
323 return prefs::kSyncSyncedNotifications
;
324 case syncer::SYNCED_NOTIFICATION_APP_INFO
:
325 return prefs::kSyncSyncedNotificationAppInfo
;
326 case syncer::DICTIONARY
:
327 return prefs::kSyncDictionary
;
328 case syncer::FAVICON_IMAGES
:
329 return prefs::kSyncFaviconImages
;
330 case syncer::FAVICON_TRACKING
:
331 return prefs::kSyncFaviconTracking
;
332 case syncer::SUPERVISED_USER_SETTINGS
:
333 return prefs::kSyncSupervisedUserSettings
;
334 case syncer::PROXY_TABS
:
335 return prefs::kSyncTabs
;
336 case syncer::PRIORITY_PREFERENCES
:
337 return prefs::kSyncPriorityPreferences
;
338 case syncer::SUPERVISED_USERS
:
339 return prefs::kSyncSupervisedUsers
;
340 case syncer::ARTICLES
:
341 return prefs::kSyncArticles
;
342 case syncer::SUPERVISED_USER_SHARED_SETTINGS
:
343 return prefs::kSyncSupervisedUserSharedSettings
;
351 #if defined(OS_CHROMEOS)
352 std::string
SyncPrefs::GetSpareBootstrapToken() const {
353 DCHECK(CalledOnValidThread());
354 return pref_service_
->GetString(prefs::kSyncSpareBootstrapToken
);
357 void SyncPrefs::SetSpareBootstrapToken(const std::string
& token
) {
358 DCHECK(CalledOnValidThread());
359 pref_service_
->SetString(prefs::kSyncSpareBootstrapToken
, token
);
363 void SyncPrefs::AcknowledgeSyncedTypes(syncer::ModelTypeSet types
) {
364 DCHECK(CalledOnValidThread());
365 // Add the types to the current set of acknowledged
366 // types, and then store the resulting set in prefs.
367 const syncer::ModelTypeSet acknowledged_types
=
369 syncer::ModelTypeSetFromValue(
370 *pref_service_
->GetList(prefs::kSyncAcknowledgedSyncTypes
)));
372 scoped_ptr
<base::ListValue
> value(
373 syncer::ModelTypeSetToValue(acknowledged_types
));
374 pref_service_
->Set(prefs::kSyncAcknowledgedSyncTypes
, *value
);
377 int SyncPrefs::GetRemainingRollbackTries() const {
378 return pref_service_
->GetInteger(prefs::kSyncRemainingRollbackTries
);
381 void SyncPrefs::SetRemainingRollbackTries(int times
) {
382 pref_service_
->SetInteger(prefs::kSyncRemainingRollbackTries
, times
);
385 void SyncPrefs::OnSyncManagedPrefChanged() {
386 DCHECK(CalledOnValidThread());
387 FOR_EACH_OBSERVER(SyncPrefObserver
,
388 sync_pref_observers_
,
389 OnSyncManagedPrefChange(*pref_sync_managed_
));
392 void SyncPrefs::SetManagedForTest(bool is_managed
) {
393 DCHECK(CalledOnValidThread());
394 pref_service_
->SetBoolean(prefs::kSyncManaged
, is_managed
);
397 syncer::ModelTypeSet
SyncPrefs::GetAcknowledgeSyncedTypesForTest() const {
398 DCHECK(CalledOnValidThread());
399 return syncer::ModelTypeSetFromValue(
400 *pref_service_
->GetList(prefs::kSyncAcknowledgedSyncTypes
));
403 void SyncPrefs::RegisterPrefGroups() {
404 pref_groups_
[syncer::APPS
].Put(syncer::APP_NOTIFICATIONS
);
405 pref_groups_
[syncer::APPS
].Put(syncer::APP_SETTINGS
);
406 pref_groups_
[syncer::APPS
].Put(syncer::APP_LIST
);
408 pref_groups_
[syncer::AUTOFILL
].Put(syncer::AUTOFILL_PROFILE
);
410 pref_groups_
[syncer::EXTENSIONS
].Put(syncer::EXTENSION_SETTINGS
);
412 pref_groups_
[syncer::PREFERENCES
].Put(syncer::DICTIONARY
);
413 pref_groups_
[syncer::PREFERENCES
].Put(syncer::PRIORITY_PREFERENCES
);
414 pref_groups_
[syncer::PREFERENCES
].Put(syncer::SEARCH_ENGINES
);
416 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::HISTORY_DELETE_DIRECTIVES
);
417 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::SESSIONS
);
418 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::FAVICON_IMAGES
);
419 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::FAVICON_TRACKING
);
421 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::SESSIONS
);
422 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::FAVICON_IMAGES
);
423 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::FAVICON_TRACKING
);
425 // TODO(zea): put favicons in the bookmarks group as well once it handles
430 void SyncPrefs::RegisterDataTypePreferredPref(
431 user_prefs::PrefRegistrySyncable
* registry
,
432 syncer::ModelType type
,
434 const char* pref_name
= GetPrefNameForDataType(type
);
439 registry
->RegisterBooleanPref(
442 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
445 bool SyncPrefs::GetDataTypePreferred(syncer::ModelType type
) const {
446 DCHECK(CalledOnValidThread());
447 const char* pref_name
= GetPrefNameForDataType(type
);
452 if (type
== syncer::PROXY_TABS
&&
453 pref_service_
->GetUserPrefValue(pref_name
) == NULL
&&
454 pref_service_
->IsUserModifiablePreference(pref_name
)) {
455 // If there is no tab sync preference yet (i.e. newly enabled type),
456 // default to the session sync preference value.
457 pref_name
= GetPrefNameForDataType(syncer::SESSIONS
);
460 return pref_service_
->GetBoolean(pref_name
);
463 void SyncPrefs::SetDataTypePreferred(syncer::ModelType type
,
465 DCHECK(CalledOnValidThread());
466 const char* pref_name
= GetPrefNameForDataType(type
);
471 pref_service_
->SetBoolean(pref_name
, is_preferred
);
474 syncer::ModelTypeSet
SyncPrefs::ResolvePrefGroups(
475 syncer::ModelTypeSet registered_types
,
476 syncer::ModelTypeSet types
) const {
477 DCHECK(registered_types
.HasAll(types
));
478 syncer::ModelTypeSet types_with_groups
= types
;
479 for (PrefGroupsMap::const_iterator i
= pref_groups_
.begin();
480 i
!= pref_groups_
.end();
482 if (types
.Has(i
->first
))
483 types_with_groups
.PutAll(i
->second
);
485 types_with_groups
.RetainAll(registered_types
);
486 return types_with_groups
;
489 base::Time
SyncPrefs::GetFirstSyncTime() const {
490 return base::Time::FromInternalValue(
491 pref_service_
->GetInt64(prefs::kSyncFirstSyncTime
));
494 void SyncPrefs::SetFirstSyncTime(base::Time time
) {
495 pref_service_
->SetInt64(prefs::kSyncFirstSyncTime
, time
.ToInternalValue());
498 void SyncPrefs::ClearFirstSyncTime() {
499 pref_service_
->ClearPref(prefs::kSyncFirstSyncTime
);
502 } // namespace sync_driver