profile_invalidation_provider_factory.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// found in the LICENSE file.
4a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
5f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
6a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
70529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/memory/scoped_ptr.h"
8eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/prefs/pref_registry.h"
923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "chrome/browser/browser_process.h"
105c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/invalidation/ticl_profile_settings_provider.h"
11a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/services/gcm/gcm_profile_service.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "chrome/browser/signin/profile_identity_provider.h"
15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
16a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
1723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
18f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/common/chrome_content_client.h"
19effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/common/pref_names.h"
200de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)#include "components/invalidation/invalidation_service.h"
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/invalidation/invalidation_state_tracker.h"
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/invalidation/invalidator_storage.h"
23f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/invalidation/profile_invalidation_provider.h"
24f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/invalidation/ticl_invalidation_service.h"
250de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)#include "components/invalidation/ticl_settings_provider.h"
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/content/browser_context_dependency_manager.h"
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
28effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/profile_oauth2_token_service.h"
29e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "components/signin/core/browser/signin_manager.h"
300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "net/url_request/url_request_context_getter.h"
31a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_ANDROID)
3358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/invalidation/invalidation_controller_android.h"
34f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/invalidation/invalidation_service_android.h"
3558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif  // defined(OS_ANDROID)
3658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#if defined(OS_CHROMEOS)
38c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "base/files/file_path.h"
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/chromeos/login/users/user_manager.h"
4023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
41c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "chrome/browser/chromeos/profiles/profile_helper.h"
420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "chrome/browser/chromeos/settings/device_identity_provider.h"
4323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h"
4423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#endif
4523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
46a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace invalidation {
47a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// static
49f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)ProfileInvalidationProvider* ProfileInvalidationProviderFactory::GetForProfile(
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    Profile* profile) {
51c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#if defined(OS_CHROMEOS)
52c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Using ProfileHelper::GetSigninProfile() here would lead to an infinite loop
53c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // when this method is called during the creation of the sign-in profile
54c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // itself. Using ProfileHelper::GetSigninProfileDir() is safe because it does
55c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // not try to access the sign-in profile.
56c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (profile->GetPath() == chromeos::ProfileHelper::GetSigninProfileDir()||
57c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      (chromeos::UserManager::IsInitialized() &&
58c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch       chromeos::UserManager::Get()->IsLoggedInAsGuest())) {
59c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // The Chrome OS login and Chrome OS guest profiles do not have GAIA
60c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // credentials and do not support invalidation.
61c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    return NULL;
62c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
63c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#endif
64f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return static_cast<ProfileInvalidationProvider*>(
65eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      GetInstance()->GetServiceForBrowserContext(profile, true));
66eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
67a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
68a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// static
69f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)ProfileInvalidationProviderFactory*
70f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)ProfileInvalidationProviderFactory::GetInstance() {
71f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return Singleton<ProfileInvalidationProviderFactory>::get();
72a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
73a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
74f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)ProfileInvalidationProviderFactory::ProfileInvalidationProviderFactory()
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    : BrowserContextKeyedServiceFactory(
7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        "InvalidationService",
77eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        BrowserContextDependencyManager::GetInstance()),
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      testing_factory_(NULL) {
79a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#if !defined(OS_ANDROID)
80a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DependsOn(SigninManagerFactory::GetInstance());
81eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DependsOn(gcm::GCMProfileServiceFactory::GetInstance());
8323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  DependsOn(LoginUIServiceFactory::GetInstance());
84a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#endif
85a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
86a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
87f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)ProfileInvalidationProviderFactory::~ProfileInvalidationProviderFactory() {
88f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
89a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
90f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void ProfileInvalidationProviderFactory::RegisterTestingFactory(
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    TestingFactoryFunction testing_factory) {
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  testing_factory_ = testing_factory;
93eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
94eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
95f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)KeyedService* ProfileInvalidationProviderFactory::BuildServiceInstanceFor(
96a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    content::BrowserContext* context) const {
97a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  Profile* profile = static_cast<Profile*>(context);
98eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (testing_factory_)
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return testing_factory_(context);
101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
102a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#if defined(OS_ANDROID)
103f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return new ProfileInvalidationProvider(scoped_ptr<InvalidationService>(
104f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      new InvalidationServiceAndroid(profile,
105f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                     new InvalidationControllerAndroid())));
106a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#else
10723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
1080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  scoped_ptr<IdentityProvider> identity_provider;
10923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
11023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#if defined(OS_CHROMEOS)
11123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  policy::BrowserPolicyConnectorChromeOS* connector =
11223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      g_browser_process->platform_part()->browser_policy_connector_chromeos();
11323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  if (chromeos::UserManager::IsInitialized() &&
11423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      chromeos::UserManager::Get()->IsLoggedInAsKioskApp() &&
11523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      connector->IsEnterpriseManaged()) {
1160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    identity_provider.reset(new chromeos::DeviceIdentityProvider(
11723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        chromeos::DeviceOAuth2TokenServiceFactory::Get()));
11823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  }
11923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#endif
12023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
1210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!identity_provider) {
1220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    identity_provider.reset(new ProfileIdentityProvider(
12323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        SigninManagerFactory::GetForProfile(profile),
12423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
12523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        LoginUIServiceFactory::GetForProfile(profile)));
12623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  }
12723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
128f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  scoped_ptr<TiclInvalidationService> service(new TiclInvalidationService(
129f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      GetUserAgent(),
1300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      identity_provider.Pass(),
1315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      scoped_ptr<TiclSettingsProvider>(
1325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu          new TiclProfileSettingsProvider(profile)),
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      gcm::GCMProfileServiceFactory::GetForProfile(profile)->driver(),
134f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      profile->GetRequestContext()));
1350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  service->Init(scoped_ptr<syncer::InvalidationStateTracker>(
1360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      new InvalidatorStorage(profile->GetPrefs())));
137f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
138f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return new ProfileInvalidationProvider(service.PassAs<InvalidationService>());
139a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#endif
140a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
141a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
142f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void ProfileInvalidationProviderFactory::RegisterProfilePrefs(
143eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    user_prefs::PrefRegistrySyncable* registry) {
144effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  registry->RegisterBooleanPref(
145effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      prefs::kInvalidationServiceUseGCMChannel,
146effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      false,
147effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
1487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  InvalidatorStorage::RegisterProfilePrefs(registry);
149eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
150eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
151a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}  // namespace invalidation
152