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_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/threading/non_thread_safe.h"
10 #include "base/timer/timer.h"
11 #include "chrome/browser/invalidation/invalidation_service.h"
12 #include "chrome/browser/invalidation/invalidator_storage.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service.h"
14 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "google_apis/gaia/oauth2_token_service.h"
18 #include "net/base/backoff_entry.h"
19 #include "sync/notifier/invalidation_handler.h"
20 #include "sync/notifier/invalidator_registrar.h"
23 class SigninManagerBase
;
29 namespace invalidation
{
31 // This InvalidationService wraps the C++ Invalidation Client (TICL) library.
32 // It provides invalidations for desktop platforms (Win, Mac, Linux).
33 class TiclInvalidationService
34 : public base::NonThreadSafe
,
35 public InvalidationService
,
36 public content::NotificationObserver
,
37 public OAuth2TokenService::Consumer
,
38 public OAuth2TokenService::Observer
,
39 public syncer::InvalidationHandler
{
41 TiclInvalidationService(SigninManagerBase
* signin
,
42 ProfileOAuth2TokenService
* oauth2_token_service
,
44 virtual ~TiclInvalidationService();
48 // InvalidationService implementation.
49 // It is an error to have registered handlers when Shutdown() is called.
50 virtual void RegisterInvalidationHandler(
51 syncer::InvalidationHandler
* handler
) OVERRIDE
;
52 virtual void UpdateRegisteredInvalidationIds(
53 syncer::InvalidationHandler
* handler
,
54 const syncer::ObjectIdSet
& ids
) OVERRIDE
;
55 virtual void UnregisterInvalidationHandler(
56 syncer::InvalidationHandler
* handler
) OVERRIDE
;
57 virtual syncer::InvalidatorState
GetInvalidatorState() const OVERRIDE
;
58 virtual std::string
GetInvalidatorClientId() const OVERRIDE
;
60 // content::NotificationObserver implementation.
61 virtual void Observe(int type
,
62 const content::NotificationSource
& source
,
63 const content::NotificationDetails
& details
) OVERRIDE
;
65 void RequestAccessToken();
67 // OAuth2TokenService::Consumer implementation
68 virtual void OnGetTokenSuccess(
69 const OAuth2TokenService::Request
* request
,
70 const std::string
& access_token
,
71 const base::Time
& expiration_time
) OVERRIDE
;
72 virtual void OnGetTokenFailure(
73 const OAuth2TokenService::Request
* request
,
74 const GoogleServiceAuthError
& error
) OVERRIDE
;
76 // OAuth2TokenService::Observer implementation
77 virtual void OnRefreshTokenAvailable(const std::string
& account_id
) OVERRIDE
;
78 virtual void OnRefreshTokenRevoked(const std::string
& account_id
) OVERRIDE
;
80 // syncer::InvalidationHandler implementation.
81 virtual void OnInvalidatorStateChange(
82 syncer::InvalidatorState state
) OVERRIDE
;
83 virtual void OnIncomingInvalidation(
84 const syncer::ObjectIdInvalidationMap
& invalidation_map
) OVERRIDE
;
86 // Overrides BrowserContextKeyedService method.
87 virtual void Shutdown() OVERRIDE
;
90 // Initializes with an injected invalidator.
91 void InitForTest(syncer::Invalidator
* invalidator
);
93 friend class TiclInvalidationServiceTestDelegate
;
96 enum InvalidationNetworkChannel
{
97 PUSH_CLIENT_CHANNEL
= 0,
98 GCM_NETWORK_CHANNEL
= 1
101 bool IsReadyToStart();
104 void StartInvalidator(InvalidationNetworkChannel network_channel
);
105 void UpdateInvalidatorCredentials();
106 void StopInvalidator();
109 Profile
*const profile_
;
110 SigninManagerBase
*const signin_manager_
;
111 ProfileOAuth2TokenService
*const oauth2_token_service_
;
113 scoped_ptr
<syncer::InvalidatorRegistrar
> invalidator_registrar_
;
114 scoped_ptr
<InvalidatorStorage
> invalidator_storage_
;
115 scoped_ptr
<syncer::Invalidator
> invalidator_
;
117 content::NotificationRegistrar notification_registrar_
;
119 // TiclInvalidationService needs to remember access token in order to
120 // invalidate it with OAuth2TokenService.
121 std::string access_token_
;
123 // TiclInvalidationService needs to hold reference to access_token_request_
124 // for the duration of request in order to receive callbacks.
125 scoped_ptr
<OAuth2TokenService::Request
> access_token_request_
;
126 base::OneShotTimer
<TiclInvalidationService
> request_access_token_retry_timer_
;
127 net::BackoffEntry request_access_token_backoff_
;
129 DISALLOW_COPY_AND_ASSIGN(TiclInvalidationService
);
132 } // namespace invalidation
134 #endif // CHROME_BROWSER_INVALIDATION_TICL_INVALIDATION_SERVICE_H_