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