diff options
Diffstat (limited to 'remoting/host/setup/oauth_client.h')
-rw-r--r-- | remoting/host/setup/oauth_client.h | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/remoting/host/setup/oauth_client.h b/remoting/host/setup/oauth_client.h new file mode 100644 index 0000000000..2fdcbfe426 --- /dev/null +++ b/remoting/host/setup/oauth_client.h @@ -0,0 +1,79 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef REMOTING_HOST_SETUP_OAUTH_CLIENT +#define REMOTING_HOST_SETUP_OAUTH_CLIENT + +#include <queue> +#include <string> + +#include "base/callback.h" +#include "google_apis/gaia/gaia_oauth_client.h" +#include "remoting/base/url_request_context.h" + +namespace remoting { + +// A wrapper around GaiaOAuthClient that provides a more convenient interface, +// with queueing of requests and a callback rather than a delegate. +class OAuthClient : public gaia::GaiaOAuthClient::Delegate { + public: + // Called when GetCredentialsFromAuthCode is completed, with the |user_email| + // and |refresh_token| that correspond to the given |auth_code|, or with empty + // strings on error. + typedef base::Callback<void( + const std::string& user_email, + const std::string& refresh_token)> CompletionCallback; + + OAuthClient( + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter); + + virtual ~OAuthClient(); + + // Redeems |auth_code| using |oauth_client_info| to obtain |refresh_token| and + // |access_token|, then uses the userinfo endpoint to obtain |user_email|. + // Calls CompletionCallback with |user_email| and |refresh_token| when done, + // or with empty strings on error. + // If a request is received while another one is being processed, it is + // enqueued and processed after the first one is finished. + void GetCredentialsFromAuthCode( + const gaia::OAuthClientInfo& oauth_client_info, + const std::string& auth_code, + CompletionCallback on_done); + + // gaia::GaiaOAuthClient::Delegate + virtual void OnGetTokensResponse(const std::string& refresh_token, + const std::string& access_token, + int expires_in_seconds) OVERRIDE; + virtual void OnRefreshTokenResponse(const std::string& access_token, + int expires_in_seconds) OVERRIDE; + virtual void OnGetUserEmailResponse(const std::string& user_email) OVERRIDE; + + virtual void OnOAuthError() OVERRIDE; + virtual void OnNetworkError(int response_code) OVERRIDE; + + private: + struct Request { + Request(const gaia::OAuthClientInfo& oauth_client_info, + const std::string& auth_code, + CompletionCallback on_done); + virtual ~Request(); + gaia::OAuthClientInfo oauth_client_info; + std::string auth_code; + CompletionCallback on_done; + }; + + void SendResponse(const std::string& user_email, + const std::string& refresh_token); + + std::queue<Request> pending_requests_; + gaia::GaiaOAuthClient gaia_oauth_client_; + std::string refresh_token_; + CompletionCallback on_done_; + + DISALLOW_COPY_AND_ASSIGN(OAuthClient); +}; + +} // namespace remoting + +#endif // REMOTING_HOST_SETUP_OAUTH_CLIENT |