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

Side by Side Diff: chrome/browser/signin/oauth2_token_service.cc

Issue 14113053: chrome: Use base::MessageLoop. (Part 3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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/signin/oauth2_token_service.h" 5 #include "chrome/browser/signin/oauth2_token_service.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 // This is intentional -- some consumers may need the token for cleanup 206 // This is intentional -- some consumers may need the token for cleanup
207 // tasks. https://chromiumcodereview.appspot.com/11312124/ 207 // tasks. https://chromiumcodereview.appspot.com/11312124/
208 oauth2_token_service_->RegisterCacheEntry(refresh_token_, 208 oauth2_token_service_->RegisterCacheEntry(refresh_token_,
209 scopes_, 209 scopes_,
210 access_token_, 210 access_token_,
211 expiration_date_); 211 expiration_date_);
212 // Deregisters itself from the service to prevent more waiting requests to 212 // Deregisters itself from the service to prevent more waiting requests to
213 // be added when it calls back the waiting requests. 213 // be added when it calls back the waiting requests.
214 oauth2_token_service_->OnFetchComplete(this); 214 oauth2_token_service_->OnFetchComplete(this);
215 InformWaitingRequests(); 215 InformWaitingRequests();
216 MessageLoop::current()->DeleteSoon(FROM_HERE, this); 216 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
217 } 217 }
218 218
219 void OAuth2TokenService::Fetcher::OnGetTokenFailure( 219 void OAuth2TokenService::Fetcher::OnGetTokenFailure(
220 const GoogleServiceAuthError& error) { 220 const GoogleServiceAuthError& error) {
221 fetcher_.reset(); 221 fetcher_.reset();
222 222
223 if (ShouldRetry(error) && retry_number_ < kMaxFetchRetryNum) { 223 if (ShouldRetry(error) && retry_number_ < kMaxFetchRetryNum) {
224 int64 backoff = ComputeExponentialBackOffMilliseconds(retry_number_); 224 int64 backoff = ComputeExponentialBackOffMilliseconds(retry_number_);
225 ++retry_number_; 225 ++retry_number_;
226 retry_timer_.Stop(); 226 retry_timer_.Stop();
227 retry_timer_.Start(FROM_HERE, 227 retry_timer_.Start(FROM_HERE,
228 base::TimeDelta::FromMilliseconds(backoff), 228 base::TimeDelta::FromMilliseconds(backoff),
229 this, 229 this,
230 &OAuth2TokenService::Fetcher::Start); 230 &OAuth2TokenService::Fetcher::Start);
231 return; 231 return;
232 } 232 }
233 233
234 // Fetch completes. 234 // Fetch completes.
235 error_ = error; 235 error_ = error;
236 236
237 // Deregisters itself from the service to prevent more waiting requests to be 237 // Deregisters itself from the service to prevent more waiting requests to be
238 // added when it calls back the waiting requests. 238 // added when it calls back the waiting requests.
239 oauth2_token_service_->OnFetchComplete(this); 239 oauth2_token_service_->OnFetchComplete(this);
240 InformWaitingRequests(); 240 InformWaitingRequests();
241 MessageLoop::current()->DeleteSoon(FROM_HERE, this); 241 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
242 } 242 }
243 243
244 // static 244 // static
245 bool OAuth2TokenService::Fetcher::ShouldRetry( 245 bool OAuth2TokenService::Fetcher::ShouldRetry(
246 const GoogleServiceAuthError& error) { 246 const GoogleServiceAuthError& error) {
247 GoogleServiceAuthError::State error_state = error.state(); 247 GoogleServiceAuthError::State error_state = error.state();
248 return error_state == GoogleServiceAuthError::CONNECTION_FAILED || 248 return error_state == GoogleServiceAuthError::CONNECTION_FAILED ||
249 error_state == GoogleServiceAuthError::REQUEST_CANCELED || 249 error_state == GoogleServiceAuthError::REQUEST_CANCELED ||
250 error_state == GoogleServiceAuthError::SERVICE_UNAVAILABLE; 250 error_state == GoogleServiceAuthError::SERVICE_UNAVAILABLE;
251 } 251 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 317
318 scoped_ptr<OAuth2TokenService::Request> OAuth2TokenService::StartRequest( 318 scoped_ptr<OAuth2TokenService::Request> OAuth2TokenService::StartRequest(
319 const OAuth2TokenService::ScopeSet& scopes, 319 const OAuth2TokenService::ScopeSet& scopes,
320 OAuth2TokenService::Consumer* consumer) { 320 OAuth2TokenService::Consumer* consumer) {
321 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 321 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
322 322
323 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); 323 scoped_ptr<RequestImpl> request(new RequestImpl(consumer));
324 324
325 std::string refresh_token = GetRefreshToken(); 325 std::string refresh_token = GetRefreshToken();
326 if (refresh_token.empty()) { 326 if (refresh_token.empty()) {
327 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 327 base::MessageLoop::current()->PostTask(
328 &OAuth2TokenService::InformConsumer, 328 FROM_HERE,
329 request->AsWeakPtr(), 329 base::Bind(
330 GoogleServiceAuthError( 330 &OAuth2TokenService::InformConsumer,
331 GoogleServiceAuthError::USER_NOT_SIGNED_UP), 331 request->AsWeakPtr(),
332 std::string(), 332 GoogleServiceAuthError(GoogleServiceAuthError::USER_NOT_SIGNED_UP),
333 base::Time())); 333 std::string(),
334 base::Time()));
334 return request.PassAs<Request>(); 335 return request.PassAs<Request>();
335 } 336 }
336 337
337 if (HasCacheEntry(scopes)) 338 if (HasCacheEntry(scopes))
338 return StartCacheLookupRequest(scopes, consumer); 339 return StartCacheLookupRequest(scopes, consumer);
339 340
340 // Makes sure there is a pending fetcher for |scopes| and |refresh_token|. 341 // Makes sure there is a pending fetcher for |scopes| and |refresh_token|.
341 // Adds |request| to the waiting request list of this fetcher so |request| 342 // Adds |request| to the waiting request list of this fetcher so |request|
342 // will be called back when this fetcher finishes fetching. 343 // will be called back when this fetcher finishes fetching.
343 FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes); 344 FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes);
344 std::map<FetchParameters, Fetcher*>::iterator iter = 345 std::map<FetchParameters, Fetcher*>::iterator iter =
345 pending_fetchers_.find(fetch_parameters); 346 pending_fetchers_.find(fetch_parameters);
346 if (iter != pending_fetchers_.end()) { 347 if (iter != pending_fetchers_.end()) {
347 iter->second->AddWaitingRequest(request->AsWeakPtr()); 348 iter->second->AddWaitingRequest(request->AsWeakPtr());
348 return request.PassAs<Request>(); 349 return request.PassAs<Request>();
349 } 350 }
350 pending_fetchers_[fetch_parameters] = Fetcher::CreateAndStart( 351 pending_fetchers_[fetch_parameters] = Fetcher::CreateAndStart(
351 this, request_context_getter_, refresh_token, scopes, 352 this, request_context_getter_, refresh_token, scopes,
352 request->AsWeakPtr()); 353 request->AsWeakPtr());
353 return request.PassAs<Request>(); 354 return request.PassAs<Request>();
354 } 355 }
355 356
356 scoped_ptr<OAuth2TokenService::Request> 357 scoped_ptr<OAuth2TokenService::Request>
357 OAuth2TokenService::StartCacheLookupRequest( 358 OAuth2TokenService::StartCacheLookupRequest(
358 const OAuth2TokenService::ScopeSet& scopes, 359 const OAuth2TokenService::ScopeSet& scopes,
359 OAuth2TokenService::Consumer* consumer) { 360 OAuth2TokenService::Consumer* consumer) {
360 CHECK(HasCacheEntry(scopes)); 361 CHECK(HasCacheEntry(scopes));
361 const CacheEntry* cache_entry = GetCacheEntry(scopes); 362 const CacheEntry* cache_entry = GetCacheEntry(scopes);
362 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); 363 scoped_ptr<RequestImpl> request(new RequestImpl(consumer));
363 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 364 base::MessageLoop::current()->PostTask(
364 &OAuth2TokenService::InformConsumer, 365 FROM_HERE,
365 request->AsWeakPtr(), 366 base::Bind(&OAuth2TokenService::InformConsumer,
366 GoogleServiceAuthError(GoogleServiceAuthError::NONE), 367 request->AsWeakPtr(),
367 cache_entry->access_token, 368 GoogleServiceAuthError(GoogleServiceAuthError::NONE),
368 cache_entry->expiration_date)); 369 cache_entry->access_token,
370 cache_entry->expiration_date));
369 return request.PassAs<Request>(); 371 return request.PassAs<Request>();
370 } 372 }
371 373
372 void OAuth2TokenService::InvalidateToken(const ScopeSet& scopes, 374 void OAuth2TokenService::InvalidateToken(const ScopeSet& scopes,
373 const std::string& invalid_token) { 375 const std::string& invalid_token) {
374 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 376 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
375 RemoveCacheEntry(scopes, invalid_token); 377 RemoveCacheEntry(scopes, invalid_token);
376 } 378 }
377 379
378 void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) { 380 void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) {
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 } 467 }
466 468
467 void OAuth2TokenService::ClearCache() { 469 void OAuth2TokenService::ClearCache() {
468 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 470 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
469 token_cache_.clear(); 471 token_cache_.clear();
470 } 472 }
471 473
472 int OAuth2TokenService::cache_size_for_testing() const { 474 int OAuth2TokenService::cache_size_for_testing() const {
473 return token_cache_.size(); 475 return token_cache_.size();
474 } 476 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698