[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 | |
Maks Orlovich | 8db7d0d6 | 2018-08-16 19:22:27 | [diff] [blame] | 13 | #include "base/memory/scoped_refptr.h" |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 14 | #include "base/observer_list.h" |
| 15 | #include "google_apis/gaia/gaia_oauth_client.h" |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame] | 16 | #include "services/identity/public/cpp/access_token_fetcher.h" |
| 17 | #include "services/identity/public/cpp/identity_manager.h" |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 18 | |
| 19 | class GoogleServiceAuthError; |
| 20 | |
Maks Orlovich | 8db7d0d6 | 2018-08-16 19:22:27 | [diff] [blame] | 21 | namespace network { |
| 22 | class SharedURLLoaderFactory; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 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. |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame] | 44 | class AccountTracker : public identity::IdentityManager::Observer { |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 45 | public: |
Maks Orlovich | 8db7d0d6 | 2018-08-16 19:22:27 | [diff] [blame] | 46 | AccountTracker( |
| 47 | identity::IdentityManager* identity_manager, |
| 48 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory); |
dcheng | f93bb58 | 2014-10-21 16:11:56 | [diff] [blame] | 49 | ~AccountTracker() override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 50 | |
| 51 | class Observer { |
| 52 | public: |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 53 | 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] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 66 | |
Colin Blundell | 8f1112c1 | 2018-05-23 09:10:09 | [diff] [blame] | 67 | // 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 Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame] | 79 | // identity::IdentityManager::Observer implementation. |
Sylvain Defresne | bbed951 | 2019-02-08 10:54:30 | [diff] [blame] | 80 | void OnPrimaryAccountSet( |
| 81 | const CoreAccountInfo& primary_account_info) override; |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame] | 82 | void OnPrimaryAccountCleared( |
Sylvain Defresne | 07a3bed | 2019-02-11 15:02:51 | [diff] [blame] | 83 | const CoreAccountInfo& previous_primary_account_info) override; |
Colin Blundell | bc1d0fc | 2018-11-30 14:56:20 | [diff] [blame] | 84 | void OnRefreshTokenUpdatedForAccount( |
Gyuyoung Kim | b70d3c7 | 2019-02-12 01:45:43 | [diff] [blame^] | 85 | const CoreAccountInfo& account_info) override; |
Colin Blundell | 63d23fa | 2018-09-20 09:01:55 | [diff] [blame] | 86 | void OnRefreshTokenRemovedForAccount(const std::string& account_id) override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 87 | |
| 88 | void OnUserInfoFetchSuccess(AccountIdFetcher* fetcher, |
| 89 | const std::string& gaia_id); |
| 90 | void OnUserInfoFetchFailure(AccountIdFetcher* fetcher); |
| 91 | |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 92 | void NotifySignInChanged(const AccountState& account); |
| 93 | |
ki.stfu | 07be839 | 2015-09-28 08:25:41 | [diff] [blame] | 94 | void UpdateSignInState(const std::string& account_key, bool is_signed_in); |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 95 | |
ki.stfu | 07be839 | 2015-09-28 08:25:41 | [diff] [blame] | 96 | 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] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 101 | void StopTrackingAccount(const std::string account_key); |
ki.stfu | 07be839 | 2015-09-28 08:25:41 | [diff] [blame] | 102 | |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 103 | void StopTrackingAllAccounts(); |
ki.stfu | 07be839 | 2015-09-28 08:25:41 | [diff] [blame] | 104 | void StartFetchingUserInfo(const std::string& account_key); |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 105 | void DeleteFetcher(AccountIdFetcher* fetcher); |
| 106 | |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame] | 107 | identity::IdentityManager* identity_manager_; |
Maks Orlovich | 8db7d0d6 | 2018-08-16 19:22:27 | [diff] [blame] | 108 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; |
avi | 144d04c | 2016-10-27 22:20:11 | [diff] [blame] | 109 | std::map<std::string, std::unique_ptr<AccountIdFetcher>> user_info_requests_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 110 | std::map<std::string, AccountState> accounts_; |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 111 | base::ObserverList<Observer>::Unchecked observer_list_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 112 | bool shutdown_called_; |
| 113 | }; |
| 114 | |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame] | 115 | class AccountIdFetcher : public gaia::GaiaOAuthClient::Delegate { |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 116 | public: |
Maks Orlovich | 8db7d0d6 | 2018-08-16 19:22:27 | [diff] [blame] | 117 | AccountIdFetcher( |
| 118 | identity::IdentityManager* identity_manager, |
| 119 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, |
| 120 | AccountTracker* tracker, |
| 121 | const std::string& account_key); |
dcheng | f93bb58 | 2014-10-21 16:11:56 | [diff] [blame] | 122 | ~AccountIdFetcher() override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 123 | |
| 124 | const std::string& account_key() { return account_key_; } |
| 125 | |
| 126 | void Start(); |
| 127 | |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame] | 128 | void AccessTokenFetched(GoogleServiceAuthError error, |
| 129 | identity::AccessTokenInfo access_token_info); |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 130 | |
| 131 | // gaia::GaiaOAuthClient::Delegate implementation. |
dcheng | f93bb58 | 2014-10-21 16:11:56 | [diff] [blame] | 132 | void OnGetUserIdResponse(const std::string& gaia_id) override; |
| 133 | void OnOAuthError() override; |
| 134 | void OnNetworkError(int response_code) override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 135 | |
| 136 | private: |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame] | 137 | identity::IdentityManager* identity_manager_; |
Maks Orlovich | 8db7d0d6 | 2018-08-16 19:22:27 | [diff] [blame] | 138 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 139 | AccountTracker* tracker_; |
| 140 | const std::string account_key_; |
| 141 | |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame] | 142 | std::unique_ptr<identity::AccessTokenFetcher> access_token_fetcher_; |
dcheng | f064ccc | 2016-04-08 17:35:40 | [diff] [blame] | 143 | std::unique_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 144 | }; |
| 145 | |
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 146 | } // namespace gcm |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 147 | |
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 148 | #endif // COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_ |