OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/invalidation/ticl_invalidation_service.h" | 5 #include "chrome/browser/invalidation/ticl_invalidation_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
10 #include "chrome/browser/invalidation/invalidation_service_util.h" | 10 #include "chrome/browser/invalidation/invalidation_service_util.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 -1, | 49 -1, |
50 | 50 |
51 // Don't use initial delay unless the last request was an error. | 51 // Don't use initial delay unless the last request was an error. |
52 false, | 52 false, |
53 }; | 53 }; |
54 | 54 |
55 namespace invalidation { | 55 namespace invalidation { |
56 | 56 |
57 TiclInvalidationService::TiclInvalidationService( | 57 TiclInvalidationService::TiclInvalidationService( |
58 SigninManagerBase* signin, | 58 SigninManagerBase* signin, |
59 TokenService* token_service, | 59 ProfileOAuth2TokenService* oauth2_token_service, |
60 OAuth2TokenService* oauth2_token_service, | |
61 Profile* profile) | 60 Profile* profile) |
62 : profile_(profile), | 61 : profile_(profile), |
63 signin_manager_(signin), | 62 signin_manager_(signin), |
64 token_service_(token_service), | |
65 oauth2_token_service_(oauth2_token_service), | 63 oauth2_token_service_(oauth2_token_service), |
66 invalidator_registrar_(new syncer::InvalidatorRegistrar()), | 64 invalidator_registrar_(new syncer::InvalidatorRegistrar()), |
67 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy) { | 65 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy) { |
68 } | 66 } |
69 | 67 |
70 TiclInvalidationService::~TiclInvalidationService() { | 68 TiclInvalidationService::~TiclInvalidationService() { |
71 DCHECK(CalledOnValidThread()); | 69 DCHECK(CalledOnValidThread()); |
70 oauth2_token_service_->RemoveObserver(this); | |
72 } | 71 } |
73 | 72 |
74 void TiclInvalidationService::Init() { | 73 void TiclInvalidationService::Init() { |
75 DCHECK(CalledOnValidThread()); | 74 DCHECK(CalledOnValidThread()); |
76 | 75 |
77 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | 76 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); |
78 if (invalidator_storage_->GetInvalidatorClientId().empty()) { | 77 if (invalidator_storage_->GetInvalidatorClientId().empty()) { |
79 // This also clears any existing state. We can't reuse old invalidator | 78 // This also clears any existing state. We can't reuse old invalidator |
80 // state with the new ID anyway. | 79 // state with the new ID anyway. |
81 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | 80 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); |
82 } | 81 } |
83 | 82 |
84 if (IsReadyToStart()) { | 83 if (IsReadyToStart()) { |
85 StartInvalidator(); | 84 StartInvalidator(); |
86 } | 85 } |
87 | 86 |
88 notification_registrar_.Add(this, | 87 notification_registrar_.Add(this, |
89 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | 88 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, |
90 content::Source<Profile>(profile_)); | 89 content::Source<Profile>(profile_)); |
91 notification_registrar_.Add(this, | 90 oauth2_token_service_->AddObserver(this); |
92 chrome::NOTIFICATION_TOKEN_AVAILABLE, | |
93 content::Source<TokenService>(token_service_)); | |
94 notification_registrar_.Add(this, | |
95 chrome::NOTIFICATION_TOKENS_CLEARED, | |
96 content::Source<TokenService>(token_service_)); | |
97 } | 91 } |
98 | 92 |
99 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { | 93 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { |
100 // Here we perform the equivalent of Init() and StartInvalidator(), but with | 94 // Here we perform the equivalent of Init() and StartInvalidator(), but with |
101 // some minor changes to account for the fact that we're injecting the | 95 // some minor changes to account for the fact that we're injecting the |
102 // invalidator. | 96 // invalidator. |
103 invalidator_.reset(invalidator); | 97 invalidator_.reset(invalidator); |
104 | 98 |
105 invalidator_->RegisterHandler(this); | 99 invalidator_->RegisterHandler(this); |
106 invalidator_->UpdateRegisteredIds( | 100 invalidator_->UpdateRegisteredIds( |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 DCHECK(CalledOnValidThread()); | 159 DCHECK(CalledOnValidThread()); |
166 return invalidator_storage_->GetInvalidatorClientId(); | 160 return invalidator_storage_->GetInvalidatorClientId(); |
167 } | 161 } |
168 | 162 |
169 void TiclInvalidationService::Observe( | 163 void TiclInvalidationService::Observe( |
170 int type, | 164 int type, |
171 const content::NotificationSource& source, | 165 const content::NotificationSource& source, |
172 const content::NotificationDetails& details) { | 166 const content::NotificationDetails& details) { |
173 DCHECK(CalledOnValidThread()); | 167 DCHECK(CalledOnValidThread()); |
174 | 168 |
175 switch (type) { | 169 if (type == chrome::NOTIFICATION_GOOGLE_SIGNED_OUT) { |
176 case chrome::NOTIFICATION_TOKEN_AVAILABLE: { | 170 Logout(); |
177 if (!IsStarted() && IsReadyToStart()) { | 171 return; |
178 StartInvalidator(); | |
179 } | |
180 break; | |
181 } | |
182 case chrome::NOTIFICATION_TOKENS_CLEARED: { | |
183 access_token_.clear(); | |
184 if (IsStarted()) { | |
185 UpdateInvalidatorCredentials(); | |
186 } | |
187 break; | |
188 } | |
189 case chrome::NOTIFICATION_GOOGLE_SIGNED_OUT: { | |
190 Logout(); | |
191 break; | |
192 } | |
193 default: { | |
194 NOTREACHED(); | |
195 } | |
196 } | 172 } |
173 | |
174 NOTREACHED(); | |
Roger Tawa OOO till Jul 10th
2013/08/26 21:04:53
Might be cleaner now to add a DCHECK for type at l
fgorski
2013/08/26 23:30:54
Done.
| |
197 } | 175 } |
198 | 176 |
199 void TiclInvalidationService::RequestAccessToken() { | 177 void TiclInvalidationService::RequestAccessToken() { |
200 // Only one active request at a time. | 178 // Only one active request at a time. |
201 if (access_token_request_ != NULL) | 179 if (access_token_request_ != NULL) |
202 return; | 180 return; |
203 request_access_token_retry_timer_.Stop(); | 181 request_access_token_retry_timer_.Stop(); |
204 OAuth2TokenService::ScopeSet oauth2_scopes; | 182 OAuth2TokenService::ScopeSet oauth2_scopes; |
205 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++) | 183 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++) |
206 oauth2_scopes.insert(kOAuth2Scopes[i]); | 184 oauth2_scopes.insert(kOAuth2Scopes[i]); |
207 // Invalidate previous token, otherwise token service will return the same | 185 // Invalidate previous token, otherwise token service will return the same |
208 // token again. | 186 // token again. |
209 oauth2_token_service_->InvalidateToken(oauth2_scopes, access_token_); | 187 oauth2_token_service_->InvalidateToken(oauth2_scopes, access_token_); |
210 access_token_.clear(); | 188 access_token_.clear(); |
211 access_token_request_ = | 189 access_token_request_ = oauth2_token_service_->StartRequest( |
212 oauth2_token_service_->StartRequest(oauth2_scopes, this); | 190 oauth2_token_service_->GetPrimaryAccountId(), oauth2_scopes, this); |
213 } | 191 } |
214 | 192 |
215 void TiclInvalidationService::OnGetTokenSuccess( | 193 void TiclInvalidationService::OnGetTokenSuccess( |
216 const OAuth2TokenService::Request* request, | 194 const OAuth2TokenService::Request* request, |
217 const std::string& access_token, | 195 const std::string& access_token, |
218 const base::Time& expiration_time) { | 196 const base::Time& expiration_time) { |
219 DCHECK_EQ(access_token_request_, request); | 197 DCHECK_EQ(access_token_request_, request); |
220 access_token_request_.reset(); | 198 access_token_request_.reset(); |
221 // Reset backoff time after successful response. | 199 // Reset backoff time after successful response. |
222 request_access_token_backoff_.Reset(); | 200 request_access_token_backoff_.Reset(); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
268 invalidator_registrar_->UpdateInvalidatorState( | 246 invalidator_registrar_->UpdateInvalidatorState( |
269 syncer::INVALIDATION_CREDENTIALS_REJECTED); | 247 syncer::INVALIDATION_CREDENTIALS_REJECTED); |
270 break; | 248 break; |
271 } | 249 } |
272 default: { | 250 default: { |
273 // We have no way to notify the user of this. Do nothing. | 251 // We have no way to notify the user of this. Do nothing. |
274 } | 252 } |
275 } | 253 } |
276 } | 254 } |
277 | 255 |
256 void TiclInvalidationService::OnRefreshTokenAvailable( | |
257 const std::string& account_id) { | |
258 if (oauth2_token_service_->GetPrimaryAccountId() == account_id) { | |
259 if (!IsStarted() && IsReadyToStart()) { | |
260 StartInvalidator(); | |
261 } | |
262 } | |
263 } | |
264 | |
265 void TiclInvalidationService::OnRefreshTokenRevoked( | |
266 const std::string& account_id) { | |
267 if (oauth2_token_service_->GetPrimaryAccountId() == account_id) { | |
268 access_token_.clear(); | |
269 if (IsStarted()) { | |
270 UpdateInvalidatorCredentials(); | |
271 } | |
272 } | |
273 } | |
274 | |
278 void TiclInvalidationService::OnInvalidatorStateChange( | 275 void TiclInvalidationService::OnInvalidatorStateChange( |
279 syncer::InvalidatorState state) { | 276 syncer::InvalidatorState state) { |
280 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { | 277 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { |
281 // This may be due to normal OAuth access token expiration. If so, we must | 278 // This may be due to normal OAuth access token expiration. If so, we must |
282 // fetch a new one using our refresh token. Resetting the invalidator's | 279 // fetch a new one using our refresh token. Resetting the invalidator's |
283 // access token will not reset the invalidator's exponential backoff, so | 280 // access token will not reset the invalidator's exponential backoff, so |
284 // it's safe to try to update the token every time we receive this signal. | 281 // it's safe to try to update the token every time we receive this signal. |
285 // | 282 // |
286 // We won't be receiving any invalidations while the refresh is in progress, | 283 // We won't be receiving any invalidations while the refresh is in progress, |
287 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials | 284 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in."; | 317 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in."; |
321 return false; | 318 return false; |
322 } | 319 } |
323 | 320 |
324 if (!oauth2_token_service_) { | 321 if (!oauth2_token_service_) { |
325 DVLOG(2) | 322 DVLOG(2) |
326 << "Not starting TiclInvalidationService: TokenService unavailable."; | 323 << "Not starting TiclInvalidationService: TokenService unavailable."; |
327 return false; | 324 return false; |
328 } | 325 } |
329 | 326 |
330 if (!oauth2_token_service_->RefreshTokenIsAvailable()) { | 327 if (!oauth2_token_service_->RefreshTokenIsAvailable( |
328 oauth2_token_service_->GetPrimaryAccountId())) { | |
331 DVLOG(2) | 329 DVLOG(2) |
332 << "Not starting TiclInvalidationServce: Waiting for refresh token."; | 330 << "Not starting TiclInvalidationServce: Waiting for refresh token."; |
333 return false; | 331 return false; |
334 } | 332 } |
335 | 333 |
336 return true; | 334 return true; |
337 } | 335 } |
338 | 336 |
339 bool TiclInvalidationService::IsStarted() { | 337 bool TiclInvalidationService::IsStarted() { |
340 return invalidator_.get() != NULL; | 338 return invalidator_.get() != NULL; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 } | 397 } |
400 | 398 |
401 // This service always expects to have a valid invalidator storage. | 399 // This service always expects to have a valid invalidator storage. |
402 // So we must not only clear the old one, but also start a new one. | 400 // So we must not only clear the old one, but also start a new one. |
403 invalidator_storage_->Clear(); | 401 invalidator_storage_->Clear(); |
404 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | 402 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); |
405 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | 403 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); |
406 } | 404 } |
407 | 405 |
408 } // namespace invalidation | 406 } // namespace invalidation |
OLD | NEW |