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::kSyncFirstSyncTime
, 0);
43 // All datatypes are on by default, but this gets set explicitly
44 // when you configure sync (when turning it on), in
45 // ProfileSyncService::OnUserChoseDatatypes.
46 registry
->RegisterBooleanPref(prefs::kSyncKeepEverythingSynced
, true);
48 syncer::ModelTypeSet user_types
= syncer::UserTypes();
50 // Include proxy types as well, as they can be individually selected,
51 // although they don't have sync representations.
52 user_types
.PutAll(syncer::ProxyTypes());
54 // Treat bookmarks and device info specially.
55 RegisterDataTypePreferredPref(registry
, syncer::BOOKMARKS
, true);
56 RegisterDataTypePreferredPref(registry
, syncer::DEVICE_INFO
, true);
57 user_types
.Remove(syncer::BOOKMARKS
);
58 user_types
.Remove(syncer::DEVICE_INFO
);
60 // All types are set to off by default, which forces a configuration to
61 // explicitly enable them. GetPreferredTypes() will ensure that any new
62 // implicit types are enabled when their pref group is, or via
63 // KeepEverythingSynced.
64 for (syncer::ModelTypeSet::Iterator it
= user_types
.First(); it
.Good();
66 RegisterDataTypePreferredPref(registry
, it
.Get(), false);
69 registry
->RegisterBooleanPref(prefs::kSyncManaged
, false);
70 registry
->RegisterStringPref(prefs::kSyncEncryptionBootstrapToken
,
72 registry
->RegisterStringPref(prefs::kSyncKeystoreEncryptionBootstrapToken
,
74 #if defined(OS_CHROMEOS)
75 registry
->RegisterStringPref(prefs::kSyncSpareBootstrapToken
, "");
78 registry
->RegisterBooleanPref(prefs::kSyncHasAuthError
, false);
80 registry
->RegisterStringPref(prefs::kSyncSessionsGUID
, std::string());
82 registry
->RegisterIntegerPref(prefs::kSyncRemainingRollbackTries
, 0);
85 void SyncPrefs::AddSyncPrefObserver(SyncPrefObserver
* sync_pref_observer
) {
86 DCHECK(CalledOnValidThread());
87 sync_pref_observers_
.AddObserver(sync_pref_observer
);
90 void SyncPrefs::RemoveSyncPrefObserver(SyncPrefObserver
* sync_pref_observer
) {
91 DCHECK(CalledOnValidThread());
92 sync_pref_observers_
.RemoveObserver(sync_pref_observer
);
95 void SyncPrefs::ClearPreferences() {
96 DCHECK(CalledOnValidThread());
97 pref_service_
->ClearPref(prefs::kSyncLastSyncedTime
);
98 pref_service_
->ClearPref(prefs::kSyncHasSetupCompleted
);
99 pref_service_
->ClearPref(prefs::kSyncEncryptionBootstrapToken
);
100 pref_service_
->ClearPref(prefs::kSyncKeystoreEncryptionBootstrapToken
);
102 // TODO(nick): The current behavior does not clear
103 // e.g. prefs::kSyncBookmarks. Is that really what we want?
106 bool SyncPrefs::HasSyncSetupCompleted() const {
107 DCHECK(CalledOnValidThread());
108 return pref_service_
->GetBoolean(prefs::kSyncHasSetupCompleted
);
111 void SyncPrefs::SetSyncSetupCompleted() {
112 DCHECK(CalledOnValidThread());
113 pref_service_
->SetBoolean(prefs::kSyncHasSetupCompleted
, true);
114 SetStartSuppressed(false);
117 bool SyncPrefs::SyncHasAuthError() const {
118 DCHECK(CalledOnValidThread());
119 return pref_service_
->GetBoolean(prefs::kSyncHasAuthError
);
122 void SyncPrefs::SetSyncAuthError(bool error
) {
123 DCHECK(CalledOnValidThread());
124 pref_service_
->SetBoolean(prefs::kSyncHasAuthError
, error
);
127 bool SyncPrefs::IsStartSuppressed() const {
128 DCHECK(CalledOnValidThread());
129 return pref_service_
->GetBoolean(prefs::kSyncSuppressStart
);
132 void SyncPrefs::SetStartSuppressed(bool is_suppressed
) {
133 DCHECK(CalledOnValidThread());
134 pref_service_
->SetBoolean(prefs::kSyncSuppressStart
, is_suppressed
);
137 base::Time
SyncPrefs::GetLastSyncedTime() const {
138 DCHECK(CalledOnValidThread());
139 return base::Time::FromInternalValue(
140 pref_service_
->GetInt64(prefs::kSyncLastSyncedTime
));
143 void SyncPrefs::SetLastSyncedTime(base::Time time
) {
144 DCHECK(CalledOnValidThread());
145 pref_service_
->SetInt64(prefs::kSyncLastSyncedTime
, time
.ToInternalValue());
148 bool SyncPrefs::HasKeepEverythingSynced() const {
149 DCHECK(CalledOnValidThread());
150 return pref_service_
->GetBoolean(prefs::kSyncKeepEverythingSynced
);
153 void SyncPrefs::SetKeepEverythingSynced(bool keep_everything_synced
) {
154 DCHECK(CalledOnValidThread());
155 pref_service_
->SetBoolean(prefs::kSyncKeepEverythingSynced
,
156 keep_everything_synced
);
159 syncer::ModelTypeSet
SyncPrefs::GetPreferredDataTypes(
160 syncer::ModelTypeSet registered_types
) const {
161 DCHECK(CalledOnValidThread());
163 if (pref_service_
->GetBoolean(prefs::kSyncKeepEverythingSynced
)) {
164 return registered_types
;
167 syncer::ModelTypeSet preferred_types
;
168 for (syncer::ModelTypeSet::Iterator it
= registered_types
.First(); it
.Good();
170 if (GetDataTypePreferred(it
.Get())) {
171 preferred_types
.Put(it
.Get());
174 return ResolvePrefGroups(registered_types
, preferred_types
);
177 void SyncPrefs::SetPreferredDataTypes(syncer::ModelTypeSet registered_types
,
178 syncer::ModelTypeSet preferred_types
) {
179 DCHECK(CalledOnValidThread());
180 DCHECK(registered_types
.HasAll(preferred_types
));
181 preferred_types
= ResolvePrefGroups(registered_types
, preferred_types
);
182 for (syncer::ModelTypeSet::Iterator i
= registered_types
.First(); i
.Good();
184 SetDataTypePreferred(i
.Get(), preferred_types
.Has(i
.Get()));
188 bool SyncPrefs::IsManaged() const {
189 DCHECK(CalledOnValidThread());
190 return pref_service_
->GetBoolean(prefs::kSyncManaged
);
193 std::string
SyncPrefs::GetEncryptionBootstrapToken() const {
194 DCHECK(CalledOnValidThread());
195 return pref_service_
->GetString(prefs::kSyncEncryptionBootstrapToken
);
198 void SyncPrefs::SetEncryptionBootstrapToken(const std::string
& token
) {
199 DCHECK(CalledOnValidThread());
200 pref_service_
->SetString(prefs::kSyncEncryptionBootstrapToken
, token
);
203 std::string
SyncPrefs::GetKeystoreEncryptionBootstrapToken() const {
204 DCHECK(CalledOnValidThread());
205 return pref_service_
->GetString(prefs::kSyncKeystoreEncryptionBootstrapToken
);
208 void SyncPrefs::SetKeystoreEncryptionBootstrapToken(const std::string
& token
) {
209 DCHECK(CalledOnValidThread());
210 pref_service_
->SetString(prefs::kSyncKeystoreEncryptionBootstrapToken
, token
);
213 std::string
SyncPrefs::GetSyncSessionsGUID() const {
214 DCHECK(CalledOnValidThread());
215 return pref_service_
->GetString(prefs::kSyncSessionsGUID
);
218 void SyncPrefs::SetSyncSessionsGUID(const std::string
& guid
) {
219 DCHECK(CalledOnValidThread());
220 pref_service_
->SetString(prefs::kSyncSessionsGUID
, guid
);
224 const char* SyncPrefs::GetPrefNameForDataType(syncer::ModelType data_type
) {
226 case syncer::BOOKMARKS
:
227 return prefs::kSyncBookmarks
;
228 case syncer::PASSWORDS
:
229 return prefs::kSyncPasswords
;
230 case syncer::PREFERENCES
:
231 return prefs::kSyncPreferences
;
232 case syncer::AUTOFILL
:
233 return prefs::kSyncAutofill
;
234 case syncer::AUTOFILL_PROFILE
:
235 return prefs::kSyncAutofillWallet
;
236 case syncer::AUTOFILL_WALLET_DATA
:
237 return prefs::kSyncAutofillProfile
;
239 return prefs::kSyncThemes
;
240 case syncer::TYPED_URLS
:
241 return prefs::kSyncTypedUrls
;
242 case syncer::EXTENSION_SETTINGS
:
243 return prefs::kSyncExtensionSettings
;
244 case syncer::EXTENSIONS
:
245 return prefs::kSyncExtensions
;
246 case syncer::APP_LIST
:
247 return prefs::kSyncAppList
;
248 case syncer::APP_SETTINGS
:
249 return prefs::kSyncAppSettings
;
251 return prefs::kSyncApps
;
252 case syncer::SEARCH_ENGINES
:
253 return prefs::kSyncSearchEngines
;
254 case syncer::SESSIONS
:
255 return prefs::kSyncSessions
;
256 case syncer::APP_NOTIFICATIONS
:
257 return prefs::kSyncAppNotifications
;
258 case syncer::HISTORY_DELETE_DIRECTIVES
:
259 return prefs::kSyncHistoryDeleteDirectives
;
260 case syncer::SYNCED_NOTIFICATIONS
:
261 return prefs::kSyncSyncedNotifications
;
262 case syncer::SYNCED_NOTIFICATION_APP_INFO
:
263 return prefs::kSyncSyncedNotificationAppInfo
;
264 case syncer::DICTIONARY
:
265 return prefs::kSyncDictionary
;
266 case syncer::FAVICON_IMAGES
:
267 return prefs::kSyncFaviconImages
;
268 case syncer::FAVICON_TRACKING
:
269 return prefs::kSyncFaviconTracking
;
270 case syncer::SUPERVISED_USER_SETTINGS
:
271 return prefs::kSyncSupervisedUserSettings
;
272 case syncer::PROXY_TABS
:
273 return prefs::kSyncTabs
;
274 case syncer::PRIORITY_PREFERENCES
:
275 return prefs::kSyncPriorityPreferences
;
276 case syncer::SUPERVISED_USERS
:
277 return prefs::kSyncSupervisedUsers
;
278 case syncer::ARTICLES
:
279 return prefs::kSyncArticles
;
280 case syncer::SUPERVISED_USER_SHARED_SETTINGS
:
281 return prefs::kSyncSupervisedUserSharedSettings
;
282 case syncer::SUPERVISED_USER_WHITELISTS
:
283 return prefs::kSyncSupervisedUserWhitelists
;
284 case syncer::DEVICE_INFO
:
285 return prefs::kSyncDeviceInfo
;
286 case syncer::WIFI_CREDENTIALS
:
287 return prefs::kSyncWifiCredentials
;
291 NOTREACHED() << "Type is " << data_type
;
295 #if defined(OS_CHROMEOS)
296 std::string
SyncPrefs::GetSpareBootstrapToken() const {
297 DCHECK(CalledOnValidThread());
298 return pref_service_
->GetString(prefs::kSyncSpareBootstrapToken
);
301 void SyncPrefs::SetSpareBootstrapToken(const std::string
& token
) {
302 DCHECK(CalledOnValidThread());
303 pref_service_
->SetString(prefs::kSyncSpareBootstrapToken
, token
);
307 int SyncPrefs::GetRemainingRollbackTries() const {
308 return pref_service_
->GetInteger(prefs::kSyncRemainingRollbackTries
);
311 void SyncPrefs::SetRemainingRollbackTries(int times
) {
312 pref_service_
->SetInteger(prefs::kSyncRemainingRollbackTries
, times
);
315 void SyncPrefs::OnSyncManagedPrefChanged() {
316 DCHECK(CalledOnValidThread());
317 FOR_EACH_OBSERVER(SyncPrefObserver
,
318 sync_pref_observers_
,
319 OnSyncManagedPrefChange(*pref_sync_managed_
));
322 void SyncPrefs::SetManagedForTest(bool is_managed
) {
323 DCHECK(CalledOnValidThread());
324 pref_service_
->SetBoolean(prefs::kSyncManaged
, is_managed
);
327 void SyncPrefs::RegisterPrefGroups() {
328 pref_groups_
[syncer::APPS
].Put(syncer::APP_NOTIFICATIONS
);
329 pref_groups_
[syncer::APPS
].Put(syncer::APP_SETTINGS
);
330 pref_groups_
[syncer::APPS
].Put(syncer::APP_LIST
);
332 pref_groups_
[syncer::AUTOFILL
].Put(syncer::AUTOFILL_PROFILE
);
333 pref_groups_
[syncer::AUTOFILL
].Put(syncer::AUTOFILL_WALLET_DATA
);
335 pref_groups_
[syncer::EXTENSIONS
].Put(syncer::EXTENSION_SETTINGS
);
337 pref_groups_
[syncer::PREFERENCES
].Put(syncer::DICTIONARY
);
338 pref_groups_
[syncer::PREFERENCES
].Put(syncer::PRIORITY_PREFERENCES
);
339 pref_groups_
[syncer::PREFERENCES
].Put(syncer::SEARCH_ENGINES
);
341 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::HISTORY_DELETE_DIRECTIVES
);
342 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::SESSIONS
);
343 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::FAVICON_IMAGES
);
344 pref_groups_
[syncer::TYPED_URLS
].Put(syncer::FAVICON_TRACKING
);
346 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::SESSIONS
);
347 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::FAVICON_IMAGES
);
348 pref_groups_
[syncer::PROXY_TABS
].Put(syncer::FAVICON_TRACKING
);
350 // TODO(zea): put favicons in the bookmarks group as well once it handles
355 void SyncPrefs::RegisterDataTypePreferredPref(
356 user_prefs::PrefRegistrySyncable
* registry
,
357 syncer::ModelType type
,
359 const char* pref_name
= GetPrefNameForDataType(type
);
364 registry
->RegisterBooleanPref(pref_name
, is_preferred
);
367 bool SyncPrefs::GetDataTypePreferred(syncer::ModelType type
) const {
368 DCHECK(CalledOnValidThread());
369 const char* pref_name
= GetPrefNameForDataType(type
);
375 // Device info is always enabled.
376 if (pref_name
== prefs::kSyncDeviceInfo
)
379 if (type
== syncer::PROXY_TABS
&&
380 pref_service_
->GetUserPrefValue(pref_name
) == NULL
&&
381 pref_service_
->IsUserModifiablePreference(pref_name
)) {
382 // If there is no tab sync preference yet (i.e. newly enabled type),
383 // default to the session sync preference value.
384 pref_name
= GetPrefNameForDataType(syncer::SESSIONS
);
387 return pref_service_
->GetBoolean(pref_name
);
390 void SyncPrefs::SetDataTypePreferred(syncer::ModelType type
,
392 DCHECK(CalledOnValidThread());
393 const char* pref_name
= GetPrefNameForDataType(type
);
399 // Device info is always preferred.
400 if (type
== syncer::DEVICE_INFO
)
403 pref_service_
->SetBoolean(pref_name
, is_preferred
);
406 syncer::ModelTypeSet
SyncPrefs::ResolvePrefGroups(
407 syncer::ModelTypeSet registered_types
,
408 syncer::ModelTypeSet types
) const {
409 DCHECK(registered_types
.HasAll(types
));
410 syncer::ModelTypeSet types_with_groups
= types
;
411 for (PrefGroupsMap::const_iterator i
= pref_groups_
.begin();
412 i
!= pref_groups_
.end();
414 if (types
.Has(i
->first
))
415 types_with_groups
.PutAll(i
->second
);
417 types_with_groups
.RetainAll(registered_types
);
418 return types_with_groups
;
421 base::Time
SyncPrefs::GetFirstSyncTime() const {
422 return base::Time::FromInternalValue(
423 pref_service_
->GetInt64(prefs::kSyncFirstSyncTime
));
426 void SyncPrefs::SetFirstSyncTime(base::Time time
) {
427 pref_service_
->SetInt64(prefs::kSyncFirstSyncTime
, time
.ToInternalValue());
430 void SyncPrefs::ClearFirstSyncTime() {
431 pref_service_
->ClearPref(prefs::kSyncFirstSyncTime
);
434 } // namespace sync_driver