Home | History | Annotate | Download | only in host
      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_DESKTOP_SESSION_WIN_H_
      6 #define REMOTING_HOST_DESKTOP_SESSION_WIN_H_
      7 
      8 #include "base/memory/ref_counted.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/time/time.h"
     11 #include "base/timer/timer.h"
     12 #include "base/win/scoped_handle.h"
     13 #include "ipc/ipc_platform_file.h"
     14 #include "remoting/host/desktop_session.h"
     15 #include "remoting/host/win/wts_terminal_observer.h"
     16 #include "remoting/host/worker_process_ipc_delegate.h"
     17 
     18 namespace tracked_objects {
     19 class Location;
     20 }  // namespace tracked_objects
     21 
     22 namespace remoting {
     23 
     24 class AutoThreadTaskRunner;
     25 class DaemonProcess;
     26 class DesktopSession;
     27 class ScreenResolution;
     28 class WorkerProcessLauncher;
     29 class WtsTerminalMonitor;
     30 
     31 // DesktopSession implementation which attaches to either physical or virtual
     32 // (RDP) console. Receives IPC messages from the desktop process, running in
     33 // the target session, via |WorkerProcessIpcDelegate|, and monitors session
     34 // attach/detach events via |WtsTerminalObserer|.
     35 class DesktopSessionWin
     36     : public DesktopSession,
     37       public WorkerProcessIpcDelegate,
     38       public WtsTerminalObserver {
     39  public:
     40   // Creates a desktop session instance that attaches to the physical console.
     41   static scoped_ptr<DesktopSession> CreateForConsole(
     42       scoped_refptr<AutoThreadTaskRunner> caller_task_runner,
     43       scoped_refptr<AutoThreadTaskRunner> io_task_runner,
     44       DaemonProcess* daemon_process,
     45       int id,
     46       const ScreenResolution& resolution);
     47 
     48   // Creates a desktop session instance that attaches to a virtual console.
     49   static scoped_ptr<DesktopSession> CreateForVirtualTerminal(
     50       scoped_refptr<AutoThreadTaskRunner> caller_task_runner,
     51       scoped_refptr<AutoThreadTaskRunner> io_task_runner,
     52       DaemonProcess* daemon_process,
     53       int id,
     54       const ScreenResolution& resolution);
     55 
     56  protected:
     57   // Passes the owning |daemon_process|, a unique identifier of the desktop
     58   // session |id| and the interface for monitoring session attach/detach events.
     59   // Both |daemon_process| and |monitor| must outlive |this|.
     60   DesktopSessionWin(
     61     scoped_refptr<AutoThreadTaskRunner> caller_task_runner,
     62     scoped_refptr<AutoThreadTaskRunner> io_task_runner,
     63     DaemonProcess* daemon_process,
     64     int id,
     65     WtsTerminalMonitor* monitor);
     66   virtual ~DesktopSessionWin();
     67 
     68   const scoped_refptr<AutoThreadTaskRunner>& caller_task_runner() const {
     69     return caller_task_runner_;
     70   }
     71 
     72   // Called when |session_attach_timer_| expires.
     73   void OnSessionAttachTimeout();
     74 
     75   // Starts monitoring for session attach/detach events for |terminal_id|.
     76   void StartMonitoring(const std::string& terminal_id);
     77 
     78   // Stops monitoring for session attach/detach events.
     79   void StopMonitoring();
     80 
     81   // Asks DaemonProcess to terminate this session.
     82   void TerminateSession();
     83 
     84   // Injects a secure attention sequence into the session.
     85   virtual void InjectSas() = 0;
     86 
     87   // WorkerProcessIpcDelegate implementation.
     88   virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
     89   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     90   virtual void OnPermanentError(int exit_code) OVERRIDE;
     91 
     92   // WtsTerminalObserver implementation.
     93   virtual void OnSessionAttached(uint32 session_id) OVERRIDE;
     94   virtual void OnSessionDetached() OVERRIDE;
     95 
     96  private:
     97   // ChromotingDesktopDaemonMsg_DesktopAttached handler.
     98   void OnDesktopSessionAgentAttached(IPC::PlatformFileForTransit desktop_pipe);
     99 
    100   // Requests the desktop process to crash.
    101   void CrashDesktopProcess(const tracked_objects::Location& location);
    102 
    103   // Reports time elapsed since previous event to the debug log.
    104   void ReportElapsedTime(const std::string& event);
    105 
    106   // Task runner on which public methods of this class should be called.
    107   scoped_refptr<AutoThreadTaskRunner> caller_task_runner_;
    108 
    109   // Message loop used by the IPC channel.
    110   scoped_refptr<AutoThreadTaskRunner> io_task_runner_;
    111 
    112   // Handle of the desktop process (running an instance of DesktopSessionAgent).
    113   base::win::ScopedHandle desktop_process_;
    114 
    115   // Launches and monitors the desktop process.
    116   scoped_ptr<WorkerProcessLauncher> launcher_;
    117 
    118   // Used to unsubscribe from session attach and detach events.
    119   WtsTerminalMonitor* monitor_;
    120 
    121   // True if |this| is subsribed to receive session attach/detach notifications.
    122   bool monitoring_notifications_;
    123 
    124   // Used to report an error if the session attach notification does not arrives
    125   // for too long.
    126   base::OneShotTimer<DesktopSessionWin> session_attach_timer_;
    127 
    128   base::Time last_timestamp_;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(DesktopSessionWin);
    131 };
    132 
    133 }  // namespace remoting
    134 
    135 #endif  // REMOTING_HOST_DESKTOP_SESSION_WIN_H_
    136