Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / invalidation / ticl_invalidation_service.h
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.
4
5 #ifndef CHROME_BROWSER_INVALIDATION_TICL_INVALIDATION_SERVICE_H_
6 #define CHROME_BROWSER_INVALIDATION_TICL_INVALIDATION_SERVICE_H_
7
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_logger.h"
12 #include "chrome/browser/invalidation/invalidation_service.h"
13 #include "chrome/browser/invalidation/invalidator_storage.h"
14 #include "chrome/browser/signin/profile_oauth2_token_service.h"
15 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "google_apis/gaia/oauth2_token_service.h"
19 #include "net/base/backoff_entry.h"
20 #include "sync/notifier/invalidation_handler.h"
21 #include "sync/notifier/invalidator_registrar.h"
22
23 class Profile;
24 class SigninManagerBase;
25
26 namespace syncer {
27 class Invalidator;
28 }
29
30 namespace invalidation {
31
32 // This InvalidationService wraps the C++ Invalidation Client (TICL) library.
33 // It provides invalidations for desktop platforms (Win, Mac, Linux).
34 class TiclInvalidationService
35     : public base::NonThreadSafe,
36       public InvalidationService,
37       public content::NotificationObserver,
38       public OAuth2TokenService::Consumer,
39       public OAuth2TokenService::Observer,
40       public syncer::InvalidationHandler {
41  public:
42   TiclInvalidationService(SigninManagerBase* signin,
43                           ProfileOAuth2TokenService* oauth2_token_service,
44                           Profile* profile);
45   virtual ~TiclInvalidationService();
46
47   void Init();
48
49   // InvalidationService implementation.
50   // It is an error to have registered handlers when Shutdown() is called.
51   virtual void RegisterInvalidationHandler(
52       syncer::InvalidationHandler* handler) OVERRIDE;
53   virtual void UpdateRegisteredInvalidationIds(
54       syncer::InvalidationHandler* handler,
55       const syncer::ObjectIdSet& ids) OVERRIDE;
56   virtual void UnregisterInvalidationHandler(
57       syncer::InvalidationHandler* handler) OVERRIDE;
58   virtual syncer::InvalidatorState GetInvalidatorState() const OVERRIDE;
59   virtual std::string GetInvalidatorClientId() const OVERRIDE;
60   virtual InvalidationLogger* GetInvalidationLogger() OVERRIDE;
61
62   // content::NotificationObserver implementation.
63   virtual void Observe(int type,
64                        const content::NotificationSource& source,
65                        const content::NotificationDetails& details) OVERRIDE;
66
67   void RequestAccessToken();
68
69   // OAuth2TokenService::Consumer implementation
70   virtual void OnGetTokenSuccess(
71       const OAuth2TokenService::Request* request,
72       const std::string& access_token,
73       const base::Time& expiration_time) OVERRIDE;
74   virtual void OnGetTokenFailure(
75       const OAuth2TokenService::Request* request,
76       const GoogleServiceAuthError& error) OVERRIDE;
77
78   // OAuth2TokenService::Observer implementation
79   virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
80   virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE;
81
82   // syncer::InvalidationHandler implementation.
83   virtual void OnInvalidatorStateChange(
84       syncer::InvalidatorState state) OVERRIDE;
85   virtual void OnIncomingInvalidation(
86       const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE;
87
88   // Overrides BrowserContextKeyedService method.
89   virtual void Shutdown() OVERRIDE;
90
91  protected:
92   // Initializes with an injected invalidator.
93   void InitForTest(syncer::Invalidator* invalidator);
94
95   friend class TiclInvalidationServiceTestDelegate;
96
97  private:
98   enum InvalidationNetworkChannel {
99     PUSH_CLIENT_CHANNEL = 0,
100     GCM_NETWORK_CHANNEL = 1
101   };
102
103   bool IsReadyToStart();
104   bool IsStarted();
105
106   void StartInvalidator(InvalidationNetworkChannel network_channel);
107   void UpdateInvalidatorCredentials();
108   void StopInvalidator();
109   void Logout();
110
111   Profile *const profile_;
112   SigninManagerBase *const signin_manager_;
113   ProfileOAuth2TokenService *const oauth2_token_service_;
114
115   scoped_ptr<syncer::InvalidatorRegistrar> invalidator_registrar_;
116   scoped_ptr<InvalidatorStorage> invalidator_storage_;
117   scoped_ptr<syncer::Invalidator> invalidator_;
118
119   content::NotificationRegistrar notification_registrar_;
120
121   // TiclInvalidationService needs to remember access token in order to
122   // invalidate it with OAuth2TokenService.
123   std::string access_token_;
124
125   // TiclInvalidationService needs to hold reference to access_token_request_
126   // for the duration of request in order to receive callbacks.
127   scoped_ptr<OAuth2TokenService::Request> access_token_request_;
128   base::OneShotTimer<TiclInvalidationService> request_access_token_retry_timer_;
129   net::BackoffEntry request_access_token_backoff_;
130
131   // The invalidation logger object we use to record state changes
132   // and invalidations.
133   InvalidationLogger logger_;
134
135   DISALLOW_COPY_AND_ASSIGN(TiclInvalidationService);
136 };
137
138 }  // namespace invalidation
139
140 #endif  // CHROME_BROWSER_INVALIDATION_TICL_INVALIDATION_SERVICE_H_