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
3 // found in the LICENSE file.
5 #include "remoting/host/setup/oauth_client.h"
8 const int kMaxGaiaRetries
= 3;
13 OAuthClient::OAuthClient(
14 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
)
15 : gaia_oauth_client_(url_request_context_getter
) {
18 OAuthClient::~OAuthClient() {
21 void OAuthClient::GetCredentialsFromAuthCode(
22 const gaia::OAuthClientInfo
& oauth_client_info
,
23 const std::string
& auth_code
,
24 CompletionCallback on_done
) {
26 if (!on_done_
.is_null()) {
27 pending_requests_
.push(Request(oauth_client_info
, auth_code
, on_done
));
32 // Map the authorization code to refresh and access tokens.
33 gaia_oauth_client_
.GetTokensFromAuthCode(oauth_client_info
, auth_code
,
34 kMaxGaiaRetries
, this);
37 void OAuthClient::OnGetTokensResponse(
38 const std::string
& refresh_token
,
39 const std::string
& access_token
,
40 int expires_in_seconds
) {
41 refresh_token_
= refresh_token
;
42 // Get the email corresponding to the access token.
43 gaia_oauth_client_
.GetUserEmail(access_token
, kMaxGaiaRetries
, this);
46 void OAuthClient::OnRefreshTokenResponse(
47 const std::string
& access_token
,
48 int expires_in_seconds
) {
49 // We never request a refresh token, so this call is not expected.
53 void OAuthClient::SendResponse(const std::string
& user_email
,
54 const std::string
& refresh_token
) {
55 CompletionCallback on_done
= on_done_
;
57 on_done
.Run(user_email
, refresh_token
);
59 // Process the next request in the queue.
60 if (pending_requests_
.size()) {
61 Request request
= pending_requests_
.front();
62 pending_requests_
.pop();
63 // GetCredentialsFromAuthCode is asynchronous, so it's safe to call it here.
64 GetCredentialsFromAuthCode(
65 request
.oauth_client_info
, request
.auth_code
, request
.on_done
);
69 void OAuthClient::OnGetUserEmailResponse(const std::string
& user_email
) {
70 SendResponse(user_email
, refresh_token_
);
73 void OAuthClient::OnOAuthError() {
77 void OAuthClient::OnNetworkError(int response_code
) {
81 OAuthClient::Request::Request(
82 const gaia::OAuthClientInfo
& oauth_client_info
,
83 const std::string
& auth_code
,
84 CompletionCallback on_done
) {
85 this->oauth_client_info
= oauth_client_info
;
86 this->auth_code
= auth_code
;
87 this->on_done
= on_done
;
90 OAuthClient::Request::~Request() {
93 } // namespace remoting