[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" |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 14 | #include "google_apis/gaia/gaia_oauth_client.h" |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame^] | 15 | #include "services/identity/public/cpp/access_token_fetcher.h" |
| 16 | #include "services/identity/public/cpp/identity_manager.h" |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 17 | |
| 18 | class GoogleServiceAuthError; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 19 | |
| 20 | namespace net { |
| 21 | class URLRequestContextGetter; |
| 22 | } |
| 23 | |
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 24 | namespace gcm { |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 25 | |
| 26 | struct AccountIds { |
| 27 | std::string account_key; // The account ID used by OAuth2TokenService. |
| 28 | std::string gaia; |
| 29 | std::string email; |
| 30 | }; |
| 31 | |
| 32 | class AccountIdFetcher; |
| 33 | |
| 34 | // The AccountTracker keeps track of what accounts exist on the |
| 35 | // profile and the state of their credentials. The tracker fetches the |
| 36 | // gaia ID of each account it knows about. |
| 37 | // |
| 38 | // The AccountTracker maintains these invariants: |
| 39 | // 1. Events are only fired after the gaia ID has been fetched. |
| 40 | // 2. Add/Remove and SignIn/SignOut pairs are always generated in order. |
| 41 | // 3. SignIn follows Add, and there will be a SignOut between SignIn & Remove. |
| 42 | // 4. If there is no primary account, there are no other accounts. |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame^] | 43 | class AccountTracker : public identity::IdentityManager::Observer { |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 44 | public: |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame^] | 45 | AccountTracker(identity::IdentityManager* identity_manager, |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 46 | net::URLRequestContextGetter* request_context_getter); |
dcheng | f93bb58 | 2014-10-21 16:11:56 | [diff] [blame] | 47 | ~AccountTracker() override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 48 | |
| 49 | class Observer { |
| 50 | public: |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 51 | virtual void OnAccountSignInChanged(const AccountIds& ids, |
| 52 | bool is_signed_in) = 0; |
| 53 | }; |
| 54 | |
| 55 | void Shutdown(); |
| 56 | |
| 57 | void AddObserver(Observer* observer); |
| 58 | void RemoveObserver(Observer* observer); |
| 59 | |
| 60 | // Returns the list of accounts that are signed in, and for which gaia IDs |
| 61 | // have been fetched. The primary account for the profile will be first |
| 62 | // in the vector. Additional accounts will be in order of their gaia IDs. |
| 63 | std::vector<AccountIds> GetAccounts() const; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 64 | |
Colin Blundell | 8f1112c1 | 2018-05-23 09:10:09 | [diff] [blame] | 65 | // Indicates if all user information has been fetched. If the result is false, |
| 66 | // there are still unfinished fetchers. |
| 67 | virtual bool IsAllUserInfoFetched() const; |
| 68 | |
| 69 | private: |
| 70 | friend class AccountIdFetcher; |
| 71 | |
| 72 | struct AccountState { |
| 73 | AccountIds ids; |
| 74 | bool is_signed_in; |
| 75 | }; |
| 76 | |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame^] | 77 | // identity::IdentityManager::Observer implementation. |
| 78 | void OnPrimaryAccountSet(const AccountInfo& primary_account_info) override; |
| 79 | void OnPrimaryAccountCleared( |
| 80 | const AccountInfo& previous_primary_account_info) override; |
| 81 | void OnRefreshTokenUpdatedForAccount(const AccountInfo& account_info, |
| 82 | bool is_valid) override; |
| 83 | void OnRefreshTokenRemovedForAccount( |
| 84 | const AccountInfo& account_info) override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 85 | |
| 86 | void OnUserInfoFetchSuccess(AccountIdFetcher* fetcher, |
| 87 | const std::string& gaia_id); |
| 88 | void OnUserInfoFetchFailure(AccountIdFetcher* fetcher); |
| 89 | |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 90 | void NotifySignInChanged(const AccountState& account); |
| 91 | |
ki.stfu | 07be839 | 2015-09-28 08:25:41 | [diff] [blame] | 92 | void UpdateSignInState(const std::string& account_key, bool is_signed_in); |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 93 | |
ki.stfu | 07be839 | 2015-09-28 08:25:41 | [diff] [blame] | 94 | void StartTrackingAccount(const std::string& account_key); |
| 95 | |
| 96 | // Note: |account_key| is passed by value here, because the original |
| 97 | // object may be stored in |accounts_| and if so, it will be destroyed |
| 98 | // after erasing the key from the map. |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 99 | void StopTrackingAccount(const std::string account_key); |
ki.stfu | 07be839 | 2015-09-28 08:25:41 | [diff] [blame] | 100 | |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 101 | void StopTrackingAllAccounts(); |
ki.stfu | 07be839 | 2015-09-28 08:25:41 | [diff] [blame] | 102 | void StartFetchingUserInfo(const std::string& account_key); |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 103 | void DeleteFetcher(AccountIdFetcher* fetcher); |
| 104 | |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame^] | 105 | identity::IdentityManager* identity_manager_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 106 | scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
avi | 144d04c | 2016-10-27 22:20:11 | [diff] [blame] | 107 | std::map<std::string, std::unique_ptr<AccountIdFetcher>> user_info_requests_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 108 | std::map<std::string, AccountState> accounts_; |
brettw | 236d317 | 2015-06-03 16:31:43 | [diff] [blame] | 109 | base::ObserverList<Observer> observer_list_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 110 | bool shutdown_called_; |
| 111 | }; |
| 112 | |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame^] | 113 | class AccountIdFetcher : public gaia::GaiaOAuthClient::Delegate { |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 114 | public: |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame^] | 115 | AccountIdFetcher(identity::IdentityManager* identity_manager, |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 116 | net::URLRequestContextGetter* request_context_getter, |
| 117 | AccountTracker* tracker, |
| 118 | const std::string& account_key); |
dcheng | f93bb58 | 2014-10-21 16:11:56 | [diff] [blame] | 119 | ~AccountIdFetcher() override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 120 | |
| 121 | const std::string& account_key() { return account_key_; } |
| 122 | |
| 123 | void Start(); |
| 124 | |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame^] | 125 | void AccessTokenFetched(GoogleServiceAuthError error, |
| 126 | identity::AccessTokenInfo access_token_info); |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 127 | |
| 128 | // gaia::GaiaOAuthClient::Delegate implementation. |
dcheng | f93bb58 | 2014-10-21 16:11:56 | [diff] [blame] | 129 | void OnGetUserIdResponse(const std::string& gaia_id) override; |
| 130 | void OnOAuthError() override; |
| 131 | void OnNetworkError(int response_code) override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 132 | |
| 133 | private: |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame^] | 134 | identity::IdentityManager* identity_manager_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 135 | net::URLRequestContextGetter* request_context_getter_; |
| 136 | AccountTracker* tracker_; |
| 137 | const std::string account_key_; |
| 138 | |
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame^] | 139 | std::unique_ptr<identity::AccessTokenFetcher> access_token_fetcher_; |
dcheng | f064ccc | 2016-04-08 17:35:40 | [diff] [blame] | 140 | std::unique_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 141 | }; |
| 142 | |
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 143 | } // namespace gcm |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 144 | |
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 145 | #endif // COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_ |