Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ |
| 6 #define CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | 6 #define CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
| 12 #include "components/gcm_driver/gcm_client.h" | 12 #include "components/gcm_driver/gcm_client.h" |
| 13 #include "components/gcm_driver/gcm_connection_observer.h" | 13 #include "components/gcm_driver/gcm_connection_observer.h" |
| 14 #include "google_apis/gaia/account_tracker.h" | 14 #include "google_apis/gaia/account_tracker.h" |
| 15 #include "google_apis/gaia/oauth2_token_service.h" | 15 #include "google_apis/gaia/oauth2_token_service.h" |
| 16 #include "testing/gtest/include/gtest/gtest_prod.h" | |
|
Nicolas Zea
2014/11/06 21:43:17
why is this needed?
fgorski
2014/11/07 01:42:13
Done.
In the end I made the test class a friend
| |
| 17 | |
| 18 namespace base { | |
| 19 class Time; | |
| 20 } | |
| 16 | 21 |
| 17 namespace gcm { | 22 namespace gcm { |
| 18 | 23 |
| 19 class GCMDriver; | 24 class GCMDriver; |
| 20 | 25 |
| 21 // Class for reporting back which accounts are signed into. It is only meant to | 26 // Class for reporting back which accounts are signed into. It is only meant to |
| 22 // be used when the user is signed into sync. | 27 // be used when the user is signed into sync. |
| 28 // | |
| 29 // This class makes a check for tokens periodically, to make sure the user is | |
| 30 // still logged into the profile, so that in the case that the user is not, we | |
|
Nicolas Zea
2014/11/06 21:43:17
Is it just to ensure that we're still logged into
fgorski
2014/11/07 01:42:13
Every checkin with GCM should have a fresh set of
Nicolas Zea
2014/11/07 22:19:25
I think its worth mentioning this part in the comm
| |
| 31 // can immediately report that to the GCM and stop messages addressed to that | |
| 32 // user from ever reaching Chrome. | |
| 23 class GCMAccountTracker : public gaia::AccountTracker::Observer, | 33 class GCMAccountTracker : public gaia::AccountTracker::Observer, |
| 24 public OAuth2TokenService::Consumer, | 34 public OAuth2TokenService::Consumer, |
| 25 public GCMConnectionObserver { | 35 public GCMConnectionObserver { |
| 26 public: | 36 public: |
| 27 // State of the account. | 37 // State of the account. |
| 28 // Allowed transitions: | 38 // Allowed transitions: |
| 29 // TOKEN_NEEDED - account info was created. | 39 // TOKEN_NEEDED - account info was created. |
| 30 // TOKEN_NEEDED -> GETTING_TOKEN - access token was requested. | 40 // TOKEN_NEEDED -> GETTING_TOKEN - access token was requested. |
| 31 // GETTING_TOKEN -> TOKEN_NEEDED - access token fetching failed. | 41 // GETTING_TOKEN -> TOKEN_NEEDED - access token fetching failed. |
| 32 // GETTING_TOKEN -> TOKEN_PRESENT - access token fetching succeeded. | 42 // GETTING_TOKEN -> TOKEN_PRESENT - access token fetching succeeded. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 | 79 |
| 70 // Starts tracking accounts. | 80 // Starts tracking accounts. |
| 71 void Start(); | 81 void Start(); |
| 72 | 82 |
| 73 // Gets the number of pending token requests. Only used for testing. | 83 // Gets the number of pending token requests. Only used for testing. |
| 74 size_t get_pending_token_request_count() const { | 84 size_t get_pending_token_request_count() const { |
| 75 return pending_token_requests_.size(); | 85 return pending_token_requests_.size(); |
| 76 } | 86 } |
| 77 | 87 |
| 78 private: | 88 private: |
| 89 friend class GCMAccountTrackerTest; | |
| 90 | |
| 79 // Maps account keys to account states. Keyed by account_ids as used by | 91 // Maps account keys to account states. Keyed by account_ids as used by |
| 80 // OAuth2TokenService. | 92 // OAuth2TokenService. |
| 81 typedef std::map<std::string, AccountInfo> AccountInfos; | 93 typedef std::map<std::string, AccountInfo> AccountInfos; |
| 82 | 94 |
| 83 // AccountTracker::Observer overrides. | 95 // AccountTracker::Observer overrides. |
| 84 void OnAccountAdded(const gaia::AccountIds& ids) override; | 96 void OnAccountAdded(const gaia::AccountIds& ids) override; |
| 85 void OnAccountRemoved(const gaia::AccountIds& ids) override; | 97 void OnAccountRemoved(const gaia::AccountIds& ids) override; |
| 86 void OnAccountSignInChanged(const gaia::AccountIds& ids, | 98 void OnAccountSignInChanged(const gaia::AccountIds& ids, |
| 87 bool is_signed_in) override; | 99 bool is_signed_in) override; |
| 88 | 100 |
| 89 // OAuth2TokenService::Consumer overrides. | 101 // OAuth2TokenService::Consumer overrides. |
| 90 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 102 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 91 const std::string& access_token, | 103 const std::string& access_token, |
| 92 const base::Time& expiration_time) override; | 104 const base::Time& expiration_time) override; |
| 93 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 105 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 94 const GoogleServiceAuthError& error) override; | 106 const GoogleServiceAuthError& error) override; |
| 95 | 107 |
| 96 // GCMConnectionObserver overrides. | 108 // GCMConnectionObserver overrides. |
| 97 void OnConnected(const net::IPEndPoint& ip_endpoint) override; | 109 void OnConnected(const net::IPEndPoint& ip_endpoint) override; |
| 98 void OnDisconnected() override; | 110 void OnDisconnected() override; |
| 99 | 111 |
| 112 // Schedules fetching tokens at the time when they are needed to be reported. | |
| 113 void ScheduleFetchingTokens(); | |
| 100 // Report the list of accounts with OAuth2 tokens back using the |callback_| | 114 // Report the list of accounts with OAuth2 tokens back using the |callback_| |
| 101 // function. If there are token requests in progress, do nothing. | 115 // function. If there are token requests in progress, do nothing. |
| 102 void CompleteCollectingTokens(); | 116 void ReportTokens(); |
| 103 // Verify that all of the tokens are ready to be passed down to the GCM | 117 // Verify that all of the tokens are ready to be passed down to the GCM |
| 104 // Driver, e.g. none of them has expired or is missing. Returns true if not | 118 // Driver, e.g. none of them has expired or is missing. Returns true if not |
| 105 // all tokens are valid and a fetching yet more tokens is required. | 119 // all tokens are valid and a fetching yet more tokens is required. |
| 106 bool SanitizeTokens(); | 120 bool SanitizeTokens(); |
| 121 // Indicates whether token reporting is required, either because it is due, or | |
| 122 // some of the accounts were removed. | |
| 123 bool IsTokenReportingRequired() const; | |
| 124 // Indicates whether there are tokens that still need fetching. | |
| 125 bool IsTokenFetchingRequired() const; | |
| 126 // Gets the time until next token fetching. | |
| 127 base::TimeDelta GetTimeToNextTokenFetching() const; | |
| 107 // Deletes a token request. Should be called from OnGetTokenSuccess(..) or | 128 // Deletes a token request. Should be called from OnGetTokenSuccess(..) or |
| 108 // OnGetTokenFailure(..). | 129 // OnGetTokenFailure(..). |
| 109 void DeleteTokenRequest(const OAuth2TokenService::Request* request); | 130 void DeleteTokenRequest(const OAuth2TokenService::Request* request); |
| 110 // Checks on all known accounts, and calls GetToken(..) for those with | 131 // Checks on all known accounts, and calls GetToken(..) for those with |
| 111 // |state == TOKEN_NEEDED|. | 132 // |state == TOKEN_NEEDED|. |
| 112 void GetAllNeededTokens(); | 133 void GetAllNeededTokens(); |
| 113 // Starts fetching the OAuth2 token for the GCM group scope. | 134 // Starts fetching the OAuth2 token for the GCM group scope. |
| 114 void GetToken(AccountInfos::iterator& account_iter); | 135 void GetToken(AccountInfos::iterator& account_iter); |
| 115 | 136 |
| 116 // Handling of actual sign in and sign out for accounts. | 137 // Handling of actual sign in and sign out for accounts. |
| 117 void OnAccountSignedIn(const gaia::AccountIds& ids); | 138 void OnAccountSignedIn(const gaia::AccountIds& ids); |
| 118 void OnAccountSignedOut(const gaia::AccountIds& ids); | 139 void OnAccountSignedOut(const gaia::AccountIds& ids); |
| 119 | 140 |
| 120 OAuth2TokenService* GetTokenService(); | 141 OAuth2TokenService* GetTokenService(); |
| 121 | 142 |
| 122 // Account tracker. | 143 // Account tracker. |
| 123 scoped_ptr<gaia::AccountTracker> account_tracker_; | 144 scoped_ptr<gaia::AccountTracker> account_tracker_; |
| 124 | 145 |
| 125 // GCM Driver. Not owned. | 146 // GCM Driver. Not owned. |
| 126 GCMDriver* driver_; | 147 GCMDriver* driver_; |
| 127 | 148 |
| 128 // State of the account. | 149 // State of the account. |
| 129 AccountInfos account_infos_; | 150 AccountInfos account_infos_; |
| 130 | 151 |
| 131 // Indicates whether shutdown has been called. | 152 // Indicates whether shutdown has been called. |
| 132 bool shutdown_called_; | 153 bool shutdown_called_; |
| 133 | 154 |
| 134 ScopedVector<OAuth2TokenService::Request> pending_token_requests_; | 155 ScopedVector<OAuth2TokenService::Request> pending_token_requests_; |
| 135 | 156 |
| 157 base::WeakPtrFactory<GCMAccountTracker> weak_ptr_factory_; | |
|
Nicolas Zea
2014/11/06 21:43:17
nit: Given that this is specifically for reporting
fgorski
2014/11/07 01:42:12
Done.
| |
| 158 | |
| 136 DISALLOW_COPY_AND_ASSIGN(GCMAccountTracker); | 159 DISALLOW_COPY_AND_ASSIGN(GCMAccountTracker); |
| 137 }; | 160 }; |
| 138 | 161 |
| 139 } // namespace gcm | 162 } // namespace gcm |
| 140 | 163 |
| 141 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | 164 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ |
| OLD | NEW |