| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/chromoting_host.h" | 5 #include "remoting/host/chromoting_host.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 audio_task_runner_(audio_task_runner), | 76 audio_task_runner_(audio_task_runner), |
| 77 video_encode_task_runner_(video_encode_task_runner), | 77 video_encode_task_runner_(video_encode_task_runner), |
| 78 started_(false), | 78 started_(false), |
| 79 login_backoff_(&kDefaultBackoffPolicy), | 79 login_backoff_(&kDefaultBackoffPolicy), |
| 80 desktop_environment_options_(options), | 80 desktop_environment_options_(options), |
| 81 weak_factory_(this) { | 81 weak_factory_(this) { |
| 82 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 82 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 ChromotingHost::~ChromotingHost() { | 85 ChromotingHost::~ChromotingHost() { |
| 86 DCHECK(CalledOnValidThread()); | 86 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 87 | 87 |
| 88 // Disconnect all of the clients. | 88 // Disconnect all of the clients. |
| 89 while (!clients_.empty()) { | 89 while (!clients_.empty()) { |
| 90 clients_.front()->DisconnectSession(protocol::OK); | 90 clients_.front()->DisconnectSession(protocol::OK); |
| 91 } | 91 } |
| 92 | 92 |
| 93 // Destroy the session manager to make sure that |signal_strategy_| does not | 93 // Destroy the session manager to make sure that |signal_strategy_| does not |
| 94 // have any listeners registered. | 94 // have any listeners registered. |
| 95 session_manager_.reset(); | 95 session_manager_.reset(); |
| 96 | 96 |
| 97 // Notify observers. | 97 // Notify observers. |
| 98 if (started_) { | 98 if (started_) { |
| 99 for (auto& observer : status_observers_) | 99 for (auto& observer : status_observers_) |
| 100 observer.OnShutdown(); | 100 observer.OnShutdown(); |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 | 103 |
| 104 void ChromotingHost::Start(const std::string& host_owner_email) { | 104 void ChromotingHost::Start(const std::string& host_owner_email) { |
| 105 DCHECK(CalledOnValidThread()); | 105 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 106 DCHECK(!started_); | 106 DCHECK(!started_); |
| 107 | 107 |
| 108 HOST_LOG << "Starting host"; | 108 HOST_LOG << "Starting host"; |
| 109 started_ = true; | 109 started_ = true; |
| 110 for (auto& observer : status_observers_) | 110 for (auto& observer : status_observers_) |
| 111 observer.OnStart(host_owner_email); | 111 observer.OnStart(host_owner_email); |
| 112 | 112 |
| 113 session_manager_->AcceptIncoming( | 113 session_manager_->AcceptIncoming( |
| 114 base::Bind(&ChromotingHost::OnIncomingSession, base::Unretained(this))); | 114 base::Bind(&ChromotingHost::OnIncomingSession, base::Unretained(this))); |
| 115 } | 115 } |
| 116 | 116 |
| 117 void ChromotingHost::AddStatusObserver(HostStatusObserver* observer) { | 117 void ChromotingHost::AddStatusObserver(HostStatusObserver* observer) { |
| 118 DCHECK(CalledOnValidThread()); | 118 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 119 status_observers_.AddObserver(observer); | 119 status_observers_.AddObserver(observer); |
| 120 } | 120 } |
| 121 | 121 |
| 122 void ChromotingHost::RemoveStatusObserver(HostStatusObserver* observer) { | 122 void ChromotingHost::RemoveStatusObserver(HostStatusObserver* observer) { |
| 123 DCHECK(CalledOnValidThread()); | 123 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 124 status_observers_.RemoveObserver(observer); | 124 status_observers_.RemoveObserver(observer); |
| 125 } | 125 } |
| 126 | 126 |
| 127 void ChromotingHost::AddExtension(std::unique_ptr<HostExtension> extension) { | 127 void ChromotingHost::AddExtension(std::unique_ptr<HostExtension> extension) { |
| 128 extensions_.push_back(std::move(extension)); | 128 extensions_.push_back(std::move(extension)); |
| 129 } | 129 } |
| 130 | 130 |
| 131 void ChromotingHost::SetAuthenticatorFactory( | 131 void ChromotingHost::SetAuthenticatorFactory( |
| 132 std::unique_ptr<protocol::AuthenticatorFactory> authenticator_factory) { | 132 std::unique_ptr<protocol::AuthenticatorFactory> authenticator_factory) { |
| 133 DCHECK(CalledOnValidThread()); | 133 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 134 session_manager_->set_authenticator_factory(std::move(authenticator_factory)); | 134 session_manager_->set_authenticator_factory(std::move(authenticator_factory)); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void ChromotingHost::SetMaximumSessionDuration( | 137 void ChromotingHost::SetMaximumSessionDuration( |
| 138 const base::TimeDelta& max_session_duration) { | 138 const base::TimeDelta& max_session_duration) { |
| 139 max_session_duration_ = max_session_duration; | 139 max_session_duration_ = max_session_duration; |
| 140 } | 140 } |
| 141 | 141 |
| 142 //////////////////////////////////////////////////////////////////////////// | 142 //////////////////////////////////////////////////////////////////////////// |
| 143 // protocol::ClientSession::EventHandler implementation. | 143 // protocol::ClientSession::EventHandler implementation. |
| 144 void ChromotingHost::OnSessionAuthenticating(ClientSession* client) { | 144 void ChromotingHost::OnSessionAuthenticating(ClientSession* client) { |
| 145 // We treat each incoming connection as a failure to authenticate, | 145 // We treat each incoming connection as a failure to authenticate, |
| 146 // and clear the backoff when a connection successfully | 146 // and clear the backoff when a connection successfully |
| 147 // authenticates. This allows the backoff to protect from parallel | 147 // authenticates. This allows the backoff to protect from parallel |
| 148 // connection attempts as well as sequential ones. | 148 // connection attempts as well as sequential ones. |
| 149 if (login_backoff_.ShouldRejectRequest()) { | 149 if (login_backoff_.ShouldRejectRequest()) { |
| 150 LOG(WARNING) << "Disconnecting client " << client->client_jid() << " due to" | 150 LOG(WARNING) << "Disconnecting client " << client->client_jid() << " due to" |
| 151 " an overload of failed login attempts."; | 151 " an overload of failed login attempts."; |
| 152 client->DisconnectSession(protocol::HOST_OVERLOAD); | 152 client->DisconnectSession(protocol::HOST_OVERLOAD); |
| 153 return; | 153 return; |
| 154 } | 154 } |
| 155 login_backoff_.InformOfRequest(false); | 155 login_backoff_.InformOfRequest(false); |
| 156 } | 156 } |
| 157 | 157 |
| 158 void ChromotingHost::OnSessionAuthenticated(ClientSession* client) { | 158 void ChromotingHost::OnSessionAuthenticated(ClientSession* client) { |
| 159 DCHECK(CalledOnValidThread()); | 159 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 160 | 160 |
| 161 login_backoff_.Reset(); | 161 login_backoff_.Reset(); |
| 162 | 162 |
| 163 // Disconnect all clients, except |client|. | 163 // Disconnect all clients, except |client|. |
| 164 base::WeakPtr<ChromotingHost> self = weak_factory_.GetWeakPtr(); | 164 base::WeakPtr<ChromotingHost> self = weak_factory_.GetWeakPtr(); |
| 165 while (clients_.size() > 1) { | 165 while (clients_.size() > 1) { |
| 166 clients_[(clients_.front().get() == client) ? 1 : 0]->DisconnectSession( | 166 clients_[(clients_.front().get() == client) ? 1 : 0]->DisconnectSession( |
| 167 protocol::OK); | 167 protocol::OK); |
| 168 | 168 |
| 169 // Quit if the host was destroyed. | 169 // Quit if the host was destroyed. |
| 170 if (!self) | 170 if (!self) |
| 171 return; | 171 return; |
| 172 } | 172 } |
| 173 | 173 |
| 174 // Disconnects above must have destroyed all other clients. | 174 // Disconnects above must have destroyed all other clients. |
| 175 DCHECK_EQ(clients_.size(), 1U); | 175 DCHECK_EQ(clients_.size(), 1U); |
| 176 DCHECK(clients_.front().get() == client); | 176 DCHECK(clients_.front().get() == client); |
| 177 | 177 |
| 178 // Notify observers that there is at least one authenticated client. | 178 // Notify observers that there is at least one authenticated client. |
| 179 for (auto& observer : status_observers_) | 179 for (auto& observer : status_observers_) |
| 180 observer.OnClientAuthenticated(client->client_jid()); | 180 observer.OnClientAuthenticated(client->client_jid()); |
| 181 } | 181 } |
| 182 | 182 |
| 183 void ChromotingHost::OnSessionChannelsConnected(ClientSession* client) { | 183 void ChromotingHost::OnSessionChannelsConnected(ClientSession* client) { |
| 184 DCHECK(CalledOnValidThread()); | 184 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 185 | 185 |
| 186 // Notify observers. | 186 // Notify observers. |
| 187 for (auto& observer : status_observers_) | 187 for (auto& observer : status_observers_) |
| 188 observer.OnClientConnected(client->client_jid()); | 188 observer.OnClientConnected(client->client_jid()); |
| 189 } | 189 } |
| 190 | 190 |
| 191 void ChromotingHost::OnSessionAuthenticationFailed(ClientSession* client) { | 191 void ChromotingHost::OnSessionAuthenticationFailed(ClientSession* client) { |
| 192 DCHECK(CalledOnValidThread()); | 192 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 193 | 193 |
| 194 // Notify observers. | 194 // Notify observers. |
| 195 for (auto& observer : status_observers_) | 195 for (auto& observer : status_observers_) |
| 196 observer.OnAccessDenied(client->client_jid()); | 196 observer.OnAccessDenied(client->client_jid()); |
| 197 } | 197 } |
| 198 | 198 |
| 199 void ChromotingHost::OnSessionClosed(ClientSession* client) { | 199 void ChromotingHost::OnSessionClosed(ClientSession* client) { |
| 200 DCHECK(CalledOnValidThread()); | 200 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 201 | 201 |
| 202 ClientSessions::iterator it = | 202 ClientSessions::iterator it = |
| 203 std::find_if(clients_.begin(), clients_.end(), | 203 std::find_if(clients_.begin(), clients_.end(), |
| 204 [client](const std::unique_ptr<ClientSession>& item) { | 204 [client](const std::unique_ptr<ClientSession>& item) { |
| 205 return item.get() == client; | 205 return item.get() == client; |
| 206 }); | 206 }); |
| 207 CHECK(it != clients_.end()); | 207 CHECK(it != clients_.end()); |
| 208 | 208 |
| 209 bool was_authenticated = client->is_authenticated(); | 209 bool was_authenticated = client->is_authenticated(); |
| 210 std::string jid = client->client_jid(); | 210 std::string jid = client->client_jid(); |
| 211 clients_.erase(it); | 211 clients_.erase(it); |
| 212 | 212 |
| 213 if (was_authenticated) { | 213 if (was_authenticated) { |
| 214 for (auto& observer : status_observers_) | 214 for (auto& observer : status_observers_) |
| 215 observer.OnClientDisconnected(jid); | 215 observer.OnClientDisconnected(jid); |
| 216 } | 216 } |
| 217 } | 217 } |
| 218 | 218 |
| 219 void ChromotingHost::OnSessionRouteChange( | 219 void ChromotingHost::OnSessionRouteChange( |
| 220 ClientSession* session, | 220 ClientSession* session, |
| 221 const std::string& channel_name, | 221 const std::string& channel_name, |
| 222 const protocol::TransportRoute& route) { | 222 const protocol::TransportRoute& route) { |
| 223 DCHECK(CalledOnValidThread()); | 223 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 224 for (auto& observer : status_observers_) | 224 for (auto& observer : status_observers_) |
| 225 observer.OnClientRouteChange(session->client_jid(), channel_name, route); | 225 observer.OnClientRouteChange(session->client_jid(), channel_name, route); |
| 226 } | 226 } |
| 227 | 227 |
| 228 void ChromotingHost::OnIncomingSession( | 228 void ChromotingHost::OnIncomingSession( |
| 229 protocol::Session* session, | 229 protocol::Session* session, |
| 230 protocol::SessionManager::IncomingSessionResponse* response) { | 230 protocol::SessionManager::IncomingSessionResponse* response) { |
| 231 DCHECK(CalledOnValidThread()); | 231 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 232 DCHECK(started_); | 232 DCHECK(started_); |
| 233 | 233 |
| 234 if (login_backoff_.ShouldRejectRequest()) { | 234 if (login_backoff_.ShouldRejectRequest()) { |
| 235 LOG(WARNING) << "Rejecting connection due to" | 235 LOG(WARNING) << "Rejecting connection due to" |
| 236 " an overload of failed login attempts."; | 236 " an overload of failed login attempts."; |
| 237 *response = protocol::SessionManager::OVERLOAD; | 237 *response = protocol::SessionManager::OVERLOAD; |
| 238 return; | 238 return; |
| 239 } | 239 } |
| 240 | 240 |
| 241 *response = protocol::SessionManager::ACCEPT; | 241 *response = protocol::SessionManager::ACCEPT; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 260 std::vector<HostExtension*> extension_ptrs; | 260 std::vector<HostExtension*> extension_ptrs; |
| 261 for (const auto& extension : extensions_) | 261 for (const auto& extension : extensions_) |
| 262 extension_ptrs.push_back(extension.get()); | 262 extension_ptrs.push_back(extension.get()); |
| 263 clients_.push_back(base::MakeUnique<ClientSession>( | 263 clients_.push_back(base::MakeUnique<ClientSession>( |
| 264 this, std::move(connection), desktop_environment_factory_, | 264 this, std::move(connection), desktop_environment_factory_, |
| 265 desktop_environment_options_, max_session_duration_, pairing_registry_, | 265 desktop_environment_options_, max_session_duration_, pairing_registry_, |
| 266 extension_ptrs)); | 266 extension_ptrs)); |
| 267 } | 267 } |
| 268 | 268 |
| 269 } // namespace remoting | 269 } // namespace remoting |
| OLD | NEW |