Home | History | Annotate | Download | only in win
      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 class CommandLine;
     19 
     20 namespace base {
     21 class SingleThreadTaskRunner;
     22 }  // namespace base
     23 
     24 namespace remoting {
     25 
     26 class AutoThreadTaskRunner;
     27 class DaemonProcess;
     28 class WtsTerminalObserver;
     29 
     30 class HostService : public WtsTerminalMonitor {
     31  public:
     32   static HostService* GetInstance();
     33 
     34   // This function parses the command line and selects the action routine.
     35   bool InitWithCommandLine(const CommandLine* command_line);
     36 
     37   // Invoke the choosen action routine.
     38   int Run();
     39 
     40   // WtsTerminalMonitor implementation
     41   virtual bool AddWtsTerminalObserver(const std::string& terminal_id,
     42                                       WtsTerminalObserver* observer) OVERRIDE;
     43   virtual void RemoveWtsTerminalObserver(
     44       WtsTerminalObserver* observer) OVERRIDE;
     45 
     46  private:
     47   HostService();
     48   ~HostService();
     49 
     50   // Notifies the service of changes in session state.
     51   void OnSessionChange(uint32 event, uint32 session_id);
     52 
     53   // Creates the process launcher.
     54   void CreateLauncher(scoped_refptr<AutoThreadTaskRunner> task_runner);
     55 
     56   // This function handshakes with the service control manager and starts
     57   // the service.
     58   int RunAsService();
     59 
     60   // Runs the service on the service thread. A separate routine is used to make
     61   // sure all local objects are destoyed by the time |stopped_event_| is
     62   // signalled.
     63   void RunAsServiceImpl();
     64 
     65   // This function starts the service in interactive mode (i.e. as a plain
     66   // console application).
     67   int RunInConsole();
     68 
     69   // Stops and deletes |daemon_process_|.
     70   void StopDaemonProcess();
     71 
     72   // Handles WM_WTSSESSION_CHANGE messages.
     73   bool HandleMessage(UINT message,
     74                      WPARAM wparam,
     75                      LPARAM lparam,
     76                      LRESULT* result);
     77 
     78   static BOOL WINAPI ConsoleControlHandler(DWORD event);
     79 
     80   // The control handler of the service.
     81   static DWORD WINAPI ServiceControlHandler(DWORD control,
     82                                             DWORD event_type,
     83                                             LPVOID event_data,
     84                                             LPVOID context);
     85 
     86   // The main service entry point.
     87   static VOID WINAPI ServiceMain(DWORD argc, WCHAR* argv[]);
     88 
     89   struct RegisteredObserver {
     90     // Unique identifier of the terminal to observe.
     91     std::string terminal_id;
     92 
     93     // Specifies ID of the attached session or |kInvalidSession| if no session
     94     // is attached to the WTS terminal.
     95     uint32 session_id;
     96 
     97     // Points to the observer receiving notifications about the WTS terminal
     98     // identified by |terminal_id|.
     99     WtsTerminalObserver* observer;
    100   };
    101 
    102   // The list of observers receiving session notifications.
    103   std::list<RegisteredObserver> observers_;
    104 
    105   scoped_ptr<DaemonProcess> daemon_process_;
    106 
    107   // Service message loop. |main_task_runner_| must be valid as long as the
    108   // Control+C or service notification handler is registered.
    109   scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
    110 
    111   // The action routine to be executed.
    112   int (HostService::*run_routine_)();
    113 
    114   // The service status handle.
    115   SERVICE_STATUS_HANDLE service_status_handle_;
    116 
    117   // A waitable event that is used to wait until the service is stopped.
    118   base::WaitableEvent stopped_event_;
    119 
    120   // Used to post session change notifications and control events.
    121   base::WeakPtrFactory<HostService> weak_factory_;
    122   base::WeakPtr<HostService> weak_ptr_;
    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