OLD | NEW |
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 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 // Release all the pending fetchers. | 296 // Release all the pending fetchers. |
297 STLDeleteContainerPairSecondPointers( | 297 STLDeleteContainerPairSecondPointers( |
298 pending_fetchers_.begin(), pending_fetchers_.end()); | 298 pending_fetchers_.begin(), pending_fetchers_.end()); |
299 } | 299 } |
300 | 300 |
301 bool OAuth2TokenService::RefreshTokenIsAvailable() { | 301 bool OAuth2TokenService::RefreshTokenIsAvailable() { |
302 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 302 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
303 return !GetRefreshToken().empty(); | 303 return !GetRefreshToken().empty(); |
304 } | 304 } |
305 | 305 |
306 // static | |
307 void OAuth2TokenService::InformConsumer( | |
308 base::WeakPtr<OAuth2TokenService::RequestImpl> request, | |
309 const GoogleServiceAuthError& error, | |
310 const std::string& access_token, | |
311 const base::Time& expiration_date) { | |
312 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
313 | |
314 if (request) | |
315 request->InformConsumer(error, access_token, expiration_date); | |
316 } | |
317 | |
318 scoped_ptr<OAuth2TokenService::Request> OAuth2TokenService::StartRequest( | 306 scoped_ptr<OAuth2TokenService::Request> OAuth2TokenService::StartRequest( |
319 const OAuth2TokenService::ScopeSet& scopes, | 307 const OAuth2TokenService::ScopeSet& scopes, |
320 OAuth2TokenService::Consumer* consumer) { | 308 OAuth2TokenService::Consumer* consumer) { |
321 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 309 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
322 | 310 |
323 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); | 311 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); |
324 | 312 |
325 std::string refresh_token = GetRefreshToken(); | 313 std::string refresh_token = GetRefreshToken(); |
326 if (refresh_token.empty()) { | 314 if (refresh_token.empty()) { |
327 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 315 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
328 &OAuth2TokenService::InformConsumer, | 316 &RequestImpl::InformConsumer, |
329 request->AsWeakPtr(), | 317 request->AsWeakPtr(), |
330 GoogleServiceAuthError( | 318 GoogleServiceAuthError( |
331 GoogleServiceAuthError::USER_NOT_SIGNED_UP), | 319 GoogleServiceAuthError::USER_NOT_SIGNED_UP), |
332 std::string(), | 320 std::string(), |
333 base::Time())); | 321 base::Time())); |
334 return request.PassAs<Request>(); | 322 return request.PassAs<Request>(); |
335 } | 323 } |
336 | 324 |
337 if (HasCacheEntry(scopes)) | 325 if (HasCacheEntry(scopes)) |
338 return StartCacheLookupRequest(scopes, consumer); | 326 return StartCacheLookupRequest(scopes, consumer); |
(...skipping 18 matching lines...) Expand all Loading... |
357 } | 345 } |
358 | 346 |
359 scoped_ptr<OAuth2TokenService::Request> | 347 scoped_ptr<OAuth2TokenService::Request> |
360 OAuth2TokenService::StartCacheLookupRequest( | 348 OAuth2TokenService::StartCacheLookupRequest( |
361 const OAuth2TokenService::ScopeSet& scopes, | 349 const OAuth2TokenService::ScopeSet& scopes, |
362 OAuth2TokenService::Consumer* consumer) { | 350 OAuth2TokenService::Consumer* consumer) { |
363 CHECK(HasCacheEntry(scopes)); | 351 CHECK(HasCacheEntry(scopes)); |
364 const CacheEntry* cache_entry = GetCacheEntry(scopes); | 352 const CacheEntry* cache_entry = GetCacheEntry(scopes); |
365 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); | 353 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); |
366 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 354 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
367 &OAuth2TokenService::InformConsumer, | 355 &RequestImpl::InformConsumer, |
368 request->AsWeakPtr(), | 356 request->AsWeakPtr(), |
369 GoogleServiceAuthError(GoogleServiceAuthError::NONE), | 357 GoogleServiceAuthError(GoogleServiceAuthError::NONE), |
370 cache_entry->access_token, | 358 cache_entry->access_token, |
371 cache_entry->expiration_date)); | 359 cache_entry->expiration_date)); |
372 return request.PassAs<Request>(); | 360 return request.PassAs<Request>(); |
373 } | 361 } |
374 | 362 |
375 void OAuth2TokenService::InvalidateToken(const ScopeSet& scopes, | 363 void OAuth2TokenService::InvalidateToken(const ScopeSet& scopes, |
376 const std::string& invalid_token) { | 364 const std::string& invalid_token) { |
377 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 365 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 return NULL; | 424 return NULL; |
437 } | 425 } |
438 return &token_iterator->second; | 426 return &token_iterator->second; |
439 } | 427 } |
440 | 428 |
441 bool OAuth2TokenService::RemoveCacheEntry( | 429 bool OAuth2TokenService::RemoveCacheEntry( |
442 const OAuth2TokenService::ScopeSet& scopes, | 430 const OAuth2TokenService::ScopeSet& scopes, |
443 const std::string& token_to_remove) { | 431 const std::string& token_to_remove) { |
444 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 432 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
445 TokenCache::iterator token_iterator = token_cache_.find(scopes); | 433 TokenCache::iterator token_iterator = token_cache_.find(scopes); |
446 if (token_iterator == token_cache_.end() && | 434 if (token_iterator != token_cache_.end() && |
447 token_iterator->second.access_token == token_to_remove) { | 435 token_iterator->second.access_token == token_to_remove) { |
448 token_cache_.erase(token_iterator); | 436 token_cache_.erase(token_iterator); |
449 return true; | 437 return true; |
450 } | 438 } |
451 return false; | 439 return false; |
452 } | 440 } |
453 | 441 |
454 void OAuth2TokenService::RegisterCacheEntry( | 442 void OAuth2TokenService::RegisterCacheEntry( |
455 const std::string& refresh_token, | 443 const std::string& refresh_token, |
456 const OAuth2TokenService::ScopeSet& scopes, | 444 const OAuth2TokenService::ScopeSet& scopes, |
(...skipping 11 matching lines...) Expand all Loading... |
468 } | 456 } |
469 | 457 |
470 void OAuth2TokenService::ClearCache() { | 458 void OAuth2TokenService::ClearCache() { |
471 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 459 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
472 token_cache_.clear(); | 460 token_cache_.clear(); |
473 } | 461 } |
474 | 462 |
475 int OAuth2TokenService::cache_size_for_testing() const { | 463 int OAuth2TokenService::cache_size_for_testing() const { |
476 return token_cache_.size(); | 464 return token_cache_.size(); |
477 } | 465 } |
OLD | NEW |