Chromium Code Reviews| 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" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "base/timer.h" | 15 #include "base/timer.h" |
| 16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "google_apis/gaia/gaia_urls.h" | 17 #include "google_apis/gaia/gaia_urls.h" |
| 18 #include "google_apis/gaia/google_service_auth_error.h" | 18 #include "google_apis/gaia/google_service_auth_error.h" |
| 19 #include "google_apis/gaia/oauth2_access_token_consumer.h" | 19 #include "google_apis/gaia/oauth2_access_token_consumer.h" |
| 20 #include "google_apis/gaia/oauth2_access_token_fetcher.h" | 20 #include "google_apis/gaia/oauth2_access_token_fetcher.h" |
| 21 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
| 22 | 22 |
| 23 namespace { | 23 int OAuth2TokenService::max_fetch_retry_num_ = 5; |
| 24 | |
| 25 // Maximum number of retries in fetching an OAuth2 access token. | |
| 26 const int kMaxFetchRetryNum = 5; | |
| 27 | |
| 28 // Returns an exponential backoff in milliseconds including randomness less than | |
| 29 // 1000 ms when retrying fetching an OAuth2 access token. | |
| 30 int64 ComputeExponentialBackOffMilliseconds(int retry_num) { | |
| 31 DCHECK(retry_num < kMaxFetchRetryNum); | |
| 32 int64 exponential_backoff_in_seconds = 1 << retry_num; | |
| 33 // Returns a backoff with randomness < 1000ms | |
| 34 return (exponential_backoff_in_seconds + base::RandDouble()) * 1000; | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | 24 |
| 39 OAuth2TokenService::RequestImpl::RequestImpl( | 25 OAuth2TokenService::RequestImpl::RequestImpl( |
| 40 OAuth2TokenService::Consumer* consumer) | 26 OAuth2TokenService::Consumer* consumer) |
| 41 : consumer_(consumer) { | 27 : consumer_(consumer) { |
| 42 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 28 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 43 } | 29 } |
| 44 | 30 |
| 45 OAuth2TokenService::RequestImpl::~RequestImpl() { | 31 OAuth2TokenService::RequestImpl::~RequestImpl() { |
| 46 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 32 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 47 } | 33 } |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 77 // fetching. | 63 // fetching. |
| 78 // | 64 // |
| 79 // The waiting requests are taken as weak pointers and they can be deleted. The | 65 // The waiting requests are taken as weak pointers and they can be deleted. The |
| 80 // waiting requests will be called back with fetching results if they are not | 66 // waiting requests will be called back with fetching results if they are not |
| 81 // deleted | 67 // deleted |
| 82 // - when the Fetcher completes fetching, if the Fetcher is not destructed | 68 // - when the Fetcher completes fetching, if the Fetcher is not destructed |
| 83 // before it completes fetching, or | 69 // before it completes fetching, or |
| 84 // - when the Fetcher is destructed if the Fetcher is destructed before it | 70 // - when the Fetcher is destructed if the Fetcher is destructed before it |
| 85 // completes fetching (in this case, the waiting requests will be called back | 71 // completes fetching (in this case, the waiting requests will be called back |
| 86 // with error). | 72 // with error). |
| 87 class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer { | 73 class OAuth2TokenService::Fetcher : public RefreshTokenValidationConsumer, |
| 74 public OAuth2AccessTokenConsumer { | |
| 88 public: | 75 public: |
| 89 // Creates a Fetcher and starts fetching an OAuth2 access token for | 76 // Creates a Fetcher and starts fetching an OAuth2 access token for |
| 90 // |refresh_token| and |scopes| in the request context obtained by |getter|. | 77 // |refresh_token| and |scopes| in the request context obtained by |getter|. |
| 91 // The given |oauth2_token_service| will be informed when fetching is done. | 78 // The given |oauth2_token_service| will be informed when fetching is done. |
| 92 static Fetcher* CreateAndStart(OAuth2TokenService* oauth2_token_service, | 79 static Fetcher* CreateAndStart(OAuth2TokenService* oauth2_token_service, |
| 93 net::URLRequestContextGetter* getter, | 80 net::URLRequestContextGetter* getter, |
| 94 const std::string& refresh_token, | 81 const std::string& refresh_token, |
| 95 const OAuth2TokenService::ScopeSet& scopes, | 82 const OAuth2TokenService::ScopeSet& scopes, |
| 96 base::WeakPtr<RequestImpl> waiting_request); | 83 base::WeakPtr<RequestImpl> waiting_request); |
| 97 virtual ~Fetcher(); | 84 virtual ~Fetcher(); |
| 98 | 85 |
| 99 // 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. |
| 100 void AddWaitingRequest(base::WeakPtr<RequestImpl> waiting_request); | 87 void AddWaitingRequest(base::WeakPtr<RequestImpl> waiting_request); |
| 101 | 88 |
| 102 const OAuth2TokenService::ScopeSet& GetScopeSet() const; | 89 const OAuth2TokenService::ScopeSet& GetScopeSet() const; |
| 103 const std::string& GetRefreshToken() const; | 90 const std::string& GetRefreshToken() const; |
| 104 | 91 |
| 105 // The error result from this fetcher. | 92 // The error result from this fetcher. |
| 106 const GoogleServiceAuthError& error() const { return error_; } | 93 const GoogleServiceAuthError& error() const { return error_; } |
| 107 | 94 |
| 108 protected: | 95 protected: |
| 109 // OAuth2AccessTokenConsumer | 96 // OAuth2AccessTokenConsumer |
| 110 virtual void OnGetTokenSuccess(const std::string& access_token, | 97 virtual void OnGetTokenSuccess(const std::string& access_token, |
| 111 const base::Time& expiration_date) OVERRIDE; | 98 const base::Time& expiration_date) OVERRIDE; |
| 112 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | 99 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; |
| 113 | 100 |
| 101 // RefreshTokenValidationConsumer | |
| 102 virtual void OnRefreshTokenValidationComplete( | |
| 103 const std::string& refresh_token, | |
| 104 bool is_valid) OVERRIDE; | |
| 105 | |
| 114 private: | 106 private: |
| 115 Fetcher(OAuth2TokenService* oauth2_token_service, | 107 Fetcher(OAuth2TokenService* oauth2_token_service, |
| 116 net::URLRequestContextGetter* getter, | 108 net::URLRequestContextGetter* getter, |
| 117 const std::string& refresh_token, | 109 const std::string& refresh_token, |
| 118 const OAuth2TokenService::ScopeSet& scopes, | 110 const OAuth2TokenService::ScopeSet& scopes, |
| 119 base::WeakPtr<RequestImpl> waiting_request); | 111 base::WeakPtr<RequestImpl> waiting_request); |
| 120 void Start(); | 112 void Start(); |
| 113 void StartAccessTokenFetch(); | |
| 121 void InformWaitingRequests(); | 114 void InformWaitingRequests(); |
| 115 void InformWaitingRequestsAndQuit(); | |
| 122 static bool ShouldRetry(const GoogleServiceAuthError& error); | 116 static bool ShouldRetry(const GoogleServiceAuthError& error); |
| 117 int64 ComputeExponentialBackOffMilliseconds(int retry_num); | |
| 123 | 118 |
| 124 // |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 |
| 125 // 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 |
| 126 // scheduled for deletion at the end of OnGetTokenFailure/OnGetTokenSuccess | 121 // scheduled for deletion at the end of OnGetTokenFailure/OnGetTokenSuccess |
| 127 // (whichever comes first). | 122 // (whichever comes first). |
| 128 OAuth2TokenService* const oauth2_token_service_; | 123 OAuth2TokenService* const oauth2_token_service_; |
| 129 scoped_refptr<net::URLRequestContextGetter> getter_; | 124 scoped_refptr<net::URLRequestContextGetter> getter_; |
| 130 const std::string refresh_token_; | 125 const std::string refresh_token_; |
| 131 const OAuth2TokenService::ScopeSet scopes_; | 126 const OAuth2TokenService::ScopeSet scopes_; |
| 132 std::vector<base::WeakPtr<RequestImpl> > waiting_requests_; | 127 std::vector<base::WeakPtr<RequestImpl> > waiting_requests_; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 waiting_requests_.push_back(waiting_request); | 171 waiting_requests_.push_back(waiting_request); |
| 177 } | 172 } |
| 178 | 173 |
| 179 OAuth2TokenService::Fetcher::~Fetcher() { | 174 OAuth2TokenService::Fetcher::~Fetcher() { |
| 180 // Inform the waiting requests if it has not done so. | 175 // Inform the waiting requests if it has not done so. |
| 181 if (waiting_requests_.size()) | 176 if (waiting_requests_.size()) |
| 182 InformWaitingRequests(); | 177 InformWaitingRequests(); |
| 183 } | 178 } |
| 184 | 179 |
| 185 void OAuth2TokenService::Fetcher::Start() { | 180 void OAuth2TokenService::Fetcher::Start() { |
| 181 if (!oauth2_token_service_->StartRefreshTokenValidation(refresh_token_, | |
| 182 this)) { | |
| 183 StartAccessTokenFetch(); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 void OAuth2TokenService::Fetcher::OnRefreshTokenValidationComplete( | |
| 188 const std::string& refresh_token, | |
|
Mattias Nissler (ping if slow)
2013/06/19 17:53:17
nit: indentation
David Roche
2013/06/20 17:49:29
Removed.
| |
| 189 bool is_valid) { | |
| 190 DCHECK(refresh_token_ == refresh_token); | |
| 191 if (is_valid) { | |
| 192 StartAccessTokenFetch(); | |
| 193 } else { | |
| 194 error_ = GoogleServiceAuthError( | |
| 195 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | |
| 196 InformWaitingRequestsAndQuit(); | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 void OAuth2TokenService::Fetcher::StartAccessTokenFetch() { | |
| 186 fetcher_.reset(new OAuth2AccessTokenFetcher(this, getter_.get())); | 201 fetcher_.reset(new OAuth2AccessTokenFetcher(this, getter_.get())); |
| 187 fetcher_->Start(GaiaUrls::GetInstance()->oauth2_chrome_client_id(), | 202 fetcher_->Start(GaiaUrls::GetInstance()->oauth2_chrome_client_id(), |
| 188 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), | 203 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), |
| 189 refresh_token_, | 204 refresh_token_, |
| 190 std::vector<std::string>(scopes_.begin(), scopes_.end())); | 205 std::vector<std::string>(scopes_.begin(), scopes_.end())); |
| 191 retry_timer_.Stop(); | 206 retry_timer_.Stop(); |
| 192 } | 207 } |
| 193 | 208 |
| 194 void OAuth2TokenService::Fetcher::OnGetTokenSuccess( | 209 void OAuth2TokenService::Fetcher::OnGetTokenSuccess( |
| 195 const std::string& access_token, | 210 const std::string& access_token, |
| 196 const base::Time& expiration_date) { | 211 const base::Time& expiration_date) { |
| 197 fetcher_.reset(); | 212 fetcher_.reset(); |
| 198 | 213 |
| 199 // Fetch completes. | 214 // Fetch completes. |
| 200 error_ = GoogleServiceAuthError::AuthErrorNone(); | 215 error_ = GoogleServiceAuthError::AuthErrorNone(); |
| 201 access_token_ = access_token; | 216 access_token_ = access_token; |
| 202 expiration_date_ = expiration_date; | 217 expiration_date_ = expiration_date; |
| 203 | 218 |
| 204 // Subclasses may override this method to skip caching in some cases, but | 219 // Subclasses may override this method to skip caching in some cases, but |
| 205 // we still inform all waiting Consumers of a successful token fetch below. | 220 // we still inform all waiting Consumers of a successful token fetch below. |
| 206 // This is intentional -- some consumers may need the token for cleanup | 221 // This is intentional -- some consumers may need the token for cleanup |
| 207 // tasks. https://chromiumcodereview.appspot.com/11312124/ | 222 // tasks. https://chromiumcodereview.appspot.com/11312124/ |
| 208 oauth2_token_service_->RegisterCacheEntry(refresh_token_, | 223 oauth2_token_service_->RegisterCacheEntry(refresh_token_, |
| 209 scopes_, | 224 scopes_, |
| 210 access_token_, | 225 access_token_, |
| 211 expiration_date_); | 226 expiration_date_); |
| 212 // Deregisters itself from the service to prevent more waiting requests to | 227 InformWaitingRequestsAndQuit(); |
| 213 // be added when it calls back the waiting requests. | |
| 214 oauth2_token_service_->OnFetchComplete(this); | |
| 215 InformWaitingRequests(); | |
| 216 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 217 } | 228 } |
| 218 | 229 |
| 219 void OAuth2TokenService::Fetcher::OnGetTokenFailure( | 230 void OAuth2TokenService::Fetcher::OnGetTokenFailure( |
| 220 const GoogleServiceAuthError& error) { | 231 const GoogleServiceAuthError& error) { |
| 221 fetcher_.reset(); | 232 fetcher_.reset(); |
| 222 | 233 |
| 223 if (ShouldRetry(error) && retry_number_ < kMaxFetchRetryNum) { | 234 if (ShouldRetry(error) && retry_number_ < max_fetch_retry_num_) { |
| 224 int64 backoff = ComputeExponentialBackOffMilliseconds(retry_number_); | 235 int64 backoff = ComputeExponentialBackOffMilliseconds(retry_number_); |
| 225 ++retry_number_; | 236 ++retry_number_; |
| 226 retry_timer_.Stop(); | 237 retry_timer_.Stop(); |
| 227 retry_timer_.Start(FROM_HERE, | 238 retry_timer_.Start(FROM_HERE, |
| 228 base::TimeDelta::FromMilliseconds(backoff), | 239 base::TimeDelta::FromMilliseconds(backoff), |
| 229 this, | 240 this, |
| 230 &OAuth2TokenService::Fetcher::Start); | 241 &OAuth2TokenService::Fetcher::Start); |
| 231 return; | 242 return; |
| 232 } | 243 } |
| 233 | 244 |
| 234 // Fetch completes. | |
| 235 error_ = error; | 245 error_ = error; |
| 246 InformWaitingRequestsAndQuit(); | |
| 247 } | |
| 236 | 248 |
| 237 // Deregisters itself from the service to prevent more waiting requests to be | 249 // Returns an exponential backoff in milliseconds including randomness less than |
| 238 // added when it calls back the waiting requests. | 250 // 1000 ms when retrying fetching an OAuth2 access token. |
| 239 oauth2_token_service_->OnFetchComplete(this); | 251 int64 OAuth2TokenService::Fetcher::ComputeExponentialBackOffMilliseconds( |
| 240 InformWaitingRequests(); | 252 int retry_num) { |
| 241 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 253 DCHECK(retry_num < max_fetch_retry_num_); |
| 254 int64 exponential_backoff_in_seconds = 1 << retry_num; | |
| 255 // Returns a backoff with randomness < 1000ms | |
| 256 return (exponential_backoff_in_seconds + base::RandDouble()) * 1000; | |
| 242 } | 257 } |
| 243 | 258 |
| 244 // static | 259 // static |
| 245 bool OAuth2TokenService::Fetcher::ShouldRetry( | 260 bool OAuth2TokenService::Fetcher::ShouldRetry( |
| 246 const GoogleServiceAuthError& error) { | 261 const GoogleServiceAuthError& error) { |
| 247 GoogleServiceAuthError::State error_state = error.state(); | 262 GoogleServiceAuthError::State error_state = error.state(); |
| 248 return error_state == GoogleServiceAuthError::CONNECTION_FAILED || | 263 return error_state == GoogleServiceAuthError::CONNECTION_FAILED || |
| 249 error_state == GoogleServiceAuthError::REQUEST_CANCELED || | 264 error_state == GoogleServiceAuthError::REQUEST_CANCELED || |
| 250 error_state == GoogleServiceAuthError::SERVICE_UNAVAILABLE; | 265 error_state == GoogleServiceAuthError::SERVICE_UNAVAILABLE; |
| 251 } | 266 } |
| 252 | 267 |
| 253 void OAuth2TokenService::Fetcher::InformWaitingRequests() { | 268 void OAuth2TokenService::Fetcher::InformWaitingRequests() { |
| 254 std::vector<base::WeakPtr<RequestImpl> >::const_iterator iter = | 269 std::vector<base::WeakPtr<RequestImpl> >::const_iterator iter = |
| 255 waiting_requests_.begin(); | 270 waiting_requests_.begin(); |
| 256 for (; iter != waiting_requests_.end(); ++iter) { | 271 for (; iter != waiting_requests_.end(); ++iter) { |
| 257 base::WeakPtr<RequestImpl> waiting_request = *iter; | 272 base::WeakPtr<RequestImpl> waiting_request = *iter; |
| 258 if (waiting_request.get()) | 273 if (waiting_request.get()) |
| 259 waiting_request->InformConsumer(error_, access_token_, expiration_date_); | 274 waiting_request->InformConsumer(error_, access_token_, expiration_date_); |
| 260 } | 275 } |
| 261 waiting_requests_.clear(); | 276 waiting_requests_.clear(); |
| 262 } | 277 } |
| 263 | 278 |
| 279 void OAuth2TokenService::Fetcher::InformWaitingRequestsAndQuit() { | |
| 280 // Deregisters itself from the service to prevent more waiting requests to | |
| 281 // be added when it calls back the waiting requests. | |
| 282 oauth2_token_service_->OnFetchComplete(this); | |
| 283 InformWaitingRequests(); | |
| 284 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 285 } | |
| 286 | |
| 264 void OAuth2TokenService::Fetcher::AddWaitingRequest( | 287 void OAuth2TokenService::Fetcher::AddWaitingRequest( |
| 265 base::WeakPtr<OAuth2TokenService::RequestImpl> waiting_request) { | 288 base::WeakPtr<OAuth2TokenService::RequestImpl> waiting_request) { |
| 266 waiting_requests_.push_back(waiting_request); | 289 waiting_requests_.push_back(waiting_request); |
| 267 } | 290 } |
| 268 | 291 |
| 269 const OAuth2TokenService::ScopeSet& OAuth2TokenService::Fetcher::GetScopeSet() | 292 const OAuth2TokenService::ScopeSet& OAuth2TokenService::Fetcher::GetScopeSet() |
| 270 const { | 293 const { |
| 271 return scopes_; | 294 return scopes_; |
| 272 } | 295 } |
| 273 | 296 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 291 : request_context_getter_(getter) { | 314 : request_context_getter_(getter) { |
| 292 } | 315 } |
| 293 | 316 |
| 294 OAuth2TokenService::~OAuth2TokenService() { | 317 OAuth2TokenService::~OAuth2TokenService() { |
| 295 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 318 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 296 // Release all the pending fetchers. | 319 // Release all the pending fetchers. |
| 297 STLDeleteContainerPairSecondPointers( | 320 STLDeleteContainerPairSecondPointers( |
| 298 pending_fetchers_.begin(), pending_fetchers_.end()); | 321 pending_fetchers_.begin(), pending_fetchers_.end()); |
| 299 } | 322 } |
| 300 | 323 |
| 301 bool OAuth2TokenService::RefreshTokenIsAvailable() { | |
| 302 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 303 return !GetRefreshToken().empty(); | |
| 304 } | |
| 305 | |
| 306 scoped_ptr<OAuth2TokenService::Request> OAuth2TokenService::StartRequest( | 324 scoped_ptr<OAuth2TokenService::Request> OAuth2TokenService::StartRequest( |
| 307 const OAuth2TokenService::ScopeSet& scopes, | 325 const OAuth2TokenService::ScopeSet& scopes, |
| 308 OAuth2TokenService::Consumer* consumer) { | 326 OAuth2TokenService::Consumer* consumer) { |
| 309 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 327 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 310 | 328 |
| 311 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); | 329 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); |
| 312 | 330 |
| 313 std::string refresh_token = GetRefreshToken(); | 331 std::string refresh_token = GetRefreshToken(); |
| 314 if (refresh_token.empty()) { | 332 if (refresh_token.empty()) { |
| 315 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 333 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 316 &RequestImpl::InformConsumer, | 334 &RequestImpl::InformConsumer, |
| 317 request->AsWeakPtr(), | 335 request->AsWeakPtr(), |
| 318 GoogleServiceAuthError( | 336 GoogleServiceAuthError( |
| 319 GoogleServiceAuthError::USER_NOT_SIGNED_UP), | 337 GoogleServiceAuthError::USER_NOT_SIGNED_UP), |
| 320 std::string(), | 338 std::string(), |
| 321 base::Time())); | 339 base::Time())); |
| 322 return request.PassAs<Request>(); | 340 return request.PassAs<Request>(); |
| 323 } | 341 } |
| 324 | 342 |
| 325 if (HasCacheEntry(scopes)) | 343 if (HasCacheEntry(scopes)) |
| 326 return StartCacheLookupRequest(scopes, consumer); | 344 return StartCacheLookupRequest(scopes, request.Pass()); |
| 327 | 345 |
| 328 // Makes sure there is a pending fetcher for |scopes| and |refresh_token|. | 346 // If there is already a pending fetcher for |scopes| and |refresh_token|, |
| 329 // Adds |request| to the waiting request list of this fetcher so |request| | 347 // simply register this |request| for those results rather than starting |
| 330 // will be called back when this fetcher finishes fetching. | 348 // a new fetcher. |
| 331 FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes); | 349 FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes); |
| 332 std::map<FetchParameters, Fetcher*>::iterator iter = | 350 std::map<FetchParameters, Fetcher*>::iterator iter = |
| 333 pending_fetchers_.find(fetch_parameters); | 351 pending_fetchers_.find(fetch_parameters); |
| 334 if (iter != pending_fetchers_.end()) { | 352 if (iter != pending_fetchers_.end()) { |
| 335 iter->second->AddWaitingRequest(request->AsWeakPtr()); | 353 iter->second->AddWaitingRequest(request->AsWeakPtr()); |
| 336 return request.PassAs<Request>(); | 354 return request.PassAs<Request>(); |
| 337 } | 355 } |
| 356 | |
| 338 pending_fetchers_[fetch_parameters] = | 357 pending_fetchers_[fetch_parameters] = |
| 339 Fetcher::CreateAndStart(this, | 358 Fetcher::CreateAndStart(this, |
| 340 request_context_getter_.get(), | 359 request_context_getter_.get(), |
| 341 refresh_token, | 360 refresh_token, |
| 342 scopes, | 361 scopes, |
| 343 request->AsWeakPtr()); | 362 request->AsWeakPtr()); |
| 344 return request.PassAs<Request>(); | 363 return request.PassAs<Request>(); |
| 345 } | 364 } |
| 346 | 365 |
| 347 scoped_ptr<OAuth2TokenService::Request> | 366 scoped_ptr<OAuth2TokenService::Request> |
| 348 OAuth2TokenService::StartCacheLookupRequest( | 367 OAuth2TokenService::StartCacheLookupRequest( |
| 349 const OAuth2TokenService::ScopeSet& scopes, | 368 const OAuth2TokenService::ScopeSet& scopes, |
| 350 OAuth2TokenService::Consumer* consumer) { | 369 scoped_ptr<RequestImpl> request) { |
| 351 CHECK(HasCacheEntry(scopes)); | 370 CHECK(HasCacheEntry(scopes)); |
| 352 const CacheEntry* cache_entry = GetCacheEntry(scopes); | 371 const CacheEntry* cache_entry = GetCacheEntry(scopes); |
| 353 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); | |
| 354 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 372 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 355 &RequestImpl::InformConsumer, | 373 &RequestImpl::InformConsumer, |
| 356 request->AsWeakPtr(), | 374 request->AsWeakPtr(), |
| 357 GoogleServiceAuthError(GoogleServiceAuthError::NONE), | 375 GoogleServiceAuthError(GoogleServiceAuthError::NONE), |
| 358 cache_entry->access_token, | 376 cache_entry->access_token, |
| 359 cache_entry->expiration_date)); | 377 cache_entry->expiration_date)); |
| 360 return request.PassAs<Request>(); | 378 return request.PassAs<Request>(); |
| 361 } | 379 } |
| 362 | 380 |
| 363 void OAuth2TokenService::InvalidateToken(const ScopeSet& scopes, | 381 void OAuth2TokenService::InvalidateToken(const ScopeSet& scopes, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 444 const OAuth2TokenService::ScopeSet& scopes, | 462 const OAuth2TokenService::ScopeSet& scopes, |
| 445 const std::string& access_token, | 463 const std::string& access_token, |
| 446 const base::Time& expiration_date) { | 464 const base::Time& expiration_date) { |
| 447 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 465 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 448 | 466 |
| 449 CacheEntry& token = token_cache_[scopes]; | 467 CacheEntry& token = token_cache_[scopes]; |
| 450 token.access_token = access_token; | 468 token.access_token = access_token; |
| 451 token.expiration_date = expiration_date; | 469 token.expiration_date = expiration_date; |
| 452 } | 470 } |
| 453 | 471 |
| 472 bool OAuth2TokenService::StartRefreshTokenValidation( | |
| 473 const std::string refresh_token, | |
|
Mattias Nissler (ping if slow)
2013/06/19 17:53:17
nit: indentation
David Roche
2013/06/20 17:49:29
Removed.
| |
| 474 RefreshTokenValidationConsumer* consumer) { | |
| 475 return false; // No validation needed; use token directly. | |
| 476 } | |
| 477 | |
| 454 void OAuth2TokenService::UpdateAuthError(const GoogleServiceAuthError& error) { | 478 void OAuth2TokenService::UpdateAuthError(const GoogleServiceAuthError& error) { |
| 455 // Default implementation does nothing. | 479 // Default implementation does nothing. |
| 456 } | 480 } |
| 457 | 481 |
| 458 void OAuth2TokenService::ClearCache() { | 482 void OAuth2TokenService::ClearCache() { |
| 459 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 483 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 460 token_cache_.clear(); | 484 token_cache_.clear(); |
| 461 } | 485 } |
| 462 | 486 |
| 463 int OAuth2TokenService::cache_size_for_testing() const { | 487 int OAuth2TokenService::cache_size_for_testing() const { |
| 464 return token_cache_.size(); | 488 return token_cache_.size(); |
| 465 } | 489 } |
| 490 | |
| 491 void OAuth2TokenService::set_max_authorization_token_fetch_retries_for_testing( | |
| 492 int max_retries) { | |
| 493 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 494 max_fetch_retry_num_ = max_retries; | |
| 495 } | |
| OLD | NEW |