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