Roll src/third_party/WebKit d26421b:4b1dbe3 (svn 194839:194840)
[chromium-blink-merge.git] / google_apis / gaia / identity_provider.h
blob6ee9b8c0c1a44e4307fbaf80fd4d5447c33325bc
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.
5 #ifndef GOOGLE_APIS_GAIA_IDENTITY_PROVIDER_H_
6 #define GOOGLE_APIS_GAIA_IDENTITY_PROVIDER_H_
8 #include <string>
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"
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.
32 virtual void OnActiveAccountLogin() {}
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().
38 virtual void OnActiveAccountLogout() {}
40 protected:
41 virtual ~Observer();
44 ~IdentityProvider() override;
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);
53 // Gets the active account's user name.
54 virtual std::string GetActiveUsername() = 0;
56 // Gets the active account's account ID.
57 virtual std::string GetActiveAccountId() = 0;
59 // Gets the token service vending OAuth tokens for all logged-in accounts.
60 virtual OAuth2TokenService* GetTokenService() = 0;
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;
68 void AddObserver(Observer* observer);
69 void RemoveObserver(Observer* observer);
71 // OAuth2TokenService::Observer:
72 void OnRefreshTokenAvailable(const std::string& account_id) override;
73 void OnRefreshTokenRevoked(const std::string& account_id) override;
74 void OnRefreshTokensLoaded() override;
76 protected:
77 IdentityProvider();
79 // Fires an OnActiveAccountLogin notification.
80 void FireOnActiveAccountLogin();
82 // Fires an OnActiveAccountLogout notification.
83 void FireOnActiveAccountLogout();
85 private:
86 ObserverList<Observer, true> observers_;
87 ObserverList<OAuth2TokenService::Observer, true> token_service_observers_;
88 int token_service_observer_count_;
90 DISALLOW_COPY_AND_ASSIGN(IdentityProvider);
93 #endif // GOOGLE_APIS_GAIA_IDENTITY_PROVIDER_H_