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.
4
5#include "chrome/browser/sync/profile_sync_service_factory.h"
6
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/ui/global_error/global_error_service_factory.h"
31#include "chrome/browser/webdata/web_data_service_factory.h"
32#include "chrome/common/pref_names.h"
33#include "components/keyed_service/content/browser_context_dependency_manager.h"
34#include "components/signin/core/browser/profile_oauth2_token_service.h"
35#include "components/signin/core/browser/signin_manager.h"
36#include "url/gurl.h"
37
38#if defined(ENABLE_EXTENSIONS)
39#include "chrome/browser/notifications/sync_notifier/chrome_notifier_service_factory.h"
40#include "extensions/browser/extension_system_provider.h"
41#include "extensions/browser/extensions_browser_client.h"
42#endif
43
44// static
45ProfileSyncServiceFactory* ProfileSyncServiceFactory::GetInstance() {
46  return Singleton<ProfileSyncServiceFactory>::get();
47}
48
49// static
50ProfileSyncService* ProfileSyncServiceFactory::GetForProfile(
51    Profile* profile) {
52  if (!ProfileSyncService::IsSyncEnabled())
53    return NULL;
54
55  return static_cast<ProfileSyncService*>(
56      GetInstance()->GetServiceForBrowserContext(profile, true));
57}
58
59ProfileSyncServiceFactory::ProfileSyncServiceFactory()
60    : BrowserContextKeyedServiceFactory(
61        "ProfileSyncService",
62        BrowserContextDependencyManager::GetInstance()) {
63  // The ProfileSyncService depends on various SyncableServices being around
64  // when it is shut down.  Specify those dependencies here to build the proper
65  // destruction order.
66  DependsOn(AboutSigninInternalsFactory::GetInstance());
67  DependsOn(autofill::PersonalDataManagerFactory::GetInstance());
68  DependsOn(BookmarkModelFactory::GetInstance());
69  DependsOn(ChromeSigninClientFactory::GetInstance());
70  DependsOn(GlobalErrorServiceFactory::GetInstance());
71  DependsOn(HistoryServiceFactory::GetInstance());
72  DependsOn(invalidation::ProfileInvalidationProviderFactory::GetInstance());
73  DependsOn(PasswordStoreFactory::GetInstance());
74  DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
75  DependsOn(SigninManagerFactory::GetInstance());
76  DependsOn(TemplateURLServiceFactory::GetInstance());
77#if defined(ENABLE_THEMES)
78  DependsOn(ThemeServiceFactory::GetInstance());
79#endif
80  DependsOn(WebDataServiceFactory::GetInstance());
81#if defined(ENABLE_EXTENSIONS)
82  DependsOn(
83      extensions::ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
84  DependsOn(notifier::ChromeNotifierServiceFactory::GetInstance());
85#endif
86
87  // The following have not been converted to KeyedServices yet,
88  // and for now they are explicitly destroyed after the
89  // BrowserContextDependencyManager is told to DestroyBrowserContextServices,
90  // so they will be around when the ProfileSyncService is destroyed.
91
92  // DependsOn(FaviconServiceFactory::GetInstance());
93}
94
95ProfileSyncServiceFactory::~ProfileSyncServiceFactory() {
96}
97
98KeyedService* ProfileSyncServiceFactory::BuildServiceInstanceFor(
99    content::BrowserContext* context) const {
100  Profile* profile = static_cast<Profile*>(context);
101
102  SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile);
103
104  // Always create the GCMProfileService instance such that we can listen to
105  // the profile notifications and purge the GCM store when the profile is
106  // being signed out.
107  gcm::GCMProfileServiceFactory::GetForProfile(profile);
108
109  // TODO(atwilson): Change AboutSigninInternalsFactory to load on startup
110  // once http://crbug.com/171406 has been fixed.
111  AboutSigninInternalsFactory::GetForProfile(profile);
112
113  const GURL sync_service_url =
114      ProfileSyncService::GetSyncServiceURL(*CommandLine::ForCurrentProcess());
115
116  scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper(
117      new SupervisedUserSigninManagerWrapper(profile, signin));
118  std::string account_id = signin_wrapper->GetAccountIdToUse();
119  OAuth2TokenService::ScopeSet scope_set;
120  scope_set.insert(signin_wrapper->GetSyncScopeToUse());
121  ProfileOAuth2TokenService* token_service =
122      ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
123  net::URLRequestContextGetter* url_request_context_getter =
124      profile->GetRequestContext();
125
126  // TODO(tim): Currently, AUTO/MANUAL settings refer to the *first* time sync
127  // is set up and *not* a browser restart for a manual-start platform (where
128  // sync has already been set up, and should be able to start without user
129  // intervention). We can get rid of the browser_default eventually, but
130  // need to take care that ProfileSyncService doesn't get tripped up between
131  // those two cases. Bug 88109.
132  browser_sync::ProfileSyncServiceStartBehavior behavior =
133      browser_defaults::kSyncAutoStarts ? browser_sync::AUTO_START
134                                        : browser_sync::MANUAL_START;
135  ProfileSyncService* pss = new ProfileSyncService(
136      scoped_ptr<ProfileSyncComponentsFactory>(
137          new ProfileSyncComponentsFactoryImpl(
138              profile,
139              CommandLine::ForCurrentProcess(),
140              sync_service_url,
141              token_service,
142              url_request_context_getter)),
143      profile,
144      signin_wrapper.Pass(),
145      token_service,
146      behavior);
147
148  pss->factory()->RegisterDataTypes(pss);
149  pss->Initialize();
150  return pss;
151}
152
153// static
154bool ProfileSyncServiceFactory::HasProfileSyncService(Profile* profile) {
155  return GetInstance()->GetServiceForBrowserContext(profile, false) != NULL;
156}
157