Home | History | Annotate | Download | only in invalidation
      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_service.h"
     12 #include "chrome/browser/invalidation/invalidator_storage.h"
     13 #include "chrome/browser/signin/oauth2_token_service.h"
     14 #include "chrome/browser/signin/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 "net/base/backoff_entry.h"
     19 #include "sync/notifier/invalidation_handler.h"
     20 #include "sync/notifier/invalidator_registrar.h"
     21 
     22 class Profile;
     23 class SigninManagerBase;
     24 
     25 namespace syncer {
     26 class Invalidator;
     27 }
     28 
     29 namespace invalidation {
     30 
     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 syncer::InvalidationHandler {
     39  public:
     40   TiclInvalidationService(SigninManagerBase* signin,
     41                           TokenService* token_service,
     42                           OAuth2TokenService* oauth2_token_service,
     43                           Profile* profile);
     44   virtual ~TiclInvalidationService();
     45 
     46   void Init();
     47 
     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 void AcknowledgeInvalidation(
     58       const invalidation::ObjectId& id,
     59       const syncer::AckHandle& ack_handle) OVERRIDE;
     60   virtual syncer::InvalidatorState GetInvalidatorState() const OVERRIDE;
     61   virtual std::string GetInvalidatorClientId() const OVERRIDE;
     62 
     63   // content::NotificationObserver implementation.
     64   virtual void Observe(int type,
     65                        const content::NotificationSource& source,
     66                        const content::NotificationDetails& details) OVERRIDE;
     67 
     68   void RequestAccessToken();
     69 
     70   // OAuth2TokenService::Consumer implementation
     71   virtual void OnGetTokenSuccess(
     72       const OAuth2TokenService::Request* request,
     73       const std::string& access_token,
     74       const base::Time& expiration_time) OVERRIDE;
     75   virtual void OnGetTokenFailure(
     76       const OAuth2TokenService::Request* request,
     77       const GoogleServiceAuthError& error) OVERRIDE;
     78 
     79   // syncer::InvalidationHandler implementation.
     80   virtual void OnInvalidatorStateChange(
     81       syncer::InvalidatorState state) OVERRIDE;
     82   virtual void OnIncomingInvalidation(
     83       const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE;
     84 
     85   // Overrides BrowserContextKeyedService method.
     86   virtual void Shutdown() OVERRIDE;
     87 
     88  protected:
     89   // Initializes with an injected invalidator.
     90   void InitForTest(syncer::Invalidator* invalidator);
     91 
     92   friend class TiclInvalidationServiceTestDelegate;
     93 
     94  private:
     95   bool IsReadyToStart();
     96   bool IsStarted();
     97 
     98   void StartInvalidator();
     99   void UpdateInvalidatorCredentials();
    100   void StopInvalidator();
    101   void Logout();
    102 
    103   Profile *const profile_;
    104   SigninManagerBase *const signin_manager_;
    105   TokenService *const token_service_;
    106   OAuth2TokenService *const oauth2_token_service_;
    107 
    108   scoped_ptr<syncer::InvalidatorRegistrar> invalidator_registrar_;
    109   scoped_ptr<InvalidatorStorage> invalidator_storage_;
    110   scoped_ptr<syncer::Invalidator> invalidator_;
    111 
    112   content::NotificationRegistrar notification_registrar_;
    113 
    114   // TiclInvalidationService needs to remember access token in order to
    115   // invalidate it with OAuth2TokenService.
    116   std::string access_token_;
    117 
    118   // TiclInvalidationService needs to hold reference to access_token_request_
    119   // for the duration of request in order to receive callbacks.
    120   scoped_ptr<OAuth2TokenService::Request> access_token_request_;
    121   base::OneShotTimer<TiclInvalidationService> request_access_token_retry_timer_;
    122   net::BackoffEntry request_access_token_backoff_;
    123 
    124   DISALLOW_COPY_AND_ASSIGN(TiclInvalidationService);
    125 };
    126 
    127 }  // namespace invalidation
    128 
    129 #endif  // CHROME_BROWSER_INVALIDATION_TICL_INVALIDATION_SERVICE_H_
    130