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 "chrome/browser/sync/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 "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/profiles/profile_io_data.h"
16 #include "chrome/browser/sync/profile_sync_service.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/pref_names.h"
19 #include "components/user_prefs/pref_registry_syncable.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_source.h"
24 namespace browser_sync
{
26 SyncPrefObserver::~SyncPrefObserver() {}
28 SyncPrefs::SyncPrefs(PrefService
* pref_service
)
29 : pref_service_(pref_service
) {
32 // Watch the preference that indicates sync is managed so we can take
33 // appropriate action.
34 pref_sync_managed_
.Init(prefs::kSyncManaged
, pref_service_
,
35 base::Bind(&SyncPrefs::OnSyncManagedPrefChanged
,
36 base::Unretained(this)));
39 SyncPrefs::~SyncPrefs() {
40 DCHECK(CalledOnValidThread());
44 void SyncPrefs::RegisterProfilePrefs(
45 user_prefs::PrefRegistrySyncable
* registry
) {
46 registry
->RegisterBooleanPref(
47 prefs::kSyncHasSetupCompleted
,
49 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
50 registry
->RegisterBooleanPref(
51 prefs::kSyncSuppressStart
,
53 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
54 registry
->RegisterInt64Pref(
55 prefs::kSyncLastSyncedTime
,
57 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
59 // All datatypes are on by default, but this gets set explicitly
60 // when you configure sync (when turning it on), in
61 // ProfileSyncService::OnUserChoseDatatypes.
62 registry
->RegisterBooleanPref(
63 prefs::kSyncKeepEverythingSynced
,
65 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
67 syncer::ModelTypeSet user_types
= syncer::UserTypes();
69 // Include proxy types as well, as they can be individually selected,
70 // although they don't have sync representations.
71 user_types
.PutAll(syncer::ProxyTypes());
73 // Treat bookmarks specially.
74 RegisterDataTypePreferredPref(registry
, syncer::BOOKMARKS
, true);
75 user_types
.Remove(syncer::BOOKMARKS
);
77 // All types are set to off by default, which forces a configuration to
78 // explicitly enable them. GetPreferredTypes() will ensure that any new
79 // implicit types are enabled when their pref group is, or via
80 // KeepEverythingSynced.
81 for (syncer::ModelTypeSet::Iterator it
= user_types
.First();
82 it
.Good(); it
.Inc()) {
83 RegisterDataTypePreferredPref(registry
, it
.Get(), false);
86 registry
->RegisterBooleanPref(
89 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
90 registry
->RegisterStringPref(
91 prefs::kSyncEncryptionBootstrapToken
,
93 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
94 registry
->RegisterStringPref(
95 prefs::kSyncKeystoreEncryptionBootstrapToken
,
97 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
98 #if defined(OS_CHROMEOS)
99 registry
->RegisterStringPref(
100 prefs::kSyncSpareBootstrapToken
,
102 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
105 registry
->RegisterBooleanPref(
106 prefs::kSyncHasAuthError
,
108 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
110 registry
->RegisterStringPref(
111 prefs::kSyncSessionsGUID
,
113 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
115 // We will start prompting people about new data types after the launch of
116 // SESSIONS - all previously launched data types are treated as if they are
117 // already acknowledged.
118 syncer::ModelTypeSet model_set
;
119 model_set
.Put(syncer::BOOKMARKS
);
120 model_set
.Put(syncer::PREFERENCES
);
121 model_set
.Put(syncer::PASSWORDS
);
122 model_set
.Put(syncer::AUTOFILL_PROFILE
);
123 model_set
.Put(syncer::AUTOFILL
);
124 model_set
.Put(syncer::THEMES
);
125 model_set
.Put(syncer::EXTENSIONS
);
126 model_set
.Put(syncer::NIGORI
);
127 model_set
.Put(syncer::SEARCH_ENGINES
);
128 model_set
.Put(syncer::APPS
);
129 model_set
.Put(syncer::APP_LIST
);
130 model_set
.Put(syncer::TYPED_URLS
);
131 model_set
.Put(syncer::SESSIONS
);
132 model_set
.Put(syncer::ARTICLES
);
133 registry
->RegisterListPref(prefs::kSyncAcknowledgedSyncTypes
,
134 syncer::ModelTypeSetToValue(model_set
),
135 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
138 void SyncPrefs::AddSyncPrefObserver(SyncPrefObserver
* sync_pref_observer
) {
139 DCHECK(CalledOnValidThread());
140 sync_pref_observers_
.AddObserver(sync_pref_observer
);
143 void SyncPrefs::RemoveSyncPrefObserver(SyncPrefObserver
* sync_pref_observer
) {
144 DCHECK(CalledOnValidThread());
145 sync_pref_observers_
.RemoveObserver(sync_pref_observer
);
148 void SyncPrefs::ClearPreferences() {
149 DCHECK(CalledOnValidThread());
150 pref_service_
->ClearPref(prefs::kSyncLastSyncedTime
);
151 pref_service_
->ClearPref(prefs::kSyncHasSetupCompleted
);
152 pref_service_
->ClearPref(prefs::kSyncEncryptionBootstrapToken
);
153 pref_service_
->ClearPref(prefs::kSyncKeystoreEncryptionBootstrapToken
);
155 // TODO(nick): The current behavior does not clear
156 // e.g. prefs::kSyncBookmarks. Is that really what we want?
159 bool SyncPrefs::HasSyncSetupCompleted() const {
160 DCHECK(CalledOnValidThread());
161 return pref_service_
->GetBoolean(prefs::kSyncHasSetupCompleted
);
164 void SyncPrefs::SetSyncSetupCompleted() {
165 DCHECK(CalledOnValidThread());
166 pref_service_
->SetBoolean(prefs::kSyncHasSetupCompleted
, true);
167 SetStartSuppressed(false);
170 bool SyncPrefs::SyncHasAuthError() const {
171 DCHECK(CalledOnValidThread());
172 return pref_service_
->GetBoolean(prefs::kSyncHasAuthError
);
175 void SyncPrefs::SetSyncAuthError(bool error
) {
176 DCHECK(CalledOnValidThread());
177 pref_service_
->SetBoolean(prefs::kSyncHasAuthError
, error
);
180 bool SyncPrefs::IsStartSuppressed() const {
181 DCHECK(CalledOnValidThread());
182 return pref_service_
->GetBoolean(prefs::kSyncSuppressStart
);
185 void SyncPrefs::SetStartSuppressed(bool is_suppressed
) {
186 DCHECK(CalledOnValidThread());
187 pref_service_
->SetBoolean(prefs::kSyncSuppressStart
, is_suppressed
);
190 std::string
SyncPrefs::GetGoogleServicesUsername() const {
191 DCHECK(CalledOnValidThread());
192 return pref_service_
->GetString(prefs::kGoogleServicesUsername
);
195 base::Time
SyncPrefs::GetLastSyncedTime() const {
196 DCHECK(CalledOnValidThread());
198 base::Time::FromInternalValue(
199 pref_service_
->GetInt64(prefs::kSyncLastSyncedTime
));
202 void SyncPrefs::SetLastSyncedTime(base::Time time
) {
203 DCHECK(CalledOnValidThread());
204 pref_service_
->SetInt64(prefs::kSyncLastSyncedTime
, time
.ToInternalValue());
207 bool SyncPrefs::HasKeepEverythingSynced() const {
208 DCHECK(CalledOnValidThread());
209 return pref_service_
->GetBoolean(prefs::kSyncKeepEverythingSynced
);
212 void SyncPrefs::SetKeepEverythingSynced(bool keep_everything_synced
) {
213 DCHECK(CalledOnValidThread());
214 pref_service_
->SetBoolean(prefs::kSyncKeepEverythingSynced
,
215 keep_everything_synced
);
218 syncer::ModelTypeSet
SyncPrefs::GetPreferredDataTypes(
219 syncer::ModelTypeSet registered_types
) const {
220 DCHECK(CalledOnValidThread());
222 // First remove any datatypes that are inconsistent with the current policies
223 // on the client (so that "keep everything synced" doesn't include them).
224 if (pref_service_
->HasPrefPath(prefs::kSavingBrowserHistoryDisabled
) &&
225 pref_service_
->GetBoolean(prefs::kSavingBrowserHistoryDisabled
)) {
226 registered_types
.Remove(syncer::TYPED_URLS
);
229 if (pref_service_
->GetBoolean(prefs::kSyncKeepEverythingSynced
)) {
230 return registered_types
;
233 syncer::ModelTypeSet preferred_types
;
234 for (syncer::ModelTypeSet::Iterator it
= registered_types
.First();
235 it
.Good(); it
.Inc()) {
236 if (GetDataTypePreferred(it
.Get())) {
237 preferred_types
.Put(it
.Get());
240 return ResolvePrefGroups(registered_types
, preferred_types
);
243 void SyncPrefs::SetPreferredDataTypes(
244 syncer::ModelTypeSet registered_types
,
245 syncer::ModelTypeSet preferred_types
) {
246 DCHECK(CalledOnValidThread());
247 DCHECK(registered_types
.HasAll(preferred_types
));
248 preferred_types
= ResolvePrefGroups(registered_types
, preferred_types
);
249 for (syncer::ModelTypeSet::Iterator i
= registered_types
.First();
251 SetDataTypePreferred(i
.Get(), preferred_types
.Has(i
.Get()));
255 bool SyncPrefs::IsManaged() const {
256 DCHECK(CalledOnValidThread());
257 return pref_service_
->GetBoolean(prefs::kSyncManaged
);
260 std::string
SyncPrefs::GetEncryptionBootstrapToken() const {
261 DCHECK(CalledOnValidThread());
262 return pref_service_
->GetString(prefs::kSyncEncryptionBootstrapToken
);
265 void SyncPrefs::SetEncryptionBootstrapToken(const std::string
& token
) {
266 DCHECK(CalledOnValidThread());
267 pref_service_
->SetString(prefs::kSyncEncryptionBootstrapToken
, token
);
270 std::string
SyncPrefs::GetKeystoreEncryptionBootstrapToken() const {
271 DCHECK(CalledOnValidThread());
272 return pref_service_
->GetString(
273 prefs::kSyncKeystoreEncryptionBootstrapToken
);
276 void SyncPrefs::SetKeystoreEncryptionBootstrapToken(const std::string
& token
) {
277 DCHECK(CalledOnValidThread());
278 pref_service_
->SetString(prefs::kSyncKeystoreEncryptionBootstrapToken
, token
);
281 std::string
SyncPrefs::GetSyncSessionsGUID() const {
282 DCHECK(CalledOnValidThread());
283 return pref_service_
->GetString(prefs::kSyncSessionsGUID
);
286 void SyncPrefs::SetSyncSessionsGUID(const std::string
& guid
) {
287 DCHECK(CalledOnValidThread());
288 pref_service_
->SetString(prefs::kSyncSessionsGUID
, guid
);
292 const char* SyncPrefs::GetPrefNameForDataType(syncer::ModelType data_type
) {
294 case syncer::BOOKMARKS
:
295 return prefs::kSyncBookmarks
;
296 case syncer::PASSWORDS
:
297 return prefs::kSyncPasswords
;
298 case syncer::PREFERENCES
:
299 return prefs::kSyncPreferences
;
300 case syncer::AUTOFILL
:
301 return prefs::kSyncAutofill
;
302 case syncer::AUTOFILL_PROFILE
:
303 return prefs::kSyncAutofillProfile
;
305 return prefs::kSyncThemes
;
306 case syncer::TYPED_URLS
:
307 return prefs::kSyncTypedUrls
;
308 case syncer::EXTENSION_SETTINGS
:
309 return prefs::kSyncExtensionSettings
;
310 case syncer::EXTENSIONS
:
311 return prefs::kSyncExtensions
;
312 case syncer::APP_LIST
:
313 return prefs::kSyncAppList
;
314 case syncer::APP_SETTINGS
:
315 return prefs::kSyncAppSettings
;
317 return prefs::kSyncApps
;
318 case syncer::SEARCH_ENGINES
:
319 return prefs::kSyncSearchEngines
;
320 case syncer::SESSIONS
:
321 return prefs::kSyncSessions
;
322 case syncer::APP_NOTIFICATIONS
:
323 return prefs::kSyncAppNotifications
;
324 case syncer::HISTORY_DELETE_DIRECTIVES
:
325 return prefs::kSyncHistoryDeleteDirectives
;
326 case syncer::SYNCED_NOTIFICATIONS
:
327 return prefs::kSyncSyncedNotifications
;
328 case syncer::DICTIONARY
:
329 return prefs::kSyncDictionary
;
330 case syncer::FAVICON_IMAGES
:
331 return prefs::kSyncFaviconImages
;
332 case syncer::FAVICON_TRACKING
:
333 return prefs::kSyncFaviconTracking
;
334 case syncer::MANAGED_USER_SETTINGS
:
335 return prefs::kSyncManagedUserSettings
;
336 case syncer::PROXY_TABS
:
337 return prefs::kSyncTabs
;
338 case syncer::PRIORITY_PREFERENCES
:
339 return prefs::kSyncPriorityPreferences
;
340 case syncer::MANAGED_USERS
:
341 return prefs::kSyncManagedUsers
;
342 case syncer::ARTICLES
:
343 return prefs::kSyncArticles
;
344 case syncer::MANAGED_USER_SHARED_SETTINGS
:
345 return prefs::kSyncManagedUserSharedSettings
;
353 #if defined(OS_CHROMEOS)
354 std::string
SyncPrefs::GetSpareBootstrapToken() const {
355 DCHECK(CalledOnValidThread());
356 return pref_service_
->GetString(prefs::kSyncSpareBootstrapToken
);
359 void SyncPrefs::SetSpareBootstrapToken(const std::string
& token
) {
360 DCHECK(CalledOnValidThread());
361 pref_service_
->SetString(prefs::kSyncSpareBootstrapToken
, token
);
365 void SyncPrefs::AcknowledgeSyncedTypes(syncer::ModelTypeSet types
) {
366 DCHECK(CalledOnValidThread());
367 // Add the types to the current set of acknowledged
368 // types, and then store the resulting set in prefs.
369 const syncer::ModelTypeSet acknowledged_types
=
371 syncer::ModelTypeSetFromValue(
372 *pref_service_
->GetList(prefs::kSyncAcknowledgedSyncTypes
)));
374 scoped_ptr
<base::ListValue
> value(
375 syncer::ModelTypeSetToValue(acknowledged_types
));
376 pref_service_
->Set(prefs::kSyncAcknowledgedSyncTypes
, *value
);
379 void SyncPrefs::OnSyncManagedPrefChanged() {
380 DCHECK(CalledOnValidThread());
381 FOR_EACH_OBSERVER(SyncPrefObserver
, sync_pref_observers_
,
382 OnSyncManagedPrefChange(*pref_sync_managed_
));
385 void SyncPrefs::SetManagedForTest(bool is_managed
) {
386 DCHECK(CalledOnValidThread());
387 pref_service_
->SetBoolean(prefs::kSyncManaged
, is_managed
);
390 syncer::ModelTypeSet
SyncPrefs::GetAcknowledgeSyncedTypesForTest() const {
391 DCHECK(CalledOnValidThread());
392 return syncer::ModelTypeSetFromValue(
393 *pref_service_
->GetList(prefs::kSyncAcknowledgedSyncTypes
));
396 void SyncPrefs::RegisterPrefGroups() {
397 pref_groups_
[syncer::APPS
].Put(syncer::APP_NOTIFICATIONS
);
398 pref_groups_
[syncer::APPS
].Put(syncer::APP_SETTINGS
);
399 pref_groups_
[syncer::APPS
].Put(syncer::APP_LIST
);
401 pref_groups_
[syncer::AUTOFILL
].Put(syncer::AUTOFILL_PROFILE
);
403 pref_groups_
[syncer::EXTENSIONS
].Put(syncer::EXTENSION_SETTINGS
);
405 pref_groups_
[syncer::PREFERENCES
].Put(syncer::DICTIONARY
);
406 pref_groups_
[syncer::PREFERENCES
].Put(syncer::PRIORITY_PREFERENCES
);
407 pref_groups_
[syncer::PREFERENCES
].Put(syncer::SEARCH_ENGINES
);
409 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::HISTORY_DELETE_DIRECTIVES
);
410 if (!CommandLine::ForCurrentProcess()->HasSwitch(
411 switches::kHistoryDisableFullHistorySync
)) {
412 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::SESSIONS
);
413 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::FAVICON_IMAGES
);
414 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::FAVICON_TRACKING
);
417 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::SESSIONS
);
418 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::FAVICON_IMAGES
);
419 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::FAVICON_TRACKING
);
421 pref_groups_
[syncer::MANAGED_USER_SETTINGS
].Put(syncer::SESSIONS
);
423 // TODO(zea): put favicons in the bookmarks group as well once it handles
428 void SyncPrefs::RegisterDataTypePreferredPref(
429 user_prefs::PrefRegistrySyncable
* registry
,
430 syncer::ModelType type
,
432 const char* pref_name
= GetPrefNameForDataType(type
);
437 registry
->RegisterBooleanPref(
440 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
443 bool SyncPrefs::GetDataTypePreferred(syncer::ModelType type
) const {
444 DCHECK(CalledOnValidThread());
445 const char* pref_name
= GetPrefNameForDataType(type
);
450 if (type
== syncer::PROXY_TABS
&&
451 pref_service_
->GetUserPrefValue(pref_name
) == NULL
&&
452 pref_service_
->IsUserModifiablePreference(pref_name
)) {
453 // If there is no tab sync preference yet (i.e. newly enabled type),
454 // default to the session sync preference value.
455 pref_name
= GetPrefNameForDataType(syncer::SESSIONS
);
458 return pref_service_
->GetBoolean(pref_name
);
461 void SyncPrefs::SetDataTypePreferred(
462 syncer::ModelType type
, bool is_preferred
) {
463 DCHECK(CalledOnValidThread());
464 const char* pref_name
= GetPrefNameForDataType(type
);
469 pref_service_
->SetBoolean(pref_name
, is_preferred
);
472 syncer::ModelTypeSet
SyncPrefs::ResolvePrefGroups(
473 syncer::ModelTypeSet registered_types
,
474 syncer::ModelTypeSet types
) const {
475 DCHECK(registered_types
.HasAll(types
));
476 syncer::ModelTypeSet types_with_groups
= types
;
477 for (PrefGroupsMap::const_iterator i
= pref_groups_
.begin();
478 i
!= pref_groups_
.end(); ++i
) {
479 if (types
.Has(i
->first
))
480 types_with_groups
.PutAll(i
->second
);
482 types_with_groups
.RetainAll(registered_types
);
483 return types_with_groups
;
486 } // namespace browser_sync