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 GOOGLE_APIS_GAIA_IDENTITY_PROVIDER_H_ 6 #define GOOGLE_APIS_GAIA_IDENTITY_PROVIDER_H_ 7 8 #include <string> 9 10 #include "base/compiler_specific.h" 11 #include "base/macros.h" 12 #include "base/observer_list.h" 13 #include "google_apis/gaia/oauth2_token_service.h" 14 15 // Helper class that provides access to information about logged-in GAIA 16 // accounts. Each instance of this class references an entity who may be logged 17 // in to zero, one or multiple GAIA accounts. The class provides access to the 18 // OAuth tokens for all logged-in accounts and indicates which of these is 19 // currently active. 20 // The main purpose of this abstraction layer is to isolate consumers of GAIA 21 // information from the different sources and various token service 22 // implementations. Whenever possible, consumers of GAIA information should be 23 // provided with an instance of this class instead of accessing other GAIA APIs 24 // directly. 25 class IdentityProvider : public OAuth2TokenService::Observer { 26 public: 27 class Observer { 28 public: 29 // Called when a GAIA account logs in and becomes the active account. All 30 // account information is available when this method is called and all 31 // |IdentityProvider| methods will return valid data. OnActiveAccountLogin()32 virtual void OnActiveAccountLogin() {} 33 34 // Called when the active GAIA account logs out. The account information may 35 // have been cleared already when this method is called. The 36 // |IdentityProvider| methods may return inconsistent or outdated 37 // information if called from within OnLogout(). OnActiveAccountLogout()38 virtual void OnActiveAccountLogout() {} 39 40 protected: 41 virtual ~Observer(); 42 }; 43 44 virtual ~IdentityProvider(); 45 46 // Adds and removes observers that will be notified of changes to the refresh 47 // token availability for the active account. 48 void AddActiveAccountRefreshTokenObserver( 49 OAuth2TokenService::Observer* observer); 50 void RemoveActiveAccountRefreshTokenObserver( 51 OAuth2TokenService::Observer* observer); 52 53 // Gets the active account's user name. 54 virtual std::string GetActiveUsername() = 0; 55 56 // Gets the active account's account ID. 57 virtual std::string GetActiveAccountId() = 0; 58 59 // Gets the token service vending OAuth tokens for all logged-in accounts. 60 virtual OAuth2TokenService* GetTokenService() = 0; 61 62 // Requests login to a GAIA account. Implementations can show a login UI, log 63 // in automatically if sufficient credentials are available or may ignore the 64 // request. Returns true if the login request was processed and false if it 65 // was ignored. 66 virtual bool RequestLogin() = 0; 67 68 void AddObserver(Observer* observer); 69 void RemoveObserver(Observer* observer); 70 71 // OAuth2TokenService::Observer: 72 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; 73 virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE; 74 virtual void OnRefreshTokensLoaded() OVERRIDE; 75 76 protected: 77 IdentityProvider(); 78 79 // Fires an OnActiveAccountLogin notification. 80 void FireOnActiveAccountLogin(); 81 82 // Fires an OnActiveAccountLogout notification. 83 void FireOnActiveAccountLogout(); 84 85 private: 86 ObserverList<Observer, true> observers_; 87 ObserverList<OAuth2TokenService::Observer, true> token_service_observers_; 88 int token_service_observer_count_; 89 90 DISALLOW_COPY_AND_ASSIGN(IdentityProvider); 91 }; 92 93 #endif // GOOGLE_APIS_GAIA_IDENTITY_PROVIDER_H_ 94