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