client_session.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. #ifndef REMOTING_HOST_CLIENT_SESSION_H_
  5. #define REMOTING_HOST_CLIENT_SESSION_H_
  6. #include <cstdint>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/sequence_checker.h"
  15. #include "base/task/sequenced_task_runner_helpers.h"
  16. #include "base/time/time.h"
  17. #include "base/timer/timer.h"
  18. #include "mojo/public/cpp/bindings/pending_receiver.h"
  19. #include "mojo/public/cpp/bindings/receiver_set.h"
  20. #include "remoting/host/base/desktop_environment_options.h"
  21. #include "remoting/host/client_session_control.h"
  22. #include "remoting/host/client_session_details.h"
  23. #include "remoting/host/client_session_events.h"
  24. #include "remoting/host/desktop_and_cursor_composer_notifier.h"
  25. #include "remoting/host/desktop_and_cursor_conditional_composer.h"
  26. #include "remoting/host/desktop_display_info.h"
  27. #include "remoting/host/host_experiment_session_plugin.h"
  28. #include "remoting/host/host_extension_session_manager.h"
  29. #include "remoting/host/mojom/chromoting_host_services.mojom.h"
  30. #include "remoting/host/mojom/webauthn_proxy.mojom.h"
  31. #include "remoting/host/remote_input_filter.h"
  32. #include "remoting/proto/action.pb.h"
  33. #include "remoting/protocol/clipboard_echo_filter.h"
  34. #include "remoting/protocol/clipboard_filter.h"
  35. #include "remoting/protocol/clipboard_stub.h"
  36. #include "remoting/protocol/connection_to_client.h"
  37. #include "remoting/protocol/data_channel_manager.h"
  38. #include "remoting/protocol/display_size.h"
  39. #include "remoting/protocol/host_stub.h"
  40. #include "remoting/protocol/input_event_tracker.h"
  41. #include "remoting/protocol/input_filter.h"
  42. #include "remoting/protocol/input_stub.h"
  43. #include "remoting/protocol/mouse_input_filter.h"
  44. #include "remoting/protocol/pairing_registry.h"
  45. #include "remoting/protocol/video_stream.h"
  46. #include "third_party/webrtc/modules/desktop_capture/desktop_capture_metadata.h"
  47. #include "third_party/webrtc/modules/desktop_capture/desktop_capture_types.h"
  48. #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
  49. #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h"
  50. #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h"
  51. #include "ui/events/event.h"
  52. namespace remoting {
  53. class AudioStream;
  54. class DesktopEnvironment;
  55. class DesktopEnvironmentFactory;
  56. class InputInjector;
  57. class KeyboardLayoutMonitor;
  58. class MouseShapePump;
  59. class RemoteOpenUrlMessageHandler;
  60. class RemoteWebAuthnMessageHandler;
  61. class ScreenControls;
  62. namespace protocol {
  63. class VideoLayout;
  64. } // namespace protocol
  65. // A ClientSession keeps a reference to a connection to a client, and maintains
  66. // per-client state.
  67. class ClientSession : public protocol::HostStub,
  68. public protocol::ConnectionToClient::EventHandler,
  69. public protocol::VideoStream::Observer,
  70. public ClientSessionControl,
  71. public ClientSessionDetails,
  72. public ClientSessionEvents,
  73. public DesktopAndCursorComposerNotifier::EventHandler,
  74. public webrtc::MouseCursorMonitor::Callback,
  75. public mojom::ChromotingSessionServices {
  76. public:
  77. // Callback interface for passing events to the ChromotingHost.
  78. class EventHandler {
  79. public:
  80. // Called after authentication has started.
  81. virtual void OnSessionAuthenticating(ClientSession* client) = 0;
  82. // Called after authentication has finished successfully.
  83. virtual void OnSessionAuthenticated(ClientSession* client) = 0;
  84. // Called after we've finished connecting all channels.
  85. virtual void OnSessionChannelsConnected(ClientSession* client) = 0;
  86. // Called after authentication has failed. Must not tear down this
  87. // object. OnSessionClosed() is notified after this handler
  88. // returns.
  89. virtual void OnSessionAuthenticationFailed(ClientSession* client) = 0;
  90. // Called after connection has failed or after the client closed it.
  91. virtual void OnSessionClosed(ClientSession* client) = 0;
  92. // Called on notification of a route change event, when a channel is
  93. // connected.
  94. virtual void OnSessionRouteChange(
  95. ClientSession* client,
  96. const std::string& channel_name,
  97. const protocol::TransportRoute& route) = 0;
  98. protected:
  99. virtual ~EventHandler() {}
  100. };
  101. // |event_handler| and |desktop_environment_factory| must outlive |this|.
  102. // All |HostExtension|s in |extensions| must outlive |this|.
  103. ClientSession(
  104. EventHandler* event_handler,
  105. std::unique_ptr<protocol::ConnectionToClient> connection,
  106. DesktopEnvironmentFactory* desktop_environment_factory,
  107. const DesktopEnvironmentOptions& desktop_environment_options,
  108. const base::TimeDelta& max_duration,
  109. scoped_refptr<protocol::PairingRegistry> pairing_registry,
  110. const std::vector<HostExtension*>& extensions);
  111. ClientSession(const ClientSession&) = delete;
  112. ClientSession& operator=(const ClientSession&) = delete;
  113. ~ClientSession() override;
  114. // Returns the set of capabilities negotiated between client and host.
  115. const std::string& capabilities() const { return capabilities_; }
  116. // protocol::HostStub interface.
  117. void NotifyClientResolution(
  118. const protocol::ClientResolution& resolution) override;
  119. void ControlVideo(const protocol::VideoControl& video_control) override;
  120. void ControlAudio(const protocol::AudioControl& audio_control) override;
  121. void SetCapabilities(const protocol::Capabilities& capabilities) override;
  122. void RequestPairing(
  123. const remoting::protocol::PairingRequest& pairing_request) override;
  124. void DeliverClientMessage(const protocol::ExtensionMessage& message) override;
  125. void SelectDesktopDisplay(
  126. const protocol::SelectDesktopDisplayRequest& select_display) override;
  127. void ControlPeerConnection(
  128. const protocol::PeerConnectionParameters& parameters) override;
  129. // protocol::ConnectionToClient::EventHandler interface.
  130. void OnConnectionAuthenticating() override;
  131. void OnConnectionAuthenticated() override;
  132. void CreateMediaStreams() override;
  133. void OnConnectionChannelsConnected() override;
  134. void OnConnectionClosed(protocol::ErrorCode error) override;
  135. void OnTransportProtocolChange(const std::string& protocol) override;
  136. void OnRouteChange(const std::string& channel_name,
  137. const protocol::TransportRoute& route) override;
  138. void OnIncomingDataChannel(
  139. const std::string& channel_name,
  140. std::unique_ptr<protocol::MessagePipe> pipe) override;
  141. // ClientSessionControl interface.
  142. const std::string& client_jid() const override;
  143. void DisconnectSession(protocol::ErrorCode error) override;
  144. void OnLocalKeyPressed(uint32_t usb_keycode) override;
  145. void OnLocalPointerMoved(const webrtc::DesktopVector& position,
  146. ui::EventType type) override;
  147. void SetDisableInputs(bool disable_inputs) override;
  148. void OnDesktopDisplayChanged(
  149. std::unique_ptr<protocol::VideoLayout> layout) override;
  150. // ClientSessionEvents interface.
  151. void OnDesktopAttached(uint32_t session_id) override;
  152. void OnDesktopDetached() override;
  153. // ClientSessionDetails interface.
  154. uint32_t desktop_session_id() const override;
  155. ClientSessionControl* session_control() override;
  156. // DesktopAndCursorComposerNotifier::EventHandler interface
  157. void SetComposeEnabled(bool enabled) override;
  158. // webrtc::MouseCursorMonitor::Callback implementation.
  159. void OnMouseCursor(webrtc::MouseCursor* mouse_cursor) override;
  160. void OnMouseCursorPosition(const webrtc::DesktopVector& position) override;
  161. // mojom::ChromotingSessionServices implementation.
  162. void BindWebAuthnProxy(
  163. mojo::PendingReceiver<mojom::WebAuthnProxy> receiver) override;
  164. void BindRemoteUrlOpener(
  165. mojo::PendingReceiver<mojom::RemoteUrlOpener> receiver) override;
  166. void BindReceiver(
  167. mojo::PendingReceiver<mojom::ChromotingSessionServices> receiver);
  168. protocol::ConnectionToClient* connection() const { return connection_.get(); }
  169. bool is_authenticated() const { return is_authenticated_; }
  170. bool channels_connected() const { return channels_connected_; }
  171. const std::string* client_capabilities() const {
  172. return client_capabilities_.get();
  173. }
  174. // Registers a DataChannelManager callback for testing.
  175. void RegisterCreateHandlerCallbackForTesting(
  176. const std::string& prefix,
  177. protocol::DataChannelManager::CreateHandlerCallback constructor);
  178. void SetEventTimestampsSourceForTests(
  179. scoped_refptr<protocol::InputEventTimestampsSource>
  180. event_timestamp_source);
  181. // Public for tests.
  182. void UpdateMouseClampingFilterOffset();
  183. private:
  184. // Struct for associating an optional DesktopAndCursorConditionalComposer
  185. // with each VideoStream.
  186. struct VideoStreamWithComposer {
  187. VideoStreamWithComposer();
  188. VideoStreamWithComposer(VideoStreamWithComposer&&);
  189. VideoStreamWithComposer& operator=(VideoStreamWithComposer&&);
  190. ~VideoStreamWithComposer();
  191. std::unique_ptr<protocol::VideoStream> stream;
  192. base::WeakPtr<DesktopAndCursorConditionalComposer> composer;
  193. };
  194. // Creates a proxy for sending clipboard events to the client.
  195. std::unique_ptr<protocol::ClipboardStub> CreateClipboardProxy();
  196. void SetMouseClampingFilter(const DisplaySize& size);
  197. // protocol::VideoStream::Observer implementation.
  198. void OnVideoSizeChanged(protocol::VideoStream* stream,
  199. const webrtc::DesktopSize& size,
  200. const webrtc::DesktopVector& dpi) override;
  201. void CreateActionMessageHandler(
  202. std::vector<protocol::ActionRequest::Action> capabilities,
  203. const std::string& channel_name,
  204. std::unique_ptr<protocol::MessagePipe> pipe);
  205. void CreateFileTransferMessageHandler(
  206. const std::string& channel_name,
  207. std::unique_ptr<protocol::MessagePipe> pipe);
  208. void CreateRtcLogTransferMessageHandler(
  209. const std::string& channel_name,
  210. std::unique_ptr<protocol::MessagePipe> pipe);
  211. void CreateRemoteOpenUrlMessageHandler(
  212. const std::string& channel_name,
  213. std::unique_ptr<protocol::MessagePipe> pipe);
  214. void CreateUrlForwarderControlMessageHandler(
  215. const std::string& channel_name,
  216. std::unique_ptr<protocol::MessagePipe> pipe);
  217. void CreateRemoteWebAuthnMessageHandler(
  218. const std::string& channel_name,
  219. std::unique_ptr<protocol::MessagePipe> pipe);
  220. void CreatePerMonitorVideoStreams();
  221. // True if |index| corresponds with an existing display (or the combined
  222. // display).
  223. bool IsValidDisplayIndex(webrtc::ScreenId index) const;
  224. #if defined(WEBRTC_USE_GIO)
  225. void ExtractAndSetInputInjectorMetadata(
  226. webrtc::DesktopCaptureMetadata capture_metadata);
  227. #endif
  228. raw_ptr<EventHandler> event_handler_;
  229. // Used to create a DesktopEnvironment instance for this session.
  230. raw_ptr<DesktopEnvironmentFactory> desktop_environment_factory_;
  231. // The DesktopEnvironmentOptions used to initialize DesktopEnvironment.
  232. DesktopEnvironmentOptions desktop_environment_options_;
  233. // The DesktopEnvironment instance for this session.
  234. std::unique_ptr<DesktopEnvironment> desktop_environment_;
  235. // Filter used as the final element in the input pipeline.
  236. protocol::InputFilter host_input_filter_;
  237. // Tracker used to release pressed keys and buttons when disconnecting.
  238. protocol::InputEventTracker input_tracker_;
  239. // Filter used to disable remote inputs during local input activity.
  240. RemoteInputFilter remote_input_filter_;
  241. // Filter used to clamp mouse events to the current display dimensions.
  242. protocol::MouseInputFilter mouse_clamping_filter_;
  243. // Filter used to detect transitions into and out of client-side pointer lock,
  244. // and to monitor local input to determine whether or not to include the mouse
  245. // cursor in the desktop image.
  246. DesktopAndCursorComposerNotifier desktop_and_cursor_composer_notifier_;
  247. // Filter to used to stop clipboard items sent from the client being echoed
  248. // back to it. It is the final element in the clipboard (client -> host)
  249. // pipeline.
  250. protocol::ClipboardEchoFilter clipboard_echo_filter_;
  251. // Filters used to manage enabling & disabling of input.
  252. protocol::InputFilter disable_input_filter_;
  253. // Used to enable/disable clipboard sync and to restrict payload size.
  254. protocol::ClipboardFilter host_clipboard_filter_;
  255. protocol::ClipboardFilter client_clipboard_filter_;
  256. // Factory for weak pointers to the client clipboard stub.
  257. // This must appear after |clipboard_echo_filter_|, so that it won't outlive
  258. // it.
  259. base::WeakPtrFactory<protocol::ClipboardStub> client_clipboard_factory_;
  260. // The maximum duration of this session.
  261. // There is no maximum if this value is <= 0.
  262. base::TimeDelta max_duration_;
  263. // A timer that triggers a disconnect when the maximum session duration
  264. // is reached.
  265. base::OneShotTimer max_duration_timer_;
  266. // Objects responsible for sending video, audio.
  267. std::map<webrtc::ScreenId, VideoStreamWithComposer> video_streams_;
  268. std::unique_ptr<protocol::AudioStream> audio_stream_;
  269. // The set of all capabilities supported by the client.
  270. std::unique_ptr<std::string> client_capabilities_;
  271. // The set of all capabilities supported by the host.
  272. std::string host_capabilities_;
  273. // The set of all capabilities negotiated between client and host.
  274. std::string capabilities_;
  275. // Used to inject mouse and keyboard input and handle clipboard events.
  276. std::unique_ptr<InputInjector> input_injector_;
  277. // Used to apply client-requested changes in screen resolution.
  278. std::unique_ptr<ScreenControls> screen_controls_;
  279. // Contains the most recently gathered info about the desktop displays;
  280. DesktopDisplayInfo desktop_display_info_;
  281. // Default DPI values to use if a display reports 0 for DPI.
  282. int default_x_dpi_;
  283. int default_y_dpi_;
  284. // The index of the desktop display to show to the user.
  285. // Default is webrtc::kInvalidScreenScreenId because we need to perform
  286. // an initial capture to determine if the current setup support capturing
  287. // the entire desktop or if it is restricted to a single display.
  288. // This value is either an index into |desktop_display_info_| or one of
  289. // the special values webrtc::kInvalidScreenId, webrtc::kFullDesktopScreenId.
  290. webrtc::ScreenId selected_display_index_ = webrtc::kInvalidScreenId;
  291. // The initial video size captured by WebRTC.
  292. // This will be the full desktop unless webrtc cannot capture the entire
  293. // desktop (e.g., because the DPIs don't match). In that case, it will
  294. // be equal to the dimensions of the default display.
  295. DisplaySize default_webrtc_desktop_size_;
  296. // The current size of the area being captured by webrtc. This will be
  297. // equal to the size of the entire desktop, or to a single display.
  298. DisplaySize webrtc_capture_size_;
  299. // Set to true if the current display configuration supports capturing the
  300. // entire desktop.
  301. bool can_capture_full_desktop_ = true;
  302. // The pairing registry for PIN-less authentication.
  303. scoped_refptr<protocol::PairingRegistry> pairing_registry_;
  304. // Used to dispatch new data channels to factory methods.
  305. protocol::DataChannelManager data_channel_manager_;
  306. // Set to true if the client was authenticated successfully.
  307. bool is_authenticated_ = false;
  308. // Set to true after all data channels have been connected.
  309. bool channels_connected_ = false;
  310. // Used to store video channel pause & lossless parameters.
  311. bool pause_video_ = false;
  312. bool lossless_video_encode_ = false;
  313. bool lossless_video_color_ = false;
  314. // VideoLayout is sent only after the control channel is connected. Until
  315. // then it's stored in |pending_video_layout_message_|.
  316. std::unique_ptr<protocol::VideoLayout> pending_video_layout_message_;
  317. scoped_refptr<protocol::InputEventTimestampsSource>
  318. event_timestamp_source_for_tests_;
  319. HostExperimentSessionPlugin host_experiment_session_plugin_;
  320. // The connection to the client.
  321. std::unique_ptr<protocol::ConnectionToClient> connection_;
  322. std::string client_jid_;
  323. // Used to manage extension functionality.
  324. std::unique_ptr<HostExtensionSessionManager> extension_manager_;
  325. // Objects to monitor and send updates for mouse shape and keyboard layout.
  326. std::unique_ptr<MouseShapePump> mouse_shape_pump_;
  327. std::unique_ptr<KeyboardLayoutMonitor> keyboard_layout_monitor_;
  328. base::WeakPtr<RemoteWebAuthnMessageHandler> remote_webauthn_message_handler_;
  329. base::WeakPtr<RemoteOpenUrlMessageHandler> remote_open_url_message_handler_;
  330. mojo::ReceiverSet<mojom::ChromotingSessionServices>
  331. session_services_receivers_;
  332. SEQUENCE_CHECKER(sequence_checker_);
  333. // Used to disable callbacks to |this| once DisconnectSession() has been
  334. // called.
  335. base::WeakPtrFactory<ClientSessionControl>
  336. client_session_control_weak_factory_{this};
  337. base::WeakPtrFactory<ClientSessionEvents> client_session_events_weak_factory_{
  338. this};
  339. };
  340. } // namespace remoting
  341. #endif // REMOTING_HOST_CLIENT_SESSION_H_