gaia_oauth_client.h 2.9 KB

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