Home | History | Annotate | Download | only in sync
      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/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_factory.h"
     23 #include "chrome/browser/signin/signin_manager.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/themes/theme_service_factory.h"
     28 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
     29 #include "chrome/browser/webdata/web_data_service_factory.h"
     30 #include "chrome/common/pref_names.h"
     31 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
     32 
     33 // static
     34 ProfileSyncServiceFactory* ProfileSyncServiceFactory::GetInstance() {
     35   return Singleton<ProfileSyncServiceFactory>::get();
     36 }
     37 
     38 // static
     39 ProfileSyncService* ProfileSyncServiceFactory::GetForProfile(
     40     Profile* profile) {
     41   if (!ProfileSyncService::IsSyncEnabled())
     42     return NULL;
     43 
     44   return static_cast<ProfileSyncService*>(
     45       GetInstance()->GetServiceForBrowserContext(profile, true));
     46 }
     47 
     48 ProfileSyncServiceFactory::ProfileSyncServiceFactory()
     49     : BrowserContextKeyedServiceFactory(
     50         "ProfileSyncService",
     51         BrowserContextDependencyManager::GetInstance()) {
     52 
     53   // The ProfileSyncService depends on various SyncableServices being around
     54   // when it is shut down.  Specify those dependencies here to build the proper
     55   // destruction order.
     56   DependsOn(AboutSigninInternalsFactory::GetInstance());
     57   DependsOn(autofill::PersonalDataManagerFactory::GetInstance());
     58   DependsOn(BookmarkModelFactory::GetInstance());
     59   DependsOn(extensions::ExtensionSystemFactory::GetInstance());
     60   DependsOn(GlobalErrorServiceFactory::GetInstance());
     61   DependsOn(HistoryServiceFactory::GetInstance());
     62   DependsOn(invalidation::InvalidationServiceFactory::GetInstance());
     63   DependsOn(PasswordStoreFactory::GetInstance());
     64   DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
     65   DependsOn(SigninManagerFactory::GetInstance());
     66   DependsOn(TemplateURLServiceFactory::GetInstance());
     67 #if defined(ENABLE_THEMES)
     68   DependsOn(ThemeServiceFactory::GetInstance());
     69 #endif
     70   DependsOn(WebDataServiceFactory::GetInstance());
     71 
     72   // The following have not been converted to BrowserContextKeyedServices yet,
     73   // and for now they are explicitly destroyed after the
     74   // BrowserContextDependencyManager is told to DestroyBrowserContextServices,
     75   // so they will be around when the ProfileSyncService is destroyed.
     76 
     77   // DependsOn(FaviconServiceFactory::GetInstance());
     78 }
     79 
     80 ProfileSyncServiceFactory::~ProfileSyncServiceFactory() {
     81 }
     82 
     83 BrowserContextKeyedService* ProfileSyncServiceFactory::BuildServiceInstanceFor(
     84     content::BrowserContext* context) const {
     85   Profile* profile = static_cast<Profile*>(context);
     86 
     87   ProfileSyncService::StartBehavior behavior =
     88       browser_defaults::kSyncAutoStarts ? ProfileSyncService::AUTO_START
     89                                         : ProfileSyncService::MANUAL_START;
     90 
     91   SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile);
     92 
     93   // TODO(atwilson): Change AboutSigninInternalsFactory to load on startup
     94   // once http://crbug.com/171406 has been fixed.
     95   AboutSigninInternalsFactory::GetForProfile(profile);
     96 
     97   // TODO(tim): Currently, AUTO/MANUAL settings refer to the *first* time sync
     98   // is set up and *not* a browser restart for a manual-start platform (where
     99   // sync has already been set up, and should be able to start without user
    100   // intervention). We can get rid of the browser_default eventually, but
    101   // need to take care that ProfileSyncService doesn't get tripped up between
    102   // those two cases. Bug 88109.
    103   ProfileSyncService* pss = new ProfileSyncService(
    104       new ProfileSyncComponentsFactoryImpl(profile,
    105                                            CommandLine::ForCurrentProcess()),
    106       profile,
    107       signin,
    108       behavior);
    109 
    110   pss->factory()->RegisterDataTypes(pss);
    111   pss->Initialize();
    112   return pss;
    113 }
    114 
    115 // static
    116 bool ProfileSyncServiceFactory::HasProfileSyncService(Profile* profile) {
    117   return GetInstance()->GetServiceForBrowserContext(profile, false) != NULL;
    118 }
    119