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/desktop_session_win.h"
10 #include "base/base_switches.h"
11 #include "base/command_line.h"
12 #include "base/files/file_path.h"
13 #include "base/guid.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/threading/thread_checker.h"
20 #include "base/timer/timer.h"
21 #include "base/win/scoped_bstr.h"
22 #include "base/win/scoped_comptr.h"
23 #include "base/win/scoped_handle.h"
24 #include "base/win/windows_version.h"
25 #include "ipc/ipc_message_macros.h"
26 #include "ipc/ipc_platform_file.h"
27 #include "remoting/base/auto_thread_task_runner.h"
28 // MIDL-generated declarations and definitions.
29 #include "remoting/host/chromoting_lib.h"
30 #include "remoting/host/chromoting_messages.h"
31 #include "remoting/host/daemon_process.h"
32 #include "remoting/host/desktop_session.h"
33 #include "remoting/host/host_main.h"
34 #include "remoting/host/ipc_constants.h"
35 #include "remoting/host/sas_injector.h"
36 #include "remoting/host/screen_resolution.h"
37 #include "remoting/host/win/host_service.h"
38 #include "remoting/host/win/worker_process_launcher.h"
39 #include "remoting/host/win/wts_session_process_delegate.h"
40 #include "remoting/host/win/wts_terminal_monitor.h"
41 #include "remoting/host/win/wts_terminal_observer.h"
42 #include "remoting/host/worker_process_ipc_delegate.h"
44 using base::win::ScopedHandle
;
50 // The security descriptor of the daemon IPC endpoint. It gives full access
51 // to SYSTEM and denies access by anyone else.
52 const wchar_t kDaemonIpcSecurityDescriptor
[] =
53 SDDL_OWNER L
":" SDDL_LOCAL_SYSTEM
54 SDDL_GROUP L
":" SDDL_LOCAL_SYSTEM
56 SDDL_ACCESS_ALLOWED L
";;" SDDL_GENERIC_ALL L
";;;" SDDL_LOCAL_SYSTEM
59 // The command line parameters that should be copied from the service's command
60 // line to the desktop process.
61 const char* kCopiedSwitchNames
[] = { switches::kV
, switches::kVModule
};
63 // The default screen dimensions for an RDP session.
64 const int kDefaultRdpScreenWidth
= 1280;
65 const int kDefaultRdpScreenHeight
= 768;
67 // RDC 6.1 (W2K8) supports dimensions of up to 4096x2048.
68 const int kMaxRdpScreenWidth
= 4096;
69 const int kMaxRdpScreenHeight
= 2048;
71 // The minimum effective screen dimensions supported by Windows are 800x600.
72 const int kMinRdpScreenWidth
= 800;
73 const int kMinRdpScreenHeight
= 600;
75 // Default dots per inch used by RDP is 96 DPI.
76 const int kDefaultRdpDpi
= 96;
78 // The session attach notification should arrive within 30 seconds.
79 const int kSessionAttachTimeoutSeconds
= 30;
81 // DesktopSession implementation which attaches to the host's physical console.
82 // Receives IPC messages from the desktop process, running in the console
83 // session, via |WorkerProcessIpcDelegate|, and monitors console session
84 // attach/detach events via |WtsConsoleObserer|.
85 class ConsoleSession
: public DesktopSessionWin
{
87 // Same as DesktopSessionWin().
89 scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner
,
90 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner
,
91 DaemonProcess
* daemon_process
,
93 WtsTerminalMonitor
* monitor
);
94 virtual ~ConsoleSession();
97 // DesktopSession overrides.
98 virtual void SetScreenResolution(const ScreenResolution
& resolution
) override
;
100 // DesktopSessionWin overrides.
101 virtual void InjectSas() override
;
104 scoped_ptr
<SasInjector
> sas_injector_
;
106 DISALLOW_COPY_AND_ASSIGN(ConsoleSession
);
109 // DesktopSession implementation which attaches to virtual RDP console.
110 // Receives IPC messages from the desktop process, running in the console
111 // session, via |WorkerProcessIpcDelegate|, and monitors console session
112 // attach/detach events via |WtsConsoleObserer|.
113 class RdpSession
: public DesktopSessionWin
{
115 // Same as DesktopSessionWin().
117 scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner
,
118 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner
,
119 DaemonProcess
* daemon_process
,
121 WtsTerminalMonitor
* monitor
);
122 virtual ~RdpSession();
124 // Performs the part of initialization that can fail.
125 bool Initialize(const ScreenResolution
& resolution
);
127 // Mirrors IRdpDesktopSessionEventHandler.
128 void OnRdpConnected();
132 // DesktopSession overrides.
133 virtual void SetScreenResolution(const ScreenResolution
& resolution
) override
;
135 // DesktopSessionWin overrides.
136 virtual void InjectSas() override
;
139 // An implementation of IRdpDesktopSessionEventHandler interface that forwards
140 // notifications to the owning desktop session.
141 class EventHandler
: public IRdpDesktopSessionEventHandler
{
143 explicit EventHandler(base::WeakPtr
<RdpSession
> desktop_session
);
144 virtual ~EventHandler();
146 // IUnknown interface.
147 STDMETHOD_(ULONG
, AddRef
)() override
;
148 STDMETHOD_(ULONG
, Release
)() override
;
149 STDMETHOD(QueryInterface
)(REFIID riid
, void** ppv
) override
;
151 // IRdpDesktopSessionEventHandler interface.
152 STDMETHOD(OnRdpConnected
)() override
;
153 STDMETHOD(OnRdpClosed
)() override
;
158 // Points to the desktop session object receiving OnRdpXxx() notifications.
159 base::WeakPtr
<RdpSession
> desktop_session_
;
161 // This class must be used on a single thread.
162 base::ThreadChecker thread_checker_
;
164 DISALLOW_COPY_AND_ASSIGN(EventHandler
);
167 // Used to create an RDP desktop session.
168 base::win::ScopedComPtr
<IRdpDesktopSession
> rdp_desktop_session_
;
170 // Used to match |rdp_desktop_session_| with the session it is attached to.
171 std::string terminal_id_
;
173 base::WeakPtrFactory
<RdpSession
> weak_factory_
;
175 DISALLOW_COPY_AND_ASSIGN(RdpSession
);
178 ConsoleSession::ConsoleSession(
179 scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner
,
180 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner
,
181 DaemonProcess
* daemon_process
,
183 WtsTerminalMonitor
* monitor
)
184 : DesktopSessionWin(caller_task_runner
, io_task_runner
, daemon_process
, id
,
186 StartMonitoring(WtsTerminalMonitor::kConsole
);
189 ConsoleSession::~ConsoleSession() {
192 void ConsoleSession::SetScreenResolution(const ScreenResolution
& resolution
) {
193 // Do nothing. The screen resolution of the console session is controlled by
194 // the DesktopSessionAgent instance running in that session.
195 DCHECK(caller_task_runner()->BelongsToCurrentThread());
198 void ConsoleSession::InjectSas() {
199 DCHECK(caller_task_runner()->BelongsToCurrentThread());
202 sas_injector_
= SasInjector::Create();
203 if (!sas_injector_
->InjectSas())
204 LOG(ERROR
) << "Failed to inject Secure Attention Sequence.";
207 RdpSession::RdpSession(
208 scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner
,
209 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner
,
210 DaemonProcess
* daemon_process
,
212 WtsTerminalMonitor
* monitor
)
213 : DesktopSessionWin(caller_task_runner
, io_task_runner
, daemon_process
, id
,
215 weak_factory_(this) {
218 RdpSession::~RdpSession() {
221 bool RdpSession::Initialize(const ScreenResolution
& resolution
) {
222 DCHECK(caller_task_runner()->BelongsToCurrentThread());
224 // Create the RDP wrapper object.
225 HRESULT result
= rdp_desktop_session_
.CreateInstance(
226 __uuidof(RdpDesktopSession
));
227 if (FAILED(result
)) {
228 LOG(ERROR
) << "Failed to create RdpSession object, 0x"
229 << std::hex
<< result
<< std::dec
<< ".";
233 ScreenResolution local_resolution
= resolution
;
235 // If the screen resolution is not specified, use the default screen
237 if (local_resolution
.IsEmpty()) {
238 local_resolution
= ScreenResolution(
239 webrtc::DesktopSize(kDefaultRdpScreenWidth
, kDefaultRdpScreenHeight
),
240 webrtc::DesktopVector(kDefaultRdpDpi
, kDefaultRdpDpi
));
243 // Get the screen dimensions assuming the default DPI.
244 webrtc::DesktopSize host_size
= local_resolution
.ScaleDimensionsToDpi(
245 webrtc::DesktopVector(kDefaultRdpDpi
, kDefaultRdpDpi
));
247 // Make sure that the host resolution is within the limits supported by RDP.
248 host_size
= webrtc::DesktopSize(
249 std::min(kMaxRdpScreenWidth
,
250 std::max(kMinRdpScreenWidth
, host_size
.width())),
251 std::min(kMaxRdpScreenHeight
,
252 std::max(kMinRdpScreenHeight
, host_size
.height())));
254 // Create an RDP session.
255 base::win::ScopedComPtr
<IRdpDesktopSessionEventHandler
> event_handler(
256 new EventHandler(weak_factory_
.GetWeakPtr()));
257 terminal_id_
= base::GenerateGUID();
258 base::win::ScopedBstr
terminal_id(base::UTF8ToUTF16(terminal_id_
).c_str());
259 result
= rdp_desktop_session_
->Connect(host_size
.width(), host_size
.height(),
260 terminal_id
, event_handler
.get());
261 if (FAILED(result
)) {
262 LOG(ERROR
) << "RdpSession::Create() failed, 0x"
263 << std::hex
<< result
<< std::dec
<< ".";
270 void RdpSession::OnRdpConnected() {
271 DCHECK(caller_task_runner()->BelongsToCurrentThread());
274 StartMonitoring(terminal_id_
);
277 void RdpSession::OnRdpClosed() {
278 DCHECK(caller_task_runner()->BelongsToCurrentThread());
283 void RdpSession::SetScreenResolution(const ScreenResolution
& resolution
) {
284 DCHECK(caller_task_runner()->BelongsToCurrentThread());
286 // TODO(alexeypa): implement resize-to-client for RDP sessions here.
287 // See http://crbug.com/137696.
291 void RdpSession::InjectSas() {
292 DCHECK(caller_task_runner()->BelongsToCurrentThread());
294 rdp_desktop_session_
->InjectSas();
297 RdpSession::EventHandler::EventHandler(
298 base::WeakPtr
<RdpSession
> desktop_session
)
300 desktop_session_(desktop_session
) {
303 RdpSession::EventHandler::~EventHandler() {
304 DCHECK(thread_checker_
.CalledOnValidThread());
306 if (desktop_session_
)
307 desktop_session_
->OnRdpClosed();
310 ULONG STDMETHODCALLTYPE
RdpSession::EventHandler::AddRef() {
311 DCHECK(thread_checker_
.CalledOnValidThread());
316 ULONG STDMETHODCALLTYPE
RdpSession::EventHandler::Release() {
317 DCHECK(thread_checker_
.CalledOnValidThread());
319 if (--ref_count_
== 0) {
327 STDMETHODIMP
RdpSession::EventHandler::QueryInterface(REFIID riid
, void** ppv
) {
328 DCHECK(thread_checker_
.CalledOnValidThread());
330 if (riid
== IID_IUnknown
||
331 riid
== IID_IRdpDesktopSessionEventHandler
) {
332 *ppv
= static_cast<IRdpDesktopSessionEventHandler
*>(this);
338 return E_NOINTERFACE
;
341 STDMETHODIMP
RdpSession::EventHandler::OnRdpConnected() {
342 DCHECK(thread_checker_
.CalledOnValidThread());
344 if (desktop_session_
)
345 desktop_session_
->OnRdpConnected();
350 STDMETHODIMP
RdpSession::EventHandler::OnRdpClosed() {
351 DCHECK(thread_checker_
.CalledOnValidThread());
353 if (!desktop_session_
)
356 base::WeakPtr
<RdpSession
> desktop_session
= desktop_session_
;
357 desktop_session_
.reset();
358 desktop_session
->OnRdpClosed();
365 scoped_ptr
<DesktopSession
> DesktopSessionWin::CreateForConsole(
366 scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner
,
367 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner
,
368 DaemonProcess
* daemon_process
,
370 const ScreenResolution
& resolution
) {
371 scoped_ptr
<ConsoleSession
> session(new ConsoleSession(
372 caller_task_runner
, io_task_runner
, daemon_process
, id
,
373 HostService::GetInstance()));
375 return session
.Pass();
379 scoped_ptr
<DesktopSession
> DesktopSessionWin::CreateForVirtualTerminal(
380 scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner
,
381 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner
,
382 DaemonProcess
* daemon_process
,
384 const ScreenResolution
& resolution
) {
385 scoped_ptr
<RdpSession
> session(new RdpSession(
386 caller_task_runner
, io_task_runner
, daemon_process
, id
,
387 HostService::GetInstance()));
388 if (!session
->Initialize(resolution
))
391 return session
.Pass();
394 DesktopSessionWin::DesktopSessionWin(
395 scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner
,
396 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner
,
397 DaemonProcess
* daemon_process
,
399 WtsTerminalMonitor
* monitor
)
400 : DesktopSession(daemon_process
, id
),
401 caller_task_runner_(caller_task_runner
),
402 io_task_runner_(io_task_runner
),
404 monitoring_notifications_(false) {
405 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
407 ReportElapsedTime("created");
410 DesktopSessionWin::~DesktopSessionWin() {
411 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
416 void DesktopSessionWin::OnSessionAttachTimeout() {
417 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
419 LOG(ERROR
) << "Session attach notification didn't arrived within "
420 << kSessionAttachTimeoutSeconds
<< " seconds.";
424 void DesktopSessionWin::StartMonitoring(const std::string
& terminal_id
) {
425 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
426 DCHECK(!monitoring_notifications_
);
427 DCHECK(!session_attach_timer_
.IsRunning());
429 ReportElapsedTime("started monitoring");
431 session_attach_timer_
.Start(
432 FROM_HERE
, base::TimeDelta::FromSeconds(kSessionAttachTimeoutSeconds
),
433 this, &DesktopSessionWin::OnSessionAttachTimeout
);
435 monitoring_notifications_
= true;
436 monitor_
->AddWtsTerminalObserver(terminal_id
, this);
439 void DesktopSessionWin::StopMonitoring() {
440 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
442 if (monitoring_notifications_
) {
443 ReportElapsedTime("stopped monitoring");
445 monitoring_notifications_
= false;
446 monitor_
->RemoveWtsTerminalObserver(this);
449 session_attach_timer_
.Stop();
453 void DesktopSessionWin::TerminateSession() {
454 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
458 // This call will delete |this| so it should be at the very end of the method.
459 daemon_process()->CloseDesktopSession(id());
462 void DesktopSessionWin::OnChannelConnected(int32 peer_pid
) {
463 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
465 ReportElapsedTime("channel connected");
467 // Obtain the handle of the desktop process. It will be passed to the network
468 // process to use to duplicate handles of shared memory objects from
469 // the desktop process.
470 desktop_process_
.Set(OpenProcess(PROCESS_DUP_HANDLE
, false, peer_pid
));
471 if (!desktop_process_
.IsValid()) {
472 CrashDesktopProcess(FROM_HERE
);
476 VLOG(1) << "IPC: daemon <- desktop (" << peer_pid
<< ")";
479 bool DesktopSessionWin::OnMessageReceived(const IPC::Message
& message
) {
480 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
483 IPC_BEGIN_MESSAGE_MAP(DesktopSessionWin
, message
)
484 IPC_MESSAGE_HANDLER(ChromotingDesktopDaemonMsg_DesktopAttached
,
485 OnDesktopSessionAgentAttached
)
486 IPC_MESSAGE_HANDLER(ChromotingDesktopDaemonMsg_InjectSas
,
488 IPC_MESSAGE_UNHANDLED(handled
= false)
489 IPC_END_MESSAGE_MAP()
492 LOG(ERROR
) << "Received unexpected IPC type: " << message
.type();
493 CrashDesktopProcess(FROM_HERE
);
499 void DesktopSessionWin::OnPermanentError(int exit_code
) {
500 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
505 void DesktopSessionWin::OnSessionAttached(uint32 session_id
) {
506 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
508 DCHECK(monitoring_notifications_
);
510 ReportElapsedTime("attached");
512 // Launch elevated on Win8 to be able to inject Alt+Tab.
513 bool launch_elevated
= base::win::GetVersion() >= base::win::VERSION_WIN8
;
515 // Get the name of the executable to run. |kDesktopBinaryName| specifies
516 // uiAccess="true" in it's manifest.
517 base::FilePath desktop_binary
;
519 if (launch_elevated
) {
520 result
= GetInstalledBinaryPath(kDesktopBinaryName
, &desktop_binary
);
522 result
= GetInstalledBinaryPath(kHostBinaryName
, &desktop_binary
);
530 session_attach_timer_
.Stop();
532 scoped_ptr
<base::CommandLine
> target(new base::CommandLine(desktop_binary
));
533 target
->AppendSwitchASCII(kProcessTypeSwitchName
, kProcessTypeDesktop
);
534 // Copy the command line switches enabling verbose logging.
535 target
->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
536 kCopiedSwitchNames
, arraysize(kCopiedSwitchNames
));
538 // Create a delegate capable of launching a process in a different session.
539 scoped_ptr
<WtsSessionProcessDelegate
> delegate(
540 new WtsSessionProcessDelegate(io_task_runner_
,
544 kDaemonIpcSecurityDescriptor
)));
545 if (!delegate
->Initialize(session_id
)) {
550 // Create a launcher for the desktop process, using the per-session delegate.
551 launcher_
.reset(new WorkerProcessLauncher(delegate
.Pass(), this));
554 void DesktopSessionWin::OnSessionDetached() {
555 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
559 if (monitoring_notifications_
) {
560 ReportElapsedTime("detached");
562 session_attach_timer_
.Start(
563 FROM_HERE
, base::TimeDelta::FromSeconds(kSessionAttachTimeoutSeconds
),
564 this, &DesktopSessionWin::OnSessionAttachTimeout
);
568 void DesktopSessionWin::OnDesktopSessionAgentAttached(
569 IPC::PlatformFileForTransit desktop_pipe
) {
570 if (!daemon_process()->OnDesktopSessionAgentAttached(id(),
571 desktop_process_
.Get(),
573 CrashDesktopProcess(FROM_HERE
);
577 void DesktopSessionWin::CrashDesktopProcess(
578 const tracked_objects::Location
& location
) {
579 DCHECK(caller_task_runner_
->BelongsToCurrentThread());
581 launcher_
->Crash(location
);
584 void DesktopSessionWin::ReportElapsedTime(const std::string
& event
) {
585 base::Time now
= base::Time::Now();
588 if (!last_timestamp_
.is_null()) {
589 passed
= base::StringPrintf(", %.2fs passed",
590 (now
- last_timestamp_
).InSecondsF());
593 base::Time::Exploded exploded
;
594 now
.LocalExplode(&exploded
);
595 VLOG(1) << base::StringPrintf("session(%d): %s at %02d:%02d:%02d.%03d%s",
601 exploded
.millisecond
,
604 last_timestamp_
= now
;
607 } // namespace remoting