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