| 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/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 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) | 16 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) |
| 17 : gaia_oauth_client_(url_request_context_getter.get()) { | 17 : gaia_oauth_client_(url_request_context_getter.get()) { |
| 18 } | 18 } |
| 19 | 19 |
| 20 OAuthClient::~OAuthClient() { | 20 OAuthClient::~OAuthClient() { |
| 21 } | 21 } |
| 22 | 22 |
| 23 void OAuthClient::GetCredentialsFromAuthCode( | 23 void OAuthClient::GetCredentialsFromAuthCode( |
| 24 const gaia::OAuthClientInfo& oauth_client_info, | 24 const gaia::OAuthClientInfo& oauth_client_info, |
| 25 const std::string& auth_code, | 25 const std::string& auth_code, |
| 26 bool need_user_email, |
| 26 CompletionCallback on_done) { | 27 CompletionCallback on_done) { |
| 27 | 28 |
| 28 if (!on_done_.is_null()) { | 29 if (!on_done_.is_null()) { |
| 29 pending_requests_.push(Request(oauth_client_info, auth_code, on_done)); | 30 pending_requests_.push( |
| 31 Request(oauth_client_info, auth_code, need_user_email, on_done)); |
| 30 return; | 32 return; |
| 31 } | 33 } |
| 32 | 34 |
| 35 need_user_email_ = need_user_email; |
| 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 OAuthClient::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 if (need_user_email_) { |
| 45 gaia_oauth_client_.GetUserEmail(access_token, kMaxGaiaRetries, this); | 48 // Get the email corresponding to the access token. |
| 49 gaia_oauth_client_.GetUserEmail(access_token, kMaxGaiaRetries, this); |
| 50 } else { |
| 51 SendResponse("", refresh_token_); |
| 52 } |
| 46 } | 53 } |
| 47 | 54 |
| 48 void OAuthClient::OnRefreshTokenResponse( | 55 void OAuthClient::OnRefreshTokenResponse( |
| 49 const std::string& access_token, | 56 const std::string& access_token, |
| 50 int expires_in_seconds) { | 57 int expires_in_seconds) { |
| 51 // We never request a refresh token, so this call is not expected. | 58 // We never request a refresh token, so this call is not expected. |
| 52 NOTREACHED(); | 59 NOTREACHED(); |
| 53 } | 60 } |
| 54 | 61 |
| 55 void OAuthClient::SendResponse(const std::string& user_email, | 62 void OAuthClient::SendResponse(const std::string& user_email, |
| 56 const std::string& refresh_token) { | 63 const std::string& refresh_token) { |
| 57 CompletionCallback on_done = on_done_; | 64 CompletionCallback on_done = on_done_; |
| 58 on_done_.Reset(); | 65 on_done_.Reset(); |
| 59 on_done.Run(user_email, refresh_token); | 66 on_done.Run(user_email, refresh_token); |
| 60 | 67 |
| 61 // Process the next request in the queue. | 68 // Process the next request in the queue. |
| 62 if (pending_requests_.size()) { | 69 if (pending_requests_.size()) { |
| 63 Request request = pending_requests_.front(); | 70 Request request = pending_requests_.front(); |
| 64 pending_requests_.pop(); | 71 pending_requests_.pop(); |
| 65 // GetCredentialsFromAuthCode is asynchronous, so it's safe to call it here. | 72 // GetCredentialsFromAuthCode is asynchronous, so it's safe to call it here. |
| 66 GetCredentialsFromAuthCode( | 73 GetCredentialsFromAuthCode( |
| 67 request.oauth_client_info, request.auth_code, request.on_done); | 74 request.oauth_client_info, |
| 75 request.auth_code, |
| 76 request.need_user_email, |
| 77 request.on_done); |
| 68 } | 78 } |
| 69 } | 79 } |
| 70 | 80 |
| 71 void OAuthClient::OnGetUserEmailResponse(const std::string& user_email) { | 81 void OAuthClient::OnGetUserEmailResponse(const std::string& user_email) { |
| 72 SendResponse(user_email, refresh_token_); | 82 SendResponse(user_email, refresh_token_); |
| 73 } | 83 } |
| 74 | 84 |
| 75 void OAuthClient::OnOAuthError() { | 85 void OAuthClient::OnOAuthError() { |
| 76 SendResponse("", ""); | 86 SendResponse("", ""); |
| 77 } | 87 } |
| 78 | 88 |
| 79 void OAuthClient::OnNetworkError(int response_code) { | 89 void OAuthClient::OnNetworkError(int response_code) { |
| 80 SendResponse("", ""); | 90 SendResponse("", ""); |
| 81 } | 91 } |
| 82 | 92 |
| 83 OAuthClient::Request::Request( | 93 OAuthClient::Request::Request( |
| 84 const gaia::OAuthClientInfo& oauth_client_info, | 94 const gaia::OAuthClientInfo& oauth_client_info, |
| 85 const std::string& auth_code, | 95 const std::string& auth_code, |
| 96 bool need_user_email, |
| 86 CompletionCallback on_done) { | 97 CompletionCallback on_done) { |
| 87 this->oauth_client_info = oauth_client_info; | 98 this->oauth_client_info = oauth_client_info; |
| 88 this->auth_code = auth_code; | 99 this->auth_code = auth_code; |
| 100 this->need_user_email = need_user_email; |
| 89 this->on_done = on_done; | 101 this->on_done = on_done; |
| 90 } | 102 } |
| 91 | 103 |
| 92 OAuthClient::Request::~Request() { | 104 OAuthClient::Request::~Request() { |
| 93 } | 105 } |
| 94 | 106 |
| 95 } // namespace remoting | 107 } // namespace remoting |
| OLD | NEW |