Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
|
Lambros
2015/04/24 18:15:50
Rename this to gaia_oauth_client.cc (see other com
John Williams
2015/04/24 21:05:13
Done.
| |
| 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 "remoting/host/setup/oauth_client.h" | 5 #include "remoting/host/setup/oauth_client.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace { | 9 namespace { |
| 10 const int kMaxGaiaRetries = 3; | 10 const int kMaxGaiaRetries = 3; |
| 11 } // namespace | 11 } // namespace |
| 12 | 12 |
| 13 namespace remoting { | 13 namespace remoting { |
| 14 | 14 |
| 15 OAuthClient::OAuthClient( | 15 OAuthClient::~OAuthClient() { |
| 16 } | |
| 17 | |
| 18 GaiaOAuthClient::GaiaOAuthClient( | |
| 16 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) | 19 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) |
| 17 : gaia_oauth_client_(url_request_context_getter.get()) { | 20 : gaia_oauth_client_(url_request_context_getter.get()) { |
| 18 } | 21 } |
| 19 | 22 |
| 20 OAuthClient::~OAuthClient() { | 23 GaiaOAuthClient::~GaiaOAuthClient() { |
| 21 } | 24 } |
| 22 | 25 |
| 23 void OAuthClient::GetCredentialsFromAuthCode( | 26 void GaiaOAuthClient::GetCredentialsFromAuthCode( |
| 24 const gaia::OAuthClientInfo& oauth_client_info, | 27 const gaia::OAuthClientInfo& oauth_client_info, |
| 25 const std::string& auth_code, | 28 const std::string& auth_code, |
| 26 CompletionCallback on_done) { | 29 CompletionCallback on_done) { |
| 27 | 30 |
| 28 if (!on_done_.is_null()) { | 31 if (!on_done_.is_null()) { |
| 29 pending_requests_.push(Request(oauth_client_info, auth_code, on_done)); | 32 pending_requests_.push(Request(oauth_client_info, auth_code, on_done)); |
| 30 return; | 33 return; |
| 31 } | 34 } |
| 32 | 35 |
| 33 on_done_ = on_done; | 36 on_done_ = on_done; |
| 34 // Map the authorization code to refresh and access tokens. | 37 // Map the authorization code to refresh and access tokens. |
| 35 gaia_oauth_client_.GetTokensFromAuthCode(oauth_client_info, auth_code, | 38 gaia_oauth_client_.GetTokensFromAuthCode(oauth_client_info, auth_code, |
| 36 kMaxGaiaRetries, this); | 39 kMaxGaiaRetries, this); |
| 37 } | 40 } |
| 38 | 41 |
| 39 void OAuthClient::OnGetTokensResponse( | 42 void GaiaOAuthClient::OnGetTokensResponse( |
| 40 const std::string& refresh_token, | 43 const std::string& refresh_token, |
| 41 const std::string& access_token, | 44 const std::string& access_token, |
| 42 int expires_in_seconds) { | 45 int expires_in_seconds) { |
| 43 refresh_token_ = refresh_token; | 46 refresh_token_ = refresh_token; |
| 44 // Get the email corresponding to the access token. | 47 // Get the email corresponding to the access token. |
| 45 gaia_oauth_client_.GetUserEmail(access_token, kMaxGaiaRetries, this); | 48 gaia_oauth_client_.GetUserEmail(access_token, kMaxGaiaRetries, this); |
| 46 } | 49 } |
| 47 | 50 |
| 48 void OAuthClient::OnRefreshTokenResponse( | 51 void GaiaOAuthClient::OnRefreshTokenResponse( |
| 49 const std::string& access_token, | 52 const std::string& access_token, |
| 50 int expires_in_seconds) { | 53 int expires_in_seconds) { |
| 51 // We never request a refresh token, so this call is not expected. | 54 // We never request a refresh token, so this call is not expected. |
| 52 NOTREACHED(); | 55 NOTREACHED(); |
| 53 } | 56 } |
| 54 | 57 |
| 55 void OAuthClient::SendResponse(const std::string& user_email, | 58 void GaiaOAuthClient::SendResponse( |
| 56 const std::string& refresh_token) { | 59 const std::string& user_email, |
| 60 const std::string& refresh_token) { | |
| 57 CompletionCallback on_done = on_done_; | 61 CompletionCallback on_done = on_done_; |
| 58 on_done_.Reset(); | 62 on_done_.Reset(); |
| 59 on_done.Run(user_email, refresh_token); | 63 on_done.Run(user_email, refresh_token); |
| 60 | 64 |
| 61 // Process the next request in the queue. | 65 // Process the next request in the queue. |
| 62 if (pending_requests_.size()) { | 66 if (pending_requests_.size()) { |
| 63 Request request = pending_requests_.front(); | 67 Request request = pending_requests_.front(); |
| 64 pending_requests_.pop(); | 68 pending_requests_.pop(); |
| 65 // GetCredentialsFromAuthCode is asynchronous, so it's safe to call it here. | 69 // GetCredentialsFromAuthCode is asynchronous, so it's safe to call it here. |
| 66 GetCredentialsFromAuthCode( | 70 GetCredentialsFromAuthCode( |
| 67 request.oauth_client_info, request.auth_code, request.on_done); | 71 request.oauth_client_info, request.auth_code, request.on_done); |
| 68 } | 72 } |
| 69 } | 73 } |
| 70 | 74 |
| 71 void OAuthClient::OnGetUserEmailResponse(const std::string& user_email) { | 75 void GaiaOAuthClient::OnGetUserEmailResponse(const std::string& user_email) { |
| 72 SendResponse(user_email, refresh_token_); | 76 SendResponse(user_email, refresh_token_); |
| 73 } | 77 } |
| 74 | 78 |
| 75 void OAuthClient::OnOAuthError() { | 79 void GaiaOAuthClient::OnOAuthError() { |
| 76 SendResponse("", ""); | 80 SendResponse("", ""); |
| 77 } | 81 } |
| 78 | 82 |
| 79 void OAuthClient::OnNetworkError(int response_code) { | 83 void GaiaOAuthClient::OnNetworkError(int response_code) { |
| 80 SendResponse("", ""); | 84 SendResponse("", ""); |
| 81 } | 85 } |
| 82 | 86 |
| 83 OAuthClient::Request::Request( | 87 GaiaOAuthClient::Request::Request( |
| 84 const gaia::OAuthClientInfo& oauth_client_info, | 88 const gaia::OAuthClientInfo& oauth_client_info, |
| 85 const std::string& auth_code, | 89 const std::string& auth_code, |
| 86 CompletionCallback on_done) { | 90 CompletionCallback on_done) { |
| 87 this->oauth_client_info = oauth_client_info; | 91 this->oauth_client_info = oauth_client_info; |
| 88 this->auth_code = auth_code; | 92 this->auth_code = auth_code; |
| 89 this->on_done = on_done; | 93 this->on_done = on_done; |
| 90 } | 94 } |
| 91 | 95 |
| 92 OAuthClient::Request::~Request() { | 96 GaiaOAuthClient::Request::~Request() { |
| 93 } | 97 } |
| 94 | 98 |
| 95 } // namespace remoting | 99 } // namespace remoting |
| OLD | NEW |