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/win/wts_terminal_monitor.h"
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "base/strings/utf_string_conversions.h"
16 // Session id that does not represent any session.
17 const uint32 kInvalidSessionId
= 0xffffffffu
;
19 const char* WtsTerminalMonitor::kConsole
= "console";
21 WtsTerminalMonitor::~WtsTerminalMonitor() {
25 bool WtsTerminalMonitor::LookupTerminalId(uint32 session_id
,
26 std::string
* terminal_id
) {
27 // Fast path for the case when |session_id| is currently attached to
28 // the physical console.
29 if (session_id
== WTSGetActiveConsoleSessionId()) {
30 *terminal_id
= kConsole
;
34 // RdpClient sets the terminal ID as the initial program's working directory.
36 wchar_t* working_directory
;
37 if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE
,
45 bool result
= base::WideToUTF8(working_directory
,
46 (bytes
/ sizeof(wchar_t)) - 1,
48 WTSFreeMemory(working_directory
);
53 uint32
WtsTerminalMonitor::LookupSessionId(const std::string
& terminal_id
) {
54 // Use the fast path if the caller wants to get id of the session attached to
55 // the physical console.
56 if (terminal_id
== kConsole
)
57 return WTSGetActiveConsoleSessionId();
59 // Enumerate all sessions and try to match the client endpoint.
60 WTS_SESSION_INFO
* session_info
;
61 DWORD session_info_count
;
62 if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE
, 0, 1, &session_info
,
63 &session_info_count
)) {
64 PLOG(ERROR
) << "Failed to enumerate all sessions";
65 return kInvalidSessionId
;
67 for (DWORD i
= 0; i
< session_info_count
; ++i
) {
68 uint32 session_id
= session_info
[i
].SessionId
;
71 if (LookupTerminalId(session_id
, &id
) && terminal_id
== id
) {
72 WTSFreeMemory(session_info
);
77 // |terminal_id| is not associated with any session.
78 WTSFreeMemory(session_info
);
79 return kInvalidSessionId
;
82 WtsTerminalMonitor::WtsTerminalMonitor() {
85 } // namespace remoting