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.
5 #ifndef REMOTING_HOST_WIN_HOST_SERVICE_H_
6 #define REMOTING_HOST_WIN_HOST_SERVICE_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/singleton.h"
12 #include "base/observer_list.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "remoting/host/win/wts_console_monitor.h"
19 class SingleThreadTaskRunner
;
24 class AutoThreadTaskRunner
;
26 class WtsConsoleObserver
;
28 class HostService
: public WtsConsoleMonitor
{
30 static HostService
* GetInstance();
32 // This function parses the command line and selects the action routine.
33 bool InitWithCommandLine(const CommandLine
* command_line
);
35 // Invoke the choosen action routine.
38 // WtsConsoleMonitor implementation
39 virtual void AddWtsConsoleObserver(WtsConsoleObserver
* observer
) OVERRIDE
;
40 virtual void RemoveWtsConsoleObserver(
41 WtsConsoleObserver
* observer
) OVERRIDE
;
47 void OnChildStopped();
49 // Notifies the service of changes in session state.
50 void OnSessionChange();
52 // Creates the process launcher.
53 void CreateLauncher(scoped_refptr
<AutoThreadTaskRunner
> task_runner
);
55 // Runs the binary specified by the command line, elevated.
58 // This function handshakes with the service control manager and starts
62 // Runs the service on the service thread. A separate routine is used to make
63 // sure all local objects are destoyed by the time |stopped_event_| is
65 void RunAsServiceImpl();
67 // This function starts the service in interactive mode (i.e. as a plain
68 // console application).
71 static BOOL WINAPI
ConsoleControlHandler(DWORD event
);
73 // The control handler of the service.
74 static DWORD WINAPI
ServiceControlHandler(DWORD control
,
79 // The main service entry point.
80 static VOID WINAPI
ServiceMain(DWORD argc
, WCHAR
* argv
[]);
82 static LRESULT CALLBACK
SessionChangeNotificationProc(HWND hwnd
,
87 // Current physical console session id.
88 uint32 console_session_id_
;
90 // The list of observers receiving notifications about any session attached
91 // to the physical console.
92 ObserverList
<WtsConsoleObserver
> console_observers_
;
94 scoped_ptr
<Stoppable
> child_
;
96 // Service message loop.
97 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
99 // The action routine to be executed.
100 int (HostService::*run_routine_
)();
102 // The service status handle.
103 SERVICE_STATUS_HANDLE service_status_handle_
;
105 // A waitable event that is used to wait until the service is stopped.
106 base::WaitableEvent stopped_event_
;
109 friend struct DefaultSingletonTraits
<HostService
>;
111 DISALLOW_COPY_AND_ASSIGN(HostService
);
114 } // namespace remoting
116 #endif // REMOTING_HOST_WIN_HOST_SERVICE_H_