Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "chrome/browser/profiles/profile.h" | |
| 8 #include "chrome/browser/signin/force_signin_verifier.h" | |
| 9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 10 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 11 #include "components/signin/core/browser/signin_manager.h" | |
| 12 #include "google_apis/gaia/gaia_constants.h" | |
| 13 | |
| 14 namespace { | |
| 15 const net::BackoffEntry::Policy kBackoffPolicy = { | |
| 16 0, // Number of initial errors to ignore before applying | |
| 17 // exponential back-off rules. | |
| 18 2000, // Initial delay in ms. | |
| 19 2, // Factor by which the waiting time will be multiplied. | |
| 20 0.2, // Fuzzing percentage. | |
| 21 4 * 60 * 1000, // Maximum amount of time to delay th request in ms. | |
| 22 -1, // Never discard the entry. | |
| 23 false // Do not always use initial delay. | |
| 24 }; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 ForceSigninVerifier::ForceSigninVerifier(Profile* profile) | |
| 29 : OAuth2TokenService::Consumer("force_signin_verifier"), | |
| 30 has_token_verified_(false), | |
| 31 backoff_entry_(&kBackoffPolicy), | |
| 32 token_request_time_(base::Time::Now()), | |
| 33 oauth2_token_service_( | |
| 34 ProfileOAuth2TokenServiceFactory::GetForProfile(profile)), | |
| 35 signin_manager_(SigninManagerFactory::GetForProfile(profile)) { | |
| 36 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | |
| 37 SendRequest(); | |
| 38 } | |
| 39 | |
| 40 ForceSigninVerifier::~ForceSigninVerifier() { | |
| 41 Cancel(); | |
| 42 } | |
| 43 | |
| 44 bool ForceSigninVerifier::ShouldSendRequest() { | |
| 45 return !has_token_verified_ && access_token_request_.get() == nullptr && | |
| 46 !net::NetworkChangeNotifier::IsOffline() && | |
| 47 signin_manager_->IsAuthenticated(); | |
| 48 } | |
| 49 | |
| 50 void ForceSigninVerifier::SendRequest() { | |
| 51 if (!ShouldSendRequest()) | |
| 52 return; | |
| 53 | |
| 54 std::string account_id = signin_manager_->GetAuthenticatedAccountId(); | |
| 55 OAuth2TokenService::ScopeSet oauth2_scopes; | |
| 56 oauth2_scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); | |
| 57 access_token_request_ = | |
| 58 oauth2_token_service_->StartRequest(account_id, oauth2_scopes, this); | |
| 59 } | |
| 60 | |
| 61 void ForceSigninVerifier::OnGetTokenSuccess( | |
| 62 const OAuth2TokenService::Request* request, | |
| 63 const std::string& access_token, | |
| 64 const base::Time& expiration_time) { | |
| 65 has_token_verified_ = true; | |
| 66 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | |
| 67 access_token_request_.reset(); | |
|
Roger Tawa OOO till Jul 10th
2017/04/10 15:15:20
Would it be better to call Cancel() instead of lin
zmin
2017/04/10 22:27:49
Done.
| |
| 68 } | |
| 69 | |
| 70 void ForceSigninVerifier::OnGetTokenFailure( | |
| 71 const OAuth2TokenService::Request* request, | |
| 72 const GoogleServiceAuthError& error) { | |
| 73 if (error.IsPersistentError()) { | |
| 74 has_token_verified_ = true; | |
| 75 ShowDialog(); | |
| 76 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | |
| 77 } else { | |
| 78 backoff_entry_.InformOfRequest(false); | |
| 79 backoff_request_timer_.Start( | |
| 80 FROM_HERE, backoff_entry_.GetTimeUntilRelease(), | |
| 81 base::Bind(&ForceSigninVerifier::SendRequest, base::Unretained(this))); | |
| 82 } | |
| 83 access_token_request_.reset(); | |
| 84 } | |
| 85 | |
| 86 void ForceSigninVerifier::OnNetworkChanged( | |
| 87 net::NetworkChangeNotifier::ConnectionType type) { | |
| 88 // Try again immediately once the network is back and cancel any pending | |
| 89 // request. | |
| 90 backoff_entry_.Reset(); | |
| 91 if (backoff_request_timer_.IsRunning()) | |
| 92 backoff_request_timer_.Stop(); | |
| 93 | |
| 94 if (type != net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE) | |
| 95 SendRequest(); | |
| 96 } | |
| 97 | |
| 98 void ForceSigninVerifier::Cancel() { | |
| 99 backoff_entry_.Reset(); | |
| 100 backoff_request_timer_.Stop(); | |
| 101 access_token_request_.reset(); | |
| 102 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | |
| 103 } | |
| 104 | |
| 105 void ForceSigninVerifier::ShowDialog() { | |
| 106 // TODO(zmin): Show app modal dialog. | |
| 107 } | |
| OLD | NEW |