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

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

Issue 17109006: Device robot refresh token integrity validation. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: fix remoting compile error on windows Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/signin/oauth2_token_service.h" 5 #include "chrome/browser/signin/oauth2_token_service.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; 98 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
113 99
114 private: 100 private:
115 Fetcher(OAuth2TokenService* oauth2_token_service, 101 Fetcher(OAuth2TokenService* oauth2_token_service,
116 net::URLRequestContextGetter* getter, 102 net::URLRequestContextGetter* getter,
117 const std::string& refresh_token, 103 const std::string& refresh_token,
118 const OAuth2TokenService::ScopeSet& scopes, 104 const OAuth2TokenService::ScopeSet& scopes,
119 base::WeakPtr<RequestImpl> waiting_request); 105 base::WeakPtr<RequestImpl> waiting_request);
120 void Start(); 106 void Start();
121 void InformWaitingRequests(); 107 void InformWaitingRequests();
108 void InformWaitingRequestsAndDelete();
122 static bool ShouldRetry(const GoogleServiceAuthError& error); 109 static bool ShouldRetry(const GoogleServiceAuthError& error);
110 int64 ComputeExponentialBackOffMilliseconds(int retry_num);
123 111
124 // |oauth2_token_service_| remains valid for the life of this Fetcher, since 112 // |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 113 // this Fetcher is destructed in the dtor of the OAuth2TokenService or is
126 // scheduled for deletion at the end of OnGetTokenFailure/OnGetTokenSuccess 114 // scheduled for deletion at the end of OnGetTokenFailure/OnGetTokenSuccess
127 // (whichever comes first). 115 // (whichever comes first).
128 OAuth2TokenService* const oauth2_token_service_; 116 OAuth2TokenService* const oauth2_token_service_;
129 scoped_refptr<net::URLRequestContextGetter> getter_; 117 scoped_refptr<net::URLRequestContextGetter> getter_;
130 const std::string refresh_token_; 118 const std::string refresh_token_;
131 const OAuth2TokenService::ScopeSet scopes_; 119 const OAuth2TokenService::ScopeSet scopes_;
132 std::vector<base::WeakPtr<RequestImpl> > waiting_requests_; 120 std::vector<base::WeakPtr<RequestImpl> > waiting_requests_;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 expiration_date_ = expiration_date; 190 expiration_date_ = expiration_date;
203 191
204 // Subclasses may override this method to skip caching in some cases, but 192 // 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. 193 // we still inform all waiting Consumers of a successful token fetch below.
206 // This is intentional -- some consumers may need the token for cleanup 194 // This is intentional -- some consumers may need the token for cleanup
207 // tasks. https://chromiumcodereview.appspot.com/11312124/ 195 // tasks. https://chromiumcodereview.appspot.com/11312124/
208 oauth2_token_service_->RegisterCacheEntry(refresh_token_, 196 oauth2_token_service_->RegisterCacheEntry(refresh_token_,
209 scopes_, 197 scopes_,
210 access_token_, 198 access_token_,
211 expiration_date_); 199 expiration_date_);
212 // Deregisters itself from the service to prevent more waiting requests to 200 InformWaitingRequestsAndDelete();
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 } 201 }
218 202
219 void OAuth2TokenService::Fetcher::OnGetTokenFailure( 203 void OAuth2TokenService::Fetcher::OnGetTokenFailure(
220 const GoogleServiceAuthError& error) { 204 const GoogleServiceAuthError& error) {
221 fetcher_.reset(); 205 fetcher_.reset();
222 206
223 if (ShouldRetry(error) && retry_number_ < kMaxFetchRetryNum) { 207 if (ShouldRetry(error) && retry_number_ < max_fetch_retry_num_) {
224 int64 backoff = ComputeExponentialBackOffMilliseconds(retry_number_); 208 int64 backoff = ComputeExponentialBackOffMilliseconds(retry_number_);
225 ++retry_number_; 209 ++retry_number_;
226 retry_timer_.Stop(); 210 retry_timer_.Stop();
227 retry_timer_.Start(FROM_HERE, 211 retry_timer_.Start(FROM_HERE,
228 base::TimeDelta::FromMilliseconds(backoff), 212 base::TimeDelta::FromMilliseconds(backoff),
229 this, 213 this,
230 &OAuth2TokenService::Fetcher::Start); 214 &OAuth2TokenService::Fetcher::Start);
231 return; 215 return;
232 } 216 }
233 217
234 // Fetch completes.
235 error_ = error; 218 error_ = error;
219 InformWaitingRequestsAndDelete();
220 }
236 221
237 // Deregisters itself from the service to prevent more waiting requests to be 222 // Returns an exponential backoff in milliseconds including randomness less than
238 // added when it calls back the waiting requests. 223 // 1000 ms when retrying fetching an OAuth2 access token.
239 oauth2_token_service_->OnFetchComplete(this); 224 int64 OAuth2TokenService::Fetcher::ComputeExponentialBackOffMilliseconds(
240 InformWaitingRequests(); 225 int retry_num) {
241 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); 226 DCHECK(retry_num < max_fetch_retry_num_);
227 int64 exponential_backoff_in_seconds = 1 << retry_num;
228 // Returns a backoff with randomness < 1000ms
229 return (exponential_backoff_in_seconds + base::RandDouble()) * 1000;
242 } 230 }
243 231
244 // static 232 // static
245 bool OAuth2TokenService::Fetcher::ShouldRetry( 233 bool OAuth2TokenService::Fetcher::ShouldRetry(
246 const GoogleServiceAuthError& error) { 234 const GoogleServiceAuthError& error) {
247 GoogleServiceAuthError::State error_state = error.state(); 235 GoogleServiceAuthError::State error_state = error.state();
248 return error_state == GoogleServiceAuthError::CONNECTION_FAILED || 236 return error_state == GoogleServiceAuthError::CONNECTION_FAILED ||
249 error_state == GoogleServiceAuthError::REQUEST_CANCELED || 237 error_state == GoogleServiceAuthError::REQUEST_CANCELED ||
250 error_state == GoogleServiceAuthError::SERVICE_UNAVAILABLE; 238 error_state == GoogleServiceAuthError::SERVICE_UNAVAILABLE;
251 } 239 }
252 240
253 void OAuth2TokenService::Fetcher::InformWaitingRequests() { 241 void OAuth2TokenService::Fetcher::InformWaitingRequests() {
254 std::vector<base::WeakPtr<RequestImpl> >::const_iterator iter = 242 std::vector<base::WeakPtr<RequestImpl> >::const_iterator iter =
255 waiting_requests_.begin(); 243 waiting_requests_.begin();
256 for (; iter != waiting_requests_.end(); ++iter) { 244 for (; iter != waiting_requests_.end(); ++iter) {
257 base::WeakPtr<RequestImpl> waiting_request = *iter; 245 base::WeakPtr<RequestImpl> waiting_request = *iter;
258 if (waiting_request.get()) 246 if (waiting_request.get())
259 waiting_request->InformConsumer(error_, access_token_, expiration_date_); 247 waiting_request->InformConsumer(error_, access_token_, expiration_date_);
260 } 248 }
261 waiting_requests_.clear(); 249 waiting_requests_.clear();
262 } 250 }
263 251
252 void OAuth2TokenService::Fetcher::InformWaitingRequestsAndDelete() {
253 // Deregisters itself from the service to prevent more waiting requests to
254 // be added when it calls back the waiting requests.
255 oauth2_token_service_->OnFetchComplete(this);
256 InformWaitingRequests();
257 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
258 }
259
264 void OAuth2TokenService::Fetcher::AddWaitingRequest( 260 void OAuth2TokenService::Fetcher::AddWaitingRequest(
265 base::WeakPtr<OAuth2TokenService::RequestImpl> waiting_request) { 261 base::WeakPtr<OAuth2TokenService::RequestImpl> waiting_request) {
266 waiting_requests_.push_back(waiting_request); 262 waiting_requests_.push_back(waiting_request);
267 } 263 }
268 264
269 const OAuth2TokenService::ScopeSet& OAuth2TokenService::Fetcher::GetScopeSet() 265 const OAuth2TokenService::ScopeSet& OAuth2TokenService::Fetcher::GetScopeSet()
270 const { 266 const {
271 return scopes_; 267 return scopes_;
272 } 268 }
273 269
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 GoogleServiceAuthError( 314 GoogleServiceAuthError(
319 GoogleServiceAuthError::USER_NOT_SIGNED_UP), 315 GoogleServiceAuthError::USER_NOT_SIGNED_UP),
320 std::string(), 316 std::string(),
321 base::Time())); 317 base::Time()));
322 return request.PassAs<Request>(); 318 return request.PassAs<Request>();
323 } 319 }
324 320
325 if (HasCacheEntry(scopes)) 321 if (HasCacheEntry(scopes))
326 return StartCacheLookupRequest(scopes, consumer); 322 return StartCacheLookupRequest(scopes, consumer);
327 323
328 // Makes sure there is a pending fetcher for |scopes| and |refresh_token|. 324 // 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| 325 // simply register this |request| for those results rather than starting
330 // will be called back when this fetcher finishes fetching. 326 // a new fetcher.
331 FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes); 327 FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes);
332 std::map<FetchParameters, Fetcher*>::iterator iter = 328 std::map<FetchParameters, Fetcher*>::iterator iter =
333 pending_fetchers_.find(fetch_parameters); 329 pending_fetchers_.find(fetch_parameters);
334 if (iter != pending_fetchers_.end()) { 330 if (iter != pending_fetchers_.end()) {
335 iter->second->AddWaitingRequest(request->AsWeakPtr()); 331 iter->second->AddWaitingRequest(request->AsWeakPtr());
336 return request.PassAs<Request>(); 332 return request.PassAs<Request>();
337 } 333 }
334
338 pending_fetchers_[fetch_parameters] = 335 pending_fetchers_[fetch_parameters] =
339 Fetcher::CreateAndStart(this, 336 Fetcher::CreateAndStart(this,
340 request_context_getter_.get(), 337 request_context_getter_.get(),
341 refresh_token, 338 refresh_token,
342 scopes, 339 scopes,
343 request->AsWeakPtr()); 340 request->AsWeakPtr());
344 return request.PassAs<Request>(); 341 return request.PassAs<Request>();
345 } 342 }
346 343
347 scoped_ptr<OAuth2TokenService::Request> 344 scoped_ptr<OAuth2TokenService::Request>
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 } 453 }
457 454
458 void OAuth2TokenService::ClearCache() { 455 void OAuth2TokenService::ClearCache() {
459 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 456 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
460 token_cache_.clear(); 457 token_cache_.clear();
461 } 458 }
462 459
463 int OAuth2TokenService::cache_size_for_testing() const { 460 int OAuth2TokenService::cache_size_for_testing() const {
464 return token_cache_.size(); 461 return token_cache_.size();
465 } 462 }
463
464 void OAuth2TokenService::set_max_authorization_token_fetch_retries_for_testing(
465 int max_retries) {
466 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
467 max_fetch_retry_num_ = max_retries;
468 }
OLDNEW
« no previous file with comments | « chrome/browser/signin/oauth2_token_service.h ('k') | chrome/browser/signin/ubertoken_fetcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698