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/history/history_service_factory.h"
14 #include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
15 #include "chrome/browser/password_manager/password_store_factory.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/browser/services/gcm/gcm_profile_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/chrome_signin_client_factory.h"
23 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
24 #include "chrome/browser/signin/signin_manager_factory.h"
25 #include "chrome/browser/sync/profile_sync_components_factory_impl.h"
26 #include "chrome/browser/sync/profile_sync_service.h"
27 #include "chrome/browser/sync/startup_controller.h"
28 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
29 #include "chrome/browser/themes/theme_service_factory.h"
30 #include "chrome/browser/web_data_service_factory.h"
31 #include "chrome/common/channel_info.h"
32 #include "components/keyed_service/content/browser_context_dependency_manager.h"
33 #include "components/signin/core/browser/profile_oauth2_token_service.h"
34 #include "components/signin/core/browser/signin_manager.h"
35 #include "components/sync_driver/signin_manager_wrapper.h"
36 #include "components/sync_driver/sync_util.h"
37 #include "components/variations/variations_associated_data.h"
40 #if defined(ENABLE_EXTENSIONS)
41 #include "extensions/browser/extension_system_provider.h"
42 #include "extensions/browser/extensions_browser_client.h"
45 #if !defined(OS_ANDROID)
46 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
50 ProfileSyncServiceFactory
* ProfileSyncServiceFactory::GetInstance() {
51 return base::Singleton
<ProfileSyncServiceFactory
>::get();
55 ProfileSyncService
* ProfileSyncServiceFactory::GetForProfile(
57 if (!ProfileSyncService::IsSyncAllowedByFlag())
60 // Disable sync experimentally to measure impact on startup time. Supervised
61 // users are unaffected, since supervised users rely completely on sync.
62 // TODO(mlerman): Remove this after the experiment. crbug.com/454788
63 if (!profile
->IsSupervised() &&
64 !variations::GetVariationParamValue("LightSpeed", "DisableSync")
69 return static_cast<ProfileSyncService
*>(
70 GetInstance()->GetServiceForBrowserContext(profile
, true));
74 sync_driver::SyncService
*
75 ProfileSyncServiceFactory::GetSyncServiceForBrowserContext(
76 content::BrowserContext
* context
) {
77 return GetForProfile(Profile::FromBrowserContext(context
));
80 ProfileSyncServiceFactory::ProfileSyncServiceFactory()
81 : BrowserContextKeyedServiceFactory(
83 BrowserContextDependencyManager::GetInstance()) {
84 // The ProfileSyncService depends on various SyncableServices being around
85 // when it is shut down. Specify those dependencies here to build the proper
87 DependsOn(AboutSigninInternalsFactory::GetInstance());
88 DependsOn(autofill::PersonalDataManagerFactory::GetInstance());
89 DependsOn(BookmarkModelFactory::GetInstance());
90 DependsOn(ChromeSigninClientFactory::GetInstance());
91 #if !defined(OS_ANDROID)
92 DependsOn(GlobalErrorServiceFactory::GetInstance());
94 DependsOn(HistoryServiceFactory::GetInstance());
95 DependsOn(invalidation::ProfileInvalidationProviderFactory::GetInstance());
96 DependsOn(PasswordStoreFactory::GetInstance());
97 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
98 DependsOn(SigninManagerFactory::GetInstance());
99 DependsOn(TemplateURLServiceFactory::GetInstance());
100 #if defined(ENABLE_THEMES)
101 DependsOn(ThemeServiceFactory::GetInstance());
103 DependsOn(WebDataServiceFactory::GetInstance());
104 #if defined(ENABLE_EXTENSIONS)
106 extensions::ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
109 // The following have not been converted to KeyedServices yet,
110 // and for now they are explicitly destroyed after the
111 // BrowserContextDependencyManager is told to DestroyBrowserContextServices,
112 // so they will be around when the ProfileSyncService is destroyed.
114 // DependsOn(FaviconServiceFactory::GetInstance());
117 ProfileSyncServiceFactory::~ProfileSyncServiceFactory() {
120 KeyedService
* ProfileSyncServiceFactory::BuildServiceInstanceFor(
121 content::BrowserContext
* context
) const {
122 Profile
* profile
= static_cast<Profile
*>(context
);
124 SigninManagerBase
* signin
= SigninManagerFactory::GetForProfile(profile
);
126 // Always create the GCMProfileService instance such that we can listen to
127 // the profile notifications and purge the GCM store when the profile is
129 gcm::GCMProfileServiceFactory::GetForProfile(profile
);
131 // TODO(atwilson): Change AboutSigninInternalsFactory to load on startup
132 // once http://crbug.com/171406 has been fixed.
133 AboutSigninInternalsFactory::GetForProfile(profile
);
135 const GURL sync_service_url
= GetSyncServiceURL(
136 *base::CommandLine::ForCurrentProcess(), chrome::GetChannel());
138 scoped_ptr
<SigninManagerWrapper
> signin_wrapper(
139 new SupervisedUserSigninManagerWrapper(profile
, signin
));
140 std::string account_id
= signin_wrapper
->GetAccountIdToUse();
141 OAuth2TokenService::ScopeSet scope_set
;
142 scope_set
.insert(signin_wrapper
->GetSyncScopeToUse());
143 ProfileOAuth2TokenService
* token_service
=
144 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
);
145 net::URLRequestContextGetter
* url_request_context_getter
=
146 profile
->GetRequestContext();
148 // TODO(tim): Currently, AUTO/MANUAL settings refer to the *first* time sync
149 // is set up and *not* a browser restart for a manual-start platform (where
150 // sync has already been set up, and should be able to start without user
151 // intervention). We can get rid of the browser_default eventually, but
152 // need to take care that ProfileSyncService doesn't get tripped up between
153 // those two cases. Bug 88109.
154 browser_sync::ProfileSyncServiceStartBehavior behavior
=
155 browser_defaults::kSyncAutoStarts
? browser_sync::AUTO_START
156 : browser_sync::MANUAL_START
;
157 ProfileSyncService
* pss
= new ProfileSyncService(
158 scoped_ptr
<sync_driver::SyncApiComponentFactory
>(
159 new ProfileSyncComponentsFactoryImpl(
160 profile
, base::CommandLine::ForCurrentProcess(), sync_service_url
,
161 token_service
, url_request_context_getter
)),
162 profile
, signin_wrapper
.Pass(), token_service
, behavior
);
163 pss
->factory()->Initialize(pss
);
164 pss
->factory()->RegisterDataTypes();
170 bool ProfileSyncServiceFactory::HasProfileSyncService(Profile
* profile
) {
171 return GetInstance()->GetServiceForBrowserContext(profile
, false) != NULL
;