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"
7 #include "base/logging.h"
10 const int kMaxGaiaRetries
= 3;
15 OAuthClient::OAuthClient(
16 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
)
17 : gaia_oauth_client_(url_request_context_getter
.get()) {
20 OAuthClient::~OAuthClient() {
23 void OAuthClient::GetCredentialsFromAuthCode(
24 const gaia::OAuthClientInfo
& oauth_client_info
,
25 const std::string
& auth_code
,
26 CompletionCallback on_done
) {
28 if (!on_done_
.is_null()) {
29 pending_requests_
.push(Request(oauth_client_info
, auth_code
, on_done
));
34 // Map the authorization code to refresh and access tokens.
35 gaia_oauth_client_
.GetTokensFromAuthCode(oauth_client_info
, auth_code
,
36 kMaxGaiaRetries
, this);
39 void OAuthClient::OnGetTokensResponse(
40 const std::string
& refresh_token
,
41 const std::string
& access_token
,
42 int expires_in_seconds
) {
43 refresh_token_
= refresh_token
;
44 // Get the email corresponding to the access token.
45 gaia_oauth_client_
.GetUserEmail(access_token
, kMaxGaiaRetries
, this);
48 void OAuthClient::OnRefreshTokenResponse(
49 const std::string
& access_token
,
50 int expires_in_seconds
) {
51 // We never request a refresh token, so this call is not expected.
55 void OAuthClient::SendResponse(const std::string
& user_email
,
56 const std::string
& refresh_token
) {
57 CompletionCallback on_done
= on_done_
;
59 on_done
.Run(user_email
, refresh_token
);
61 // Process the next request in the queue.
62 if (pending_requests_
.size()) {
63 Request request
= pending_requests_
.front();
64 pending_requests_
.pop();
65 // GetCredentialsFromAuthCode is asynchronous, so it's safe to call it here.
66 GetCredentialsFromAuthCode(
67 request
.oauth_client_info
, request
.auth_code
, request
.on_done
);
71 void OAuthClient::OnGetUserEmailResponse(const std::string
& user_email
) {
72 SendResponse(user_email
, refresh_token_
);
75 void OAuthClient::OnOAuthError() {
79 void OAuthClient::OnNetworkError(int response_code
) {
83 OAuthClient::Request::Request(
84 const gaia::OAuthClientInfo
& oauth_client_info
,
85 const std::string
& auth_code
,
86 CompletionCallback on_done
) {
87 this->oauth_client_info
= oauth_client_info
;
88 this->auth_code
= auth_code
;
89 this->on_done
= on_done
;
92 OAuthClient::Request::~Request() {
95 } // namespace remoting