1 // Copyright 2014 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/invalidation/profile_invalidation_provider_factory.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_registry.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/invalidation/ticl_profile_settings_provider.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/services/gcm/gcm_profile_service.h"
13 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
15 #include "chrome/browser/signin/signin_manager_factory.h"
16 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
17 #include "chrome/common/chrome_content_client.h"
18 #include "chrome/common/pref_names.h"
19 #include "components/invalidation/impl/invalidation_state_tracker.h"
20 #include "components/invalidation/impl/invalidator_storage.h"
21 #include "components/invalidation/impl/profile_invalidation_provider.h"
22 #include "components/invalidation/impl/ticl_invalidation_service.h"
23 #include "components/invalidation/impl/ticl_settings_provider.h"
24 #include "components/invalidation/public/invalidation_service.h"
25 #include "components/keyed_service/content/browser_context_dependency_manager.h"
26 #include "components/pref_registry/pref_registry_syncable.h"
27 #include "components/signin/core/browser/profile_identity_provider.h"
28 #include "components/signin/core/browser/profile_oauth2_token_service.h"
29 #include "components/signin/core/browser/signin_manager.h"
30 #include "net/url_request/url_request_context_getter.h"
32 #if defined(OS_ANDROID)
33 #include "base/android/jni_android.h"
34 #include "components/invalidation/impl/invalidation_service_android.h"
35 #endif // defined(OS_ANDROID)
37 #if defined(OS_CHROMEOS)
38 #include "base/files/file_path.h"
39 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
40 #include "chrome/browser/chromeos/profiles/profile_helper.h"
41 #include "chrome/browser/chromeos/settings/device_identity_provider.h"
42 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h"
43 #include "components/user_manager/user_manager.h"
46 namespace invalidation
{
49 ProfileInvalidationProvider
* ProfileInvalidationProviderFactory::GetForProfile(
51 #if defined(OS_CHROMEOS)
52 // Using ProfileHelper::GetSigninProfile() here would lead to an infinite loop
53 // when this method is called during the creation of the sign-in profile
54 // itself. Using ProfileHelper::GetSigninProfileDir() is safe because it does
55 // not try to access the sign-in profile.
56 if (profile
->GetPath() == chromeos::ProfileHelper::GetSigninProfileDir() ||
57 (user_manager::UserManager::IsInitialized() &&
58 user_manager::UserManager::Get()->IsLoggedInAsGuest())) {
59 // The Chrome OS login and Chrome OS guest profiles do not have GAIA
60 // credentials and do not support invalidation.
64 return static_cast<ProfileInvalidationProvider
*>(
65 GetInstance()->GetServiceForBrowserContext(profile
, true));
69 ProfileInvalidationProviderFactory
*
70 ProfileInvalidationProviderFactory::GetInstance() {
71 return Singleton
<ProfileInvalidationProviderFactory
>::get();
74 ProfileInvalidationProviderFactory::ProfileInvalidationProviderFactory()
75 : BrowserContextKeyedServiceFactory(
76 "InvalidationService",
77 BrowserContextDependencyManager::GetInstance()),
78 testing_factory_(NULL
) {
79 #if !defined(OS_ANDROID)
80 DependsOn(SigninManagerFactory::GetInstance());
81 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
82 DependsOn(gcm::GCMProfileServiceFactory::GetInstance());
83 DependsOn(LoginUIServiceFactory::GetInstance());
87 ProfileInvalidationProviderFactory::~ProfileInvalidationProviderFactory() {
90 void ProfileInvalidationProviderFactory::RegisterTestingFactory(
91 TestingFactoryFunction testing_factory
) {
92 testing_factory_
= testing_factory
;
95 KeyedService
* ProfileInvalidationProviderFactory::BuildServiceInstanceFor(
96 content::BrowserContext
* context
) const {
98 return testing_factory_(context
).release();
100 #if defined(OS_ANDROID)
101 return new ProfileInvalidationProvider(scoped_ptr
<InvalidationService
>(
102 new InvalidationServiceAndroid(base::android::GetApplicationContext())));
105 scoped_ptr
<IdentityProvider
> identity_provider
;
107 #if defined(OS_CHROMEOS)
108 policy::BrowserPolicyConnectorChromeOS
* connector
=
109 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
110 if (user_manager::UserManager::IsInitialized() &&
111 user_manager::UserManager::Get()->IsLoggedInAsKioskApp() &&
112 connector
->IsEnterpriseManaged()) {
113 identity_provider
.reset(new chromeos::DeviceIdentityProvider(
114 chromeos::DeviceOAuth2TokenServiceFactory::Get()));
117 Profile
* profile
= Profile::FromBrowserContext(context
);
119 if (!identity_provider
) {
120 identity_provider
.reset(new ProfileIdentityProvider(
121 SigninManagerFactory::GetForProfile(profile
),
122 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
),
123 LoginUIServiceFactory::GetShowLoginPopupCallbackForProfile(profile
)));
126 scoped_ptr
<TiclInvalidationService
> service(new TiclInvalidationService(
128 identity_provider
.Pass(),
129 scoped_ptr
<TiclSettingsProvider
>(
130 new TiclProfileSettingsProvider(profile
)),
131 gcm::GCMProfileServiceFactory::GetForProfile(profile
)->driver(),
132 profile
->GetRequestContext()));
133 service
->Init(scoped_ptr
<syncer::InvalidationStateTracker
>(
134 new InvalidatorStorage(profile
->GetPrefs())));
136 return new ProfileInvalidationProvider(service
.Pass());
140 void ProfileInvalidationProviderFactory::RegisterProfilePrefs(
141 user_prefs::PrefRegistrySyncable
* registry
) {
142 registry
->RegisterBooleanPref(
143 prefs::kInvalidationServiceUseGCMChannel
,
144 true); // if no value in prefs, use GCM channel.
145 InvalidatorStorage::RegisterProfilePrefs(registry
);
148 } // namespace invalidation