OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/win/wts_terminal_monitor.h" | 5 #include "remoting/host/win/wts_terminal_monitor.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <wtsapi32.h> | 8 #include <wtsapi32.h> |
9 | 9 |
10 #include "base/basictypes.h" | |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
13 | 12 |
14 namespace remoting { | 13 namespace remoting { |
15 | 14 |
16 // Session id that does not represent any session. | 15 // Session id that does not represent any session. |
17 const uint32 kInvalidSessionId = 0xffffffffu; | 16 const uint32_t kInvalidSessionId = 0xffffffffu; |
18 | 17 |
19 const char WtsTerminalMonitor::kConsole[] = "console"; | 18 const char WtsTerminalMonitor::kConsole[] = "console"; |
20 | 19 |
21 WtsTerminalMonitor::~WtsTerminalMonitor() { | 20 WtsTerminalMonitor::~WtsTerminalMonitor() { |
22 } | 21 } |
23 | 22 |
24 // static | 23 // static |
25 bool WtsTerminalMonitor::LookupTerminalId(uint32 session_id, | 24 bool WtsTerminalMonitor::LookupTerminalId(uint32_t session_id, |
26 std::string* terminal_id) { | 25 std::string* terminal_id) { |
27 // Fast path for the case when |session_id| is currently attached to | 26 // Fast path for the case when |session_id| is currently attached to |
28 // the physical console. | 27 // the physical console. |
29 if (session_id == WTSGetActiveConsoleSessionId()) { | 28 if (session_id == WTSGetActiveConsoleSessionId()) { |
30 *terminal_id = kConsole; | 29 *terminal_id = kConsole; |
31 return true; | 30 return true; |
32 } | 31 } |
33 | 32 |
34 // RdpClient sets the terminal ID as the initial program's working directory. | 33 // RdpClient sets the terminal ID as the initial program's working directory. |
35 DWORD bytes; | 34 DWORD bytes; |
36 wchar_t* working_directory; | 35 wchar_t* working_directory; |
37 if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, | 36 if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, |
38 session_id, | 37 session_id, |
39 WTSWorkingDirectory, | 38 WTSWorkingDirectory, |
40 &working_directory, | 39 &working_directory, |
41 &bytes)) { | 40 &bytes)) { |
42 return false; | 41 return false; |
43 } | 42 } |
44 | 43 |
45 bool result = base::WideToUTF8(working_directory, | 44 bool result = base::WideToUTF8(working_directory, |
46 (bytes / sizeof(wchar_t)) - 1, | 45 (bytes / sizeof(wchar_t)) - 1, |
47 terminal_id); | 46 terminal_id); |
48 WTSFreeMemory(working_directory); | 47 WTSFreeMemory(working_directory); |
49 return result; | 48 return result; |
50 } | 49 } |
51 | 50 |
52 // static | 51 // static |
53 uint32 WtsTerminalMonitor::LookupSessionId(const std::string& terminal_id) { | 52 uint32_t WtsTerminalMonitor::LookupSessionId(const std::string& terminal_id) { |
54 // Use the fast path if the caller wants to get id of the session attached to | 53 // Use the fast path if the caller wants to get id of the session attached to |
55 // the physical console. | 54 // the physical console. |
56 if (terminal_id == kConsole) | 55 if (terminal_id == kConsole) |
57 return WTSGetActiveConsoleSessionId(); | 56 return WTSGetActiveConsoleSessionId(); |
58 | 57 |
59 // Enumerate all sessions and try to match the client endpoint. | 58 // Enumerate all sessions and try to match the client endpoint. |
60 WTS_SESSION_INFO* session_info; | 59 WTS_SESSION_INFO* session_info; |
61 DWORD session_info_count; | 60 DWORD session_info_count; |
62 if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &session_info, | 61 if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &session_info, |
63 &session_info_count)) { | 62 &session_info_count)) { |
64 PLOG(ERROR) << "Failed to enumerate all sessions"; | 63 PLOG(ERROR) << "Failed to enumerate all sessions"; |
65 return kInvalidSessionId; | 64 return kInvalidSessionId; |
66 } | 65 } |
67 for (DWORD i = 0; i < session_info_count; ++i) { | 66 for (DWORD i = 0; i < session_info_count; ++i) { |
68 uint32 session_id = session_info[i].SessionId; | 67 uint32_t session_id = session_info[i].SessionId; |
69 | 68 |
70 std::string id; | 69 std::string id; |
71 if (LookupTerminalId(session_id, &id) && terminal_id == id) { | 70 if (LookupTerminalId(session_id, &id) && terminal_id == id) { |
72 WTSFreeMemory(session_info); | 71 WTSFreeMemory(session_info); |
73 return session_id; | 72 return session_id; |
74 } | 73 } |
75 } | 74 } |
76 | 75 |
77 // |terminal_id| is not associated with any session. | 76 // |terminal_id| is not associated with any session. |
78 WTSFreeMemory(session_info); | 77 WTSFreeMemory(session_info); |
79 return kInvalidSessionId; | 78 return kInvalidSessionId; |
80 } | 79 } |
81 | 80 |
82 WtsTerminalMonitor::WtsTerminalMonitor() { | 81 WtsTerminalMonitor::WtsTerminalMonitor() { |
83 } | 82 } |
84 | 83 |
85 } // namespace remoting | 84 } // namespace remoting |
OLD | NEW |