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 #ifndef REMOTING_BASE_GAIA_OAUTH_CLIENT_H_
6 #define REMOTING_BASE_GAIA_OAUTH_CLIENT_H_
7 
8 #include "base/containers/queue.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "google_apis/gaia/gaia_oauth_client.h"
12 #include "services/network/public/cpp/shared_url_loader_factory.h"
13 
14 #include "remoting/base/oauth_client.h"
15 
16 namespace remoting {
17 
18 // A wrapper around gaia::GaiaOAuthClient that provides a more
19 // convenient interface, with queueing of requests and a callback
20 // rather than a delegate.
21 class GaiaOAuthClient : public OAuthClient,
22                         public gaia::GaiaOAuthClient::Delegate {
23  public:
24   GaiaOAuthClient(
25       scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
26 
27   ~GaiaOAuthClient() override;
28 
29   // Redeems |auth_code| using |oauth_client_info| to obtain |refresh_token| and
30   // |access_token|, then uses the userinfo endpoint to obtain |user_email|.
31   // Calls CompletionCallback with |user_email| and |refresh_token| when done,
32   // or with empty strings on error.
33   // If a request is received while another one is  being processed, it is
34   // enqueued and processed after the first one is finished.
35   void GetCredentialsFromAuthCode(
36       const gaia::OAuthClientInfo& oauth_client_info,
37       const std::string& auth_code,
38       bool need_user_email,
39       CompletionCallback on_done) override;
40 
41   // gaia::GaiaOAuthClient::Delegate
42   void OnGetTokensResponse(const std::string& refresh_token,
43                            const std::string& access_token,
44                            int expires_in_seconds) override;
45   void OnRefreshTokenResponse(const std::string& access_token,
46                               int expires_in_seconds) override;
47   void OnGetUserEmailResponse(const std::string& user_email) override;
48 
49   void OnOAuthError() override;
50   void OnNetworkError(int response_code) override;
51 
52  private:
53   struct Request {
54     Request(const gaia::OAuthClientInfo& oauth_client_info,
55             const std::string& auth_code,
56             bool need_user_email,
57             CompletionCallback on_done);
58     Request(Request&& other);
59     Request& operator=(Request&& other);
60     virtual ~Request();
61     gaia::OAuthClientInfo oauth_client_info;
62     std::string auth_code;
63     bool need_user_email;
64     CompletionCallback on_done;
65   };
66 
67   void SendResponse(const std::string& user_email,
68                     const std::string& refresh_token);
69 
70   base::queue<Request> pending_requests_;
71   gaia::GaiaOAuthClient gaia_oauth_client_;
72   std::string refresh_token_;
73   bool need_user_email_;
74   CompletionCallback on_done_;
75 
76   DISALLOW_COPY_AND_ASSIGN(GaiaOAuthClient);
77 };
78 
79 }  // namespace remoting
80 
81 #endif  // REMOTING_BASE_GAIA_OAUTH_CLIENT_H_
82