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/command_line.h"
8 #include "base/logging.h"
9 #include "base/prefs/pref_member.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "build/build_config.h"
14 #include "components/pref_registry/pref_registry_syncable.h"
15 #include "components/sync_driver/pref_names.h"
17 namespace sync_driver
{
19 SyncPrefObserver::~SyncPrefObserver() {}
21 SyncPrefs::SyncPrefs(PrefService
* pref_service
) : pref_service_(pref_service
) {
24 // Watch the preference that indicates sync is managed so we can take
25 // appropriate action.
26 pref_sync_managed_
.Init(
29 base::Bind(&SyncPrefs::OnSyncManagedPrefChanged
, base::Unretained(this)));
32 SyncPrefs::SyncPrefs() : pref_service_(NULL
) {}
34 SyncPrefs::~SyncPrefs() { DCHECK(CalledOnValidThread()); }
37 void SyncPrefs::RegisterProfilePrefs(
38 user_prefs::PrefRegistrySyncable
* registry
) {
39 registry
->RegisterBooleanPref(
40 prefs::kSyncHasSetupCompleted
,
42 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
43 registry
->RegisterBooleanPref(
44 prefs::kSyncSuppressStart
,
46 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
47 registry
->RegisterInt64Pref(
48 prefs::kSyncLastSyncedTime
,
50 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
52 // All datatypes are on by default, but this gets set explicitly
53 // when you configure sync (when turning it on), in
54 // ProfileSyncService::OnUserChoseDatatypes.
55 registry
->RegisterBooleanPref(
56 prefs::kSyncKeepEverythingSynced
,
58 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
60 syncer::ModelTypeSet user_types
= syncer::UserTypes();
62 // Include proxy types as well, as they can be individually selected,
63 // although they don't have sync representations.
64 user_types
.PutAll(syncer::ProxyTypes());
66 // Treat bookmarks specially.
67 RegisterDataTypePreferredPref(registry
, syncer::BOOKMARKS
, true);
68 user_types
.Remove(syncer::BOOKMARKS
);
70 // These two prefs are set from sync experiment to enable enhanced bookmarks.
71 registry
->RegisterIntegerPref(
72 prefs::kEnhancedBookmarksExperimentEnabled
,
74 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
76 registry
->RegisterStringPref(
77 prefs::kEnhancedBookmarksExtensionId
,
79 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
81 // All types are set to off by default, which forces a configuration to
82 // explicitly enable them. GetPreferredTypes() will ensure that any new
83 // implicit types are enabled when their pref group is, or via
84 // KeepEverythingSynced.
85 for (syncer::ModelTypeSet::Iterator it
= user_types
.First(); it
.Good();
87 RegisterDataTypePreferredPref(registry
, it
.Get(), false);
90 registry
->RegisterBooleanPref(
93 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
94 registry
->RegisterStringPref(
95 prefs::kSyncEncryptionBootstrapToken
,
97 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
98 registry
->RegisterStringPref(
99 prefs::kSyncKeystoreEncryptionBootstrapToken
,
101 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
102 #if defined(OS_CHROMEOS)
103 registry
->RegisterStringPref(
104 prefs::kSyncSpareBootstrapToken
,
106 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
109 registry
->RegisterBooleanPref(
110 prefs::kSyncHasAuthError
,
112 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
114 registry
->RegisterStringPref(
115 prefs::kSyncSessionsGUID
,
117 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
119 // We will start prompting people about new data types after the launch of
120 // SESSIONS - all previously launched data types are treated as if they are
121 // already acknowledged.
122 syncer::ModelTypeSet model_set
;
123 model_set
.Put(syncer::BOOKMARKS
);
124 model_set
.Put(syncer::PREFERENCES
);
125 model_set
.Put(syncer::PASSWORDS
);
126 model_set
.Put(syncer::AUTOFILL_PROFILE
);
127 model_set
.Put(syncer::AUTOFILL
);
128 model_set
.Put(syncer::THEMES
);
129 model_set
.Put(syncer::EXTENSIONS
);
130 model_set
.Put(syncer::NIGORI
);
131 model_set
.Put(syncer::SEARCH_ENGINES
);
132 model_set
.Put(syncer::APPS
);
133 model_set
.Put(syncer::APP_LIST
);
134 model_set
.Put(syncer::TYPED_URLS
);
135 model_set
.Put(syncer::SESSIONS
);
136 model_set
.Put(syncer::ARTICLES
);
137 registry
->RegisterListPref(prefs::kSyncAcknowledgedSyncTypes
,
138 syncer::ModelTypeSetToValue(model_set
),
139 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
141 registry
->RegisterIntegerPref(
142 prefs::kSyncRemainingRollbackTries
, 0,
143 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
146 void SyncPrefs::AddSyncPrefObserver(SyncPrefObserver
* sync_pref_observer
) {
147 DCHECK(CalledOnValidThread());
148 sync_pref_observers_
.AddObserver(sync_pref_observer
);
151 void SyncPrefs::RemoveSyncPrefObserver(SyncPrefObserver
* sync_pref_observer
) {
152 DCHECK(CalledOnValidThread());
153 sync_pref_observers_
.RemoveObserver(sync_pref_observer
);
156 void SyncPrefs::ClearPreferences() {
157 DCHECK(CalledOnValidThread());
158 pref_service_
->ClearPref(prefs::kSyncLastSyncedTime
);
159 pref_service_
->ClearPref(prefs::kSyncHasSetupCompleted
);
160 pref_service_
->ClearPref(prefs::kSyncEncryptionBootstrapToken
);
161 pref_service_
->ClearPref(prefs::kSyncKeystoreEncryptionBootstrapToken
);
163 // TODO(nick): The current behavior does not clear
164 // e.g. prefs::kSyncBookmarks. Is that really what we want?
167 bool SyncPrefs::HasSyncSetupCompleted() const {
168 DCHECK(CalledOnValidThread());
169 return pref_service_
->GetBoolean(prefs::kSyncHasSetupCompleted
);
172 void SyncPrefs::SetSyncSetupCompleted() {
173 DCHECK(CalledOnValidThread());
174 pref_service_
->SetBoolean(prefs::kSyncHasSetupCompleted
, true);
175 SetStartSuppressed(false);
178 bool SyncPrefs::SyncHasAuthError() const {
179 DCHECK(CalledOnValidThread());
180 return pref_service_
->GetBoolean(prefs::kSyncHasAuthError
);
183 void SyncPrefs::SetSyncAuthError(bool error
) {
184 DCHECK(CalledOnValidThread());
185 pref_service_
->SetBoolean(prefs::kSyncHasAuthError
, error
);
188 bool SyncPrefs::IsStartSuppressed() const {
189 DCHECK(CalledOnValidThread());
190 return pref_service_
->GetBoolean(prefs::kSyncSuppressStart
);
193 void SyncPrefs::SetStartSuppressed(bool is_suppressed
) {
194 DCHECK(CalledOnValidThread());
195 pref_service_
->SetBoolean(prefs::kSyncSuppressStart
, is_suppressed
);
198 base::Time
SyncPrefs::GetLastSyncedTime() const {
199 DCHECK(CalledOnValidThread());
200 return base::Time::FromInternalValue(
201 pref_service_
->GetInt64(prefs::kSyncLastSyncedTime
));
204 void SyncPrefs::SetLastSyncedTime(base::Time time
) {
205 DCHECK(CalledOnValidThread());
206 pref_service_
->SetInt64(prefs::kSyncLastSyncedTime
, time
.ToInternalValue());
209 bool SyncPrefs::HasKeepEverythingSynced() const {
210 DCHECK(CalledOnValidThread());
211 return pref_service_
->GetBoolean(prefs::kSyncKeepEverythingSynced
);
214 void SyncPrefs::SetKeepEverythingSynced(bool keep_everything_synced
) {
215 DCHECK(CalledOnValidThread());
216 pref_service_
->SetBoolean(prefs::kSyncKeepEverythingSynced
,
217 keep_everything_synced
);
220 syncer::ModelTypeSet
SyncPrefs::GetPreferredDataTypes(
221 syncer::ModelTypeSet registered_types
) const {
222 DCHECK(CalledOnValidThread());
224 if (pref_service_
->GetBoolean(prefs::kSyncKeepEverythingSynced
)) {
225 return registered_types
;
228 syncer::ModelTypeSet preferred_types
;
229 for (syncer::ModelTypeSet::Iterator it
= registered_types
.First(); it
.Good();
231 if (GetDataTypePreferred(it
.Get())) {
232 preferred_types
.Put(it
.Get());
235 return ResolvePrefGroups(registered_types
, preferred_types
);
238 void SyncPrefs::SetPreferredDataTypes(syncer::ModelTypeSet registered_types
,
239 syncer::ModelTypeSet preferred_types
) {
240 DCHECK(CalledOnValidThread());
241 DCHECK(registered_types
.HasAll(preferred_types
));
242 preferred_types
= ResolvePrefGroups(registered_types
, preferred_types
);
243 for (syncer::ModelTypeSet::Iterator i
= registered_types
.First(); i
.Good();
245 SetDataTypePreferred(i
.Get(), preferred_types
.Has(i
.Get()));
249 bool SyncPrefs::IsManaged() const {
250 DCHECK(CalledOnValidThread());
251 return pref_service_
->GetBoolean(prefs::kSyncManaged
);
254 std::string
SyncPrefs::GetEncryptionBootstrapToken() const {
255 DCHECK(CalledOnValidThread());
256 return pref_service_
->GetString(prefs::kSyncEncryptionBootstrapToken
);
259 void SyncPrefs::SetEncryptionBootstrapToken(const std::string
& token
) {
260 DCHECK(CalledOnValidThread());
261 pref_service_
->SetString(prefs::kSyncEncryptionBootstrapToken
, token
);
264 std::string
SyncPrefs::GetKeystoreEncryptionBootstrapToken() const {
265 DCHECK(CalledOnValidThread());
266 return pref_service_
->GetString(prefs::kSyncKeystoreEncryptionBootstrapToken
);
269 void SyncPrefs::SetKeystoreEncryptionBootstrapToken(const std::string
& token
) {
270 DCHECK(CalledOnValidThread());
271 pref_service_
->SetString(prefs::kSyncKeystoreEncryptionBootstrapToken
, token
);
274 std::string
SyncPrefs::GetSyncSessionsGUID() const {
275 DCHECK(CalledOnValidThread());
276 return pref_service_
->GetString(prefs::kSyncSessionsGUID
);
279 void SyncPrefs::SetSyncSessionsGUID(const std::string
& guid
) {
280 DCHECK(CalledOnValidThread());
281 pref_service_
->SetString(prefs::kSyncSessionsGUID
, guid
);
285 const char* SyncPrefs::GetPrefNameForDataType(syncer::ModelType data_type
) {
287 case syncer::BOOKMARKS
:
288 return prefs::kSyncBookmarks
;
289 case syncer::PASSWORDS
:
290 return prefs::kSyncPasswords
;
291 case syncer::PREFERENCES
:
292 return prefs::kSyncPreferences
;
293 case syncer::AUTOFILL
:
294 return prefs::kSyncAutofill
;
295 case syncer::AUTOFILL_PROFILE
:
296 return prefs::kSyncAutofillProfile
;
298 return prefs::kSyncThemes
;
299 case syncer::TYPED_URLS
:
300 return prefs::kSyncTypedUrls
;
301 case syncer::EXTENSION_SETTINGS
:
302 return prefs::kSyncExtensionSettings
;
303 case syncer::EXTENSIONS
:
304 return prefs::kSyncExtensions
;
305 case syncer::APP_LIST
:
306 return prefs::kSyncAppList
;
307 case syncer::APP_SETTINGS
:
308 return prefs::kSyncAppSettings
;
310 return prefs::kSyncApps
;
311 case syncer::SEARCH_ENGINES
:
312 return prefs::kSyncSearchEngines
;
313 case syncer::SESSIONS
:
314 return prefs::kSyncSessions
;
315 case syncer::APP_NOTIFICATIONS
:
316 return prefs::kSyncAppNotifications
;
317 case syncer::HISTORY_DELETE_DIRECTIVES
:
318 return prefs::kSyncHistoryDeleteDirectives
;
319 case syncer::SYNCED_NOTIFICATIONS
:
320 return prefs::kSyncSyncedNotifications
;
321 case syncer::SYNCED_NOTIFICATION_APP_INFO
:
322 return prefs::kSyncSyncedNotificationAppInfo
;
323 case syncer::DICTIONARY
:
324 return prefs::kSyncDictionary
;
325 case syncer::FAVICON_IMAGES
:
326 return prefs::kSyncFaviconImages
;
327 case syncer::FAVICON_TRACKING
:
328 return prefs::kSyncFaviconTracking
;
329 case syncer::MANAGED_USER_SETTINGS
:
330 return prefs::kSyncManagedUserSettings
;
331 case syncer::PROXY_TABS
:
332 return prefs::kSyncTabs
;
333 case syncer::PRIORITY_PREFERENCES
:
334 return prefs::kSyncPriorityPreferences
;
335 case syncer::MANAGED_USERS
:
336 return prefs::kSyncManagedUsers
;
337 case syncer::ARTICLES
:
338 return prefs::kSyncArticles
;
339 case syncer::MANAGED_USER_SHARED_SETTINGS
:
340 return prefs::kSyncManagedUserSharedSettings
;
348 #if defined(OS_CHROMEOS)
349 std::string
SyncPrefs::GetSpareBootstrapToken() const {
350 DCHECK(CalledOnValidThread());
351 return pref_service_
->GetString(prefs::kSyncSpareBootstrapToken
);
354 void SyncPrefs::SetSpareBootstrapToken(const std::string
& token
) {
355 DCHECK(CalledOnValidThread());
356 pref_service_
->SetString(prefs::kSyncSpareBootstrapToken
, token
);
360 void SyncPrefs::AcknowledgeSyncedTypes(syncer::ModelTypeSet types
) {
361 DCHECK(CalledOnValidThread());
362 // Add the types to the current set of acknowledged
363 // types, and then store the resulting set in prefs.
364 const syncer::ModelTypeSet acknowledged_types
=
366 syncer::ModelTypeSetFromValue(
367 *pref_service_
->GetList(prefs::kSyncAcknowledgedSyncTypes
)));
369 scoped_ptr
<base::ListValue
> value(
370 syncer::ModelTypeSetToValue(acknowledged_types
));
371 pref_service_
->Set(prefs::kSyncAcknowledgedSyncTypes
, *value
);
374 int SyncPrefs::GetRemainingRollbackTries() const {
375 return pref_service_
->GetInteger(prefs::kSyncRemainingRollbackTries
);
378 void SyncPrefs::SetRemainingRollbackTries(int times
) {
379 pref_service_
->SetInteger(prefs::kSyncRemainingRollbackTries
, times
);
382 void SyncPrefs::OnSyncManagedPrefChanged() {
383 DCHECK(CalledOnValidThread());
384 FOR_EACH_OBSERVER(SyncPrefObserver
,
385 sync_pref_observers_
,
386 OnSyncManagedPrefChange(*pref_sync_managed_
));
389 void SyncPrefs::SetManagedForTest(bool is_managed
) {
390 DCHECK(CalledOnValidThread());
391 pref_service_
->SetBoolean(prefs::kSyncManaged
, is_managed
);
394 syncer::ModelTypeSet
SyncPrefs::GetAcknowledgeSyncedTypesForTest() const {
395 DCHECK(CalledOnValidThread());
396 return syncer::ModelTypeSetFromValue(
397 *pref_service_
->GetList(prefs::kSyncAcknowledgedSyncTypes
));
400 void SyncPrefs::RegisterPrefGroups() {
401 pref_groups_
[syncer::APPS
].Put(syncer::APP_NOTIFICATIONS
);
402 pref_groups_
[syncer::APPS
].Put(syncer::APP_SETTINGS
);
403 pref_groups_
[syncer::APPS
].Put(syncer::APP_LIST
);
405 pref_groups_
[syncer::AUTOFILL
].Put(syncer::AUTOFILL_PROFILE
);
407 pref_groups_
[syncer::EXTENSIONS
].Put(syncer::EXTENSION_SETTINGS
);
409 pref_groups_
[syncer::PREFERENCES
].Put(syncer::DICTIONARY
);
410 pref_groups_
[syncer::PREFERENCES
].Put(syncer::PRIORITY_PREFERENCES
);
411 pref_groups_
[syncer::PREFERENCES
].Put(syncer::SEARCH_ENGINES
);
413 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::HISTORY_DELETE_DIRECTIVES
);
414 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::SESSIONS
);
415 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::FAVICON_IMAGES
);
416 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::FAVICON_TRACKING
);
418 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::SESSIONS
);
419 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::FAVICON_IMAGES
);
420 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::FAVICON_TRACKING
);
422 pref_groups_
[syncer::MANAGED_USER_SETTINGS
].Put(syncer::SESSIONS
);
424 // TODO(zea): put favicons in the bookmarks group as well once it handles
429 void SyncPrefs::RegisterDataTypePreferredPref(
430 user_prefs::PrefRegistrySyncable
* registry
,
431 syncer::ModelType type
,
433 const char* pref_name
= GetPrefNameForDataType(type
);
438 registry
->RegisterBooleanPref(
441 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
444 bool SyncPrefs::GetDataTypePreferred(syncer::ModelType type
) const {
445 DCHECK(CalledOnValidThread());
446 const char* pref_name
= GetPrefNameForDataType(type
);
451 if (type
== syncer::PROXY_TABS
&&
452 pref_service_
->GetUserPrefValue(pref_name
) == NULL
&&
453 pref_service_
->IsUserModifiablePreference(pref_name
)) {
454 // If there is no tab sync preference yet (i.e. newly enabled type),
455 // default to the session sync preference value.
456 pref_name
= GetPrefNameForDataType(syncer::SESSIONS
);
459 return pref_service_
->GetBoolean(pref_name
);
462 void SyncPrefs::SetDataTypePreferred(syncer::ModelType type
,
464 DCHECK(CalledOnValidThread());
465 const char* pref_name
= GetPrefNameForDataType(type
);
470 pref_service_
->SetBoolean(pref_name
, is_preferred
);
473 syncer::ModelTypeSet
SyncPrefs::ResolvePrefGroups(
474 syncer::ModelTypeSet registered_types
,
475 syncer::ModelTypeSet types
) const {
476 DCHECK(registered_types
.HasAll(types
));
477 syncer::ModelTypeSet types_with_groups
= types
;
478 for (PrefGroupsMap::const_iterator i
= pref_groups_
.begin();
479 i
!= pref_groups_
.end();
481 if (types
.Has(i
->first
))
482 types_with_groups
.PutAll(i
->second
);
484 types_with_groups
.RetainAll(registered_types
);
485 return types_with_groups
;
488 } // namespace browser_sync