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 DCHECK(caller_task_runner
->BelongsToCurrentThread());
37 desktop_session_proxy_
= new DesktopSessionProxy(audio_task_runner
,
41 client_session_control
,
42 desktop_session_connector
,
46 IpcDesktopEnvironment::~IpcDesktopEnvironment() {
49 scoped_ptr
<AudioCapturer
> IpcDesktopEnvironment::CreateAudioCapturer() {
50 return desktop_session_proxy_
->CreateAudioCapturer();
53 scoped_ptr
<InputInjector
> IpcDesktopEnvironment::CreateInputInjector() {
54 return desktop_session_proxy_
->CreateInputInjector();
57 scoped_ptr
<ScreenControls
> IpcDesktopEnvironment::CreateScreenControls() {
58 return desktop_session_proxy_
->CreateScreenControls();
61 scoped_ptr
<webrtc::MouseCursorMonitor
>
62 IpcDesktopEnvironment::CreateMouseCursorMonitor() {
63 return desktop_session_proxy_
->CreateMouseCursorMonitor();
66 scoped_ptr
<webrtc::DesktopCapturer
>
67 IpcDesktopEnvironment::CreateVideoCapturer() {
68 return desktop_session_proxy_
->CreateVideoCapturer();
71 std::string
IpcDesktopEnvironment::GetCapabilities() const {
72 return desktop_session_proxy_
->GetCapabilities();
75 void IpcDesktopEnvironment::SetCapabilities(const std::string
& capabilities
) {
76 return desktop_session_proxy_
->SetCapabilities(capabilities
);
79 scoped_ptr
<GnubbyAuthHandler
> IpcDesktopEnvironment::CreateGnubbyAuthHandler(
80 protocol::ClientStub
* client_stub
) {
84 IpcDesktopEnvironmentFactory::IpcDesktopEnvironmentFactory(
85 scoped_refptr
<base::SingleThreadTaskRunner
> audio_task_runner
,
86 scoped_refptr
<base::SingleThreadTaskRunner
> caller_task_runner
,
87 scoped_refptr
<base::SingleThreadTaskRunner
> capture_task_runner
,
88 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
89 IPC::Sender
* daemon_channel
)
90 : audio_task_runner_(audio_task_runner
),
91 caller_task_runner_(caller_task_runner
),
92 capture_task_runner_(capture_task_runner
),
93 io_task_runner_(io_task_runner
),
94 curtain_enabled_(false),
95 daemon_channel_(daemon_channel
),
97 connector_factory_(this) {
100 IpcDesktopEnvironmentFactory::~IpcDesktopEnvironmentFactory() {
103 scoped_ptr
<DesktopEnvironment
> IpcDesktopEnvironmentFactory::Create(
104 base::WeakPtr
<ClientSessionControl
> client_session_control
) {
105 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
107 return make_scoped_ptr(
108 new IpcDesktopEnvironment(audio_task_runner_
,
110 capture_task_runner_
,
112 client_session_control
,
113 connector_factory_
.GetWeakPtr(),
117 void IpcDesktopEnvironmentFactory::SetEnableCurtaining(bool enable
) {
118 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
120 curtain_enabled_
= enable
;
123 bool IpcDesktopEnvironmentFactory::SupportsAudioCapture() const {
124 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
126 return AudioCapturer::IsSupported();
129 void IpcDesktopEnvironmentFactory::ConnectTerminal(
130 DesktopSessionProxy
* desktop_session_proxy
,
131 const ScreenResolution
& resolution
,
132 bool virtual_terminal
) {
133 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
136 bool inserted
= active_connections_
.insert(
137 std::make_pair(id
, desktop_session_proxy
)).second
;
140 VLOG(1) << "Network: registered desktop environment " << id
;
142 daemon_channel_
->Send(new ChromotingNetworkHostMsg_ConnectTerminal(
143 id
, resolution
, virtual_terminal
));
146 void IpcDesktopEnvironmentFactory::DisconnectTerminal(
147 DesktopSessionProxy
* desktop_session_proxy
) {
148 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
150 ActiveConnectionsList::iterator i
;
151 for (i
= active_connections_
.begin(); i
!= active_connections_
.end(); ++i
) {
152 if (i
->second
== desktop_session_proxy
)
156 if (i
!= active_connections_
.end()) {
158 active_connections_
.erase(i
);
160 VLOG(1) << "Network: unregistered desktop environment " << id
;
161 daemon_channel_
->Send(new ChromotingNetworkHostMsg_DisconnectTerminal(id
));
165 void IpcDesktopEnvironmentFactory::SetScreenResolution(
166 DesktopSessionProxy
* desktop_session_proxy
,
167 const ScreenResolution
& resolution
) {
168 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
170 ActiveConnectionsList::iterator i
;
171 for (i
= active_connections_
.begin(); i
!= active_connections_
.end(); ++i
) {
172 if (i
->second
== desktop_session_proxy
)
176 if (i
!= active_connections_
.end()) {
177 daemon_channel_
->Send(new ChromotingNetworkDaemonMsg_SetScreenResolution(
178 i
->first
, resolution
));
182 void IpcDesktopEnvironmentFactory::OnDesktopSessionAgentAttached(
184 base::ProcessHandle desktop_process_handle
,
185 IPC::PlatformFileForTransit desktop_pipe
) {
186 if (!caller_task_runner_
->BelongsToCurrentThread()) {
187 caller_task_runner_
->PostTask(FROM_HERE
, base::Bind(
188 &IpcDesktopEnvironmentFactory::OnDesktopSessionAgentAttached
,
189 base::Unretained(this), terminal_id
, desktop_process_handle
,
194 base::Process
desktop_process(desktop_process_handle
);
195 ActiveConnectionsList::iterator i
= active_connections_
.find(terminal_id
);
196 if (i
!= active_connections_
.end()) {
197 i
->second
->DetachFromDesktop();
198 i
->second
->AttachToDesktop(desktop_process
.Pass(), desktop_pipe
);
200 #if defined(OS_POSIX)
201 DCHECK(desktop_pipe
.auto_close
);
202 base::File
pipe_closer(IPC::PlatformFileForTransitToFile(desktop_pipe
));
203 #endif // defined(OS_POSIX)
207 void IpcDesktopEnvironmentFactory::OnTerminalDisconnected(int terminal_id
) {
208 if (!caller_task_runner_
->BelongsToCurrentThread()) {
209 caller_task_runner_
->PostTask(FROM_HERE
, base::Bind(
210 &IpcDesktopEnvironmentFactory::OnTerminalDisconnected
,
211 base::Unretained(this), terminal_id
));
215 ActiveConnectionsList::iterator i
= active_connections_
.find(terminal_id
);
216 if (i
!= active_connections_
.end()) {
217 DesktopSessionProxy
* desktop_session_proxy
= i
->second
;
218 active_connections_
.erase(i
);
220 // Disconnect the client session.
221 desktop_session_proxy
->DisconnectSession();
225 } // namespace remoting