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.
4
5#ifndef COMPONENTS_INVALIDATION_TICL_INVALIDATION_SERVICE_H_
6#define COMPONENTS_INVALIDATION_TICL_INVALIDATION_SERVICE_H_
7
8#include <string>
9
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"
26
27namespace gcm {
28class GCMDriver;
29}
30
31namespace net {
32class URLRequestContextGetter;
33}
34
35namespace syncer {
36class InvalidationStateTracker;
37class Invalidator;
38}
39
40namespace invalidation {
41class GCMInvalidationBridge;
42
43// This InvalidationService wraps the C++ Invalidation Client (TICL) library.
44// It provides invalidations for desktop platforms (Win, Mac, Linux).
45class 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 {
52 public:
53  enum InvalidationNetworkChannel {
54    PUSH_CLIENT_CHANNEL = 0,
55    GCM_NETWORK_CHANNEL = 1,
56
57    // This enum is used in UMA_HISTOGRAM_ENUMERATION. Insert new values above
58    // this line.
59    NETWORK_CHANNELS_COUNT = 2
60  };
61
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();
69
70  void Init(
71      scoped_ptr<syncer::InvalidationStateTracker> invalidation_state_tracker);
72
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;
88
89  void RequestAccessToken();
90
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;
99
100  // OAuth2TokenService::Observer implementation
101  virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
102  virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE;
103
104  // IdentityProvider::Observer implementation.
105  virtual void OnActiveAccountLogout() OVERRIDE;
106
107  // TiclSettingsProvider::Observer implementation.
108  virtual void OnUseGCMChannelChanged() OVERRIDE;
109
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;
116
117 protected:
118  // Initializes with an injected invalidator.
119  void InitForTest(
120      scoped_ptr<syncer::InvalidationStateTracker> invalidation_state_tracker,
121      syncer::Invalidator* invalidator);
122
123 private:
124  friend class TiclInvalidationServiceTestDelegate;
125  friend class TiclProfileSettingsProviderTest;
126
127  bool IsReadyToStart();
128  bool IsStarted() const;
129
130  void StartInvalidator(InvalidationNetworkChannel network_channel);
131  void UpdateInvalidationNetworkChannel();
132  void UpdateInvalidatorCredentials();
133  void StopInvalidator();
134
135  const std::string user_agent_;
136
137  scoped_ptr<IdentityProvider> identity_provider_;
138  scoped_ptr<TiclSettingsProvider> settings_provider_;
139
140  scoped_ptr<syncer::InvalidatorRegistrar> invalidator_registrar_;
141  scoped_ptr<syncer::InvalidationStateTracker> invalidation_state_tracker_;
142  scoped_ptr<syncer::Invalidator> invalidator_;
143
144  // TiclInvalidationService needs to remember access token in order to
145  // invalidate it with OAuth2TokenService.
146  std::string access_token_;
147
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_;
153
154  InvalidationNetworkChannel network_channel_type_;
155  gcm::GCMDriver* gcm_driver_;
156  scoped_ptr<GCMInvalidationBridge> gcm_invalidation_bridge_;
157  scoped_refptr<net::URLRequestContextGetter> request_context_;
158
159  // The invalidation logger object we use to record state changes
160  // and invalidations.
161  InvalidationLogger logger_;
162
163  // Keep a copy of the important parameters used in network channel creation
164  // for debugging.
165  base::DictionaryValue network_channel_options_;
166
167  DISALLOW_COPY_AND_ASSIGN(TiclInvalidationService);
168};
169
170}  // namespace invalidation
171
172#endif  // COMPONENTS_INVALIDATION_TICL_INVALIDATION_SERVICE_H_
173