account_tracker.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #ifndef COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_
  5. #define COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/observer_list.h"
  11. #include "components/signin/public/identity_manager/identity_manager.h"
  12. #include "google_apis/gaia/core_account_id.h"
  13. namespace gcm {
  14. // The AccountTracker keeps track of what accounts exist on the
  15. // profile and the state of their credentials.
  16. class AccountTracker : public signin::IdentityManager::Observer {
  17. public:
  18. explicit AccountTracker(signin::IdentityManager* identity_manager);
  19. ~AccountTracker() override;
  20. class Observer {
  21. public:
  22. virtual void OnAccountSignInChanged(const CoreAccountInfo& account,
  23. bool is_signed_in) = 0;
  24. };
  25. void Shutdown();
  26. void AddObserver(Observer* observer);
  27. void RemoveObserver(Observer* observer);
  28. // Returns the list of accounts that are signed in, and for which gaia account
  29. // have been fetched. The primary account for the profile will be first
  30. // in the vector. Additional accounts will be in order of their gaia account.
  31. std::vector<CoreAccountInfo> GetAccounts() const;
  32. private:
  33. struct AccountState {
  34. CoreAccountInfo account;
  35. bool is_signed_in;
  36. };
  37. // signin::IdentityManager::Observer implementation.
  38. void OnPrimaryAccountChanged(
  39. const signin::PrimaryAccountChangeEvent& event) override;
  40. void OnRefreshTokenUpdatedForAccount(
  41. const CoreAccountInfo& account_info) override;
  42. void OnRefreshTokenRemovedForAccount(
  43. const CoreAccountId& account_id) override;
  44. // Add |account_info| to the lists of accounts tracked by this AccountTracker.
  45. void StartTrackingAccount(const CoreAccountInfo& account_info);
  46. // Stops tracking |account_id|. Notifies all observers if the account was
  47. // previously signed in.
  48. void StopTrackingAccount(const CoreAccountId account_id);
  49. // Stops tracking all accounts.
  50. void StopTrackingAllAccounts();
  51. // Updates the is_signed_in corresponding to the given account. Notifies all
  52. // observers of the signed in state changes.
  53. void UpdateSignInState(const CoreAccountId& account_id, bool is_signed_in);
  54. raw_ptr<signin::IdentityManager> identity_manager_;
  55. std::map<CoreAccountId, AccountState> accounts_;
  56. base::ObserverList<Observer>::Unchecked observer_list_;
  57. bool shutdown_called_;
  58. };
  59. } // namespace gcm
  60. #endif // COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_