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_CHROMOTING_HOST_H_ 6 #define REMOTING_HOST_CHROMOTING_HOST_H_ 7 8 #include <list> 9 #include <string> 10 11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/observer_list.h" 15 #include "base/threading/non_thread_safe.h" 16 #include "base/threading/thread.h" 17 #include "net/base/backoff_entry.h" 18 #include "remoting/host/client_session.h" 19 #include "remoting/host/host_status_monitor.h" 20 #include "remoting/host/host_status_observer.h" 21 #include "remoting/protocol/authenticator.h" 22 #include "remoting/protocol/connection_to_client.h" 23 #include "remoting/protocol/pairing_registry.h" 24 #include "remoting/protocol/session_manager.h" 25 #include "third_party/skia/include/core/SkSize.h" 26 27 namespace base { 28 class SingleThreadTaskRunner; 29 } // namespace base 30 31 namespace remoting { 32 33 namespace protocol { 34 class InputStub; 35 class SessionConfig; 36 class CandidateSessionConfig; 37 } // namespace protocol 38 39 class DesktopEnvironmentFactory; 40 41 // A class to implement the functionality of a host process. 42 // 43 // Here's the work flow of this class: 44 // 1. We should load the saved GAIA ID token or if this is the first 45 // time the host process runs we should prompt user for the 46 // credential. We will use this token or credentials to authenicate 47 // and register the host. 48 // 49 // 2. We listen for incoming connection using libjingle. We will create 50 // a ConnectionToClient object that wraps around linjingle for transport. 51 // A VideoScheduler is created with an Encoder and a webrtc::ScreenCapturer. 52 // A ConnectionToClient is added to the ScreenRecorder for transporting 53 // the screen captures. An InputStub is created and registered with the 54 // ConnectionToClient to receive mouse / keyboard events from the remote 55 // client. 56 // After we have done all the initialization we'll start the ScreenRecorder. 57 // We'll then enter the running state of the host process. 58 // 59 // 3. When the user is disconnected, we will pause the ScreenRecorder 60 // and try to terminate the threads we have created. This will allow 61 // all pending tasks to complete. After all of that completed we 62 // return to the idle state. We then go to step (2) if there a new 63 // incoming connection. 64 class ChromotingHost : public base::NonThreadSafe, 65 public ClientSession::EventHandler, 66 public protocol::SessionManager::Listener, 67 public HostStatusMonitor { 68 public: 69 // Both |signal_strategy| and |desktop_environment_factory| should outlive 70 // this object. 71 ChromotingHost( 72 SignalStrategy* signal_strategy, 73 DesktopEnvironmentFactory* desktop_environment_factory, 74 scoped_ptr<protocol::SessionManager> session_manager, 75 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner, 76 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, 77 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner, 78 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner, 79 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, 80 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); 81 virtual ~ChromotingHost(); 82 83 // Asynchronously starts the host. 84 // 85 // After this is invoked, the host process will connect to the talk 86 // network and start listening for incoming connections. 87 // 88 // This method can only be called once during the lifetime of this object. 89 void Start(const std::string& xmpp_login); 90 91 // HostStatusMonitor interface. 92 virtual void AddStatusObserver(HostStatusObserver* observer) OVERRIDE; 93 virtual void RemoveStatusObserver(HostStatusObserver* observer) OVERRIDE; 94 95 // This method may be called only from 96 // HostStatusObserver::OnClientAuthenticated() to reject the new 97 // client. 98 void RejectAuthenticatingClient(); 99 100 // Sets the authenticator factory to use for incoming 101 // connections. Incoming connections are rejected until 102 // authenticator factory is set. Must be called on the network 103 // thread after the host is started. Must not be called more than 104 // once per host instance because it may not be safe to delete 105 // factory before all authenticators it created are deleted. 106 void SetAuthenticatorFactory( 107 scoped_ptr<protocol::AuthenticatorFactory> authenticator_factory); 108 109 // Enables/disables curtaining when one or more clients are connected. 110 // Takes immediate effect if clients are already connected. 111 void SetEnableCurtaining(bool enable); 112 113 // Sets the maximum duration of any session. By default, a session has no 114 // maximum duration. 115 void SetMaximumSessionDuration(const base::TimeDelta& max_session_duration); 116 117 //////////////////////////////////////////////////////////////////////////// 118 // ClientSession::EventHandler implementation. 119 virtual bool OnSessionAuthenticated(ClientSession* client) OVERRIDE; 120 virtual void OnSessionChannelsConnected(ClientSession* client) OVERRIDE; 121 virtual void OnSessionAuthenticationFailed(ClientSession* client) OVERRIDE; 122 virtual void OnSessionClosed(ClientSession* session) OVERRIDE; 123 virtual void OnSessionSequenceNumber(ClientSession* session, 124 int64 sequence_number) OVERRIDE; 125 virtual void OnSessionRouteChange( 126 ClientSession* session, 127 const std::string& channel_name, 128 const protocol::TransportRoute& route) OVERRIDE; 129 130 // SessionManager::Listener implementation. 131 virtual void OnSessionManagerReady() OVERRIDE; 132 virtual void OnIncomingSession( 133 protocol::Session* session, 134 protocol::SessionManager::IncomingSessionResponse* response) OVERRIDE; 135 136 // Sets desired configuration for the protocol. Must be called before Start(). 137 void set_protocol_config(scoped_ptr<protocol::CandidateSessionConfig> config); 138 139 base::WeakPtr<ChromotingHost> AsWeakPtr() { 140 return weak_factory_.GetWeakPtr(); 141 } 142 143 // The host uses a pairing registry to generate and store pairing information 144 // for clients for PIN-less authentication. 145 scoped_refptr<protocol::PairingRegistry> pairing_registry() const { 146 return pairing_registry_; 147 } 148 void set_pairing_registry( 149 scoped_refptr<protocol::PairingRegistry> pairing_registry) { 150 pairing_registry_ = pairing_registry; 151 } 152 153 private: 154 friend class ChromotingHostTest; 155 156 typedef std::list<ClientSession*> ClientList; 157 158 // Immediately disconnects all active clients. Host-internal components may 159 // shutdown asynchronously, but the caller is guaranteed not to receive 160 // callbacks for disconnected clients after this call returns. 161 void DisconnectAllClients(); 162 163 // Unless specified otherwise all members of this class must be 164 // used on the network thread only. 165 166 // Parameters specified when the host was created. 167 DesktopEnvironmentFactory* desktop_environment_factory_; 168 scoped_ptr<protocol::SessionManager> session_manager_; 169 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner_; 170 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner_; 171 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner_; 172 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner_; 173 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; 174 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; 175 176 // Connection objects. 177 SignalStrategy* signal_strategy_; 178 179 // Must be used on the network thread only. 180 ObserverList<HostStatusObserver> status_observers_; 181 182 // The connections to remote clients. 183 ClientList clients_; 184 185 // True if the host has been started. 186 bool started_; 187 188 // Configuration of the protocol. 189 scoped_ptr<protocol::CandidateSessionConfig> protocol_config_; 190 191 // Login backoff state. 192 net::BackoffEntry login_backoff_; 193 194 // Flags used for RejectAuthenticatingClient(). 195 bool authenticating_client_; 196 bool reject_authenticating_client_; 197 198 // True if the curtain mode is enabled. 199 bool enable_curtaining_; 200 201 // The maximum duration of any session. 202 base::TimeDelta max_session_duration_; 203 204 // The pairing registry for PIN-less authentication. 205 scoped_refptr<protocol::PairingRegistry> pairing_registry_; 206 207 base::WeakPtrFactory<ChromotingHost> weak_factory_; 208 209 DISALLOW_COPY_AND_ASSIGN(ChromotingHost); 210 }; 211 212 } // namespace remoting 213 214 #endif // REMOTING_HOST_CHROMOTING_HOST_H_ 215