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 #include "chrome/browser/services/gcm/gcm_account_tracker.h" | 5 #include "chrome/browser/services/gcm/gcm_account_tracker.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 if (IsTokenReportingRequired()) | 76 if (IsTokenReportingRequired()) |
77 ReportTokens(); | 77 ReportTokens(); |
78 else | 78 else |
79 ScheduleReportTokens(); | 79 ScheduleReportTokens(); |
80 } | 80 } |
81 | 81 |
82 void GCMAccountTracker::ScheduleReportTokens() { | 82 void GCMAccountTracker::ScheduleReportTokens() { |
| 83 // Shortcutting here, in case GCM Driver is not yet connected. In that case |
| 84 // reporting will be scheduled/started when the connection is made. |
| 85 if (!driver_->IsConnected()) |
| 86 return; |
| 87 |
83 DVLOG(1) << "Deferring the token reporting for: " | 88 DVLOG(1) << "Deferring the token reporting for: " |
84 << GetTimeToNextTokenReporting().InSeconds() << " seconds."; | 89 << GetTimeToNextTokenReporting().InSeconds() << " seconds."; |
85 | 90 |
86 reporting_weak_ptr_factory_.InvalidateWeakPtrs(); | 91 reporting_weak_ptr_factory_.InvalidateWeakPtrs(); |
87 base::MessageLoop::current()->PostDelayedTask( | 92 base::MessageLoop::current()->PostDelayedTask( |
88 FROM_HERE, | 93 FROM_HERE, |
89 base::Bind(&GCMAccountTracker::ReportTokens, | 94 base::Bind(&GCMAccountTracker::ReportTokens, |
90 reporting_weak_ptr_factory_.GetWeakPtr()), | 95 reporting_weak_ptr_factory_.GetWeakPtr()), |
91 GetTimeToNextTokenReporting()); | 96 GetTimeToNextTokenReporting()); |
92 } | 97 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 iter->second.access_token.clear(); | 162 iter->second.access_token.clear(); |
158 iter->second.state = ACCOUNT_REMOVED; | 163 iter->second.state = ACCOUNT_REMOVED; |
159 } | 164 } |
160 } | 165 } |
161 | 166 |
162 DeleteTokenRequest(request); | 167 DeleteTokenRequest(request); |
163 ReportTokens(); | 168 ReportTokens(); |
164 } | 169 } |
165 | 170 |
166 void GCMAccountTracker::OnConnected(const net::IPEndPoint& ip_endpoint) { | 171 void GCMAccountTracker::OnConnected(const net::IPEndPoint& ip_endpoint) { |
| 172 // We are sure here, that GCM is running and connected. We can start reporting |
| 173 // tokens if reporting is due now, or schedule reporting for later. |
167 if (IsTokenReportingRequired()) | 174 if (IsTokenReportingRequired()) |
168 ReportTokens(); | 175 ReportTokens(); |
| 176 else |
| 177 ScheduleReportTokens(); |
169 } | 178 } |
170 | 179 |
171 void GCMAccountTracker::OnDisconnected() { | 180 void GCMAccountTracker::OnDisconnected() { |
172 // We are disconnected, so no point in trying to work with tokens. | 181 // We are disconnected, so no point in trying to work with tokens. |
173 } | 182 } |
174 | 183 |
175 void GCMAccountTracker::ReportTokens() { | 184 void GCMAccountTracker::ReportTokens() { |
176 SanitizeTokens(); | 185 SanitizeTokens(); |
177 // Make sure all tokens are valid. | 186 // Make sure all tokens are valid. |
178 if (IsTokenFetchingRequired()) { | 187 if (IsTokenFetchingRequired()) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 } | 278 } |
270 | 279 |
271 return token_needed; | 280 return token_needed; |
272 } | 281 } |
273 | 282 |
274 base::TimeDelta GCMAccountTracker::GetTimeToNextTokenReporting() const { | 283 base::TimeDelta GCMAccountTracker::GetTimeToNextTokenReporting() const { |
275 base::TimeDelta time_till_next_reporting = | 284 base::TimeDelta time_till_next_reporting = |
276 driver_->GetLastTokenFetchTime() + | 285 driver_->GetLastTokenFetchTime() + |
277 base::TimeDelta::FromMilliseconds(kTokenReportingIntervalMs) - | 286 base::TimeDelta::FromMilliseconds(kTokenReportingIntervalMs) - |
278 base::Time::Now(); | 287 base::Time::Now(); |
279 return time_till_next_reporting < base::TimeDelta() ? | 288 |
280 base::TimeDelta() : time_till_next_reporting; | 289 // Case when token fetching is overdue. |
| 290 if (time_till_next_reporting < base::TimeDelta()) |
| 291 return base::TimeDelta(); |
| 292 |
| 293 // Case when calculated period is larger than expected, including the |
| 294 // situation when the method is called before GCM driver is completely |
| 295 // initialized. |
| 296 if (time_till_next_reporting > |
| 297 base::TimeDelta::FromMilliseconds(kTokenReportingIntervalMs)) { |
| 298 return base::TimeDelta::FromMilliseconds(kTokenReportingIntervalMs); |
| 299 } |
| 300 |
| 301 return time_till_next_reporting; |
281 } | 302 } |
282 | 303 |
283 void GCMAccountTracker::DeleteTokenRequest( | 304 void GCMAccountTracker::DeleteTokenRequest( |
284 const OAuth2TokenService::Request* request) { | 305 const OAuth2TokenService::Request* request) { |
285 ScopedVector<OAuth2TokenService::Request>::iterator iter = std::find( | 306 ScopedVector<OAuth2TokenService::Request>::iterator iter = std::find( |
286 pending_token_requests_.begin(), pending_token_requests_.end(), request); | 307 pending_token_requests_.begin(), pending_token_requests_.end(), request); |
287 if (iter != pending_token_requests_.end()) | 308 if (iter != pending_token_requests_.end()) |
288 pending_token_requests_.erase(iter); | 309 pending_token_requests_.erase(iter); |
289 } | 310 } |
290 | 311 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 iter->second.state = ACCOUNT_REMOVED; | 364 iter->second.state = ACCOUNT_REMOVED; |
344 ReportTokens(); | 365 ReportTokens(); |
345 } | 366 } |
346 | 367 |
347 OAuth2TokenService* GCMAccountTracker::GetTokenService() { | 368 OAuth2TokenService* GCMAccountTracker::GetTokenService() { |
348 DCHECK(account_tracker_->identity_provider()); | 369 DCHECK(account_tracker_->identity_provider()); |
349 return account_tracker_->identity_provider()->GetTokenService(); | 370 return account_tracker_->identity_provider()->GetTokenService(); |
350 } | 371 } |
351 | 372 |
352 } // namespace gcm | 373 } // namespace gcm |
OLD | NEW |