Fire an error if a pref used in the UI is missing once all prefs are fetched.
[chromium-blink-merge.git] / chrome / browser / sync / profile_sync_components_factory_impl.cc
blob1d0514773111a980750e0a34c7b756e91565c794
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 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 history::HistoryService* history = HistoryServiceFactory::GetForProfile(
535 profile_, ServiceAccessType::EXPLICIT_ACCESS);
536 return history ? history->AsWeakPtr()
537 : base::WeakPtr<history::HistoryService>();
539 #if defined(ENABLE_SPELLCHECK)
540 case syncer::DICTIONARY:
541 return SpellcheckServiceFactory::GetForContext(profile_)->
542 GetCustomDictionary()->AsWeakPtr();
543 #endif
544 case syncer::FAVICON_IMAGES:
545 case syncer::FAVICON_TRACKING: {
546 browser_sync::FaviconCache* favicons =
547 ProfileSyncServiceFactory::GetForProfile(profile_)->
548 GetFaviconCache();
549 return favicons ? favicons->AsWeakPtr()
550 : base::WeakPtr<syncer::SyncableService>();
552 #if defined(ENABLE_SUPERVISED_USERS)
553 case syncer::SUPERVISED_USER_SETTINGS:
554 return SupervisedUserSettingsServiceFactory::GetForProfile(profile_)->
555 AsWeakPtr();
556 case syncer::SUPERVISED_USERS:
557 return SupervisedUserSyncServiceFactory::GetForProfile(profile_)->
558 AsWeakPtr();
559 case syncer::SUPERVISED_USER_SHARED_SETTINGS:
560 return SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(
561 profile_)->AsWeakPtr();
562 case syncer::SUPERVISED_USER_WHITELISTS:
563 return SupervisedUserServiceFactory::GetForProfile(profile_)
564 ->GetWhitelistService()
565 ->AsWeakPtr();
566 #endif
567 case syncer::ARTICLES: {
568 dom_distiller::DomDistillerService* service =
569 dom_distiller::DomDistillerServiceFactory::GetForBrowserContext(
570 profile_);
571 if (service)
572 return service->GetSyncableService()->AsWeakPtr();
573 return base::WeakPtr<syncer::SyncableService>();
575 case syncer::SESSIONS: {
576 return ProfileSyncServiceFactory::GetForProfile(profile_)->
577 GetSessionsSyncableService()->AsWeakPtr();
579 case syncer::PASSWORDS: {
580 #if defined(PASSWORD_MANAGER_ENABLE_SYNC)
581 scoped_refptr<password_manager::PasswordStore> password_store =
582 PasswordStoreFactory::GetForProfile(
583 profile_, ServiceAccessType::EXPLICIT_ACCESS);
584 return password_store.get() ? password_store->GetPasswordSyncableService()
585 : base::WeakPtr<syncer::SyncableService>();
586 #else
587 return base::WeakPtr<syncer::SyncableService>();
588 #endif
590 #if defined(OS_CHROMEOS)
591 case syncer::WIFI_CREDENTIALS:
592 return wifi_sync::WifiCredentialSyncableServiceFactory::
593 GetForBrowserContext(profile_)->AsWeakPtr();
594 #endif
595 default:
596 // The following datatypes still need to be transitioned to the
597 // syncer::SyncableService API:
598 // Bookmarks
599 // Typed URLs
600 NOTREACHED();
601 return base::WeakPtr<syncer::SyncableService>();
605 class TokenServiceProvider
606 : public OAuth2TokenServiceRequest::TokenServiceProvider {
607 public:
608 TokenServiceProvider(
609 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
610 OAuth2TokenService* token_service);
612 // OAuth2TokenServiceRequest::TokenServiceProvider implementation.
613 scoped_refptr<base::SingleThreadTaskRunner> GetTokenServiceTaskRunner()
614 override;
615 OAuth2TokenService* GetTokenService() override;
617 private:
618 ~TokenServiceProvider() override;
620 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
621 OAuth2TokenService* token_service_;
624 TokenServiceProvider::TokenServiceProvider(
625 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
626 OAuth2TokenService* token_service)
627 : task_runner_(task_runner), token_service_(token_service) {
630 TokenServiceProvider::~TokenServiceProvider() {
633 scoped_refptr<base::SingleThreadTaskRunner>
634 TokenServiceProvider::GetTokenServiceTaskRunner() {
635 return task_runner_;
638 OAuth2TokenService* TokenServiceProvider::GetTokenService() {
639 return token_service_;
642 scoped_ptr<syncer::AttachmentService>
643 ProfileSyncComponentsFactoryImpl::CreateAttachmentService(
644 scoped_ptr<syncer::AttachmentStore> attachment_store,
645 const syncer::UserShare& user_share,
646 const std::string& store_birthday,
647 syncer::ModelType model_type,
648 syncer::AttachmentService::Delegate* delegate) {
649 scoped_ptr<syncer::AttachmentUploader> attachment_uploader;
650 scoped_ptr<syncer::AttachmentDownloader> attachment_downloader;
651 // Only construct an AttachmentUploader and AttachmentDownload if we have sync
652 // credentials. We may not have sync credentials because there may not be a
653 // signed in sync user (e.g. sync is running in "backup" mode).
654 if (!user_share.sync_credentials.email.empty() &&
655 !user_share.sync_credentials.scope_set.empty()) {
656 scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider>
657 token_service_provider(new TokenServiceProvider(
658 content::BrowserThread::GetMessageLoopProxyForThread(
659 content::BrowserThread::UI),
660 token_service_));
661 // TODO(maniscalco): Use shared (one per profile) thread-safe instances of
662 // AttachmentUploader and AttachmentDownloader instead of creating a new one
663 // per AttachmentService (bug 369536).
664 attachment_uploader.reset(new syncer::AttachmentUploaderImpl(
665 sync_service_url_, url_request_context_getter_,
666 user_share.sync_credentials.email,
667 user_share.sync_credentials.scope_set, token_service_provider,
668 store_birthday, model_type));
670 token_service_provider = new TokenServiceProvider(
671 content::BrowserThread::GetMessageLoopProxyForThread(
672 content::BrowserThread::UI),
673 token_service_);
674 attachment_downloader = syncer::AttachmentDownloader::Create(
675 sync_service_url_, url_request_context_getter_,
676 user_share.sync_credentials.email,
677 user_share.sync_credentials.scope_set, token_service_provider,
678 store_birthday, model_type);
681 // It is important that the initial backoff delay is relatively large. For
682 // whatever reason, the server may fail all requests for a short period of
683 // time. When this happens we don't want to overwhelm the server with
684 // requests so we use a large initial backoff.
685 const base::TimeDelta initial_backoff_delay =
686 base::TimeDelta::FromMinutes(30);
687 const base::TimeDelta max_backoff_delay = base::TimeDelta::FromHours(4);
688 scoped_ptr<syncer::AttachmentService> attachment_service(
689 new syncer::AttachmentServiceImpl(
690 attachment_store.Pass(), attachment_uploader.Pass(),
691 attachment_downloader.Pass(), delegate, initial_backoff_delay,
692 max_backoff_delay));
693 return attachment_service.Pass();
696 ProfileSyncComponentsFactory::SyncComponents
697 ProfileSyncComponentsFactoryImpl::CreateBookmarkSyncComponents(
698 ProfileSyncService* profile_sync_service,
699 sync_driver::DataTypeErrorHandler* error_handler) {
700 BookmarkModel* bookmark_model =
701 BookmarkModelFactory::GetForProfile(profile_sync_service->profile());
702 syncer::UserShare* user_share = profile_sync_service->GetUserShare();
703 // TODO(akalin): We may want to propagate this switch up eventually.
704 #if defined(OS_ANDROID)
705 const bool kExpectMobileBookmarksFolder = true;
706 #else
707 const bool kExpectMobileBookmarksFolder = false;
708 #endif
709 BookmarkModelAssociator* model_associator =
710 new BookmarkModelAssociator(bookmark_model,
711 profile_sync_service->profile(),
712 user_share,
713 error_handler,
714 kExpectMobileBookmarksFolder);
715 BookmarkChangeProcessor* change_processor =
716 new BookmarkChangeProcessor(profile_sync_service->profile(),
717 model_associator,
718 error_handler);
719 return SyncComponents(model_associator, change_processor);
722 ProfileSyncComponentsFactory::SyncComponents
723 ProfileSyncComponentsFactoryImpl::CreateTypedUrlSyncComponents(
724 ProfileSyncService* profile_sync_service,
725 history::HistoryBackend* history_backend,
726 sync_driver::DataTypeErrorHandler* error_handler) {
727 TypedUrlModelAssociator* model_associator =
728 new TypedUrlModelAssociator(profile_sync_service,
729 history_backend,
730 error_handler);
731 TypedUrlChangeProcessor* change_processor =
732 new TypedUrlChangeProcessor(profile_,
733 model_associator,
734 history_backend,
735 error_handler);
736 return SyncComponents(model_associator, change_processor);