gcm_account_tracker.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_GCM_ACCOUNT_TRACKER_H_
  5. #define COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_TRACKER_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/time/time.h"
  12. #include "components/gcm_driver/account_tracker.h"
  13. #include "components/gcm_driver/gcm_client.h"
  14. #include "components/gcm_driver/gcm_connection_observer.h"
  15. #include "components/signin/public/identity_manager/access_token_fetcher.h"
  16. namespace signin {
  17. struct AccessTokenInfo;
  18. class IdentityManager;
  19. } // namespace signin
  20. namespace base {
  21. class Time;
  22. }
  23. namespace gcm {
  24. class GCMDriver;
  25. // Class for reporting back which accounts are signed into. It is only meant to
  26. // be used when the user is signed into sync.
  27. //
  28. // This class makes a check for tokens periodically, to make sure the user is
  29. // still logged into the profile, so that in the case that the user is not, we
  30. // can immediately report that to the GCM and stop messages addressed to that
  31. // user from ever reaching Chrome.
  32. class GCMAccountTracker : public AccountTracker::Observer,
  33. public GCMConnectionObserver {
  34. public:
  35. // State of the account.
  36. // Allowed transitions:
  37. // TOKEN_NEEDED - account info was created.
  38. // TOKEN_NEEDED -> GETTING_TOKEN - access token was requested.
  39. // GETTING_TOKEN -> TOKEN_NEEDED - access token fetching failed.
  40. // GETTING_TOKEN -> TOKEN_PRESENT - access token fetching succeeded.
  41. // GETTING_TOKEN -> ACCOUNT_REMOVED - account was removed.
  42. // TOKEN_NEEDED -> ACCOUNT_REMOVED - account was removed.
  43. // TOKEN_PRESENT -> ACCOUNT_REMOVED - account was removed.
  44. enum AccountState {
  45. TOKEN_NEEDED, // Needs a token (AccountInfo was recently created or
  46. // token request failed).
  47. GETTING_TOKEN, // There is a pending token request.
  48. TOKEN_PRESENT, // We have a token for the account.
  49. ACCOUNT_REMOVED, // Account was removed, and we didn't report it yet.
  50. };
  51. // Stores necessary account information and state of token fetching.
  52. struct AccountInfo {
  53. AccountInfo(const std::string& email, AccountState state);
  54. ~AccountInfo();
  55. // Email address of the tracked account.
  56. std::string email;
  57. // OAuth2 access token, when |state| is TOKEN_PRESENT.
  58. std::string access_token;
  59. // Expiration time of the access tokens.
  60. base::Time expiration_time;
  61. // Status of the token fetching.
  62. AccountState state;
  63. };
  64. // |account_tracker| is used to deliver information about the accounts present
  65. // in the browser context to |driver|.
  66. GCMAccountTracker(std::unique_ptr<AccountTracker> account_tracker,
  67. signin::IdentityManager* identity_manager,
  68. GCMDriver* driver);
  69. GCMAccountTracker(const GCMAccountTracker&) = delete;
  70. GCMAccountTracker& operator=(const GCMAccountTracker&) = delete;
  71. ~GCMAccountTracker() override;
  72. // Shuts down the tracker ensuring a proper clean up. After Shutdown() is
  73. // called Start() and Stop() should no longer be used. Must be called before
  74. // destruction.
  75. void Shutdown();
  76. // Starts tracking accounts.
  77. void Start();
  78. // Gets the number of pending token requests. Only used for testing.
  79. size_t get_pending_token_request_count() const {
  80. return pending_token_requests_.size();
  81. }
  82. private:
  83. friend class GCMAccountTrackerTest;
  84. // Maps account keys to account states. Keyed by account_id as used by
  85. // IdentityManager.
  86. typedef std::map<CoreAccountId, AccountInfo> AccountInfos;
  87. // AccountTracker::Observer overrides.
  88. void OnAccountSignInChanged(const CoreAccountInfo& account,
  89. bool is_signed_in) override;
  90. void OnAccessTokenFetchCompleteForAccount(
  91. CoreAccountId account_id,
  92. GoogleServiceAuthError error,
  93. signin::AccessTokenInfo access_token_info);
  94. // GCMConnectionObserver overrides.
  95. void OnConnected(const net::IPEndPoint& ip_endpoint) override;
  96. void OnDisconnected() override;
  97. // Schedules token reporting.
  98. void ScheduleReportTokens();
  99. // Report the list of accounts with OAuth2 tokens back using the |callback_|
  100. // function. If there are token requests in progress, do nothing.
  101. void ReportTokens();
  102. // Verify that all of the tokens are ready to be passed down to the GCM
  103. // Driver, e.g. none of them has expired or is missing. Returns true if not
  104. // all tokens are valid and a fetching yet more tokens is required.
  105. void SanitizeTokens();
  106. // Indicates whether token reporting is required, either because it is due, or
  107. // some of the accounts were removed.
  108. bool IsTokenReportingRequired() const;
  109. // Indicates whether there are tokens that still need fetching.
  110. bool IsTokenFetchingRequired() const;
  111. // Gets the time until next token reporting.
  112. base::TimeDelta GetTimeToNextTokenReporting() const;
  113. // Checks on all known accounts, and calls GetToken(..) for those with
  114. // |state == TOKEN_NEEDED|.
  115. void GetAllNeededTokens();
  116. // Starts fetching the OAuth2 token for the GCM group scope.
  117. void GetToken(AccountInfos::iterator& account_iter);
  118. // Handling of actual sign in and sign out for accounts.
  119. void OnAccountSignedIn(const CoreAccountInfo& account);
  120. void OnAccountSignedOut(const CoreAccountInfo& account);
  121. // Account tracker.
  122. std::unique_ptr<AccountTracker> account_tracker_;
  123. raw_ptr<signin::IdentityManager> identity_manager_;
  124. raw_ptr<GCMDriver> driver_;
  125. // State of the account.
  126. AccountInfos account_infos_;
  127. // Indicates whether shutdown has been called.
  128. bool shutdown_called_;
  129. // Stores the ongoing access token fetchers for deletion either upon
  130. // completion or upon signout of the account for which the request is being
  131. // made.
  132. using AccountIDToTokenFetcherMap =
  133. std::map<CoreAccountId, std::unique_ptr<signin::AccessTokenFetcher>>;
  134. AccountIDToTokenFetcherMap pending_token_requests_;
  135. // Creates weak pointers used to postpone reporting tokens. See
  136. // ScheduleReportTokens.
  137. base::WeakPtrFactory<GCMAccountTracker> reporting_weak_ptr_factory_{this};
  138. };
  139. } // namespace gcm
  140. #endif // COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_TRACKER_H_