daemon_process.h revision a3f7b4e666c476898878fa745f637129375cd889
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_DAEMON_PROCESS_H_ 6#define REMOTING_HOST_DAEMON_PROCESS_H_ 7 8#include <list> 9 10#include "base/basictypes.h" 11#include "base/compiler_specific.h" 12#include "base/memory/ref_counted.h" 13#include "base/memory/scoped_ptr.h" 14#include "base/memory/weak_ptr.h" 15#include "base/observer_list.h" 16#include "base/process/process.h" 17#include "ipc/ipc_channel.h" 18#include "ipc/ipc_channel_proxy.h" 19#include "ipc/ipc_platform_file.h" 20#include "remoting/host/config_file_watcher.h" 21#include "remoting/host/host_status_monitor.h" 22#include "remoting/host/worker_process_ipc_delegate.h" 23 24struct SerializedTransportRoute; 25 26namespace tracked_objects { 27class Location; 28} // namespace tracked_objects 29 30namespace remoting { 31 32class AutoThreadTaskRunner; 33class DesktopSession; 34class HostEventLogger; 35class HostStatusObserver; 36class ScreenResolution; 37 38// This class implements core of the daemon process. It manages the networking 39// process running at lower privileges and maintains the list of desktop 40// sessions. 41class DaemonProcess 42 : public ConfigFileWatcher::Delegate, 43 public HostStatusMonitor, 44 public WorkerProcessIpcDelegate { 45 public: 46 typedef std::list<DesktopSession*> DesktopSessionList; 47 48 virtual ~DaemonProcess(); 49 50 // Creates a platform-specific implementation of the daemon process object 51 // passing relevant task runners. Public methods of this class must be called 52 // on the |caller_task_runner| thread. |io_task_runner| is used to handle IPC 53 // and background I/O tasks. 54 static scoped_ptr<DaemonProcess> Create( 55 scoped_refptr<AutoThreadTaskRunner> caller_task_runner, 56 scoped_refptr<AutoThreadTaskRunner> io_task_runner, 57 const base::Closure& stopped_callback); 58 59 // ConfigFileWatcher::Delegate 60 virtual void OnConfigUpdated(const std::string& serialized_config) OVERRIDE; 61 virtual void OnConfigWatcherError() OVERRIDE; 62 63 // HostStatusMonitor interface. 64 virtual void AddStatusObserver(HostStatusObserver* observer) OVERRIDE; 65 virtual void RemoveStatusObserver(HostStatusObserver* observer) OVERRIDE; 66 67 // WorkerProcessIpcDelegate implementation. 68 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; 69 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 70 virtual void OnPermanentError(int exit_code) OVERRIDE; 71 72 // Sends an IPC message to the network process. The message will be dropped 73 // unless the network process is connected over the IPC channel. 74 virtual void SendToNetwork(IPC::Message* message) = 0; 75 76 // Called when a desktop integration process attaches to |terminal_id|. 77 // |desktop_process| is a handle of the desktop integration process. 78 // |desktop_pipe| specifies the client end of the desktop pipe. Returns true 79 // on success, false otherwise. 80 virtual bool OnDesktopSessionAgentAttached( 81 int terminal_id, 82 base::ProcessHandle desktop_process, 83 IPC::PlatformFileForTransit desktop_pipe) = 0; 84 85 // Closes the desktop session identified by |terminal_id|. 86 void CloseDesktopSession(int terminal_id); 87 88 protected: 89 DaemonProcess(scoped_refptr<AutoThreadTaskRunner> caller_task_runner, 90 scoped_refptr<AutoThreadTaskRunner> io_task_runner, 91 const base::Closure& stopped_callback); 92 93 // Creates a desktop session and assigns a unique ID to it. 94 void CreateDesktopSession(int terminal_id, 95 const ScreenResolution& resolution, 96 bool virtual_terminal); 97 98 // Changes the screen resolution of the desktop session identified by 99 // |terminal_id|. 100 void SetScreenResolution(int terminal_id, const ScreenResolution& resolution); 101 102 // Requests the network process to crash. 103 void CrashNetworkProcess(const tracked_objects::Location& location); 104 105 // Reads the host configuration and launches the network process. 106 void Initialize(); 107 108 // Invokes |stopped_callback_| to ask the owner to delete |this|. 109 void Stop(); 110 111 // Returns true if |terminal_id| is in the range of allocated IDs. I.e. it is 112 // less or equal to the highest ID we have seen so far. 113 bool WasTerminalIdAllocated(int terminal_id); 114 115 // Handlers for the host status notifications received from the network 116 // process. 117 void OnAccessDenied(const std::string& jid); 118 void OnClientAuthenticated(const std::string& jid); 119 void OnClientConnected(const std::string& jid); 120 void OnClientDisconnected(const std::string& jid); 121 void OnClientRouteChange(const std::string& jid, 122 const std::string& channel_name, 123 const SerializedTransportRoute& route); 124 void OnHostStarted(const std::string& xmpp_login); 125 void OnHostShutdown(); 126 127 // Creates a platform-specific desktop session and assigns a unique ID to it. 128 // An implementation should validate |params| as they are received via IPC. 129 virtual scoped_ptr<DesktopSession> DoCreateDesktopSession( 130 int terminal_id, 131 const ScreenResolution& resolution, 132 bool virtual_terminal) = 0; 133 134 // Requests the network process to crash. 135 virtual void DoCrashNetworkProcess( 136 const tracked_objects::Location& location) = 0; 137 138 // Launches the network process and establishes an IPC channel with it. 139 virtual void LaunchNetworkProcess() = 0; 140 141 scoped_refptr<AutoThreadTaskRunner> caller_task_runner() { 142 return caller_task_runner_; 143 } 144 145 scoped_refptr<AutoThreadTaskRunner> io_task_runner() { 146 return io_task_runner_; 147 } 148 149 // Let the test code analyze the list of desktop sessions. 150 friend class DaemonProcessTest; 151 const DesktopSessionList& desktop_sessions() const { 152 return desktop_sessions_; 153 } 154 155 private: 156 // Deletes all desktop sessions. 157 void DeleteAllDesktopSessions(); 158 159 // Task runner on which public methods of this class must be called. 160 scoped_refptr<AutoThreadTaskRunner> caller_task_runner_; 161 162 // Handles IPC and background I/O tasks. 163 scoped_refptr<AutoThreadTaskRunner> io_task_runner_; 164 165 scoped_ptr<ConfigFileWatcher> config_watcher_; 166 167 // The configuration file contents. 168 std::string serialized_config_; 169 170 // The list of active desktop sessions. 171 DesktopSessionList desktop_sessions_; 172 173 // The highest desktop session ID that has been seen so far. 174 int next_terminal_id_; 175 176 // Keeps track of observers receiving host status notifications. 177 ObserverList<HostStatusObserver> status_observers_; 178 179 // Invoked to ask the owner to delete |this|. 180 base::Closure stopped_callback_; 181 182 // Writes host status updates to the system event log. 183 scoped_ptr<HostEventLogger> host_event_logger_; 184 185 base::WeakPtrFactory<DaemonProcess> weak_factory_; 186 187 DISALLOW_COPY_AND_ASSIGN(DaemonProcess); 188}; 189 190} // namespace remoting 191 192#endif // REMOTING_HOST_DAEMON_PROCESS_H_ 193