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/memory/ref_counted.h" 12 #include "base/memory/weak_ptr.h" 13 #include "google_apis/gaia/gaia_oauth_client.h" 14 #include "remoting/base/rsa_key_pair.h" 15 #include "remoting/host/setup/daemon_controller.h" 16 #include "remoting/host/setup/service_client.h" 17 18 namespace net { 19 class URLRequestContextGetter; 20 } 21 22 namespace remoting { 23 24 // A helper class that registers and starts a host. 25 class HostStarter : public gaia::GaiaOAuthClient::Delegate, 26 public remoting::ServiceClient::Delegate { 27 public: 28 enum Result { 29 START_COMPLETE, 30 NETWORK_ERROR, 31 OAUTH_ERROR, 32 START_ERROR, 33 }; 34 35 typedef base::Callback<void(Result)> CompletionCallback; 36 37 virtual ~HostStarter(); 38 39 // Creates a HostStarter. 40 static scoped_ptr<HostStarter> Create( 41 const std::string& chromoting_hosts_url, 42 net::URLRequestContextGetter* url_request_context_getter); 43 44 // Registers a new host with the Chromoting service, and starts it. 45 // |auth_code| must be a valid OAuth2 authorization code, typically acquired 46 // from a browser. This method uses that code to get an OAuth2 refresh token. 47 void StartHost(const std::string& host_name, 48 const std::string& host_pin, 49 bool consent_to_data_collection, 50 const std::string& auth_code, 51 const std::string& redirect_url, 52 CompletionCallback on_done); 53 54 // gaia::GaiaOAuthClient::Delegate 55 virtual void OnGetTokensResponse(const std::string& refresh_token, 56 const std::string& access_token, 57 int expires_in_seconds) OVERRIDE; 58 virtual void OnRefreshTokenResponse(const std::string& access_token, 59 int expires_in_seconds) OVERRIDE; 60 virtual void OnGetUserEmailResponse(const std::string& user_email) OVERRIDE; 61 62 // remoting::ServiceClient::Delegate 63 virtual void OnHostRegistered(const std::string& authorization_code) OVERRIDE; 64 virtual void OnHostUnregistered() OVERRIDE; 65 66 // TODO(sergeyu): Following methods are members of all three delegate 67 // interfaces implemented in this class. Fix ServiceClient and 68 // GaiaUserEmailFetcher so that Delegate interfaces do not overlap (ideally 69 // they should be changed to use Callback<>). 70 virtual void OnOAuthError() OVERRIDE; 71 virtual void OnNetworkError(int response_code) OVERRIDE; 72 73 private: 74 HostStarter(scoped_ptr<gaia::GaiaOAuthClient> oauth_client, 75 scoped_ptr<remoting::ServiceClient> service_client, 76 scoped_refptr<remoting::DaemonController> daemon_controller); 77 78 void StartHostProcess(); 79 80 void OnHostStarted(DaemonController::AsyncResult result); 81 82 scoped_ptr<gaia::GaiaOAuthClient> oauth_client_; 83 scoped_ptr<remoting::ServiceClient> service_client_; 84 scoped_refptr<remoting::DaemonController> daemon_controller_; 85 gaia::OAuthClientInfo oauth_client_info_; 86 std::string host_name_; 87 std::string host_pin_; 88 bool consent_to_data_collection_; 89 CompletionCallback on_done_; 90 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; 91 std::string refresh_token_; 92 std::string 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 use_service_account_; 98 99 // True if the host was not started and unregistration was requested. If this 100 // is set and a network/OAuth error occurs during unregistration, this will 101 // be logged, but the error will still be reported as START_ERROR. 102 bool unregistering_host_; 103 104 base::WeakPtr<HostStarter> weak_ptr_; 105 base::WeakPtrFactory<HostStarter> weak_ptr_factory_; 106 107 DISALLOW_COPY_AND_ASSIGN(HostStarter); 108 }; 109 110 } // namespace remoting 111 112 #endif // REMOTING_HOST_HOST_STARTER 113