[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" |
Henrique Ferreiro | 94eb46f | 2019-07-03 14:38:56 | [diff] [blame] | 14 | #include "components/signin/public/identity_manager/identity_manager.h" |
Tanmoy Mollik | c15f5297 | 2019-08-19 17:36:03 | [diff] [blame] | 15 | #include "google_apis/gaia/core_account_id.h" |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 16 | |
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 17 | namespace gcm { |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 18 | |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 19 | // The AccountTracker keeps track of what accounts exist on the |
Mihai Sardarescu | 386e6e51 | 2019-12-02 16:20:40 | [diff] [blame] | 20 | // profile and the state of their credentials. |
Miyoung Shin | 23737f6 | 2019-07-23 15:43:31 | [diff] [blame] | 21 | class AccountTracker : public signin::IdentityManager::Observer { |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 22 | public: |
Mihai Sardarescu | 386e6e51 | 2019-12-02 16:20:40 | [diff] [blame] | 23 | explicit AccountTracker(signin::IdentityManager* identity_manager); |
dcheng | f93bb58 | 2014-10-21 16:11:56 | [diff] [blame] | 24 | ~AccountTracker() override; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 25 | |
26 | class Observer { | ||||
27 | public: | ||||
Mihai Sardarescu | 386e6e51 | 2019-12-02 16:20:40 | [diff] [blame] | 28 | virtual void OnAccountSignInChanged(const CoreAccountInfo& account, |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 29 | bool is_signed_in) = 0; |
30 | }; | ||||
31 | |||||
32 | void Shutdown(); | ||||
33 | |||||
34 | void AddObserver(Observer* observer); | ||||
35 | void RemoveObserver(Observer* observer); | ||||
36 | |||||
Mihai Sardarescu | 386e6e51 | 2019-12-02 16:20:40 | [diff] [blame] | 37 | // Returns the list of accounts that are signed in, and for which gaia account |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 38 | // have been fetched. The primary account for the profile will be first |
Mihai Sardarescu | 386e6e51 | 2019-12-02 16:20:40 | [diff] [blame] | 39 | // in the vector. Additional accounts will be in order of their gaia account. |
40 | std::vector<CoreAccountInfo> GetAccounts() const; | ||||
Colin Blundell | 8f1112c1 | 2018-05-23 09:10:09 | [diff] [blame] | 41 | |
42 | private: | ||||
Colin Blundell | 8f1112c1 | 2018-05-23 09:10:09 | [diff] [blame] | 43 | struct AccountState { |
Mihai Sardarescu | 386e6e51 | 2019-12-02 16:20:40 | [diff] [blame] | 44 | CoreAccountInfo account; |
Colin Blundell | 8f1112c1 | 2018-05-23 09:10:09 | [diff] [blame] | 45 | bool is_signed_in; |
46 | }; | ||||
47 | |||||
Miyoung Shin | 23737f6 | 2019-07-23 15:43:31 | [diff] [blame] | 48 | // signin::IdentityManager::Observer implementation. |
Sylvain Defresne | bbed951 | 2019-02-08 10:54:30 | [diff] [blame] | 49 | void OnPrimaryAccountSet( |
50 | const CoreAccountInfo& primary_account_info) override; | ||||
Colin Blundell | 2b8d7478 | 2018-08-02 07:27:52 | [diff] [blame] | 51 | void OnPrimaryAccountCleared( |
Sylvain Defresne | 07a3bed | 2019-02-11 15:02:51 | [diff] [blame] | 52 | const CoreAccountInfo& previous_primary_account_info) override; |
Colin Blundell | bc1d0fc | 2018-11-30 14:56:20 | [diff] [blame] | 53 | void OnRefreshTokenUpdatedForAccount( |
Gyuyoung Kim | b70d3c7 | 2019-02-12 01:45:43 | [diff] [blame] | 54 | const CoreAccountInfo& account_info) override; |
Tanmoy Mollik | 66a47f78 | 2019-05-20 15:05:18 | [diff] [blame] | 55 | void OnRefreshTokenRemovedForAccount( |
56 | const CoreAccountId& account_id) override; | ||||
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 57 | |
Mihai Sardarescu | 386e6e51 | 2019-12-02 16:20:40 | [diff] [blame] | 58 | // Add |account_info| to the lists of accounts tracked by this AccountTracker. |
59 | void StartTrackingAccount(const CoreAccountInfo& account_info); | ||||
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 60 | |
Mihai Sardarescu | 386e6e51 | 2019-12-02 16:20:40 | [diff] [blame] | 61 | // Stops tracking |account_id|. Notifies all observers if the account was |
62 | // previously signed in. | ||||
63 | void StopTrackingAccount(const CoreAccountId account_id); | ||||
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 64 | |
Mihai Sardarescu | 386e6e51 | 2019-12-02 16:20:40 | [diff] [blame] | 65 | // Stops tracking all accounts. |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 66 | void StopTrackingAllAccounts(); |
Mihai Sardarescu | 386e6e51 | 2019-12-02 16:20:40 | [diff] [blame] | 67 | |
68 | // Updates the is_signed_in corresponding to the given account. Notifies all | ||||
69 | // observers of the signed in state changes. | ||||
70 | void UpdateSignInState(const CoreAccountId& account_id, bool is_signed_in); | ||||
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 71 | |
Miyoung Shin | 23737f6 | 2019-07-23 15:43:31 | [diff] [blame] | 72 | signin::IdentityManager* identity_manager_; |
Tanmoy Mollik | c15f5297 | 2019-08-19 17:36:03 | [diff] [blame] | 73 | std::map<CoreAccountId, AccountState> accounts_; |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 74 | base::ObserverList<Observer>::Unchecked observer_list_; |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 75 | bool shutdown_called_; |
76 | }; | ||||
77 | |||||
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 78 | } // namespace gcm |
[email protected] | 0d02c33 | 2014-06-19 20:56:50 | [diff] [blame] | 79 | |
Colin Blundell | 021e3e4d | 2018-04-19 01:03:24 | [diff] [blame] | 80 | #endif // COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_ |