[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 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 | |
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 5 | #ifndef COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_ |
| 6 | #define COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_ |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 7 | |
| 8 | #include <map> |
dcheng | f064ccc | 2016-04-08 17:35:40 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 13 | #include "base/observer_list.h" |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 14 | #include "components/signin/core/browser/signin_manager.h" |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 15 | #include "google_apis/gaia/gaia_oauth_client.h" |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 16 | #include "google_apis/gaia/oauth2_token_service.h" |
| 17 | |
| 18 | class GoogleServiceAuthError; |
Colin Blundell | f878993 | 2018-05-22 11:35:56 | [diff] [blame] | 19 | class ProfileOAuth2TokenService; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 20 | |
| 21 | namespace net { |
| 22 | class URLRequestContextGetter; |
| 23 | } |
| 24 | |
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 25 | namespace gcm { |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 26 | |
| 27 | struct AccountIds { |
| 28 | std::string account_key; // The account ID used by OAuth2TokenService. |
| 29 | std::string gaia; |
| 30 | std::string email; |
| 31 | }; |
| 32 | |
| 33 | class 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. |
| 44 | class AccountTracker : public OAuth2TokenService::Observer, |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 45 | public SigninManagerBase::Observer { |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 46 | public: |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 47 | AccountTracker(SigninManagerBase* signin_manager, |
Colin Blundell | f878993 | 2018-05-22 11:35:56 | [diff] [blame] | 48 | ProfileOAuth2TokenService* token_service, |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 49 | net::URLRequestContextGetter* request_context_getter); |
dcheng | f93bb58 | 2014-10-21 16:11:56 | [diff] [blame] | 50 | ~AccountTracker() override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 51 | |
| 52 | class Observer { |
| 53 | public: |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 54 | virtual void OnAccountSignInChanged(const AccountIds& ids, |
| 55 | bool is_signed_in) = 0; |
| 56 | }; |
| 57 | |
| 58 | void Shutdown(); |
| 59 | |
| 60 | void AddObserver(Observer* observer); |
| 61 | void RemoveObserver(Observer* observer); |
| 62 | |
| 63 | // Returns the list of accounts that are signed in, and for which gaia IDs |
| 64 | // have been fetched. The primary account for the profile will be first |
| 65 | // in the vector. Additional accounts will be in order of their gaia IDs. |
| 66 | std::vector<AccountIds> GetAccounts() const; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 67 | |
Colin Blundell | 8f1112c1 | 2018-05-23 09:10:09 | [diff] [blame^] | 68 | // Indicates if all user information has been fetched. If the result is false, |
| 69 | // there are still unfinished fetchers. |
| 70 | virtual bool IsAllUserInfoFetched() const; |
| 71 | |
| 72 | private: |
| 73 | friend class AccountIdFetcher; |
| 74 | |
| 75 | struct AccountState { |
| 76 | AccountIds ids; |
| 77 | bool is_signed_in; |
| 78 | }; |
| 79 | |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 80 | // OAuth2TokenService::Observer implementation. |
dcheng | f93bb58 | 2014-10-21 16:11:56 | [diff] [blame] | 81 | void OnRefreshTokenAvailable(const std::string& account_key) override; |
| 82 | void OnRefreshTokenRevoked(const std::string& account_key) override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 83 | |
| 84 | void OnUserInfoFetchSuccess(AccountIdFetcher* fetcher, |
| 85 | const std::string& gaia_id); |
| 86 | void OnUserInfoFetchFailure(AccountIdFetcher* fetcher); |
| 87 | |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 88 | // SigninManagerBase::Observer implementation. |
| 89 | void GoogleSigninSucceeded(const std::string& account_id, |
| 90 | const std::string& username) override; |
| 91 | void GoogleSignedOut(const std::string& account_id, |
| 92 | const std::string& username) override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 93 | |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 94 | void NotifySignInChanged(const AccountState& account); |
| 95 | |
ki.stfu | 07be839 | 2015-09-28 08:25:41 | [diff] [blame] | 96 | void UpdateSignInState(const std::string& account_key, bool is_signed_in); |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 97 | |
ki.stfu | 07be839 | 2015-09-28 08:25:41 | [diff] [blame] | 98 | void StartTrackingAccount(const std::string& account_key); |
| 99 | |
| 100 | // Note: |account_key| is passed by value here, because the original |
| 101 | // object may be stored in |accounts_| and if so, it will be destroyed |
| 102 | // after erasing the key from the map. |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 103 | void StopTrackingAccount(const std::string account_key); |
ki.stfu | 07be839 | 2015-09-28 08:25:41 | [diff] [blame] | 104 | |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 105 | void StopTrackingAllAccounts(); |
ki.stfu | 07be839 | 2015-09-28 08:25:41 | [diff] [blame] | 106 | void StartFetchingUserInfo(const std::string& account_key); |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 107 | void DeleteFetcher(AccountIdFetcher* fetcher); |
| 108 | |
Colin Blundell | bf6d4c5f | 2018-05-16 11:12:29 | [diff] [blame] | 109 | SigninManagerBase* signin_manager_; |
Colin Blundell | f878993 | 2018-05-22 11:35:56 | [diff] [blame] | 110 | ProfileOAuth2TokenService* token_service_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 111 | scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
avi | 144d04c | 2016-10-27 22:20:11 | [diff] [blame] | 112 | std::map<std::string, std::unique_ptr<AccountIdFetcher>> user_info_requests_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 113 | std::map<std::string, AccountState> accounts_; |
brettw | 236d317 | 2015-06-03 16:31:43 | [diff] [blame] | 114 | base::ObserverList<Observer> observer_list_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 115 | bool shutdown_called_; |
| 116 | }; |
| 117 | |
| 118 | class AccountIdFetcher : public OAuth2TokenService::Consumer, |
| 119 | public gaia::GaiaOAuthClient::Delegate { |
| 120 | public: |
| 121 | AccountIdFetcher(OAuth2TokenService* token_service, |
| 122 | net::URLRequestContextGetter* request_context_getter, |
| 123 | AccountTracker* tracker, |
| 124 | const std::string& account_key); |
dcheng | f93bb58 | 2014-10-21 16:11:56 | [diff] [blame] | 125 | ~AccountIdFetcher() override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 126 | |
| 127 | const std::string& account_key() { return account_key_; } |
| 128 | |
| 129 | void Start(); |
| 130 | |
| 131 | // OAuth2TokenService::Consumer implementation. |
dcheng | f93bb58 | 2014-10-21 16:11:56 | [diff] [blame] | 132 | void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 133 | const std::string& access_token, |
| 134 | const base::Time& expiration_time) override; |
| 135 | void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 136 | const GoogleServiceAuthError& error) override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 137 | |
| 138 | // gaia::GaiaOAuthClient::Delegate implementation. |
dcheng | f93bb58 | 2014-10-21 16:11:56 | [diff] [blame] | 139 | void OnGetUserIdResponse(const std::string& gaia_id) override; |
| 140 | void OnOAuthError() override; |
| 141 | void OnNetworkError(int response_code) override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 142 | |
| 143 | private: |
| 144 | OAuth2TokenService* token_service_; |
| 145 | net::URLRequestContextGetter* request_context_getter_; |
| 146 | AccountTracker* tracker_; |
| 147 | const std::string account_key_; |
| 148 | |
dcheng | f064ccc | 2016-04-08 17:35:40 | [diff] [blame] | 149 | std::unique_ptr<OAuth2TokenService::Request> login_token_request_; |
| 150 | std::unique_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 151 | }; |
| 152 | |
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 153 | } // namespace gcm |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 154 | |
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 155 | #endif // COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_ |