[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "remoting/host/desktop_session_win.h" |
| 6 | |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 7 | #include <objbase.h> |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 8 | #include <sddl.h> |
Robert Liao | 1a37835 | 2017-10-18 01:31:17 | [diff] [blame] | 9 | #include <wrl/client.h> |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 10 | |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 11 | #include <limits> |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 12 | #include <memory> |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 13 | #include <utility> |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 14 | |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 15 | #include "base/base_switches.h" |
| 16 | #include "base/command_line.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 17 | #include "base/files/file_path.h" |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 18 | #include "base/guid.h" |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 19 | #include "base/macros.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 20 | #include "base/memory/ref_counted.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 21 | #include "base/memory/weak_ptr.h" |
[email protected] | eaf9253 | 2013-06-11 07:39:19 | [diff] [blame] | 22 | #include "base/strings/stringprintf.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 23 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 24 | #include "base/threading/thread_checker.h" |
[email protected] | 5d7eb86 | 2013-06-28 15:21:24 | [diff] [blame] | 25 | #include "base/timer/timer.h" |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 26 | #include "base/win/registry.h" |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 27 | #include "base/win/scoped_bstr.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 28 | #include "base/win/scoped_handle.h" |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 29 | #include "base/win/windows_version.h" |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 30 | #include "ipc/ipc_message_macros.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 31 | #include "ipc/ipc_platform_file.h" |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 32 | #include "remoting/base/auto_thread_task_runner.h" |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 33 | #include "remoting/host/chromoting_messages.h" |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 34 | #include "remoting/host/daemon_process.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 35 | #include "remoting/host/desktop_session.h" |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 36 | #include "remoting/host/host_main.h" |
| 37 | #include "remoting/host/ipc_constants.h" |
[email protected] | 0dffd55 | 2012-12-07 01:08:09 | [diff] [blame] | 38 | #include "remoting/host/sas_injector.h" |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 39 | #include "remoting/host/screen_resolution.h" |
Zijie He | 9bb4149 | 2017-07-30 21:14:49 | [diff] [blame] | 40 | #include "remoting/host/switches.h" |
nicholss | e3320ae | 2016-09-16 20:12:59 | [diff] [blame] | 41 | // MIDL-generated declarations and definitions. |
| 42 | #include "remoting/host/win/chromoting_lib.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 43 | #include "remoting/host/win/host_service.h" |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 44 | #include "remoting/host/win/worker_process_launcher.h" |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 45 | #include "remoting/host/win/wts_session_process_delegate.h" |
[email protected] | 3fd005e2 | 2013-02-28 20:11:29 | [diff] [blame] | 46 | #include "remoting/host/win/wts_terminal_monitor.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 47 | #include "remoting/host/win/wts_terminal_observer.h" |
| 48 | #include "remoting/host/worker_process_ipc_delegate.h" |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 49 | |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 50 | using base::win::ScopedHandle; |
| 51 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 52 | namespace remoting { |
| 53 | |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 54 | namespace { |
| 55 | |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 56 | // The security descriptor of the daemon IPC endpoint. It gives full access |
[email protected] | 6d65223d | 2013-03-05 20:32:46 | [diff] [blame] | 57 | // to SYSTEM and denies access by anyone else. |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 58 | const wchar_t kDaemonIpcSecurityDescriptor[] = |
| 59 | SDDL_OWNER L":" SDDL_LOCAL_SYSTEM |
| 60 | SDDL_GROUP L":" SDDL_LOCAL_SYSTEM |
| 61 | SDDL_DACL L":(" |
| 62 | SDDL_ACCESS_ALLOWED L";;" SDDL_GENERIC_ALL L";;;" SDDL_LOCAL_SYSTEM |
| 63 | L")"; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 64 | |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 65 | // The command line parameters that should be copied from the service's command |
[email protected] | f0ce8fbb | 2014-06-16 18:36:17 | [diff] [blame] | 66 | // line to the desktop process. |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 67 | const char* kCopiedSwitchNames[] = { switches::kV, switches::kVModule }; |
| 68 | |
[email protected] | 3c6ae17 | 2013-03-18 08:50:58 | [diff] [blame] | 69 | // The default screen dimensions for an RDP session. |
[email protected] | 792c4f4 | 2013-04-06 16:05:21 | [diff] [blame] | 70 | const int kDefaultRdpScreenWidth = 1280; |
[email protected] | 3c6ae17 | 2013-03-18 08:50:58 | [diff] [blame] | 71 | const int kDefaultRdpScreenHeight = 768; |
| 72 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 73 | // RDC 6.1 (W2K8) supports dimensions of up to 4096x2048. |
| 74 | const int kMaxRdpScreenWidth = 4096; |
| 75 | const int kMaxRdpScreenHeight = 2048; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 76 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 77 | // The minimum effective screen dimensions supported by Windows are 800x600. |
| 78 | const int kMinRdpScreenWidth = 800; |
| 79 | const int kMinRdpScreenHeight = 600; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 80 | |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 81 | // Default dots per inch used by RDP is 96 DPI. |
| 82 | const int kDefaultRdpDpi = 96; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 83 | |
| 84 | // The session attach notification should arrive within 30 seconds. |
| 85 | const int kSessionAttachTimeoutSeconds = 30; |
| 86 | |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 87 | // The default port number used for establishing an RDP session. |
| 88 | const int kDefaultRdpPort = 3389; |
| 89 | |
joedow | 0bf730db | 2016-05-03 20:09:01 | [diff] [blame] | 90 | // Used for validating the required RDP registry values. |
| 91 | const int kRdpConnectionsDisabled = 1; |
| 92 | const int kNetworkLevelAuthEnabled = 1; |
| 93 | const int kSecurityLayerTlsRequired = 2; |
| 94 | |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 95 | // The values used to establish RDP connections are stored in the registry. |
joedow | 0bf730db | 2016-05-03 20:09:01 | [diff] [blame] | 96 | const wchar_t kRdpSettingsKeyName[] = |
| 97 | L"SYSTEM\\CurrentControlSet\\Control\\Terminal Server"; |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 98 | const wchar_t kRdpTcpSettingsKeyName[] = L"SYSTEM\\CurrentControlSet\\" |
| 99 | L"Control\\Terminal Server\\WinStations\\RDP-Tcp"; |
| 100 | const wchar_t kRdpPortValueName[] = L"PortNumber"; |
joedow | 0bf730db | 2016-05-03 20:09:01 | [diff] [blame] | 101 | const wchar_t kDenyTsConnectionsValueName[] = L"fDenyTSConnections"; |
| 102 | const wchar_t kNetworkLevelAuthValueName[] = L"UserAuthentication"; |
| 103 | const wchar_t kSecurityLayerValueName[] = L"SecurityLayer"; |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 104 | |
joedow | b7a9ca3 | 2016-12-23 00:15:25 | [diff] [blame] | 105 | webrtc::DesktopSize GetBoundedRdpDesktopSize(int width, int height) { |
| 106 | return webrtc::DesktopSize( |
| 107 | std::min(kMaxRdpScreenWidth, std::max(kMinRdpScreenWidth, width)), |
| 108 | std::min(kMaxRdpScreenHeight, std::max(kMinRdpScreenHeight, height))); |
| 109 | } |
| 110 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 111 | // DesktopSession implementation which attaches to the host's physical console. |
| 112 | // Receives IPC messages from the desktop process, running in the console |
| 113 | // session, via |WorkerProcessIpcDelegate|, and monitors console session |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 114 | // attach/detach events via |WtsConsoleObserver|. |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 115 | class ConsoleSession : public DesktopSessionWin { |
| 116 | public: |
| 117 | // Same as DesktopSessionWin(). |
| 118 | ConsoleSession( |
| 119 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 120 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 121 | DaemonProcess* daemon_process, |
| 122 | int id, |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 123 | WtsTerminalMonitor* monitor); |
nick | 697f429 | 2015-04-23 18:22:31 | [diff] [blame] | 124 | ~ConsoleSession() override; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 125 | |
| 126 | protected: |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 127 | // DesktopSession overrides. |
nick | 697f429 | 2015-04-23 18:22:31 | [diff] [blame] | 128 | void SetScreenResolution(const ScreenResolution& resolution) override; |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 129 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 130 | // DesktopSessionWin overrides. |
nick | 697f429 | 2015-04-23 18:22:31 | [diff] [blame] | 131 | void InjectSas() override; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 132 | |
| 133 | private: |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 134 | std::unique_ptr<SasInjector> sas_injector_; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 135 | |
| 136 | DISALLOW_COPY_AND_ASSIGN(ConsoleSession); |
| 137 | }; |
| 138 | |
| 139 | // DesktopSession implementation which attaches to virtual RDP console. |
| 140 | // Receives IPC messages from the desktop process, running in the console |
| 141 | // session, via |WorkerProcessIpcDelegate|, and monitors console session |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 142 | // attach/detach events via |WtsConsoleObserver|. |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 143 | class RdpSession : public DesktopSessionWin { |
| 144 | public: |
| 145 | // Same as DesktopSessionWin(). |
| 146 | RdpSession( |
| 147 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 148 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 149 | DaemonProcess* daemon_process, |
| 150 | int id, |
| 151 | WtsTerminalMonitor* monitor); |
nick | 697f429 | 2015-04-23 18:22:31 | [diff] [blame] | 152 | ~RdpSession() override; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 153 | |
| 154 | // Performs the part of initialization that can fail. |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 155 | bool Initialize(const ScreenResolution& resolution); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 156 | |
| 157 | // Mirrors IRdpDesktopSessionEventHandler. |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 158 | void OnRdpConnected(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 159 | void OnRdpClosed(); |
| 160 | |
| 161 | protected: |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 162 | // DesktopSession overrides. |
nick | 697f429 | 2015-04-23 18:22:31 | [diff] [blame] | 163 | void SetScreenResolution(const ScreenResolution& resolution) override; |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 164 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 165 | // DesktopSessionWin overrides. |
nick | 697f429 | 2015-04-23 18:22:31 | [diff] [blame] | 166 | void InjectSas() override; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 167 | |
| 168 | private: |
| 169 | // An implementation of IRdpDesktopSessionEventHandler interface that forwards |
| 170 | // notifications to the owning desktop session. |
| 171 | class EventHandler : public IRdpDesktopSessionEventHandler { |
| 172 | public: |
| 173 | explicit EventHandler(base::WeakPtr<RdpSession> desktop_session); |
| 174 | virtual ~EventHandler(); |
| 175 | |
| 176 | // IUnknown interface. |
mostynb | 11d989c | 2014-10-08 16:58:09 | [diff] [blame] | 177 | STDMETHOD_(ULONG, AddRef)() override; |
| 178 | STDMETHOD_(ULONG, Release)() override; |
| 179 | STDMETHOD(QueryInterface)(REFIID riid, void** ppv) override; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 180 | |
| 181 | // IRdpDesktopSessionEventHandler interface. |
mostynb | 11d989c | 2014-10-08 16:58:09 | [diff] [blame] | 182 | STDMETHOD(OnRdpConnected)() override; |
| 183 | STDMETHOD(OnRdpClosed)() override; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 184 | |
| 185 | private: |
| 186 | ULONG ref_count_; |
| 187 | |
| 188 | // Points to the desktop session object receiving OnRdpXxx() notifications. |
| 189 | base::WeakPtr<RdpSession> desktop_session_; |
| 190 | |
| 191 | // This class must be used on a single thread. |
| 192 | base::ThreadChecker thread_checker_; |
| 193 | |
| 194 | DISALLOW_COPY_AND_ASSIGN(EventHandler); |
| 195 | }; |
| 196 | |
joedow | 0bf730db | 2016-05-03 20:09:01 | [diff] [blame] | 197 | // Examines the system settings required to establish an RDP session. |
| 198 | // This method returns false if the values are retrieved and any of them would |
| 199 | // prevent us from creating an RDP connection. |
| 200 | bool VerifyRdpSettings(); |
| 201 | |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 202 | // Retrieves a DWORD value from the registry. Returns true on success. |
| 203 | bool RetrieveDwordRegistryValue(const wchar_t* key_name, |
| 204 | const wchar_t* value_name, |
| 205 | DWORD* value); |
| 206 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 207 | // Used to create an RDP desktop session. |
Robert Liao | 1a37835 | 2017-10-18 01:31:17 | [diff] [blame] | 208 | Microsoft::WRL::ComPtr<IRdpDesktopSession> rdp_desktop_session_; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 209 | |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 210 | // Used to match |rdp_desktop_session_| with the session it is attached to. |
| 211 | std::string terminal_id_; |
| 212 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 213 | base::WeakPtrFactory<RdpSession> weak_factory_; |
| 214 | |
| 215 | DISALLOW_COPY_AND_ASSIGN(RdpSession); |
| 216 | }; |
| 217 | |
| 218 | ConsoleSession::ConsoleSession( |
| 219 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 220 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 221 | DaemonProcess* daemon_process, |
| 222 | int id, |
| 223 | WtsTerminalMonitor* monitor) |
| 224 | : DesktopSessionWin(caller_task_runner, io_task_runner, daemon_process, id, |
| 225 | monitor) { |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 226 | StartMonitoring(WtsTerminalMonitor::kConsole); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | ConsoleSession::~ConsoleSession() { |
| 230 | } |
| 231 | |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 232 | void ConsoleSession::SetScreenResolution(const ScreenResolution& resolution) { |
| 233 | // Do nothing. The screen resolution of the console session is controlled by |
| 234 | // the DesktopSessionAgent instance running in that session. |
| 235 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 236 | } |
| 237 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 238 | void ConsoleSession::InjectSas() { |
[email protected] | dd8116608 | 2013-03-18 20:33:33 | [diff] [blame] | 239 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 240 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 241 | if (!sas_injector_) |
| 242 | sas_injector_ = SasInjector::Create(); |
| 243 | if (!sas_injector_->InjectSas()) |
| 244 | LOG(ERROR) << "Failed to inject Secure Attention Sequence."; |
| 245 | } |
| 246 | |
| 247 | RdpSession::RdpSession( |
| 248 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 249 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 250 | DaemonProcess* daemon_process, |
| 251 | int id, |
| 252 | WtsTerminalMonitor* monitor) |
| 253 | : DesktopSessionWin(caller_task_runner, io_task_runner, daemon_process, id, |
| 254 | monitor), |
[email protected] | aa46ba5 | 2013-04-27 00:00:44 | [diff] [blame] | 255 | weak_factory_(this) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | RdpSession::~RdpSession() { |
| 259 | } |
| 260 | |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 261 | bool RdpSession::Initialize(const ScreenResolution& resolution) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 262 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 263 | |
joedow | 0bf730db | 2016-05-03 20:09:01 | [diff] [blame] | 264 | if (!VerifyRdpSettings()) { |
| 265 | LOG(ERROR) << "Could not create an RDP session due to invalid settings."; |
| 266 | return false; |
| 267 | } |
| 268 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 269 | // Create the RDP wrapper object. |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 270 | HRESULT result = |
| 271 | ::CoCreateInstance(__uuidof(RdpDesktopSession), nullptr, CLSCTX_ALL, |
| 272 | IID_PPV_ARGS(&rdp_desktop_session_)); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 273 | if (FAILED(result)) { |
| 274 | LOG(ERROR) << "Failed to create RdpSession object, 0x" |
| 275 | << std::hex << result << std::dec << "."; |
| 276 | return false; |
| 277 | } |
| 278 | |
[email protected] | 3c6ae17 | 2013-03-18 08:50:58 | [diff] [blame] | 279 | ScreenResolution local_resolution = resolution; |
| 280 | |
| 281 | // If the screen resolution is not specified, use the default screen |
| 282 | // resolution. |
| 283 | if (local_resolution.IsEmpty()) { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 284 | local_resolution = ScreenResolution( |
| 285 | webrtc::DesktopSize(kDefaultRdpScreenWidth, kDefaultRdpScreenHeight), |
| 286 | webrtc::DesktopVector(kDefaultRdpDpi, kDefaultRdpDpi)); |
[email protected] | 3c6ae17 | 2013-03-18 08:50:58 | [diff] [blame] | 287 | } |
| 288 | |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 289 | // Get the screen dimensions assuming the default DPI. |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 290 | webrtc::DesktopSize host_size = local_resolution.ScaleDimensionsToDpi( |
| 291 | webrtc::DesktopVector(kDefaultRdpDpi, kDefaultRdpDpi)); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 292 | |
| 293 | // Make sure that the host resolution is within the limits supported by RDP. |
joedow | b7a9ca3 | 2016-12-23 00:15:25 | [diff] [blame] | 294 | host_size = GetBoundedRdpDesktopSize(host_size.width(), host_size.height()); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 295 | |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 296 | // Read the port number used by RDP. |
| 297 | DWORD server_port = kDefaultRdpPort; |
| 298 | if (RetrieveDwordRegistryValue(kRdpTcpSettingsKeyName, kRdpPortValueName, |
| 299 | &server_port) && |
| 300 | server_port > 65535) { |
| 301 | LOG(ERROR) << "Invalid RDP port specified: " << server_port; |
| 302 | return false; |
| 303 | } |
| 304 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 305 | // Create an RDP session. |
Robert Liao | 1a37835 | 2017-10-18 01:31:17 | [diff] [blame] | 306 | Microsoft::WRL::ComPtr<IRdpDesktopSessionEventHandler> event_handler( |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 307 | new EventHandler(weak_factory_.GetWeakPtr())); |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 308 | terminal_id_ = base::GenerateGUID(); |
[email protected] | 6c3bf03 | 2013-12-25 19:37:03 | [diff] [blame] | 309 | base::win::ScopedBstr terminal_id(base::UTF8ToUTF16(terminal_id_).c_str()); |
dcheng | 858fd17 | 2014-11-13 01:17:30 | [diff] [blame] | 310 | result = rdp_desktop_session_->Connect(host_size.width(), host_size.height(), |
joedow | b7a9ca3 | 2016-12-23 00:15:25 | [diff] [blame] | 311 | kDefaultRdpDpi, kDefaultRdpDpi, |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 312 | terminal_id, server_port, |
robliao | b44b907 | 2017-04-19 00:28:09 | [diff] [blame] | 313 | event_handler.Get()); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 314 | if (FAILED(result)) { |
| 315 | LOG(ERROR) << "RdpSession::Create() failed, 0x" |
| 316 | << std::hex << result << std::dec << "."; |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | return true; |
| 321 | } |
| 322 | |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 323 | void RdpSession::OnRdpConnected() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 324 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 325 | |
| 326 | StopMonitoring(); |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 327 | StartMonitoring(terminal_id_); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void RdpSession::OnRdpClosed() { |
| 331 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 332 | |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 333 | TerminateSession(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 334 | } |
| 335 | |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 336 | void RdpSession::SetScreenResolution(const ScreenResolution& resolution) { |
| 337 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
joedow | b7a9ca3 | 2016-12-23 00:15:25 | [diff] [blame] | 338 | DCHECK(!resolution.IsEmpty()); |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 339 | |
joedow | b7a9ca3 | 2016-12-23 00:15:25 | [diff] [blame] | 340 | webrtc::DesktopSize new_size = resolution.ScaleDimensionsToDpi( |
| 341 | webrtc::DesktopVector(kDefaultRdpDpi, kDefaultRdpDpi)); |
| 342 | new_size = GetBoundedRdpDesktopSize(new_size.width(), new_size.height()); |
| 343 | |
| 344 | rdp_desktop_session_->ChangeResolution(new_size.width(), new_size.height(), |
| 345 | kDefaultRdpDpi, kDefaultRdpDpi); |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 346 | } |
| 347 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 348 | void RdpSession::InjectSas() { |
| 349 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 350 | |
[email protected] | 97328ee | 2013-06-04 06:46:59 | [diff] [blame] | 351 | rdp_desktop_session_->InjectSas(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 352 | } |
| 353 | |
joedow | 0bf730db | 2016-05-03 20:09:01 | [diff] [blame] | 354 | bool RdpSession::VerifyRdpSettings() { |
| 355 | // Verify RDP connections are enabled. |
| 356 | DWORD deny_ts_connections_flag = 0; |
| 357 | if (RetrieveDwordRegistryValue(kRdpSettingsKeyName, |
| 358 | kDenyTsConnectionsValueName, |
| 359 | &deny_ts_connections_flag) && |
| 360 | deny_ts_connections_flag == kRdpConnectionsDisabled) { |
| 361 | LOG(ERROR) << "RDP Connections must be enabled."; |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | // Verify Network Level Authentication is disabled. |
| 366 | DWORD network_level_auth_flag = 0; |
| 367 | if (RetrieveDwordRegistryValue(kRdpTcpSettingsKeyName, |
| 368 | kNetworkLevelAuthValueName, |
| 369 | &network_level_auth_flag) && |
| 370 | network_level_auth_flag == kNetworkLevelAuthEnabled) { |
| 371 | LOG(ERROR) << "Network Level Authentication for RDP must be disabled."; |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | // Verify Security Layer is not set to TLS. It can be either of the other two |
| 376 | // values, but forcing TLS will prevent us from establishing a connection. |
| 377 | DWORD security_layer_flag = 0; |
| 378 | if (RetrieveDwordRegistryValue(kRdpTcpSettingsKeyName, |
| 379 | kSecurityLayerValueName, |
| 380 | &security_layer_flag) && |
| 381 | security_layer_flag == kSecurityLayerTlsRequired) { |
| 382 | LOG(ERROR) << "RDP SecurityLayer must not be set to TLS."; |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | return true; |
| 387 | } |
| 388 | |
joedow | 69094d5f | 2016-05-03 20:00:02 | [diff] [blame] | 389 | bool RdpSession::RetrieveDwordRegistryValue(const wchar_t* key_name, |
| 390 | const wchar_t* value_name, |
| 391 | DWORD* value) { |
| 392 | DCHECK(key_name); |
| 393 | DCHECK(value_name); |
| 394 | DCHECK(value); |
| 395 | |
| 396 | base::win::RegKey key(HKEY_LOCAL_MACHINE, key_name, KEY_READ); |
| 397 | if (!key.Valid()) { |
| 398 | LOG(WARNING) << "Failed to open key: " << key_name; |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | if (key.ReadValueDW(value_name, value) != ERROR_SUCCESS) { |
| 403 | LOG(WARNING) << "Failed to read registry value: " << value_name; |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | return true; |
| 408 | } |
| 409 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 410 | RdpSession::EventHandler::EventHandler( |
| 411 | base::WeakPtr<RdpSession> desktop_session) |
| 412 | : ref_count_(0), |
| 413 | desktop_session_(desktop_session) { |
| 414 | } |
| 415 | |
| 416 | RdpSession::EventHandler::~EventHandler() { |
| 417 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 418 | |
[email protected] | 1bf0623 | 2013-06-07 13:50:24 | [diff] [blame] | 419 | if (desktop_session_) |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 420 | desktop_session_->OnRdpClosed(); |
| 421 | } |
| 422 | |
| 423 | ULONG STDMETHODCALLTYPE RdpSession::EventHandler::AddRef() { |
| 424 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 425 | |
| 426 | return ++ref_count_; |
| 427 | } |
| 428 | |
| 429 | ULONG STDMETHODCALLTYPE RdpSession::EventHandler::Release() { |
| 430 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 431 | |
| 432 | if (--ref_count_ == 0) { |
| 433 | delete this; |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | return ref_count_; |
| 438 | } |
| 439 | |
| 440 | STDMETHODIMP RdpSession::EventHandler::QueryInterface(REFIID riid, void** ppv) { |
| 441 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 442 | |
| 443 | if (riid == IID_IUnknown || |
| 444 | riid == IID_IRdpDesktopSessionEventHandler) { |
| 445 | *ppv = static_cast<IRdpDesktopSessionEventHandler*>(this); |
| 446 | AddRef(); |
| 447 | return S_OK; |
| 448 | } |
| 449 | |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 450 | *ppv = nullptr; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 451 | return E_NOINTERFACE; |
| 452 | } |
| 453 | |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 454 | STDMETHODIMP RdpSession::EventHandler::OnRdpConnected() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 455 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 456 | |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 457 | if (desktop_session_) |
| 458 | desktop_session_->OnRdpConnected(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 459 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 460 | return S_OK; |
| 461 | } |
| 462 | |
| 463 | STDMETHODIMP RdpSession::EventHandler::OnRdpClosed() { |
| 464 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 465 | |
[email protected] | 1bf0623 | 2013-06-07 13:50:24 | [diff] [blame] | 466 | if (!desktop_session_) |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 467 | return S_OK; |
| 468 | |
| 469 | base::WeakPtr<RdpSession> desktop_session = desktop_session_; |
| 470 | desktop_session_.reset(); |
| 471 | desktop_session->OnRdpClosed(); |
| 472 | return S_OK; |
| 473 | } |
| 474 | |
| 475 | } // namespace |
| 476 | |
| 477 | // static |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 478 | std::unique_ptr<DesktopSession> DesktopSessionWin::CreateForConsole( |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 479 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 480 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 481 | DaemonProcess* daemon_process, |
| 482 | int id, |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 483 | const ScreenResolution& resolution) { |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame^] | 484 | return std::make_unique<ConsoleSession>(caller_task_runner, io_task_runner, |
ricea | 68860bd | 2016-08-22 02:48:56 | [diff] [blame] | 485 | daemon_process, id, |
| 486 | HostService::GetInstance()); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // static |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 490 | std::unique_ptr<DesktopSession> DesktopSessionWin::CreateForVirtualTerminal( |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 491 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 492 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 493 | DaemonProcess* daemon_process, |
| 494 | int id, |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 495 | const ScreenResolution& resolution) { |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 496 | std::unique_ptr<RdpSession> session( |
| 497 | new RdpSession(caller_task_runner, io_task_runner, daemon_process, id, |
| 498 | HostService::GetInstance())); |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 499 | if (!session->Initialize(resolution)) |
sergeyu | afce978 | 2014-09-29 19:38:30 | [diff] [blame] | 500 | return nullptr; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 501 | |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 502 | return std::move(session); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | DesktopSessionWin::DesktopSessionWin( |
| 506 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 507 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 508 | DaemonProcess* daemon_process, |
| 509 | int id, |
[email protected] | 3fd005e2 | 2013-02-28 20:11:29 | [diff] [blame] | 510 | WtsTerminalMonitor* monitor) |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 511 | : DesktopSession(daemon_process, id), |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 512 | caller_task_runner_(caller_task_runner), |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 513 | io_task_runner_(io_task_runner), |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 514 | monitor_(monitor), |
| 515 | monitoring_notifications_(false) { |
| 516 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 517 | |
| 518 | ReportElapsedTime("created"); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | DesktopSessionWin::~DesktopSessionWin() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 522 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 523 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 524 | StopMonitoring(); |
| 525 | } |
| 526 | |
| 527 | void DesktopSessionWin::OnSessionAttachTimeout() { |
| 528 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 529 | |
| 530 | LOG(ERROR) << "Session attach notification didn't arrived within " |
| 531 | << kSessionAttachTimeoutSeconds << " seconds."; |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 532 | TerminateSession(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 533 | } |
| 534 | |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 535 | void DesktopSessionWin::StartMonitoring(const std::string& terminal_id) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 536 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 537 | DCHECK(!monitoring_notifications_); |
| 538 | DCHECK(!session_attach_timer_.IsRunning()); |
| 539 | |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 540 | ReportElapsedTime("started monitoring"); |
| 541 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 542 | session_attach_timer_.Start( |
| 543 | FROM_HERE, base::TimeDelta::FromSeconds(kSessionAttachTimeoutSeconds), |
| 544 | this, &DesktopSessionWin::OnSessionAttachTimeout); |
| 545 | |
| 546 | monitoring_notifications_ = true; |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 547 | monitor_->AddWtsTerminalObserver(terminal_id, this); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void DesktopSessionWin::StopMonitoring() { |
| 551 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 552 | |
| 553 | if (monitoring_notifications_) { |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 554 | ReportElapsedTime("stopped monitoring"); |
| 555 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 556 | monitoring_notifications_ = false; |
| 557 | monitor_->RemoveWtsTerminalObserver(this); |
| 558 | } |
| 559 | |
| 560 | session_attach_timer_.Stop(); |
| 561 | OnSessionDetached(); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 562 | } |
| 563 | |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 564 | void DesktopSessionWin::TerminateSession() { |
| 565 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 566 | |
| 567 | StopMonitoring(); |
| 568 | |
| 569 | // This call will delete |this| so it should be at the very end of the method. |
| 570 | daemon_process()->CloseDesktopSession(id()); |
| 571 | } |
| 572 | |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 573 | void DesktopSessionWin::OnChannelConnected(int32_t peer_pid) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 574 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 575 | |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 576 | ReportElapsedTime("channel connected"); |
| 577 | |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 578 | VLOG(1) << "IPC: daemon <- desktop (" << peer_pid << ")"; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | bool DesktopSessionWin::OnMessageReceived(const IPC::Message& message) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 582 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 583 | |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 584 | bool handled = true; |
| 585 | IPC_BEGIN_MESSAGE_MAP(DesktopSessionWin, message) |
| 586 | IPC_MESSAGE_HANDLER(ChromotingDesktopDaemonMsg_DesktopAttached, |
| 587 | OnDesktopSessionAgentAttached) |
[email protected] | 0dffd55 | 2012-12-07 01:08:09 | [diff] [blame] | 588 | IPC_MESSAGE_HANDLER(ChromotingDesktopDaemonMsg_InjectSas, |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 589 | InjectSas) |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 590 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 591 | IPC_END_MESSAGE_MAP() |
[email protected] | f5f0f40 | 2013-03-05 22:46:38 | [diff] [blame] | 592 | |
| 593 | if (!handled) { |
| 594 | LOG(ERROR) << "Received unexpected IPC type: " << message.type(); |
[email protected] | e9057a6f | 2013-03-08 22:37:55 | [diff] [blame] | 595 | CrashDesktopProcess(FROM_HERE); |
[email protected] | f5f0f40 | 2013-03-05 22:46:38 | [diff] [blame] | 596 | } |
| 597 | |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 598 | return handled; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 599 | } |
| 600 | |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 601 | void DesktopSessionWin::OnPermanentError(int exit_code) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 602 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 603 | |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 604 | TerminateSession(); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 605 | } |
| 606 | |
zijiehe | dda3acb | 2017-06-29 23:19:16 | [diff] [blame] | 607 | void DesktopSessionWin::OnWorkerProcessStopped() {} |
| 608 | |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 609 | void DesktopSessionWin::OnSessionAttached(uint32_t session_id) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 610 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 611 | DCHECK(!launcher_); |
| 612 | DCHECK(monitoring_notifications_); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 613 | |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 614 | ReportElapsedTime("attached"); |
| 615 | |
joedow | 1cdb0481 | 2016-12-13 06:11:15 | [diff] [blame] | 616 | // Launch elevated on Win8+ to enable injection of Alt+Tab and Ctrl+Alt+Del. |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 617 | bool launch_elevated = base::win::GetVersion() >= base::win::VERSION_WIN8; |
| 618 | |
| 619 | // Get the name of the executable to run. |kDesktopBinaryName| specifies |
joedow | 1cdb0481 | 2016-12-13 06:11:15 | [diff] [blame] | 620 | // uiAccess="true" in its manifest. |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 621 | base::FilePath desktop_binary; |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 622 | bool result; |
| 623 | if (launch_elevated) { |
| 624 | result = GetInstalledBinaryPath(kDesktopBinaryName, &desktop_binary); |
| 625 | } else { |
| 626 | result = GetInstalledBinaryPath(kHostBinaryName, &desktop_binary); |
| 627 | } |
| 628 | |
| 629 | if (!result) { |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 630 | TerminateSession(); |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 631 | return; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 632 | } |
| 633 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 634 | session_attach_timer_.Stop(); |
| 635 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 636 | std::unique_ptr<base::CommandLine> target( |
| 637 | new base::CommandLine(desktop_binary)); |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 638 | target->AppendSwitchASCII(kProcessTypeSwitchName, kProcessTypeDesktop); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 639 | // Copy the command line switches enabling verbose logging. |
avi | 429bbdd | 2014-12-23 00:27:27 | [diff] [blame] | 640 | target->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(), |
| 641 | kCopiedSwitchNames, arraysize(kCopiedSwitchNames)); |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 642 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 643 | // Create a delegate capable of launching a process in a different session. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 644 | std::unique_ptr<WtsSessionProcessDelegate> delegate( |
| 645 | new WtsSessionProcessDelegate( |
| 646 | io_task_runner_, std::move(target), launch_elevated, |
joedow | f84fffae | 2016-12-20 03:35:51 | [diff] [blame] | 647 | base::WideToUTF8(kDaemonIpcSecurityDescriptor))); |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 648 | if (!delegate->Initialize(session_id)) { |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 649 | TerminateSession(); |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 650 | return; |
| 651 | } |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 652 | |
| 653 | // Create a launcher for the desktop process, using the per-session delegate. |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 654 | launcher_.reset(new WorkerProcessLauncher(std::move(delegate), this)); |
sammc | f9cd299a | 2016-11-02 22:13:26 | [diff] [blame] | 655 | session_id_ = session_id; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | void DesktopSessionWin::OnSessionDetached() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 659 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 660 | |
| 661 | launcher_.reset(); |
sammc | f9cd299a | 2016-11-02 22:13:26 | [diff] [blame] | 662 | session_id_ = UINT32_MAX; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 663 | |
| 664 | if (monitoring_notifications_) { |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 665 | ReportElapsedTime("detached"); |
| 666 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 667 | session_attach_timer_.Start( |
| 668 | FROM_HERE, base::TimeDelta::FromSeconds(kSessionAttachTimeoutSeconds), |
| 669 | this, &DesktopSessionWin::OnSessionAttachTimeout); |
| 670 | } |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 671 | } |
| 672 | |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 673 | void DesktopSessionWin::OnDesktopSessionAgentAttached( |
sammc | e66272fae | 2016-11-01 03:49:04 | [diff] [blame] | 674 | const IPC::ChannelHandle& desktop_pipe) { |
sammc | f9cd299a | 2016-11-02 22:13:26 | [diff] [blame] | 675 | if (!daemon_process()->OnDesktopSessionAgentAttached(id(), session_id_, |
| 676 | desktop_pipe)) { |
[email protected] | e9057a6f | 2013-03-08 22:37:55 | [diff] [blame] | 677 | CrashDesktopProcess(FROM_HERE); |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
Brett Wilson | 9c36199 | 2017-09-12 06:05:21 | [diff] [blame] | 681 | void DesktopSessionWin::CrashDesktopProcess(const base::Location& location) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 682 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 683 | |
[email protected] | e9057a6f | 2013-03-08 22:37:55 | [diff] [blame] | 684 | launcher_->Crash(location); |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 685 | } |
| 686 | |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 687 | void DesktopSessionWin::ReportElapsedTime(const std::string& event) { |
| 688 | base::Time now = base::Time::Now(); |
| 689 | |
| 690 | std::string passed; |
| 691 | if (!last_timestamp_.is_null()) { |
| 692 | passed = base::StringPrintf(", %.2fs passed", |
| 693 | (now - last_timestamp_).InSecondsF()); |
| 694 | } |
| 695 | |
| 696 | base::Time::Exploded exploded; |
| 697 | now.LocalExplode(&exploded); |
| 698 | VLOG(1) << base::StringPrintf("session(%d): %s at %02d:%02d:%02d.%03d%s", |
| 699 | id(), |
| 700 | event.c_str(), |
| 701 | exploded.hour, |
| 702 | exploded.minute, |
| 703 | exploded.second, |
| 704 | exploded.millisecond, |
| 705 | passed.c_str()); |
| 706 | |
| 707 | last_timestamp_ = now; |
| 708 | } |
| 709 | |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 710 | } // namespace remoting |