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 "base/command_line.h"
6 #include "build/build_config.h"
7 #include "chrome/browser/about_flags.h"
8 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
9 #include "chrome/browser/bookmarks/enhanced_bookmarks_features.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
12 #include "chrome/browser/extensions/api/storage/settings_frontend.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_sync_service.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/extensions/extension_system_factory.h"
17 #include "chrome/browser/history/history_service.h"
18 #include "chrome/browser/history/history_service_factory.h"
19 #include "chrome/browser/pref_service_flags_storage.h"
20 #include "chrome/browser/prefs/pref_model_associator.h"
21 #include "chrome/browser/prefs/pref_service_syncable.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/search_engines/template_url_service.h"
24 #include "chrome/browser/search_engines/template_url_service_factory.h"
25 #include "chrome/browser/signin/signin_manager.h"
26 #include "chrome/browser/signin/signin_manager_factory.h"
27 #include "chrome/browser/sync/glue/autofill_data_type_controller.h"
28 #include "chrome/browser/sync/glue/autofill_profile_data_type_controller.h"
29 #include "chrome/browser/sync/glue/bookmark_change_processor.h"
30 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h"
31 #include "chrome/browser/sync/glue/bookmark_model_associator.h"
32 #include "chrome/browser/sync/glue/data_type_manager_impl.h"
33 #include "chrome/browser/sync/glue/data_type_manager_observer.h"
34 #include "chrome/browser/sync/glue/extension_data_type_controller.h"
35 #include "chrome/browser/sync/glue/extension_setting_data_type_controller.h"
36 #include "chrome/browser/sync/glue/generic_change_processor.h"
37 #include "chrome/browser/sync/glue/password_change_processor.h"
38 #include "chrome/browser/sync/glue/password_data_type_controller.h"
39 #include "chrome/browser/sync/glue/password_model_associator.h"
40 #include "chrome/browser/sync/glue/proxy_data_type_controller.h"
41 #include "chrome/browser/sync/glue/search_engine_data_type_controller.h"
42 #include "chrome/browser/sync/glue/session_change_processor.h"
43 #include "chrome/browser/sync/glue/session_data_type_controller.h"
44 #include "chrome/browser/sync/glue/session_model_associator.h"
45 #include "chrome/browser/sync/glue/shared_change_processor.h"
46 #include "chrome/browser/sync/glue/sync_backend_host.h"
47 #include "chrome/browser/sync/glue/sync_backend_host_impl.h"
48 #include "chrome/browser/sync/glue/theme_data_type_controller.h"
49 #include "chrome/browser/sync/glue/typed_url_change_processor.h"
50 #include "chrome/browser/sync/glue/typed_url_data_type_controller.h"
51 #include "chrome/browser/sync/glue/typed_url_model_associator.h"
52 #include "chrome/browser/sync/glue/ui_data_type_controller.h"
53 #include "chrome/browser/sync/profile_sync_components_factory_impl.h"
54 #include "chrome/browser/sync/profile_sync_service.h"
55 #include "chrome/browser/sync/profile_sync_service_factory.h"
56 #include "chrome/browser/sync/sessions2/session_data_type_controller2.h"
57 #include "chrome/browser/themes/theme_service.h"
58 #include "chrome/browser/themes/theme_service_factory.h"
59 #include "chrome/browser/themes/theme_syncable_service.h"
60 #include "chrome/browser/ui/app_list/app_list_syncable_service.h"
61 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h"
62 #include "chrome/browser/webdata/autocomplete_syncable_service.h"
63 #include "chrome/browser/webdata/web_data_service_factory.h"
64 #include "chrome/common/chrome_switches.h"
65 #include "chrome/common/pref_names.h"
66 #include "components/autofill/core/browser/webdata/autofill_profile_syncable_service.h"
67 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
68 #include "components/dom_distiller/core/dom_distiller_service.h"
69 #include "content/public/browser/browser_thread.h"
70 #include "sync/api/syncable_service.h"
72 #if defined(ENABLE_MANAGED_USERS)
73 #include "chrome/browser/managed_mode/managed_user_settings_service.h"
74 #include "chrome/browser/managed_mode/managed_user_settings_service_factory.h"
75 #include "chrome/browser/managed_mode/managed_user_shared_settings_service.h"
76 #include "chrome/browser/managed_mode/managed_user_shared_settings_service_factory.h"
77 #include "chrome/browser/managed_mode/managed_user_sync_service.h"
78 #include "chrome/browser/managed_mode/managed_user_sync_service_factory.h"
81 #if !defined(OS_ANDROID)
82 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h"
83 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service_factory.h"
86 #if defined(ENABLE_SPELLCHECK)
87 #include "chrome/browser/spellchecker/spellcheck_factory.h"
88 #include "chrome/browser/spellchecker/spellcheck_service.h"
91 using browser_sync::AutofillDataTypeController
;
92 using browser_sync::AutofillProfileDataTypeController
;
93 using browser_sync::BookmarkChangeProcessor
;
94 using browser_sync::BookmarkDataTypeController
;
95 using browser_sync::BookmarkModelAssociator
;
96 using browser_sync::DataTypeController
;
97 using browser_sync::DataTypeErrorHandler
;
98 using browser_sync::DataTypeManager
;
99 using browser_sync::DataTypeManagerImpl
;
100 using browser_sync::DataTypeManagerObserver
;
101 using browser_sync::ExtensionDataTypeController
;
102 using browser_sync::ExtensionSettingDataTypeController
;
103 using browser_sync::GenericChangeProcessor
;
104 using browser_sync::PasswordChangeProcessor
;
105 using browser_sync::PasswordDataTypeController
;
106 using browser_sync::PasswordModelAssociator
;
107 using browser_sync::ProxyDataTypeController
;
108 using browser_sync::SearchEngineDataTypeController
;
109 using browser_sync::SessionChangeProcessor
;
110 using browser_sync::SessionDataTypeController
;
111 using browser_sync::SessionDataTypeController2
;
112 using browser_sync::SessionModelAssociator
;
113 using browser_sync::SharedChangeProcessor
;
114 using browser_sync::SyncBackendHost
;
115 using browser_sync::ThemeDataTypeController
;
116 using browser_sync::TypedUrlChangeProcessor
;
117 using browser_sync::TypedUrlDataTypeController
;
118 using browser_sync::TypedUrlModelAssociator
;
119 using browser_sync::UIDataTypeController
;
120 using content::BrowserThread
;
122 ProfileSyncComponentsFactoryImpl::ProfileSyncComponentsFactoryImpl(
123 Profile
* profile
, CommandLine
* command_line
)
125 command_line_(command_line
),
127 extensions::ExtensionSystemFactory::GetForProfile(profile
)),
129 WebDataServiceFactory::GetAutofillWebDataForProfile(
130 profile_
, Profile::EXPLICIT_ACCESS
)) {
133 ProfileSyncComponentsFactoryImpl::~ProfileSyncComponentsFactoryImpl() {
136 void ProfileSyncComponentsFactoryImpl::RegisterDataTypes(
137 ProfileSyncService
* pss
) {
138 RegisterCommonDataTypes(pss
);
139 #if !defined(OS_ANDROID)
140 RegisterDesktopDataTypes(pss
);
144 void ProfileSyncComponentsFactoryImpl::RegisterCommonDataTypes(
145 ProfileSyncService
* pss
) {
146 // Autofill sync is enabled by default. Register unless explicitly
148 if (!command_line_
->HasSwitch(switches::kDisableSyncAutofill
)) {
149 pss
->RegisterDataTypeController(
150 new AutofillDataTypeController(this, profile_
, pss
));
153 // Autofill profile sync is enabled by default. Register unless explicitly
155 if (!command_line_
->HasSwitch(switches::kDisableSyncAutofillProfile
)) {
156 pss
->RegisterDataTypeController(
157 new AutofillProfileDataTypeController(this, profile_
, pss
));
160 // Bookmark sync is enabled by default. Register unless explicitly
162 if (!command_line_
->HasSwitch(switches::kDisableSyncBookmarks
)) {
163 pss
->RegisterDataTypeController(
164 new BookmarkDataTypeController(this, profile_
, pss
));
167 // TypedUrl sync is enabled by default. Register unless explicitly disabled,
168 // or if saving history is disabled.
169 if (!profile_
->GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled
) &&
170 !command_line_
->HasSwitch(switches::kDisableSyncTypedUrls
)) {
171 pss
->RegisterDataTypeController(
172 new TypedUrlDataTypeController(this, profile_
, pss
));
175 // Delete directive sync is enabled by default. Register unless full history
177 if (!command_line_
->HasSwitch(switches::kHistoryDisableFullHistorySync
)) {
178 pss
->RegisterDataTypeController(
179 new UIDataTypeController(
180 syncer::HISTORY_DELETE_DIRECTIVES
, this, profile_
, pss
));
183 // Session sync is enabled by default. Register unless explicitly disabled.
184 if (!command_line_
->HasSwitch(switches::kDisableSyncTabs
)) {
185 pss
->RegisterDataTypeController(
186 new ProxyDataTypeController(syncer::PROXY_TABS
));
187 if (!command_line_
->HasSwitch(switches::kEnableSyncSessionsV2
)) {
188 pss
->RegisterDataTypeController(
189 new SessionDataTypeController(this, profile_
, pss
));
191 pss
->RegisterDataTypeController(
192 new SessionDataTypeController2(this, profile_
, pss
));
196 // Favicon sync is enabled by default. Register unless explicitly disabled.
197 if (!command_line_
->HasSwitch(switches::kDisableSyncFavicons
)) {
198 pss
->RegisterDataTypeController(
199 new UIDataTypeController(syncer::FAVICON_IMAGES
,
203 pss
->RegisterDataTypeController(
204 new UIDataTypeController(syncer::FAVICON_TRACKING
,
210 // Password sync is enabled by default. Register unless explicitly
212 if (!command_line_
->HasSwitch(switches::kDisableSyncPasswords
)) {
213 pss
->RegisterDataTypeController(
214 new PasswordDataTypeController(this, profile_
, pss
));
216 // Article sync is disabled by default. Register only if explicitly enabled.
217 ExtensionService
* extension_service
= extension_system_
->extension_service();
218 if (extension_service
) {
219 OptInIntoBookmarksExperimentIfHasExtension(
220 extension_service
->extensions()->GetIDs());
222 if (IsEnableSyncArticlesSet()) {
223 pss
->RegisterDataTypeController(
224 new UIDataTypeController(syncer::ARTICLES
, this, profile_
, pss
));
227 #if defined(ENABLE_MANAGED_USERS)
228 if (profile_
->IsManaged()) {
229 pss
->RegisterDataTypeController(
230 new UIDataTypeController(
231 syncer::MANAGED_USER_SETTINGS
, this, profile_
, pss
));
233 pss
->RegisterDataTypeController(
234 new UIDataTypeController(
235 syncer::MANAGED_USERS
, this, profile_
, pss
));
237 // TODO(bauerb): Enable once the Sync server has been updated to know about
239 // pss->RegisterDataTypeController(
240 // new UIDataTypeController(
241 // syncer::MANAGED_USER_SHARED_SETTINGS, this, profile_, pss));
245 void ProfileSyncComponentsFactoryImpl::RegisterDesktopDataTypes(
246 ProfileSyncService
* pss
) {
247 // App sync is enabled by default. Register unless explicitly
249 if (!command_line_
->HasSwitch(switches::kDisableSyncApps
)) {
250 pss
->RegisterDataTypeController(
251 new ExtensionDataTypeController(syncer::APPS
, this, profile_
, pss
));
254 // Extension sync is enabled by default. Register unless explicitly
256 if (!command_line_
->HasSwitch(switches::kDisableSyncExtensions
)) {
257 pss
->RegisterDataTypeController(
258 new ExtensionDataTypeController(syncer::EXTENSIONS
,
259 this, profile_
, pss
));
262 // Preference sync is enabled by default. Register unless explicitly
264 if (!command_line_
->HasSwitch(switches::kDisableSyncPreferences
)) {
265 pss
->RegisterDataTypeController(
266 new UIDataTypeController(syncer::PREFERENCES
, this, profile_
, pss
));
270 if (!command_line_
->HasSwitch(switches::kDisableSyncPriorityPreferences
)) {
271 pss
->RegisterDataTypeController(
272 new UIDataTypeController(syncer::PRIORITY_PREFERENCES
,
273 this, profile_
, pss
));
276 #if defined(ENABLE_THEMES)
277 // Theme sync is enabled by default. Register unless explicitly disabled.
278 if (!command_line_
->HasSwitch(switches::kDisableSyncThemes
)) {
279 pss
->RegisterDataTypeController(
280 new ThemeDataTypeController(this, profile_
, pss
));
284 // Search Engine sync is enabled by default. Register unless explicitly
286 if (!command_line_
->HasSwitch(switches::kDisableSyncSearchEngines
)) {
287 pss
->RegisterDataTypeController(
288 new SearchEngineDataTypeController(this, profile_
, pss
));
291 // Extension setting sync is enabled by default. Register unless explicitly
293 if (!command_line_
->HasSwitch(switches::kDisableSyncExtensionSettings
)) {
294 pss
->RegisterDataTypeController(
295 new ExtensionSettingDataTypeController(
296 syncer::EXTENSION_SETTINGS
, this, profile_
, pss
));
299 // App setting sync is enabled by default. Register unless explicitly
301 if (!command_line_
->HasSwitch(switches::kDisableSyncAppSettings
)) {
302 pss
->RegisterDataTypeController(
303 new ExtensionSettingDataTypeController(
304 syncer::APP_SETTINGS
, this, profile_
, pss
));
307 #if defined(ENABLE_APP_LIST)
308 // App List sync is disabled by default. Register only if enabled.
309 if (command_line_
->HasSwitch(switches::kEnableSyncAppList
)) {
310 pss
->RegisterDataTypeController(
311 new UIDataTypeController(syncer::APP_LIST
, this, profile_
, pss
));
315 // Synced Notifications are enabled by default.
316 pss
->RegisterDataTypeController(
317 new UIDataTypeController(
318 syncer::SYNCED_NOTIFICATIONS
, this, profile_
, pss
));
320 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_CHROMEOS)
321 // Dictionary sync is enabled by default.
322 if (!command_line_
->HasSwitch(switches::kDisableSyncDictionary
)) {
323 pss
->RegisterDataTypeController(
324 new UIDataTypeController(syncer::DICTIONARY
, this, profile_
, pss
));
329 DataTypeManager
* ProfileSyncComponentsFactoryImpl::CreateDataTypeManager(
330 const syncer::WeakHandle
<syncer::DataTypeDebugInfoListener
>&
332 const DataTypeController::TypeMap
* controllers
,
333 const browser_sync::DataTypeEncryptionHandler
* encryption_handler
,
334 SyncBackendHost
* backend
,
335 DataTypeManagerObserver
* observer
,
336 browser_sync::FailedDataTypesHandler
* failed_data_types_handler
) {
337 return new DataTypeManagerImpl(debug_info_listener
,
342 failed_data_types_handler
);
345 browser_sync::SyncBackendHost
*
346 ProfileSyncComponentsFactoryImpl::CreateSyncBackendHost(
347 const std::string
& name
,
349 const base::WeakPtr
<browser_sync::SyncPrefs
>& sync_prefs
) {
350 return new browser_sync::SyncBackendHostImpl(name
, profile
, sync_prefs
);
353 browser_sync::GenericChangeProcessor
*
354 ProfileSyncComponentsFactoryImpl::CreateGenericChangeProcessor(
355 ProfileSyncService
* profile_sync_service
,
356 browser_sync::DataTypeErrorHandler
* error_handler
,
357 const base::WeakPtr
<syncer::SyncableService
>& local_service
,
358 const base::WeakPtr
<syncer::SyncMergeResult
>& merge_result
) {
359 syncer::UserShare
* user_share
= profile_sync_service
->GetUserShare();
360 return new GenericChangeProcessor(error_handler
,
366 browser_sync::SharedChangeProcessor
* ProfileSyncComponentsFactoryImpl::
367 CreateSharedChangeProcessor() {
368 return new SharedChangeProcessor();
371 base::WeakPtr
<syncer::SyncableService
> ProfileSyncComponentsFactoryImpl::
372 GetSyncableServiceForType(syncer::ModelType type
) {
373 if (!profile_
) { // For tests.
374 return base::WeakPtr
<syncer::SyncableService
>();
377 case syncer::PREFERENCES
:
378 return PrefServiceSyncable::FromProfile(
379 profile_
)->GetSyncableService(syncer::PREFERENCES
)->AsWeakPtr();
380 case syncer::PRIORITY_PREFERENCES
:
381 return PrefServiceSyncable::FromProfile(profile_
)->GetSyncableService(
382 syncer::PRIORITY_PREFERENCES
)->AsWeakPtr();
383 case syncer::AUTOFILL
:
384 case syncer::AUTOFILL_PROFILE
: {
385 if (!web_data_service_
.get())
386 return base::WeakPtr
<syncer::SyncableService
>();
387 if (type
== syncer::AUTOFILL
) {
388 return AutocompleteSyncableService::FromWebDataService(
389 web_data_service_
.get())->AsWeakPtr();
391 return autofill::AutofillProfileSyncableService::FromWebDataService(
392 web_data_service_
.get())->AsWeakPtr();
396 case syncer::EXTENSIONS
:
397 return ExtensionSyncService::Get(profile_
)->AsWeakPtr();
398 case syncer::SEARCH_ENGINES
:
399 return TemplateURLServiceFactory::GetForProfile(profile_
)->AsWeakPtr();
400 case syncer::APP_SETTINGS
:
401 case syncer::EXTENSION_SETTINGS
:
402 return extension_system_
->extension_service()->settings_frontend()->
403 GetBackendForSync(type
)->AsWeakPtr();
404 #if defined(ENABLE_APP_LIST)
405 case syncer::APP_LIST
:
406 return app_list::AppListSyncableServiceFactory::GetForProfile(profile_
)->
409 #if defined(ENABLE_THEMES)
411 return ThemeServiceFactory::GetForProfile(profile_
)->
412 GetThemeSyncableService()->AsWeakPtr();
414 case syncer::HISTORY_DELETE_DIRECTIVES
: {
415 HistoryService
* history
=
416 HistoryServiceFactory::GetForProfile(
417 profile_
, Profile::EXPLICIT_ACCESS
);
418 return history
? history
->AsWeakPtr() : base::WeakPtr
<HistoryService
>();
420 #if !defined(OS_ANDROID)
421 case syncer::SYNCED_NOTIFICATIONS
: {
422 notifier::ChromeNotifierService
* notifier_service
=
423 notifier::ChromeNotifierServiceFactory::GetForProfile(
424 profile_
, Profile::EXPLICIT_ACCESS
);
425 return notifier_service
? notifier_service
->AsWeakPtr()
426 : base::WeakPtr
<syncer::SyncableService
>();
429 #if defined(ENABLE_SPELLCHECK)
430 case syncer::DICTIONARY
:
431 return SpellcheckServiceFactory::GetForContext(profile_
)->
432 GetCustomDictionary()->AsWeakPtr();
434 case syncer::FAVICON_IMAGES
:
435 case syncer::FAVICON_TRACKING
: {
436 browser_sync::FaviconCache
* favicons
=
437 ProfileSyncServiceFactory::GetForProfile(profile_
)->
439 return favicons
? favicons
->AsWeakPtr()
440 : base::WeakPtr
<syncer::SyncableService
>();
442 #if defined(ENABLE_MANAGED_USERS)
443 case syncer::MANAGED_USER_SETTINGS
:
444 return ManagedUserSettingsServiceFactory::GetForProfile(profile_
)->
446 case syncer::MANAGED_USERS
:
447 return ManagedUserSyncServiceFactory::GetForProfile(profile_
)->
449 case syncer::MANAGED_USER_SHARED_SETTINGS
:
450 return ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(
451 profile_
)->AsWeakPtr();
453 case syncer::ARTICLES
: {
454 dom_distiller::DomDistillerService
* service
=
455 dom_distiller::DomDistillerServiceFactory::GetForBrowserContext(
458 return service
->GetSyncableService()->AsWeakPtr();
459 return base::WeakPtr
<syncer::SyncableService
>();
461 case syncer::SESSIONS
: {
462 DCHECK(command_line_
->HasSwitch(switches::kEnableSyncSessionsV2
));
463 return ProfileSyncServiceFactory::GetForProfile(profile_
)->
464 GetSessionsSyncableService()->AsWeakPtr();
467 // The following datatypes still need to be transitioned to the
468 // syncer::SyncableService API:
473 return base::WeakPtr
<syncer::SyncableService
>();
477 ProfileSyncComponentsFactory::SyncComponents
478 ProfileSyncComponentsFactoryImpl::CreateBookmarkSyncComponents(
479 ProfileSyncService
* profile_sync_service
,
480 DataTypeErrorHandler
* error_handler
) {
481 BookmarkModel
* bookmark_model
=
482 BookmarkModelFactory::GetForProfile(profile_sync_service
->profile());
483 syncer::UserShare
* user_share
= profile_sync_service
->GetUserShare();
484 // TODO(akalin): We may want to propagate this switch up eventually.
485 #if defined(OS_ANDROID)
486 const bool kExpectMobileBookmarksFolder
= true;
488 const bool kExpectMobileBookmarksFolder
= false;
490 BookmarkModelAssociator
* model_associator
=
491 new BookmarkModelAssociator(bookmark_model
,
492 profile_sync_service
->profile(),
495 kExpectMobileBookmarksFolder
);
496 BookmarkChangeProcessor
* change_processor
=
497 new BookmarkChangeProcessor(model_associator
,
499 return SyncComponents(model_associator
, change_processor
);
502 ProfileSyncComponentsFactory::SyncComponents
503 ProfileSyncComponentsFactoryImpl::CreatePasswordSyncComponents(
504 ProfileSyncService
* profile_sync_service
,
505 PasswordStore
* password_store
,
506 DataTypeErrorHandler
* error_handler
) {
507 PasswordModelAssociator
* model_associator
=
508 new PasswordModelAssociator(profile_sync_service
,
511 PasswordChangeProcessor
* change_processor
=
512 new PasswordChangeProcessor(model_associator
,
515 return SyncComponents(model_associator
, change_processor
);
518 ProfileSyncComponentsFactory::SyncComponents
519 ProfileSyncComponentsFactoryImpl::CreateTypedUrlSyncComponents(
520 ProfileSyncService
* profile_sync_service
,
521 history::HistoryBackend
* history_backend
,
522 browser_sync::DataTypeErrorHandler
* error_handler
) {
523 TypedUrlModelAssociator
* model_associator
=
524 new TypedUrlModelAssociator(profile_sync_service
,
527 TypedUrlChangeProcessor
* change_processor
=
528 new TypedUrlChangeProcessor(profile_
,
532 return SyncComponents(model_associator
, change_processor
);
535 ProfileSyncComponentsFactory::SyncComponents
536 ProfileSyncComponentsFactoryImpl::CreateSessionSyncComponents(
537 ProfileSyncService
* profile_sync_service
,
538 DataTypeErrorHandler
* error_handler
) {
539 SessionModelAssociator
* model_associator
=
540 new SessionModelAssociator(profile_sync_service
, error_handler
);
541 SessionChangeProcessor
* change_processor
=
542 new SessionChangeProcessor(error_handler
, model_associator
);
543 return SyncComponents(model_associator
, change_processor
);