Home | History | Annotate | Download | only in setup
      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.
      4 
      5 #include "remoting/host/setup/oauth_client.h"
      6 
      7 namespace {
      8 const int kMaxGaiaRetries = 3;
      9 }  // namespace
     10 
     11 namespace remoting {
     12 
     13 OAuthClient::OAuthClient(
     14     scoped_refptr<net::URLRequestContextGetter> url_request_context_getter)
     15     : gaia_oauth_client_(url_request_context_getter) {
     16 }
     17 
     18 OAuthClient::~OAuthClient() {
     19 }
     20 
     21 void OAuthClient::GetCredentialsFromAuthCode(
     22     const gaia::OAuthClientInfo& oauth_client_info,
     23     const std::string& auth_code,
     24     CompletionCallback on_done) {
     25 
     26   if (!on_done_.is_null()) {
     27     pending_requests_.push(Request(oauth_client_info, auth_code, on_done));
     28     return;
     29   }
     30 
     31   on_done_ = 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);
     35 }
     36 
     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);
     44 }
     45 
     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.
     50   NOTREACHED();
     51 }
     52 
     53 void OAuthClient::SendResponse(const std::string& user_email,
     54                                const std::string& refresh_token) {
     55   CompletionCallback on_done = on_done_;
     56   on_done_.Reset();
     57   on_done.Run(user_email, refresh_token);
     58 
     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);
     66   }
     67 }
     68 
     69 void OAuthClient::OnGetUserEmailResponse(const std::string& user_email) {
     70   SendResponse(user_email, refresh_token_);
     71 }
     72 
     73 void OAuthClient::OnOAuthError() {
     74   SendResponse("", "");
     75 }
     76 
     77 void OAuthClient::OnNetworkError(int response_code) {
     78   SendResponse("", "");
     79 }
     80 
     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;
     88 }
     89 
     90 OAuthClient::Request::~Request() {
     91 }
     92 
     93 }  // namespace remoting
     94