Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / sync / profile_sync_components_factory_impl.cc
blob3db66fa0fadc98e32c558d79eb21eae0930eb064
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/bookmarks/bookmark_model_factory.h"
8 #include "chrome/browser/extensions/app_notification_manager.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_system.h"
11 #include "chrome/browser/extensions/extension_system_factory.h"
12 #include "chrome/browser/extensions/settings/settings_frontend.h"
13 #include "chrome/browser/history/history.h"
14 #include "chrome/browser/history/history_service_factory.h"
15 #include "chrome/browser/prefs/pref_model_associator.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/search_engines/template_url_service.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/browser/signin/signin_manager.h"
20 #include "chrome/browser/signin/signin_manager_factory.h"
21 #include "chrome/browser/sync/glue/app_notification_data_type_controller.h"
22 #include "chrome/browser/sync/glue/autofill_data_type_controller.h"
23 #include "chrome/browser/sync/glue/autofill_profile_data_type_controller.h"
24 #include "chrome/browser/sync/glue/bookmark_change_processor.h"
25 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h"
26 #include "chrome/browser/sync/glue/bookmark_model_associator.h"
27 #include "chrome/browser/sync/glue/data_type_manager_impl.h"
28 #include "chrome/browser/sync/glue/data_type_manager_observer.h"
29 #include "chrome/browser/sync/glue/extension_data_type_controller.h"
30 #include "chrome/browser/sync/glue/extension_setting_data_type_controller.h"
31 #include "chrome/browser/sync/glue/generic_change_processor.h"
32 #include "chrome/browser/sync/glue/password_change_processor.h"
33 #include "chrome/browser/sync/glue/password_data_type_controller.h"
34 #include "chrome/browser/sync/glue/password_model_associator.h"
35 #include "chrome/browser/sync/glue/search_engine_data_type_controller.h"
36 #include "chrome/browser/sync/glue/session_change_processor.h"
37 #include "chrome/browser/sync/glue/session_data_type_controller.h"
38 #include "chrome/browser/sync/glue/session_model_associator.h"
39 #include "chrome/browser/sync/glue/shared_change_processor.h"
40 #include "chrome/browser/sync/glue/sync_backend_host.h"
41 #include "chrome/browser/sync/glue/theme_data_type_controller.h"
42 #include "chrome/browser/sync/glue/typed_url_change_processor.h"
43 #include "chrome/browser/sync/glue/typed_url_data_type_controller.h"
44 #include "chrome/browser/sync/glue/typed_url_model_associator.h"
45 #include "chrome/browser/sync/glue/ui_data_type_controller.h"
46 #include "chrome/browser/sync/profile_sync_components_factory_impl.h"
47 #include "chrome/browser/sync/profile_sync_service.h"
48 #include "chrome/browser/themes/theme_service_factory.h"
49 #include "chrome/browser/themes/theme_service.h"
50 #include "chrome/browser/themes/theme_syncable_service.h"
51 #include "chrome/browser/webdata/autocomplete_syncable_service.h"
52 #include "chrome/browser/webdata/autofill_profile_syncable_service.h"
53 #include "chrome/browser/webdata/web_data_service.h"
54 #include "chrome/browser/webdata/web_data_service_factory.h"
55 #include "chrome/common/chrome_switches.h"
56 #include "chrome/common/pref_names.h"
57 #include "content/public/browser/browser_thread.h"
58 #include "sync/api/syncable_service.h"
60 using browser_sync::AppNotificationDataTypeController;
61 using browser_sync::AutofillDataTypeController;
62 using browser_sync::AutofillProfileDataTypeController;
63 using browser_sync::BookmarkChangeProcessor;
64 using browser_sync::BookmarkDataTypeController;
65 using browser_sync::BookmarkModelAssociator;
66 using browser_sync::DataTypeController;
67 using browser_sync::DataTypeManager;
68 using browser_sync::DataTypeManagerImpl;
69 using browser_sync::DataTypeManagerObserver;
70 using browser_sync::ExtensionDataTypeController;
71 using browser_sync::ExtensionSettingDataTypeController;
72 using browser_sync::GenericChangeProcessor;
73 using browser_sync::PasswordChangeProcessor;
74 using browser_sync::PasswordDataTypeController;
75 using browser_sync::PasswordModelAssociator;
76 using browser_sync::SearchEngineDataTypeController;
77 using browser_sync::SessionChangeProcessor;
78 using browser_sync::SessionDataTypeController;
79 using browser_sync::SessionModelAssociator;
80 using browser_sync::SharedChangeProcessor;
81 using browser_sync::SyncBackendHost;
82 using browser_sync::ThemeDataTypeController;
83 using browser_sync::TypedUrlChangeProcessor;
84 using browser_sync::TypedUrlDataTypeController;
85 using browser_sync::TypedUrlModelAssociator;
86 using browser_sync::UIDataTypeController;
87 using browser_sync::DataTypeErrorHandler;
88 using content::BrowserThread;
90 ProfileSyncComponentsFactoryImpl::ProfileSyncComponentsFactoryImpl(
91 Profile* profile, CommandLine* command_line)
92 : profile_(profile),
93 command_line_(command_line),
94 extension_system_(
95 extensions::ExtensionSystemFactory::GetForProfile(profile)),
96 web_data_service_(WebDataServiceFactory::GetForProfile(
97 profile_, Profile::IMPLICIT_ACCESS)) {
100 ProfileSyncComponentsFactoryImpl::~ProfileSyncComponentsFactoryImpl() {
103 void ProfileSyncComponentsFactoryImpl::RegisterDataTypes(
104 ProfileSyncService* pss) {
105 RegisterCommonDataTypes(pss);
106 #if !defined(OS_ANDROID)
107 RegisterDesktopDataTypes(pss);
108 #endif
111 void ProfileSyncComponentsFactoryImpl::RegisterCommonDataTypes(
112 ProfileSyncService* pss) {
113 // Bookmark sync is enabled by default. Register unless explicitly
114 // disabled.
115 if (!command_line_->HasSwitch(switches::kDisableSyncBookmarks)) {
116 pss->RegisterDataTypeController(
117 new BookmarkDataTypeController(this, profile_, pss));
120 // TypedUrl sync is enabled by default. Register unless explicitly disabled,
121 // or if saving history is disabled.
122 if (!profile_->GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled) &&
123 !command_line_->HasSwitch(switches::kDisableSyncTypedUrls)) {
124 pss->RegisterDataTypeController(
125 new TypedUrlDataTypeController(this, profile_, pss));
128 // Session sync is enabled by default. Register unless explicitly disabled.
129 if (!command_line_->HasSwitch(switches::kDisableSyncTabs)) {
130 pss->RegisterDataTypeController(
131 new SessionDataTypeController(this, profile_, pss));
135 void ProfileSyncComponentsFactoryImpl::RegisterDesktopDataTypes(
136 ProfileSyncService* pss) {
137 // App sync is enabled by default. Register unless explicitly
138 // disabled.
139 if (!command_line_->HasSwitch(switches::kDisableSyncApps)) {
140 pss->RegisterDataTypeController(
141 new ExtensionDataTypeController(syncer::APPS, this, profile_, pss));
144 // Autofill sync is enabled by default. Register unless explicitly
145 // disabled.
146 if (!command_line_->HasSwitch(switches::kDisableSyncAutofill)) {
147 pss->RegisterDataTypeController(
148 new AutofillDataTypeController(this, profile_, pss));
151 // Extension sync is enabled by default. Register unless explicitly
152 // disabled.
153 if (!command_line_->HasSwitch(switches::kDisableSyncExtensions)) {
154 pss->RegisterDataTypeController(
155 new ExtensionDataTypeController(syncer::EXTENSIONS,
156 this, profile_, pss));
159 // Password sync is enabled by default. Register unless explicitly
160 // disabled.
161 if (!command_line_->HasSwitch(switches::kDisableSyncPasswords)) {
162 pss->RegisterDataTypeController(
163 new PasswordDataTypeController(this, profile_, pss));
166 // Preference sync is enabled by default. Register unless explicitly
167 // disabled.
168 if (!command_line_->HasSwitch(switches::kDisableSyncPreferences)) {
169 pss->RegisterDataTypeController(
170 new UIDataTypeController(syncer::PREFERENCES, this, profile_, pss));
173 #if defined(ENABLE_THEMES)
174 // Theme sync is enabled by default. Register unless explicitly disabled.
175 if (!command_line_->HasSwitch(switches::kDisableSyncThemes)) {
176 pss->RegisterDataTypeController(
177 new ThemeDataTypeController(this, profile_, pss));
179 #endif
181 // Search Engine sync is enabled by default. Register unless explicitly
182 // disabled.
183 if (!command_line_->HasSwitch(switches::kDisableSyncSearchEngines)) {
184 pss->RegisterDataTypeController(
185 new SearchEngineDataTypeController(this, profile_, pss));
188 // Extension setting sync is enabled by default. Register unless explicitly
189 // disabled.
190 if (!command_line_->HasSwitch(switches::kDisableSyncExtensionSettings)) {
191 pss->RegisterDataTypeController(
192 new ExtensionSettingDataTypeController(
193 syncer::EXTENSION_SETTINGS, this, profile_, pss));
196 // App setting sync is enabled by default. Register unless explicitly
197 // disabled.
198 if (!command_line_->HasSwitch(switches::kDisableSyncAppSettings)) {
199 pss->RegisterDataTypeController(
200 new ExtensionSettingDataTypeController(
201 syncer::APP_SETTINGS, this, profile_, pss));
204 if (!command_line_->HasSwitch(switches::kDisableSyncAutofillProfile)) {
205 pss->RegisterDataTypeController(
206 new AutofillProfileDataTypeController(this, profile_, pss));
209 // App notifications sync is enabled by default. Register unless explicitly
210 // disabled.
211 if (!command_line_->HasSwitch(switches::kDisableSyncAppNotifications)) {
212 pss->RegisterDataTypeController(
213 new AppNotificationDataTypeController(this, profile_, pss));
216 // History delete directives sync is disabled by default. Register only if
217 // explicitly enabled.
218 if (command_line_->HasSwitch(switches::kEnableSyncHistoryDeleteDirectives)) {
219 pss->RegisterDataTypeController(
220 new UIDataTypeController(
221 syncer::HISTORY_DELETE_DIRECTIVES, this, profile_, pss));
225 DataTypeManager* ProfileSyncComponentsFactoryImpl::CreateDataTypeManager(
226 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>&
227 debug_info_listener,
228 SyncBackendHost* backend,
229 const DataTypeController::TypeMap* controllers,
230 DataTypeManagerObserver* observer) {
231 return new DataTypeManagerImpl(debug_info_listener,
232 backend,
233 controllers,
234 observer);
237 browser_sync::GenericChangeProcessor*
238 ProfileSyncComponentsFactoryImpl::CreateGenericChangeProcessor(
239 ProfileSyncService* profile_sync_service,
240 browser_sync::DataTypeErrorHandler* error_handler,
241 const base::WeakPtr<syncer::SyncableService>& local_service) {
242 syncer::UserShare* user_share = profile_sync_service->GetUserShare();
243 return new GenericChangeProcessor(error_handler,
244 local_service,
245 user_share);
248 browser_sync::SharedChangeProcessor* ProfileSyncComponentsFactoryImpl::
249 CreateSharedChangeProcessor() {
250 return new SharedChangeProcessor();
253 base::WeakPtr<syncer::SyncableService> ProfileSyncComponentsFactoryImpl::
254 GetSyncableServiceForType(syncer::ModelType type) {
255 if (!profile_) { // For tests.
256 return base::WeakPtr<syncer::SyncableService>();
258 switch (type) {
259 case syncer::PREFERENCES:
260 return profile_->GetPrefs()->GetSyncableService()->AsWeakPtr();
261 case syncer::AUTOFILL:
262 case syncer::AUTOFILL_PROFILE: {
263 if (!web_data_service_.get())
264 return base::WeakPtr<syncer::SyncableService>();
265 if (type == syncer::AUTOFILL) {
266 return web_data_service_->GetAutocompleteSyncableService()->AsWeakPtr();
267 } else {
268 return web_data_service_->
269 GetAutofillProfileSyncableService()->AsWeakPtr();
272 case syncer::APPS:
273 case syncer::EXTENSIONS:
274 return extension_system_->extension_service()->AsWeakPtr();
275 case syncer::SEARCH_ENGINES:
276 return TemplateURLServiceFactory::GetForProfile(profile_)->AsWeakPtr();
277 case syncer::APP_SETTINGS:
278 case syncer::EXTENSION_SETTINGS:
279 return extension_system_->extension_service()->settings_frontend()->
280 GetBackendForSync(type)->AsWeakPtr();
281 case syncer::APP_NOTIFICATIONS:
282 return extension_system_->extension_service()->
283 app_notification_manager()->AsWeakPtr();
284 #if defined(ENABLE_THEMES)
285 case syncer::THEMES:
286 return ThemeServiceFactory::GetForProfile(profile_)->
287 GetThemeSyncableService()->AsWeakPtr();
288 #endif
289 case syncer::HISTORY_DELETE_DIRECTIVES: {
290 HistoryService* history =
291 HistoryServiceFactory::GetForProfile(
292 profile_, Profile::EXPLICIT_ACCESS);
293 return history ? history->AsWeakPtr() : base::WeakPtr<HistoryService>();
295 default:
296 // The following datatypes still need to be transitioned to the
297 // syncer::SyncableService API:
298 // Bookmarks
299 // Passwords
300 // Sessions
301 // Typed URLs
302 NOTREACHED();
303 return base::WeakPtr<syncer::SyncableService>();
307 ProfileSyncComponentsFactory::SyncComponents
308 ProfileSyncComponentsFactoryImpl::CreateBookmarkSyncComponents(
309 ProfileSyncService* profile_sync_service,
310 DataTypeErrorHandler* error_handler) {
311 BookmarkModel* bookmark_model =
312 BookmarkModelFactory::GetForProfile(profile_sync_service->profile());
313 syncer::UserShare* user_share = profile_sync_service->GetUserShare();
314 // TODO(akalin): We may want to propagate this switch up eventually.
315 #if defined(OS_ANDROID)
316 const bool kExpectMobileBookmarksFolder = true;
317 #else
318 const bool kExpectMobileBookmarksFolder = false;
319 #endif
320 BookmarkModelAssociator* model_associator =
321 new BookmarkModelAssociator(bookmark_model,
322 user_share,
323 error_handler,
324 kExpectMobileBookmarksFolder);
325 BookmarkChangeProcessor* change_processor =
326 new BookmarkChangeProcessor(model_associator,
327 error_handler);
328 return SyncComponents(model_associator, change_processor);
331 ProfileSyncComponentsFactory::SyncComponents
332 ProfileSyncComponentsFactoryImpl::CreatePasswordSyncComponents(
333 ProfileSyncService* profile_sync_service,
334 PasswordStore* password_store,
335 DataTypeErrorHandler* error_handler) {
336 PasswordModelAssociator* model_associator =
337 new PasswordModelAssociator(profile_sync_service,
338 password_store,
339 error_handler);
340 PasswordChangeProcessor* change_processor =
341 new PasswordChangeProcessor(model_associator,
342 password_store,
343 error_handler);
344 return SyncComponents(model_associator, change_processor);
347 ProfileSyncComponentsFactory::SyncComponents
348 ProfileSyncComponentsFactoryImpl::CreateTypedUrlSyncComponents(
349 ProfileSyncService* profile_sync_service,
350 history::HistoryBackend* history_backend,
351 browser_sync::DataTypeErrorHandler* error_handler) {
352 TypedUrlModelAssociator* model_associator =
353 new TypedUrlModelAssociator(profile_sync_service,
354 history_backend,
355 error_handler);
356 TypedUrlChangeProcessor* change_processor =
357 new TypedUrlChangeProcessor(profile_,
358 model_associator,
359 history_backend,
360 error_handler);
361 return SyncComponents(model_associator, change_processor);
364 ProfileSyncComponentsFactory::SyncComponents
365 ProfileSyncComponentsFactoryImpl::CreateSessionSyncComponents(
366 ProfileSyncService* profile_sync_service,
367 DataTypeErrorHandler* error_handler) {
368 SessionModelAssociator* model_associator =
369 new SessionModelAssociator(profile_sync_service, error_handler);
370 SessionChangeProcessor* change_processor =
371 new SessionChangeProcessor(error_handler, model_associator);
372 return SyncComponents(model_associator, change_processor);