[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 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 7 | #include <limits> |
| 8 | #include <sddl.h> |
| 9 | |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 10 | #include "base/base_switches.h" |
| 11 | #include "base/command_line.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 13 | #include "base/guid.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
| 15 | #include "base/memory/scoped_ptr.h" |
| 16 | #include "base/memory/weak_ptr.h" |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 17 | #include "base/path_service.h" |
[email protected] | eaf9253 | 2013-06-11 07:39:19 | [diff] [blame] | 18 | #include "base/strings/stringprintf.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 19 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 20 | #include "base/threading/thread_checker.h" |
[email protected] | 5d7eb86 | 2013-06-28 15:21:24 | [diff] [blame] | 21 | #include "base/timer/timer.h" |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 22 | #include "base/win/scoped_bstr.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 23 | #include "base/win/scoped_comptr.h" |
| 24 | #include "base/win/scoped_handle.h" |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 25 | #include "base/win/windows_version.h" |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 26 | #include "ipc/ipc_message_macros.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 27 | #include "ipc/ipc_platform_file.h" |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 28 | #include "remoting/base/auto_thread_task_runner.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 29 | // MIDL-generated declarations and definitions. |
| 30 | #include "remoting/host/chromoting_lib.h" |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 31 | #include "remoting/host/chromoting_messages.h" |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 32 | #include "remoting/host/daemon_process.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 33 | #include "remoting/host/desktop_session.h" |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 34 | #include "remoting/host/host_main.h" |
| 35 | #include "remoting/host/ipc_constants.h" |
[email protected] | 0dffd55 | 2012-12-07 01:08:09 | [diff] [blame] | 36 | #include "remoting/host/sas_injector.h" |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 37 | #include "remoting/host/screen_resolution.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 38 | #include "remoting/host/win/host_service.h" |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 39 | #include "remoting/host/win/worker_process_launcher.h" |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 40 | #include "remoting/host/win/wts_session_process_delegate.h" |
[email protected] | 3fd005e2 | 2013-02-28 20:11:29 | [diff] [blame] | 41 | #include "remoting/host/win/wts_terminal_monitor.h" |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 42 | #include "remoting/host/win/wts_terminal_observer.h" |
| 43 | #include "remoting/host/worker_process_ipc_delegate.h" |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 44 | |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 45 | using base::win::ScopedHandle; |
| 46 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 47 | namespace remoting { |
| 48 | |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 49 | namespace { |
| 50 | |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 51 | // The security descriptor of the daemon IPC endpoint. It gives full access |
[email protected] | 6d65223d | 2013-03-05 20:32:46 | [diff] [blame] | 52 | // to SYSTEM and denies access by anyone else. |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 53 | const wchar_t kDaemonIpcSecurityDescriptor[] = |
| 54 | SDDL_OWNER L":" SDDL_LOCAL_SYSTEM |
| 55 | SDDL_GROUP L":" SDDL_LOCAL_SYSTEM |
| 56 | SDDL_DACL L":(" |
| 57 | SDDL_ACCESS_ALLOWED L";;" SDDL_GENERIC_ALL L";;;" SDDL_LOCAL_SYSTEM |
| 58 | L")"; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 59 | |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 60 | // The command line parameters that should be copied from the service's command |
[email protected] | f0ce8fbb | 2014-06-16 18:36:17 | [diff] [blame^] | 61 | // line to the desktop process. |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 62 | const char* kCopiedSwitchNames[] = { switches::kV, switches::kVModule }; |
| 63 | |
[email protected] | 3c6ae17 | 2013-03-18 08:50:58 | [diff] [blame] | 64 | // The default screen dimensions for an RDP session. |
[email protected] | 792c4f4 | 2013-04-06 16:05:21 | [diff] [blame] | 65 | const int kDefaultRdpScreenWidth = 1280; |
[email protected] | 3c6ae17 | 2013-03-18 08:50:58 | [diff] [blame] | 66 | const int kDefaultRdpScreenHeight = 768; |
| 67 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 68 | // RDC 6.1 (W2K8) supports dimensions of up to 4096x2048. |
| 69 | const int kMaxRdpScreenWidth = 4096; |
| 70 | const int kMaxRdpScreenHeight = 2048; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 71 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 72 | // The minimum effective screen dimensions supported by Windows are 800x600. |
| 73 | const int kMinRdpScreenWidth = 800; |
| 74 | const int kMinRdpScreenHeight = 600; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 75 | |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 76 | // Default dots per inch used by RDP is 96 DPI. |
| 77 | const int kDefaultRdpDpi = 96; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 78 | |
| 79 | // The session attach notification should arrive within 30 seconds. |
| 80 | const int kSessionAttachTimeoutSeconds = 30; |
| 81 | |
| 82 | // DesktopSession implementation which attaches to the host's physical console. |
| 83 | // Receives IPC messages from the desktop process, running in the console |
| 84 | // session, via |WorkerProcessIpcDelegate|, and monitors console session |
| 85 | // attach/detach events via |WtsConsoleObserer|. |
| 86 | class ConsoleSession : public DesktopSessionWin { |
| 87 | public: |
| 88 | // Same as DesktopSessionWin(). |
| 89 | ConsoleSession( |
| 90 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 91 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 92 | DaemonProcess* daemon_process, |
| 93 | int id, |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 94 | WtsTerminalMonitor* monitor); |
| 95 | virtual ~ConsoleSession(); |
| 96 | |
| 97 | protected: |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 98 | // DesktopSession overrides. |
| 99 | virtual void SetScreenResolution(const ScreenResolution& resolution) OVERRIDE; |
| 100 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 101 | // DesktopSessionWin overrides. |
| 102 | virtual void InjectSas() OVERRIDE; |
| 103 | |
| 104 | private: |
| 105 | scoped_ptr<SasInjector> sas_injector_; |
| 106 | |
| 107 | DISALLOW_COPY_AND_ASSIGN(ConsoleSession); |
| 108 | }; |
| 109 | |
| 110 | // DesktopSession implementation which attaches to virtual RDP console. |
| 111 | // Receives IPC messages from the desktop process, running in the console |
| 112 | // session, via |WorkerProcessIpcDelegate|, and monitors console session |
| 113 | // attach/detach events via |WtsConsoleObserer|. |
| 114 | class RdpSession : public DesktopSessionWin { |
| 115 | public: |
| 116 | // Same as DesktopSessionWin(). |
| 117 | RdpSession( |
| 118 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 119 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 120 | DaemonProcess* daemon_process, |
| 121 | int id, |
| 122 | WtsTerminalMonitor* monitor); |
| 123 | virtual ~RdpSession(); |
| 124 | |
| 125 | // Performs the part of initialization that can fail. |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 126 | bool Initialize(const ScreenResolution& resolution); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 127 | |
| 128 | // Mirrors IRdpDesktopSessionEventHandler. |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 129 | void OnRdpConnected(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 130 | void OnRdpClosed(); |
| 131 | |
| 132 | protected: |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 133 | // DesktopSession overrides. |
| 134 | virtual void SetScreenResolution(const ScreenResolution& resolution) OVERRIDE; |
| 135 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 136 | // DesktopSessionWin overrides. |
| 137 | virtual void InjectSas() OVERRIDE; |
| 138 | |
| 139 | private: |
| 140 | // An implementation of IRdpDesktopSessionEventHandler interface that forwards |
| 141 | // notifications to the owning desktop session. |
| 142 | class EventHandler : public IRdpDesktopSessionEventHandler { |
| 143 | public: |
| 144 | explicit EventHandler(base::WeakPtr<RdpSession> desktop_session); |
| 145 | virtual ~EventHandler(); |
| 146 | |
| 147 | // IUnknown interface. |
| 148 | STDMETHOD_(ULONG, AddRef)() OVERRIDE; |
| 149 | STDMETHOD_(ULONG, Release)() OVERRIDE; |
| 150 | STDMETHOD(QueryInterface)(REFIID riid, void** ppv) OVERRIDE; |
| 151 | |
| 152 | // IRdpDesktopSessionEventHandler interface. |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 153 | STDMETHOD(OnRdpConnected)() OVERRIDE; |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 154 | STDMETHOD(OnRdpClosed)() OVERRIDE; |
| 155 | |
| 156 | private: |
| 157 | ULONG ref_count_; |
| 158 | |
| 159 | // Points to the desktop session object receiving OnRdpXxx() notifications. |
| 160 | base::WeakPtr<RdpSession> desktop_session_; |
| 161 | |
| 162 | // This class must be used on a single thread. |
| 163 | base::ThreadChecker thread_checker_; |
| 164 | |
| 165 | DISALLOW_COPY_AND_ASSIGN(EventHandler); |
| 166 | }; |
| 167 | |
| 168 | // Used to create an RDP desktop session. |
| 169 | base::win::ScopedComPtr<IRdpDesktopSession> rdp_desktop_session_; |
| 170 | |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 171 | // Used to match |rdp_desktop_session_| with the session it is attached to. |
| 172 | std::string terminal_id_; |
| 173 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 174 | base::WeakPtrFactory<RdpSession> weak_factory_; |
| 175 | |
| 176 | DISALLOW_COPY_AND_ASSIGN(RdpSession); |
| 177 | }; |
| 178 | |
| 179 | ConsoleSession::ConsoleSession( |
| 180 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 181 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 182 | DaemonProcess* daemon_process, |
| 183 | int id, |
| 184 | WtsTerminalMonitor* monitor) |
| 185 | : DesktopSessionWin(caller_task_runner, io_task_runner, daemon_process, id, |
| 186 | monitor) { |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 187 | StartMonitoring(WtsTerminalMonitor::kConsole); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | ConsoleSession::~ConsoleSession() { |
| 191 | } |
| 192 | |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 193 | void ConsoleSession::SetScreenResolution(const ScreenResolution& resolution) { |
| 194 | // Do nothing. The screen resolution of the console session is controlled by |
| 195 | // the DesktopSessionAgent instance running in that session. |
| 196 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 197 | } |
| 198 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 199 | void ConsoleSession::InjectSas() { |
[email protected] | dd8116608 | 2013-03-18 20:33:33 | [diff] [blame] | 200 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 201 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 202 | if (!sas_injector_) |
| 203 | sas_injector_ = SasInjector::Create(); |
| 204 | if (!sas_injector_->InjectSas()) |
| 205 | LOG(ERROR) << "Failed to inject Secure Attention Sequence."; |
| 206 | } |
| 207 | |
| 208 | RdpSession::RdpSession( |
| 209 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 210 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 211 | DaemonProcess* daemon_process, |
| 212 | int id, |
| 213 | WtsTerminalMonitor* monitor) |
| 214 | : DesktopSessionWin(caller_task_runner, io_task_runner, daemon_process, id, |
| 215 | monitor), |
[email protected] | aa46ba5 | 2013-04-27 00:00:44 | [diff] [blame] | 216 | weak_factory_(this) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | RdpSession::~RdpSession() { |
| 220 | } |
| 221 | |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 222 | bool RdpSession::Initialize(const ScreenResolution& resolution) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 223 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 224 | |
| 225 | // Create the RDP wrapper object. |
| 226 | HRESULT result = rdp_desktop_session_.CreateInstance( |
| 227 | __uuidof(RdpDesktopSession)); |
| 228 | if (FAILED(result)) { |
| 229 | LOG(ERROR) << "Failed to create RdpSession object, 0x" |
| 230 | << std::hex << result << std::dec << "."; |
| 231 | return false; |
| 232 | } |
| 233 | |
[email protected] | 3c6ae17 | 2013-03-18 08:50:58 | [diff] [blame] | 234 | ScreenResolution local_resolution = resolution; |
| 235 | |
| 236 | // If the screen resolution is not specified, use the default screen |
| 237 | // resolution. |
| 238 | if (local_resolution.IsEmpty()) { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 239 | local_resolution = ScreenResolution( |
| 240 | webrtc::DesktopSize(kDefaultRdpScreenWidth, kDefaultRdpScreenHeight), |
| 241 | webrtc::DesktopVector(kDefaultRdpDpi, kDefaultRdpDpi)); |
[email protected] | 3c6ae17 | 2013-03-18 08:50:58 | [diff] [blame] | 242 | } |
| 243 | |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 244 | // Get the screen dimensions assuming the default DPI. |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 245 | webrtc::DesktopSize host_size = local_resolution.ScaleDimensionsToDpi( |
| 246 | webrtc::DesktopVector(kDefaultRdpDpi, kDefaultRdpDpi)); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 247 | |
| 248 | // Make sure that the host resolution is within the limits supported by RDP. |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 249 | host_size = webrtc::DesktopSize( |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 250 | std::min(kMaxRdpScreenWidth, |
| 251 | std::max(kMinRdpScreenWidth, host_size.width())), |
| 252 | std::min(kMaxRdpScreenHeight, |
| 253 | std::max(kMinRdpScreenHeight, host_size.height()))); |
| 254 | |
| 255 | // Create an RDP session. |
| 256 | base::win::ScopedComPtr<IRdpDesktopSessionEventHandler> event_handler( |
| 257 | new EventHandler(weak_factory_.GetWeakPtr())); |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 258 | terminal_id_ = base::GenerateGUID(); |
[email protected] | 6c3bf03 | 2013-12-25 19:37:03 | [diff] [blame] | 259 | base::win::ScopedBstr terminal_id(base::UTF8ToUTF16(terminal_id_).c_str()); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 260 | result = rdp_desktop_session_->Connect(host_size.width(), |
| 261 | host_size.height(), |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 262 | terminal_id, |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 263 | event_handler); |
| 264 | if (FAILED(result)) { |
| 265 | LOG(ERROR) << "RdpSession::Create() failed, 0x" |
| 266 | << std::hex << result << std::dec << "."; |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 273 | void RdpSession::OnRdpConnected() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 274 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 275 | |
| 276 | StopMonitoring(); |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 277 | StartMonitoring(terminal_id_); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void RdpSession::OnRdpClosed() { |
| 281 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 282 | |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 283 | TerminateSession(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 284 | } |
| 285 | |
[email protected] | 6f526ce | 2013-03-18 04:38:56 | [diff] [blame] | 286 | void RdpSession::SetScreenResolution(const ScreenResolution& resolution) { |
| 287 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 288 | |
| 289 | // TODO(alexeypa): implement resize-to-client for RDP sessions here. |
| 290 | // See http://crbug.com/137696. |
| 291 | NOTIMPLEMENTED(); |
| 292 | } |
| 293 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 294 | void RdpSession::InjectSas() { |
| 295 | DCHECK(caller_task_runner()->BelongsToCurrentThread()); |
| 296 | |
[email protected] | 97328ee | 2013-06-04 06:46:59 | [diff] [blame] | 297 | rdp_desktop_session_->InjectSas(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | RdpSession::EventHandler::EventHandler( |
| 301 | base::WeakPtr<RdpSession> desktop_session) |
| 302 | : ref_count_(0), |
| 303 | desktop_session_(desktop_session) { |
| 304 | } |
| 305 | |
| 306 | RdpSession::EventHandler::~EventHandler() { |
| 307 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 308 | |
[email protected] | 1bf0623 | 2013-06-07 13:50:24 | [diff] [blame] | 309 | if (desktop_session_) |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 310 | desktop_session_->OnRdpClosed(); |
| 311 | } |
| 312 | |
| 313 | ULONG STDMETHODCALLTYPE RdpSession::EventHandler::AddRef() { |
| 314 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 315 | |
| 316 | return ++ref_count_; |
| 317 | } |
| 318 | |
| 319 | ULONG STDMETHODCALLTYPE RdpSession::EventHandler::Release() { |
| 320 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 321 | |
| 322 | if (--ref_count_ == 0) { |
| 323 | delete this; |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | return ref_count_; |
| 328 | } |
| 329 | |
| 330 | STDMETHODIMP RdpSession::EventHandler::QueryInterface(REFIID riid, void** ppv) { |
| 331 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 332 | |
| 333 | if (riid == IID_IUnknown || |
| 334 | riid == IID_IRdpDesktopSessionEventHandler) { |
| 335 | *ppv = static_cast<IRdpDesktopSessionEventHandler*>(this); |
| 336 | AddRef(); |
| 337 | return S_OK; |
| 338 | } |
| 339 | |
| 340 | *ppv = NULL; |
| 341 | return E_NOINTERFACE; |
| 342 | } |
| 343 | |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 344 | STDMETHODIMP RdpSession::EventHandler::OnRdpConnected() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 345 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 346 | |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 347 | if (desktop_session_) |
| 348 | desktop_session_->OnRdpConnected(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 349 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 350 | return S_OK; |
| 351 | } |
| 352 | |
| 353 | STDMETHODIMP RdpSession::EventHandler::OnRdpClosed() { |
| 354 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 355 | |
[email protected] | 1bf0623 | 2013-06-07 13:50:24 | [diff] [blame] | 356 | if (!desktop_session_) |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 357 | return S_OK; |
| 358 | |
| 359 | base::WeakPtr<RdpSession> desktop_session = desktop_session_; |
| 360 | desktop_session_.reset(); |
| 361 | desktop_session->OnRdpClosed(); |
| 362 | return S_OK; |
| 363 | } |
| 364 | |
| 365 | } // namespace |
| 366 | |
| 367 | // static |
| 368 | scoped_ptr<DesktopSession> DesktopSessionWin::CreateForConsole( |
| 369 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 370 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 371 | DaemonProcess* daemon_process, |
| 372 | int id, |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 373 | const ScreenResolution& resolution) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 374 | scoped_ptr<ConsoleSession> session(new ConsoleSession( |
| 375 | caller_task_runner, io_task_runner, daemon_process, id, |
| 376 | HostService::GetInstance())); |
| 377 | |
| 378 | return session.PassAs<DesktopSession>(); |
| 379 | } |
| 380 | |
| 381 | // static |
| 382 | scoped_ptr<DesktopSession> DesktopSessionWin::CreateForVirtualTerminal( |
| 383 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 384 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 385 | DaemonProcess* daemon_process, |
| 386 | int id, |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 387 | const ScreenResolution& resolution) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 388 | scoped_ptr<RdpSession> session(new RdpSession( |
| 389 | caller_task_runner, io_task_runner, daemon_process, id, |
| 390 | HostService::GetInstance())); |
[email protected] | 739e280 | 2013-03-18 01:03:48 | [diff] [blame] | 391 | if (!session->Initialize(resolution)) |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 392 | return scoped_ptr<DesktopSession>(); |
| 393 | |
| 394 | return session.PassAs<DesktopSession>(); |
| 395 | } |
| 396 | |
| 397 | DesktopSessionWin::DesktopSessionWin( |
| 398 | scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 399 | scoped_refptr<AutoThreadTaskRunner> io_task_runner, |
| 400 | DaemonProcess* daemon_process, |
| 401 | int id, |
[email protected] | 3fd005e2 | 2013-02-28 20:11:29 | [diff] [blame] | 402 | WtsTerminalMonitor* monitor) |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 403 | : DesktopSession(daemon_process, id), |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 404 | caller_task_runner_(caller_task_runner), |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 405 | io_task_runner_(io_task_runner), |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 406 | monitor_(monitor), |
| 407 | monitoring_notifications_(false) { |
| 408 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 409 | |
| 410 | ReportElapsedTime("created"); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | DesktopSessionWin::~DesktopSessionWin() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 414 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 415 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 416 | StopMonitoring(); |
| 417 | } |
| 418 | |
| 419 | void DesktopSessionWin::OnSessionAttachTimeout() { |
| 420 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 421 | |
| 422 | LOG(ERROR) << "Session attach notification didn't arrived within " |
| 423 | << kSessionAttachTimeoutSeconds << " seconds."; |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 424 | TerminateSession(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 425 | } |
| 426 | |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 427 | void DesktopSessionWin::StartMonitoring(const std::string& terminal_id) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 428 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 429 | DCHECK(!monitoring_notifications_); |
| 430 | DCHECK(!session_attach_timer_.IsRunning()); |
| 431 | |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 432 | ReportElapsedTime("started monitoring"); |
| 433 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 434 | session_attach_timer_.Start( |
| 435 | FROM_HERE, base::TimeDelta::FromSeconds(kSessionAttachTimeoutSeconds), |
| 436 | this, &DesktopSessionWin::OnSessionAttachTimeout); |
| 437 | |
| 438 | monitoring_notifications_ = true; |
[email protected] | a4eca0a | 2013-06-20 22:15:05 | [diff] [blame] | 439 | monitor_->AddWtsTerminalObserver(terminal_id, this); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | void DesktopSessionWin::StopMonitoring() { |
| 443 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 444 | |
| 445 | if (monitoring_notifications_) { |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 446 | ReportElapsedTime("stopped monitoring"); |
| 447 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 448 | monitoring_notifications_ = false; |
| 449 | monitor_->RemoveWtsTerminalObserver(this); |
| 450 | } |
| 451 | |
| 452 | session_attach_timer_.Stop(); |
| 453 | OnSessionDetached(); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 454 | } |
| 455 | |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 456 | void DesktopSessionWin::TerminateSession() { |
| 457 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 458 | |
| 459 | StopMonitoring(); |
| 460 | |
| 461 | // This call will delete |this| so it should be at the very end of the method. |
| 462 | daemon_process()->CloseDesktopSession(id()); |
| 463 | } |
| 464 | |
[email protected] | b15c870 | 2012-10-18 20:34:21 | [diff] [blame] | 465 | void DesktopSessionWin::OnChannelConnected(int32 peer_pid) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 466 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 467 | |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 468 | ReportElapsedTime("channel connected"); |
| 469 | |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 470 | // Obtain the handle of the desktop process. It will be passed to the network |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 471 | // process to use to duplicate handles of shared memory objects from |
| 472 | // the desktop process. |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 473 | desktop_process_.Set(OpenProcess(PROCESS_DUP_HANDLE, false, peer_pid)); |
| 474 | if (!desktop_process_.IsValid()) { |
[email protected] | e9057a6f | 2013-03-08 22:37:55 | [diff] [blame] | 475 | CrashDesktopProcess(FROM_HERE); |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 476 | return; |
| 477 | } |
| 478 | |
| 479 | VLOG(1) << "IPC: daemon <- desktop (" << peer_pid << ")"; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | bool DesktopSessionWin::OnMessageReceived(const IPC::Message& message) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 483 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 484 | |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 485 | bool handled = true; |
| 486 | IPC_BEGIN_MESSAGE_MAP(DesktopSessionWin, message) |
| 487 | IPC_MESSAGE_HANDLER(ChromotingDesktopDaemonMsg_DesktopAttached, |
| 488 | OnDesktopSessionAgentAttached) |
[email protected] | 0dffd55 | 2012-12-07 01:08:09 | [diff] [blame] | 489 | IPC_MESSAGE_HANDLER(ChromotingDesktopDaemonMsg_InjectSas, |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 490 | InjectSas) |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 491 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 492 | IPC_END_MESSAGE_MAP() |
[email protected] | f5f0f40 | 2013-03-05 22:46:38 | [diff] [blame] | 493 | |
| 494 | if (!handled) { |
| 495 | LOG(ERROR) << "Received unexpected IPC type: " << message.type(); |
[email protected] | e9057a6f | 2013-03-08 22:37:55 | [diff] [blame] | 496 | CrashDesktopProcess(FROM_HERE); |
[email protected] | f5f0f40 | 2013-03-05 22:46:38 | [diff] [blame] | 497 | } |
| 498 | |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 499 | return handled; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 500 | } |
| 501 | |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 502 | void DesktopSessionWin::OnPermanentError(int exit_code) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 503 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 504 | |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 505 | TerminateSession(); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | void DesktopSessionWin::OnSessionAttached(uint32 session_id) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 509 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 510 | DCHECK(!launcher_); |
| 511 | DCHECK(monitoring_notifications_); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 512 | |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 513 | ReportElapsedTime("attached"); |
| 514 | |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 515 | // Launch elevated on Win8 to be able to inject Alt+Tab. |
| 516 | bool launch_elevated = base::win::GetVersion() >= base::win::VERSION_WIN8; |
| 517 | |
| 518 | // Get the name of the executable to run. |kDesktopBinaryName| specifies |
| 519 | // uiAccess="true" in it's manifest. |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 520 | base::FilePath desktop_binary; |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 521 | bool result; |
| 522 | if (launch_elevated) { |
| 523 | result = GetInstalledBinaryPath(kDesktopBinaryName, &desktop_binary); |
| 524 | } else { |
| 525 | result = GetInstalledBinaryPath(kHostBinaryName, &desktop_binary); |
| 526 | } |
| 527 | |
| 528 | if (!result) { |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 529 | TerminateSession(); |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 530 | return; |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 531 | } |
| 532 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 533 | session_attach_timer_.Stop(); |
| 534 | |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 535 | scoped_ptr<CommandLine> target(new CommandLine(desktop_binary)); |
| 536 | target->AppendSwitchASCII(kProcessTypeSwitchName, kProcessTypeDesktop); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 537 | // Copy the command line switches enabling verbose logging. |
[email protected] | 9410c0a | 2013-02-23 06:07:21 | [diff] [blame] | 538 | target->CopySwitchesFrom(*CommandLine::ForCurrentProcess(), |
| 539 | kCopiedSwitchNames, |
| 540 | arraysize(kCopiedSwitchNames)); |
| 541 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 542 | // Create a delegate capable of launching a process in a different session. |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 543 | scoped_ptr<WtsSessionProcessDelegate> delegate( |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 544 | new WtsSessionProcessDelegate(io_task_runner_, |
| 545 | target.Pass(), |
| 546 | launch_elevated, |
[email protected] | 6c3bf03 | 2013-12-25 19:37:03 | [diff] [blame] | 547 | base::WideToUTF8( |
| 548 | kDaemonIpcSecurityDescriptor))); |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 549 | if (!delegate->Initialize(session_id)) { |
[email protected] | a4b3995 | 2013-06-21 03:57:08 | [diff] [blame] | 550 | TerminateSession(); |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 551 | return; |
| 552 | } |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 553 | |
| 554 | // Create a launcher for the desktop process, using the per-session delegate. |
[email protected] | 85c7fda2 | 2013-05-18 01:35:47 | [diff] [blame] | 555 | launcher_.reset(new WorkerProcessLauncher(delegate.Pass(), this)); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | void DesktopSessionWin::OnSessionDetached() { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 559 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 560 | |
| 561 | launcher_.reset(); |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 562 | |
| 563 | if (monitoring_notifications_) { |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 564 | ReportElapsedTime("detached"); |
| 565 | |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 566 | session_attach_timer_.Start( |
| 567 | FROM_HERE, base::TimeDelta::FromSeconds(kSessionAttachTimeoutSeconds), |
| 568 | this, &DesktopSessionWin::OnSessionAttachTimeout); |
| 569 | } |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 570 | } |
| 571 | |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 572 | void DesktopSessionWin::OnDesktopSessionAgentAttached( |
| 573 | IPC::PlatformFileForTransit desktop_pipe) { |
| 574 | if (!daemon_process()->OnDesktopSessionAgentAttached(id(), |
| 575 | desktop_process_, |
| 576 | desktop_pipe)) { |
[email protected] | e9057a6f | 2013-03-08 22:37:55 | [diff] [blame] | 577 | CrashDesktopProcess(FROM_HERE); |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
[email protected] | e9057a6f | 2013-03-08 22:37:55 | [diff] [blame] | 581 | void DesktopSessionWin::CrashDesktopProcess( |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 582 | const tracked_objects::Location& location) { |
[email protected] | 5b731f3e | 2013-03-15 07:28:22 | [diff] [blame] | 583 | DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 584 | |
[email protected] | e9057a6f | 2013-03-08 22:37:55 | [diff] [blame] | 585 | launcher_->Crash(location); |
[email protected] | 12066cb | 2012-10-25 03:25:43 | [diff] [blame] | 586 | } |
| 587 | |
[email protected] | 94618e7 | 2013-05-14 00:13:08 | [diff] [blame] | 588 | void DesktopSessionWin::ReportElapsedTime(const std::string& event) { |
| 589 | base::Time now = base::Time::Now(); |
| 590 | |
| 591 | std::string passed; |
| 592 | if (!last_timestamp_.is_null()) { |
| 593 | passed = base::StringPrintf(", %.2fs passed", |
| 594 | (now - last_timestamp_).InSecondsF()); |
| 595 | } |
| 596 | |
| 597 | base::Time::Exploded exploded; |
| 598 | now.LocalExplode(&exploded); |
| 599 | VLOG(1) << base::StringPrintf("session(%d): %s at %02d:%02d:%02d.%03d%s", |
| 600 | id(), |
| 601 | event.c_str(), |
| 602 | exploded.hour, |
| 603 | exploded.minute, |
| 604 | exploded.second, |
| 605 | exploded.millisecond, |
| 606 | passed.c_str()); |
| 607 | |
| 608 | last_timestamp_ = now; |
| 609 | } |
| 610 | |
[email protected] | 60ccc24 | 2012-10-17 21:06:24 | [diff] [blame] | 611 | } // namespace remoting |