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.
5 #include "remoting/host/ipc_desktop_environment.h"
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/process/process_handle.h"
12 #include "base/single_thread_task_runner.h"
13 #include "ipc/ipc_sender.h"
14 #include "remoting/host/audio_capturer.h"
15 #include "remoting/host/chromoting_messages.h"
16 #include "remoting/host/client_session_control.h"
17 #include "remoting/host/desktop_session.h"
18 #include "remoting/host/desktop_session_proxy.h"
19 #include "remoting/host/gnubby_auth_handler.h"
20 #include "remoting/host/input_injector.h"
21 #include "remoting/host/screen_controls.h"
22 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h"
23 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h"
27 IpcDesktopEnvironment::IpcDesktopEnvironment(
28 scoped_refptr
<base::SingleThreadTaskRunner
> audio_task_runner
,
29 scoped_refptr
<base::SingleThreadTaskRunner
> caller_task_runner
,
30 scoped_refptr
<base::SingleThreadTaskRunner
> capture_task_runner
,
31 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
32 base::WeakPtr
<ClientSessionControl
> client_session_control
,
33 base::WeakPtr
<DesktopSessionConnector
> desktop_session_connector
,
34 bool virtual_terminal
,
35 bool supports_touch_events
) {
36 DCHECK(caller_task_runner
->BelongsToCurrentThread());
38 desktop_session_proxy_
= new DesktopSessionProxy(audio_task_runner
,
42 client_session_control
,
43 desktop_session_connector
,
45 supports_touch_events
);
48 IpcDesktopEnvironment::~IpcDesktopEnvironment() {
51 scoped_ptr
<AudioCapturer
> IpcDesktopEnvironment::CreateAudioCapturer() {
52 return desktop_session_proxy_
->CreateAudioCapturer();
55 scoped_ptr
<InputInjector
> IpcDesktopEnvironment::CreateInputInjector() {
56 return desktop_session_proxy_
->CreateInputInjector();
59 scoped_ptr
<ScreenControls
> IpcDesktopEnvironment::CreateScreenControls() {
60 return desktop_session_proxy_
->CreateScreenControls();
63 scoped_ptr
<webrtc::MouseCursorMonitor
>
64 IpcDesktopEnvironment::CreateMouseCursorMonitor() {
65 return desktop_session_proxy_
->CreateMouseCursorMonitor();
68 scoped_ptr
<webrtc::DesktopCapturer
>
69 IpcDesktopEnvironment::CreateVideoCapturer() {
70 return desktop_session_proxy_
->CreateVideoCapturer();
73 std::string
IpcDesktopEnvironment::GetCapabilities() const {
74 return desktop_session_proxy_
->GetCapabilities();
77 void IpcDesktopEnvironment::SetCapabilities(const std::string
& capabilities
) {
78 return desktop_session_proxy_
->SetCapabilities(capabilities
);
81 scoped_ptr
<GnubbyAuthHandler
> IpcDesktopEnvironment::CreateGnubbyAuthHandler(
82 protocol::ClientStub
* client_stub
) {
86 IpcDesktopEnvironmentFactory::IpcDesktopEnvironmentFactory(
87 scoped_refptr
<base::SingleThreadTaskRunner
> audio_task_runner
,
88 scoped_refptr
<base::SingleThreadTaskRunner
> caller_task_runner
,
89 scoped_refptr
<base::SingleThreadTaskRunner
> capture_task_runner
,
90 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
91 IPC::Sender
* daemon_channel
)
92 : audio_task_runner_(audio_task_runner
),
93 caller_task_runner_(caller_task_runner
),
94 capture_task_runner_(capture_task_runner
),
95 io_task_runner_(io_task_runner
),
96 curtain_enabled_(false),
97 daemon_channel_(daemon_channel
),
99 connector_factory_(this),
100 supports_touch_events_(false) {
103 IpcDesktopEnvironmentFactory::~IpcDesktopEnvironmentFactory() {
106 scoped_ptr
<DesktopEnvironment
> IpcDesktopEnvironmentFactory::Create(
107 base::WeakPtr
<ClientSessionControl
> client_session_control
) {
108 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
110 return make_scoped_ptr(
111 new IpcDesktopEnvironment(audio_task_runner_
,
113 capture_task_runner_
,
115 client_session_control
,
116 connector_factory_
.GetWeakPtr(),
118 supports_touch_events_
));
121 void IpcDesktopEnvironmentFactory::SetEnableCurtaining(bool enable
) {
122 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
124 curtain_enabled_
= enable
;
127 bool IpcDesktopEnvironmentFactory::SupportsAudioCapture() const {
128 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
130 return AudioCapturer::IsSupported();
133 void IpcDesktopEnvironmentFactory::ConnectTerminal(
134 DesktopSessionProxy
* desktop_session_proxy
,
135 const ScreenResolution
& resolution
,
136 bool virtual_terminal
) {
137 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
140 bool inserted
= active_connections_
.insert(
141 std::make_pair(id
, desktop_session_proxy
)).second
;
144 VLOG(1) << "Network: registered desktop environment " << id
;
146 daemon_channel_
->Send(new ChromotingNetworkHostMsg_ConnectTerminal(
147 id
, resolution
, virtual_terminal
));
150 void IpcDesktopEnvironmentFactory::DisconnectTerminal(
151 DesktopSessionProxy
* desktop_session_proxy
) {
152 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
154 ActiveConnectionsList::iterator i
;
155 for (i
= active_connections_
.begin(); i
!= active_connections_
.end(); ++i
) {
156 if (i
->second
== desktop_session_proxy
)
160 if (i
!= active_connections_
.end()) {
162 active_connections_
.erase(i
);
164 VLOG(1) << "Network: unregistered desktop environment " << id
;
165 daemon_channel_
->Send(new ChromotingNetworkHostMsg_DisconnectTerminal(id
));
169 void IpcDesktopEnvironmentFactory::SetScreenResolution(
170 DesktopSessionProxy
* desktop_session_proxy
,
171 const ScreenResolution
& resolution
) {
172 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
174 ActiveConnectionsList::iterator i
;
175 for (i
= active_connections_
.begin(); i
!= active_connections_
.end(); ++i
) {
176 if (i
->second
== desktop_session_proxy
)
180 if (i
!= active_connections_
.end()) {
181 daemon_channel_
->Send(new ChromotingNetworkDaemonMsg_SetScreenResolution(
182 i
->first
, resolution
));
186 void IpcDesktopEnvironmentFactory::OnDesktopSessionAgentAttached(
188 base::ProcessHandle desktop_process_handle
,
189 IPC::PlatformFileForTransit desktop_pipe
) {
190 if (!caller_task_runner_
->BelongsToCurrentThread()) {
191 caller_task_runner_
->PostTask(FROM_HERE
, base::Bind(
192 &IpcDesktopEnvironmentFactory::OnDesktopSessionAgentAttached
,
193 base::Unretained(this), terminal_id
, desktop_process_handle
,
198 base::Process
desktop_process(desktop_process_handle
);
199 ActiveConnectionsList::iterator i
= active_connections_
.find(terminal_id
);
200 if (i
!= active_connections_
.end()) {
201 i
->second
->DetachFromDesktop();
202 i
->second
->AttachToDesktop(desktop_process
.Pass(), desktop_pipe
);
204 #if defined(OS_POSIX)
205 DCHECK(desktop_pipe
.auto_close
);
206 base::File
pipe_closer(IPC::PlatformFileForTransitToFile(desktop_pipe
));
207 #endif // defined(OS_POSIX)
211 void IpcDesktopEnvironmentFactory::OnTerminalDisconnected(int terminal_id
) {
212 if (!caller_task_runner_
->BelongsToCurrentThread()) {
213 caller_task_runner_
->PostTask(FROM_HERE
, base::Bind(
214 &IpcDesktopEnvironmentFactory::OnTerminalDisconnected
,
215 base::Unretained(this), terminal_id
));
219 ActiveConnectionsList::iterator i
= active_connections_
.find(terminal_id
);
220 if (i
!= active_connections_
.end()) {
221 DesktopSessionProxy
* desktop_session_proxy
= i
->second
;
222 active_connections_
.erase(i
);
224 // Disconnect the client session.
225 desktop_session_proxy
->DisconnectSession();
229 } // namespace remoting