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/host_service_win.h" | 8 #include "remoting/host/host_service_win.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/shutdownable.h" | 28 #include "remoting/base/shutdownable.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/host_service_resource.h" | 35 #include "remoting/host/host_service_resource.h" |
31 #include "remoting/host/usage_stats_consent.h" | 36 #include "remoting/host/usage_stats_consent.h" |
32 #include "remoting/host/wts_console_observer_win.h" | 37 #include "remoting/host/wts_console_observer_win.h" |
38 | |
39 #if !defined(REMOTING_MULTI_PROCESS) | |
33 #include "remoting/host/wts_session_process_launcher_win.h" | 40 #include "remoting/host/wts_session_process_launcher_win.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(Shutdownable* /* launcher */) { | 114 void HostService::OnShutdownableStopped(Shutdownable* /* launcher */) { |
Sergey Ulanov
2012/07/30 19:50:04
OnChildStopped?
alexeypa (please no reviews)
2012/07/30 21:50:03
Done.
| |
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::Shutdown, | 152 &Shutdownable::Shutdown, 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::OnShutdownableStopped, base::Unretained(this))); | |
200 | |
201 #else | |
Sergey Ulanov
2012/07/30 19:50:04
// !defined(REMOTING_MULTI_PROCESS)
alexeypa (please no reviews)
2012/07/30 21:50:03
Done.
| |
202 | |
185 // Launch the I/O thread. | 203 // Launch the I/O thread. |
186 base::Thread io_thread(kIoThreadName); | 204 base::Thread io_thread(kIoThreadName); |
187 base::Thread::Options io_thread_options(MessageLoop::TYPE_IO, 0); | 205 base::Thread::Options io_thread_options(MessageLoop::TYPE_IO, 0); |
188 if (!io_thread.StartWithOptions(io_thread_options)) { | 206 if (!io_thread.StartWithOptions(io_thread_options)) { |
189 LOG(FATAL) << "Failed to start the I/O thread"; | 207 LOG(ERROR) << "Failed to start the I/O thread"; |
190 stopped_event_.Signal(); | 208 stopped_event_.Signal(); |
191 return; | 209 return; |
192 } | 210 } |
193 | 211 |
194 // Create the session process launcher. | 212 // Create the session process launcher. |
195 launcher_.reset(new WtsSessionProcessLauncher( | 213 child_.reset(new WtsSessionProcessLauncher( |
196 base::Bind(&HostService::OnLauncherShutdown, base::Unretained(this)), | 214 base::Bind(&HostService::OnShutdownableStopped, base::Unretained(this)), |
197 this, | 215 this, |
198 main_task_runner_, | 216 main_task_runner_, |
199 io_thread.message_loop_proxy())); | 217 io_thread.message_loop_proxy())); |
200 | 218 |
219 #endif // defined(REMOTING_MULTI_PROCESS) | |
Sergey Ulanov
2012/07/30 19:50:04
nit: // !defined(REMOTING_MULTI_PROCESS)
alexeypa (please no reviews)
2012/07/30 21:50:03
Done.
| |
220 | |
201 // Run the service. | 221 // Run the service. |
202 message_loop->Run(); | 222 message_loop->Run(); |
203 | 223 |
204 // Release the control handler. | 224 // Release the control handler. |
205 stopped_event_.Signal(); | 225 stopped_event_.Signal(); |
206 } | 226 } |
207 | 227 |
208 int HostService::RunAsService() { | 228 int HostService::RunAsService() { |
209 SERVICE_TABLE_ENTRYW dispatch_table[] = { | 229 SERVICE_TABLE_ENTRYW dispatch_table[] = { |
210 { const_cast<LPWSTR>(service_name_.c_str()), &HostService::ServiceMain }, | 230 { 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, | 317 LPVOID event_data, |
298 LPVOID context) { | 318 LPVOID context) { |
299 HostService* self = reinterpret_cast<HostService*>(context); | 319 HostService* self = reinterpret_cast<HostService*>(context); |
300 switch (control) { | 320 switch (control) { |
301 case SERVICE_CONTROL_INTERROGATE: | 321 case SERVICE_CONTROL_INTERROGATE: |
302 return NO_ERROR; | 322 return NO_ERROR; |
303 | 323 |
304 case SERVICE_CONTROL_SHUTDOWN: | 324 case SERVICE_CONTROL_SHUTDOWN: |
305 case SERVICE_CONTROL_STOP: | 325 case SERVICE_CONTROL_STOP: |
306 self->main_task_runner_->PostTask(FROM_HERE, base::Bind( | 326 self->main_task_runner_->PostTask(FROM_HERE, base::Bind( |
307 &WtsSessionProcessLauncher::Shutdown, | 327 &Shutdownable::Shutdown, base::Unretained(self->child_.get()))); |
308 base::Unretained(self->launcher_.get()))); | |
309 self->stopped_event_.Wait(); | 328 self->stopped_event_.Wait(); |
310 return NO_ERROR; | 329 return NO_ERROR; |
311 | 330 |
312 case SERVICE_CONTROL_SESSIONCHANGE: | 331 case SERVICE_CONTROL_SESSIONCHANGE: |
313 self->main_task_runner_->PostTask(FROM_HERE, base::Bind( | 332 self->main_task_runner_->PostTask(FROM_HERE, base::Bind( |
314 &HostService::OnSessionChange, base::Unretained(self))); | 333 &HostService::OnSessionChange, base::Unretained(self))); |
315 return NO_ERROR; | 334 return NO_ERROR; |
316 | 335 |
317 default: | 336 default: |
318 return ERROR_CALL_NOT_IMPLEMENTED; | 337 return ERROR_CALL_NOT_IMPLEMENTED; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
421 } | 440 } |
422 | 441 |
423 remoting::HostService* service = remoting::HostService::GetInstance(); | 442 remoting::HostService* service = remoting::HostService::GetInstance(); |
424 if (!service->InitWithCommandLine(command_line)) { | 443 if (!service->InitWithCommandLine(command_line)) { |
425 usage(argv[0]); | 444 usage(argv[0]); |
426 return kUsageExitCode; | 445 return kUsageExitCode; |
427 } | 446 } |
428 | 447 |
429 return service->Run(); | 448 return service->Run(); |
430 } | 449 } |
OLD | NEW |