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.
5 #ifndef REMOTING_HOST_DAEMON_PROCESS_H_
6 #define REMOTING_HOST_DAEMON_PROCESS_H_
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"
25 struct SerializedTransportRoute
;
27 namespace tracked_objects
{
29 } // namespace tracked_objects
33 class AutoThreadTaskRunner
;
35 class HostEventLogger
;
36 class HostStatusObserver
;
37 class ScreenResolution
;
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
43 : public ConfigWatcher::Delegate
,
44 public HostStatusMonitor
,
45 public WorkerProcessIpcDelegate
{
47 typedef std::list
<DesktopSession
*> DesktopSessionList
;
49 ~DaemonProcess() override
;
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
);
60 // ConfigWatcher::Delegate
61 void OnConfigUpdated(const std::string
& serialized_config
) override
;
62 void OnConfigWatcherError() override
;
64 // HostStatusMonitor interface.
65 void AddStatusObserver(HostStatusObserver
* observer
) override
;
66 void RemoveStatusObserver(HostStatusObserver
* observer
) override
;
68 // WorkerProcessIpcDelegate implementation.
69 void OnChannelConnected(int32 peer_pid
) override
;
70 bool OnMessageReceived(const IPC::Message
& message
) override
;
71 void OnPermanentError(int exit_code
) override
;
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;
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(
83 base::ProcessHandle desktop_process
,
84 IPC::PlatformFileForTransit desktop_pipe
) = 0;
86 // Closes the desktop session identified by |terminal_id|.
87 void CloseDesktopSession(int terminal_id
);
90 DaemonProcess(scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner
,
91 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner
,
92 const base::Closure
& stopped_callback
);
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
);
99 // Changes the screen resolution of the desktop session identified by
101 void SetScreenResolution(int terminal_id
, const ScreenResolution
& resolution
);
103 // Requests the network process to crash.
104 void CrashNetworkProcess(const tracked_objects::Location
& location
);
106 // Reads the host configuration and launches the network process.
109 // Invokes |stopped_callback_| to ask the owner to delete |this|.
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
);
116 // Handlers for the host status notifications received from the network
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();
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(
132 const ScreenResolution
& resolution
,
133 bool virtual_terminal
) = 0;
135 // Requests the network process to crash.
136 virtual void DoCrashNetworkProcess(
137 const tracked_objects::Location
& location
) = 0;
139 // Launches the network process and establishes an IPC channel with it.
140 virtual void LaunchNetworkProcess() = 0;
142 scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner() {
143 return caller_task_runner_
;
146 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner() {
147 return io_task_runner_
;
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_
;
157 // Deletes all desktop sessions.
158 void DeleteAllDesktopSessions();
160 // Task runner on which public methods of this class must be called.
161 scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner_
;
163 // Handles IPC and background I/O tasks.
164 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner_
;
166 scoped_ptr
<ConfigWatcher
> config_watcher_
;
168 // The configuration file contents.
169 std::string serialized_config_
;
171 // The list of active desktop sessions.
172 DesktopSessionList desktop_sessions_
;
174 // The highest desktop session ID that has been seen so far.
175 int next_terminal_id_
;
177 // Keeps track of observers receiving host status notifications.
178 base::ObserverList
<HostStatusObserver
> status_observers_
;
180 // Invoked to ask the owner to delete |this|.
181 base::Closure stopped_callback_
;
183 // Writes host status updates to the system event log.
184 scoped_ptr
<HostEventLogger
> host_event_logger_
;
186 base::WeakPtrFactory
<DaemonProcess
> weak_factory_
;
188 DISALLOW_COPY_AND_ASSIGN(DaemonProcess
);
191 } // namespace remoting
193 #endif // REMOTING_HOST_DAEMON_PROCESS_H_