| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "blimp/client/core/session/identity_source.h" | 5 #include "blimp/client/core/session/identity_source.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 8 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 9 #include "blimp/client/core/switches/blimp_client_switches.h" | 11 #include "blimp/client/core/switches/blimp_client_switches.h" |
| 10 | 12 |
| 11 namespace blimp { | 13 namespace blimp { |
| 12 namespace client { | 14 namespace client { |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 // OAuth2 token scope. | 17 // OAuth2 token scope. |
| 16 const char kOAuth2TokenScope[] = | 18 const char kOAuth2TokenScope[] = |
| 17 "https://www.googleapis.com/auth/userinfo.email"; | 19 "https://www.googleapis.com/auth/userinfo.email"; |
| 18 | 20 |
| 19 // Max retry times when OAuth2 token request is canceled. | 21 // Max retry times when OAuth2 token request is canceled. |
| 20 const int kTokenRequestCancelMaxRetry = 3; | 22 const int kTokenRequestCancelMaxRetry = 3; |
| 21 } // namespace | 23 } // namespace |
| 22 | 24 |
| 23 IdentitySource::IdentitySource(BlimpClientContextDelegate* delegate, | 25 IdentitySource::IdentitySource( |
| 24 const TokenCallback& callback) | 26 std::unique_ptr<IdentityProvider> identity_provider, |
| 27 const base::Callback<void(const GoogleServiceAuthError&)>& error_callback, |
| 28 const TokenCallback& callback) |
| 25 : OAuth2TokenService::Consumer("blimp_client"), | 29 : OAuth2TokenService::Consumer("blimp_client"), |
| 30 identity_provider_(std::move(identity_provider)), |
| 31 error_callback_(error_callback), |
| 26 token_callback_(callback), | 32 token_callback_(callback), |
| 27 is_fetching_token_(false), | 33 is_fetching_token_(false), |
| 28 retry_times_(0), | 34 retry_times_(0) { |
| 29 delegate_(delegate) { | |
| 30 DCHECK(delegate_); | |
| 31 | |
| 32 // Create identity provider. | |
| 33 identity_provider_ = delegate_->CreateIdentityProvider(); | |
| 34 DCHECK(identity_provider_.get()); | 35 DCHECK(identity_provider_.get()); |
| 35 identity_provider_->AddObserver(this); | 36 identity_provider_->AddObserver(this); |
| 36 } | 37 } |
| 37 | 38 |
| 38 IdentitySource::~IdentitySource() { | 39 IdentitySource::~IdentitySource() { |
| 39 identity_provider_->RemoveActiveAccountRefreshTokenObserver(this); | 40 identity_provider_->RemoveActiveAccountRefreshTokenObserver(this); |
| 40 identity_provider_->RemoveObserver(this); | 41 identity_provider_->RemoveObserver(this); |
| 41 } | 42 } |
| 42 | 43 |
| 43 void IdentitySource::Connect() { | 44 void IdentitySource::Connect() { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 << retry_times_; | 109 << retry_times_; |
| 109 FetchAuthToken(); | 110 FetchAuthToken(); |
| 110 return; | 111 return; |
| 111 } | 112 } |
| 112 | 113 |
| 113 // If request failure was not caused by cancellation, or reached max retry | 114 // If request failure was not caused by cancellation, or reached max retry |
| 114 // times on request cancellation, propagate the error to embedder. | 115 // times on request cancellation, propagate the error to embedder. |
| 115 is_fetching_token_ = false; | 116 is_fetching_token_ = false; |
| 116 retry_times_ = 0; | 117 retry_times_ = 0; |
| 117 VLOG(1) << "OAuth2 token error: " << error.state(); | 118 VLOG(1) << "OAuth2 token error: " << error.state(); |
| 118 | 119 error_callback_.Run(error); |
| 119 // Propagate the error. | |
| 120 DCHECK(delegate_); | |
| 121 delegate_->OnAuthenticationError(error); | |
| 122 } | 120 } |
| 123 | 121 |
| 124 void IdentitySource::OnRefreshTokenAvailable(const std::string& account_id) { | 122 void IdentitySource::OnRefreshTokenAvailable(const std::string& account_id) { |
| 125 if (account_id != account_id_) { | 123 if (account_id != account_id_) { |
| 126 return; | 124 return; |
| 127 } | 125 } |
| 128 | 126 |
| 129 identity_provider_->RemoveActiveAccountRefreshTokenObserver(this); | 127 identity_provider_->RemoveActiveAccountRefreshTokenObserver(this); |
| 130 FetchAuthToken(); | 128 FetchAuthToken(); |
| 131 } | 129 } |
| 132 | 130 |
| 133 void IdentitySource::FetchAuthToken() { | 131 void IdentitySource::FetchAuthToken() { |
| 134 OAuth2TokenService* token_service = identity_provider_->GetTokenService(); | 132 OAuth2TokenService* token_service = identity_provider_->GetTokenService(); |
| 135 DCHECK(token_service); | 133 DCHECK(token_service); |
| 136 | 134 |
| 137 if (token_service->RefreshTokenIsAvailable(account_id_)) { | 135 if (token_service->RefreshTokenIsAvailable(account_id_)) { |
| 138 OAuth2TokenService::ScopeSet scopes; | 136 OAuth2TokenService::ScopeSet scopes; |
| 139 scopes.insert(kOAuth2TokenScope); | 137 scopes.insert(kOAuth2TokenScope); |
| 140 token_request_ = token_service->StartRequest(account_id_, scopes, this); | 138 token_request_ = token_service->StartRequest(account_id_, scopes, this); |
| 141 } else { | 139 } else { |
| 142 identity_provider_->AddActiveAccountRefreshTokenObserver(this); | 140 identity_provider_->AddActiveAccountRefreshTokenObserver(this); |
| 143 } | 141 } |
| 144 } | 142 } |
| 145 | 143 |
| 146 } // namespace client | 144 } // namespace client |
| 147 } // namespace blimp | 145 } // namespace blimp |
| OLD | NEW |