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_
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/singleton.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "remoting/host/win/wts_terminal_monitor.h"
20 class SingleThreadTaskRunner
;
25 class AutoThreadTaskRunner
;
27 class WtsTerminalObserver
;
29 class HostService
: public WtsTerminalMonitor
{
31 static HostService
* GetInstance();
33 // This function parses the command line and selects the action routine.
34 bool InitWithCommandLine(const base::CommandLine
* command_line
);
36 // Invoke the choosen action routine.
39 // WtsTerminalMonitor implementation
40 virtual bool AddWtsTerminalObserver(const std::string
& terminal_id
,
41 WtsTerminalObserver
* observer
) override
;
42 virtual void RemoveWtsTerminalObserver(
43 WtsTerminalObserver
* observer
) override
;
49 // Notifies the service of changes in session state.
50 void OnSessionChange(uint32 event
, uint32 session_id
);
52 // Creates the process launcher.
53 void CreateLauncher(scoped_refptr
<AutoThreadTaskRunner
> task_runner
);
55 // This function handshakes with the service control manager and starts
59 // Runs the service on the service thread. A separate routine is used to make
60 // sure all local objects are destoyed by the time |stopped_event_| is
62 void RunAsServiceImpl();
64 // This function starts the service in interactive mode (i.e. as a plain
65 // console application).
68 // Stops and deletes |daemon_process_|.
69 void StopDaemonProcess();
71 // Handles WM_WTSSESSION_CHANGE messages.
72 bool HandleMessage(UINT message
,
77 static BOOL WINAPI
ConsoleControlHandler(DWORD event
);
79 // The control handler of the service.
80 static DWORD WINAPI
ServiceControlHandler(DWORD control
,
85 // The main service entry point.
86 static VOID WINAPI
ServiceMain(DWORD argc
, WCHAR
* argv
[]);
88 struct RegisteredObserver
{
89 // Unique identifier of the terminal to observe.
90 std::string terminal_id
;
92 // Specifies ID of the attached session or |kInvalidSession| if no session
93 // is attached to the WTS terminal.
96 // Points to the observer receiving notifications about the WTS terminal
97 // identified by |terminal_id|.
98 WtsTerminalObserver
* observer
;
101 // The list of observers receiving session notifications.
102 std::list
<RegisteredObserver
> observers_
;
104 scoped_ptr
<DaemonProcess
> daemon_process_
;
106 // Service message loop. |main_task_runner_| must be valid as long as the
107 // Control+C or service notification handler is registered.
108 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
110 // The action routine to be executed.
111 int (HostService::*run_routine_
)();
113 // The service status handle.
114 SERVICE_STATUS_HANDLE service_status_handle_
;
116 // A waitable event that is used to wait until the service is stopped.
117 base::WaitableEvent stopped_event_
;
119 base::WeakPtr
<HostService
> weak_ptr_
;
121 // Used to post session change notifications and control events.
122 base::WeakPtrFactory
<HostService
> weak_factory_
;
125 friend struct DefaultSingletonTraits
<HostService
>;
127 DISALLOW_COPY_AND_ASSIGN(HostService
);
130 } // namespace remoting
132 #endif // REMOTING_HOST_WIN_HOST_SERVICE_H_