ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / sync / profile_sync_components_factory_impl.cc
blobc71e48549377517dcd3384e54bf90d348db93b6d
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.h"
11 #include "chrome/browser/history/history_service_factory.h"
12 #include "chrome/browser/password_manager/password_store_factory.h"
13 #include "chrome/browser/pref_service_flags_storage.h"
14 #include "chrome/browser/prefs/pref_model_associator.h"
15 #include "chrome/browser/prefs/pref_service_syncable.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/search_engines/template_url_service_factory.h"
18 #include "chrome/browser/signin/signin_manager_factory.h"
19 #include "chrome/browser/sync/glue/autofill_data_type_controller.h"
20 #include "chrome/browser/sync/glue/autofill_profile_data_type_controller.h"
21 #include "chrome/browser/sync/glue/autofill_wallet_data_type_controller.h"
22 #include "chrome/browser/sync/glue/bookmark_change_processor.h"
23 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h"
24 #include "chrome/browser/sync/glue/bookmark_model_associator.h"
25 #include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h"
26 #include "chrome/browser/sync/glue/history_delete_directives_data_type_controller.h"
27 #include "chrome/browser/sync/glue/local_device_info_provider_impl.h"
28 #include "chrome/browser/sync/glue/password_data_type_controller.h"
29 #include "chrome/browser/sync/glue/search_engine_data_type_controller.h"
30 #include "chrome/browser/sync/glue/sync_backend_host.h"
31 #include "chrome/browser/sync/glue/sync_backend_host_impl.h"
32 #include "chrome/browser/sync/glue/theme_data_type_controller.h"
33 #include "chrome/browser/sync/glue/typed_url_change_processor.h"
34 #include "chrome/browser/sync/glue/typed_url_data_type_controller.h"
35 #include "chrome/browser/sync/glue/typed_url_model_associator.h"
36 #include "chrome/browser/sync/profile_sync_components_factory_impl.h"
37 #include "chrome/browser/sync/profile_sync_service.h"
38 #include "chrome/browser/sync/profile_sync_service_factory.h"
39 #include "chrome/browser/sync/sessions/session_data_type_controller.h"
40 #include "chrome/browser/themes/theme_service.h"
41 #include "chrome/browser/themes/theme_service_factory.h"
42 #include "chrome/browser/themes/theme_syncable_service.h"
43 #include "chrome/browser/webdata/web_data_service_factory.h"
44 #include "chrome/common/chrome_switches.h"
45 #include "chrome/common/chrome_version_info.h"
46 #include "chrome/common/pref_names.h"
47 #include "components/autofill/core/browser/webdata/autocomplete_syncable_service.h"
48 #include "components/autofill/core/browser/webdata/autofill_profile_syncable_service.h"
49 #include "components/autofill/core/browser/webdata/autofill_wallet_syncable_service.h"
50 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
51 #include "components/autofill/core/common/autofill_pref_names.h"
52 #include "components/autofill/core/common/autofill_switches.h"
53 #include "components/dom_distiller/core/dom_distiller_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 if (profile_->GetPrefs()->GetBoolean(
233 autofill::prefs::kAutofillWalletSyncExperimentEnabled) &&
234 !disabled_types.Has(syncer::AUTOFILL_WALLET_DATA)) {
235 // The feature can be enabled by sync experiment *or* command line flag,
236 // and additionally the sync type must be enabled.
237 pss->RegisterDataTypeController(
238 new browser_sync::AutofillWalletDataTypeController(this, profile_));
241 // Bookmark sync is enabled by default. Register unless explicitly
242 // disabled.
243 if (!disabled_types.Has(syncer::BOOKMARKS)) {
244 pss->RegisterDataTypeController(
245 new BookmarkDataTypeController(this, profile_, pss));
248 const bool history_disabled =
249 profile_->GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled);
250 // TypedUrl sync is enabled by default. Register unless explicitly disabled,
251 // or if saving history is disabled.
252 if (!disabled_types.Has(syncer::TYPED_URLS) && !history_disabled) {
253 pss->RegisterDataTypeController(
254 new TypedUrlDataTypeController(this, profile_, pss));
257 // Delete directive sync is enabled by default. Register unless full history
258 // sync is disabled.
259 if (!disabled_types.Has(syncer::HISTORY_DELETE_DIRECTIVES) &&
260 !history_disabled) {
261 pss->RegisterDataTypeController(
262 new HistoryDeleteDirectivesDataTypeController(this, pss));
265 // Session sync is enabled by default. Register unless explicitly disabled.
266 // This is also disabled if the browser history is disabled, because the
267 // tab sync data is added to the web history on the server.
268 if (!disabled_types.Has(syncer::PROXY_TABS) && !history_disabled) {
269 pss->RegisterDataTypeController(new ProxyDataTypeController(
270 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
271 syncer::PROXY_TABS));
272 pss->RegisterDataTypeController(
273 new SessionDataTypeController(this,
274 profile_,
275 pss->GetSyncedWindowDelegatesGetter(),
276 pss->GetLocalDeviceInfoProvider()));
279 // Favicon sync is enabled by default. Register unless explicitly disabled.
280 if (!disabled_types.Has(syncer::FAVICON_IMAGES) &&
281 !disabled_types.Has(syncer::FAVICON_TRACKING) &&
282 !history_disabled) {
283 // crbug/384552. We disable error uploading for this data types for now.
284 pss->RegisterDataTypeController(
285 new UIDataTypeController(
286 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
287 base::Closure(),
288 syncer::FAVICON_IMAGES,
289 this));
290 pss->RegisterDataTypeController(
291 new UIDataTypeController(
292 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
293 base::Closure(),
294 syncer::FAVICON_TRACKING,
295 this));
298 // Password sync is enabled by default. Register unless explicitly
299 // disabled.
300 if (!disabled_types.Has(syncer::PASSWORDS)) {
301 pss->RegisterDataTypeController(
302 new PasswordDataTypeController(this, profile_));
305 // Article sync is disabled by default. Register only if explicitly enabled.
306 if (IsEnableSyncArticlesSet()) {
307 pss->RegisterDataTypeController(
308 new UIDataTypeController(
309 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
310 base::Bind(&ChromeReportUnrecoverableError),
311 syncer::ARTICLES,
312 this));
315 #if defined(ENABLE_SUPERVISED_USERS)
316 pss->RegisterDataTypeController(
317 new SupervisedUserSyncDataTypeController(
318 syncer::SUPERVISED_USER_SETTINGS,
319 this,
320 profile_));
321 pss->RegisterDataTypeController(
322 new SupervisedUserSyncDataTypeController(
323 syncer::SUPERVISED_USER_WHITELISTS,
324 this,
325 profile_));
326 #endif
329 void ProfileSyncComponentsFactoryImpl::RegisterDesktopDataTypes(
330 syncer::ModelTypeSet disabled_types,
331 syncer::ModelTypeSet enabled_types,
332 ProfileSyncService* pss) {
333 #if defined(ENABLE_EXTENSIONS)
334 // App sync is enabled by default. Register unless explicitly
335 // disabled.
336 if (!disabled_types.Has(syncer::APPS)) {
337 pss->RegisterDataTypeController(
338 new ExtensionDataTypeController(syncer::APPS, this, profile_));
341 // Extension sync is enabled by default. Register unless explicitly
342 // disabled.
343 if (!disabled_types.Has(syncer::EXTENSIONS)) {
344 pss->RegisterDataTypeController(
345 new ExtensionDataTypeController(syncer::EXTENSIONS, this, profile_));
347 #endif
349 // Preference sync is enabled by default. Register unless explicitly
350 // disabled.
351 if (!disabled_types.Has(syncer::PREFERENCES)) {
352 pss->RegisterDataTypeController(
353 new UIDataTypeController(
354 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
355 base::Bind(&ChromeReportUnrecoverableError),
356 syncer::PREFERENCES,
357 this));
360 if (!disabled_types.Has(syncer::PRIORITY_PREFERENCES)) {
361 pss->RegisterDataTypeController(
362 new UIDataTypeController(
363 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
364 base::Bind(&ChromeReportUnrecoverableError),
365 syncer::PRIORITY_PREFERENCES,
366 this));
369 #if defined(ENABLE_THEMES)
370 // Theme sync is enabled by default. Register unless explicitly disabled.
371 if (!disabled_types.Has(syncer::THEMES)) {
372 pss->RegisterDataTypeController(
373 new ThemeDataTypeController(this, profile_));
375 #endif
377 // Search Engine sync is enabled by default. Register unless explicitly
378 // disabled.
379 if (!disabled_types.Has(syncer::SEARCH_ENGINES)) {
380 pss->RegisterDataTypeController(
381 new SearchEngineDataTypeController(this, profile_));
384 #if defined(ENABLE_EXTENSIONS)
385 // Extension setting sync is enabled by default. Register unless explicitly
386 // disabled.
387 if (!disabled_types.Has(syncer::EXTENSION_SETTINGS)) {
388 pss->RegisterDataTypeController(new ExtensionSettingDataTypeController(
389 syncer::EXTENSION_SETTINGS, this, profile_));
392 // App setting sync is enabled by default. Register unless explicitly
393 // disabled.
394 if (!disabled_types.Has(syncer::APP_SETTINGS)) {
395 pss->RegisterDataTypeController(new ExtensionSettingDataTypeController(
396 syncer::APP_SETTINGS, this, profile_));
398 #endif
400 #if defined(ENABLE_APP_LIST)
401 if (app_list::switches::IsAppListSyncEnabled()) {
402 pss->RegisterDataTypeController(
403 new UIDataTypeController(
404 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
405 base::Bind(&ChromeReportUnrecoverableError),
406 syncer::APP_LIST,
407 this));
409 #endif
411 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_CHROMEOS)
412 // Dictionary sync is enabled by default.
413 if (!disabled_types.Has(syncer::DICTIONARY)) {
414 pss->RegisterDataTypeController(
415 new UIDataTypeController(
416 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
417 base::Bind(&ChromeReportUnrecoverableError),
418 syncer::DICTIONARY,
419 this));
421 #endif
423 #if defined(ENABLE_SUPERVISED_USERS)
424 pss->RegisterDataTypeController(
425 new SupervisedUserSyncDataTypeController(
426 syncer::SUPERVISED_USERS,
427 this,
428 profile_));
429 pss->RegisterDataTypeController(
430 new SupervisedUserSyncDataTypeController(
431 syncer::SUPERVISED_USER_SHARED_SETTINGS,
432 this,
433 profile_));
434 #endif
436 #if defined(OS_CHROMEOS)
437 if (command_line_->HasSwitch(switches::kEnableWifiCredentialSync) &&
438 !disabled_types.Has(syncer::WIFI_CREDENTIALS)) {
439 pss->RegisterDataTypeController(
440 new UIDataTypeController(
441 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
442 base::Bind(&ChromeReportUnrecoverableError),
443 syncer::WIFI_CREDENTIALS,
444 this));
446 #endif
449 DataTypeManager* ProfileSyncComponentsFactoryImpl::CreateDataTypeManager(
450 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>&
451 debug_info_listener,
452 const DataTypeController::TypeMap* controllers,
453 const sync_driver::DataTypeEncryptionHandler* encryption_handler,
454 SyncBackendHost* backend,
455 DataTypeManagerObserver* observer) {
456 return new DataTypeManagerImpl(base::Bind(ChromeReportUnrecoverableError),
457 debug_info_listener,
458 controllers,
459 encryption_handler,
460 backend,
461 observer);
464 browser_sync::SyncBackendHost*
465 ProfileSyncComponentsFactoryImpl::CreateSyncBackendHost(
466 const std::string& name,
467 Profile* profile,
468 invalidation::InvalidationService* invalidator,
469 const base::WeakPtr<sync_driver::SyncPrefs>& sync_prefs,
470 const base::FilePath& sync_folder) {
471 return new browser_sync::SyncBackendHostImpl(name, profile, invalidator,
472 sync_prefs, sync_folder);
475 scoped_ptr<sync_driver::LocalDeviceInfoProvider>
476 ProfileSyncComponentsFactoryImpl::CreateLocalDeviceInfoProvider() {
477 return scoped_ptr<sync_driver::LocalDeviceInfoProvider>(
478 new browser_sync::LocalDeviceInfoProviderImpl());
481 base::WeakPtr<syncer::SyncableService> ProfileSyncComponentsFactoryImpl::
482 GetSyncableServiceForType(syncer::ModelType type) {
483 if (!profile_) { // For tests.
484 return base::WeakPtr<syncer::SyncableService>();
486 switch (type) {
487 case syncer::DEVICE_INFO:
488 return ProfileSyncServiceFactory::GetForProfile(profile_)
489 ->GetDeviceInfoSyncableService()
490 ->AsWeakPtr();
491 case syncer::PREFERENCES:
492 return PrefServiceSyncable::FromProfile(
493 profile_)->GetSyncableService(syncer::PREFERENCES)->AsWeakPtr();
494 case syncer::PRIORITY_PREFERENCES:
495 return PrefServiceSyncable::FromProfile(profile_)->GetSyncableService(
496 syncer::PRIORITY_PREFERENCES)->AsWeakPtr();
497 case syncer::AUTOFILL:
498 case syncer::AUTOFILL_PROFILE:
499 case syncer::AUTOFILL_WALLET_DATA: {
500 if (!web_data_service_.get())
501 return base::WeakPtr<syncer::SyncableService>();
502 if (type == syncer::AUTOFILL) {
503 return autofill::AutocompleteSyncableService::FromWebDataService(
504 web_data_service_.get())->AsWeakPtr();
505 } else if (type == syncer::AUTOFILL_PROFILE) {
506 return autofill::AutofillProfileSyncableService::FromWebDataService(
507 web_data_service_.get())->AsWeakPtr();
509 return autofill::AutofillWalletSyncableService::FromWebDataService(
510 web_data_service_.get())->AsWeakPtr();
512 case syncer::SEARCH_ENGINES:
513 return TemplateURLServiceFactory::GetForProfile(profile_)->AsWeakPtr();
514 #if defined(ENABLE_EXTENSIONS)
515 case syncer::APPS:
516 case syncer::EXTENSIONS:
517 return ExtensionSyncService::Get(profile_)->AsWeakPtr();
518 case syncer::APP_SETTINGS:
519 case syncer::EXTENSION_SETTINGS:
520 return extensions::settings_sync_util::GetSyncableService(profile_, type)
521 ->AsWeakPtr();
522 #endif
523 #if defined(ENABLE_APP_LIST)
524 case syncer::APP_LIST:
525 return app_list::AppListSyncableServiceFactory::GetForProfile(profile_)->
526 AsWeakPtr();
527 #endif
528 #if defined(ENABLE_THEMES)
529 case syncer::THEMES:
530 return ThemeServiceFactory::GetForProfile(profile_)->
531 GetThemeSyncableService()->AsWeakPtr();
532 #endif
533 case syncer::HISTORY_DELETE_DIRECTIVES: {
534 HistoryService* history = HistoryServiceFactory::GetForProfile(
535 profile_, ServiceAccessType::EXPLICIT_ACCESS);
536 return history ? history->AsWeakPtr() : base::WeakPtr<HistoryService>();
538 #if defined(ENABLE_SPELLCHECK)
539 case syncer::DICTIONARY:
540 return SpellcheckServiceFactory::GetForContext(profile_)->
541 GetCustomDictionary()->AsWeakPtr();
542 #endif
543 case syncer::FAVICON_IMAGES:
544 case syncer::FAVICON_TRACKING: {
545 browser_sync::FaviconCache* favicons =
546 ProfileSyncServiceFactory::GetForProfile(profile_)->
547 GetFaviconCache();
548 return favicons ? favicons->AsWeakPtr()
549 : base::WeakPtr<syncer::SyncableService>();
551 #if defined(ENABLE_SUPERVISED_USERS)
552 case syncer::SUPERVISED_USER_SETTINGS:
553 return SupervisedUserSettingsServiceFactory::GetForProfile(profile_)->
554 AsWeakPtr();
555 case syncer::SUPERVISED_USERS:
556 return SupervisedUserSyncServiceFactory::GetForProfile(profile_)->
557 AsWeakPtr();
558 case syncer::SUPERVISED_USER_SHARED_SETTINGS:
559 return SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(
560 profile_)->AsWeakPtr();
561 case syncer::SUPERVISED_USER_WHITELISTS:
562 return SupervisedUserServiceFactory::GetForProfile(profile_)
563 ->GetWhitelistService()
564 ->AsWeakPtr();
565 #endif
566 case syncer::ARTICLES: {
567 dom_distiller::DomDistillerService* service =
568 dom_distiller::DomDistillerServiceFactory::GetForBrowserContext(
569 profile_);
570 if (service)
571 return service->GetSyncableService()->AsWeakPtr();
572 return base::WeakPtr<syncer::SyncableService>();
574 case syncer::SESSIONS: {
575 return ProfileSyncServiceFactory::GetForProfile(profile_)->
576 GetSessionsSyncableService()->AsWeakPtr();
578 case syncer::PASSWORDS: {
579 #if defined(PASSWORD_MANAGER_ENABLE_SYNC)
580 scoped_refptr<password_manager::PasswordStore> password_store =
581 PasswordStoreFactory::GetForProfile(
582 profile_, ServiceAccessType::EXPLICIT_ACCESS);
583 return password_store.get() ? password_store->GetPasswordSyncableService()
584 : base::WeakPtr<syncer::SyncableService>();
585 #else
586 return base::WeakPtr<syncer::SyncableService>();
587 #endif
589 #if defined(OS_CHROMEOS)
590 case syncer::WIFI_CREDENTIALS:
591 return wifi_sync::WifiCredentialSyncableServiceFactory::
592 GetForBrowserContext(profile_)->AsWeakPtr();
593 #endif
594 default:
595 // The following datatypes still need to be transitioned to the
596 // syncer::SyncableService API:
597 // Bookmarks
598 // Typed URLs
599 NOTREACHED();
600 return base::WeakPtr<syncer::SyncableService>();
604 class TokenServiceProvider
605 : public OAuth2TokenServiceRequest::TokenServiceProvider {
606 public:
607 TokenServiceProvider(
608 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
609 OAuth2TokenService* token_service);
611 // OAuth2TokenServiceRequest::TokenServiceProvider implementation.
612 scoped_refptr<base::SingleThreadTaskRunner> GetTokenServiceTaskRunner()
613 override;
614 OAuth2TokenService* GetTokenService() override;
616 private:
617 ~TokenServiceProvider() override;
619 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
620 OAuth2TokenService* token_service_;
623 TokenServiceProvider::TokenServiceProvider(
624 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
625 OAuth2TokenService* token_service)
626 : task_runner_(task_runner), token_service_(token_service) {
629 TokenServiceProvider::~TokenServiceProvider() {
632 scoped_refptr<base::SingleThreadTaskRunner>
633 TokenServiceProvider::GetTokenServiceTaskRunner() {
634 return task_runner_;
637 OAuth2TokenService* TokenServiceProvider::GetTokenService() {
638 return token_service_;
641 scoped_ptr<syncer::AttachmentService>
642 ProfileSyncComponentsFactoryImpl::CreateAttachmentService(
643 const scoped_refptr<syncer::AttachmentStore>& attachment_store,
644 const syncer::UserShare& user_share,
645 const std::string& store_birthday,
646 syncer::ModelType model_type,
647 syncer::AttachmentService::Delegate* delegate) {
648 scoped_ptr<syncer::AttachmentUploader> attachment_uploader;
649 scoped_ptr<syncer::AttachmentDownloader> attachment_downloader;
650 // Only construct an AttachmentUploader and AttachmentDownload if we have sync
651 // credentials. We may not have sync credentials because there may not be a
652 // signed in sync user (e.g. sync is running in "backup" mode).
653 if (!user_share.sync_credentials.email.empty() &&
654 !user_share.sync_credentials.scope_set.empty()) {
655 scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider>
656 token_service_provider(new TokenServiceProvider(
657 content::BrowserThread::GetMessageLoopProxyForThread(
658 content::BrowserThread::UI),
659 token_service_));
660 // TODO(maniscalco): Use shared (one per profile) thread-safe instances of
661 // AttachmentUploader and AttachmentDownloader instead of creating a new one
662 // per AttachmentService (bug 369536).
663 attachment_uploader.reset(new syncer::AttachmentUploaderImpl(
664 sync_service_url_, url_request_context_getter_,
665 user_share.sync_credentials.email,
666 user_share.sync_credentials.scope_set, token_service_provider,
667 store_birthday, model_type));
669 token_service_provider = new TokenServiceProvider(
670 content::BrowserThread::GetMessageLoopProxyForThread(
671 content::BrowserThread::UI),
672 token_service_);
673 attachment_downloader = syncer::AttachmentDownloader::Create(
674 sync_service_url_, url_request_context_getter_,
675 user_share.sync_credentials.email,
676 user_share.sync_credentials.scope_set, token_service_provider,
677 store_birthday, model_type);
680 // It is important that the initial backoff delay is relatively large. For
681 // whatever reason, the server may fail all requests for a short period of
682 // time. When this happens we don't want to overwhelm the server with
683 // requests so we use a large initial backoff.
684 const base::TimeDelta initial_backoff_delay =
685 base::TimeDelta::FromMinutes(30);
686 const base::TimeDelta max_backoff_delay = base::TimeDelta::FromHours(4);
687 scoped_ptr<syncer::AttachmentService> attachment_service(
688 new syncer::AttachmentServiceImpl(attachment_store,
689 attachment_uploader.Pass(),
690 attachment_downloader.Pass(),
691 delegate,
692 initial_backoff_delay,
693 max_backoff_delay));
694 return attachment_service.Pass();
697 ProfileSyncComponentsFactory::SyncComponents
698 ProfileSyncComponentsFactoryImpl::CreateBookmarkSyncComponents(
699 ProfileSyncService* profile_sync_service,
700 sync_driver::DataTypeErrorHandler* error_handler) {
701 BookmarkModel* bookmark_model =
702 BookmarkModelFactory::GetForProfile(profile_sync_service->profile());
703 syncer::UserShare* user_share = profile_sync_service->GetUserShare();
704 // TODO(akalin): We may want to propagate this switch up eventually.
705 #if defined(OS_ANDROID)
706 const bool kExpectMobileBookmarksFolder = true;
707 #else
708 const bool kExpectMobileBookmarksFolder = false;
709 #endif
710 BookmarkModelAssociator* model_associator =
711 new BookmarkModelAssociator(bookmark_model,
712 profile_sync_service->profile(),
713 user_share,
714 error_handler,
715 kExpectMobileBookmarksFolder);
716 BookmarkChangeProcessor* change_processor =
717 new BookmarkChangeProcessor(profile_sync_service->profile(),
718 model_associator,
719 error_handler);
720 return SyncComponents(model_associator, change_processor);
723 ProfileSyncComponentsFactory::SyncComponents
724 ProfileSyncComponentsFactoryImpl::CreateTypedUrlSyncComponents(
725 ProfileSyncService* profile_sync_service,
726 history::HistoryBackend* history_backend,
727 sync_driver::DataTypeErrorHandler* error_handler) {
728 TypedUrlModelAssociator* model_associator =
729 new TypedUrlModelAssociator(profile_sync_service,
730 history_backend,
731 error_handler);
732 TypedUrlChangeProcessor* change_processor =
733 new TypedUrlChangeProcessor(profile_,
734 model_associator,
735 history_backend,
736 error_handler);
737 return SyncComponents(model_associator, change_processor);