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. 4 5 #ifndef REMOTING_HOST_WIN_HOST_SERVICE_H_ 6 #define REMOTING_HOST_WIN_HOST_SERVICE_H_ 7 8 #include <windows.h> 9 10 #include <list> 11 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" 17 18 namespace base { 19 class CommandLine; 20 class SingleThreadTaskRunner; 21 } // namespace base 22 23 namespace remoting { 24 25 class AutoThreadTaskRunner; 26 class DaemonProcess; 27 class WtsTerminalObserver; 28 29 class HostService : public WtsTerminalMonitor { 30 public: 31 static HostService* GetInstance(); 32 33 // This function parses the command line and selects the action routine. 34 bool InitWithCommandLine(const base::CommandLine* command_line); 35 36 // Invoke the choosen action routine. 37 int Run(); 38 39 // WtsTerminalMonitor implementation 40 virtual bool AddWtsTerminalObserver(const std::string& terminal_id, 41 WtsTerminalObserver* observer) OVERRIDE; 42 virtual void RemoveWtsTerminalObserver( 43 WtsTerminalObserver* observer) OVERRIDE; 44 45 private: 46 HostService(); 47 ~HostService(); 48 49 // Notifies the service of changes in session state. 50 void OnSessionChange(uint32 event, uint32 session_id); 51 52 // Creates the process launcher. 53 void CreateLauncher(scoped_refptr<AutoThreadTaskRunner> task_runner); 54 55 // This function handshakes with the service control manager and starts 56 // the service. 57 int RunAsService(); 58 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(); 63 64 // This function starts the service in interactive mode (i.e. as a plain 65 // console application). 66 int RunInConsole(); 67 68 // Stops and deletes |daemon_process_|. 69 void StopDaemonProcess(); 70 71 // Handles WM_WTSSESSION_CHANGE messages. 72 bool HandleMessage(UINT message, 73 WPARAM wparam, 74 LPARAM lparam, 75 LRESULT* result); 76 77 static BOOL WINAPI ConsoleControlHandler(DWORD event); 78 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); 84 85 // The main service entry point. 86 static VOID WINAPI ServiceMain(DWORD argc, WCHAR* argv[]); 87 88 struct RegisteredObserver { 89 // Unique identifier of the terminal to observe. 90 std::string terminal_id; 91 92 // Specifies ID of the attached session or |kInvalidSession| if no session 93 // is attached to the WTS terminal. 94 uint32 session_id; 95 96 // Points to the observer receiving notifications about the WTS terminal 97 // identified by |terminal_id|. 98 WtsTerminalObserver* observer; 99 }; 100 101 // The list of observers receiving session notifications. 102 std::list<RegisteredObserver> observers_; 103 104 scoped_ptr<DaemonProcess> daemon_process_; 105 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_; 109 110 // The action routine to be executed. 111 int (HostService::*run_routine_)(); 112 113 // The service status handle. 114 SERVICE_STATUS_HANDLE service_status_handle_; 115 116 // A waitable event that is used to wait until the service is stopped. 117 base::WaitableEvent stopped_event_; 118 119 base::WeakPtr<HostService> weak_ptr_; 120 121 // Used to post session change notifications and control events. 122 base::WeakPtrFactory<HostService> weak_factory_; 123 124 // Singleton. 125 friend struct DefaultSingletonTraits<HostService>; 126 127 DISALLOW_COPY_AND_ASSIGN(HostService); 128 }; 129 130 } // namespace remoting 131 132 #endif // REMOTING_HOST_WIN_HOST_SERVICE_H_ 133