summaryrefslogtreecommitdiffstats
path: root/chrome/browser/invalidation/invalidation_service_factory.cc
blob: 32bbaf5dc225964405619e7dc6ce03bfeb6eb75a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/invalidation/invalidation_service_factory.h"

#include "base/prefs/pref_registry.h"
#include "chrome/browser/invalidation/fake_invalidation_service.h"
#include "chrome/browser/invalidation/invalidation_service.h"
#include "chrome/browser/invalidation/invalidation_service_android.h"
#include "chrome/browser/invalidation/invalidator_storage.h"
#include "chrome/browser/invalidation/p2p_invalidation_service.h"
#include "chrome/browser/invalidation/ticl_invalidation_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "components/browser_context_keyed_service/browser_context_dependency_manager.h"

#if defined(OS_ANDROID)
#include "chrome/browser/invalidation/invalidation_controller_android.h"
#endif  // defined(OS_ANDROID)

namespace invalidation {

// static
InvalidationService* InvalidationServiceFactory::GetForProfile(
    Profile* profile) {
  return static_cast<InvalidationService*>(
      GetInstance()->GetServiceForBrowserContext(profile, true));
}

// static
InvalidationServiceFactory* InvalidationServiceFactory::GetInstance() {
  return Singleton<InvalidationServiceFactory>::get();
}

InvalidationServiceFactory::InvalidationServiceFactory()
    : BrowserContextKeyedServiceFactory(
        "InvalidationService",
        BrowserContextDependencyManager::GetInstance()),
      build_fake_invalidators_(false) {
#if !defined(OS_ANDROID)
  DependsOn(SigninManagerFactory::GetInstance());
  DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
#endif
}

InvalidationServiceFactory::~InvalidationServiceFactory() {}

namespace {

BrowserContextKeyedService* BuildP2PInvalidationService(
    content::BrowserContext* context) {
  Profile* profile = static_cast<Profile*>(context);
  return new P2PInvalidationService(profile);
}

BrowserContextKeyedService* BuildFakeInvalidationService(
    content::BrowserContext* context) {
  return new FakeInvalidationService();
}

}  // namespace

void InvalidationServiceFactory::SetBuildOnlyFakeInvalidatorsForTest(
    bool test_mode_enabled) {
  build_fake_invalidators_ = test_mode_enabled;
}

P2PInvalidationService*
InvalidationServiceFactory::BuildAndUseP2PInvalidationServiceForTest(
    content::BrowserContext* context) {
  BrowserContextKeyedService* service =
      SetTestingFactoryAndUse(context, BuildP2PInvalidationService);
  return static_cast<P2PInvalidationService*>(service);
}

BrowserContextKeyedService* InvalidationServiceFactory::BuildServiceInstanceFor(
    content::BrowserContext* context) const {
  Profile* profile = static_cast<Profile*>(context);

  if (build_fake_invalidators_) {
    return BuildFakeInvalidationService(context);
  }

#if defined(OS_ANDROID)
  InvalidationServiceAndroid* service = new InvalidationServiceAndroid(
      profile,
      new InvalidationControllerAndroid());
  return service;
#else
  SigninManagerBase* signin_manager =
      SigninManagerFactory::GetForProfile(profile);
  ProfileOAuth2TokenService* oauth2_token_service =
      ProfileOAuth2TokenServiceFactory::GetForProfile(profile);

  TiclInvalidationService* service = new TiclInvalidationService(
      signin_manager,
      oauth2_token_service,
      profile);
  service->Init();
  return service;
#endif
}

void InvalidationServiceFactory::RegisterProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {
  InvalidatorStorage::RegisterProfilePrefs(registry);
}

}  // namespace invalidation