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