Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / host / win / host_service.h
blobe7fbf2614a2646303deb933541e15a09a45ac067
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_
8 #include <windows.h>
10 #include <list>
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"
18 namespace base {
19 class CommandLine;
20 class SingleThreadTaskRunner;
21 } // namespace base
23 namespace remoting {
25 class AutoThreadTaskRunner;
26 class DaemonProcess;
27 class WtsTerminalObserver;
29 class HostService : public WtsTerminalMonitor {
30 public:
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.
37 int Run();
39 // WtsTerminalMonitor implementation
40 bool AddWtsTerminalObserver(const std::string& terminal_id,
41 WtsTerminalObserver* observer) override;
42 void RemoveWtsTerminalObserver(
43 WtsTerminalObserver* observer) override;
45 private:
46 HostService();
47 ~HostService() 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
56 // the service.
57 int RunAsService();
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
61 // signalled.
62 void RunAsServiceImpl();
64 // This function starts the service in interactive mode (i.e. as a plain
65 // console application).
66 int RunInConsole();
68 // Stops and deletes |daemon_process_|.
69 void StopDaemonProcess();
71 // Handles WM_WTSSESSION_CHANGE messages.
72 bool HandleMessage(UINT message,
73 WPARAM wparam,
74 LPARAM lparam,
75 LRESULT* result);
77 static BOOL WINAPI ConsoleControlHandler(DWORD event);
79 // The control handler of the service.
80 static DWORD WINAPI ServiceControlHandler(DWORD control,
81 DWORD event_type,
82 LPVOID event_data,
83 LPVOID context);
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.
94 uint32 session_id;
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_;
124 // Singleton.
125 friend struct base::DefaultSingletonTraits<HostService>;
127 DISALLOW_COPY_AND_ASSIGN(HostService);
130 } // namespace remoting
132 #endif // REMOTING_HOST_WIN_HOST_SERVICE_H_