Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / remoting / host / chromoting_host.cc
blob427ced6d8794b54a13b1040d159bc9cd6368fe3a
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 #include "remoting/host/chromoting_host.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/command_line.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "build/build_config.h"
14 #include "jingle/glue/thread_wrapper.h"
15 #include "remoting/base/constants.h"
16 #include "remoting/base/logging.h"
17 #include "remoting/host/chromoting_host_context.h"
18 #include "remoting/host/desktop_environment.h"
19 #include "remoting/host/host_config.h"
20 #include "remoting/host/input_injector.h"
21 #include "remoting/host/video_frame_recorder.h"
22 #include "remoting/protocol/connection_to_client.h"
23 #include "remoting/protocol/client_stub.h"
24 #include "remoting/protocol/host_stub.h"
25 #include "remoting/protocol/input_stub.h"
26 #include "remoting/protocol/session_config.h"
28 using remoting::protocol::ConnectionToClient;
29 using remoting::protocol::InputStub;
31 namespace remoting {
33 namespace {
35 const net::BackoffEntry::Policy kDefaultBackoffPolicy = {
36 // Number of initial errors (in sequence) to ignore before applying
37 // exponential back-off rules.
40 // Initial delay for exponential back-off in ms.
41 2000,
43 // Factor by which the waiting time will be multiplied.
46 // Fuzzing percentage. ex: 10% will spread requests randomly
47 // between 90%-100% of the calculated time.
50 // Maximum amount of time we are willing to delay our request in ms.
51 -1,
53 // Time to keep an entry from being discarded even when it
54 // has no significant state, -1 to never discard.
55 -1,
57 // Don't use initial delay unless the last request was an error.
58 false,
61 } // namespace
63 ChromotingHost::ChromotingHost(
64 SignalStrategy* signal_strategy,
65 DesktopEnvironmentFactory* desktop_environment_factory,
66 scoped_ptr<protocol::SessionManager> session_manager,
67 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner,
68 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
69 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner,
70 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner,
71 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
72 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
73 : desktop_environment_factory_(desktop_environment_factory),
74 session_manager_(session_manager.Pass()),
75 audio_task_runner_(audio_task_runner),
76 input_task_runner_(input_task_runner),
77 video_capture_task_runner_(video_capture_task_runner),
78 video_encode_task_runner_(video_encode_task_runner),
79 network_task_runner_(network_task_runner),
80 ui_task_runner_(ui_task_runner),
81 signal_strategy_(signal_strategy),
82 started_(false),
83 protocol_config_(protocol::CandidateSessionConfig::CreateDefault()),
84 login_backoff_(&kDefaultBackoffPolicy),
85 authenticating_client_(false),
86 reject_authenticating_client_(false),
87 enable_curtaining_(false),
88 weak_factory_(this) {
89 DCHECK(network_task_runner_->BelongsToCurrentThread());
90 DCHECK(signal_strategy);
92 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
94 if (!desktop_environment_factory_->SupportsAudioCapture()) {
95 protocol_config_->DisableAudioChannel();
99 ChromotingHost::~ChromotingHost() {
100 DCHECK(CalledOnValidThread());
102 // Disconnect all of the clients.
103 while (!clients_.empty()) {
104 clients_.front()->DisconnectSession();
107 // Destroy the session manager to make sure that |signal_strategy_| does not
108 // have any listeners registered.
109 session_manager_.reset();
111 // Notify observers.
112 if (started_)
113 FOR_EACH_OBSERVER(HostStatusObserver, status_observers_, OnShutdown());
116 void ChromotingHost::Start(const std::string& host_owner) {
117 DCHECK(CalledOnValidThread());
118 DCHECK(!started_);
120 HOST_LOG << "Starting host";
121 started_ = true;
122 FOR_EACH_OBSERVER(HostStatusObserver, status_observers_, OnStart(host_owner));
124 // Start the SessionManager, supplying this ChromotingHost as the listener.
125 session_manager_->Init(signal_strategy_, this);
128 void ChromotingHost::AddStatusObserver(HostStatusObserver* observer) {
129 DCHECK(CalledOnValidThread());
130 status_observers_.AddObserver(observer);
133 void ChromotingHost::RemoveStatusObserver(HostStatusObserver* observer) {
134 DCHECK(CalledOnValidThread());
135 status_observers_.RemoveObserver(observer);
138 void ChromotingHost::AddExtension(scoped_ptr<HostExtension> extension) {
139 extensions_.push_back(extension.release());
142 void ChromotingHost::RejectAuthenticatingClient() {
143 DCHECK(authenticating_client_);
144 reject_authenticating_client_ = true;
147 void ChromotingHost::SetAuthenticatorFactory(
148 scoped_ptr<protocol::AuthenticatorFactory> authenticator_factory) {
149 DCHECK(CalledOnValidThread());
150 session_manager_->set_authenticator_factory(authenticator_factory.Pass());
153 void ChromotingHost::SetEnableCurtaining(bool enable) {
154 DCHECK(network_task_runner_->BelongsToCurrentThread());
156 if (enable_curtaining_ == enable)
157 return;
159 enable_curtaining_ = enable;
160 desktop_environment_factory_->SetEnableCurtaining(enable_curtaining_);
162 // Disconnect all existing clients because they might be running not
163 // curtained.
164 // TODO(alexeypa): fix this such that the curtain is applied to the not
165 // curtained sessions or disconnect only the client connected to not
166 // curtained sessions.
167 if (enable_curtaining_)
168 DisconnectAllClients();
171 void ChromotingHost::SetMaximumSessionDuration(
172 const base::TimeDelta& max_session_duration) {
173 max_session_duration_ = max_session_duration;
176 ////////////////////////////////////////////////////////////////////////////
177 // protocol::ClientSession::EventHandler implementation.
178 void ChromotingHost::OnSessionAuthenticating(ClientSession* client) {
179 // We treat each incoming connection as a failure to authenticate,
180 // and clear the backoff when a connection successfully
181 // authenticates. This allows the backoff to protect from parallel
182 // connection attempts as well as sequential ones.
183 if (login_backoff_.ShouldRejectRequest()) {
184 LOG(WARNING) << "Disconnecting client " << client->client_jid() << " due to"
185 " an overload of failed login attempts.";
186 client->DisconnectSession();
187 return;
189 login_backoff_.InformOfRequest(false);
192 bool ChromotingHost::OnSessionAuthenticated(ClientSession* client) {
193 DCHECK(CalledOnValidThread());
195 login_backoff_.Reset();
197 // Disconnect all other clients. |it| should be advanced before Disconnect()
198 // is called to avoid it becoming invalid when the client is removed from
199 // the list.
200 ClientList::iterator it = clients_.begin();
201 while (it != clients_.end()) {
202 ClientSession* other_client = *it++;
203 if (other_client != client)
204 other_client->DisconnectSession();
207 // Disconnects above must have destroyed all other clients.
208 DCHECK_EQ(clients_.size(), 1U);
210 // Notify observers that there is at least one authenticated client.
211 const std::string& jid = client->client_jid();
213 reject_authenticating_client_ = false;
215 authenticating_client_ = true;
216 FOR_EACH_OBSERVER(HostStatusObserver, status_observers_,
217 OnClientAuthenticated(jid));
218 authenticating_client_ = false;
220 return !reject_authenticating_client_;
223 void ChromotingHost::OnSessionChannelsConnected(ClientSession* client) {
224 DCHECK(CalledOnValidThread());
226 // Notify observers.
227 FOR_EACH_OBSERVER(HostStatusObserver, status_observers_,
228 OnClientConnected(client->client_jid()));
231 void ChromotingHost::OnSessionAuthenticationFailed(ClientSession* client) {
232 DCHECK(CalledOnValidThread());
234 // Notify observers.
235 FOR_EACH_OBSERVER(HostStatusObserver, status_observers_,
236 OnAccessDenied(client->client_jid()));
239 void ChromotingHost::OnSessionClosed(ClientSession* client) {
240 DCHECK(CalledOnValidThread());
242 ClientList::iterator it = std::find(clients_.begin(), clients_.end(), client);
243 CHECK(it != clients_.end());
245 if (client->is_authenticated()) {
246 FOR_EACH_OBSERVER(HostStatusObserver, status_observers_,
247 OnClientDisconnected(client->client_jid()));
250 clients_.erase(it);
251 delete client;
254 void ChromotingHost::OnSessionRouteChange(
255 ClientSession* session,
256 const std::string& channel_name,
257 const protocol::TransportRoute& route) {
258 DCHECK(CalledOnValidThread());
259 FOR_EACH_OBSERVER(HostStatusObserver, status_observers_,
260 OnClientRouteChange(session->client_jid(), channel_name,
261 route));
264 void ChromotingHost::OnSessionManagerReady() {
265 DCHECK(CalledOnValidThread());
266 // Don't need to do anything here, just wait for incoming
267 // connections.
270 void ChromotingHost::OnIncomingSession(
271 protocol::Session* session,
272 protocol::SessionManager::IncomingSessionResponse* response) {
273 DCHECK(CalledOnValidThread());
275 if (!started_) {
276 *response = protocol::SessionManager::DECLINE;
277 return;
280 if (login_backoff_.ShouldRejectRequest()) {
281 LOG(WARNING) << "Rejecting connection due to"
282 " an overload of failed login attempts.";
283 *response = protocol::SessionManager::OVERLOAD;
284 return;
287 protocol::SessionConfig config;
288 if (!protocol_config_->Select(session->candidate_config(), &config)) {
289 LOG(WARNING) << "Rejecting connection from " << session->jid()
290 << " because no compatible configuration has been found.";
291 *response = protocol::SessionManager::INCOMPATIBLE;
292 return;
295 session->set_config(config);
297 *response = protocol::SessionManager::ACCEPT;
299 HOST_LOG << "Client connected: " << session->jid();
301 // Create a client object.
302 scoped_ptr<protocol::ConnectionToClient> connection(
303 new protocol::ConnectionToClient(session));
304 ClientSession* client = new ClientSession(
305 this,
306 audio_task_runner_,
307 input_task_runner_,
308 video_capture_task_runner_,
309 video_encode_task_runner_,
310 network_task_runner_,
311 ui_task_runner_,
312 connection.Pass(),
313 desktop_environment_factory_,
314 max_session_duration_,
315 pairing_registry_,
316 extensions_.get());
318 clients_.push_back(client);
321 void ChromotingHost::set_protocol_config(
322 scoped_ptr<protocol::CandidateSessionConfig> config) {
323 DCHECK(CalledOnValidThread());
324 DCHECK(config.get());
325 DCHECK(!started_);
326 protocol_config_ = config.Pass();
329 void ChromotingHost::DisconnectAllClients() {
330 DCHECK(CalledOnValidThread());
332 while (!clients_.empty()) {
333 size_t size = clients_.size();
334 clients_.front()->DisconnectSession();
335 CHECK_EQ(clients_.size(), size - 1);
339 } // namespace remoting