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