summaryrefslogtreecommitdiffstats
path: root/google_apis/gaia/identity_provider.h
blob: e0e99ee688cf4dc6aac99792a04cf1fa578a9b04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GOOGLE_APIS_GAIA_IDENTITY_PROVIDER_H_
#define GOOGLE_APIS_GAIA_IDENTITY_PROVIDER_H_

#include <string>

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/observer_list.h"
#include "google_apis/gaia/oauth2_token_service.h"

// Helper class that provides access to information about logged-in GAIA
// accounts. Each instance of this class references an entity who may be logged
// in to zero, one or multiple GAIA accounts. The class provides access to the
// OAuth tokens for all logged-in accounts and indicates which of these is
// currently active.
// The main purpose of this abstraction layer is to isolate consumers of GAIA
// information from the different sources and various token service
// implementations. Whenever possible, consumers of GAIA information should be
// provided with an instance of this class instead of accessing other GAIA APIs
// directly.
class IdentityProvider : public OAuth2TokenService::Observer {
 public:
  class Observer {
   public:
    // Called when a GAIA account logs in and becomes the active account. All
    // account information is available when this method is called and all
    // |IdentityProvider| methods will return valid data.
    virtual void OnActiveAccountLogin() {}

    // Called when the active GAIA account logs out. The account information may
    // have been cleared already when this method is called. The
    // |IdentityProvider| methods may return inconsistent or outdated
    // information if called from within OnLogout().
    virtual void OnActiveAccountLogout() {}

   protected:
    virtual ~Observer();
  };

  virtual ~IdentityProvider();

  // Adds and removes observers that will be notified of changes to the refresh
  // token availability for the active account.
  void AddActiveAccountRefreshTokenObserver(
      OAuth2TokenService::Observer* observer);
  void RemoveActiveAccountRefreshTokenObserver(
      OAuth2TokenService::Observer* observer);

  // Gets the active account's user name.
  virtual std::string GetActiveUsername() = 0;

  // Gets the active account's account ID.
  virtual std::string GetActiveAccountId() = 0;

  // Gets the token service vending OAuth tokens for all logged-in accounts.
  virtual OAuth2TokenService* GetTokenService() = 0;

  // Requests login to a GAIA account. Implementations can show a login UI, log
  // in automatically if sufficient credentials are available or may ignore the
  // request. Returns true if the login request was processed and false if it
  // was ignored.
  virtual bool RequestLogin() = 0;

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // OAuth2TokenService::Observer:
  virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
  virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE;
  virtual void OnRefreshTokensLoaded() OVERRIDE;

 protected:
  IdentityProvider();

  // Fires an OnActiveAccountLogin notification.
  void FireOnActiveAccountLogin();

  // Fires an OnActiveAccountLogout notification.
  void FireOnActiveAccountLogout();

 private:
  ObserverList<Observer, true> observers_;
  ObserverList<OAuth2TokenService::Observer, true> token_service_observers_;
  int token_service_observer_count_;

  DISALLOW_COPY_AND_ASSIGN(IdentityProvider);
};

#endif  // GOOGLE_APIS_GAIA_IDENTITY_PROVIDER_H_