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_service_factory.h"
7 #include "base/command_line.h"
8 #include "base/memory/singleton.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/autofill/personal_data_manager_factory.h"
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
12 #include "chrome/browser/defaults.h"
13 #include "chrome/browser/extensions/extension_system_factory.h"
14 #include "chrome/browser/history/history_service_factory.h"
15 #include "chrome/browser/invalidation/invalidation_service_factory.h"
16 #include "chrome/browser/password_manager/password_store_factory.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/search_engines/template_url_service_factory.h"
20 #include "chrome/browser/sessions/tab_restore_service_factory.h"
21 #include "chrome/browser/signin/about_signin_internals_factory.h"
22 #include "chrome/browser/signin/profile_oauth2_token_service.h"
23 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
24 #include "chrome/browser/signin/signin_manager.h"
25 #include "chrome/browser/signin/signin_manager_factory.h"
26 #include "chrome/browser/sync/profile_sync_components_factory_impl.h"
27 #include "chrome/browser/sync/profile_sync_service.h"
28 #include "chrome/browser/themes/theme_service_factory.h"
29 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
30 #include "chrome/browser/webdata/web_data_service_factory.h"
31 #include "chrome/common/pref_names.h"
32 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
35 ProfileSyncServiceFactory
* ProfileSyncServiceFactory::GetInstance() {
36 return Singleton
<ProfileSyncServiceFactory
>::get();
40 ProfileSyncService
* ProfileSyncServiceFactory::GetForProfile(
42 if (!ProfileSyncService::IsSyncEnabled())
45 return static_cast<ProfileSyncService
*>(
46 GetInstance()->GetServiceForBrowserContext(profile
, true));
49 ProfileSyncServiceFactory::ProfileSyncServiceFactory()
50 : BrowserContextKeyedServiceFactory(
52 BrowserContextDependencyManager::GetInstance()) {
54 // The ProfileSyncService depends on various SyncableServices being around
55 // when it is shut down. Specify those dependencies here to build the proper
57 DependsOn(AboutSigninInternalsFactory::GetInstance());
58 DependsOn(autofill::PersonalDataManagerFactory::GetInstance());
59 DependsOn(BookmarkModelFactory::GetInstance());
60 DependsOn(extensions::ExtensionSystemFactory::GetInstance());
61 DependsOn(GlobalErrorServiceFactory::GetInstance());
62 DependsOn(HistoryServiceFactory::GetInstance());
63 DependsOn(invalidation::InvalidationServiceFactory::GetInstance());
64 DependsOn(PasswordStoreFactory::GetInstance());
65 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
66 DependsOn(SigninManagerFactory::GetInstance());
67 DependsOn(TemplateURLServiceFactory::GetInstance());
68 #if defined(ENABLE_THEMES)
69 DependsOn(ThemeServiceFactory::GetInstance());
71 DependsOn(WebDataServiceFactory::GetInstance());
73 // The following have not been converted to BrowserContextKeyedServices yet,
74 // and for now they are explicitly destroyed after the
75 // BrowserContextDependencyManager is told to DestroyBrowserContextServices,
76 // so they will be around when the ProfileSyncService is destroyed.
78 // DependsOn(FaviconServiceFactory::GetInstance());
81 ProfileSyncServiceFactory::~ProfileSyncServiceFactory() {
84 BrowserContextKeyedService
* ProfileSyncServiceFactory::BuildServiceInstanceFor(
85 content::BrowserContext
* context
) const {
86 Profile
* profile
= static_cast<Profile
*>(context
);
88 ProfileSyncService::StartBehavior behavior
=
89 browser_defaults::kSyncAutoStarts
? ProfileSyncService::AUTO_START
90 : ProfileSyncService::MANUAL_START
;
92 SigninManagerBase
* signin
= SigninManagerFactory::GetForProfile(profile
);
94 // TODO(atwilson): Change AboutSigninInternalsFactory to load on startup
95 // once http://crbug.com/171406 has been fixed.
96 AboutSigninInternalsFactory::GetForProfile(profile
);
98 // TODO(tim): Currently, AUTO/MANUAL settings refer to the *first* time sync
99 // is set up and *not* a browser restart for a manual-start platform (where
100 // sync has already been set up, and should be able to start without user
101 // intervention). We can get rid of the browser_default eventually, but
102 // need to take care that ProfileSyncService doesn't get tripped up between
103 // those two cases. Bug 88109.
104 ProfileSyncService
* pss
= new ProfileSyncService(
105 new ProfileSyncComponentsFactoryImpl(profile
,
106 CommandLine::ForCurrentProcess()),
109 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
),
112 pss
->factory()->RegisterDataTypes(pss
);
118 bool ProfileSyncServiceFactory::HasProfileSyncService(Profile
* profile
) {
119 return GetInstance()->GetServiceForBrowserContext(profile
, false) != NULL
;