| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "google_apis/gaia/oauth2_token_service.h" | 5 #include "google_apis/gaia/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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 // - when the Fetcher is destructed if the Fetcher is destructed before it | 68 // - when the Fetcher is destructed if the Fetcher is destructed before it |
| 69 // completes fetching (in this case, the waiting requests will be called back | 69 // completes fetching (in this case, the waiting requests will be called back |
| 70 // with error). | 70 // with error). |
| 71 class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer { | 71 class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer { |
| 72 public: | 72 public: |
| 73 // Creates a Fetcher and starts fetching an OAuth2 access token for | 73 // Creates a Fetcher and starts fetching an OAuth2 access token for |
| 74 // |refresh_token| and |scopes| in the request context obtained by |getter|. | 74 // |refresh_token| and |scopes| in the request context obtained by |getter|. |
| 75 // The given |oauth2_token_service| will be informed when fetching is done. | 75 // The given |oauth2_token_service| will be informed when fetching is done. |
| 76 static Fetcher* CreateAndStart(OAuth2TokenService* oauth2_token_service, | 76 static Fetcher* CreateAndStart(OAuth2TokenService* oauth2_token_service, |
| 77 net::URLRequestContextGetter* getter, | 77 net::URLRequestContextGetter* getter, |
| 78 const std::string& account_id, |
| 78 const std::string& chrome_client_id, | 79 const std::string& chrome_client_id, |
| 79 const std::string& chrome_client_secret, | 80 const std::string& chrome_client_secret, |
| 80 const std::string& refresh_token, | 81 const std::string& refresh_token, |
| 81 const OAuth2TokenService::ScopeSet& scopes, | 82 const OAuth2TokenService::ScopeSet& scopes, |
| 82 base::WeakPtr<RequestImpl> waiting_request); | 83 base::WeakPtr<RequestImpl> waiting_request); |
| 83 virtual ~Fetcher(); | 84 virtual ~Fetcher(); |
| 84 | 85 |
| 85 // Add a request that is waiting for the result of this Fetcher. | 86 // Add a request that is waiting for the result of this Fetcher. |
| 86 void AddWaitingRequest(base::WeakPtr<RequestImpl> waiting_request); | 87 void AddWaitingRequest(base::WeakPtr<RequestImpl> waiting_request); |
| 87 | 88 |
| 88 void Cancel(); | 89 void Cancel(); |
| 89 | 90 |
| 90 const OAuth2TokenService::ScopeSet& GetScopeSet() const; | 91 const OAuth2TokenService::ScopeSet& GetScopeSet() const; |
| 91 const std::string& GetRefreshToken() const; | 92 const std::string& GetRefreshToken() const; |
| 93 const std::string& GetAccountId() const; |
| 92 | 94 |
| 93 // The error result from this fetcher. | 95 // The error result from this fetcher. |
| 94 const GoogleServiceAuthError& error() const { return error_; } | 96 const GoogleServiceAuthError& error() const { return error_; } |
| 95 | 97 |
| 96 protected: | 98 protected: |
| 97 // OAuth2AccessTokenConsumer | 99 // OAuth2AccessTokenConsumer |
| 98 virtual void OnGetTokenSuccess(const std::string& access_token, | 100 virtual void OnGetTokenSuccess(const std::string& access_token, |
| 99 const base::Time& expiration_date) OVERRIDE; | 101 const base::Time& expiration_date) OVERRIDE; |
| 100 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | 102 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; |
| 101 | 103 |
| 102 private: | 104 private: |
| 103 Fetcher(OAuth2TokenService* oauth2_token_service, | 105 Fetcher(OAuth2TokenService* oauth2_token_service, |
| 104 net::URLRequestContextGetter* getter, | 106 net::URLRequestContextGetter* getter, |
| 105 const std::string& chrome_client_id, | 107 const std::string& chrome_client_id, |
| 106 const std::string& chrome_client_secret, | 108 const std::string& chrome_client_secret, |
| 109 const std::string& account_id, |
| 107 const std::string& refresh_token, | 110 const std::string& refresh_token, |
| 108 const OAuth2TokenService::ScopeSet& scopes, | 111 const OAuth2TokenService::ScopeSet& scopes, |
| 109 base::WeakPtr<RequestImpl> waiting_request); | 112 base::WeakPtr<RequestImpl> waiting_request); |
| 110 void Start(); | 113 void Start(); |
| 111 void InformWaitingRequests(); | 114 void InformWaitingRequests(); |
| 112 void InformWaitingRequestsAndDelete(); | 115 void InformWaitingRequestsAndDelete(); |
| 113 static bool ShouldRetry(const GoogleServiceAuthError& error); | 116 static bool ShouldRetry(const GoogleServiceAuthError& error); |
| 114 int64 ComputeExponentialBackOffMilliseconds(int retry_num); | 117 int64 ComputeExponentialBackOffMilliseconds(int retry_num); |
| 115 | 118 |
| 116 // |oauth2_token_service_| remains valid for the life of this Fetcher, since | 119 // |oauth2_token_service_| remains valid for the life of this Fetcher, since |
| 117 // this Fetcher is destructed in the dtor of the OAuth2TokenService or is | 120 // this Fetcher is destructed in the dtor of the OAuth2TokenService or is |
| 118 // scheduled for deletion at the end of OnGetTokenFailure/OnGetTokenSuccess | 121 // scheduled for deletion at the end of OnGetTokenFailure/OnGetTokenSuccess |
| 119 // (whichever comes first). | 122 // (whichever comes first). |
| 120 OAuth2TokenService* const oauth2_token_service_; | 123 OAuth2TokenService* const oauth2_token_service_; |
| 121 scoped_refptr<net::URLRequestContextGetter> getter_; | 124 scoped_refptr<net::URLRequestContextGetter> getter_; |
| 125 const std::string account_id_; |
| 122 const std::string refresh_token_; | 126 const std::string refresh_token_; |
| 123 const OAuth2TokenService::ScopeSet scopes_; | 127 const OAuth2TokenService::ScopeSet scopes_; |
| 124 std::vector<base::WeakPtr<RequestImpl> > waiting_requests_; | 128 std::vector<base::WeakPtr<RequestImpl> > waiting_requests_; |
| 125 | 129 |
| 126 int retry_number_; | 130 int retry_number_; |
| 127 base::OneShotTimer<OAuth2TokenService::Fetcher> retry_timer_; | 131 base::OneShotTimer<OAuth2TokenService::Fetcher> retry_timer_; |
| 128 scoped_ptr<OAuth2AccessTokenFetcher> fetcher_; | 132 scoped_ptr<OAuth2AccessTokenFetcher> fetcher_; |
| 129 | 133 |
| 130 // Variables that store fetch results. | 134 // Variables that store fetch results. |
| 131 // Initialized to be GoogleServiceAuthError::SERVICE_UNAVAILABLE to handle | 135 // Initialized to be GoogleServiceAuthError::SERVICE_UNAVAILABLE to handle |
| 132 // destruction. | 136 // destruction. |
| 133 GoogleServiceAuthError error_; | 137 GoogleServiceAuthError error_; |
| 134 std::string access_token_; | 138 std::string access_token_; |
| 135 base::Time expiration_date_; | 139 base::Time expiration_date_; |
| 136 // OAuth2 client id and secret. | 140 // OAuth2 client id and secret. |
| 137 std::string chrome_client_id_; | 141 std::string chrome_client_id_; |
| 138 std::string chrome_client_secret_; | 142 std::string chrome_client_secret_; |
| 139 | 143 |
| 140 DISALLOW_COPY_AND_ASSIGN(Fetcher); | 144 DISALLOW_COPY_AND_ASSIGN(Fetcher); |
| 141 }; | 145 }; |
| 142 | 146 |
| 143 // static | 147 // static |
| 144 OAuth2TokenService::Fetcher* OAuth2TokenService::Fetcher::CreateAndStart( | 148 OAuth2TokenService::Fetcher* OAuth2TokenService::Fetcher::CreateAndStart( |
| 145 OAuth2TokenService* oauth2_token_service, | 149 OAuth2TokenService* oauth2_token_service, |
| 146 net::URLRequestContextGetter* getter, | 150 net::URLRequestContextGetter* getter, |
| 151 const std::string& account_id, |
| 147 const std::string& chrome_client_id, | 152 const std::string& chrome_client_id, |
| 148 const std::string& chrome_client_secret, | 153 const std::string& chrome_client_secret, |
| 149 const std::string& refresh_token, | 154 const std::string& refresh_token, |
| 150 const OAuth2TokenService::ScopeSet& scopes, | 155 const OAuth2TokenService::ScopeSet& scopes, |
| 151 base::WeakPtr<RequestImpl> waiting_request) { | 156 base::WeakPtr<RequestImpl> waiting_request) { |
| 152 OAuth2TokenService::Fetcher* fetcher = new Fetcher( | 157 OAuth2TokenService::Fetcher* fetcher = new Fetcher( |
| 153 oauth2_token_service, | 158 oauth2_token_service, |
| 154 getter, | 159 getter, |
| 155 chrome_client_id, | 160 chrome_client_id, |
| 156 chrome_client_secret, | 161 chrome_client_secret, |
| 162 account_id, |
| 157 refresh_token, | 163 refresh_token, |
| 158 scopes, | 164 scopes, |
| 159 waiting_request); | 165 waiting_request); |
| 160 fetcher->Start(); | 166 fetcher->Start(); |
| 161 return fetcher; | 167 return fetcher; |
| 162 } | 168 } |
| 163 | 169 |
| 164 OAuth2TokenService::Fetcher::Fetcher( | 170 OAuth2TokenService::Fetcher::Fetcher( |
| 165 OAuth2TokenService* oauth2_token_service, | 171 OAuth2TokenService* oauth2_token_service, |
| 166 net::URLRequestContextGetter* getter, | 172 net::URLRequestContextGetter* getter, |
| 167 const std::string& chrome_client_id, | 173 const std::string& chrome_client_id, |
| 168 const std::string& chrome_client_secret, | 174 const std::string& chrome_client_secret, |
| 175 const std::string& account_id, |
| 169 const std::string& refresh_token, | 176 const std::string& refresh_token, |
| 170 const OAuth2TokenService::ScopeSet& scopes, | 177 const OAuth2TokenService::ScopeSet& scopes, |
| 171 base::WeakPtr<RequestImpl> waiting_request) | 178 base::WeakPtr<RequestImpl> waiting_request) |
| 172 : oauth2_token_service_(oauth2_token_service), | 179 : oauth2_token_service_(oauth2_token_service), |
| 173 getter_(getter), | 180 getter_(getter), |
| 181 account_id_(account_id), |
| 174 refresh_token_(refresh_token), | 182 refresh_token_(refresh_token), |
| 175 scopes_(scopes), | 183 scopes_(scopes), |
| 176 retry_number_(0), | 184 retry_number_(0), |
| 177 error_(GoogleServiceAuthError::SERVICE_UNAVAILABLE), | 185 error_(GoogleServiceAuthError::SERVICE_UNAVAILABLE), |
| 178 chrome_client_id_(chrome_client_id), | 186 chrome_client_id_(chrome_client_id), |
| 179 chrome_client_secret_(chrome_client_secret) { | 187 chrome_client_secret_(chrome_client_secret) { |
| 180 DCHECK(oauth2_token_service_); | 188 DCHECK(oauth2_token_service_); |
| 181 DCHECK(getter_.get()); | 189 DCHECK(getter_.get()); |
| 182 DCHECK(refresh_token_.length()); | 190 DCHECK(refresh_token_.length()); |
| 183 waiting_requests_.push_back(waiting_request); | 191 waiting_requests_.push_back(waiting_request); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 | 298 |
| 291 const OAuth2TokenService::ScopeSet& OAuth2TokenService::Fetcher::GetScopeSet() | 299 const OAuth2TokenService::ScopeSet& OAuth2TokenService::Fetcher::GetScopeSet() |
| 292 const { | 300 const { |
| 293 return scopes_; | 301 return scopes_; |
| 294 } | 302 } |
| 295 | 303 |
| 296 const std::string& OAuth2TokenService::Fetcher::GetRefreshToken() const { | 304 const std::string& OAuth2TokenService::Fetcher::GetRefreshToken() const { |
| 297 return refresh_token_; | 305 return refresh_token_; |
| 298 } | 306 } |
| 299 | 307 |
| 308 const std::string& OAuth2TokenService::Fetcher::GetAccountId() const { |
| 309 return account_id_; |
| 310 } |
| 311 |
| 300 OAuth2TokenService::Request::Request() { | 312 OAuth2TokenService::Request::Request() { |
| 301 } | 313 } |
| 302 | 314 |
| 303 OAuth2TokenService::Request::~Request() { | 315 OAuth2TokenService::Request::~Request() { |
| 304 } | 316 } |
| 305 | 317 |
| 306 OAuth2TokenService::Consumer::Consumer() { | 318 OAuth2TokenService::Consumer::Consumer() { |
| 307 } | 319 } |
| 308 | 320 |
| 309 OAuth2TokenService::Consumer::~Consumer() { | 321 OAuth2TokenService::Consumer::~Consumer() { |
| 310 } | 322 } |
| 311 | 323 |
| 312 OAuth2TokenService::OAuth2TokenService() { | 324 OAuth2TokenService::OAuth2TokenService() { |
| 313 } | 325 } |
| 314 | 326 |
| 315 OAuth2TokenService::~OAuth2TokenService() { | 327 OAuth2TokenService::~OAuth2TokenService() { |
| 316 // Release all the pending fetchers. | 328 // Release all the pending fetchers. |
| 317 STLDeleteContainerPairSecondPointers( | 329 STLDeleteContainerPairSecondPointers( |
| 318 pending_fetchers_.begin(), pending_fetchers_.end()); | 330 pending_fetchers_.begin(), pending_fetchers_.end()); |
| 319 } | 331 } |
| 320 | 332 |
| 321 void OAuth2TokenService::AddObserver(Observer* observer) { | 333 void OAuth2TokenService::AddObserver(Observer* observer) { |
| 322 observer_list_.AddObserver(observer); | 334 observer_list_.AddObserver(observer); |
| 323 } | 335 } |
| 324 | 336 |
| 325 void OAuth2TokenService::RemoveObserver(Observer* observer) { | 337 void OAuth2TokenService::RemoveObserver(Observer* observer) { |
| 326 observer_list_.RemoveObserver(observer); | 338 observer_list_.RemoveObserver(observer); |
| 327 } | 339 } |
| 328 | 340 |
| 329 bool OAuth2TokenService::RefreshTokenIsAvailable() { | 341 bool OAuth2TokenService::RefreshTokenIsAvailable( |
| 330 return !GetRefreshToken().empty(); | 342 const std::string& account_id) { |
| 343 DCHECK(CalledOnValidThread()); |
| 344 return !GetRefreshToken(account_id).empty(); |
| 345 } |
| 346 |
| 347 std::vector<std::string> OAuth2TokenService::GetAccounts() { |
| 348 return std::vector<std::string>(); |
| 331 } | 349 } |
| 332 | 350 |
| 333 scoped_ptr<OAuth2TokenService::Request> OAuth2TokenService::StartRequest( | 351 scoped_ptr<OAuth2TokenService::Request> OAuth2TokenService::StartRequest( |
| 352 const std::string& account_id, |
| 334 const OAuth2TokenService::ScopeSet& scopes, | 353 const OAuth2TokenService::ScopeSet& scopes, |
| 335 OAuth2TokenService::Consumer* consumer) { | 354 OAuth2TokenService::Consumer* consumer) { |
| 336 return StartRequestForClientWithContext( | 355 return StartRequestForClientWithContext( |
| 356 account_id, |
| 337 GetRequestContext(), | 357 GetRequestContext(), |
| 338 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), | 358 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), |
| 339 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), | 359 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), |
| 340 scopes, | 360 scopes, |
| 341 consumer); | 361 consumer); |
| 342 } | 362 } |
| 343 | 363 |
| 344 scoped_ptr<OAuth2TokenService::Request> | 364 scoped_ptr<OAuth2TokenService::Request> |
| 345 OAuth2TokenService::StartRequestForClient( | 365 OAuth2TokenService::StartRequestForClient( |
| 366 const std::string& account_id, |
| 346 const std::string& client_id, | 367 const std::string& client_id, |
| 347 const std::string& client_secret, | 368 const std::string& client_secret, |
| 348 const OAuth2TokenService::ScopeSet& scopes, | 369 const OAuth2TokenService::ScopeSet& scopes, |
| 349 OAuth2TokenService::Consumer* consumer) { | 370 OAuth2TokenService::Consumer* consumer) { |
| 350 return StartRequestForClientWithContext( | 371 return StartRequestForClientWithContext( |
| 372 account_id, |
| 351 GetRequestContext(), | 373 GetRequestContext(), |
| 352 client_id, | 374 client_id, |
| 353 client_secret, | 375 client_secret, |
| 354 scopes, | 376 scopes, |
| 355 consumer); | 377 consumer); |
| 356 } | 378 } |
| 357 | 379 |
| 358 scoped_ptr<OAuth2TokenService::Request> | 380 scoped_ptr<OAuth2TokenService::Request> |
| 359 OAuth2TokenService::StartRequestWithContext( | 381 OAuth2TokenService::StartRequestWithContext( |
| 382 const std::string& account_id, |
| 360 net::URLRequestContextGetter* getter, | 383 net::URLRequestContextGetter* getter, |
| 361 const ScopeSet& scopes, | 384 const ScopeSet& scopes, |
| 362 Consumer* consumer) { | 385 Consumer* consumer) { |
| 363 return StartRequestForClientWithContext( | 386 return StartRequestForClientWithContext( |
| 387 account_id, |
| 364 getter, | 388 getter, |
| 365 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), | 389 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), |
| 366 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), | 390 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), |
| 367 scopes, | 391 scopes, |
| 368 consumer); | 392 consumer); |
| 369 } | 393 } |
| 370 | 394 |
| 371 scoped_ptr<OAuth2TokenService::Request> | 395 scoped_ptr<OAuth2TokenService::Request> |
| 372 OAuth2TokenService::StartRequestForClientWithContext( | 396 OAuth2TokenService::StartRequestForClientWithContext( |
| 397 const std::string& account_id, |
| 373 net::URLRequestContextGetter* getter, | 398 net::URLRequestContextGetter* getter, |
| 374 const std::string& client_id, | 399 const std::string& client_id, |
| 375 const std::string& client_secret, | 400 const std::string& client_secret, |
| 376 const ScopeSet& scopes, | 401 const ScopeSet& scopes, |
| 377 Consumer* consumer) { | 402 Consumer* consumer) { |
| 378 DCHECK(CalledOnValidThread()); | 403 DCHECK(CalledOnValidThread()); |
| 379 | 404 |
| 380 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); | 405 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); |
| 381 | 406 |
| 382 if (!RefreshTokenIsAvailable()) { | 407 if (!RefreshTokenIsAvailable(account_id)) { |
| 383 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 408 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 384 &RequestImpl::InformConsumer, | 409 &RequestImpl::InformConsumer, |
| 385 request->AsWeakPtr(), | 410 request->AsWeakPtr(), |
| 386 GoogleServiceAuthError(GoogleServiceAuthError::USER_NOT_SIGNED_UP), | 411 GoogleServiceAuthError(GoogleServiceAuthError::USER_NOT_SIGNED_UP), |
| 387 std::string(), | 412 std::string(), |
| 388 base::Time())); | 413 base::Time())); |
| 389 return request.PassAs<Request>(); | 414 return request.PassAs<Request>(); |
| 390 } | 415 } |
| 391 | 416 |
| 392 if (HasCacheEntry(scopes)) { | 417 if (HasCacheEntry(scopes)) { |
| 393 StartCacheLookupRequest(request.get(), scopes, consumer); | 418 StartCacheLookupRequest(request.get(), scopes, consumer); |
| 394 } else { | 419 } else { |
| 395 FetchOAuth2Token(request.get(), | 420 FetchOAuth2Token(request.get(), |
| 421 account_id, |
| 396 getter, | 422 getter, |
| 397 client_id, | 423 client_id, |
| 398 client_secret, | 424 client_secret, |
| 399 scopes); | 425 scopes); |
| 400 } | 426 } |
| 401 return request.PassAs<Request>(); | 427 return request.PassAs<Request>(); |
| 402 } | 428 } |
| 403 | 429 |
| 404 void OAuth2TokenService::FetchOAuth2Token(RequestImpl* request, | 430 void OAuth2TokenService::FetchOAuth2Token(RequestImpl* request, |
| 431 const std::string& account_id, |
| 405 net::URLRequestContextGetter* getter, | 432 net::URLRequestContextGetter* getter, |
| 406 const std::string& client_id, | 433 const std::string& client_id, |
| 407 const std::string& client_secret, | 434 const std::string& client_secret, |
| 408 const ScopeSet& scopes) { | 435 const ScopeSet& scopes) { |
| 409 std::string refresh_token = GetRefreshToken(); | 436 std::string refresh_token = GetRefreshToken(account_id); |
| 410 | 437 |
| 411 // If there is already a pending fetcher for |scopes| and |refresh_token|, | 438 // If there is already a pending fetcher for |scopes| and |refresh_token|, |
| 412 // simply register this |request| for those results rather than starting | 439 // simply register this |request| for those results rather than starting |
| 413 // a new fetcher. | 440 // a new fetcher. |
| 414 FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes); | 441 FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes); |
| 415 std::map<FetchParameters, Fetcher*>::iterator iter = | 442 std::map<FetchParameters, Fetcher*>::iterator iter = |
| 416 pending_fetchers_.find(fetch_parameters); | 443 pending_fetchers_.find(fetch_parameters); |
| 417 if (iter != pending_fetchers_.end()) { | 444 if (iter != pending_fetchers_.end()) { |
| 418 iter->second->AddWaitingRequest(request->AsWeakPtr()); | 445 iter->second->AddWaitingRequest(request->AsWeakPtr()); |
| 419 return; | 446 return; |
| 420 } | 447 } |
| 421 | 448 |
| 422 pending_fetchers_[fetch_parameters] = | 449 pending_fetchers_[fetch_parameters] = |
| 423 Fetcher::CreateAndStart(this, | 450 Fetcher::CreateAndStart(this, |
| 424 getter, | 451 getter, |
| 452 account_id, |
| 425 client_id, | 453 client_id, |
| 426 client_secret, | 454 client_secret, |
| 427 refresh_token, | 455 refresh_token, |
| 428 scopes, | 456 scopes, |
| 429 request->AsWeakPtr()); | 457 request->AsWeakPtr()); |
| 430 } | 458 } |
| 431 | 459 |
| 432 void OAuth2TokenService::StartCacheLookupRequest( | 460 void OAuth2TokenService::StartCacheLookupRequest( |
| 433 RequestImpl* request, | 461 RequestImpl* request, |
| 434 const OAuth2TokenService::ScopeSet& scopes, | 462 const OAuth2TokenService::ScopeSet& scopes, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 447 const std::string& invalid_token) { | 475 const std::string& invalid_token) { |
| 448 DCHECK(CalledOnValidThread()); | 476 DCHECK(CalledOnValidThread()); |
| 449 RemoveCacheEntry(scopes, invalid_token); | 477 RemoveCacheEntry(scopes, invalid_token); |
| 450 } | 478 } |
| 451 | 479 |
| 452 void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) { | 480 void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) { |
| 453 DCHECK(CalledOnValidThread()); | 481 DCHECK(CalledOnValidThread()); |
| 454 | 482 |
| 455 // Update the auth error state so auth errors are appropriately communicated | 483 // Update the auth error state so auth errors are appropriately communicated |
| 456 // to the user. | 484 // to the user. |
| 457 UpdateAuthError(fetcher->error()); | 485 UpdateAuthError(fetcher->GetAccountId(), fetcher->error()); |
| 458 | 486 |
| 459 // Note |fetcher| is recorded in |pending_fetcher_| mapped to its refresh | 487 // Note |fetcher| is recorded in |pending_fetcher_| mapped to its refresh |
| 460 // token and scope set. This is guaranteed as follows; here a Fetcher is said | 488 // token and scope set. This is guaranteed as follows; here a Fetcher is said |
| 461 // to be uncompleted if it has not finished calling back | 489 // to be uncompleted if it has not finished calling back |
| 462 // OAuth2TokenService::OnFetchComplete(). | 490 // OAuth2TokenService::OnFetchComplete(). |
| 463 // | 491 // |
| 464 // (1) All the live Fetchers are created by this service. | 492 // (1) All the live Fetchers are created by this service. |
| 465 // This is because (1) all the live Fetchers are created by a live | 493 // This is because (1) all the live Fetchers are created by a live |
| 466 // service, as all the fetchers created by a service are destructed in the | 494 // service, as all the fetchers created by a service are destructed in the |
| 467 // service's dtor. | 495 // service's dtor. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 485 std::map<FetchParameters, Fetcher*>::iterator iter = | 513 std::map<FetchParameters, Fetcher*>::iterator iter = |
| 486 pending_fetchers_.find(std::make_pair( | 514 pending_fetchers_.find(std::make_pair( |
| 487 fetcher->GetRefreshToken(), fetcher->GetScopeSet())); | 515 fetcher->GetRefreshToken(), fetcher->GetScopeSet())); |
| 488 DCHECK(iter != pending_fetchers_.end()); | 516 DCHECK(iter != pending_fetchers_.end()); |
| 489 DCHECK_EQ(fetcher, iter->second); | 517 DCHECK_EQ(fetcher, iter->second); |
| 490 pending_fetchers_.erase(iter); | 518 pending_fetchers_.erase(iter); |
| 491 } | 519 } |
| 492 | 520 |
| 493 bool OAuth2TokenService::HasCacheEntry( | 521 bool OAuth2TokenService::HasCacheEntry( |
| 494 const OAuth2TokenService::ScopeSet& scopes) { | 522 const OAuth2TokenService::ScopeSet& scopes) { |
| 523 // TODO(fgorski): Update keying of the cache to include account_id. |
| 495 const CacheEntry* cache_entry = GetCacheEntry(scopes); | 524 const CacheEntry* cache_entry = GetCacheEntry(scopes); |
| 496 return cache_entry && cache_entry->access_token.length(); | 525 return cache_entry && cache_entry->access_token.length(); |
| 497 } | 526 } |
| 498 | 527 |
| 499 const OAuth2TokenService::CacheEntry* OAuth2TokenService::GetCacheEntry( | 528 const OAuth2TokenService::CacheEntry* OAuth2TokenService::GetCacheEntry( |
| 500 const OAuth2TokenService::ScopeSet& scopes) { | 529 const OAuth2TokenService::ScopeSet& scopes) { |
| 530 // TODO(fgorski): Update keying of the cache to include account_id. |
| 501 DCHECK(CalledOnValidThread()); | 531 DCHECK(CalledOnValidThread()); |
| 502 TokenCache::iterator token_iterator = token_cache_.find(scopes); | 532 TokenCache::iterator token_iterator = token_cache_.find(scopes); |
| 503 if (token_iterator == token_cache_.end()) | 533 if (token_iterator == token_cache_.end()) |
| 504 return NULL; | 534 return NULL; |
| 505 if (token_iterator->second.expiration_date <= base::Time::Now()) { | 535 if (token_iterator->second.expiration_date <= base::Time::Now()) { |
| 506 token_cache_.erase(token_iterator); | 536 token_cache_.erase(token_iterator); |
| 507 return NULL; | 537 return NULL; |
| 508 } | 538 } |
| 509 return &token_iterator->second; | 539 return &token_iterator->second; |
| 510 } | 540 } |
| 511 | 541 |
| 512 bool OAuth2TokenService::RemoveCacheEntry( | 542 bool OAuth2TokenService::RemoveCacheEntry( |
| 513 const OAuth2TokenService::ScopeSet& scopes, | 543 const OAuth2TokenService::ScopeSet& scopes, |
| 514 const std::string& token_to_remove) { | 544 const std::string& token_to_remove) { |
| 545 // TODO(fgorski): Update keying of the cache to include account_id. |
| 515 DCHECK(CalledOnValidThread()); | 546 DCHECK(CalledOnValidThread()); |
| 516 TokenCache::iterator token_iterator = token_cache_.find(scopes); | 547 TokenCache::iterator token_iterator = token_cache_.find(scopes); |
| 517 if (token_iterator != token_cache_.end() && | 548 if (token_iterator != token_cache_.end() && |
| 518 token_iterator->second.access_token == token_to_remove) { | 549 token_iterator->second.access_token == token_to_remove) { |
| 519 token_cache_.erase(token_iterator); | 550 token_cache_.erase(token_iterator); |
| 520 return true; | 551 return true; |
| 521 } | 552 } |
| 522 return false; | 553 return false; |
| 523 } | 554 } |
| 524 | 555 |
| 525 void OAuth2TokenService::RegisterCacheEntry( | 556 void OAuth2TokenService::RegisterCacheEntry( |
| 526 const std::string& refresh_token, | 557 const std::string& refresh_token, |
| 527 const OAuth2TokenService::ScopeSet& scopes, | 558 const OAuth2TokenService::ScopeSet& scopes, |
| 528 const std::string& access_token, | 559 const std::string& access_token, |
| 529 const base::Time& expiration_date) { | 560 const base::Time& expiration_date) { |
| 561 // TODO(fgorski): Update keying of the cache to include account_id. |
| 530 DCHECK(CalledOnValidThread()); | 562 DCHECK(CalledOnValidThread()); |
| 531 | 563 |
| 532 CacheEntry& token = token_cache_[scopes]; | 564 CacheEntry& token = token_cache_[scopes]; |
| 533 token.access_token = access_token; | 565 token.access_token = access_token; |
| 534 token.expiration_date = expiration_date; | 566 token.expiration_date = expiration_date; |
| 535 } | 567 } |
| 536 | 568 |
| 537 void OAuth2TokenService::UpdateAuthError(const GoogleServiceAuthError& error) { | 569 void OAuth2TokenService::UpdateAuthError( |
| 570 const std::string& account_id, |
| 571 const GoogleServiceAuthError& error) { |
| 538 // Default implementation does nothing. | 572 // Default implementation does nothing. |
| 539 } | 573 } |
| 540 | 574 |
| 541 void OAuth2TokenService::ClearCache() { | 575 void OAuth2TokenService::ClearCache() { |
| 542 DCHECK(CalledOnValidThread()); | 576 DCHECK(CalledOnValidThread()); |
| 543 token_cache_.clear(); | 577 token_cache_.clear(); |
| 544 } | 578 } |
| 545 | 579 |
| 546 void OAuth2TokenService::CancelAllRequests() { | 580 void OAuth2TokenService::CancelAllRequests() { |
| 547 std::vector<Fetcher*> fetchers_to_cancel; | 581 std::vector<Fetcher*> fetchers_to_cancel; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 void OAuth2TokenService::FireRefreshTokenRevoked( | 620 void OAuth2TokenService::FireRefreshTokenRevoked( |
| 587 const std::string& account_id) { | 621 const std::string& account_id) { |
| 588 FOR_EACH_OBSERVER(Observer, observer_list_, | 622 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 589 OnRefreshTokenRevoked(account_id)); | 623 OnRefreshTokenRevoked(account_id)); |
| 590 } | 624 } |
| 591 | 625 |
| 592 void OAuth2TokenService::FireRefreshTokensLoaded() { | 626 void OAuth2TokenService::FireRefreshTokensLoaded() { |
| 593 FOR_EACH_OBSERVER(Observer, observer_list_, OnRefreshTokensLoaded()); | 627 FOR_EACH_OBSERVER(Observer, observer_list_, OnRefreshTokensLoaded()); |
| 594 } | 628 } |
| 595 | 629 |
| 596 void OAuth2TokenService::FireRefreshTokensCleared() { | |
| 597 FOR_EACH_OBSERVER(Observer, observer_list_, OnRefreshTokensCleared()); | |
| 598 } | |
| 599 | |
| 600 int OAuth2TokenService::cache_size_for_testing() const { | 630 int OAuth2TokenService::cache_size_for_testing() const { |
| 601 return token_cache_.size(); | 631 return token_cache_.size(); |
| 602 } | 632 } |
| 603 | 633 |
| 604 void OAuth2TokenService::set_max_authorization_token_fetch_retries_for_testing( | 634 void OAuth2TokenService::set_max_authorization_token_fetch_retries_for_testing( |
| 605 int max_retries) { | 635 int max_retries) { |
| 606 DCHECK(CalledOnValidThread()); | 636 DCHECK(CalledOnValidThread()); |
| 607 max_fetch_retry_num_ = max_retries; | 637 max_fetch_retry_num_ = max_retries; |
| 608 } | 638 } |
| OLD | NEW |