1// Copyright (c) 2011 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.
4
5#include "base/command_line.h"
6#include "chrome/browser/extensions/extension_service.h"
7#include "chrome/browser/profiles/profile.h"
8#include "chrome/browser/sync/glue/app_data_type_controller.h"
9#include "chrome/browser/sync/glue/autofill_change_processor.h"
10#include "chrome/browser/sync/glue/autofill_data_type_controller.h"
11#include "chrome/browser/sync/glue/autofill_model_associator.h"
12#include "chrome/browser/sync/glue/autofill_profile_change_processor.h"
13#include "chrome/browser/sync/glue/autofill_profile_data_type_controller.h"
14#include "chrome/browser/sync/glue/autofill_profile_model_associator.h"
15#include "chrome/browser/sync/glue/bookmark_change_processor.h"
16#include "chrome/browser/sync/glue/bookmark_data_type_controller.h"
17#include "chrome/browser/sync/glue/bookmark_model_associator.h"
18#include "chrome/browser/sync/glue/data_type_manager_impl.h"
19#include "chrome/browser/sync/glue/extension_change_processor.h"
20#include "chrome/browser/sync/glue/extension_data_type_controller.h"
21#include "chrome/browser/sync/glue/extension_model_associator.h"
22#include "chrome/browser/sync/glue/extension_sync_traits.h"
23#include "chrome/browser/sync/glue/password_change_processor.h"
24#include "chrome/browser/sync/glue/password_data_type_controller.h"
25#include "chrome/browser/sync/glue/password_model_associator.h"
26#include "chrome/browser/sync/glue/preference_change_processor.h"
27#include "chrome/browser/sync/glue/preference_data_type_controller.h"
28#include "chrome/browser/sync/glue/preference_model_associator.h"
29#include "chrome/browser/sync/glue/session_change_processor.h"
30#include "chrome/browser/sync/glue/session_data_type_controller.h"
31#include "chrome/browser/sync/glue/session_model_associator.h"
32#include "chrome/browser/sync/glue/sync_backend_host.h"
33#include "chrome/browser/sync/glue/theme_change_processor.h"
34#include "chrome/browser/sync/glue/theme_data_type_controller.h"
35#include "chrome/browser/sync/glue/theme_model_associator.h"
36#include "chrome/browser/sync/glue/typed_url_change_processor.h"
37#include "chrome/browser/sync/glue/typed_url_data_type_controller.h"
38#include "chrome/browser/sync/glue/typed_url_model_associator.h"
39#include "chrome/browser/sync/profile_sync_service.h"
40#include "chrome/browser/sync/profile_sync_factory_impl.h"
41#include "chrome/browser/webdata/web_data_service.h"
42#include "chrome/common/chrome_switches.h"
43
44using browser_sync::AppDataTypeController;
45using browser_sync::AutofillChangeProcessor;
46using browser_sync::AutofillProfileChangeProcessor;
47using browser_sync::AutofillDataTypeController;
48using browser_sync::AutofillProfileDataTypeController;
49using browser_sync::AutofillModelAssociator;
50using browser_sync::AutofillProfileModelAssociator;
51using browser_sync::BookmarkChangeProcessor;
52using browser_sync::BookmarkDataTypeController;
53using browser_sync::BookmarkModelAssociator;
54using browser_sync::DataTypeController;
55using browser_sync::DataTypeManager;
56using browser_sync::DataTypeManagerImpl;
57using browser_sync::ExtensionChangeProcessor;
58using browser_sync::ExtensionDataTypeController;
59using browser_sync::ExtensionModelAssociator;
60using browser_sync::PasswordChangeProcessor;
61using browser_sync::PasswordDataTypeController;
62using browser_sync::PasswordModelAssociator;
63using browser_sync::PreferenceChangeProcessor;
64using browser_sync::PreferenceDataTypeController;
65using browser_sync::PreferenceModelAssociator;
66using browser_sync::SessionChangeProcessor;
67using browser_sync::SessionDataTypeController;
68using browser_sync::SessionModelAssociator;
69using browser_sync::SyncBackendHost;
70using browser_sync::ThemeChangeProcessor;
71using browser_sync::ThemeDataTypeController;
72using browser_sync::ThemeModelAssociator;
73using browser_sync::TypedUrlChangeProcessor;
74using browser_sync::TypedUrlDataTypeController;
75using browser_sync::TypedUrlModelAssociator;
76using browser_sync::UnrecoverableErrorHandler;
77
78ProfileSyncFactoryImpl::ProfileSyncFactoryImpl(Profile* profile,
79                                               CommandLine* command_line)
80    : profile_(profile),
81      command_line_(command_line) {
82}
83
84ProfileSyncService* ProfileSyncFactoryImpl::CreateProfileSyncService(
85    const std::string& cros_user) {
86
87  ProfileSyncService* pss = new ProfileSyncService(
88      this, profile_, cros_user);
89
90  // App sync is enabled by default.  Register unless explicitly
91  // disabled.
92  if (!command_line_->HasSwitch(switches::kDisableSyncApps)) {
93    pss->RegisterDataTypeController(
94        new AppDataTypeController(this, profile_, pss));
95  }
96
97  // Autofill sync is enabled by default.  Register unless explicitly
98  // disabled.
99  if (!command_line_->HasSwitch(switches::kDisableSyncAutofill)) {
100    pss->RegisterDataTypeController(
101        new AutofillDataTypeController(this, profile_, pss));
102  }
103
104  // Bookmark sync is enabled by default.  Register unless explicitly
105  // disabled.
106  if (!command_line_->HasSwitch(switches::kDisableSyncBookmarks)) {
107    pss->RegisterDataTypeController(
108        new BookmarkDataTypeController(this, profile_, pss));
109  }
110
111  // Extension sync is enabled by default.  Register unless explicitly
112  // disabled.
113  if (!command_line_->HasSwitch(switches::kDisableSyncExtensions)) {
114    pss->RegisterDataTypeController(
115        new ExtensionDataTypeController(this, profile_, pss));
116  }
117
118  // Password sync is enabled by default.  Register unless explicitly
119  // disabled.
120  if (!command_line_->HasSwitch(switches::kDisableSyncPasswords)) {
121    pss->RegisterDataTypeController(
122        new PasswordDataTypeController(this, profile_, pss));
123  }
124
125  // Preference sync is enabled by default.  Register unless explicitly
126  // disabled.
127  if (!command_line_->HasSwitch(switches::kDisableSyncPreferences)) {
128    pss->RegisterDataTypeController(
129        new PreferenceDataTypeController(this, profile_, pss));
130  }
131
132  // Theme sync is enabled by default.  Register unless explicitly disabled.
133  if (!command_line_->HasSwitch(switches::kDisableSyncThemes)) {
134    pss->RegisterDataTypeController(
135        new ThemeDataTypeController(this, profile_, pss));
136  }
137
138  // TypedUrl sync is disabled by default.  Register only if
139  // explicitly enabled.
140  if (command_line_->HasSwitch(switches::kEnableSyncTypedUrls)) {
141    pss->RegisterDataTypeController(
142        new TypedUrlDataTypeController(this, profile_, pss));
143  }
144
145  // Session sync is disabled by default.  Register only if explicitly
146  // enabled.
147  if (command_line_->HasSwitch(switches::kEnableSyncSessions)) {
148    pss->RegisterDataTypeController(
149        new SessionDataTypeController(this, profile_, pss));
150  }
151
152  if (!command_line_->HasSwitch(switches::kDisableSyncAutofillProfile)) {
153    pss->RegisterDataTypeController(new AutofillProfileDataTypeController(
154        this, profile_, pss));
155  }
156  return pss;
157}
158
159DataTypeManager* ProfileSyncFactoryImpl::CreateDataTypeManager(
160    SyncBackendHost* backend,
161    const DataTypeController::TypeMap& controllers) {
162  return new DataTypeManagerImpl(backend, controllers);
163}
164
165ProfileSyncFactory::SyncComponents
166ProfileSyncFactoryImpl::CreateAppSyncComponents(
167    ProfileSyncService* profile_sync_service,
168    UnrecoverableErrorHandler* error_handler) {
169  browser_sync::ExtensionSyncTraits traits = browser_sync::GetAppSyncTraits();
170  // For now we simply use extensions sync objects with the app sync
171  // traits.  If apps become more than simply extensions, we may have
172  // to write our own apps model associator and/or change processor.
173  ExtensionServiceInterface* extension_service =
174      profile_sync_service->profile()->GetExtensionService();
175  sync_api::UserShare* user_share = profile_sync_service->GetUserShare();
176  ExtensionModelAssociator* model_associator =
177      new ExtensionModelAssociator(traits, extension_service, user_share);
178  ExtensionChangeProcessor* change_processor =
179      new ExtensionChangeProcessor(traits, error_handler);
180  return SyncComponents(model_associator, change_processor);
181}
182
183ProfileSyncFactory::SyncComponents
184ProfileSyncFactoryImpl::CreateAutofillSyncComponents(
185    ProfileSyncService* profile_sync_service,
186    WebDatabase* web_database,
187    PersonalDataManager* personal_data,
188    browser_sync::UnrecoverableErrorHandler* error_handler) {
189
190  AutofillModelAssociator* model_associator =
191      new AutofillModelAssociator(profile_sync_service,
192                                  web_database,
193                                  personal_data);
194  AutofillChangeProcessor* change_processor =
195      new AutofillChangeProcessor(model_associator,
196                                  web_database,
197                                  personal_data,
198                                  error_handler);
199  return SyncComponents(model_associator, change_processor);
200}
201
202ProfileSyncFactory::SyncComponents
203ProfileSyncFactoryImpl::CreateAutofillProfileSyncComponents(
204    ProfileSyncService* profile_sync_service,
205    WebDatabase* web_database,
206    PersonalDataManager* personal_data,
207    browser_sync::UnrecoverableErrorHandler* error_handler) {
208
209  AutofillProfileModelAssociator* model_associator =
210      new AutofillProfileModelAssociator(profile_sync_service,
211                                  web_database,
212                                  personal_data);
213  AutofillProfileChangeProcessor* change_processor =
214      new AutofillProfileChangeProcessor(model_associator,
215                                  web_database,
216                                  personal_data,
217                                  error_handler);
218  return SyncComponents(model_associator, change_processor);
219}
220
221ProfileSyncFactory::SyncComponents
222ProfileSyncFactoryImpl::CreateBookmarkSyncComponents(
223    ProfileSyncService* profile_sync_service,
224    UnrecoverableErrorHandler* error_handler) {
225  BookmarkModel* bookmark_model =
226      profile_sync_service->profile()->GetBookmarkModel();
227  sync_api::UserShare* user_share = profile_sync_service->GetUserShare();
228  BookmarkModelAssociator* model_associator =
229      new BookmarkModelAssociator(bookmark_model,
230                                  user_share,
231                                  error_handler);
232  BookmarkChangeProcessor* change_processor =
233      new BookmarkChangeProcessor(model_associator,
234                                  error_handler);
235  return SyncComponents(model_associator, change_processor);
236}
237
238ProfileSyncFactory::SyncComponents
239ProfileSyncFactoryImpl::CreateExtensionSyncComponents(
240    ProfileSyncService* profile_sync_service,
241    UnrecoverableErrorHandler* error_handler) {
242  browser_sync::ExtensionSyncTraits traits =
243      browser_sync::GetExtensionSyncTraits();
244  ExtensionServiceInterface* extension_service =
245      profile_sync_service->profile()->GetExtensionService();
246  sync_api::UserShare* user_share = profile_sync_service->GetUserShare();
247  ExtensionModelAssociator* model_associator =
248      new ExtensionModelAssociator(traits, extension_service, user_share);
249  ExtensionChangeProcessor* change_processor =
250      new ExtensionChangeProcessor(traits, error_handler);
251  return SyncComponents(model_associator, change_processor);
252}
253
254ProfileSyncFactory::SyncComponents
255ProfileSyncFactoryImpl::CreatePasswordSyncComponents(
256    ProfileSyncService* profile_sync_service,
257    PasswordStore* password_store,
258    UnrecoverableErrorHandler* error_handler) {
259  PasswordModelAssociator* model_associator =
260      new PasswordModelAssociator(profile_sync_service,
261                                  password_store);
262  PasswordChangeProcessor* change_processor =
263      new PasswordChangeProcessor(model_associator,
264                                  password_store,
265                                  error_handler);
266  return SyncComponents(model_associator, change_processor);
267}
268
269ProfileSyncFactory::SyncComponents
270ProfileSyncFactoryImpl::CreatePreferenceSyncComponents(
271    ProfileSyncService* profile_sync_service,
272    UnrecoverableErrorHandler* error_handler) {
273  PreferenceModelAssociator* model_associator =
274      new PreferenceModelAssociator(profile_sync_service);
275  PreferenceChangeProcessor* change_processor =
276      new PreferenceChangeProcessor(model_associator,
277                                    error_handler);
278  return SyncComponents(model_associator, change_processor);
279}
280
281ProfileSyncFactory::SyncComponents
282ProfileSyncFactoryImpl::CreateThemeSyncComponents(
283    ProfileSyncService* profile_sync_service,
284    UnrecoverableErrorHandler* error_handler) {
285  ThemeModelAssociator* model_associator =
286      new ThemeModelAssociator(profile_sync_service);
287  ThemeChangeProcessor* change_processor =
288      new ThemeChangeProcessor(error_handler);
289  return SyncComponents(model_associator, change_processor);
290}
291
292ProfileSyncFactory::SyncComponents
293ProfileSyncFactoryImpl::CreateTypedUrlSyncComponents(
294    ProfileSyncService* profile_sync_service,
295    history::HistoryBackend* history_backend,
296    browser_sync::UnrecoverableErrorHandler* error_handler) {
297  TypedUrlModelAssociator* model_associator =
298      new TypedUrlModelAssociator(profile_sync_service,
299                                  history_backend);
300  TypedUrlChangeProcessor* change_processor =
301      new TypedUrlChangeProcessor(model_associator,
302                                  history_backend,
303                                  error_handler);
304  return SyncComponents(model_associator, change_processor);
305}
306
307ProfileSyncFactory::SyncComponents
308ProfileSyncFactoryImpl::CreateSessionSyncComponents(
309    ProfileSyncService* profile_sync_service,
310    UnrecoverableErrorHandler* error_handler) {
311  SessionModelAssociator* model_associator =
312      new SessionModelAssociator(profile_sync_service);
313  SessionChangeProcessor* change_processor =
314      new SessionChangeProcessor(error_handler, model_associator);
315  return SyncComponents(model_associator, change_processor);
316}
317