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/gaia_oauth_client.h"
7 #include "base/callback_helpers.h"
8 #include "base/logging.h"
11 const int kMaxGaiaRetries
= 3;
16 GaiaOAuthClient::GaiaOAuthClient(
17 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
)
18 : gaia_oauth_client_(url_request_context_getter
.get()) {
21 GaiaOAuthClient::~GaiaOAuthClient() {
24 void GaiaOAuthClient::GetCredentialsFromAuthCode(
25 const gaia::OAuthClientInfo
& oauth_client_info
,
26 const std::string
& auth_code
,
28 CompletionCallback on_done
) {
29 if (!on_done_
.is_null()) {
30 pending_requests_
.push(
31 Request(oauth_client_info
, auth_code
, need_user_email
, on_done
));
35 need_user_email_
= need_user_email
;
37 // Map the authorization code to refresh and access tokens.
38 gaia_oauth_client_
.GetTokensFromAuthCode(oauth_client_info
, auth_code
,
39 kMaxGaiaRetries
, this);
42 void GaiaOAuthClient::OnGetTokensResponse(const std::string
& refresh_token
,
43 const std::string
& access_token
,
44 int expires_in_seconds
) {
45 refresh_token_
= refresh_token
;
46 if (need_user_email_
) {
47 // Get the email corresponding to the access token.
48 gaia_oauth_client_
.GetUserEmail(access_token
, kMaxGaiaRetries
, this);
50 SendResponse("", refresh_token_
);
54 void GaiaOAuthClient::OnRefreshTokenResponse(const std::string
& access_token
,
55 int expires_in_seconds
) {
56 // We never request a refresh token, so this call is not expected.
60 void GaiaOAuthClient::SendResponse(const std::string
& user_email
,
61 const std::string
& refresh_token
) {
62 base::ResetAndReturn(&on_done_
).Run(user_email
, refresh_token
);
64 // Process the next request in the queue.
65 if (pending_requests_
.size()) {
66 Request request
= pending_requests_
.front();
67 pending_requests_
.pop();
68 // GetCredentialsFromAuthCode is asynchronous, so it's safe to call it here.
69 GetCredentialsFromAuthCode(request
.oauth_client_info
, request
.auth_code
,
70 request
.need_user_email
, request
.on_done
);
74 void GaiaOAuthClient::OnGetUserEmailResponse(const std::string
& user_email
) {
75 SendResponse(user_email
, refresh_token_
);
78 void GaiaOAuthClient::OnOAuthError() {
82 void GaiaOAuthClient::OnNetworkError(int response_code
) {
86 GaiaOAuthClient::Request::Request(
87 const gaia::OAuthClientInfo
& oauth_client_info
,
88 const std::string
& auth_code
,
90 CompletionCallback on_done
) {
91 this->oauth_client_info
= oauth_client_info
;
92 this->auth_code
= auth_code
;
93 this->need_user_email
= need_user_email
;
94 this->on_done
= on_done
;
97 GaiaOAuthClient::Request::~Request() {
100 } // namespace remoting