// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "base/command_line.h" #include "build/build_config.h" #include "chrome/browser/extensions/app_notification_manager.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_system.h" #include "chrome/browser/extensions/extension_system_factory.h" #include "chrome/browser/extensions/settings/settings_frontend.h" #include "chrome/browser/prefs/pref_model_associator.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search_engines/template_url_service.h" #include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/browser/signin/signin_manager.h" #include "chrome/browser/signin/signin_manager_factory.h" #include "chrome/browser/sync/api/syncable_service.h" #include "chrome/browser/sync/glue/app_notification_data_type_controller.h" #include "chrome/browser/sync/glue/autofill_data_type_controller.h" #include "chrome/browser/sync/glue/autofill_profile_data_type_controller.h" #include "chrome/browser/sync/glue/bookmark_change_processor.h" #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" #include "chrome/browser/sync/glue/bookmark_model_associator.h" #include "chrome/browser/sync/glue/data_type_manager_impl.h" #include "chrome/browser/sync/glue/extension_data_type_controller.h" #include "chrome/browser/sync/glue/extension_setting_data_type_controller.h" #include "chrome/browser/sync/glue/generic_change_processor.h" #include "chrome/browser/sync/glue/password_change_processor.h" #include "chrome/browser/sync/glue/password_data_type_controller.h" #include "chrome/browser/sync/glue/password_model_associator.h" #include "chrome/browser/sync/glue/search_engine_data_type_controller.h" #include "chrome/browser/sync/glue/session_change_processor.h" #include "chrome/browser/sync/glue/session_data_type_controller.h" #include "chrome/browser/sync/glue/session_model_associator.h" #include "chrome/browser/sync/glue/shared_change_processor.h" #include "chrome/browser/sync/glue/sync_backend_host.h" #include "chrome/browser/sync/glue/theme_change_processor.h" #include "chrome/browser/sync/glue/theme_data_type_controller.h" #include "chrome/browser/sync/glue/theme_model_associator.h" #include "chrome/browser/sync/glue/typed_url_change_processor.h" #include "chrome/browser/sync/glue/typed_url_data_type_controller.h" #include "chrome/browser/sync/glue/typed_url_model_associator.h" #include "chrome/browser/sync/glue/ui_data_type_controller.h" #include "chrome/browser/sync/profile_sync_components_factory_impl.h" #include "chrome/browser/sync/profile_sync_service.h" #include "chrome/browser/webdata/autocomplete_syncable_service.h" #include "chrome/browser/webdata/autofill_profile_syncable_service.h" #include "chrome/browser/webdata/web_data_service.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/pref_names.h" #include "content/public/browser/browser_thread.h" using browser_sync::AppNotificationDataTypeController; using browser_sync::AutofillDataTypeController; using browser_sync::AutofillProfileDataTypeController; using browser_sync::BookmarkChangeProcessor; using browser_sync::BookmarkDataTypeController; using browser_sync::BookmarkModelAssociator; using browser_sync::DataTypeController; using browser_sync::DataTypeManager; using browser_sync::DataTypeManagerImpl; using browser_sync::ExtensionDataTypeController; using browser_sync::ExtensionSettingDataTypeController; using browser_sync::GenericChangeProcessor; using browser_sync::PasswordChangeProcessor; using browser_sync::PasswordDataTypeController; using browser_sync::PasswordModelAssociator; using browser_sync::SearchEngineDataTypeController; using browser_sync::SessionChangeProcessor; using browser_sync::SessionDataTypeController; using browser_sync::SessionModelAssociator; using browser_sync::SharedChangeProcessor; using browser_sync::SyncBackendHost; using browser_sync::ThemeChangeProcessor; using browser_sync::ThemeDataTypeController; using browser_sync::ThemeModelAssociator; using browser_sync::TypedUrlChangeProcessor; using browser_sync::TypedUrlDataTypeController; using browser_sync::TypedUrlModelAssociator; using browser_sync::UIDataTypeController; using browser_sync::DataTypeErrorHandler; using content::BrowserThread; ProfileSyncComponentsFactoryImpl::ProfileSyncComponentsFactoryImpl( Profile* profile, CommandLine* command_line) : profile_(profile), command_line_(command_line), extension_system_( ExtensionSystem::Get(profile)) { } void ProfileSyncComponentsFactoryImpl::RegisterDataTypes( ProfileSyncService* pss) { // App sync is enabled by default. Register unless explicitly // disabled. if (!command_line_->HasSwitch(switches::kDisableSyncApps)) { pss->RegisterDataTypeController( new ExtensionDataTypeController(syncable::APPS, this, profile_, pss)); } // Autofill sync is enabled by default. Register unless explicitly // disabled. if (!command_line_->HasSwitch(switches::kDisableSyncAutofill)) { pss->RegisterDataTypeController( new AutofillDataTypeController(this, profile_, pss)); } // Bookmark sync is enabled by default. Register unless explicitly // disabled. if (!command_line_->HasSwitch(switches::kDisableSyncBookmarks)) { pss->RegisterDataTypeController( new BookmarkDataTypeController(this, profile_, pss)); } // Extension sync is enabled by default. Register unless explicitly // disabled. if (!command_line_->HasSwitch(switches::kDisableSyncExtensions)) { pss->RegisterDataTypeController( new ExtensionDataTypeController(syncable::EXTENSIONS, this, profile_, pss)); } // Password sync is enabled by default. Register unless explicitly // disabled. if (!command_line_->HasSwitch(switches::kDisableSyncPasswords)) { pss->RegisterDataTypeController( new PasswordDataTypeController(this, profile_, pss)); } // Preference sync is enabled by default. Register unless explicitly // disabled. if (!command_line_->HasSwitch(switches::kDisableSyncPreferences)) { pss->RegisterDataTypeController( new UIDataTypeController(syncable::PREFERENCES, this, profile_, pss)); } #if defined(ENABLE_THEMES) // Theme sync is enabled by default. Register unless explicitly disabled. if (!command_line_->HasSwitch(switches::kDisableSyncThemes)) { pss->RegisterDataTypeController( new ThemeDataTypeController(this, profile_, pss)); } #endif // TypedUrl sync is enabled by default. Register unless explicitly disabled, // or if saving history is disabled. if (!profile_->GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled) && !command_line_->HasSwitch(switches::kDisableSyncTypedUrls)) { pss->RegisterDataTypeController( new TypedUrlDataTypeController(this, profile_, pss)); } // Search Engine sync is enabled by default. Register only if explicitly // disabled. if (!command_line_->HasSwitch(switches::kDisableSyncSearchEngines)) { pss->RegisterDataTypeController( new SearchEngineDataTypeController(this, profile_, pss)); } // Session sync is enabled by default. Register unless explicitly disabled. // TODO(dubroy): Once this change is permanent, clean up the logic for // explicitly enabling tab sync (http://crbug.com/118570). if (!command_line_->HasSwitch(switches::kDisableSyncTabs)) { pss->RegisterDataTypeController( new SessionDataTypeController(this, profile_, pss)); } // Extension setting sync is enabled by default. Register unless explicitly // disabled. if (!command_line_->HasSwitch(switches::kDisableSyncExtensionSettings)) { pss->RegisterDataTypeController( new ExtensionSettingDataTypeController( syncable::EXTENSION_SETTINGS, this, profile_, pss)); } // App setting sync is enabled by default. Register unless explicitly // disabled. if (!command_line_->HasSwitch(switches::kDisableSyncAppSettings)) { pss->RegisterDataTypeController( new ExtensionSettingDataTypeController( syncable::APP_SETTINGS, this, profile_, pss)); } if (!command_line_->HasSwitch(switches::kDisableSyncAutofillProfile)) { pss->RegisterDataTypeController( new AutofillProfileDataTypeController(this, profile_, pss)); } // App notifications sync is enabled by default. Register only if // explicitly disabled. if (!command_line_->HasSwitch(switches::kDisableSyncAppNotifications)) { pss->RegisterDataTypeController( new AppNotificationDataTypeController(this, profile_, pss)); } } DataTypeManager* ProfileSyncComponentsFactoryImpl::CreateDataTypeManager( SyncBackendHost* backend, const DataTypeController::TypeMap* controllers) { return new DataTypeManagerImpl(backend, controllers); } browser_sync::GenericChangeProcessor* ProfileSyncComponentsFactoryImpl::CreateGenericChangeProcessor( ProfileSyncService* profile_sync_service, browser_sync::DataTypeErrorHandler* error_handler, const base::WeakPtr& local_service) { sync_api::UserShare* user_share = profile_sync_service->GetUserShare(); return new GenericChangeProcessor(error_handler, local_service, user_share); } browser_sync::SharedChangeProcessor* ProfileSyncComponentsFactoryImpl:: CreateSharedChangeProcessor() { return new SharedChangeProcessor(); } base::WeakPtr ProfileSyncComponentsFactoryImpl:: GetSyncableServiceForType(syncable::ModelType type) { if (!profile_) { // For tests. return base::WeakPtr(); } switch (type) { case syncable::PREFERENCES: return profile_->GetPrefs()->GetSyncableService()->AsWeakPtr(); case syncable::AUTOFILL: case syncable::AUTOFILL_PROFILE: { WebDataService* wds = profile_->GetWebDataService(Profile::IMPLICIT_ACCESS); if (!wds) return base::WeakPtr(); if (type == syncable::AUTOFILL) return wds->GetAutocompleteSyncableService()->AsWeakPtr(); else return wds->GetAutofillProfileSyncableService()->AsWeakPtr(); } case syncable::APPS: case syncable::EXTENSIONS: return extension_system_->extension_service()->AsWeakPtr(); case syncable::SEARCH_ENGINES: return TemplateURLServiceFactory::GetForProfile(profile_)->AsWeakPtr(); case syncable::APP_SETTINGS: case syncable::EXTENSION_SETTINGS: return extension_system_->extension_service()->settings_frontend()-> GetBackendForSync(type)->AsWeakPtr(); case syncable::APP_NOTIFICATIONS: return extension_system_->extension_service()-> app_notification_manager()->AsWeakPtr(); default: // The following datatypes still need to be transitioned to the // SyncableService API: // Bookmarks // Passwords // Sessions // Themes // Typed URLs NOTREACHED(); return base::WeakPtr(); } } ProfileSyncComponentsFactory::SyncComponents ProfileSyncComponentsFactoryImpl::CreateBookmarkSyncComponents( ProfileSyncService* profile_sync_service, DataTypeErrorHandler* error_handler) { BookmarkModel* bookmark_model = profile_sync_service->profile()->GetBookmarkModel(); sync_api::UserShare* user_share = profile_sync_service->GetUserShare(); // TODO(akalin): We may want to propagate this switch up eventually. #if defined(OS_ANDROID) const bool kExpectMobileBookmarksFolder = true; #else const bool kExpectMobileBookmarksFolder = false; #endif BookmarkModelAssociator* model_associator = new BookmarkModelAssociator(bookmark_model, user_share, error_handler, kExpectMobileBookmarksFolder); BookmarkChangeProcessor* change_processor = new BookmarkChangeProcessor(model_associator, error_handler); return SyncComponents(model_associator, change_processor); } ProfileSyncComponentsFactory::SyncComponents ProfileSyncComponentsFactoryImpl::CreatePasswordSyncComponents( ProfileSyncService* profile_sync_service, PasswordStore* password_store, DataTypeErrorHandler* error_handler) { PasswordModelAssociator* model_associator = new PasswordModelAssociator(profile_sync_service, password_store); PasswordChangeProcessor* change_processor = new PasswordChangeProcessor(model_associator, password_store, error_handler); return SyncComponents(model_associator, change_processor); } #if defined(ENABLE_THEMES) ProfileSyncComponentsFactory::SyncComponents ProfileSyncComponentsFactoryImpl::CreateThemeSyncComponents( ProfileSyncService* profile_sync_service, DataTypeErrorHandler* error_handler) { ThemeModelAssociator* model_associator = new ThemeModelAssociator(profile_sync_service); ThemeChangeProcessor* change_processor = new ThemeChangeProcessor(error_handler); return SyncComponents(model_associator, change_processor); } #endif ProfileSyncComponentsFactory::SyncComponents ProfileSyncComponentsFactoryImpl::CreateTypedUrlSyncComponents( ProfileSyncService* profile_sync_service, history::HistoryBackend* history_backend, browser_sync::DataTypeErrorHandler* error_handler) { TypedUrlModelAssociator* model_associator = new TypedUrlModelAssociator(profile_sync_service, history_backend); TypedUrlChangeProcessor* change_processor = new TypedUrlChangeProcessor(profile_, model_associator, history_backend, error_handler); return SyncComponents(model_associator, change_processor); } ProfileSyncComponentsFactory::SyncComponents ProfileSyncComponentsFactoryImpl::CreateSessionSyncComponents( ProfileSyncService* profile_sync_service, DataTypeErrorHandler* error_handler) { SessionModelAssociator* model_associator = new SessionModelAssociator(profile_sync_service); SessionChangeProcessor* change_processor = new SessionChangeProcessor(error_handler, model_associator); return SyncComponents(model_associator, change_processor); }