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 #ifndef COMPONENTS_INVALIDATION_TICL_INVALIDATION_SERVICE_H_
6 #define COMPONENTS_INVALIDATION_TICL_INVALIDATION_SERVICE_H_
10 #include "base/compiler_specific.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "base/timer/timer.h"
16 #include "base/values.h"
17 #include "components/invalidation/invalidation_logger.h"
18 #include "components/invalidation/invalidation_service.h"
19 #include "components/invalidation/invalidator_registrar.h"
20 #include "components/invalidation/ticl_settings_provider.h"
21 #include "components/keyed_service/core/keyed_service.h"
22 #include "google_apis/gaia/identity_provider.h"
23 #include "google_apis/gaia/oauth2_token_service.h"
24 #include "net/base/backoff_entry.h"
25 #include "sync/notifier/invalidation_handler.h"
32 class URLRequestContextGetter
;
36 class InvalidationStateTracker
;
40 namespace invalidation
{
41 class GCMInvalidationBridge
;
43 // This InvalidationService wraps the C++ Invalidation Client (TICL) library.
44 // It provides invalidations for desktop platforms (Win, Mac, Linux).
45 class TiclInvalidationService
: public base::NonThreadSafe
,
46 public InvalidationService
,
47 public OAuth2TokenService::Consumer
,
48 public OAuth2TokenService::Observer
,
49 public IdentityProvider::Observer
,
50 public TiclSettingsProvider::Observer
,
51 public syncer::InvalidationHandler
{
53 enum InvalidationNetworkChannel
{
54 PUSH_CLIENT_CHANNEL
= 0,
55 GCM_NETWORK_CHANNEL
= 1,
57 // This enum is used in UMA_HISTOGRAM_ENUMERATION. Insert new values above
59 NETWORK_CHANNELS_COUNT
= 2
62 TiclInvalidationService(
63 const std::string
& user_agent
,
64 scoped_ptr
<IdentityProvider
> identity_provider
,
65 scoped_ptr
<TiclSettingsProvider
> settings_provider
,
66 gcm::GCMDriver
* gcm_driver
,
67 const scoped_refptr
<net::URLRequestContextGetter
>& request_context
);
68 virtual ~TiclInvalidationService();
71 scoped_ptr
<syncer::InvalidationStateTracker
> invalidation_state_tracker
);
73 // InvalidationService implementation.
74 // It is an error to have registered handlers when the service is destroyed.
75 virtual void RegisterInvalidationHandler(
76 syncer::InvalidationHandler
* handler
) OVERRIDE
;
77 virtual void UpdateRegisteredInvalidationIds(
78 syncer::InvalidationHandler
* handler
,
79 const syncer::ObjectIdSet
& ids
) OVERRIDE
;
80 virtual void UnregisterInvalidationHandler(
81 syncer::InvalidationHandler
* handler
) OVERRIDE
;
82 virtual syncer::InvalidatorState
GetInvalidatorState() const OVERRIDE
;
83 virtual std::string
GetInvalidatorClientId() const OVERRIDE
;
84 virtual InvalidationLogger
* GetInvalidationLogger() OVERRIDE
;
85 virtual void RequestDetailedStatus(
86 base::Callback
<void(const base::DictionaryValue
&)> caller
) const OVERRIDE
;
87 virtual IdentityProvider
* GetIdentityProvider() OVERRIDE
;
89 void RequestAccessToken();
91 // OAuth2TokenService::Consumer implementation
92 virtual void OnGetTokenSuccess(
93 const OAuth2TokenService::Request
* request
,
94 const std::string
& access_token
,
95 const base::Time
& expiration_time
) OVERRIDE
;
96 virtual void OnGetTokenFailure(
97 const OAuth2TokenService::Request
* request
,
98 const GoogleServiceAuthError
& error
) OVERRIDE
;
100 // OAuth2TokenService::Observer implementation
101 virtual void OnRefreshTokenAvailable(const std::string
& account_id
) OVERRIDE
;
102 virtual void OnRefreshTokenRevoked(const std::string
& account_id
) OVERRIDE
;
104 // IdentityProvider::Observer implementation.
105 virtual void OnActiveAccountLogout() OVERRIDE
;
107 // TiclSettingsProvider::Observer implementation.
108 virtual void OnUseGCMChannelChanged() OVERRIDE
;
110 // syncer::InvalidationHandler implementation.
111 virtual void OnInvalidatorStateChange(
112 syncer::InvalidatorState state
) OVERRIDE
;
113 virtual void OnIncomingInvalidation(
114 const syncer::ObjectIdInvalidationMap
& invalidation_map
) OVERRIDE
;
115 virtual std::string
GetOwnerName() const OVERRIDE
;
118 // Initializes with an injected invalidator.
120 scoped_ptr
<syncer::InvalidationStateTracker
> invalidation_state_tracker
,
121 syncer::Invalidator
* invalidator
);
124 friend class TiclInvalidationServiceTestDelegate
;
125 friend class TiclProfileSettingsProviderTest
;
127 bool IsReadyToStart();
128 bool IsStarted() const;
130 void StartInvalidator(InvalidationNetworkChannel network_channel
);
131 void UpdateInvalidationNetworkChannel();
132 void UpdateInvalidatorCredentials();
133 void StopInvalidator();
135 const std::string user_agent_
;
137 scoped_ptr
<IdentityProvider
> identity_provider_
;
138 scoped_ptr
<TiclSettingsProvider
> settings_provider_
;
140 scoped_ptr
<syncer::InvalidatorRegistrar
> invalidator_registrar_
;
141 scoped_ptr
<syncer::InvalidationStateTracker
> invalidation_state_tracker_
;
142 scoped_ptr
<syncer::Invalidator
> invalidator_
;
144 // TiclInvalidationService needs to remember access token in order to
145 // invalidate it with OAuth2TokenService.
146 std::string access_token_
;
148 // TiclInvalidationService needs to hold reference to access_token_request_
149 // for the duration of request in order to receive callbacks.
150 scoped_ptr
<OAuth2TokenService::Request
> access_token_request_
;
151 base::OneShotTimer
<TiclInvalidationService
> request_access_token_retry_timer_
;
152 net::BackoffEntry request_access_token_backoff_
;
154 InvalidationNetworkChannel network_channel_type_
;
155 gcm::GCMDriver
* gcm_driver_
;
156 scoped_ptr
<GCMInvalidationBridge
> gcm_invalidation_bridge_
;
157 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
159 // The invalidation logger object we use to record state changes
160 // and invalidations.
161 InvalidationLogger logger_
;
163 // Keep a copy of the important parameters used in network channel creation
165 base::DictionaryValue network_channel_options_
;
167 DISALLOW_COPY_AND_ASSIGN(TiclInvalidationService
);
170 } // namespace invalidation
172 #endif // COMPONENTS_INVALIDATION_TICL_INVALIDATION_SERVICE_H_