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