Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(370)

Side by Side Diff: chrome/browser/invalidation/ticl_invalidation_service.cc

Issue 23382008: Making OAuth2TokenService multi-login aware, updating callers, minor fixes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebasing to include the update to ProfileSyncService: r224220 Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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());
72 } 70 }
73 71
74 void TiclInvalidationService::Init() { 72 void TiclInvalidationService::Init() {
75 DCHECK(CalledOnValidThread()); 73 DCHECK(CalledOnValidThread());
76 74
77 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); 75 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs()));
78 if (invalidator_storage_->GetInvalidatorClientId().empty()) { 76 if (invalidator_storage_->GetInvalidatorClientId().empty()) {
79 // This also clears any existing state. We can't reuse old invalidator 77 // This also clears any existing state. We can't reuse old invalidator
80 // state with the new ID anyway. 78 // state with the new ID anyway.
81 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); 79 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId());
82 } 80 }
83 81
84 if (IsReadyToStart()) { 82 if (IsReadyToStart()) {
85 StartInvalidator(); 83 StartInvalidator();
86 } 84 }
87 85
88 notification_registrar_.Add(this, 86 notification_registrar_.Add(this,
89 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, 87 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT,
90 content::Source<Profile>(profile_)); 88 content::Source<Profile>(profile_));
91 notification_registrar_.Add(this, 89 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 } 90 }
98 91
99 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { 92 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) {
100 // Here we perform the equivalent of Init() and StartInvalidator(), but with 93 // 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 94 // some minor changes to account for the fact that we're injecting the
102 // invalidator. 95 // invalidator.
103 invalidator_.reset(invalidator); 96 invalidator_.reset(invalidator);
104 97
105 invalidator_->RegisterHandler(this); 98 invalidator_->RegisterHandler(this);
106 invalidator_->UpdateRegisteredIds( 99 invalidator_->UpdateRegisteredIds(
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 std::string TiclInvalidationService::GetInvalidatorClientId() const { 157 std::string TiclInvalidationService::GetInvalidatorClientId() const {
165 DCHECK(CalledOnValidThread()); 158 DCHECK(CalledOnValidThread());
166 return invalidator_storage_->GetInvalidatorClientId(); 159 return invalidator_storage_->GetInvalidatorClientId();
167 } 160 }
168 161
169 void TiclInvalidationService::Observe( 162 void TiclInvalidationService::Observe(
170 int type, 163 int type,
171 const content::NotificationSource& source, 164 const content::NotificationSource& source,
172 const content::NotificationDetails& details) { 165 const content::NotificationDetails& details) {
173 DCHECK(CalledOnValidThread()); 166 DCHECK(CalledOnValidThread());
174 167 DCHECK_EQ(type, chrome::NOTIFICATION_GOOGLE_SIGNED_OUT);
175 switch (type) { 168 Logout();
176 case chrome::NOTIFICATION_TOKEN_AVAILABLE: {
177 if (!IsStarted() && IsReadyToStart()) {
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 }
197 } 169 }
198 170
199 void TiclInvalidationService::RequestAccessToken() { 171 void TiclInvalidationService::RequestAccessToken() {
200 // Only one active request at a time. 172 // Only one active request at a time.
201 if (access_token_request_ != NULL) 173 if (access_token_request_ != NULL)
202 return; 174 return;
203 request_access_token_retry_timer_.Stop(); 175 request_access_token_retry_timer_.Stop();
204 OAuth2TokenService::ScopeSet oauth2_scopes; 176 OAuth2TokenService::ScopeSet oauth2_scopes;
205 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++) 177 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++)
206 oauth2_scopes.insert(kOAuth2Scopes[i]); 178 oauth2_scopes.insert(kOAuth2Scopes[i]);
207 // Invalidate previous token, otherwise token service will return the same 179 // Invalidate previous token, otherwise token service will return the same
208 // token again. 180 // token again.
209 oauth2_token_service_->InvalidateToken(oauth2_scopes, access_token_); 181 const std::string& account_id = oauth2_token_service_->GetPrimaryAccountId();
182 oauth2_token_service_->InvalidateToken(account_id,
183 oauth2_scopes,
184 access_token_);
210 access_token_.clear(); 185 access_token_.clear();
211 access_token_request_ = 186 access_token_request_ = oauth2_token_service_->StartRequest(account_id,
212 oauth2_token_service_->StartRequest(oauth2_scopes, this); 187 oauth2_scopes,
188 this);
213 } 189 }
214 190
215 void TiclInvalidationService::OnGetTokenSuccess( 191 void TiclInvalidationService::OnGetTokenSuccess(
216 const OAuth2TokenService::Request* request, 192 const OAuth2TokenService::Request* request,
217 const std::string& access_token, 193 const std::string& access_token,
218 const base::Time& expiration_time) { 194 const base::Time& expiration_time) {
219 DCHECK_EQ(access_token_request_, request); 195 DCHECK_EQ(access_token_request_, request);
220 access_token_request_.reset(); 196 access_token_request_.reset();
221 // Reset backoff time after successful response. 197 // Reset backoff time after successful response.
222 request_access_token_backoff_.Reset(); 198 request_access_token_backoff_.Reset();
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 invalidator_registrar_->UpdateInvalidatorState( 244 invalidator_registrar_->UpdateInvalidatorState(
269 syncer::INVALIDATION_CREDENTIALS_REJECTED); 245 syncer::INVALIDATION_CREDENTIALS_REJECTED);
270 break; 246 break;
271 } 247 }
272 default: { 248 default: {
273 // We have no way to notify the user of this. Do nothing. 249 // We have no way to notify the user of this. Do nothing.
274 } 250 }
275 } 251 }
276 } 252 }
277 253
254 void TiclInvalidationService::OnRefreshTokenAvailable(
255 const std::string& account_id) {
256 if (oauth2_token_service_->GetPrimaryAccountId() == account_id) {
257 if (!IsStarted() && IsReadyToStart()) {
258 StartInvalidator();
259 }
260 }
261 }
262
263 void TiclInvalidationService::OnRefreshTokenRevoked(
264 const std::string& account_id) {
265 if (oauth2_token_service_->GetPrimaryAccountId() == account_id) {
266 access_token_.clear();
267 if (IsStarted()) {
268 UpdateInvalidatorCredentials();
269 }
270 }
271 }
272
278 void TiclInvalidationService::OnInvalidatorStateChange( 273 void TiclInvalidationService::OnInvalidatorStateChange(
279 syncer::InvalidatorState state) { 274 syncer::InvalidatorState state) {
280 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { 275 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) {
281 // This may be due to normal OAuth access token expiration. If so, we must 276 // 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 277 // fetch a new one using our refresh token. Resetting the invalidator's
283 // access token will not reset the invalidator's exponential backoff, so 278 // 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. 279 // it's safe to try to update the token every time we receive this signal.
285 // 280 //
286 // We won't be receiving any invalidations while the refresh is in progress, 281 // 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 282 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials
288 // really are invalid, the refresh request should fail and 283 // really are invalid, the refresh request should fail and
289 // OnGetTokenFailure() will put us into a INVALIDATION_CREDENTIALS_REJECTED 284 // OnGetTokenFailure() will put us into a INVALIDATION_CREDENTIALS_REJECTED
290 // state. 285 // state.
291 invalidator_registrar_->UpdateInvalidatorState( 286 invalidator_registrar_->UpdateInvalidatorState(
292 syncer::TRANSIENT_INVALIDATION_ERROR); 287 syncer::TRANSIENT_INVALIDATION_ERROR);
293 RequestAccessToken(); 288 RequestAccessToken();
294 } else { 289 } else {
295 invalidator_registrar_->UpdateInvalidatorState(state); 290 invalidator_registrar_->UpdateInvalidatorState(state);
296 } 291 }
297 } 292 }
298 293
299 void TiclInvalidationService::OnIncomingInvalidation( 294 void TiclInvalidationService::OnIncomingInvalidation(
300 const syncer::ObjectIdInvalidationMap& invalidation_map) { 295 const syncer::ObjectIdInvalidationMap& invalidation_map) {
301 invalidator_registrar_->DispatchInvalidationsToHandlers(invalidation_map); 296 invalidator_registrar_->DispatchInvalidationsToHandlers(invalidation_map);
302 } 297 }
303 298
304 void TiclInvalidationService::Shutdown() { 299 void TiclInvalidationService::Shutdown() {
305 DCHECK(CalledOnValidThread()); 300 DCHECK(CalledOnValidThread());
301 oauth2_token_service_->RemoveObserver(this);
306 if (IsStarted()) { 302 if (IsStarted()) {
307 StopInvalidator(); 303 StopInvalidator();
308 } 304 }
309 invalidator_storage_.reset(); 305 invalidator_storage_.reset();
310 invalidator_registrar_.reset(); 306 invalidator_registrar_.reset();
311 } 307 }
312 308
313 bool TiclInvalidationService::IsReadyToStart() { 309 bool TiclInvalidationService::IsReadyToStart() {
314 if (profile_->IsManaged()) { 310 if (profile_->IsManaged()) {
315 DVLOG(2) << "Not starting TiclInvalidationService: User is managed."; 311 DVLOG(2) << "Not starting TiclInvalidationService: User is managed.";
316 return false; 312 return false;
317 } 313 }
318 314
319 if (signin_manager_->GetAuthenticatedUsername().empty()) { 315 if (signin_manager_->GetAuthenticatedUsername().empty()) {
320 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in."; 316 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in.";
321 return false; 317 return false;
322 } 318 }
323 319
324 if (!oauth2_token_service_) { 320 if (!oauth2_token_service_) {
325 DVLOG(2) 321 DVLOG(2)
326 << "Not starting TiclInvalidationService: TokenService unavailable."; 322 << "Not starting TiclInvalidationService: TokenService unavailable.";
327 return false; 323 return false;
328 } 324 }
329 325
330 if (!oauth2_token_service_->RefreshTokenIsAvailable()) { 326 if (!oauth2_token_service_->RefreshTokenIsAvailable(
327 oauth2_token_service_->GetPrimaryAccountId())) {
331 DVLOG(2) 328 DVLOG(2)
332 << "Not starting TiclInvalidationServce: Waiting for refresh token."; 329 << "Not starting TiclInvalidationServce: Waiting for refresh token.";
333 return false; 330 return false;
334 } 331 }
335 332
336 return true; 333 return true;
337 } 334 }
338 335
339 bool TiclInvalidationService::IsStarted() { 336 bool TiclInvalidationService::IsStarted() {
340 return invalidator_.get() != NULL; 337 return invalidator_.get() != NULL;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 } 396 }
400 397
401 // This service always expects to have a valid invalidator storage. 398 // 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. 399 // So we must not only clear the old one, but also start a new one.
403 invalidator_storage_->Clear(); 400 invalidator_storage_->Clear();
404 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); 401 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs()));
405 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); 402 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId());
406 } 403 }
407 404
408 } // namespace invalidation 405 } // namespace invalidation
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698