host_starter.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2012 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_HOST_SETUP_HOST_STARTER_H_
  5. #define REMOTING_HOST_SETUP_HOST_STARTER_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "google_apis/gaia/gaia_oauth_client.h"
  11. #include "remoting/base/rsa_key_pair.h"
  12. #include "remoting/host/setup/daemon_controller.h"
  13. #include "remoting/host/setup/host_stopper.h"
  14. #include "remoting/host/setup/service_client.h"
  15. namespace network {
  16. class SharedURLLoaderFactory;
  17. }
  18. namespace remoting {
  19. // A helper class that registers and starts a host.
  20. class HostStarter : public gaia::GaiaOAuthClient::Delegate,
  21. public remoting::ServiceClient::Delegate {
  22. public:
  23. enum Result {
  24. START_COMPLETE,
  25. NETWORK_ERROR,
  26. OAUTH_ERROR,
  27. START_ERROR,
  28. };
  29. typedef base::OnceCallback<void(Result)> CompletionCallback;
  30. HostStarter(const HostStarter&) = delete;
  31. HostStarter& operator=(const HostStarter&) = delete;
  32. ~HostStarter() override;
  33. // Creates a HostStarter.
  34. static std::unique_ptr<HostStarter> Create(
  35. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
  36. // Registers a new host with the Chromoting service, and starts it.
  37. // |auth_code| must be a valid OAuth2 authorization code, typically acquired
  38. // from a browser. This method uses that code to get an OAuth2 refresh token.
  39. void StartHost(const std::string& host_id,
  40. const std::string& host_name,
  41. const std::string& host_pin,
  42. const std::string& host_owner,
  43. bool consent_to_data_collection,
  44. const std::string& auth_code,
  45. const std::string& redirect_url,
  46. CompletionCallback on_done);
  47. // gaia::GaiaOAuthClient::Delegate
  48. void OnGetTokensResponse(const std::string& refresh_token,
  49. const std::string& access_token,
  50. int expires_in_seconds) override;
  51. void OnRefreshTokenResponse(const std::string& access_token,
  52. int expires_in_seconds) override;
  53. void OnGetUserEmailResponse(const std::string& user_email) override;
  54. // remoting::ServiceClient::Delegate
  55. void OnHostRegistered(const std::string& authorization_code) override;
  56. void OnHostUnregistered() override;
  57. // TODO(sergeyu): Following methods are members of all three delegate
  58. // interfaces implemented in this class. Fix ServiceClient and
  59. // GaiaUserEmailFetcher so that Delegate interfaces do not overlap (ideally
  60. // they should be changed to use Callback<>).
  61. void OnOAuthError() override;
  62. void OnNetworkError(int response_code) override;
  63. private:
  64. // GetTokensFromAuthCode() is used for getting an access token for the
  65. // Directory API (to register/unregister a new host). It is also used for
  66. // getting access+refresh tokens for the new host (for getting the robot
  67. // email and for writing the new config file).
  68. enum PendingGetTokensRequest {
  69. GET_TOKENS_NONE,
  70. GET_TOKENS_DIRECTORY,
  71. GET_TOKENS_HOST
  72. };
  73. HostStarter(std::unique_ptr<gaia::GaiaOAuthClient> oauth_client,
  74. std::unique_ptr<remoting::ServiceClient> service_client,
  75. scoped_refptr<remoting::DaemonController> daemon_controller,
  76. std::unique_ptr<remoting::HostStopper> host_stopper);
  77. void StartHostProcess();
  78. void OnLocalHostStopped();
  79. void OnHostStarted(DaemonController::AsyncResult result);
  80. std::unique_ptr<gaia::GaiaOAuthClient> oauth_client_;
  81. std::unique_ptr<remoting::ServiceClient> service_client_;
  82. scoped_refptr<remoting::DaemonController> daemon_controller_;
  83. std::unique_ptr<remoting::HostStopper> host_stopper_;
  84. gaia::OAuthClientInfo oauth_client_info_;
  85. std::string host_name_;
  86. std::string host_pin_;
  87. bool consent_to_data_collection_;
  88. CompletionCallback on_done_;
  89. scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
  90. std::string host_refresh_token_;
  91. std::string host_access_token_;
  92. std::string directory_access_token_;
  93. std::string host_owner_;
  94. std::string xmpp_login_;
  95. scoped_refptr<remoting::RsaKeyPair> key_pair_;
  96. std::string host_id_;
  97. bool auth_code_exchanged_ = false;
  98. // True if the host was not started and unregistration was requested. If this
  99. // is set and a network/OAuth error occurs during unregistration, this will
  100. // be logged, but the error will still be reported as START_ERROR.
  101. bool unregistering_host_;
  102. PendingGetTokensRequest pending_get_tokens_ = GET_TOKENS_NONE;
  103. base::WeakPtr<HostStarter> weak_ptr_;
  104. base::WeakPtrFactory<HostStarter> weak_ptr_factory_{this};
  105. };
  106. } // namespace remoting
  107. #endif // REMOTING_HOST_SETUP_HOST_STARTER_H_