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" |
11 #include "components/gcm_driver/gcm_driver.h" | 11 #include "components/gcm_driver/gcm_driver.h" |
12 #include "google_apis/gaia/google_service_auth_error.h" | 12 #include "google_apis/gaia/google_service_auth_error.h" |
13 #include "net/base/ip_endpoint.h" | 13 #include "net/base/ip_endpoint.h" |
14 | 14 |
15 namespace gcm { | 15 namespace gcm { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | |
19 // Scopes needed by the OAuth2 access tokens. | |
18 const char kGCMGroupServerScope[] = "https://www.googleapis.com/auth/gcm"; | 20 const char kGCMGroupServerScope[] = "https://www.googleapis.com/auth/gcm"; |
19 const char kGCMCheckinServerScope[] = | 21 const char kGCMCheckinServerScope[] = |
20 "https://www.googleapis.com/auth/android_checkin"; | 22 "https://www.googleapis.com/auth/android_checkin"; |
23 // Name of the GCM account tracker for the OAuth2TokenService. | |
21 const char kGCMAccountTrackerName[] = "gcm_account_tracker"; | 24 const char kGCMAccountTrackerName[] = "gcm_account_tracker"; |
25 // Minimum token validity when sending to GCM groups server. | |
22 const int64 kMinimumTokenValidityMs = 500; | 26 const int64 kMinimumTokenValidityMs = 500; |
27 // Token fetching interval, when no account changes are detected. | |
28 const int64 kTokenFetchingIntervalMs = 12 * 60 * 60 * 1000; // 12 hours in ms. | |
29 | |
23 } // namespace | 30 } // namespace |
24 | 31 |
25 GCMAccountTracker::AccountInfo::AccountInfo(const std::string& email, | 32 GCMAccountTracker::AccountInfo::AccountInfo(const std::string& email, |
26 AccountState state) | 33 AccountState state) |
27 : email(email), state(state) { | 34 : email(email), state(state) { |
28 } | 35 } |
29 | 36 |
30 GCMAccountTracker::AccountInfo::~AccountInfo() { | 37 GCMAccountTracker::AccountInfo::~AccountInfo() { |
31 } | 38 } |
32 | 39 |
33 GCMAccountTracker::GCMAccountTracker( | 40 GCMAccountTracker::GCMAccountTracker( |
34 scoped_ptr<gaia::AccountTracker> account_tracker, | 41 scoped_ptr<gaia::AccountTracker> account_tracker, |
35 GCMDriver* driver) | 42 GCMDriver* driver) |
36 : OAuth2TokenService::Consumer(kGCMAccountTrackerName), | 43 : OAuth2TokenService::Consumer(kGCMAccountTrackerName), |
37 account_tracker_(account_tracker.release()), | 44 account_tracker_(account_tracker.release()), |
38 driver_(driver), | 45 driver_(driver), |
39 shutdown_called_(false) { | 46 shutdown_called_(false), |
47 reporting_weak_ptr_factory_(this) { | |
40 } | 48 } |
41 | 49 |
42 GCMAccountTracker::~GCMAccountTracker() { | 50 GCMAccountTracker::~GCMAccountTracker() { |
43 DCHECK(shutdown_called_); | 51 DCHECK(shutdown_called_); |
44 } | 52 } |
45 | 53 |
46 void GCMAccountTracker::Shutdown() { | 54 void GCMAccountTracker::Shutdown() { |
47 shutdown_called_ = true; | 55 shutdown_called_ = true; |
48 driver_->RemoveConnectionObserver(this); | 56 driver_->RemoveConnectionObserver(this); |
49 account_tracker_->RemoveObserver(this); | 57 account_tracker_->RemoveObserver(this); |
50 account_tracker_->Shutdown(); | 58 account_tracker_->Shutdown(); |
51 } | 59 } |
52 | 60 |
53 void GCMAccountTracker::Start() { | 61 void GCMAccountTracker::Start() { |
54 DCHECK(!shutdown_called_); | 62 DCHECK(!shutdown_called_); |
55 account_tracker_->AddObserver(this); | 63 account_tracker_->AddObserver(this); |
56 driver_->AddConnectionObserver(this); | 64 driver_->AddConnectionObserver(this); |
57 | 65 |
58 std::vector<gaia::AccountIds> accounts = account_tracker_->GetAccounts(); | 66 std::vector<gaia::AccountIds> accounts = account_tracker_->GetAccounts(); |
59 if (accounts.empty()) { | |
60 CompleteCollectingTokens(); | |
61 return; | |
62 } | |
63 | |
64 for (std::vector<gaia::AccountIds>::const_iterator iter = accounts.begin(); | 67 for (std::vector<gaia::AccountIds>::const_iterator iter = accounts.begin(); |
65 iter != accounts.end(); | 68 iter != accounts.end(); |
66 ++iter) { | 69 ++iter) { |
67 if (!iter->email.empty()) { | 70 if (!iter->email.empty()) { |
68 account_infos_.insert(std::make_pair( | 71 account_infos_.insert(std::make_pair( |
69 iter->account_key, AccountInfo(iter->email, TOKEN_NEEDED))); | 72 iter->account_key, AccountInfo(iter->email, TOKEN_NEEDED))); |
70 } | 73 } |
71 } | 74 } |
72 | 75 |
73 GetAllNeededTokens(); | 76 if (IsTokenReportingRequired()) |
77 ReportTokens(); | |
78 else | |
79 ScheduleReportTokens(); | |
80 } | |
81 | |
82 void GCMAccountTracker::ScheduleReportTokens() { | |
83 DVLOG(1) << "Deferring the token reporting for: " | |
84 << GetTimeToNextTokenReporting().InSeconds() << " seconds."; | |
85 | |
86 reporting_weak_ptr_factory_.InvalidateWeakPtrs(); | |
87 base::MessageLoop::current()->PostDelayedTask( | |
88 FROM_HERE, | |
89 base::Bind(&GCMAccountTracker::ReportTokens, | |
90 reporting_weak_ptr_factory_.GetWeakPtr()), | |
91 GetTimeToNextTokenReporting()); | |
74 } | 92 } |
75 | 93 |
76 void GCMAccountTracker::OnAccountAdded(const gaia::AccountIds& ids) { | 94 void GCMAccountTracker::OnAccountAdded(const gaia::AccountIds& ids) { |
77 DVLOG(1) << "Account added: " << ids.email; | 95 DVLOG(1) << "Account added: " << ids.email; |
78 // We listen for the account signing in, which happens after account is added. | 96 // We listen for the account signing in, which happens after account is added. |
79 } | 97 } |
80 | 98 |
81 void GCMAccountTracker::OnAccountRemoved(const gaia::AccountIds& ids) { | 99 void GCMAccountTracker::OnAccountRemoved(const gaia::AccountIds& ids) { |
82 DVLOG(1) << "Account removed: " << ids.email; | 100 DVLOG(1) << "Account removed: " << ids.email; |
83 // We listen for the account signing out, which happens before account is | 101 // We listen for the account signing out, which happens before account is |
(...skipping 24 matching lines...) Expand all Loading... | |
108 // If OnAccountSignedOut(..) was called most recently, account is kept in | 126 // If OnAccountSignedOut(..) was called most recently, account is kept in |
109 // ACCOUNT_REMOVED state. | 127 // ACCOUNT_REMOVED state. |
110 if (iter->second.state == GETTING_TOKEN) { | 128 if (iter->second.state == GETTING_TOKEN) { |
111 iter->second.state = TOKEN_PRESENT; | 129 iter->second.state = TOKEN_PRESENT; |
112 iter->second.access_token = access_token; | 130 iter->second.access_token = access_token; |
113 iter->second.expiration_time = expiration_time; | 131 iter->second.expiration_time = expiration_time; |
114 } | 132 } |
115 } | 133 } |
116 | 134 |
117 DeleteTokenRequest(request); | 135 DeleteTokenRequest(request); |
118 CompleteCollectingTokens(); | 136 ReportTokens(); |
119 } | 137 } |
120 | 138 |
121 void GCMAccountTracker::OnGetTokenFailure( | 139 void GCMAccountTracker::OnGetTokenFailure( |
122 const OAuth2TokenService::Request* request, | 140 const OAuth2TokenService::Request* request, |
123 const GoogleServiceAuthError& error) { | 141 const GoogleServiceAuthError& error) { |
124 DCHECK(request); | 142 DCHECK(request); |
125 DCHECK(!request->GetAccountId().empty()); | 143 DCHECK(!request->GetAccountId().empty()); |
126 DVLOG(1) << "Get token failure: " << request->GetAccountId(); | 144 DVLOG(1) << "Get token failure: " << request->GetAccountId(); |
127 | 145 |
128 AccountInfos::iterator iter = account_infos_.find(request->GetAccountId()); | 146 AccountInfos::iterator iter = account_infos_.find(request->GetAccountId()); |
129 DCHECK(iter != account_infos_.end()); | 147 DCHECK(iter != account_infos_.end()); |
130 if (iter != account_infos_.end()) { | 148 if (iter != account_infos_.end()) { |
131 DCHECK(iter->second.state == GETTING_TOKEN || | 149 DCHECK(iter->second.state == GETTING_TOKEN || |
132 iter->second.state == ACCOUNT_REMOVED); | 150 iter->second.state == ACCOUNT_REMOVED); |
133 // If OnAccountSignedOut(..) was called most recently, account is kept in | 151 // If OnAccountSignedOut(..) was called most recently, account is kept in |
134 // ACCOUNT_REMOVED state. | 152 // ACCOUNT_REMOVED state. |
135 if (iter->second.state == GETTING_TOKEN) | 153 if (iter->second.state == GETTING_TOKEN) |
136 iter->second.state = TOKEN_NEEDED; | 154 iter->second.state = TOKEN_NEEDED; |
137 } | 155 } |
138 | 156 |
139 DeleteTokenRequest(request); | 157 DeleteTokenRequest(request); |
140 CompleteCollectingTokens(); | 158 ReportTokens(); |
141 } | 159 } |
142 | 160 |
143 void GCMAccountTracker::OnConnected(const net::IPEndPoint& ip_endpoint) { | 161 void GCMAccountTracker::OnConnected(const net::IPEndPoint& ip_endpoint) { |
144 if (SanitizeTokens()) | 162 if (IsTokenReportingRequired()) |
145 GetAllNeededTokens(); | 163 ReportTokens(); |
146 } | 164 } |
147 | 165 |
148 void GCMAccountTracker::OnDisconnected() { | 166 void GCMAccountTracker::OnDisconnected() { |
149 // We are disconnected, so no point in trying to work with tokens. | 167 // We are disconnected, so no point in trying to work with tokens. |
150 } | 168 } |
151 | 169 |
152 void GCMAccountTracker::CompleteCollectingTokens() { | 170 void GCMAccountTracker::ReportTokens() { |
171 SanitizeTokens(); | |
153 // Make sure all tokens are valid. | 172 // Make sure all tokens are valid. |
154 if (SanitizeTokens()) { | 173 if (IsTokenFetchingRequired()) { |
155 GetAllNeededTokens(); | 174 GetAllNeededTokens(); |
156 return; | 175 return; |
157 } | 176 } |
158 | 177 |
159 // Wait for gaia::AccountTracker to be done with fetching the user info, as | 178 // Wait for gaia::AccountTracker to be done with fetching the user info, as |
160 // well as all of the pending token requests from GCMAccountTracker to be done | 179 // well as all of the pending token requests from GCMAccountTracker to be done |
161 // before you report the results. | 180 // before you report the results. |
162 if (!account_tracker_->IsAllUserInfoFetched() || | 181 if (!account_tracker_->IsAllUserInfoFetched() || |
163 !pending_token_requests_.empty()) { | 182 !pending_token_requests_.empty()) { |
164 return; | 183 return; |
(...skipping 26 matching lines...) Expand all Loading... | |
191 // pending requests above, stopping tracking of removed accounts, or start | 210 // pending requests above, stopping tracking of removed accounts, or start |
192 // fetching tokens. | 211 // fetching tokens. |
193 NOTREACHED(); | 212 NOTREACHED(); |
194 } | 213 } |
195 } | 214 } |
196 | 215 |
197 // Make sure that there is something to report, otherwise bail out. | 216 // Make sure that there is something to report, otherwise bail out. |
198 if (!account_tokens.empty() || account_removed) { | 217 if (!account_tokens.empty() || account_removed) { |
199 DVLOG(1) << "Reporting the tokens to driver: " << account_tokens.size(); | 218 DVLOG(1) << "Reporting the tokens to driver: " << account_tokens.size(); |
200 driver_->SetAccountTokens(account_tokens); | 219 driver_->SetAccountTokens(account_tokens); |
220 driver_->SetLastTokenFetchTime(base::Time::Now()); | |
221 ScheduleReportTokens(); | |
201 } else { | 222 } else { |
202 DVLOG(1) << "No tokens and nothing removed. Skipping callback."; | 223 DVLOG(1) << "No tokens and nothing removed. Skipping callback."; |
203 } | 224 } |
204 } | 225 } |
205 | 226 |
206 bool GCMAccountTracker::SanitizeTokens() { | 227 void GCMAccountTracker::SanitizeTokens() { |
207 bool tokens_needed = false; | |
208 for (AccountInfos::iterator iter = account_infos_.begin(); | 228 for (AccountInfos::iterator iter = account_infos_.begin(); |
209 iter != account_infos_.end(); | 229 iter != account_infos_.end(); |
210 ++iter) { | 230 ++iter) { |
211 if (iter->second.state == TOKEN_PRESENT && | 231 if (iter->second.state == TOKEN_PRESENT && |
212 iter->second.expiration_time < | 232 iter->second.expiration_time < |
213 base::Time::Now() + | 233 base::Time::Now() + |
214 base::TimeDelta::FromMilliseconds(kMinimumTokenValidityMs)) { | 234 base::TimeDelta::FromMilliseconds(kMinimumTokenValidityMs)) { |
215 iter->second.access_token.clear(); | 235 iter->second.access_token.clear(); |
216 iter->second.state = TOKEN_NEEDED; | 236 iter->second.state = TOKEN_NEEDED; |
217 iter->second.expiration_time = base::Time(); | 237 iter->second.expiration_time = base::Time(); |
218 } | 238 } |
239 } | |
240 } | |
219 | 241 |
220 if (iter->second.state == TOKEN_NEEDED) | 242 bool GCMAccountTracker::IsTokenReportingRequired() const { |
221 tokens_needed = true; | 243 if (GetTimeToNextTokenReporting() == base::TimeDelta()) |
244 return true; | |
245 | |
246 bool reporting_required = false; | |
247 for (AccountInfos::const_iterator iter = account_infos_.begin(); | |
248 iter != account_infos_.end(); | |
249 ++iter) { | |
250 if (iter->second.state == ACCOUNT_REMOVED) | |
251 reporting_required = true; | |
222 } | 252 } |
223 | 253 |
224 return tokens_needed; | 254 return reporting_required; |
255 } | |
256 | |
257 bool GCMAccountTracker::IsTokenFetchingRequired() const { | |
258 bool token_needed = false; | |
259 for (AccountInfos::const_iterator iter = account_infos_.begin(); | |
260 iter != account_infos_.end(); | |
261 ++iter) { | |
262 if (iter->second.state == TOKEN_NEEDED) | |
263 token_needed = true; | |
264 } | |
265 | |
266 return token_needed; | |
267 } | |
268 | |
269 base::TimeDelta GCMAccountTracker::GetTimeToNextTokenReporting() const { | |
270 base::TimeDelta time_till_next_fetching = | |
Nicolas Zea
2014/11/07 23:03:23
I'm still confused that these variables (and the m
fgorski
2014/11/07 23:29:07
Done.
| |
271 driver_->GetLastTokenFetchTime() + | |
272 base::TimeDelta::FromMilliseconds(kTokenFetchingIntervalMs) - | |
273 base::Time::Now(); | |
274 return time_till_next_fetching < base::TimeDelta() ? base::TimeDelta() | |
275 : time_till_next_fetching; | |
225 } | 276 } |
226 | 277 |
227 void GCMAccountTracker::DeleteTokenRequest( | 278 void GCMAccountTracker::DeleteTokenRequest( |
228 const OAuth2TokenService::Request* request) { | 279 const OAuth2TokenService::Request* request) { |
229 ScopedVector<OAuth2TokenService::Request>::iterator iter = std::find( | 280 ScopedVector<OAuth2TokenService::Request>::iterator iter = std::find( |
230 pending_token_requests_.begin(), pending_token_requests_.end(), request); | 281 pending_token_requests_.begin(), pending_token_requests_.end(), request); |
231 if (iter != pending_token_requests_.end()) | 282 if (iter != pending_token_requests_.end()) |
232 pending_token_requests_.erase(iter); | 283 pending_token_requests_.erase(iter); |
233 } | 284 } |
234 | 285 |
235 void GCMAccountTracker::GetAllNeededTokens() { | 286 void GCMAccountTracker::GetAllNeededTokens() { |
236 // Only start fetching tokens if driver is running, they have a limited | 287 // Only start fetching tokens if driver is running, they have a limited |
237 // validity time and GCM connection is a good indication of network running. | 288 // validity time and GCM connection is a good indication of network running. |
289 // If the GetAllNeededTokens was called as part of periodic schedule, it may | |
290 // not have network. In that case the next network change will trigger token | |
291 // fetching. | |
238 if (!driver_->IsConnected()) | 292 if (!driver_->IsConnected()) |
239 return; | 293 return; |
240 | 294 |
241 for (AccountInfos::iterator iter = account_infos_.begin(); | 295 for (AccountInfos::iterator iter = account_infos_.begin(); |
242 iter != account_infos_.end(); | 296 iter != account_infos_.end(); |
243 ++iter) { | 297 ++iter) { |
244 if (iter->second.state == TOKEN_NEEDED) | 298 if (iter->second.state == TOKEN_NEEDED) |
245 GetToken(iter); | 299 GetToken(iter); |
246 } | 300 } |
247 } | 301 } |
(...skipping 27 matching lines...) Expand all Loading... | |
275 } | 329 } |
276 | 330 |
277 void GCMAccountTracker::OnAccountSignedOut(const gaia::AccountIds& ids) { | 331 void GCMAccountTracker::OnAccountSignedOut(const gaia::AccountIds& ids) { |
278 DVLOG(1) << "Account signed out: " << ids.email; | 332 DVLOG(1) << "Account signed out: " << ids.email; |
279 AccountInfos::iterator iter = account_infos_.find(ids.account_key); | 333 AccountInfos::iterator iter = account_infos_.find(ids.account_key); |
280 if (iter == account_infos_.end()) | 334 if (iter == account_infos_.end()) |
281 return; | 335 return; |
282 | 336 |
283 iter->second.access_token.clear(); | 337 iter->second.access_token.clear(); |
284 iter->second.state = ACCOUNT_REMOVED; | 338 iter->second.state = ACCOUNT_REMOVED; |
285 CompleteCollectingTokens(); | 339 ReportTokens(); |
286 } | 340 } |
287 | 341 |
288 OAuth2TokenService* GCMAccountTracker::GetTokenService() { | 342 OAuth2TokenService* GCMAccountTracker::GetTokenService() { |
289 DCHECK(account_tracker_->identity_provider()); | 343 DCHECK(account_tracker_->identity_provider()); |
290 return account_tracker_->identity_provider()->GetTokenService(); | 344 return account_tracker_->identity_provider()->GetTokenService(); |
291 } | 345 } |
292 | 346 |
293 } // namespace gcm | 347 } // namespace gcm |
OLD | NEW |