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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_INVALIDATION_PUBLIC_IDENTITY_PROVIDER_H_
#define COMPONENTS_INVALIDATION_PUBLIC_IDENTITY_PROVIDER_H_
#include <string>
#include "base/compiler_specific.h"
#include "base/functional/callback.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "base/values.h"
#include "google_apis/gaia/core_account_id.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "google_apis/gaia/oauth2_access_token_manager.h"
namespace invalidation {
// An opaque object that clients can use to control the lifetime of access
// token requests.
class ActiveAccountAccessTokenFetcher {
public:
ActiveAccountAccessTokenFetcher() = default;
ActiveAccountAccessTokenFetcher(
const ActiveAccountAccessTokenFetcher& other) = delete;
ActiveAccountAccessTokenFetcher& operator=(
const ActiveAccountAccessTokenFetcher& other) = delete;
virtual ~ActiveAccountAccessTokenFetcher() = default;
};
using ActiveAccountAccessTokenCallback =
base::OnceCallback<void(GoogleServiceAuthError error,
std::string access_token)>;
// Helper class that provides access to information about the "active GAIA
// account" with which invalidation should interact. The definition of the
// "active Gaia account is context-dependent": the purpose of this abstraction
// layer is to allow invalidation to interact with either device identity or
// user identity via a uniform interface.
class IdentityProvider {
public:
class Observer {
public:
Observer() = default;
Observer(const Observer& other) = delete;
Observer& operator=(const Observer& other) = delete;
virtual ~Observer() = default;
// 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() = 0;
// 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() = 0;
// Called when the active GAIA account's refresh token is updated.
virtual void OnActiveAccountRefreshTokenUpdated() = 0;
};
IdentityProvider(const IdentityProvider& other) = delete;
IdentityProvider& operator=(const IdentityProvider& other) = delete;
virtual ~IdentityProvider();
// Gets the active account's account ID.
virtual CoreAccountId GetActiveAccountId() = 0;
// Returns true iff (1) there is an active account and (2) that account has
// a refresh token.
virtual bool IsActiveAccountWithRefreshToken() = 0;
// Starts an access token request for |oauth_consumer_name| and |scopes|. When
// the request completes, |callback| will be invoked with the access token
// or error. To cancel the request, destroy the returned TokenFetcher.
virtual std::unique_ptr<ActiveAccountAccessTokenFetcher> FetchAccessToken(
const std::string& oauth_consumer_name,
const OAuth2AccessTokenManager::ScopeSet& scopes,
ActiveAccountAccessTokenCallback callback) = 0;
// Marks an OAuth2 |access_token| issued for the active account and |scopes|
// as invalid.
virtual void InvalidateAccessToken(
const OAuth2AccessTokenManager::ScopeSet& scopes,
const std::string& access_token) = 0;
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
protected:
IdentityProvider();
// Processes a refresh token update, firing the observer callback if
// |account_id| is the active account.
void ProcessRefreshTokenUpdateForAccount(const CoreAccountId& account_id);
// Fires an OnActiveAccountLogin notification.
void FireOnActiveAccountLogin();
// Fires an OnActiveAccountLogout notification.
void FireOnActiveAccountLogout();
private:
base::ObserverList<Observer, true>::Unchecked observers_;
};
} // namespace invalidation
#endif // COMPONENTS_INVALIDATION_PUBLIC_IDENTITY_PROVIDER_H_
|