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_CHROMOTING_HOST_H_
6#define REMOTING_HOST_CHROMOTING_HOST_H_
7
8#include <list>
9#include <string>
10
11#include "base/memory/scoped_ptr.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/weak_ptr.h"
14#include "base/observer_list.h"
15#include "base/threading/non_thread_safe.h"
16#include "base/threading/thread.h"
17#include "net/base/backoff_entry.h"
18#include "remoting/host/client_session.h"
19#include "remoting/host/host_status_monitor.h"
20#include "remoting/host/host_status_observer.h"
21#include "remoting/protocol/authenticator.h"
22#include "remoting/protocol/connection_to_client.h"
23#include "remoting/protocol/pairing_registry.h"
24#include "remoting/protocol/session_manager.h"
25
26namespace base {
27class SingleThreadTaskRunner;
28}  // namespace base
29
30namespace remoting {
31
32namespace protocol {
33class InputStub;
34class SessionConfig;
35class CandidateSessionConfig;
36}  // namespace protocol
37
38class DesktopEnvironmentFactory;
39
40// A class to implement the functionality of a host process.
41//
42// Here's the work flow of this class:
43// 1. We should load the saved GAIA ID token or if this is the first
44//    time the host process runs we should prompt user for the
45//    credential. We will use this token or credentials to authenicate
46//    and register the host.
47//
48// 2. We listen for incoming connection using libjingle. We will create
49//    a ConnectionToClient object that wraps around linjingle for transport.
50//    A VideoScheduler is created with an Encoder and a webrtc::ScreenCapturer.
51//    A ConnectionToClient is added to the ScreenRecorder for transporting
52//    the screen captures. An InputStub is created and registered with the
53//    ConnectionToClient to receive mouse / keyboard events from the remote
54//    client.
55//    After we have done all the initialization we'll start the ScreenRecorder.
56//    We'll then enter the running state of the host process.
57//
58// 3. When the user is disconnected, we will pause the ScreenRecorder
59//    and try to terminate the threads we have created. This will allow
60//    all pending tasks to complete. After all of that completed we
61//    return to the idle state. We then go to step (2) if there a new
62//    incoming connection.
63class ChromotingHost : public base::NonThreadSafe,
64                       public ClientSession::EventHandler,
65                       public protocol::SessionManager::Listener,
66                       public HostStatusMonitor {
67 public:
68  // Both |signal_strategy| and |desktop_environment_factory| should outlive
69  // this object.
70  ChromotingHost(
71      SignalStrategy* signal_strategy,
72      DesktopEnvironmentFactory* desktop_environment_factory,
73      scoped_ptr<protocol::SessionManager> session_manager,
74      scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner,
75      scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
76      scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner,
77      scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner,
78      scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
79      scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
80  virtual ~ChromotingHost();
81
82  // Asynchronously starts the host.
83  //
84  // After this is invoked, the host process will connect to the talk
85  // network and start listening for incoming connections.
86  //
87  // This method can only be called once during the lifetime of this object.
88  void Start(const std::string& host_owner);
89
90  // HostStatusMonitor interface.
91  virtual void AddStatusObserver(HostStatusObserver* observer) OVERRIDE;
92  virtual void RemoveStatusObserver(HostStatusObserver* observer) OVERRIDE;
93
94  // This method may be called only from
95  // HostStatusObserver::OnClientAuthenticated() to reject the new
96  // client.
97  void RejectAuthenticatingClient();
98
99  // Sets the authenticator factory to use for incoming
100  // connections. Incoming connections are rejected until
101  // authenticator factory is set. Must be called on the network
102  // thread after the host is started. Must not be called more than
103  // once per host instance because it may not be safe to delete
104  // factory before all authenticators it created are deleted.
105  void SetAuthenticatorFactory(
106      scoped_ptr<protocol::AuthenticatorFactory> authenticator_factory);
107
108  // Enables/disables curtaining when one or more clients are connected.
109  // Takes immediate effect if clients are already connected.
110  void SetEnableCurtaining(bool enable);
111
112  // Sets the maximum duration of any session. By default, a session has no
113  // maximum duration.
114  void SetMaximumSessionDuration(const base::TimeDelta& max_session_duration);
115
116  ////////////////////////////////////////////////////////////////////////////
117  // ClientSession::EventHandler implementation.
118  virtual bool OnSessionAuthenticated(ClientSession* client) OVERRIDE;
119  virtual void OnSessionChannelsConnected(ClientSession* client) OVERRIDE;
120  virtual void OnSessionAuthenticationFailed(ClientSession* client) OVERRIDE;
121  virtual void OnSessionClosed(ClientSession* session) OVERRIDE;
122  virtual void OnSessionSequenceNumber(ClientSession* session,
123                                       int64 sequence_number) OVERRIDE;
124  virtual void OnSessionRouteChange(
125      ClientSession* session,
126      const std::string& channel_name,
127      const protocol::TransportRoute& route) OVERRIDE;
128
129  // SessionManager::Listener implementation.
130  virtual void OnSessionManagerReady() OVERRIDE;
131  virtual void OnIncomingSession(
132      protocol::Session* session,
133      protocol::SessionManager::IncomingSessionResponse* response) OVERRIDE;
134
135  // Sets desired configuration for the protocol. Must be called before Start().
136  void set_protocol_config(scoped_ptr<protocol::CandidateSessionConfig> config);
137
138  base::WeakPtr<ChromotingHost> AsWeakPtr() {
139    return weak_factory_.GetWeakPtr();
140  }
141
142  // The host uses a pairing registry to generate and store pairing information
143  // for clients for PIN-less authentication.
144  scoped_refptr<protocol::PairingRegistry> pairing_registry() const {
145    return pairing_registry_;
146  }
147  void set_pairing_registry(
148      scoped_refptr<protocol::PairingRegistry> pairing_registry) {
149    pairing_registry_ = pairing_registry;
150  }
151
152 private:
153  friend class ChromotingHostTest;
154
155  typedef std::list<ClientSession*> ClientList;
156
157  // Immediately disconnects all active clients. Host-internal components may
158  // shutdown asynchronously, but the caller is guaranteed not to receive
159  // callbacks for disconnected clients after this call returns.
160  void DisconnectAllClients();
161
162  // Unless specified otherwise all members of this class must be
163  // used on the network thread only.
164
165  // Parameters specified when the host was created.
166  DesktopEnvironmentFactory* desktop_environment_factory_;
167  scoped_ptr<protocol::SessionManager> session_manager_;
168  scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner_;
169  scoped_refptr<base::SingleThreadTaskRunner> input_task_runner_;
170  scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner_;
171  scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner_;
172  scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
173  scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
174
175  // Connection objects.
176  SignalStrategy* signal_strategy_;
177
178  // Must be used on the network thread only.
179  ObserverList<HostStatusObserver> status_observers_;
180
181  // The connections to remote clients.
182  ClientList clients_;
183
184  // True if the host has been started.
185  bool started_;
186
187  // Configuration of the protocol.
188  scoped_ptr<protocol::CandidateSessionConfig> protocol_config_;
189
190  // Login backoff state.
191  net::BackoffEntry login_backoff_;
192
193  // Flags used for RejectAuthenticatingClient().
194  bool authenticating_client_;
195  bool reject_authenticating_client_;
196
197  // True if the curtain mode is enabled.
198  bool enable_curtaining_;
199
200  // The maximum duration of any session.
201  base::TimeDelta max_session_duration_;
202
203  // The pairing registry for PIN-less authentication.
204  scoped_refptr<protocol::PairingRegistry> pairing_registry_;
205
206  base::WeakPtrFactory<ChromotingHost> weak_factory_;
207
208  DISALLOW_COPY_AND_ASSIGN(ChromotingHost);
209};
210
211}  // namespace remoting
212
213#endif  // REMOTING_HOST_CHROMOTING_HOST_H_
214