blob: 79d1d9a9b5988ed73e80f53006f0584e7135648b [file] [log] [blame]
[email protected]6f5f90962013-08-16 21:28:481// 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#ifndef REMOTING_HOST_SETUP_OAUTH_CLIENT
6#define REMOTING_HOST_SETUP_OAUTH_CLIENT
7
8#include <queue>
9#include <string>
10
11#include "base/callback.h"
[email protected]305ed872014-07-08 02:33:5312#include "base/memory/ref_counted.h"
[email protected]6f5f90962013-08-16 21:28:4813#include "google_apis/gaia/gaia_oauth_client.h"
[email protected]305ed872014-07-08 02:33:5314#include "net/url_request/url_request_context_getter.h"
15
16namespace net {
17class URLRequestContext;
18}
[email protected]6f5f90962013-08-16 21:28:4819
20namespace remoting {
21
22// A wrapper around GaiaOAuthClient that provides a more convenient interface,
23// with queueing of requests and a callback rather than a delegate.
24class OAuthClient : public gaia::GaiaOAuthClient::Delegate {
25 public:
26 // Called when GetCredentialsFromAuthCode is completed, with the |user_email|
27 // and |refresh_token| that correspond to the given |auth_code|, or with empty
28 // strings on error.
29 typedef base::Callback<void(
30 const std::string& user_email,
31 const std::string& refresh_token)> CompletionCallback;
32
33 OAuthClient(
34 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter);
35
dcheng562aba52014-10-21 12:30:1436 ~OAuthClient() override;
[email protected]6f5f90962013-08-16 21:28:4837
jrwec9abae2015-04-24 21:57:0838 // Redeems |auth_code| using |oauth_client_info| to obtain
39 // |refresh_token| and |access_token|, then, if |need_user_email| is
40 // true, uses the userinfo endpoint to obtain |user_email|. Calls
41 // CompletionCallback with |user_email| and |refresh_token| when
42 // done, or with empty strings on error. If a request is received
43 // while another one is being processed, it is enqueued and
44 // processed after the first one is finished.
[email protected]6f5f90962013-08-16 21:28:4845 void GetCredentialsFromAuthCode(
46 const gaia::OAuthClientInfo& oauth_client_info,
47 const std::string& auth_code,
jrwec9abae2015-04-24 21:57:0848 bool need_user_email,
[email protected]6f5f90962013-08-16 21:28:4849 CompletionCallback on_done);
50
51 // gaia::GaiaOAuthClient::Delegate
dcheng562aba52014-10-21 12:30:1452 void OnGetTokensResponse(const std::string& refresh_token,
53 const std::string& access_token,
54 int expires_in_seconds) override;
55 void OnRefreshTokenResponse(const std::string& access_token,
56 int expires_in_seconds) override;
57 void OnGetUserEmailResponse(const std::string& user_email) override;
[email protected]6f5f90962013-08-16 21:28:4858
dcheng562aba52014-10-21 12:30:1459 void OnOAuthError() override;
60 void OnNetworkError(int response_code) override;
[email protected]6f5f90962013-08-16 21:28:4861
62 private:
63 struct Request {
64 Request(const gaia::OAuthClientInfo& oauth_client_info,
65 const std::string& auth_code,
jrwec9abae2015-04-24 21:57:0866 bool need_user_email,
[email protected]6f5f90962013-08-16 21:28:4867 CompletionCallback on_done);
68 virtual ~Request();
69 gaia::OAuthClientInfo oauth_client_info;
70 std::string auth_code;
jrwec9abae2015-04-24 21:57:0871 bool need_user_email;
[email protected]6f5f90962013-08-16 21:28:4872 CompletionCallback on_done;
73 };
74
75 void SendResponse(const std::string& user_email,
76 const std::string& refresh_token);
77
78 std::queue<Request> pending_requests_;
79 gaia::GaiaOAuthClient gaia_oauth_client_;
80 std::string refresh_token_;
jrwec9abae2015-04-24 21:57:0881 bool need_user_email_;
[email protected]6f5f90962013-08-16 21:28:4882 CompletionCallback on_done_;
83
84 DISALLOW_COPY_AND_ASSIGN(OAuthClient);
85};
86
87} // namespace remoting
88
89#endif // REMOTING_HOST_SETUP_OAUTH_CLIENT