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_IPC_DESKTOP_ENVIRONMENT_H_ 6 #define REMOTING_HOST_IPC_DESKTOP_ENVIRONMENT_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/callback_forward.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/weak_ptr.h" 16 #include "remoting/host/desktop_environment.h" 17 #include "remoting/host/desktop_session_connector.h" 18 19 namespace base { 20 class SingleThreadTaskRunner; 21 } // base 22 23 namespace IPC { 24 class Sender; 25 } // namespace IPC 26 27 namespace remoting { 28 29 class ClientSessionControl; 30 class DesktopSessionProxy; 31 class ScreenResolution; 32 33 // A variant of desktop environment integrating with the desktop by means of 34 // a helper process and talking to that process via IPC. 35 class IpcDesktopEnvironment : public DesktopEnvironment { 36 public: 37 // |desktop_session_connector| is used to bind DesktopSessionProxy to 38 // a desktop session, to be notified every time the desktop process is 39 // restarted. 40 IpcDesktopEnvironment( 41 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner, 42 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, 43 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, 44 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, 45 base::WeakPtr<ClientSessionControl> client_session_control, 46 base::WeakPtr<DesktopSessionConnector> desktop_session_connector, 47 bool virtual_terminal); 48 virtual ~IpcDesktopEnvironment(); 49 50 // DesktopEnvironment implementation. 51 virtual scoped_ptr<AudioCapturer> CreateAudioCapturer() OVERRIDE; 52 virtual scoped_ptr<InputInjector> CreateInputInjector() OVERRIDE; 53 virtual scoped_ptr<ScreenControls> CreateScreenControls() OVERRIDE; 54 virtual scoped_ptr<webrtc::ScreenCapturer> CreateVideoCapturer() OVERRIDE; 55 virtual std::string GetCapabilities() const OVERRIDE; 56 virtual void SetCapabilities(const std::string& capabilities) OVERRIDE; 57 58 private: 59 scoped_refptr<DesktopSessionProxy> desktop_session_proxy_; 60 61 DISALLOW_COPY_AND_ASSIGN(IpcDesktopEnvironment); 62 }; 63 64 // Used to create IpcDesktopEnvironment objects integrating with the desktop via 65 // a helper process and talking to that process via IPC. 66 class IpcDesktopEnvironmentFactory 67 : public DesktopEnvironmentFactory, 68 public DesktopSessionConnector { 69 public: 70 // Passes a reference to the IPC channel connected to the daemon process and 71 // relevant task runners. |daemon_channel| must outlive this object. 72 IpcDesktopEnvironmentFactory( 73 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner, 74 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, 75 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, 76 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, 77 IPC::Sender* daemon_channel); 78 virtual ~IpcDesktopEnvironmentFactory(); 79 80 // DesktopEnvironmentFactory implementation. 81 virtual scoped_ptr<DesktopEnvironment> Create( 82 base::WeakPtr<ClientSessionControl> client_session_control) OVERRIDE; 83 virtual void SetEnableCurtaining(bool enable) OVERRIDE; 84 virtual bool SupportsAudioCapture() const OVERRIDE; 85 86 // DesktopSessionConnector implementation. 87 virtual void ConnectTerminal( 88 DesktopSessionProxy* desktop_session_proxy, 89 const ScreenResolution& resolution, 90 bool virtual_terminal) OVERRIDE; 91 virtual void DisconnectTerminal( 92 DesktopSessionProxy* desktop_session_proxy) OVERRIDE; 93 virtual void SetScreenResolution( 94 DesktopSessionProxy* desktop_session_proxy, 95 const ScreenResolution& resolution) OVERRIDE; 96 virtual void OnDesktopSessionAgentAttached( 97 int terminal_id, 98 base::ProcessHandle desktop_process, 99 IPC::PlatformFileForTransit desktop_pipe) OVERRIDE; 100 virtual void OnTerminalDisconnected(int terminal_id) OVERRIDE; 101 102 private: 103 // Used to run the audio capturer. 104 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner_; 105 106 // Task runner on which methods of DesktopEnvironmentFactory interface should 107 // be called. 108 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; 109 110 // Used to run the video capturer. 111 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner_; 112 113 // Task runner used for running background I/O. 114 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 115 116 // True if curtain mode is enabled. 117 bool curtain_enabled_; 118 119 // IPC channel connected to the daemon process. 120 IPC::Sender* daemon_channel_; 121 122 // List of DesktopEnvironment instances we've told the daemon process about. 123 typedef std::map<int, DesktopSessionProxy*> ActiveConnectionsList; 124 ActiveConnectionsList active_connections_; 125 126 // Factory for weak pointers to DesktopSessionConnector interface. 127 base::WeakPtrFactory<DesktopSessionConnector> connector_factory_; 128 129 // Next desktop session ID. IDs are allocated sequentially starting from 0. 130 // This gives us more than 67 years of unique IDs assuming a new ID is 131 // allocated every second. 132 int next_id_; 133 134 DISALLOW_COPY_AND_ASSIGN(IpcDesktopEnvironmentFactory); 135 }; 136 137 } // namespace remoting 138 139 #endif // REMOTING_HOST_IPC_DESKTOP_ENVIRONMENT_H_ 140