Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / remoting / protocol / connection_to_host_impl.cc
blobb6dac130a47b949ac6cf9c4b7c2682ddb9a3bb7f
1 // Copyright 2015 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/protocol/connection_to_host_impl.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "remoting/base/constants.h"
11 #include "remoting/protocol/audio_reader.h"
12 #include "remoting/protocol/audio_stub.h"
13 #include "remoting/protocol/auth_util.h"
14 #include "remoting/protocol/authenticator.h"
15 #include "remoting/protocol/client_control_dispatcher.h"
16 #include "remoting/protocol/client_event_dispatcher.h"
17 #include "remoting/protocol/client_stub.h"
18 #include "remoting/protocol/client_video_dispatcher.h"
19 #include "remoting/protocol/clipboard_stub.h"
20 #include "remoting/protocol/errors.h"
21 #include "remoting/protocol/jingle_session_manager.h"
22 #include "remoting/protocol/transport.h"
23 #include "remoting/protocol/video_stub.h"
25 namespace remoting {
26 namespace protocol {
28 ConnectionToHostImpl::ConnectionToHostImpl()
29 : event_callback_(nullptr),
30 client_stub_(nullptr),
31 clipboard_stub_(nullptr),
32 audio_stub_(nullptr),
33 signal_strategy_(nullptr),
34 state_(INITIALIZING),
35 error_(OK) {
38 ConnectionToHostImpl::~ConnectionToHostImpl() {
39 CloseChannels();
41 if (session_.get())
42 session_.reset();
44 if (session_manager_.get())
45 session_manager_.reset();
47 if (signal_strategy_)
48 signal_strategy_->RemoveListener(this);
51 #define RETURN_STRING_LITERAL(x) \
52 case x: \
53 return #x;
55 const char* ConnectionToHost::StateToString(State state) {
56 switch (state) {
57 RETURN_STRING_LITERAL(INITIALIZING);
58 RETURN_STRING_LITERAL(CONNECTING);
59 RETURN_STRING_LITERAL(AUTHENTICATED);
60 RETURN_STRING_LITERAL(CONNECTED);
61 RETURN_STRING_LITERAL(CLOSED);
62 RETURN_STRING_LITERAL(FAILED);
64 NOTREACHED();
65 return nullptr;
68 void ConnectionToHostImpl::Connect(
69 SignalStrategy* signal_strategy,
70 scoped_ptr<TransportFactory> transport_factory,
71 scoped_ptr<Authenticator> authenticator,
72 const std::string& host_jid,
73 HostEventCallback* event_callback) {
74 DCHECK(client_stub_);
75 DCHECK(clipboard_stub_);
76 DCHECK(monitored_video_stub_);
78 // Initialize default |candidate_config_| if set_candidate_config() wasn't
79 // called.
80 if (!candidate_config_) {
81 candidate_config_ = CandidateSessionConfig::CreateDefault();
82 if (!audio_stub_) {
83 candidate_config_->DisableAudioChannel();
85 candidate_config_->EnableVideoCodec(ChannelConfig::CODEC_VP9);
88 signal_strategy_ = signal_strategy;
89 event_callback_ = event_callback;
90 authenticator_ = authenticator.Pass();
92 // Save jid of the host. The actual connection is created later after
93 // |signal_strategy_| is connected.
94 host_jid_ = host_jid;
96 signal_strategy_->AddListener(this);
97 signal_strategy_->Connect();
99 session_manager_.reset(new JingleSessionManager(transport_factory.Pass()));
100 session_manager_->Init(signal_strategy_, this);
101 session_manager_->set_protocol_config(candidate_config_->Clone());
103 SetState(CONNECTING, OK);
106 void ConnectionToHostImpl::set_candidate_config(
107 scoped_ptr<CandidateSessionConfig> config) {
108 DCHECK_EQ(state_, INITIALIZING);
110 candidate_config_ = config.Pass();
113 const SessionConfig& ConnectionToHostImpl::config() {
114 return session_->config();
117 ClipboardStub* ConnectionToHostImpl::clipboard_forwarder() {
118 return &clipboard_forwarder_;
121 HostStub* ConnectionToHostImpl::host_stub() {
122 // TODO(wez): Add a HostFilter class, equivalent to input filter.
123 return control_dispatcher_.get();
126 InputStub* ConnectionToHostImpl::input_stub() {
127 return &event_forwarder_;
130 void ConnectionToHostImpl::set_client_stub(ClientStub* client_stub) {
131 client_stub_ = client_stub;
134 void ConnectionToHostImpl::set_clipboard_stub(ClipboardStub* clipboard_stub) {
135 clipboard_stub_ = clipboard_stub;
138 void ConnectionToHostImpl::set_video_stub(VideoStub* video_stub) {
139 DCHECK(video_stub);
140 monitored_video_stub_.reset(new MonitoredVideoStub(
141 video_stub, base::TimeDelta::FromSeconds(
142 MonitoredVideoStub::kConnectivityCheckDelaySeconds),
143 base::Bind(&ConnectionToHostImpl::OnVideoChannelStatus,
144 base::Unretained(this))));
147 void ConnectionToHostImpl::set_audio_stub(AudioStub* audio_stub) {
148 audio_stub_ = audio_stub;
151 void ConnectionToHostImpl::OnSignalStrategyStateChange(
152 SignalStrategy::State state) {
153 DCHECK(CalledOnValidThread());
154 DCHECK(event_callback_);
156 if (state == SignalStrategy::CONNECTED) {
157 VLOG(1) << "Connected as: " << signal_strategy_->GetLocalJid();
158 } else if (state == SignalStrategy::DISCONNECTED) {
159 VLOG(1) << "Connection closed.";
160 CloseOnError(SIGNALING_ERROR);
164 bool ConnectionToHostImpl::OnSignalStrategyIncomingStanza(
165 const buzz::XmlElement* stanza) {
166 return false;
169 void ConnectionToHostImpl::OnSessionManagerReady() {
170 DCHECK(CalledOnValidThread());
172 // After SessionManager is initialized we can try to connect to the host.
173 session_ = session_manager_->Connect(host_jid_, authenticator_.Pass());
174 session_->SetEventHandler(this);
177 void ConnectionToHostImpl::OnIncomingSession(
178 Session* session,
179 SessionManager::IncomingSessionResponse* response) {
180 DCHECK(CalledOnValidThread());
181 // Client always rejects incoming sessions.
182 *response = SessionManager::DECLINE;
185 void ConnectionToHostImpl::OnSessionStateChange(Session::State state) {
186 DCHECK(CalledOnValidThread());
187 DCHECK(event_callback_);
189 switch (state) {
190 case Session::INITIALIZING:
191 case Session::CONNECTING:
192 case Session::ACCEPTING:
193 case Session::CONNECTED:
194 case Session::AUTHENTICATING:
195 // Don't care about these events.
196 break;
198 case Session::AUTHENTICATED:
199 SetState(AUTHENTICATED, OK);
201 control_dispatcher_.reset(new ClientControlDispatcher());
202 control_dispatcher_->Init(session_.get(),
203 session_->config().control_config(), this);
204 control_dispatcher_->set_client_stub(client_stub_);
205 control_dispatcher_->set_clipboard_stub(clipboard_stub_);
207 event_dispatcher_.reset(new ClientEventDispatcher());
208 event_dispatcher_->Init(session_.get(), session_->config().event_config(),
209 this);
211 video_dispatcher_.reset(
212 new ClientVideoDispatcher(monitored_video_stub_.get()));
213 video_dispatcher_->Init(session_.get(), session_->config().video_config(),
214 this);
216 if (session_->config().is_audio_enabled()) {
217 audio_reader_.reset(new AudioReader(audio_stub_));
218 audio_reader_->Init(session_.get(), session_->config().audio_config(),
219 this);
221 break;
223 case Session::CLOSED:
224 CloseChannels();
225 SetState(CLOSED, OK);
226 break;
228 case Session::FAILED:
229 // If we were connected then treat signaling timeout error as if
230 // the connection was closed by the peer.
232 // TODO(sergeyu): This logic belongs to the webapp, but we
233 // currently don't expose this error code to the webapp, and it
234 // would be hard to add it because client plugin and webapp
235 // versions may not be in sync. It should be easy to do after we
236 // are finished moving the client plugin to NaCl.
237 if (state_ == CONNECTED && session_->error() == SIGNALING_TIMEOUT) {
238 CloseChannels();
239 SetState(CLOSED, OK);
240 } else {
241 CloseOnError(session_->error());
243 break;
247 void ConnectionToHostImpl::OnSessionRouteChange(const std::string& channel_name,
248 const TransportRoute& route) {
249 event_callback_->OnRouteChanged(channel_name, route);
252 void ConnectionToHostImpl::OnChannelInitialized(
253 ChannelDispatcherBase* channel_dispatcher) {
254 NotifyIfChannelsReady();
257 void ConnectionToHostImpl::OnChannelError(
258 ChannelDispatcherBase* channel_dispatcher,
259 ErrorCode error) {
260 LOG(ERROR) << "Failed to connect channel " << channel_dispatcher;
261 CloseOnError(CHANNEL_CONNECTION_ERROR);
262 return;
265 void ConnectionToHostImpl::OnVideoChannelStatus(bool active) {
266 event_callback_->OnConnectionReady(active);
269 ConnectionToHost::State ConnectionToHostImpl::state() const {
270 return state_;
273 void ConnectionToHostImpl::NotifyIfChannelsReady() {
274 if (!control_dispatcher_.get() || !control_dispatcher_->is_connected())
275 return;
276 if (!event_dispatcher_.get() || !event_dispatcher_->is_connected())
277 return;
278 if (!video_dispatcher_.get() || !video_dispatcher_->is_connected())
279 return;
280 if ((!audio_reader_.get() || !audio_reader_->is_connected()) &&
281 session_->config().is_audio_enabled()) {
282 return;
284 if (state_ != AUTHENTICATED)
285 return;
287 // Start forwarding clipboard and input events.
288 clipboard_forwarder_.set_clipboard_stub(control_dispatcher_.get());
289 event_forwarder_.set_input_stub(event_dispatcher_.get());
290 SetState(CONNECTED, OK);
293 void ConnectionToHostImpl::CloseOnError(ErrorCode error) {
294 CloseChannels();
295 SetState(FAILED, error);
298 void ConnectionToHostImpl::CloseChannels() {
299 control_dispatcher_.reset();
300 event_dispatcher_.reset();
301 clipboard_forwarder_.set_clipboard_stub(nullptr);
302 event_forwarder_.set_input_stub(nullptr);
303 video_dispatcher_.reset();
304 audio_reader_.reset();
307 void ConnectionToHostImpl::SetState(State state, ErrorCode error) {
308 DCHECK(CalledOnValidThread());
309 // |error| should be specified only when |state| is set to FAILED.
310 DCHECK(state == FAILED || error == OK);
312 if (state != state_) {
313 state_ = state;
314 error_ = error;
315 event_callback_->OnConnectionState(state_, error_);
319 } // namespace protocol
320 } // namespace remoting