| 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 // This file implements the Windows service controlling Me2Me host processes | 5 // This file implements the Windows service controlling Me2Me host processes |
| 6 // running within user sessions. | 6 // running within user sessions. |
| 7 | 7 |
| 8 #include "remoting/host/win/host_service.h" | 8 #include "remoting/host/win/host_service.h" |
| 9 | 9 |
| 10 #include <windows.h> | 10 #include <windows.h> |
| 11 #include <wtsapi32.h> | 11 #include <wtsapi32.h> |
| 12 #include <stdio.h> | 12 #include <stdio.h> |
| 13 | 13 |
| 14 #include "base/at_exit.h" | 14 #include "base/at_exit.h" |
| 15 #include "base/base_paths.h" | 15 #include "base/base_paths.h" |
| 16 #include "base/bind.h" | 16 #include "base/bind.h" |
| 17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 18 #include "base/file_path.h" | 18 #include "base/file_path.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/message_loop.h" | 20 #include "base/message_loop.h" |
| 21 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
| 22 #include "base/stringprintf.h" | 22 #include "base/stringprintf.h" |
| 23 #include "base/threading/thread.h" | 23 #include "base/threading/thread.h" |
| 24 #include "base/utf_string_conversions.h" | 24 #include "base/utf_string_conversions.h" |
| 25 #include "base/win/wrapped_window_proc.h" | 25 #include "base/win/wrapped_window_proc.h" |
| 26 #include "remoting/base/breakpad.h" | 26 #include "remoting/base/breakpad.h" |
| 27 #include "remoting/base/scoped_sc_handle_win.h" | 27 #include "remoting/base/scoped_sc_handle_win.h" |
| 28 #include "remoting/base/stoppable.h" | 28 #include "remoting/base/stoppable.h" |
| 29 #include "remoting/host/branding.h" | 29 #include "remoting/host/branding.h" |
| 30 |
| 31 #if defined(REMOTING_MULTI_PROCESS) |
| 32 #include "remoting/host/daemon_process.h" |
| 33 #endif // defined(REMOTING_MULTI_PROCESS) |
| 34 |
| 30 #include "remoting/host/usage_stats_consent.h" | 35 #include "remoting/host/usage_stats_consent.h" |
| 31 #include "remoting/host/win/host_service_resource.h" | 36 #include "remoting/host/win/host_service_resource.h" |
| 32 #include "remoting/host/win/wts_console_observer.h" | 37 #include "remoting/host/win/wts_console_observer.h" |
| 38 |
| 39 #if !defined(REMOTING_MULTI_PROCESS) |
| 33 #include "remoting/host/win/wts_session_process_launcher.h" | 40 #include "remoting/host/win/wts_session_process_launcher.h" |
| 41 #endif // !defined(REMOTING_MULTI_PROCESS) |
| 34 | 42 |
| 35 using base::StringPrintf; | 43 using base::StringPrintf; |
| 36 | 44 |
| 37 namespace { | 45 namespace { |
| 38 | 46 |
| 47 // Session id that does not represent any session. |
| 48 const uint32 kInvalidSessionId = 0xffffffffu; |
| 49 |
| 39 const char kIoThreadName[] = "I/O thread"; | 50 const char kIoThreadName[] = "I/O thread"; |
| 40 | 51 |
| 41 // A window class for the session change notifications window. | 52 // A window class for the session change notifications window. |
| 42 const wchar_t kSessionNotificationWindowClass[] = | 53 const wchar_t kSessionNotificationWindowClass[] = |
| 43 L"Chromoting_SessionNotificationWindow"; | 54 L"Chromoting_SessionNotificationWindow"; |
| 44 | 55 |
| 45 // Command line actions and switches: | 56 // Command line actions and switches: |
| 46 // "run" sumply runs the service as usual. | 57 // "run" sumply runs the service as usual. |
| 47 const wchar_t kRunActionName[] = L"run"; | 58 const wchar_t kRunActionName[] = L"run"; |
| 48 | 59 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 104 |
| 94 console_observers_.AddObserver(observer); | 105 console_observers_.AddObserver(observer); |
| 95 } | 106 } |
| 96 | 107 |
| 97 void HostService::RemoveWtsConsoleObserver(WtsConsoleObserver* observer) { | 108 void HostService::RemoveWtsConsoleObserver(WtsConsoleObserver* observer) { |
| 98 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 109 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 99 | 110 |
| 100 console_observers_.RemoveObserver(observer); | 111 console_observers_.RemoveObserver(observer); |
| 101 } | 112 } |
| 102 | 113 |
| 103 void HostService::OnLauncherShutdown() { | 114 void HostService::OnChildStopped() { |
| 104 launcher_.reset(NULL); | 115 child_.reset(NULL); |
| 105 main_task_runner_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 116 main_task_runner_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 106 } | 117 } |
| 107 | 118 |
| 108 void HostService::OnSessionChange() { | 119 void HostService::OnSessionChange() { |
| 109 // WTSGetActiveConsoleSessionId is a very cheap API. It basically reads | 120 // WTSGetActiveConsoleSessionId is a very cheap API. It basically reads |
| 110 // a single value from shared memory. Therefore it is better to check if | 121 // a single value from shared memory. Therefore it is better to check if |
| 111 // the console session is still the same every time a session change | 122 // the console session is still the same every time a session change |
| 112 // notification event is posted. This also takes care of coalescing multiple | 123 // notification event is posted. This also takes care of coalescing multiple |
| 113 // events into one since we look at the latest state. | 124 // events into one since we look at the latest state. |
| 114 uint32 console_session_id = WTSGetActiveConsoleSessionId(); | 125 uint32 console_session_id = WTSGetActiveConsoleSessionId(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 131 | 142 |
| 132 BOOL WINAPI HostService::ConsoleControlHandler(DWORD event) { | 143 BOOL WINAPI HostService::ConsoleControlHandler(DWORD event) { |
| 133 HostService* self = HostService::GetInstance(); | 144 HostService* self = HostService::GetInstance(); |
| 134 switch (event) { | 145 switch (event) { |
| 135 case CTRL_C_EVENT: | 146 case CTRL_C_EVENT: |
| 136 case CTRL_BREAK_EVENT: | 147 case CTRL_BREAK_EVENT: |
| 137 case CTRL_CLOSE_EVENT: | 148 case CTRL_CLOSE_EVENT: |
| 138 case CTRL_LOGOFF_EVENT: | 149 case CTRL_LOGOFF_EVENT: |
| 139 case CTRL_SHUTDOWN_EVENT: | 150 case CTRL_SHUTDOWN_EVENT: |
| 140 self->main_task_runner_->PostTask(FROM_HERE, base::Bind( | 151 self->main_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 141 &WtsSessionProcessLauncher::Stop, | 152 &Stoppable::Stop, base::Unretained(self->child_.get()))); |
| 142 base::Unretained(self->launcher_.get()))); | |
| 143 self->stopped_event_.Wait(); | 153 self->stopped_event_.Wait(); |
| 144 return TRUE; | 154 return TRUE; |
| 145 | 155 |
| 146 default: | 156 default: |
| 147 return FALSE; | 157 return FALSE; |
| 148 } | 158 } |
| 149 } | 159 } |
| 150 | 160 |
| 151 HostService* HostService::GetInstance() { | 161 HostService* HostService::GetInstance() { |
| 152 return Singleton<HostService>::get(); | 162 return Singleton<HostService>::get(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 175 } | 185 } |
| 176 | 186 |
| 177 return true; | 187 return true; |
| 178 } | 188 } |
| 179 | 189 |
| 180 int HostService::Run() { | 190 int HostService::Run() { |
| 181 return (this->*run_routine_)(); | 191 return (this->*run_routine_)(); |
| 182 } | 192 } |
| 183 | 193 |
| 184 void HostService::RunMessageLoop(MessageLoop* message_loop) { | 194 void HostService::RunMessageLoop(MessageLoop* message_loop) { |
| 195 #if defined(REMOTING_MULTI_PROCESS) |
| 196 |
| 197 child_ = DaemonProcess::Create( |
| 198 main_task_runner_, |
| 199 base::Bind(&HostService::OnChildStopped, |
| 200 base::Unretained(this))).PassAs<Stoppable>(); |
| 201 |
| 202 #else // !defined(REMOTING_MULTI_PROCESS) |
| 203 |
| 185 // Launch the I/O thread. | 204 // Launch the I/O thread. |
| 186 base::Thread io_thread(kIoThreadName); | 205 base::Thread io_thread(kIoThreadName); |
| 187 base::Thread::Options io_thread_options(MessageLoop::TYPE_IO, 0); | 206 base::Thread::Options io_thread_options(MessageLoop::TYPE_IO, 0); |
| 188 if (!io_thread.StartWithOptions(io_thread_options)) { | 207 if (!io_thread.StartWithOptions(io_thread_options)) { |
| 189 LOG(FATAL) << "Failed to start the I/O thread"; | 208 LOG(ERROR) << "Failed to start the I/O thread"; |
| 190 stopped_event_.Signal(); | 209 stopped_event_.Signal(); |
| 191 return; | 210 return; |
| 192 } | 211 } |
| 193 | 212 |
| 194 // Create the session process launcher. | 213 // Create the session process launcher. |
| 195 launcher_.reset(new WtsSessionProcessLauncher( | 214 child_.reset(new WtsSessionProcessLauncher( |
| 196 base::Bind(&HostService::OnLauncherShutdown, base::Unretained(this)), | 215 base::Bind(&HostService::OnChildStopped, base::Unretained(this)), |
| 197 this, | 216 this, |
| 198 main_task_runner_, | 217 main_task_runner_, |
| 199 io_thread.message_loop_proxy())); | 218 io_thread.message_loop_proxy())); |
| 200 | 219 |
| 220 #endif // !defined(REMOTING_MULTI_PROCESS) |
| 221 |
| 201 // Run the service. | 222 // Run the service. |
| 202 message_loop->Run(); | 223 message_loop->Run(); |
| 203 | 224 |
| 204 // Release the control handler. | 225 // Release the control handler. |
| 205 stopped_event_.Signal(); | 226 stopped_event_.Signal(); |
| 206 } | 227 } |
| 207 | 228 |
| 208 int HostService::RunAsService() { | 229 int HostService::RunAsService() { |
| 209 SERVICE_TABLE_ENTRYW dispatch_table[] = { | 230 SERVICE_TABLE_ENTRYW dispatch_table[] = { |
| 210 { const_cast<LPWSTR>(service_name_.c_str()), &HostService::ServiceMain }, | 231 { const_cast<LPWSTR>(service_name_.c_str()), &HostService::ServiceMain }, |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 LPVOID event_data, | 318 LPVOID event_data, |
| 298 LPVOID context) { | 319 LPVOID context) { |
| 299 HostService* self = reinterpret_cast<HostService*>(context); | 320 HostService* self = reinterpret_cast<HostService*>(context); |
| 300 switch (control) { | 321 switch (control) { |
| 301 case SERVICE_CONTROL_INTERROGATE: | 322 case SERVICE_CONTROL_INTERROGATE: |
| 302 return NO_ERROR; | 323 return NO_ERROR; |
| 303 | 324 |
| 304 case SERVICE_CONTROL_SHUTDOWN: | 325 case SERVICE_CONTROL_SHUTDOWN: |
| 305 case SERVICE_CONTROL_STOP: | 326 case SERVICE_CONTROL_STOP: |
| 306 self->main_task_runner_->PostTask(FROM_HERE, base::Bind( | 327 self->main_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 307 &WtsSessionProcessLauncher::Stop, | 328 &Stoppable::Stop, base::Unretained(self->child_.get()))); |
| 308 base::Unretained(self->launcher_.get()))); | |
| 309 self->stopped_event_.Wait(); | 329 self->stopped_event_.Wait(); |
| 310 return NO_ERROR; | 330 return NO_ERROR; |
| 311 | 331 |
| 312 case SERVICE_CONTROL_SESSIONCHANGE: | 332 case SERVICE_CONTROL_SESSIONCHANGE: |
| 313 self->main_task_runner_->PostTask(FROM_HERE, base::Bind( | 333 self->main_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 314 &HostService::OnSessionChange, base::Unretained(self))); | 334 &HostService::OnSessionChange, base::Unretained(self))); |
| 315 return NO_ERROR; | 335 return NO_ERROR; |
| 316 | 336 |
| 317 default: | 337 default: |
| 318 return ERROR_CALL_NOT_IMPLEMENTED; | 338 return ERROR_CALL_NOT_IMPLEMENTED; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 } | 441 } |
| 422 | 442 |
| 423 remoting::HostService* service = remoting::HostService::GetInstance(); | 443 remoting::HostService* service = remoting::HostService::GetInstance(); |
| 424 if (!service->InitWithCommandLine(command_line)) { | 444 if (!service->InitWithCommandLine(command_line)) { |
| 425 usage(argv[0]); | 445 usage(argv[0]); |
| 426 return kUsageExitCode; | 446 return kUsageExitCode; |
| 427 } | 447 } |
| 428 | 448 |
| 429 return service->Run(); | 449 return service->Run(); |
| 430 } | 450 } |
| OLD | NEW |