• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
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/bookmarks/enhanced_bookmarks_features.h"
9 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
10 #include "chrome/browser/history/history_service.h"
11 #include "chrome/browser/history/history_service_factory.h"
12 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h"
13 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service_factory.h"
14 #include "chrome/browser/password_manager/password_store_factory.h"
15 #include "chrome/browser/pref_service_flags_storage.h"
16 #include "chrome/browser/prefs/pref_model_associator.h"
17 #include "chrome/browser/prefs/pref_service_syncable.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/search_engines/template_url_service_factory.h"
20 #include "chrome/browser/signin/signin_manager_factory.h"
21 #include "chrome/browser/sync/glue/autofill_data_type_controller.h"
22 #include "chrome/browser/sync/glue/autofill_profile_data_type_controller.h"
23 #include "chrome/browser/sync/glue/bookmark_change_processor.h"
24 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h"
25 #include "chrome/browser/sync/glue/bookmark_model_associator.h"
26 #include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h"
27 #include "chrome/browser/sync/glue/extension_backed_data_type_controller.h"
28 #include "chrome/browser/sync/glue/extension_data_type_controller.h"
29 #include "chrome/browser/sync/glue/extension_setting_data_type_controller.h"
30 #include "chrome/browser/sync/glue/history_delete_directives_data_type_controller.h"
31 #include "chrome/browser/sync/glue/local_device_info_provider_impl.h"
32 #include "chrome/browser/sync/glue/password_data_type_controller.h"
33 #include "chrome/browser/sync/glue/search_engine_data_type_controller.h"
34 #include "chrome/browser/sync/glue/sync_backend_host.h"
35 #include "chrome/browser/sync/glue/sync_backend_host_impl.h"
36 #include "chrome/browser/sync/glue/theme_data_type_controller.h"
37 #include "chrome/browser/sync/glue/typed_url_change_processor.h"
38 #include "chrome/browser/sync/glue/typed_url_data_type_controller.h"
39 #include "chrome/browser/sync/glue/typed_url_model_associator.h"
40 #include "chrome/browser/sync/profile_sync_components_factory_impl.h"
41 #include "chrome/browser/sync/profile_sync_service.h"
42 #include "chrome/browser/sync/profile_sync_service_factory.h"
43 #include "chrome/browser/sync/sessions/session_data_type_controller.h"
44 #include "chrome/browser/themes/theme_service.h"
45 #include "chrome/browser/themes/theme_service_factory.h"
46 #include "chrome/browser/themes/theme_syncable_service.h"
47 #include "chrome/browser/webdata/autocomplete_syncable_service.h"
48 #include "chrome/browser/webdata/web_data_service_factory.h"
49 #include "chrome/common/chrome_switches.h"
50 #include "chrome/common/chrome_version_info.h"
51 #include "chrome/common/pref_names.h"
52 #include "components/autofill/core/browser/webdata/autofill_profile_syncable_service.h"
53 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
54 #include "components/dom_distiller/core/dom_distiller_service.h"
55 #include "components/password_manager/core/browser/password_store.h"
56 #include "components/search_engines/template_url_service.h"
57 #include "components/signin/core/browser/signin_manager.h"
58 #include "components/sync_driver/data_type_manager_impl.h"
59 #include "components/sync_driver/data_type_manager_observer.h"
60 #include "components/sync_driver/device_info_data_type_controller.h"
61 #include "components/sync_driver/generic_change_processor.h"
62 #include "components/sync_driver/proxy_data_type_controller.h"
63 #include "components/sync_driver/shared_change_processor.h"
64 #include "components/sync_driver/ui_data_type_controller.h"
65 #include "content/public/browser/browser_thread.h"
66 #include "google_apis/gaia/oauth2_token_service_request.h"
67 #include "net/url_request/url_request_context_getter.h"
68 #include "sync/api/attachments/fake_attachment_store.h"
69 #include "sync/api/syncable_service.h"
70 #include "sync/internal_api/public/attachments/attachment_downloader.h"
71 #include "sync/internal_api/public/attachments/attachment_service.h"
72 #include "sync/internal_api/public/attachments/attachment_service_impl.h"
73 #include "sync/internal_api/public/attachments/attachment_uploader_impl.h"
74 
75 #if defined(ENABLE_APP_LIST)
76 #include "chrome/browser/ui/app_list/app_list_syncable_service.h"
77 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h"
78 #include "ui/app_list/app_list_switches.h"
79 #endif
80 
81 #if defined(ENABLE_EXTENSIONS)
82 #include "chrome/browser/extensions/api/storage/settings_sync_util.h"
83 #include "chrome/browser/extensions/api/synced_notifications_private/synced_notifications_shim.h"
84 #include "chrome/browser/extensions/extension_sync_service.h"
85 #endif
86 
87 #if defined(ENABLE_MANAGED_USERS)
88 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
89 #include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
90 #include "chrome/browser/supervised_user/supervised_user_shared_settings_service.h"
91 #include "chrome/browser/supervised_user/supervised_user_shared_settings_service_factory.h"
92 #include "chrome/browser/supervised_user/supervised_user_sync_data_type_controller.h"
93 #include "chrome/browser/supervised_user/supervised_user_sync_service.h"
94 #include "chrome/browser/supervised_user/supervised_user_sync_service_factory.h"
95 #endif
96 
97 #if defined(ENABLE_SPELLCHECK)
98 #include "chrome/browser/spellchecker/spellcheck_factory.h"
99 #include "chrome/browser/spellchecker/spellcheck_service.h"
100 #endif
101 
102 using browser_sync::AutofillDataTypeController;
103 using browser_sync::AutofillProfileDataTypeController;
104 using browser_sync::BookmarkChangeProcessor;
105 using browser_sync::BookmarkDataTypeController;
106 using browser_sync::BookmarkModelAssociator;
107 using browser_sync::ChromeReportUnrecoverableError;
108 using browser_sync::ExtensionBackedDataTypeController;
109 using browser_sync::ExtensionDataTypeController;
110 using browser_sync::ExtensionSettingDataTypeController;
111 using browser_sync::HistoryDeleteDirectivesDataTypeController;
112 using browser_sync::PasswordDataTypeController;
113 using browser_sync::SearchEngineDataTypeController;
114 using browser_sync::SessionDataTypeController;
115 using browser_sync::SyncBackendHost;
116 using browser_sync::ThemeDataTypeController;
117 using browser_sync::TypedUrlChangeProcessor;
118 using browser_sync::TypedUrlDataTypeController;
119 using browser_sync::TypedUrlModelAssociator;
120 using content::BrowserThread;
121 using sync_driver::DataTypeController;
122 using sync_driver::DataTypeErrorHandler;
123 using sync_driver::DataTypeManager;
124 using sync_driver::DataTypeManagerImpl;
125 using sync_driver::DataTypeManagerObserver;
126 using sync_driver::DeviceInfoDataTypeController;
127 using sync_driver::ProxyDataTypeController;
128 using sync_driver::SharedChangeProcessor;
129 using sync_driver::UIDataTypeController;
130 
131 namespace {
132 
GetDisabledTypesFromCommandLine(const CommandLine & command_line)133 syncer::ModelTypeSet GetDisabledTypesFromCommandLine(
134     const CommandLine& command_line) {
135   syncer::ModelTypeSet disabled_types;
136   std::string disabled_types_str =
137       command_line.GetSwitchValueASCII(switches::kDisableSyncTypes);
138   disabled_types = syncer::ModelTypeSetFromString(disabled_types_str);
139   return disabled_types;
140 }
141 
GetEnabledTypesFromCommandLine(const CommandLine & command_line)142 syncer::ModelTypeSet GetEnabledTypesFromCommandLine(
143     const CommandLine& command_line) {
144   syncer::ModelTypeSet enabled_types;
145   if (command_line.HasSwitch(switches::kEnableSyncSyncedNotifications)) {
146     enabled_types.Put(syncer::SYNCED_NOTIFICATIONS);
147   }
148   return enabled_types;
149 }
150 
151 }  // namespace
152 
ProfileSyncComponentsFactoryImpl(Profile * profile,CommandLine * command_line,const GURL & sync_service_url,OAuth2TokenService * token_service,net::URLRequestContextGetter * url_request_context_getter)153 ProfileSyncComponentsFactoryImpl::ProfileSyncComponentsFactoryImpl(
154     Profile* profile,
155     CommandLine* command_line,
156     const GURL& sync_service_url,
157     OAuth2TokenService* token_service,
158     net::URLRequestContextGetter* url_request_context_getter)
159     : profile_(profile),
160       command_line_(command_line),
161       web_data_service_(WebDataServiceFactory::GetAutofillWebDataForProfile(
162           profile_, Profile::EXPLICIT_ACCESS)),
163       sync_service_url_(sync_service_url),
164       token_service_(token_service),
165       url_request_context_getter_(url_request_context_getter),
166       weak_factory_(this) {
167   DCHECK(token_service_);
168   DCHECK(url_request_context_getter_);
169 }
170 
~ProfileSyncComponentsFactoryImpl()171 ProfileSyncComponentsFactoryImpl::~ProfileSyncComponentsFactoryImpl() {
172 }
173 
RegisterDataTypes(ProfileSyncService * pss)174 void ProfileSyncComponentsFactoryImpl::RegisterDataTypes(
175     ProfileSyncService* pss) {
176   syncer::ModelTypeSet disabled_types =
177       GetDisabledTypesFromCommandLine(*command_line_);
178   syncer::ModelTypeSet enabled_types =
179       GetEnabledTypesFromCommandLine(*command_line_);
180   RegisterCommonDataTypes(disabled_types, enabled_types, pss);
181 #if !defined(OS_ANDROID)
182   RegisterDesktopDataTypes(disabled_types, enabled_types, pss);
183 #endif
184 }
185 
RegisterCommonDataTypes(syncer::ModelTypeSet disabled_types,syncer::ModelTypeSet enabled_types,ProfileSyncService * pss)186 void ProfileSyncComponentsFactoryImpl::RegisterCommonDataTypes(
187     syncer::ModelTypeSet disabled_types,
188     syncer::ModelTypeSet enabled_types,
189     ProfileSyncService* pss) {
190   // TODO(stanisc): can DEVICE_INFO be one of disabled datatypes?
191   pss->RegisterDataTypeController(new DeviceInfoDataTypeController(
192       BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
193       base::Bind(&ChromeReportUnrecoverableError),
194       this,
195       pss->GetLocalDeviceInfoProvider()));
196 
197   // Autofill sync is enabled by default.  Register unless explicitly
198   // disabled.
199   if (!disabled_types.Has(syncer::AUTOFILL)) {
200     pss->RegisterDataTypeController(
201         new AutofillDataTypeController(this, profile_));
202   }
203 
204   // Autofill profile sync is enabled by default.  Register unless explicitly
205   // disabled.
206   if (!disabled_types.Has(syncer::AUTOFILL_PROFILE)) {
207     pss->RegisterDataTypeController(
208         new AutofillProfileDataTypeController(this, profile_));
209   }
210 
211   // Bookmark sync is enabled by default.  Register unless explicitly
212   // disabled.
213   if (!disabled_types.Has(syncer::BOOKMARKS)) {
214     pss->RegisterDataTypeController(
215         new BookmarkDataTypeController(this, profile_, pss));
216   }
217 
218   const bool history_disabled =
219       profile_->GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled);
220   // TypedUrl sync is enabled by default.  Register unless explicitly disabled,
221   // or if saving history is disabled.
222   if (!disabled_types.Has(syncer::TYPED_URLS) && !history_disabled) {
223     pss->RegisterDataTypeController(
224         new TypedUrlDataTypeController(this, profile_, pss));
225   }
226 
227   // Delete directive sync is enabled by default.  Register unless full history
228   // sync is disabled.
229   if (!disabled_types.Has(syncer::HISTORY_DELETE_DIRECTIVES) &&
230       !history_disabled) {
231     pss->RegisterDataTypeController(
232         new HistoryDeleteDirectivesDataTypeController(this, pss));
233   }
234 
235   // Session sync is enabled by default.  Register unless explicitly disabled.
236   // This is also disabled if the browser history is disabled, because the
237   // tab sync data is added to the web history on the server.
238   if (!disabled_types.Has(syncer::PROXY_TABS) && !history_disabled) {
239     pss->RegisterDataTypeController(new ProxyDataTypeController(
240         BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
241         syncer::PROXY_TABS));
242     pss->RegisterDataTypeController(
243         new SessionDataTypeController(this,
244                                       profile_,
245                                       pss->GetSyncedWindowDelegatesGetter(),
246                                       pss->GetLocalDeviceInfoProvider()));
247   }
248 
249   // Favicon sync is enabled by default. Register unless explicitly disabled.
250   if (!disabled_types.Has(syncer::FAVICON_IMAGES) &&
251       !disabled_types.Has(syncer::FAVICON_TRACKING) &&
252       !history_disabled) {
253     // crbug/384552. We disable error uploading for this data types for now.
254     pss->RegisterDataTypeController(
255         new UIDataTypeController(
256             BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
257             base::Closure(),
258             syncer::FAVICON_IMAGES,
259             this));
260     pss->RegisterDataTypeController(
261         new UIDataTypeController(
262             BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
263             base::Closure(),
264             syncer::FAVICON_TRACKING,
265             this));
266   }
267 
268   // Password sync is enabled by default.  Register unless explicitly
269   // disabled.
270   if (!disabled_types.Has(syncer::PASSWORDS)) {
271     pss->RegisterDataTypeController(
272         new PasswordDataTypeController(this, profile_));
273   }
274 
275   // Article sync is disabled by default.  Register only if explicitly enabled.
276   if (IsEnableSyncArticlesSet()) {
277     pss->RegisterDataTypeController(
278         new UIDataTypeController(
279             BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
280             base::Bind(&ChromeReportUnrecoverableError),
281             syncer::ARTICLES,
282             this));
283   }
284 
285 #if defined(ENABLE_MANAGED_USERS)
286   pss->RegisterDataTypeController(
287       new SupervisedUserSyncDataTypeController(
288           syncer::SUPERVISED_USER_SETTINGS,
289           this,
290           profile_));
291 #endif
292 }
293 
RegisterDesktopDataTypes(syncer::ModelTypeSet disabled_types,syncer::ModelTypeSet enabled_types,ProfileSyncService * pss)294 void ProfileSyncComponentsFactoryImpl::RegisterDesktopDataTypes(
295     syncer::ModelTypeSet disabled_types,
296     syncer::ModelTypeSet enabled_types,
297     ProfileSyncService* pss) {
298   // App sync is enabled by default.  Register unless explicitly
299   // disabled.
300   if (!disabled_types.Has(syncer::APPS)) {
301     pss->RegisterDataTypeController(
302         new ExtensionDataTypeController(syncer::APPS, this, profile_));
303   }
304 
305   // Extension sync is enabled by default.  Register unless explicitly
306   // disabled.
307   if (!disabled_types.Has(syncer::EXTENSIONS)) {
308     pss->RegisterDataTypeController(
309         new ExtensionDataTypeController(syncer::EXTENSIONS, this, profile_));
310   }
311 
312   // Preference sync is enabled by default.  Register unless explicitly
313   // disabled.
314   if (!disabled_types.Has(syncer::PREFERENCES)) {
315     pss->RegisterDataTypeController(
316         new UIDataTypeController(
317             BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
318             base::Bind(&ChromeReportUnrecoverableError),
319             syncer::PREFERENCES,
320             this));
321   }
322 
323   if (!disabled_types.Has(syncer::PRIORITY_PREFERENCES)) {
324     pss->RegisterDataTypeController(
325         new UIDataTypeController(
326             BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
327             base::Bind(&ChromeReportUnrecoverableError),
328             syncer::PRIORITY_PREFERENCES,
329             this));
330   }
331 
332 #if defined(ENABLE_THEMES)
333   // Theme sync is enabled by default.  Register unless explicitly disabled.
334   if (!disabled_types.Has(syncer::THEMES)) {
335     pss->RegisterDataTypeController(
336         new ThemeDataTypeController(this, profile_));
337   }
338 #endif
339 
340   // Search Engine sync is enabled by default.  Register unless explicitly
341   // disabled.
342   if (!disabled_types.Has(syncer::SEARCH_ENGINES)) {
343     pss->RegisterDataTypeController(
344         new SearchEngineDataTypeController(this, profile_));
345   }
346 
347   // Extension setting sync is enabled by default.  Register unless explicitly
348   // disabled.
349   if (!disabled_types.Has(syncer::EXTENSION_SETTINGS)) {
350     pss->RegisterDataTypeController(new ExtensionSettingDataTypeController(
351         syncer::EXTENSION_SETTINGS, this, profile_));
352   }
353 
354   // App setting sync is enabled by default.  Register unless explicitly
355   // disabled.
356   if (!disabled_types.Has(syncer::APP_SETTINGS)) {
357     pss->RegisterDataTypeController(new ExtensionSettingDataTypeController(
358         syncer::APP_SETTINGS, this, profile_));
359   }
360 
361 #if defined(ENABLE_APP_LIST)
362   if (app_list::switches::IsAppListSyncEnabled()) {
363     pss->RegisterDataTypeController(
364         new UIDataTypeController(
365             BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
366             base::Bind(&ChromeReportUnrecoverableError),
367             syncer::APP_LIST,
368             this));
369   }
370 #endif
371 
372   // Synced Notifications are disabled by default.
373 #if defined(ENABLE_EXTENSIONS) && defined(ENABLE_NOTIFICATIONS)
374   if (enabled_types.Has(syncer::SYNCED_NOTIFICATIONS)) {
375     pss->RegisterDataTypeController(
376         new ExtensionBackedDataTypeController(
377               syncer::SYNCED_NOTIFICATIONS,
378               "",  // TODO(dewittj): pass the extension hash here.
379               this,
380               profile_));
381 
382     pss->RegisterDataTypeController(
383         new ExtensionBackedDataTypeController(
384               syncer::SYNCED_NOTIFICATION_APP_INFO,
385               "",  // TODO(dewittj): pass the extension hash here.
386               this,
387               profile_));
388   }
389 #endif
390 
391 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_CHROMEOS)
392   // Dictionary sync is enabled by default.
393   if (!disabled_types.Has(syncer::DICTIONARY)) {
394     pss->RegisterDataTypeController(
395         new UIDataTypeController(
396             BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
397             base::Bind(&ChromeReportUnrecoverableError),
398             syncer::DICTIONARY,
399             this));
400   }
401 #endif
402 
403 #if defined(ENABLE_MANAGED_USERS)
404   pss->RegisterDataTypeController(
405       new SupervisedUserSyncDataTypeController(
406           syncer::SUPERVISED_USERS,
407           this,
408           profile_));
409   pss->RegisterDataTypeController(
410       new SupervisedUserSyncDataTypeController(
411           syncer::SUPERVISED_USER_SHARED_SETTINGS,
412           this,
413           profile_));
414 #endif
415 }
416 
CreateDataTypeManager(const syncer::WeakHandle<syncer::DataTypeDebugInfoListener> & debug_info_listener,const DataTypeController::TypeMap * controllers,const sync_driver::DataTypeEncryptionHandler * encryption_handler,SyncBackendHost * backend,DataTypeManagerObserver * observer)417 DataTypeManager* ProfileSyncComponentsFactoryImpl::CreateDataTypeManager(
418     const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>&
419         debug_info_listener,
420     const DataTypeController::TypeMap* controllers,
421     const sync_driver::DataTypeEncryptionHandler* encryption_handler,
422     SyncBackendHost* backend,
423     DataTypeManagerObserver* observer) {
424   return new DataTypeManagerImpl(base::Bind(ChromeReportUnrecoverableError),
425                                  debug_info_listener,
426                                  controllers,
427                                  encryption_handler,
428                                  backend,
429                                  observer);
430 }
431 
432 browser_sync::SyncBackendHost*
CreateSyncBackendHost(const std::string & name,Profile * profile,invalidation::InvalidationService * invalidator,const base::WeakPtr<sync_driver::SyncPrefs> & sync_prefs,const base::FilePath & sync_folder)433 ProfileSyncComponentsFactoryImpl::CreateSyncBackendHost(
434     const std::string& name,
435     Profile* profile,
436     invalidation::InvalidationService* invalidator,
437     const base::WeakPtr<sync_driver::SyncPrefs>& sync_prefs,
438     const base::FilePath& sync_folder) {
439   return new browser_sync::SyncBackendHostImpl(name, profile, invalidator,
440                                                sync_prefs, sync_folder);
441 }
442 
443 scoped_ptr<sync_driver::LocalDeviceInfoProvider>
CreateLocalDeviceInfoProvider()444 ProfileSyncComponentsFactoryImpl::CreateLocalDeviceInfoProvider() {
445   return scoped_ptr<sync_driver::LocalDeviceInfoProvider>(
446       new browser_sync::LocalDeviceInfoProviderImpl());
447 }
448 
449 base::WeakPtr<syncer::SyncableService> ProfileSyncComponentsFactoryImpl::
GetSyncableServiceForType(syncer::ModelType type)450     GetSyncableServiceForType(syncer::ModelType type) {
451   if (!profile_) {  // For tests.
452      return base::WeakPtr<syncer::SyncableService>();
453   }
454   switch (type) {
455     case syncer::DEVICE_INFO:
456       return ProfileSyncServiceFactory::GetForProfile(profile_)
457           ->GetDeviceInfoSyncableService()
458           ->AsWeakPtr();
459     case syncer::PREFERENCES:
460       return PrefServiceSyncable::FromProfile(
461           profile_)->GetSyncableService(syncer::PREFERENCES)->AsWeakPtr();
462     case syncer::PRIORITY_PREFERENCES:
463       return PrefServiceSyncable::FromProfile(profile_)->GetSyncableService(
464           syncer::PRIORITY_PREFERENCES)->AsWeakPtr();
465     case syncer::AUTOFILL:
466     case syncer::AUTOFILL_PROFILE: {
467       if (!web_data_service_.get())
468         return base::WeakPtr<syncer::SyncableService>();
469       if (type == syncer::AUTOFILL) {
470         return AutocompleteSyncableService::FromWebDataService(
471             web_data_service_.get())->AsWeakPtr();
472       } else {
473         return autofill::AutofillProfileSyncableService::FromWebDataService(
474             web_data_service_.get())->AsWeakPtr();
475       }
476     }
477     case syncer::SEARCH_ENGINES:
478       return TemplateURLServiceFactory::GetForProfile(profile_)->AsWeakPtr();
479 #if defined(ENABLE_EXTENSIONS)
480     case syncer::APPS:
481     case syncer::EXTENSIONS:
482       return ExtensionSyncService::Get(profile_)->AsWeakPtr();
483     case syncer::APP_SETTINGS:
484     case syncer::EXTENSION_SETTINGS:
485       return extensions::settings_sync_util::GetSyncableService(profile_, type)
486           ->AsWeakPtr();
487 #endif
488 #if defined(ENABLE_APP_LIST)
489     case syncer::APP_LIST:
490       return app_list::AppListSyncableServiceFactory::GetForProfile(profile_)->
491           AsWeakPtr();
492 #endif
493 #if defined(ENABLE_THEMES)
494     case syncer::THEMES:
495       return ThemeServiceFactory::GetForProfile(profile_)->
496           GetThemeSyncableService()->AsWeakPtr();
497 #endif
498     case syncer::HISTORY_DELETE_DIRECTIVES: {
499       HistoryService* history =
500           HistoryServiceFactory::GetForProfile(
501               profile_, Profile::EXPLICIT_ACCESS);
502       return history ? history->AsWeakPtr() : base::WeakPtr<HistoryService>();
503     }
504 #if defined(ENABLE_EXTENSIONS)
505     case syncer::SYNCED_NOTIFICATIONS:
506     case syncer::SYNCED_NOTIFICATION_APP_INFO: {
507       return notifier::ChromeNotifierServiceFactory::GetForProfile(
508                  profile_, Profile::IMPLICIT_ACCESS)
509           ->GetSyncedNotificationsShim()
510           ->AsWeakPtr();
511     }
512 #endif
513 #if defined(ENABLE_SPELLCHECK)
514     case syncer::DICTIONARY:
515       return SpellcheckServiceFactory::GetForContext(profile_)->
516           GetCustomDictionary()->AsWeakPtr();
517 #endif
518     case syncer::FAVICON_IMAGES:
519     case syncer::FAVICON_TRACKING: {
520       browser_sync::FaviconCache* favicons =
521           ProfileSyncServiceFactory::GetForProfile(profile_)->
522               GetFaviconCache();
523       return favicons ? favicons->AsWeakPtr()
524                       : base::WeakPtr<syncer::SyncableService>();
525     }
526 #if defined(ENABLE_MANAGED_USERS)
527     case syncer::SUPERVISED_USER_SETTINGS:
528       return SupervisedUserSettingsServiceFactory::GetForProfile(profile_)->
529           AsWeakPtr();
530     case syncer::SUPERVISED_USERS:
531       return SupervisedUserSyncServiceFactory::GetForProfile(profile_)->
532           AsWeakPtr();
533     case syncer::SUPERVISED_USER_SHARED_SETTINGS:
534       return SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(
535           profile_)->AsWeakPtr();
536 #endif
537     case syncer::ARTICLES: {
538       dom_distiller::DomDistillerService* service =
539           dom_distiller::DomDistillerServiceFactory::GetForBrowserContext(
540               profile_);
541       if (service)
542         return service->GetSyncableService()->AsWeakPtr();
543       return base::WeakPtr<syncer::SyncableService>();
544     }
545     case syncer::SESSIONS: {
546       return ProfileSyncServiceFactory::GetForProfile(profile_)->
547           GetSessionsSyncableService()->AsWeakPtr();
548     }
549     case syncer::PASSWORDS: {
550 #if defined(PASSWORD_MANAGER_ENABLE_SYNC)
551       scoped_refptr<password_manager::PasswordStore> password_store =
552           PasswordStoreFactory::GetForProfile(profile_,
553                                               Profile::EXPLICIT_ACCESS);
554       return password_store.get() ? password_store->GetPasswordSyncableService()
555                                   : base::WeakPtr<syncer::SyncableService>();
556 #else
557       return base::WeakPtr<syncer::SyncableService>();
558 #endif
559     }
560     default:
561       // The following datatypes still need to be transitioned to the
562       // syncer::SyncableService API:
563       // Bookmarks
564       // Typed URLs
565       NOTREACHED();
566       return base::WeakPtr<syncer::SyncableService>();
567   }
568 }
569 
570 class TokenServiceProvider
571     : public OAuth2TokenServiceRequest::TokenServiceProvider {
572  public:
573   TokenServiceProvider(
574       const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
575       OAuth2TokenService* token_service);
576 
577   // OAuth2TokenServiceRequest::TokenServiceProvider implementation.
578   virtual scoped_refptr<base::SingleThreadTaskRunner>
579       GetTokenServiceTaskRunner() OVERRIDE;
580   virtual OAuth2TokenService* GetTokenService() OVERRIDE;
581 
582  private:
583   virtual ~TokenServiceProvider();
584 
585   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
586   OAuth2TokenService* token_service_;
587 };
588 
TokenServiceProvider(const scoped_refptr<base::SingleThreadTaskRunner> & task_runner,OAuth2TokenService * token_service)589 TokenServiceProvider::TokenServiceProvider(
590     const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
591     OAuth2TokenService* token_service)
592     : task_runner_(task_runner), token_service_(token_service) {
593 }
594 
~TokenServiceProvider()595 TokenServiceProvider::~TokenServiceProvider() {
596 }
597 
598 scoped_refptr<base::SingleThreadTaskRunner>
GetTokenServiceTaskRunner()599 TokenServiceProvider::GetTokenServiceTaskRunner() {
600   return task_runner_;
601 }
602 
GetTokenService()603 OAuth2TokenService* TokenServiceProvider::GetTokenService() {
604   return token_service_;
605 }
606 
607 scoped_ptr<syncer::AttachmentService>
CreateAttachmentService(const scoped_refptr<syncer::AttachmentStore> & attachment_store,const syncer::UserShare & user_share,syncer::AttachmentService::Delegate * delegate)608 ProfileSyncComponentsFactoryImpl::CreateAttachmentService(
609     const scoped_refptr<syncer::AttachmentStore>& attachment_store,
610     const syncer::UserShare& user_share,
611     syncer::AttachmentService::Delegate* delegate) {
612   scoped_ptr<syncer::AttachmentUploader> attachment_uploader;
613   scoped_ptr<syncer::AttachmentDownloader> attachment_downloader;
614   // Only construct an AttachmentUploader and AttachmentDownload if we have sync
615   // credentials. We may not have sync credentials because there may not be a
616   // signed in sync user (e.g. sync is running in "backup" mode).
617   if (!user_share.sync_credentials.email.empty() &&
618       !user_share.sync_credentials.scope_set.empty()) {
619     scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider>
620         token_service_provider(new TokenServiceProvider(
621             content::BrowserThread::GetMessageLoopProxyForThread(
622                 content::BrowserThread::UI),
623             token_service_));
624     // TODO(maniscalco): Use shared (one per profile) thread-safe instances of
625     // AttachmentUploader and AttachmentDownloader instead of creating a new one
626     // per AttachmentService (bug 369536).
627     attachment_uploader.reset(new syncer::AttachmentUploaderImpl(
628         sync_service_url_,
629         url_request_context_getter_,
630         user_share.sync_credentials.email,
631         user_share.sync_credentials.scope_set,
632         token_service_provider));
633 
634     token_service_provider = new TokenServiceProvider(
635         content::BrowserThread::GetMessageLoopProxyForThread(
636             content::BrowserThread::UI),
637         token_service_);
638     attachment_downloader = syncer::AttachmentDownloader::Create(
639         sync_service_url_,
640         url_request_context_getter_,
641         user_share.sync_credentials.email,
642         user_share.sync_credentials.scope_set,
643         token_service_provider);
644   }
645 
646   // It is important that the initial backoff delay is relatively large.  For
647   // whatever reason, the server may fail all requests for a short period of
648   // time.  When this happens we don't want to overwhelm the server with
649   // requests so we use a large initial backoff.
650   const base::TimeDelta initial_backoff_delay =
651       base::TimeDelta::FromMinutes(30);
652   const base::TimeDelta max_backoff_delay = base::TimeDelta::FromHours(4);
653   scoped_ptr<syncer::AttachmentService> attachment_service(
654       new syncer::AttachmentServiceImpl(attachment_store,
655                                         attachment_uploader.Pass(),
656                                         attachment_downloader.Pass(),
657                                         delegate,
658                                         initial_backoff_delay,
659                                         max_backoff_delay));
660   return attachment_service.Pass();
661 }
662 
663 ProfileSyncComponentsFactory::SyncComponents
CreateBookmarkSyncComponents(ProfileSyncService * profile_sync_service,sync_driver::DataTypeErrorHandler * error_handler)664     ProfileSyncComponentsFactoryImpl::CreateBookmarkSyncComponents(
665         ProfileSyncService* profile_sync_service,
666         sync_driver::DataTypeErrorHandler* error_handler) {
667   BookmarkModel* bookmark_model =
668       BookmarkModelFactory::GetForProfile(profile_sync_service->profile());
669   syncer::UserShare* user_share = profile_sync_service->GetUserShare();
670   // TODO(akalin): We may want to propagate this switch up eventually.
671 #if defined(OS_ANDROID)
672   const bool kExpectMobileBookmarksFolder = true;
673 #else
674   const bool kExpectMobileBookmarksFolder = false;
675 #endif
676   BookmarkModelAssociator* model_associator =
677       new BookmarkModelAssociator(bookmark_model,
678                                   profile_sync_service->profile(),
679                                   user_share,
680                                   error_handler,
681                                   kExpectMobileBookmarksFolder);
682   BookmarkChangeProcessor* change_processor =
683       new BookmarkChangeProcessor(profile_sync_service->profile(),
684                                   model_associator,
685                                   error_handler);
686   return SyncComponents(model_associator, change_processor);
687 }
688 
689 ProfileSyncComponentsFactory::SyncComponents
CreateTypedUrlSyncComponents(ProfileSyncService * profile_sync_service,history::HistoryBackend * history_backend,sync_driver::DataTypeErrorHandler * error_handler)690     ProfileSyncComponentsFactoryImpl::CreateTypedUrlSyncComponents(
691         ProfileSyncService* profile_sync_service,
692         history::HistoryBackend* history_backend,
693         sync_driver::DataTypeErrorHandler* error_handler) {
694   TypedUrlModelAssociator* model_associator =
695       new TypedUrlModelAssociator(profile_sync_service,
696                                   history_backend,
697                                   error_handler);
698   TypedUrlChangeProcessor* change_processor =
699       new TypedUrlChangeProcessor(profile_,
700                                   model_associator,
701                                   history_backend,
702                                   error_handler);
703   return SyncComponents(model_associator, change_processor);
704 }
705