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_handler.h"
18 #include "components/invalidation/invalidation_logger.h"
19 #include "components/invalidation/invalidation_service.h"
20 #include "components/invalidation/invalidator_registrar.h"
21 #include "components/invalidation/ticl_settings_provider.h"
22 #include "components/keyed_service/core/keyed_service.h"
23 #include "google_apis/gaia/identity_provider.h"
24 #include "google_apis/gaia/oauth2_token_service.h"
25 #include "net/base/backoff_entry.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 ~TiclInvalidationService() override
;
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 void RegisterInvalidationHandler(
76 syncer::InvalidationHandler
* handler
) override
;
77 bool UpdateRegisteredInvalidationIds(syncer::InvalidationHandler
* handler
,
78 const syncer::ObjectIdSet
& ids
) override
;
79 void UnregisterInvalidationHandler(
80 syncer::InvalidationHandler
* handler
) override
;
81 syncer::InvalidatorState
GetInvalidatorState() const override
;
82 std::string
GetInvalidatorClientId() const override
;
83 InvalidationLogger
* GetInvalidationLogger() override
;
84 void RequestDetailedStatus(
85 base::Callback
<void(const base::DictionaryValue
&)> caller
) const override
;
86 IdentityProvider
* GetIdentityProvider() override
;
88 void RequestAccessToken();
90 // OAuth2TokenService::Consumer implementation
91 void OnGetTokenSuccess(const OAuth2TokenService::Request
* request
,
92 const std::string
& access_token
,
93 const base::Time
& expiration_time
) override
;
94 void OnGetTokenFailure(const OAuth2TokenService::Request
* request
,
95 const GoogleServiceAuthError
& error
) override
;
97 // OAuth2TokenService::Observer implementation
98 void OnRefreshTokenAvailable(const std::string
& account_id
) override
;
99 void OnRefreshTokenRevoked(const std::string
& account_id
) override
;
101 // IdentityProvider::Observer implementation.
102 void OnActiveAccountLogout() override
;
104 // TiclSettingsProvider::Observer implementation.
105 void OnUseGCMChannelChanged() override
;
107 // syncer::InvalidationHandler implementation.
108 void OnInvalidatorStateChange(syncer::InvalidatorState state
) override
;
109 void OnIncomingInvalidation(
110 const syncer::ObjectIdInvalidationMap
& invalidation_map
) override
;
111 std::string
GetOwnerName() const override
;
114 // Initializes with an injected invalidator.
116 scoped_ptr
<syncer::InvalidationStateTracker
> invalidation_state_tracker
,
117 syncer::Invalidator
* invalidator
);
120 friend class TiclInvalidationServiceTestDelegate
;
121 friend class TiclProfileSettingsProviderTest
;
123 bool IsReadyToStart();
124 bool IsStarted() const;
126 void StartInvalidator(InvalidationNetworkChannel network_channel
);
127 void UpdateInvalidationNetworkChannel();
128 void UpdateInvalidatorCredentials();
129 void StopInvalidator();
131 const std::string user_agent_
;
133 scoped_ptr
<IdentityProvider
> identity_provider_
;
134 scoped_ptr
<TiclSettingsProvider
> settings_provider_
;
136 scoped_ptr
<syncer::InvalidatorRegistrar
> invalidator_registrar_
;
137 scoped_ptr
<syncer::InvalidationStateTracker
> invalidation_state_tracker_
;
138 scoped_ptr
<syncer::Invalidator
> invalidator_
;
140 // TiclInvalidationService needs to remember access token in order to
141 // invalidate it with OAuth2TokenService.
142 std::string access_token_
;
144 // TiclInvalidationService needs to hold reference to access_token_request_
145 // for the duration of request in order to receive callbacks.
146 scoped_ptr
<OAuth2TokenService::Request
> access_token_request_
;
147 base::OneShotTimer
<TiclInvalidationService
> request_access_token_retry_timer_
;
148 net::BackoffEntry request_access_token_backoff_
;
150 InvalidationNetworkChannel network_channel_type_
;
151 gcm::GCMDriver
* gcm_driver_
;
152 scoped_ptr
<GCMInvalidationBridge
> gcm_invalidation_bridge_
;
153 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
155 // The invalidation logger object we use to record state changes
156 // and invalidations.
157 InvalidationLogger logger_
;
159 // Keep a copy of the important parameters used in network channel creation
161 base::DictionaryValue network_channel_options_
;
163 DISALLOW_COPY_AND_ASSIGN(TiclInvalidationService
);
166 } // namespace invalidation
168 #endif // COMPONENTS_INVALIDATION_TICL_INVALIDATION_SERVICE_H_