blob: 04c4e6ea9dd205ac26cf4c50c2b962b6608021c3 [file] [log] [blame]
[email protected]0d02c332014-06-19 20:56:501// 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
Colin Blundell021e3e4d2018-04-19 01:03:245#ifndef COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_
6#define COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_
[email protected]0d02c332014-06-19 20:56:507
8#include <map>
dchengf064ccc2016-04-08 17:35:409#include <memory>
[email protected]0d02c332014-06-19 20:56:5010#include <string>
11#include <vector>
12
Maks Orlovich8db7d0d62018-08-16 19:22:2713#include "base/memory/scoped_refptr.h"
[email protected]0d02c332014-06-19 20:56:5014#include "base/observer_list.h"
15#include "google_apis/gaia/gaia_oauth_client.h"
Colin Blundell2b8d74782018-08-02 07:27:5216#include "services/identity/public/cpp/access_token_fetcher.h"
17#include "services/identity/public/cpp/identity_manager.h"
[email protected]0d02c332014-06-19 20:56:5018
19class GoogleServiceAuthError;
20
Maks Orlovich8db7d0d62018-08-16 19:22:2721namespace network {
22class SharedURLLoaderFactory;
[email protected]0d02c332014-06-19 20:56:5023}
24
Colin Blundell021e3e4d2018-04-19 01:03:2425namespace gcm {
[email protected]0d02c332014-06-19 20:56:5026
27struct AccountIds {
28 std::string account_key; // The account ID used by OAuth2TokenService.
29 std::string gaia;
30 std::string email;
31};
32
33class AccountIdFetcher;
34
35// The AccountTracker keeps track of what accounts exist on the
36// profile and the state of their credentials. The tracker fetches the
37// gaia ID of each account it knows about.
38//
39// The AccountTracker maintains these invariants:
40// 1. Events are only fired after the gaia ID has been fetched.
41// 2. Add/Remove and SignIn/SignOut pairs are always generated in order.
42// 3. SignIn follows Add, and there will be a SignOut between SignIn & Remove.
43// 4. If there is no primary account, there are no other accounts.
Colin Blundell2b8d74782018-08-02 07:27:5244class AccountTracker : public identity::IdentityManager::Observer {
[email protected]0d02c332014-06-19 20:56:5045 public:
Maks Orlovich8db7d0d62018-08-16 19:22:2746 AccountTracker(
47 identity::IdentityManager* identity_manager,
48 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
dchengf93bb582014-10-21 16:11:5649 ~AccountTracker() override;
[email protected]0d02c332014-06-19 20:56:5050
51 class Observer {
52 public:
[email protected]0d02c332014-06-19 20:56:5053 virtual void OnAccountSignInChanged(const AccountIds& ids,
54 bool is_signed_in) = 0;
55 };
56
57 void Shutdown();
58
59 void AddObserver(Observer* observer);
60 void RemoveObserver(Observer* observer);
61
62 // Returns the list of accounts that are signed in, and for which gaia IDs
63 // have been fetched. The primary account for the profile will be first
64 // in the vector. Additional accounts will be in order of their gaia IDs.
65 std::vector<AccountIds> GetAccounts() const;
[email protected]0d02c332014-06-19 20:56:5066
Colin Blundell8f1112c12018-05-23 09:10:0967 // Indicates if all user information has been fetched. If the result is false,
68 // there are still unfinished fetchers.
69 virtual bool IsAllUserInfoFetched() const;
70
71 private:
72 friend class AccountIdFetcher;
73
74 struct AccountState {
75 AccountIds ids;
76 bool is_signed_in;
77 };
78
Colin Blundell2b8d74782018-08-02 07:27:5279 // identity::IdentityManager::Observer implementation.
Sylvain Defresnebbed9512019-02-08 10:54:3080 void OnPrimaryAccountSet(
81 const CoreAccountInfo& primary_account_info) override;
Colin Blundell2b8d74782018-08-02 07:27:5282 void OnPrimaryAccountCleared(
Sylvain Defresne07a3bed2019-02-11 15:02:5183 const CoreAccountInfo& previous_primary_account_info) override;
Colin Blundellbc1d0fc2018-11-30 14:56:2084 void OnRefreshTokenUpdatedForAccount(
85 const AccountInfo& account_info) override;
Colin Blundell63d23fa2018-09-20 09:01:5586 void OnRefreshTokenRemovedForAccount(const std::string& account_id) override;
[email protected]0d02c332014-06-19 20:56:5087
88 void OnUserInfoFetchSuccess(AccountIdFetcher* fetcher,
89 const std::string& gaia_id);
90 void OnUserInfoFetchFailure(AccountIdFetcher* fetcher);
91
[email protected]0d02c332014-06-19 20:56:5092 void NotifySignInChanged(const AccountState& account);
93
ki.stfu07be8392015-09-28 08:25:4194 void UpdateSignInState(const std::string& account_key, bool is_signed_in);
[email protected]0d02c332014-06-19 20:56:5095
ki.stfu07be8392015-09-28 08:25:4196 void StartTrackingAccount(const std::string& account_key);
97
98 // Note: |account_key| is passed by value here, because the original
99 // object may be stored in |accounts_| and if so, it will be destroyed
100 // after erasing the key from the map.
[email protected]0d02c332014-06-19 20:56:50101 void StopTrackingAccount(const std::string account_key);
ki.stfu07be8392015-09-28 08:25:41102
[email protected]0d02c332014-06-19 20:56:50103 void StopTrackingAllAccounts();
ki.stfu07be8392015-09-28 08:25:41104 void StartFetchingUserInfo(const std::string& account_key);
[email protected]0d02c332014-06-19 20:56:50105 void DeleteFetcher(AccountIdFetcher* fetcher);
106
Colin Blundell2b8d74782018-08-02 07:27:52107 identity::IdentityManager* identity_manager_;
Maks Orlovich8db7d0d62018-08-16 19:22:27108 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
avi144d04c2016-10-27 22:20:11109 std::map<std::string, std::unique_ptr<AccountIdFetcher>> user_info_requests_;
[email protected]0d02c332014-06-19 20:56:50110 std::map<std::string, AccountState> accounts_;
Trent Apteda250ec3ab2018-08-19 08:52:19111 base::ObserverList<Observer>::Unchecked observer_list_;
[email protected]0d02c332014-06-19 20:56:50112 bool shutdown_called_;
113};
114
Colin Blundell2b8d74782018-08-02 07:27:52115class AccountIdFetcher : public gaia::GaiaOAuthClient::Delegate {
[email protected]0d02c332014-06-19 20:56:50116 public:
Maks Orlovich8db7d0d62018-08-16 19:22:27117 AccountIdFetcher(
118 identity::IdentityManager* identity_manager,
119 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
120 AccountTracker* tracker,
121 const std::string& account_key);
dchengf93bb582014-10-21 16:11:56122 ~AccountIdFetcher() override;
[email protected]0d02c332014-06-19 20:56:50123
124 const std::string& account_key() { return account_key_; }
125
126 void Start();
127
Colin Blundell2b8d74782018-08-02 07:27:52128 void AccessTokenFetched(GoogleServiceAuthError error,
129 identity::AccessTokenInfo access_token_info);
[email protected]0d02c332014-06-19 20:56:50130
131 // gaia::GaiaOAuthClient::Delegate implementation.
dchengf93bb582014-10-21 16:11:56132 void OnGetUserIdResponse(const std::string& gaia_id) override;
133 void OnOAuthError() override;
134 void OnNetworkError(int response_code) override;
[email protected]0d02c332014-06-19 20:56:50135
136 private:
Colin Blundell2b8d74782018-08-02 07:27:52137 identity::IdentityManager* identity_manager_;
Maks Orlovich8db7d0d62018-08-16 19:22:27138 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
[email protected]0d02c332014-06-19 20:56:50139 AccountTracker* tracker_;
140 const std::string account_key_;
141
Colin Blundell2b8d74782018-08-02 07:27:52142 std::unique_ptr<identity::AccessTokenFetcher> access_token_fetcher_;
dchengf064ccc2016-04-08 17:35:40143 std::unique_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_;
[email protected]0d02c332014-06-19 20:56:50144};
145
Colin Blundell021e3e4d2018-04-19 01:03:24146} // namespace gcm
[email protected]0d02c332014-06-19 20:56:50147
Colin Blundell021e3e4d2018-04-19 01:03:24148#endif // COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_