[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [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 | |
jitendra.ks | 70c5b4c | 2015-08-19 18:53:34 | [diff] [blame] | 5 | #include "components/gcm_driver/gcm_account_tracker.h" |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 6 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | #include <vector> |
| 11 | |
eroman | c9a6b72 | 2015-06-03 22:19:00 | [diff] [blame] | 12 | #include "base/bind.h" |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 13 | #include "base/location.h" |
| 14 | #include "base/single_thread_task_runner.h" |
| 15 | #include "base/thread_task_runner_handle.h" |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 16 | #include "base/time/time.h" |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 17 | #include "components/gcm_driver/gcm_driver.h" |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 18 | #include "google_apis/gaia/google_service_auth_error.h" |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 19 | #include "net/base/ip_endpoint.h" |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 20 | |
| 21 | namespace gcm { |
| 22 | |
| 23 | namespace { |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 24 | |
| 25 | // Scopes needed by the OAuth2 access tokens. |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 26 | const char kGCMGroupServerScope[] = "https://www.googleapis.com/auth/gcm"; |
[email protected] | 669b162 | 2014-08-08 08:35:50 | [diff] [blame] | 27 | const char kGCMCheckinServerScope[] = |
| 28 | "https://www.googleapis.com/auth/android_checkin"; |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 29 | // Name of the GCM account tracker for the OAuth2TokenService. |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 30 | const char kGCMAccountTrackerName[] = "gcm_account_tracker"; |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 31 | // Minimum token validity when sending to GCM groups server. |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 32 | const int64_t kMinimumTokenValidityMs = 500; |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 33 | // Token reporting interval, when no account changes are detected. |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 34 | const int64_t kTokenReportingIntervalMs = |
| 35 | 12 * 60 * 60 * 1000; // 12 hours in ms. |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 36 | |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 37 | } // namespace |
| 38 | |
| 39 | GCMAccountTracker::AccountInfo::AccountInfo(const std::string& email, |
| 40 | AccountState state) |
| 41 | : email(email), state(state) { |
| 42 | } |
| 43 | |
| 44 | GCMAccountTracker::AccountInfo::~AccountInfo() { |
| 45 | } |
| 46 | |
| 47 | GCMAccountTracker::GCMAccountTracker( |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 48 | std::unique_ptr<gaia::AccountTracker> account_tracker, |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 49 | GCMDriver* driver) |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 50 | : OAuth2TokenService::Consumer(kGCMAccountTrackerName), |
| 51 | account_tracker_(account_tracker.release()), |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 52 | driver_(driver), |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 53 | shutdown_called_(false), |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 54 | reporting_weak_ptr_factory_(this) {} |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 55 | |
| 56 | GCMAccountTracker::~GCMAccountTracker() { |
| 57 | DCHECK(shutdown_called_); |
| 58 | } |
| 59 | |
| 60 | void GCMAccountTracker::Shutdown() { |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 61 | shutdown_called_ = true; |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 62 | driver_->RemoveConnectionObserver(this); |
| 63 | account_tracker_->RemoveObserver(this); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 64 | account_tracker_->Shutdown(); |
| 65 | } |
| 66 | |
| 67 | void GCMAccountTracker::Start() { |
| 68 | DCHECK(!shutdown_called_); |
| 69 | account_tracker_->AddObserver(this); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 70 | driver_->AddConnectionObserver(this); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 71 | |
| 72 | std::vector<gaia::AccountIds> accounts = account_tracker_->GetAccounts(); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 73 | for (std::vector<gaia::AccountIds>::const_iterator iter = accounts.begin(); |
| 74 | iter != accounts.end(); |
| 75 | ++iter) { |
| 76 | if (!iter->email.empty()) { |
| 77 | account_infos_.insert(std::make_pair( |
| 78 | iter->account_key, AccountInfo(iter->email, TOKEN_NEEDED))); |
| 79 | } |
| 80 | } |
| 81 | |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 82 | if (IsTokenReportingRequired()) |
| 83 | ReportTokens(); |
| 84 | else |
| 85 | ScheduleReportTokens(); |
| 86 | } |
| 87 | |
| 88 | void GCMAccountTracker::ScheduleReportTokens() { |
fgorski | 702e92ed | 2015-01-29 19:22:02 | [diff] [blame] | 89 | // Shortcutting here, in case GCM Driver is not yet connected. In that case |
| 90 | // reporting will be scheduled/started when the connection is made. |
| 91 | if (!driver_->IsConnected()) |
| 92 | return; |
| 93 | |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 94 | DVLOG(1) << "Deferring the token reporting for: " |
| 95 | << GetTimeToNextTokenReporting().InSeconds() << " seconds."; |
| 96 | |
| 97 | reporting_weak_ptr_factory_.InvalidateWeakPtrs(); |
skyostil | 0259835 | 2015-06-12 12:37:25 | [diff] [blame] | 98 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 99 | FROM_HERE, base::Bind(&GCMAccountTracker::ReportTokens, |
| 100 | reporting_weak_ptr_factory_.GetWeakPtr()), |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 101 | GetTimeToNextTokenReporting()); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 102 | } |
| 103 | |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 104 | void GCMAccountTracker::OnAccountAdded(const gaia::AccountIds& ids) { |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 105 | DVLOG(1) << "Account added: " << ids.email; |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 106 | // We listen for the account signing in, which happens after account is added. |
| 107 | } |
| 108 | |
| 109 | void GCMAccountTracker::OnAccountRemoved(const gaia::AccountIds& ids) { |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 110 | DVLOG(1) << "Account removed: " << ids.email; |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 111 | // We listen for the account signing out, which happens before account is |
| 112 | // removed. |
| 113 | } |
| 114 | |
| 115 | void GCMAccountTracker::OnAccountSignInChanged(const gaia::AccountIds& ids, |
| 116 | bool is_signed_in) { |
| 117 | if (is_signed_in) |
| 118 | OnAccountSignedIn(ids); |
| 119 | else |
| 120 | OnAccountSignedOut(ids); |
| 121 | } |
| 122 | |
| 123 | void GCMAccountTracker::OnGetTokenSuccess( |
| 124 | const OAuth2TokenService::Request* request, |
| 125 | const std::string& access_token, |
| 126 | const base::Time& expiration_time) { |
| 127 | DCHECK(request); |
| 128 | DCHECK(!request->GetAccountId().empty()); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 129 | DVLOG(1) << "Get token success: " << request->GetAccountId(); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 130 | |
| 131 | AccountInfos::iterator iter = account_infos_.find(request->GetAccountId()); |
| 132 | DCHECK(iter != account_infos_.end()); |
| 133 | if (iter != account_infos_.end()) { |
| 134 | DCHECK(iter->second.state == GETTING_TOKEN || |
| 135 | iter->second.state == ACCOUNT_REMOVED); |
| 136 | // If OnAccountSignedOut(..) was called most recently, account is kept in |
| 137 | // ACCOUNT_REMOVED state. |
| 138 | if (iter->second.state == GETTING_TOKEN) { |
| 139 | iter->second.state = TOKEN_PRESENT; |
| 140 | iter->second.access_token = access_token; |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 141 | iter->second.expiration_time = expiration_time; |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | DeleteTokenRequest(request); |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 146 | ReportTokens(); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void GCMAccountTracker::OnGetTokenFailure( |
| 150 | const OAuth2TokenService::Request* request, |
| 151 | const GoogleServiceAuthError& error) { |
| 152 | DCHECK(request); |
| 153 | DCHECK(!request->GetAccountId().empty()); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 154 | DVLOG(1) << "Get token failure: " << request->GetAccountId(); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 155 | |
| 156 | AccountInfos::iterator iter = account_infos_.find(request->GetAccountId()); |
| 157 | DCHECK(iter != account_infos_.end()); |
| 158 | if (iter != account_infos_.end()) { |
| 159 | DCHECK(iter->second.state == GETTING_TOKEN || |
| 160 | iter->second.state == ACCOUNT_REMOVED); |
| 161 | // If OnAccountSignedOut(..) was called most recently, account is kept in |
| 162 | // ACCOUNT_REMOVED state. |
fgorski | be981e7 | 2015-01-26 21:07:19 | [diff] [blame] | 163 | if (iter->second.state == GETTING_TOKEN) { |
| 164 | // Given the fetcher has a built in retry logic, consider this situation |
| 165 | // to be invalid refresh token, that is only fixed when user signs in. |
| 166 | // Once the users signs in properly the minting will retry. |
| 167 | iter->second.access_token.clear(); |
| 168 | iter->second.state = ACCOUNT_REMOVED; |
| 169 | } |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | DeleteTokenRequest(request); |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 173 | ReportTokens(); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 174 | } |
| 175 | |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 176 | void GCMAccountTracker::OnConnected(const net::IPEndPoint& ip_endpoint) { |
fgorski | 702e92ed | 2015-01-29 19:22:02 | [diff] [blame] | 177 | // We are sure here, that GCM is running and connected. We can start reporting |
| 178 | // tokens if reporting is due now, or schedule reporting for later. |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 179 | if (IsTokenReportingRequired()) |
| 180 | ReportTokens(); |
fgorski | 702e92ed | 2015-01-29 19:22:02 | [diff] [blame] | 181 | else |
| 182 | ScheduleReportTokens(); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void GCMAccountTracker::OnDisconnected() { |
| 186 | // We are disconnected, so no point in trying to work with tokens. |
| 187 | } |
| 188 | |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 189 | void GCMAccountTracker::ReportTokens() { |
| 190 | SanitizeTokens(); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 191 | // Make sure all tokens are valid. |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 192 | if (IsTokenFetchingRequired()) { |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 193 | GetAllNeededTokens(); |
| 194 | return; |
| 195 | } |
| 196 | |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 197 | // Wait for gaia::AccountTracker to be done with fetching the user info, as |
| 198 | // well as all of the pending token requests from GCMAccountTracker to be done |
| 199 | // before you report the results. |
| 200 | if (!account_tracker_->IsAllUserInfoFetched() || |
| 201 | !pending_token_requests_.empty()) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | bool account_removed = false; |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 206 | // Stop tracking the accounts, that were removed, as it will be reported to |
| 207 | // the driver. |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 208 | for (AccountInfos::iterator iter = account_infos_.begin(); |
| 209 | iter != account_infos_.end();) { |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 210 | if (iter->second.state == ACCOUNT_REMOVED) { |
| 211 | account_removed = true; |
| 212 | account_infos_.erase(iter++); |
| 213 | } else { |
| 214 | ++iter; |
| 215 | } |
| 216 | } |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 217 | |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 218 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 219 | for (AccountInfos::iterator iter = account_infos_.begin(); |
| 220 | iter != account_infos_.end(); ++iter) { |
| 221 | if (iter->second.state == TOKEN_PRESENT) { |
| 222 | GCMClient::AccountTokenInfo token_info; |
| 223 | token_info.account_id = iter->first; |
| 224 | token_info.email = iter->second.email; |
| 225 | token_info.access_token = iter->second.access_token; |
| 226 | account_tokens.push_back(token_info); |
| 227 | } else { |
| 228 | // This should not happen, as we are making a check that there are no |
| 229 | // pending requests above, stopping tracking of removed accounts, or start |
| 230 | // fetching tokens. |
| 231 | NOTREACHED(); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 235 | // Make sure that there is something to report, otherwise bail out. |
| 236 | if (!account_tokens.empty() || account_removed) { |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 237 | DVLOG(1) << "Reporting the tokens to driver: " << account_tokens.size(); |
| 238 | driver_->SetAccountTokens(account_tokens); |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 239 | driver_->SetLastTokenFetchTime(base::Time::Now()); |
| 240 | ScheduleReportTokens(); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 241 | } else { |
| 242 | DVLOG(1) << "No tokens and nothing removed. Skipping callback."; |
| 243 | } |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 244 | } |
| 245 | |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 246 | void GCMAccountTracker::SanitizeTokens() { |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 247 | for (AccountInfos::iterator iter = account_infos_.begin(); |
| 248 | iter != account_infos_.end(); |
| 249 | ++iter) { |
| 250 | if (iter->second.state == TOKEN_PRESENT && |
| 251 | iter->second.expiration_time < |
| 252 | base::Time::Now() + |
| 253 | base::TimeDelta::FromMilliseconds(kMinimumTokenValidityMs)) { |
| 254 | iter->second.access_token.clear(); |
| 255 | iter->second.state = TOKEN_NEEDED; |
| 256 | iter->second.expiration_time = base::Time(); |
| 257 | } |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 258 | } |
| 259 | } |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 260 | |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 261 | bool GCMAccountTracker::IsTokenReportingRequired() const { |
| 262 | if (GetTimeToNextTokenReporting() == base::TimeDelta()) |
| 263 | return true; |
| 264 | |
| 265 | bool reporting_required = false; |
| 266 | for (AccountInfos::const_iterator iter = account_infos_.begin(); |
| 267 | iter != account_infos_.end(); |
| 268 | ++iter) { |
| 269 | if (iter->second.state == ACCOUNT_REMOVED) |
| 270 | reporting_required = true; |
fgorski | ede4117 | 2014-11-08 00:38:35 | [diff] [blame] | 271 | } |
| 272 | |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 273 | return reporting_required; |
| 274 | } |
| 275 | |
| 276 | bool GCMAccountTracker::IsTokenFetchingRequired() const { |
| 277 | bool token_needed = false; |
| 278 | for (AccountInfos::const_iterator iter = account_infos_.begin(); |
| 279 | iter != account_infos_.end(); |
| 280 | ++iter) { |
| 281 | if (iter->second.state == TOKEN_NEEDED) |
| 282 | token_needed = true; |
| 283 | } |
| 284 | |
| 285 | return token_needed; |
| 286 | } |
| 287 | |
| 288 | base::TimeDelta GCMAccountTracker::GetTimeToNextTokenReporting() const { |
| 289 | base::TimeDelta time_till_next_reporting = |
| 290 | driver_->GetLastTokenFetchTime() + |
| 291 | base::TimeDelta::FromMilliseconds(kTokenReportingIntervalMs) - |
| 292 | base::Time::Now(); |
fgorski | 702e92ed | 2015-01-29 19:22:02 | [diff] [blame] | 293 | |
| 294 | // Case when token fetching is overdue. |
| 295 | if (time_till_next_reporting < base::TimeDelta()) |
| 296 | return base::TimeDelta(); |
| 297 | |
| 298 | // Case when calculated period is larger than expected, including the |
| 299 | // situation when the method is called before GCM driver is completely |
| 300 | // initialized. |
| 301 | if (time_till_next_reporting > |
| 302 | base::TimeDelta::FromMilliseconds(kTokenReportingIntervalMs)) { |
| 303 | return base::TimeDelta::FromMilliseconds(kTokenReportingIntervalMs); |
| 304 | } |
| 305 | |
| 306 | return time_till_next_reporting; |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 309 | void GCMAccountTracker::DeleteTokenRequest( |
| 310 | const OAuth2TokenService::Request* request) { |
| 311 | ScopedVector<OAuth2TokenService::Request>::iterator iter = std::find( |
| 312 | pending_token_requests_.begin(), pending_token_requests_.end(), request); |
| 313 | if (iter != pending_token_requests_.end()) |
| 314 | pending_token_requests_.erase(iter); |
| 315 | } |
| 316 | |
| 317 | void GCMAccountTracker::GetAllNeededTokens() { |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 318 | // Only start fetching tokens if driver is running, they have a limited |
| 319 | // validity time and GCM connection is a good indication of network running. |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 320 | // If the GetAllNeededTokens was called as part of periodic schedule, it may |
| 321 | // not have network. In that case the next network change will trigger token |
| 322 | // fetching. |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 323 | if (!driver_->IsConnected()) |
| 324 | return; |
| 325 | |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 326 | for (AccountInfos::iterator iter = account_infos_.begin(); |
| 327 | iter != account_infos_.end(); |
| 328 | ++iter) { |
| 329 | if (iter->second.state == TOKEN_NEEDED) |
| 330 | GetToken(iter); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | void GCMAccountTracker::GetToken(AccountInfos::iterator& account_iter) { |
| 335 | DCHECK(GetTokenService()); |
| 336 | DCHECK_EQ(account_iter->second.state, TOKEN_NEEDED); |
| 337 | |
| 338 | OAuth2TokenService::ScopeSet scopes; |
| 339 | scopes.insert(kGCMGroupServerScope); |
[email protected] | 669b162 | 2014-08-08 08:35:50 | [diff] [blame] | 340 | scopes.insert(kGCMCheckinServerScope); |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 341 | std::unique_ptr<OAuth2TokenService::Request> request = |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 342 | GetTokenService()->StartRequest(account_iter->first, scopes, this); |
| 343 | |
| 344 | pending_token_requests_.push_back(request.release()); |
| 345 | account_iter->second.state = GETTING_TOKEN; |
| 346 | } |
| 347 | |
| 348 | void GCMAccountTracker::OnAccountSignedIn(const gaia::AccountIds& ids) { |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 349 | DVLOG(1) << "Account signed in: " << ids.email; |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 350 | AccountInfos::iterator iter = account_infos_.find(ids.account_key); |
| 351 | if (iter == account_infos_.end()) { |
| 352 | DCHECK(!ids.email.empty()); |
| 353 | account_infos_.insert( |
| 354 | std::make_pair(ids.account_key, AccountInfo(ids.email, TOKEN_NEEDED))); |
| 355 | } else if (iter->second.state == ACCOUNT_REMOVED) { |
| 356 | iter->second.state = TOKEN_NEEDED; |
| 357 | } |
| 358 | |
| 359 | GetAllNeededTokens(); |
| 360 | } |
| 361 | |
| 362 | void GCMAccountTracker::OnAccountSignedOut(const gaia::AccountIds& ids) { |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 363 | DVLOG(1) << "Account signed out: " << ids.email; |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 364 | AccountInfos::iterator iter = account_infos_.find(ids.account_key); |
| 365 | if (iter == account_infos_.end()) |
| 366 | return; |
| 367 | |
| 368 | iter->second.access_token.clear(); |
| 369 | iter->second.state = ACCOUNT_REMOVED; |
fgorski | 9a40510 | 2014-11-19 01:25:16 | [diff] [blame] | 370 | ReportTokens(); |
[email protected] | 641c8cc7 | 2014-07-02 00:33:06 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | OAuth2TokenService* GCMAccountTracker::GetTokenService() { |
| 374 | DCHECK(account_tracker_->identity_provider()); |
| 375 | return account_tracker_->identity_provider()->GetTokenService(); |
| 376 | } |
| 377 | |
| 378 | } // namespace gcm |