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 #ifndef CHROME_BROWSER_INVALIDATION_TICL_INVALIDATION_SERVICE_H_
6 #define CHROME_BROWSER_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/ticl_settings_provider.h"
20 #include "components/keyed_service/core/keyed_service.h"
21 #include "google_apis/gaia/identity_provider.h"
22 #include "google_apis/gaia/oauth2_token_service.h"
23 #include "net/base/backoff_entry.h"
24 #include "sync/notifier/invalidation_handler.h"
25 #include "sync/notifier/invalidator_registrar.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 scoped_ptr
<IdentityProvider
> identity_provider
,
64 scoped_ptr
<TiclSettingsProvider
> settings_provider
,
65 gcm::GCMDriver
* gcm_service
,
66 const scoped_refptr
<net::URLRequestContextGetter
>& request_context
);
67 virtual ~TiclInvalidationService();
70 scoped_ptr
<syncer::InvalidationStateTracker
> invalidation_state_tracker
);
72 // InvalidationService implementation.
73 // It is an error to have registered handlers when Shutdown() is called.
74 virtual void RegisterInvalidationHandler(
75 syncer::InvalidationHandler
* handler
) OVERRIDE
;
76 virtual void UpdateRegisteredInvalidationIds(
77 syncer::InvalidationHandler
* handler
,
78 const syncer::ObjectIdSet
& ids
) OVERRIDE
;
79 virtual void UnregisterInvalidationHandler(
80 syncer::InvalidationHandler
* handler
) OVERRIDE
;
81 virtual syncer::InvalidatorState
GetInvalidatorState() const OVERRIDE
;
82 virtual std::string
GetInvalidatorClientId() const OVERRIDE
;
83 virtual InvalidationLogger
* GetInvalidationLogger() OVERRIDE
;
84 virtual void RequestDetailedStatus(
85 base::Callback
<void(const base::DictionaryValue
&)> caller
) const OVERRIDE
;
86 virtual IdentityProvider
* GetIdentityProvider() OVERRIDE
;
88 void RequestAccessToken();
90 // OAuth2TokenService::Consumer implementation
91 virtual void OnGetTokenSuccess(
92 const OAuth2TokenService::Request
* request
,
93 const std::string
& access_token
,
94 const base::Time
& expiration_time
) OVERRIDE
;
95 virtual void OnGetTokenFailure(
96 const OAuth2TokenService::Request
* request
,
97 const GoogleServiceAuthError
& error
) OVERRIDE
;
99 // OAuth2TokenService::Observer implementation
100 virtual void OnRefreshTokenAvailable(const std::string
& account_id
) OVERRIDE
;
101 virtual void OnRefreshTokenRevoked(const std::string
& account_id
) OVERRIDE
;
103 // IdentityProvider::Observer implementation.
104 virtual void OnActiveAccountLogout() OVERRIDE
;
106 // TiclSettingsProvider::Observer implementation.
107 virtual void OnUseGCMChannelChanged() OVERRIDE
;
109 // syncer::InvalidationHandler implementation.
110 virtual void OnInvalidatorStateChange(
111 syncer::InvalidatorState state
) OVERRIDE
;
112 virtual void OnIncomingInvalidation(
113 const syncer::ObjectIdInvalidationMap
& invalidation_map
) OVERRIDE
;
114 virtual std::string
GetOwnerName() const OVERRIDE
;
116 // Overrides KeyedService method.
117 virtual void Shutdown() OVERRIDE
;
120 // Initializes with an injected invalidator.
122 scoped_ptr
<syncer::InvalidationStateTracker
> invalidation_state_tracker
,
123 syncer::Invalidator
* invalidator
);
126 friend class TiclInvalidationServiceTestDelegate
;
127 friend class TiclProfileSettingsProviderTest
;
129 bool IsReadyToStart();
130 bool IsStarted() const;
132 void StartInvalidator(InvalidationNetworkChannel network_channel
);
133 void UpdateInvalidationNetworkChannel();
134 void UpdateInvalidatorCredentials();
135 void StopInvalidator();
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 // CHROME_BROWSER_INVALIDATION_TICL_INVALIDATION_SERVICE_H_