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_CLIENT_SESSION_H_
      6 #define REMOTING_HOST_CLIENT_SESSION_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "base/sequenced_task_runner_helpers.h"
     13 #include "base/threading/non_thread_safe.h"
     14 #include "base/time/time.h"
     15 #include "base/timer/timer.h"
     16 #include "remoting/host/client_session_control.h"
     17 #include "remoting/host/mouse_clamping_filter.h"
     18 #include "remoting/host/remote_input_filter.h"
     19 #include "remoting/protocol/clipboard_echo_filter.h"
     20 #include "remoting/protocol/clipboard_filter.h"
     21 #include "remoting/protocol/clipboard_stub.h"
     22 #include "remoting/protocol/connection_to_client.h"
     23 #include "remoting/protocol/host_stub.h"
     24 #include "remoting/protocol/input_event_tracker.h"
     25 #include "remoting/protocol/input_filter.h"
     26 #include "remoting/protocol/input_stub.h"
     27 #include "remoting/protocol/pairing_registry.h"
     28 #include "third_party/skia/include/core/SkPoint.h"
     29 #include "third_party/skia/include/core/SkSize.h"
     30 
     31 namespace base {
     32 class SingleThreadTaskRunner;
     33 }  // namespace base
     34 
     35 namespace remoting {
     36 
     37 class AudioEncoder;
     38 class AudioScheduler;
     39 class DesktopEnvironment;
     40 class DesktopEnvironmentFactory;
     41 class InputInjector;
     42 class ScreenControls;
     43 class VideoEncoder;
     44 class VideoScheduler;
     45 
     46 // A ClientSession keeps a reference to a connection to a client, and maintains
     47 // per-client state.
     48 class ClientSession
     49     : public base::NonThreadSafe,
     50       public protocol::HostStub,
     51       public protocol::ConnectionToClient::EventHandler,
     52       public ClientSessionControl {
     53  public:
     54   // Callback interface for passing events to the ChromotingHost.
     55   class EventHandler {
     56    public:
     57     // Called after authentication has finished successfully. Returns true if
     58     // the connection is allowed, or false otherwise.
     59     virtual bool OnSessionAuthenticated(ClientSession* client) = 0;
     60 
     61     // Called after we've finished connecting all channels.
     62     virtual void OnSessionChannelsConnected(ClientSession* client) = 0;
     63 
     64     // Called after authentication has failed. Must not tear down this
     65     // object. OnSessionClosed() is notified after this handler
     66     // returns.
     67     virtual void OnSessionAuthenticationFailed(ClientSession* client) = 0;
     68 
     69     // Called after connection has failed or after the client closed it.
     70     virtual void OnSessionClosed(ClientSession* client) = 0;
     71 
     72     // Called to notify of each message's sequence number. The
     73     // callback must not tear down this object.
     74     virtual void OnSessionSequenceNumber(ClientSession* client,
     75                                          int64 sequence_number) = 0;
     76 
     77     // Called on notification of a route change event, when a channel is
     78     // connected.
     79     virtual void OnSessionRouteChange(
     80         ClientSession* client,
     81         const std::string& channel_name,
     82         const protocol::TransportRoute& route) = 0;
     83 
     84    protected:
     85     virtual ~EventHandler() {}
     86   };
     87 
     88   // |event_handler| and |desktop_environment_factory| must outlive |this|.
     89   ClientSession(
     90       EventHandler* event_handler,
     91       scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner,
     92       scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
     93       scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner,
     94       scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner,
     95       scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
     96       scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
     97       scoped_ptr<protocol::ConnectionToClient> connection,
     98       DesktopEnvironmentFactory* desktop_environment_factory,
     99       const base::TimeDelta& max_duration,
    100       scoped_refptr<protocol::PairingRegistry> pairing_registry);
    101   virtual ~ClientSession();
    102 
    103   // protocol::HostStub interface.
    104   virtual void NotifyClientResolution(
    105       const protocol::ClientResolution& resolution) OVERRIDE;
    106   virtual void ControlVideo(
    107       const protocol::VideoControl& video_control) OVERRIDE;
    108   virtual void ControlAudio(
    109       const protocol::AudioControl& audio_control) OVERRIDE;
    110   virtual void SetCapabilities(
    111       const protocol::Capabilities& capabilities) OVERRIDE;
    112   virtual void RequestPairing(
    113       const remoting::protocol::PairingRequest& pairing_request) OVERRIDE;
    114   virtual void DeliverClientMessage(
    115       const protocol::ExtensionMessage& message) OVERRIDE;
    116 
    117   // protocol::ConnectionToClient::EventHandler interface.
    118   virtual void OnConnectionAuthenticated(
    119       protocol::ConnectionToClient* connection) OVERRIDE;
    120   virtual void OnConnectionChannelsConnected(
    121       protocol::ConnectionToClient* connection) OVERRIDE;
    122   virtual void OnConnectionClosed(protocol::ConnectionToClient* connection,
    123                                   protocol::ErrorCode error) OVERRIDE;
    124   virtual void OnSequenceNumberUpdated(
    125       protocol::ConnectionToClient* connection, int64 sequence_number) OVERRIDE;
    126   virtual void OnRouteChange(
    127       protocol::ConnectionToClient* connection,
    128       const std::string& channel_name,
    129       const protocol::TransportRoute& route) OVERRIDE;
    130 
    131   // ClientSessionControl interface.
    132   virtual const std::string& client_jid() const OVERRIDE;
    133   virtual void DisconnectSession() OVERRIDE;
    134   virtual void OnLocalMouseMoved(const SkIPoint& position) OVERRIDE;
    135   virtual void SetDisableInputs(bool disable_inputs) OVERRIDE;
    136 
    137   protocol::ConnectionToClient* connection() const {
    138     return connection_.get();
    139   }
    140 
    141   bool is_authenticated() { return auth_input_filter_.enabled();  }
    142 
    143  private:
    144   // Creates a proxy for sending clipboard events to the client.
    145   scoped_ptr<protocol::ClipboardStub> CreateClipboardProxy();
    146 
    147   // Creates an audio encoder for the specified configuration.
    148   static scoped_ptr<AudioEncoder> CreateAudioEncoder(
    149       const protocol::SessionConfig& config);
    150 
    151   // Creates a video encoder for the specified configuration.
    152   static scoped_ptr<VideoEncoder> CreateVideoEncoder(
    153       const protocol::SessionConfig& config);
    154 
    155   EventHandler* event_handler_;
    156 
    157   // The connection to the client.
    158   scoped_ptr<protocol::ConnectionToClient> connection_;
    159 
    160   std::string client_jid_;
    161 
    162   // Used to disable callbacks to |this| once DisconnectSession() has been
    163   // called.
    164   base::WeakPtrFactory<ClientSessionControl> control_factory_;
    165 
    166   // Used to create a DesktopEnvironment instance for this session.
    167   DesktopEnvironmentFactory* desktop_environment_factory_;
    168 
    169   // The DesktopEnvironment instance for this session.
    170   scoped_ptr<DesktopEnvironment> desktop_environment_;
    171 
    172   // Filter used as the final element in the input pipeline.
    173   protocol::InputFilter host_input_filter_;
    174 
    175   // Tracker used to release pressed keys and buttons when disconnecting.
    176   protocol::InputEventTracker input_tracker_;
    177 
    178   // Filter used to disable remote inputs during local input activity.
    179   RemoteInputFilter remote_input_filter_;
    180 
    181   // Filter used to clamp mouse events to the current display dimensions.
    182   MouseClampingFilter mouse_clamping_filter_;
    183 
    184   // Filter to used to stop clipboard items sent from the client being echoed
    185   // back to it.  It is the final element in the clipboard (client -> host)
    186   // pipeline.
    187   protocol::ClipboardEchoFilter clipboard_echo_filter_;
    188 
    189   // Filters used to manage enabling & disabling of input & clipboard.
    190   protocol::InputFilter disable_input_filter_;
    191   protocol::ClipboardFilter disable_clipboard_filter_;
    192 
    193   // Filters used to disable input & clipboard when we're not authenticated.
    194   protocol::InputFilter auth_input_filter_;
    195   protocol::ClipboardFilter auth_clipboard_filter_;
    196 
    197   // Factory for weak pointers to the client clipboard stub.
    198   // This must appear after |clipboard_echo_filter_|, so that it won't outlive
    199   // it.
    200   base::WeakPtrFactory<protocol::ClipboardStub> client_clipboard_factory_;
    201 
    202   // The maximum duration of this session.
    203   // There is no maximum if this value is <= 0.
    204   base::TimeDelta max_duration_;
    205 
    206   // A timer that triggers a disconnect when the maximum session duration
    207   // is reached.
    208   base::OneShotTimer<ClientSession> max_duration_timer_;
    209 
    210   scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner_;
    211   scoped_refptr<base::SingleThreadTaskRunner> input_task_runner_;
    212   scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner_;
    213   scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner_;
    214   scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
    215   scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
    216 
    217   // Schedulers for audio and video capture.
    218   scoped_refptr<AudioScheduler> audio_scheduler_;
    219   scoped_refptr<VideoScheduler> video_scheduler_;
    220 
    221   // The set of all capabilities supported by the client.
    222   scoped_ptr<std::string> client_capabilities_;
    223 
    224   // The set of all capabilities supported by the host.
    225   std::string host_capabilities_;
    226 
    227   // Used to inject mouse and keyboard input and handle clipboard events.
    228   scoped_ptr<InputInjector> input_injector_;
    229 
    230   // Used to apply client-requested changes in screen resolution.
    231   scoped_ptr<ScreenControls> screen_controls_;
    232 
    233   // The pairing registry for PIN-less authentication.
    234   scoped_refptr<protocol::PairingRegistry> pairing_registry_;
    235 
    236   DISALLOW_COPY_AND_ASSIGN(ClientSession);
    237 };
    238 
    239 }  // namespace remoting
    240 
    241 #endif  // REMOTING_HOST_CLIENT_SESSION_H_
    242