[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / invalidation / invalidation_service_factory.cc
bloba78014317e32fcd3592e2d69214d9aaba3a6cd71
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.
5 #include "chrome/browser/invalidation/invalidation_service_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/fake_invalidation_service.h"
11 #include "chrome/browser/invalidation/invalidation_service_android.h"
12 #include "chrome/browser/invalidation/ticl_invalidation_service.h"
13 #include "chrome/browser/invalidation/ticl_profile_settings_provider.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/services/gcm/gcm_profile_service.h"
16 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
17 #include "chrome/browser/signin/profile_identity_provider.h"
18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
19 #include "chrome/browser/signin/signin_manager_factory.h"
20 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
21 #include "chrome/common/pref_names.h"
22 #include "components/invalidation/invalidation_service.h"
23 #include "components/invalidation/invalidator_storage.h"
24 #include "components/invalidation/ticl_settings_provider.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_oauth2_token_service.h"
28 #include "components/signin/core/browser/signin_manager.h"
29 #include "net/url_request/url_request_context_getter.h"
30 #include "sync/notifier/invalidation_state_tracker.h"
32 #if defined(OS_ANDROID)
33 #include "chrome/browser/invalidation/invalidation_controller_android.h"
34 #endif // defined(OS_ANDROID)
36 #if defined(OS_CHROMEOS)
37 #include "base/files/file_path.h"
38 #include "chrome/browser/chromeos/login/users/user_manager.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 #endif
45 namespace invalidation {
47 // static
48 InvalidationService* InvalidationServiceFactory::GetForProfile(
49 Profile* profile) {
50 #if defined(OS_CHROMEOS)
51 // Using ProfileHelper::GetSigninProfile() here would lead to an infinite loop
52 // when this method is called during the creation of the sign-in profile
53 // itself. Using ProfileHelper::GetSigninProfileDir() is safe because it does
54 // not try to access the sign-in profile.
55 if (profile->GetPath() == chromeos::ProfileHelper::GetSigninProfileDir()||
56 (chromeos::UserManager::IsInitialized() &&
57 chromeos::UserManager::Get()->IsLoggedInAsGuest())) {
58 // The Chrome OS login and Chrome OS guest profiles do not have GAIA
59 // credentials and do not support invalidation.
60 return NULL;
62 #endif
63 return static_cast<InvalidationService*>(
64 GetInstance()->GetServiceForBrowserContext(profile, true));
67 // static
68 InvalidationServiceFactory* InvalidationServiceFactory::GetInstance() {
69 return Singleton<InvalidationServiceFactory>::get();
72 InvalidationServiceFactory::InvalidationServiceFactory()
73 : BrowserContextKeyedServiceFactory(
74 "InvalidationService",
75 BrowserContextDependencyManager::GetInstance()),
76 testing_factory_(NULL) {
77 #if !defined(OS_ANDROID)
78 DependsOn(SigninManagerFactory::GetInstance());
79 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
80 DependsOn(gcm::GCMProfileServiceFactory::GetInstance());
81 DependsOn(LoginUIServiceFactory::GetInstance());
82 #endif
85 InvalidationServiceFactory::~InvalidationServiceFactory() {}
87 void InvalidationServiceFactory::RegisterTestingFactory(
88 TestingFactoryFunction testing_factory) {
89 testing_factory_ = testing_factory;
92 KeyedService* InvalidationServiceFactory::BuildServiceInstanceFor(
93 content::BrowserContext* context) const {
94 Profile* profile = static_cast<Profile*>(context);
96 if (testing_factory_)
97 return testing_factory_(context);
99 #if defined(OS_ANDROID)
100 return new InvalidationServiceAndroid(profile,
101 new InvalidationControllerAndroid());
102 #else
104 scoped_ptr<IdentityProvider> identity_provider;
106 #if defined(OS_CHROMEOS)
107 policy::BrowserPolicyConnectorChromeOS* connector =
108 g_browser_process->platform_part()->browser_policy_connector_chromeos();
109 if (chromeos::UserManager::IsInitialized() &&
110 chromeos::UserManager::Get()->IsLoggedInAsKioskApp() &&
111 connector->IsEnterpriseManaged()) {
112 identity_provider.reset(new chromeos::DeviceIdentityProvider(
113 chromeos::DeviceOAuth2TokenServiceFactory::Get()));
115 #endif
117 if (!identity_provider) {
118 identity_provider.reset(new ProfileIdentityProvider(
119 SigninManagerFactory::GetForProfile(profile),
120 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
121 LoginUIServiceFactory::GetForProfile(profile)));
124 TiclInvalidationService* service = new TiclInvalidationService(
125 identity_provider.Pass(),
126 scoped_ptr<TiclSettingsProvider>(
127 new TiclProfileSettingsProvider(profile)),
128 gcm::GCMProfileServiceFactory::GetForProfile(profile),
129 profile->GetRequestContext());
130 service->Init(scoped_ptr<syncer::InvalidationStateTracker>(
131 new InvalidatorStorage(profile->GetPrefs())));
132 return service;
133 #endif
136 void InvalidationServiceFactory::RegisterProfilePrefs(
137 user_prefs::PrefRegistrySyncable* registry) {
138 registry->RegisterBooleanPref(
139 prefs::kInvalidationServiceUseGCMChannel,
140 false,
141 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
142 InvalidatorStorage::RegisterProfilePrefs(registry);
145 } // namespace invalidation