Move documentScan.html from extensions to apps
[chromium-blink-merge.git] / components / sync_driver / sync_prefs.cc
blobe8c3f19bc5db776c44a6684d3c27e312364e80d0
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) {
21 DCHECK(pref_service);
22 RegisterPrefGroups();
23 // Watch the preference that indicates sync is managed so we can take
24 // appropriate action.
25 pref_sync_managed_.Init(
26 prefs::kSyncManaged,
27 pref_service_,
28 base::Bind(&SyncPrefs::OnSyncManagedPrefChanged, base::Unretained(this)));
31 SyncPrefs::SyncPrefs() : pref_service_(NULL) {}
33 SyncPrefs::~SyncPrefs() { DCHECK(CalledOnValidThread()); }
35 // static
36 void SyncPrefs::RegisterProfilePrefs(
37 user_prefs::PrefRegistrySyncable* registry) {
38 registry->RegisterBooleanPref(
39 prefs::kSyncHasSetupCompleted,
40 false,
41 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
42 registry->RegisterBooleanPref(
43 prefs::kSyncSuppressStart,
44 false,
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,
60 true,
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 // These two prefs are set from sync experiment to enable enhanced bookmarks.
76 registry->RegisterIntegerPref(
77 prefs::kEnhancedBookmarksExperimentEnabled,
79 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
81 registry->RegisterStringPref(
82 prefs::kEnhancedBookmarksExtensionId,
83 std::string(),
84 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
86 // All types are set to off by default, which forces a configuration to
87 // explicitly enable them. GetPreferredTypes() will ensure that any new
88 // implicit types are enabled when their pref group is, or via
89 // KeepEverythingSynced.
90 for (syncer::ModelTypeSet::Iterator it = user_types.First(); it.Good();
91 it.Inc()) {
92 RegisterDataTypePreferredPref(registry, it.Get(), false);
95 registry->RegisterBooleanPref(
96 prefs::kSyncManaged,
97 false,
98 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
99 registry->RegisterStringPref(
100 prefs::kSyncEncryptionBootstrapToken,
101 std::string(),
102 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
103 registry->RegisterStringPref(
104 prefs::kSyncKeystoreEncryptionBootstrapToken,
105 std::string(),
106 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
107 #if defined(OS_CHROMEOS)
108 registry->RegisterStringPref(
109 prefs::kSyncSpareBootstrapToken,
111 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
112 #endif
114 registry->RegisterBooleanPref(
115 prefs::kSyncHasAuthError,
116 false,
117 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
119 registry->RegisterStringPref(
120 prefs::kSyncSessionsGUID,
121 std::string(),
122 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
124 // We will start prompting people about new data types after the launch of
125 // SESSIONS - all previously launched data types are treated as if they are
126 // already acknowledged.
127 syncer::ModelTypeSet model_set;
128 model_set.Put(syncer::BOOKMARKS);
129 model_set.Put(syncer::PREFERENCES);
130 model_set.Put(syncer::PASSWORDS);
131 model_set.Put(syncer::AUTOFILL_PROFILE);
132 model_set.Put(syncer::AUTOFILL);
133 model_set.Put(syncer::THEMES);
134 model_set.Put(syncer::EXTENSIONS);
135 model_set.Put(syncer::NIGORI);
136 model_set.Put(syncer::SEARCH_ENGINES);
137 model_set.Put(syncer::APPS);
138 model_set.Put(syncer::APP_LIST);
139 model_set.Put(syncer::TYPED_URLS);
140 model_set.Put(syncer::SESSIONS);
141 model_set.Put(syncer::ARTICLES);
142 model_set.Put(syncer::WIFI_CREDENTIALS);
143 registry->RegisterListPref(prefs::kSyncAcknowledgedSyncTypes,
144 syncer::ModelTypeSetToValue(model_set),
145 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
147 registry->RegisterIntegerPref(
148 prefs::kSyncRemainingRollbackTries, 0,
149 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
152 void SyncPrefs::AddSyncPrefObserver(SyncPrefObserver* sync_pref_observer) {
153 DCHECK(CalledOnValidThread());
154 sync_pref_observers_.AddObserver(sync_pref_observer);
157 void SyncPrefs::RemoveSyncPrefObserver(SyncPrefObserver* sync_pref_observer) {
158 DCHECK(CalledOnValidThread());
159 sync_pref_observers_.RemoveObserver(sync_pref_observer);
162 void SyncPrefs::ClearPreferences() {
163 DCHECK(CalledOnValidThread());
164 pref_service_->ClearPref(prefs::kSyncLastSyncedTime);
165 pref_service_->ClearPref(prefs::kSyncHasSetupCompleted);
166 pref_service_->ClearPref(prefs::kSyncEncryptionBootstrapToken);
167 pref_service_->ClearPref(prefs::kSyncKeystoreEncryptionBootstrapToken);
169 // TODO(nick): The current behavior does not clear
170 // e.g. prefs::kSyncBookmarks. Is that really what we want?
173 bool SyncPrefs::HasSyncSetupCompleted() const {
174 DCHECK(CalledOnValidThread());
175 return pref_service_->GetBoolean(prefs::kSyncHasSetupCompleted);
178 void SyncPrefs::SetSyncSetupCompleted() {
179 DCHECK(CalledOnValidThread());
180 pref_service_->SetBoolean(prefs::kSyncHasSetupCompleted, true);
181 SetStartSuppressed(false);
184 bool SyncPrefs::SyncHasAuthError() const {
185 DCHECK(CalledOnValidThread());
186 return pref_service_->GetBoolean(prefs::kSyncHasAuthError);
189 void SyncPrefs::SetSyncAuthError(bool error) {
190 DCHECK(CalledOnValidThread());
191 pref_service_->SetBoolean(prefs::kSyncHasAuthError, error);
194 bool SyncPrefs::IsStartSuppressed() const {
195 DCHECK(CalledOnValidThread());
196 return pref_service_->GetBoolean(prefs::kSyncSuppressStart);
199 void SyncPrefs::SetStartSuppressed(bool is_suppressed) {
200 DCHECK(CalledOnValidThread());
201 pref_service_->SetBoolean(prefs::kSyncSuppressStart, is_suppressed);
204 base::Time SyncPrefs::GetLastSyncedTime() const {
205 DCHECK(CalledOnValidThread());
206 return base::Time::FromInternalValue(
207 pref_service_->GetInt64(prefs::kSyncLastSyncedTime));
210 void SyncPrefs::SetLastSyncedTime(base::Time time) {
211 DCHECK(CalledOnValidThread());
212 pref_service_->SetInt64(prefs::kSyncLastSyncedTime, time.ToInternalValue());
215 bool SyncPrefs::HasKeepEverythingSynced() const {
216 DCHECK(CalledOnValidThread());
217 return pref_service_->GetBoolean(prefs::kSyncKeepEverythingSynced);
220 void SyncPrefs::SetKeepEverythingSynced(bool keep_everything_synced) {
221 DCHECK(CalledOnValidThread());
222 pref_service_->SetBoolean(prefs::kSyncKeepEverythingSynced,
223 keep_everything_synced);
226 syncer::ModelTypeSet SyncPrefs::GetPreferredDataTypes(
227 syncer::ModelTypeSet registered_types) const {
228 DCHECK(CalledOnValidThread());
230 if (pref_service_->GetBoolean(prefs::kSyncKeepEverythingSynced)) {
231 return registered_types;
234 syncer::ModelTypeSet preferred_types;
235 for (syncer::ModelTypeSet::Iterator it = registered_types.First(); it.Good();
236 it.Inc()) {
237 if (GetDataTypePreferred(it.Get())) {
238 preferred_types.Put(it.Get());
241 return ResolvePrefGroups(registered_types, preferred_types);
244 void SyncPrefs::SetPreferredDataTypes(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(); i.Good();
250 i.Inc()) {
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(prefs::kSyncKeystoreEncryptionBootstrapToken);
275 void SyncPrefs::SetKeystoreEncryptionBootstrapToken(const std::string& token) {
276 DCHECK(CalledOnValidThread());
277 pref_service_->SetString(prefs::kSyncKeystoreEncryptionBootstrapToken, token);
280 std::string SyncPrefs::GetSyncSessionsGUID() const {
281 DCHECK(CalledOnValidThread());
282 return pref_service_->GetString(prefs::kSyncSessionsGUID);
285 void SyncPrefs::SetSyncSessionsGUID(const std::string& guid) {
286 DCHECK(CalledOnValidThread());
287 pref_service_->SetString(prefs::kSyncSessionsGUID, guid);
290 // static
291 const char* SyncPrefs::GetPrefNameForDataType(syncer::ModelType data_type) {
292 switch (data_type) {
293 case syncer::BOOKMARKS:
294 return prefs::kSyncBookmarks;
295 case syncer::PASSWORDS:
296 return prefs::kSyncPasswords;
297 case syncer::PREFERENCES:
298 return prefs::kSyncPreferences;
299 case syncer::AUTOFILL:
300 return prefs::kSyncAutofill;
301 case syncer::AUTOFILL_PROFILE:
302 return prefs::kSyncAutofillProfile;
303 case syncer::THEMES:
304 return prefs::kSyncThemes;
305 case syncer::TYPED_URLS:
306 return prefs::kSyncTypedUrls;
307 case syncer::EXTENSION_SETTINGS:
308 return prefs::kSyncExtensionSettings;
309 case syncer::EXTENSIONS:
310 return prefs::kSyncExtensions;
311 case syncer::APP_LIST:
312 return prefs::kSyncAppList;
313 case syncer::APP_SETTINGS:
314 return prefs::kSyncAppSettings;
315 case syncer::APPS:
316 return prefs::kSyncApps;
317 case syncer::SEARCH_ENGINES:
318 return prefs::kSyncSearchEngines;
319 case syncer::SESSIONS:
320 return prefs::kSyncSessions;
321 case syncer::APP_NOTIFICATIONS:
322 return prefs::kSyncAppNotifications;
323 case syncer::HISTORY_DELETE_DIRECTIVES:
324 return prefs::kSyncHistoryDeleteDirectives;
325 case syncer::SYNCED_NOTIFICATIONS:
326 return prefs::kSyncSyncedNotifications;
327 case syncer::SYNCED_NOTIFICATION_APP_INFO:
328 return prefs::kSyncSyncedNotificationAppInfo;
329 case syncer::DICTIONARY:
330 return prefs::kSyncDictionary;
331 case syncer::FAVICON_IMAGES:
332 return prefs::kSyncFaviconImages;
333 case syncer::FAVICON_TRACKING:
334 return prefs::kSyncFaviconTracking;
335 case syncer::SUPERVISED_USER_SETTINGS:
336 return prefs::kSyncSupervisedUserSettings;
337 case syncer::PROXY_TABS:
338 return prefs::kSyncTabs;
339 case syncer::PRIORITY_PREFERENCES:
340 return prefs::kSyncPriorityPreferences;
341 case syncer::SUPERVISED_USERS:
342 return prefs::kSyncSupervisedUsers;
343 case syncer::ARTICLES:
344 return prefs::kSyncArticles;
345 case syncer::SUPERVISED_USER_SHARED_SETTINGS:
346 return prefs::kSyncSupervisedUserSharedSettings;
347 case syncer::SUPERVISED_USER_WHITELISTS:
348 return prefs::kSyncSupervisedUserWhitelists;
349 case syncer::DEVICE_INFO:
350 return prefs::kSyncDeviceInfo;
351 case syncer::WIFI_CREDENTIALS:
352 return prefs::kSyncWifiCredentials;
353 default:
354 break;
356 NOTREACHED() << "Type is " << data_type;
357 return NULL;
360 #if defined(OS_CHROMEOS)
361 std::string SyncPrefs::GetSpareBootstrapToken() const {
362 DCHECK(CalledOnValidThread());
363 return pref_service_->GetString(prefs::kSyncSpareBootstrapToken);
366 void SyncPrefs::SetSpareBootstrapToken(const std::string& token) {
367 DCHECK(CalledOnValidThread());
368 pref_service_->SetString(prefs::kSyncSpareBootstrapToken, token);
370 #endif
372 void SyncPrefs::AcknowledgeSyncedTypes(syncer::ModelTypeSet types) {
373 DCHECK(CalledOnValidThread());
374 // Add the types to the current set of acknowledged
375 // types, and then store the resulting set in prefs.
376 const syncer::ModelTypeSet acknowledged_types =
377 Union(types,
378 syncer::ModelTypeSetFromValue(
379 *pref_service_->GetList(prefs::kSyncAcknowledgedSyncTypes)));
381 scoped_ptr<base::ListValue> value(
382 syncer::ModelTypeSetToValue(acknowledged_types));
383 pref_service_->Set(prefs::kSyncAcknowledgedSyncTypes, *value);
386 int SyncPrefs::GetRemainingRollbackTries() const {
387 return pref_service_->GetInteger(prefs::kSyncRemainingRollbackTries);
390 void SyncPrefs::SetRemainingRollbackTries(int times) {
391 pref_service_->SetInteger(prefs::kSyncRemainingRollbackTries, times);
394 void SyncPrefs::OnSyncManagedPrefChanged() {
395 DCHECK(CalledOnValidThread());
396 FOR_EACH_OBSERVER(SyncPrefObserver,
397 sync_pref_observers_,
398 OnSyncManagedPrefChange(*pref_sync_managed_));
401 void SyncPrefs::SetManagedForTest(bool is_managed) {
402 DCHECK(CalledOnValidThread());
403 pref_service_->SetBoolean(prefs::kSyncManaged, is_managed);
406 syncer::ModelTypeSet SyncPrefs::GetAcknowledgeSyncedTypesForTest() const {
407 DCHECK(CalledOnValidThread());
408 return syncer::ModelTypeSetFromValue(
409 *pref_service_->GetList(prefs::kSyncAcknowledgedSyncTypes));
412 void SyncPrefs::RegisterPrefGroups() {
413 pref_groups_[syncer::APPS].Put(syncer::APP_NOTIFICATIONS);
414 pref_groups_[syncer::APPS].Put(syncer::APP_SETTINGS);
415 pref_groups_[syncer::APPS].Put(syncer::APP_LIST);
417 pref_groups_[syncer::AUTOFILL].Put(syncer::AUTOFILL_PROFILE);
419 pref_groups_[syncer::EXTENSIONS].Put(syncer::EXTENSION_SETTINGS);
421 pref_groups_[syncer::PREFERENCES].Put(syncer::DICTIONARY);
422 pref_groups_[syncer::PREFERENCES].Put(syncer::PRIORITY_PREFERENCES);
423 pref_groups_[syncer::PREFERENCES].Put(syncer::SEARCH_ENGINES);
425 pref_groups_[syncer::TYPED_URLS].Put(syncer::HISTORY_DELETE_DIRECTIVES);
426 pref_groups_[syncer::TYPED_URLS].Put(syncer::SESSIONS);
427 pref_groups_[syncer::TYPED_URLS].Put(syncer::FAVICON_IMAGES);
428 pref_groups_[syncer::TYPED_URLS].Put(syncer::FAVICON_TRACKING);
430 pref_groups_[syncer::PROXY_TABS].Put(syncer::SESSIONS);
431 pref_groups_[syncer::PROXY_TABS].Put(syncer::FAVICON_IMAGES);
432 pref_groups_[syncer::PROXY_TABS].Put(syncer::FAVICON_TRACKING);
434 // TODO(zea): put favicons in the bookmarks group as well once it handles
435 // those favicons.
438 // static
439 void SyncPrefs::RegisterDataTypePreferredPref(
440 user_prefs::PrefRegistrySyncable* registry,
441 syncer::ModelType type,
442 bool is_preferred) {
443 const char* pref_name = GetPrefNameForDataType(type);
444 if (!pref_name) {
445 NOTREACHED();
446 return;
448 registry->RegisterBooleanPref(
449 pref_name,
450 is_preferred,
451 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
454 bool SyncPrefs::GetDataTypePreferred(syncer::ModelType type) const {
455 DCHECK(CalledOnValidThread());
456 const char* pref_name = GetPrefNameForDataType(type);
457 if (!pref_name) {
458 NOTREACHED();
459 return false;
462 // Device info is always enabled.
463 if (pref_name == prefs::kSyncDeviceInfo)
464 return true;
466 if (type == syncer::PROXY_TABS &&
467 pref_service_->GetUserPrefValue(pref_name) == NULL &&
468 pref_service_->IsUserModifiablePreference(pref_name)) {
469 // If there is no tab sync preference yet (i.e. newly enabled type),
470 // default to the session sync preference value.
471 pref_name = GetPrefNameForDataType(syncer::SESSIONS);
474 return pref_service_->GetBoolean(pref_name);
477 void SyncPrefs::SetDataTypePreferred(syncer::ModelType type,
478 bool is_preferred) {
479 DCHECK(CalledOnValidThread());
480 const char* pref_name = GetPrefNameForDataType(type);
481 if (!pref_name) {
482 NOTREACHED();
483 return;
486 // Device info is always preferred.
487 if (type == syncer::DEVICE_INFO)
488 return;
490 pref_service_->SetBoolean(pref_name, is_preferred);
493 syncer::ModelTypeSet SyncPrefs::ResolvePrefGroups(
494 syncer::ModelTypeSet registered_types,
495 syncer::ModelTypeSet types) const {
496 DCHECK(registered_types.HasAll(types));
497 syncer::ModelTypeSet types_with_groups = types;
498 for (PrefGroupsMap::const_iterator i = pref_groups_.begin();
499 i != pref_groups_.end();
500 ++i) {
501 if (types.Has(i->first))
502 types_with_groups.PutAll(i->second);
504 types_with_groups.RetainAll(registered_types);
505 return types_with_groups;
508 base::Time SyncPrefs::GetFirstSyncTime() const {
509 return base::Time::FromInternalValue(
510 pref_service_->GetInt64(prefs::kSyncFirstSyncTime));
513 void SyncPrefs::SetFirstSyncTime(base::Time time) {
514 pref_service_->SetInt64(prefs::kSyncFirstSyncTime, time.ToInternalValue());
517 void SyncPrefs::ClearFirstSyncTime() {
518 pref_service_->ClearPref(prefs::kSyncFirstSyncTime);
521 } // namespace sync_driver