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/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" 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 OAuth2TokenService::Observer, 39 public syncer::InvalidationHandler { 40 public: 41 TiclInvalidationService(SigninManagerBase* signin, 42 ProfileOAuth2TokenService* 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 syncer::InvalidatorState GetInvalidatorState() const OVERRIDE; 58 virtual std::string GetInvalidatorClientId() const OVERRIDE; 59 60 // content::NotificationObserver implementation. 61 virtual void Observe(int type, 62 const content::NotificationSource& source, 63 const content::NotificationDetails& details) OVERRIDE; 64 65 void RequestAccessToken(); 66 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; 75 76 // OAuth2TokenService::Observer implementation 77 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; 78 virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE; 79 80 // syncer::InvalidationHandler implementation. 81 virtual void OnInvalidatorStateChange( 82 syncer::InvalidatorState state) OVERRIDE; 83 virtual void OnIncomingInvalidation( 84 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE; 85 86 // Overrides BrowserContextKeyedService method. 87 virtual void Shutdown() OVERRIDE; 88 89 protected: 90 // Initializes with an injected invalidator. 91 void InitForTest(syncer::Invalidator* invalidator); 92 93 friend class TiclInvalidationServiceTestDelegate; 94 95 private: 96 bool IsReadyToStart(); 97 bool IsStarted(); 98 99 void StartInvalidator(); 100 void UpdateInvalidatorCredentials(); 101 void StopInvalidator(); 102 void Logout(); 103 104 Profile *const profile_; 105 SigninManagerBase *const signin_manager_; 106 ProfileOAuth2TokenService *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