Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / sync / profile_sync_components_factory_impl.cc
blob111315905a50bf142f0476512e13b13a2d188f13
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 "chrome/browser/sync/profile_sync_components_factory_impl.h"
7 #include "base/command_line.h"
8 #include "build/build_config.h"
9 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
10 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.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_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/web_data_service_factory.h"
43 #include "chrome/common/chrome_switches.h"
44 #include "chrome/common/pref_names.h"
45 #include "components/autofill/core/browser/webdata/autocomplete_syncable_service.h"
46 #include "components/autofill/core/browser/webdata/autofill_profile_syncable_service.h"
47 #include "components/autofill/core/browser/webdata/autofill_wallet_metadata_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_features.h"
53 #include "components/dom_distiller/core/dom_distiller_service.h"
54 #include "components/history/core/browser/history_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/syncable_service.h"
69 #include "sync/internal_api/public/attachments/attachment_downloader.h"
70 #include "sync/internal_api/public/attachments/attachment_service.h"
71 #include "sync/internal_api/public/attachments/attachment_service_impl.h"
72 #include "sync/internal_api/public/attachments/attachment_uploader_impl.h"
74 #if defined(ENABLE_APP_LIST)
75 #include "chrome/browser/ui/app_list/app_list_syncable_service.h"
76 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h"
77 #include "ui/app_list/app_list_switches.h"
78 #endif
80 #if defined(ENABLE_EXTENSIONS)
81 #include "chrome/browser/extensions/api/storage/settings_sync_util.h"
82 #include "chrome/browser/extensions/extension_sync_service.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::ExtensionDataTypeController;
119 using browser_sync::ExtensionSettingDataTypeController;
120 #endif
121 using browser_sync::HistoryDeleteDirectivesDataTypeController;
122 using browser_sync::PasswordDataTypeController;
123 using browser_sync::SearchEngineDataTypeController;
124 using browser_sync::SessionDataTypeController;
125 using browser_sync::SyncBackendHost;
126 using browser_sync::ThemeDataTypeController;
127 using browser_sync::TypedUrlChangeProcessor;
128 using browser_sync::TypedUrlDataTypeController;
129 using browser_sync::TypedUrlModelAssociator;
130 using content::BrowserThread;
131 using sync_driver::DataTypeController;
132 using sync_driver::DataTypeErrorHandler;
133 using sync_driver::DataTypeManager;
134 using sync_driver::DataTypeManagerImpl;
135 using sync_driver::DataTypeManagerObserver;
136 using sync_driver::DeviceInfoDataTypeController;
137 using sync_driver::ProxyDataTypeController;
138 using sync_driver::SharedChangeProcessor;
139 using sync_driver::UIDataTypeController;
141 namespace {
143 syncer::ModelTypeSet GetDisabledTypesFromCommandLine(
144 const base::CommandLine& command_line) {
145 syncer::ModelTypeSet disabled_types;
146 std::string disabled_types_str =
147 command_line.GetSwitchValueASCII(switches::kDisableSyncTypes);
149 // Disable sync types experimentally to measure impact on startup time.
150 // TODO(mlerman): Remove this after the experiment. crbug.com/454788
151 std::string disable_types_finch =
152 variations::GetVariationParamValue("LightSpeed", "DisableSyncPart");
153 if (!disable_types_finch.empty()) {
154 if (disabled_types_str.empty())
155 disabled_types_str = disable_types_finch;
156 else
157 disabled_types_str += ", " + disable_types_finch;
160 disabled_types = syncer::ModelTypeSetFromString(disabled_types_str);
161 return disabled_types;
164 syncer::ModelTypeSet GetEnabledTypesFromCommandLine(
165 const base::CommandLine& command_line) {
166 syncer::ModelTypeSet enabled_types;
167 if (command_line.HasSwitch(autofill::switches::kEnableWalletMetadataSync))
168 enabled_types.Put(syncer::AUTOFILL_WALLET_METADATA);
170 return enabled_types;
173 } // namespace
175 ProfileSyncComponentsFactoryImpl::ProfileSyncComponentsFactoryImpl(
176 Profile* profile,
177 base::CommandLine* command_line,
178 const GURL& sync_service_url,
179 OAuth2TokenService* token_service,
180 net::URLRequestContextGetter* url_request_context_getter)
181 : profile_(profile),
182 command_line_(command_line),
183 web_data_service_(WebDataServiceFactory::GetAutofillWebDataForProfile(
184 profile_,
185 ServiceAccessType::EXPLICIT_ACCESS)),
186 sync_service_url_(sync_service_url),
187 token_service_(token_service),
188 url_request_context_getter_(url_request_context_getter),
189 weak_factory_(this) {
190 DCHECK(token_service_);
191 DCHECK(url_request_context_getter_);
194 ProfileSyncComponentsFactoryImpl::~ProfileSyncComponentsFactoryImpl() {
197 void ProfileSyncComponentsFactoryImpl::RegisterDataTypes(
198 ProfileSyncService* pss) {
199 syncer::ModelTypeSet disabled_types =
200 GetDisabledTypesFromCommandLine(*command_line_);
201 syncer::ModelTypeSet enabled_types =
202 GetEnabledTypesFromCommandLine(*command_line_);
203 RegisterCommonDataTypes(disabled_types, enabled_types, pss);
204 #if !defined(OS_ANDROID)
205 RegisterDesktopDataTypes(disabled_types, enabled_types, pss);
206 #endif
209 void ProfileSyncComponentsFactoryImpl::RegisterCommonDataTypes(
210 syncer::ModelTypeSet disabled_types,
211 syncer::ModelTypeSet enabled_types,
212 ProfileSyncService* pss) {
213 // TODO(stanisc): can DEVICE_INFO be one of disabled datatypes?
214 pss->RegisterDataTypeController(new DeviceInfoDataTypeController(
215 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
216 base::Bind(&ChromeReportUnrecoverableError),
217 this,
218 pss->GetLocalDeviceInfoProvider()));
220 // Autofill sync is enabled by default. Register unless explicitly
221 // disabled.
222 if (!disabled_types.Has(syncer::AUTOFILL)) {
223 pss->RegisterDataTypeController(
224 new AutofillDataTypeController(this, profile_));
227 // Autofill profile sync is enabled by default. Register unless explicitly
228 // disabled.
229 if (!disabled_types.Has(syncer::AUTOFILL_PROFILE)) {
230 pss->RegisterDataTypeController(
231 new AutofillProfileDataTypeController(this, profile_));
234 // Wallet data sync is enabled by default, but behind a syncer experiment
235 // enforced by the datatype controller. Register unless explicitly disabled.
236 bool wallet_disabled = disabled_types.Has(syncer::AUTOFILL_WALLET_DATA);
237 if (!wallet_disabled) {
238 pss->RegisterDataTypeController(
239 new browser_sync::AutofillWalletDataTypeController(
240 this, profile_, syncer::AUTOFILL_WALLET_DATA));
243 // Wallet metadata sync depends on Wallet data sync and is disabled by
244 // default. Register if Wallet data is syncing and metadata sync is explicitly
245 // enabled.
246 if (!wallet_disabled && enabled_types.Has(syncer::AUTOFILL_WALLET_METADATA)) {
247 pss->RegisterDataTypeController(
248 new browser_sync::AutofillWalletDataTypeController(
249 this, profile_, syncer::AUTOFILL_WALLET_METADATA));
252 // Bookmark sync is enabled by default. Register unless explicitly
253 // disabled.
254 if (!disabled_types.Has(syncer::BOOKMARKS)) {
255 pss->RegisterDataTypeController(
256 new BookmarkDataTypeController(this, profile_, pss));
259 const bool history_disabled =
260 profile_->GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled);
261 // TypedUrl sync is enabled by default. Register unless explicitly disabled,
262 // or if saving history is disabled.
263 if (!disabled_types.Has(syncer::TYPED_URLS) && !history_disabled) {
264 pss->RegisterDataTypeController(
265 new TypedUrlDataTypeController(this, profile_, pss));
268 // Delete directive sync is enabled by default. Register unless full history
269 // sync is disabled.
270 if (!disabled_types.Has(syncer::HISTORY_DELETE_DIRECTIVES) &&
271 !history_disabled) {
272 pss->RegisterDataTypeController(
273 new HistoryDeleteDirectivesDataTypeController(this, pss));
276 // Session sync is enabled by default. Register unless explicitly disabled.
277 // This is also disabled if the browser history is disabled, because the
278 // tab sync data is added to the web history on the server.
279 if (!disabled_types.Has(syncer::PROXY_TABS) && !history_disabled) {
280 pss->RegisterDataTypeController(new ProxyDataTypeController(
281 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
282 syncer::PROXY_TABS));
283 pss->RegisterDataTypeController(
284 new SessionDataTypeController(this,
285 profile_,
286 pss->GetSyncedWindowDelegatesGetter(),
287 pss->GetLocalDeviceInfoProvider()));
290 // Favicon sync is enabled by default. Register unless explicitly disabled.
291 if (!disabled_types.Has(syncer::FAVICON_IMAGES) &&
292 !disabled_types.Has(syncer::FAVICON_TRACKING) &&
293 !history_disabled) {
294 // crbug/384552. We disable error uploading for this data types for now.
295 pss->RegisterDataTypeController(
296 new UIDataTypeController(
297 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
298 base::Closure(),
299 syncer::FAVICON_IMAGES,
300 this));
301 pss->RegisterDataTypeController(
302 new UIDataTypeController(
303 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
304 base::Closure(),
305 syncer::FAVICON_TRACKING,
306 this));
309 // Password sync is enabled by default. Register unless explicitly
310 // disabled.
311 if (!disabled_types.Has(syncer::PASSWORDS)) {
312 pss->RegisterDataTypeController(
313 new PasswordDataTypeController(this, profile_));
316 if (!disabled_types.Has(syncer::PRIORITY_PREFERENCES)) {
317 pss->RegisterDataTypeController(
318 new UIDataTypeController(
319 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
320 base::Bind(&ChromeReportUnrecoverableError),
321 syncer::PRIORITY_PREFERENCES,
322 this));
325 // Article sync is disabled by default. Register only if explicitly enabled.
326 if (dom_distiller::IsEnableSyncArticlesSet()) {
327 pss->RegisterDataTypeController(
328 new UIDataTypeController(
329 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
330 base::Bind(&ChromeReportUnrecoverableError),
331 syncer::ARTICLES,
332 this));
335 #if defined(ENABLE_SUPERVISED_USERS)
336 pss->RegisterDataTypeController(
337 new SupervisedUserSyncDataTypeController(
338 syncer::SUPERVISED_USER_SETTINGS,
339 this,
340 profile_));
341 pss->RegisterDataTypeController(
342 new SupervisedUserSyncDataTypeController(
343 syncer::SUPERVISED_USER_WHITELISTS,
344 this,
345 profile_));
346 #endif
349 void ProfileSyncComponentsFactoryImpl::RegisterDesktopDataTypes(
350 syncer::ModelTypeSet disabled_types,
351 syncer::ModelTypeSet enabled_types,
352 ProfileSyncService* pss) {
353 #if defined(ENABLE_EXTENSIONS)
354 // App sync is enabled by default. Register unless explicitly
355 // disabled.
356 if (!disabled_types.Has(syncer::APPS)) {
357 pss->RegisterDataTypeController(
358 new ExtensionDataTypeController(syncer::APPS, this, profile_));
361 // Extension sync is enabled by default. Register unless explicitly
362 // disabled.
363 if (!disabled_types.Has(syncer::EXTENSIONS)) {
364 pss->RegisterDataTypeController(
365 new ExtensionDataTypeController(syncer::EXTENSIONS, this, profile_));
367 #endif
369 // Preference sync is enabled by default. Register unless explicitly
370 // disabled.
371 if (!disabled_types.Has(syncer::PREFERENCES)) {
372 pss->RegisterDataTypeController(
373 new UIDataTypeController(
374 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
375 base::Bind(&ChromeReportUnrecoverableError),
376 syncer::PREFERENCES,
377 this));
380 #if defined(ENABLE_THEMES)
381 // Theme sync is enabled by default. Register unless explicitly disabled.
382 if (!disabled_types.Has(syncer::THEMES)) {
383 pss->RegisterDataTypeController(
384 new ThemeDataTypeController(this, profile_));
386 #endif
388 // Search Engine sync is enabled by default. Register unless explicitly
389 // disabled.
390 if (!disabled_types.Has(syncer::SEARCH_ENGINES)) {
391 pss->RegisterDataTypeController(
392 new SearchEngineDataTypeController(this, profile_));
395 #if defined(ENABLE_EXTENSIONS)
396 // Extension setting sync is enabled by default. Register unless explicitly
397 // disabled.
398 if (!disabled_types.Has(syncer::EXTENSION_SETTINGS)) {
399 pss->RegisterDataTypeController(new ExtensionSettingDataTypeController(
400 syncer::EXTENSION_SETTINGS, this, profile_));
403 // App setting sync is enabled by default. Register unless explicitly
404 // disabled.
405 if (!disabled_types.Has(syncer::APP_SETTINGS)) {
406 pss->RegisterDataTypeController(new ExtensionSettingDataTypeController(
407 syncer::APP_SETTINGS, this, profile_));
409 #endif
411 #if defined(ENABLE_APP_LIST)
412 if (app_list::switches::IsAppListSyncEnabled()) {
413 pss->RegisterDataTypeController(
414 new UIDataTypeController(
415 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
416 base::Bind(&ChromeReportUnrecoverableError),
417 syncer::APP_LIST,
418 this));
420 #endif
422 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_CHROMEOS)
423 // Dictionary sync is enabled by default.
424 if (!disabled_types.Has(syncer::DICTIONARY)) {
425 pss->RegisterDataTypeController(
426 new UIDataTypeController(
427 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
428 base::Bind(&ChromeReportUnrecoverableError),
429 syncer::DICTIONARY,
430 this));
432 #endif
434 #if defined(ENABLE_SUPERVISED_USERS)
435 pss->RegisterDataTypeController(
436 new SupervisedUserSyncDataTypeController(
437 syncer::SUPERVISED_USERS,
438 this,
439 profile_));
440 pss->RegisterDataTypeController(
441 new SupervisedUserSyncDataTypeController(
442 syncer::SUPERVISED_USER_SHARED_SETTINGS,
443 this,
444 profile_));
445 #endif
447 #if defined(OS_CHROMEOS)
448 if (command_line_->HasSwitch(switches::kEnableWifiCredentialSync) &&
449 !disabled_types.Has(syncer::WIFI_CREDENTIALS)) {
450 pss->RegisterDataTypeController(
451 new UIDataTypeController(
452 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
453 base::Bind(&ChromeReportUnrecoverableError),
454 syncer::WIFI_CREDENTIALS,
455 this));
457 #endif
460 DataTypeManager* ProfileSyncComponentsFactoryImpl::CreateDataTypeManager(
461 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>&
462 debug_info_listener,
463 const DataTypeController::TypeMap* controllers,
464 const sync_driver::DataTypeEncryptionHandler* encryption_handler,
465 SyncBackendHost* backend,
466 DataTypeManagerObserver* observer) {
467 return new DataTypeManagerImpl(base::Bind(ChromeReportUnrecoverableError),
468 debug_info_listener,
469 controllers,
470 encryption_handler,
471 backend,
472 observer);
475 browser_sync::SyncBackendHost*
476 ProfileSyncComponentsFactoryImpl::CreateSyncBackendHost(
477 const std::string& name,
478 Profile* profile,
479 invalidation::InvalidationService* invalidator,
480 const base::WeakPtr<sync_driver::SyncPrefs>& sync_prefs,
481 const base::FilePath& sync_folder) {
482 return new browser_sync::SyncBackendHostImpl(name, profile, invalidator,
483 sync_prefs, sync_folder);
486 scoped_ptr<sync_driver::LocalDeviceInfoProvider>
487 ProfileSyncComponentsFactoryImpl::CreateLocalDeviceInfoProvider() {
488 return scoped_ptr<sync_driver::LocalDeviceInfoProvider>(
489 new browser_sync::LocalDeviceInfoProviderImpl());
492 base::WeakPtr<syncer::SyncableService> ProfileSyncComponentsFactoryImpl::
493 GetSyncableServiceForType(syncer::ModelType type) {
494 if (!profile_) { // For tests.
495 return base::WeakPtr<syncer::SyncableService>();
497 switch (type) {
498 case syncer::DEVICE_INFO:
499 return ProfileSyncServiceFactory::GetForProfile(profile_)
500 ->GetDeviceInfoSyncableService()
501 ->AsWeakPtr();
502 case syncer::PREFERENCES:
503 return PrefServiceSyncable::FromProfile(
504 profile_)->GetSyncableService(syncer::PREFERENCES)->AsWeakPtr();
505 case syncer::PRIORITY_PREFERENCES:
506 return PrefServiceSyncable::FromProfile(profile_)->GetSyncableService(
507 syncer::PRIORITY_PREFERENCES)->AsWeakPtr();
508 case syncer::AUTOFILL:
509 case syncer::AUTOFILL_PROFILE:
510 case syncer::AUTOFILL_WALLET_DATA:
511 case syncer::AUTOFILL_WALLET_METADATA: {
512 if (!web_data_service_.get())
513 return base::WeakPtr<syncer::SyncableService>();
514 if (type == syncer::AUTOFILL) {
515 return autofill::AutocompleteSyncableService::FromWebDataService(
516 web_data_service_.get())->AsWeakPtr();
517 } else if (type == syncer::AUTOFILL_PROFILE) {
518 return autofill::AutofillProfileSyncableService::FromWebDataService(
519 web_data_service_.get())->AsWeakPtr();
520 } else if (type == syncer::AUTOFILL_WALLET_METADATA) {
521 return autofill::AutofillWalletMetadataSyncableService::
522 FromWebDataService(web_data_service_.get())->AsWeakPtr();
524 return autofill::AutofillWalletSyncableService::FromWebDataService(
525 web_data_service_.get())->AsWeakPtr();
527 case syncer::SEARCH_ENGINES:
528 return TemplateURLServiceFactory::GetForProfile(profile_)->AsWeakPtr();
529 #if defined(ENABLE_EXTENSIONS)
530 case syncer::APPS:
531 case syncer::EXTENSIONS:
532 return ExtensionSyncService::Get(profile_)->AsWeakPtr();
533 case syncer::APP_SETTINGS:
534 case syncer::EXTENSION_SETTINGS:
535 return extensions::settings_sync_util::GetSyncableService(profile_, type)
536 ->AsWeakPtr();
537 #endif
538 #if defined(ENABLE_APP_LIST)
539 case syncer::APP_LIST:
540 return app_list::AppListSyncableServiceFactory::GetForProfile(profile_)->
541 AsWeakPtr();
542 #endif
543 #if defined(ENABLE_THEMES)
544 case syncer::THEMES:
545 return ThemeServiceFactory::GetForProfile(profile_)->
546 GetThemeSyncableService()->AsWeakPtr();
547 #endif
548 case syncer::HISTORY_DELETE_DIRECTIVES: {
549 history::HistoryService* history = HistoryServiceFactory::GetForProfile(
550 profile_, ServiceAccessType::EXPLICIT_ACCESS);
551 return history ? history->AsWeakPtr()
552 : base::WeakPtr<history::HistoryService>();
554 #if defined(ENABLE_SPELLCHECK)
555 case syncer::DICTIONARY:
556 return SpellcheckServiceFactory::GetForContext(profile_)->
557 GetCustomDictionary()->AsWeakPtr();
558 #endif
559 case syncer::FAVICON_IMAGES:
560 case syncer::FAVICON_TRACKING: {
561 browser_sync::FaviconCache* favicons =
562 ProfileSyncServiceFactory::GetForProfile(profile_)->
563 GetFaviconCache();
564 return favicons ? favicons->AsWeakPtr()
565 : base::WeakPtr<syncer::SyncableService>();
567 #if defined(ENABLE_SUPERVISED_USERS)
568 case syncer::SUPERVISED_USER_SETTINGS:
569 return SupervisedUserSettingsServiceFactory::GetForProfile(profile_)->
570 AsWeakPtr();
571 #if !defined(OS_ANDROID) && !defined(OS_IOS)
572 case syncer::SUPERVISED_USERS:
573 return SupervisedUserSyncServiceFactory::GetForProfile(profile_)->
574 AsWeakPtr();
575 case syncer::SUPERVISED_USER_SHARED_SETTINGS:
576 return SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(
577 profile_)->AsWeakPtr();
578 #endif
579 case syncer::SUPERVISED_USER_WHITELISTS:
580 return SupervisedUserServiceFactory::GetForProfile(profile_)
581 ->GetWhitelistService()
582 ->AsWeakPtr();
583 #endif
584 case syncer::ARTICLES: {
585 dom_distiller::DomDistillerService* service =
586 dom_distiller::DomDistillerServiceFactory::GetForBrowserContext(
587 profile_);
588 if (service)
589 return service->GetSyncableService()->AsWeakPtr();
590 return base::WeakPtr<syncer::SyncableService>();
592 case syncer::SESSIONS: {
593 return ProfileSyncServiceFactory::GetForProfile(profile_)->
594 GetSessionsSyncableService()->AsWeakPtr();
596 case syncer::PASSWORDS: {
597 scoped_refptr<password_manager::PasswordStore> password_store =
598 PasswordStoreFactory::GetForProfile(
599 profile_, ServiceAccessType::EXPLICIT_ACCESS);
600 return password_store.get() ? password_store->GetPasswordSyncableService()
601 : base::WeakPtr<syncer::SyncableService>();
603 #if defined(OS_CHROMEOS)
604 case syncer::WIFI_CREDENTIALS:
605 return wifi_sync::WifiCredentialSyncableServiceFactory::
606 GetForBrowserContext(profile_)->AsWeakPtr();
607 #endif
608 default:
609 // The following datatypes still need to be transitioned to the
610 // syncer::SyncableService API:
611 // Bookmarks
612 // Typed URLs
613 NOTREACHED();
614 return base::WeakPtr<syncer::SyncableService>();
618 class TokenServiceProvider
619 : public OAuth2TokenServiceRequest::TokenServiceProvider {
620 public:
621 TokenServiceProvider(
622 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
623 OAuth2TokenService* token_service);
625 // OAuth2TokenServiceRequest::TokenServiceProvider implementation.
626 scoped_refptr<base::SingleThreadTaskRunner> GetTokenServiceTaskRunner()
627 override;
628 OAuth2TokenService* GetTokenService() override;
630 private:
631 ~TokenServiceProvider() override;
633 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
634 OAuth2TokenService* token_service_;
637 TokenServiceProvider::TokenServiceProvider(
638 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
639 OAuth2TokenService* token_service)
640 : task_runner_(task_runner), token_service_(token_service) {
643 TokenServiceProvider::~TokenServiceProvider() {
646 scoped_refptr<base::SingleThreadTaskRunner>
647 TokenServiceProvider::GetTokenServiceTaskRunner() {
648 return task_runner_;
651 OAuth2TokenService* TokenServiceProvider::GetTokenService() {
652 return token_service_;
655 scoped_ptr<syncer::AttachmentService>
656 ProfileSyncComponentsFactoryImpl::CreateAttachmentService(
657 scoped_ptr<syncer::AttachmentStoreForSync> attachment_store,
658 const syncer::UserShare& user_share,
659 const std::string& store_birthday,
660 syncer::ModelType model_type,
661 syncer::AttachmentService::Delegate* delegate) {
662 scoped_ptr<syncer::AttachmentUploader> attachment_uploader;
663 scoped_ptr<syncer::AttachmentDownloader> attachment_downloader;
664 // Only construct an AttachmentUploader and AttachmentDownload if we have sync
665 // credentials. We may not have sync credentials because there may not be a
666 // signed in sync user (e.g. sync is running in "backup" mode).
667 if (!user_share.sync_credentials.email.empty() &&
668 !user_share.sync_credentials.scope_set.empty()) {
669 scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider>
670 token_service_provider(new TokenServiceProvider(
671 content::BrowserThread::GetMessageLoopProxyForThread(
672 content::BrowserThread::UI),
673 token_service_));
674 // TODO(maniscalco): Use shared (one per profile) thread-safe instances of
675 // AttachmentUploader and AttachmentDownloader instead of creating a new one
676 // per AttachmentService (bug 369536).
677 attachment_uploader.reset(new syncer::AttachmentUploaderImpl(
678 sync_service_url_, url_request_context_getter_,
679 user_share.sync_credentials.email,
680 user_share.sync_credentials.scope_set, token_service_provider,
681 store_birthday, model_type));
683 token_service_provider = new TokenServiceProvider(
684 content::BrowserThread::GetMessageLoopProxyForThread(
685 content::BrowserThread::UI),
686 token_service_);
687 attachment_downloader = syncer::AttachmentDownloader::Create(
688 sync_service_url_, url_request_context_getter_,
689 user_share.sync_credentials.email,
690 user_share.sync_credentials.scope_set, token_service_provider,
691 store_birthday, model_type);
694 // It is important that the initial backoff delay is relatively large. For
695 // whatever reason, the server may fail all requests for a short period of
696 // time. When this happens we don't want to overwhelm the server with
697 // requests so we use a large initial backoff.
698 const base::TimeDelta initial_backoff_delay =
699 base::TimeDelta::FromMinutes(30);
700 const base::TimeDelta max_backoff_delay = base::TimeDelta::FromHours(4);
701 scoped_ptr<syncer::AttachmentService> attachment_service(
702 new syncer::AttachmentServiceImpl(
703 attachment_store.Pass(), attachment_uploader.Pass(),
704 attachment_downloader.Pass(), delegate, initial_backoff_delay,
705 max_backoff_delay));
706 return attachment_service.Pass();
709 ProfileSyncComponentsFactory::SyncComponents
710 ProfileSyncComponentsFactoryImpl::CreateBookmarkSyncComponents(
711 ProfileSyncService* profile_sync_service,
712 sync_driver::DataTypeErrorHandler* error_handler) {
713 BookmarkModel* bookmark_model =
714 BookmarkModelFactory::GetForProfile(profile_sync_service->profile());
715 syncer::UserShare* user_share = profile_sync_service->GetUserShare();
716 // TODO(akalin): We may want to propagate this switch up eventually.
717 #if defined(OS_ANDROID)
718 const bool kExpectMobileBookmarksFolder = true;
719 #else
720 const bool kExpectMobileBookmarksFolder = false;
721 #endif
722 BookmarkModelAssociator* model_associator =
723 new BookmarkModelAssociator(bookmark_model,
724 profile_sync_service->profile(),
725 user_share,
726 error_handler,
727 kExpectMobileBookmarksFolder);
728 BookmarkChangeProcessor* change_processor =
729 new BookmarkChangeProcessor(profile_sync_service->profile(),
730 model_associator,
731 error_handler);
732 return SyncComponents(model_associator, change_processor);
735 ProfileSyncComponentsFactory::SyncComponents
736 ProfileSyncComponentsFactoryImpl::CreateTypedUrlSyncComponents(
737 ProfileSyncService* profile_sync_service,
738 history::HistoryBackend* history_backend,
739 sync_driver::DataTypeErrorHandler* error_handler) {
740 TypedUrlModelAssociator* model_associator =
741 new TypedUrlModelAssociator(profile_sync_service,
742 history_backend,
743 error_handler);
744 TypedUrlChangeProcessor* change_processor =
745 new TypedUrlChangeProcessor(profile_,
746 model_associator,
747 history_backend,
748 error_handler);
749 return SyncComponents(model_associator, change_processor);