Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / invalidation / ticl_invalidation_service.h
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
27 namespace gcm {
28 class GCMDriver;
29 }
30
31 namespace net {
32 class URLRequestContextGetter;
33 }
34
35 namespace syncer {
36 class InvalidationStateTracker;
37 class Invalidator;
38 }
39
40 namespace invalidation {
41 class GCMInvalidationBridge;
42
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 {
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   ~TiclInvalidationService() override;
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   void RegisterInvalidationHandler(
76       syncer::InvalidationHandler* handler) override;
77   void 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;
87
88   void RequestAccessToken();
89
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;
96
97   // OAuth2TokenService::Observer implementation
98   void OnRefreshTokenAvailable(const std::string& account_id) override;
99   void OnRefreshTokenRevoked(const std::string& account_id) override;
100
101   // IdentityProvider::Observer implementation.
102   void OnActiveAccountLogout() override;
103
104   // TiclSettingsProvider::Observer implementation.
105   void OnUseGCMChannelChanged() override;
106
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;
112
113  protected:
114   // Initializes with an injected invalidator.
115   void InitForTest(
116       scoped_ptr<syncer::InvalidationStateTracker> invalidation_state_tracker,
117       syncer::Invalidator* invalidator);
118
119  private:
120   friend class TiclInvalidationServiceTestDelegate;
121   friend class TiclProfileSettingsProviderTest;
122
123   bool IsReadyToStart();
124   bool IsStarted() const;
125
126   void StartInvalidator(InvalidationNetworkChannel network_channel);
127   void UpdateInvalidationNetworkChannel();
128   void UpdateInvalidatorCredentials();
129   void StopInvalidator();
130
131   const std::string user_agent_;
132
133   scoped_ptr<IdentityProvider> identity_provider_;
134   scoped_ptr<TiclSettingsProvider> settings_provider_;
135
136   scoped_ptr<syncer::InvalidatorRegistrar> invalidator_registrar_;
137   scoped_ptr<syncer::InvalidationStateTracker> invalidation_state_tracker_;
138   scoped_ptr<syncer::Invalidator> invalidator_;
139
140   // TiclInvalidationService needs to remember access token in order to
141   // invalidate it with OAuth2TokenService.
142   std::string access_token_;
143
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_;
149
150   InvalidationNetworkChannel network_channel_type_;
151   gcm::GCMDriver* gcm_driver_;
152   scoped_ptr<GCMInvalidationBridge> gcm_invalidation_bridge_;
153   scoped_refptr<net::URLRequestContextGetter> request_context_;
154
155   // The invalidation logger object we use to record state changes
156   // and invalidations.
157   InvalidationLogger logger_;
158
159   // Keep a copy of the important parameters used in network channel creation
160   // for debugging.
161   base::DictionaryValue network_channel_options_;
162
163   DISALLOW_COPY_AND_ASSIGN(TiclInvalidationService);
164 };
165
166 }  // namespace invalidation
167
168 #endif  // COMPONENTS_INVALIDATION_TICL_INVALIDATION_SERVICE_H_