1 // Copyright (c) 2013 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/invalidation/invalidation_service_factory.h" 6 7 #include "base/prefs/pref_registry.h" 8 #include "chrome/browser/invalidation/fake_invalidation_service.h" 9 #include "chrome/browser/invalidation/invalidation_service.h" 10 #include "chrome/browser/invalidation/invalidation_service_android.h" 11 #include "chrome/browser/invalidation/invalidator_storage.h" 12 #include "chrome/browser/invalidation/p2p_invalidation_service.h" 13 #include "chrome/browser/invalidation/ticl_invalidation_service.h" 14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/signin/profile_oauth2_token_service.h" 16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 17 #include "chrome/browser/signin/signin_manager.h" 18 #include "chrome/browser/signin/signin_manager_factory.h" 19 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h" 20 21 #if defined(OS_ANDROID) 22 #include "chrome/browser/invalidation/invalidation_controller_android.h" 23 #endif // defined(OS_ANDROID) 24 25 namespace invalidation { 26 27 // static 28 InvalidationService* InvalidationServiceFactory::GetForProfile( 29 Profile* profile) { 30 return static_cast<InvalidationService*>( 31 GetInstance()->GetServiceForBrowserContext(profile, true)); 32 } 33 34 // static 35 InvalidationServiceFactory* InvalidationServiceFactory::GetInstance() { 36 return Singleton<InvalidationServiceFactory>::get(); 37 } 38 39 InvalidationServiceFactory::InvalidationServiceFactory() 40 : BrowserContextKeyedServiceFactory( 41 "InvalidationService", 42 BrowserContextDependencyManager::GetInstance()), 43 build_fake_invalidators_(false) { 44 #if !defined(OS_ANDROID) 45 DependsOn(SigninManagerFactory::GetInstance()); 46 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); 47 #endif 48 } 49 50 InvalidationServiceFactory::~InvalidationServiceFactory() {} 51 52 namespace { 53 54 BrowserContextKeyedService* BuildP2PInvalidationService( 55 content::BrowserContext* context) { 56 Profile* profile = static_cast<Profile*>(context); 57 return new P2PInvalidationService(profile); 58 } 59 60 BrowserContextKeyedService* BuildFakeInvalidationService( 61 content::BrowserContext* context) { 62 return new FakeInvalidationService(); 63 } 64 65 } // namespace 66 67 void InvalidationServiceFactory::SetBuildOnlyFakeInvalidatorsForTest( 68 bool test_mode_enabled) { 69 build_fake_invalidators_ = test_mode_enabled; 70 } 71 72 P2PInvalidationService* 73 InvalidationServiceFactory::BuildAndUseP2PInvalidationServiceForTest( 74 content::BrowserContext* context) { 75 BrowserContextKeyedService* service = 76 SetTestingFactoryAndUse(context, BuildP2PInvalidationService); 77 return static_cast<P2PInvalidationService*>(service); 78 } 79 80 BrowserContextKeyedService* InvalidationServiceFactory::BuildServiceInstanceFor( 81 content::BrowserContext* context) const { 82 Profile* profile = static_cast<Profile*>(context); 83 84 if (build_fake_invalidators_) { 85 return BuildFakeInvalidationService(context); 86 } 87 88 #if defined(OS_ANDROID) 89 InvalidationServiceAndroid* service = new InvalidationServiceAndroid( 90 profile, 91 new InvalidationControllerAndroid()); 92 return service; 93 #else 94 SigninManagerBase* signin_manager = 95 SigninManagerFactory::GetForProfile(profile); 96 ProfileOAuth2TokenService* oauth2_token_service = 97 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); 98 99 TiclInvalidationService* service = new TiclInvalidationService( 100 signin_manager, 101 oauth2_token_service, 102 profile); 103 service->Init(); 104 return service; 105 #endif 106 } 107 108 void InvalidationServiceFactory::RegisterProfilePrefs( 109 user_prefs::PrefRegistrySyncable* registry) { 110 InvalidatorStorage::RegisterProfilePrefs(registry); 111 } 112 113 } // namespace invalidation 114