Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / host / setup / gaia_oauth_client.cc
blob9855dd674ccd4213d884a1a57c1ca77736272509
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"
10 namespace {
11 const int kMaxGaiaRetries = 3;
12 } // namespace
14 namespace remoting {
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,
27 bool need_user_email,
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));
32 return;
35 need_user_email_ = need_user_email;
36 on_done_ = on_done;
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);
49 } else {
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.
57 NOTREACHED();
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() {
79 SendResponse("", "");
82 void GaiaOAuthClient::OnNetworkError(int response_code) {
83 SendResponse("", "");
86 GaiaOAuthClient::Request::Request(
87 const gaia::OAuthClientInfo& oauth_client_info,
88 const std::string& auth_code,
89 bool need_user_email,
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