Refactor SnapSizer now that the --ash-multiple-snap-window-widths flag no longer...
[chromium-blink-merge.git] / remoting / protocol / connection_to_host.cc
blobcdaf4b61a06c4955904cc2de28f91af4799beaed
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/protocol/connection_to_host.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/jingle_glue/signal_strategy.h"
12 #include "remoting/protocol/audio_reader.h"
13 #include "remoting/protocol/audio_stub.h"
14 #include "remoting/protocol/auth_util.h"
15 #include "remoting/protocol/authenticator.h"
16 #include "remoting/protocol/client_control_dispatcher.h"
17 #include "remoting/protocol/client_event_dispatcher.h"
18 #include "remoting/protocol/client_stub.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_reader.h"
24 #include "remoting/protocol/video_stub.h"
25 #include "remoting/protocol/util.h"
27 namespace remoting {
28 namespace protocol {
30 ConnectionToHost::ConnectionToHost(
31 bool allow_nat_traversal)
32 : allow_nat_traversal_(allow_nat_traversal),
33 event_callback_(NULL),
34 client_stub_(NULL),
35 clipboard_stub_(NULL),
36 video_stub_(NULL),
37 audio_stub_(NULL),
38 signal_strategy_(NULL),
39 state_(INITIALIZING),
40 error_(OK) {
43 ConnectionToHost::~ConnectionToHost() {
44 CloseChannels();
46 if (session_.get())
47 session_.reset();
49 if (session_manager_.get())
50 session_manager_.reset();
52 if (signal_strategy_)
53 signal_strategy_->RemoveListener(this);
56 ClipboardStub* ConnectionToHost::clipboard_stub() {
57 return &clipboard_forwarder_;
60 HostStub* ConnectionToHost::host_stub() {
61 // TODO(wez): Add a HostFilter class, equivalent to input filter.
62 return control_dispatcher_.get();
65 InputStub* ConnectionToHost::input_stub() {
66 return &event_forwarder_;
69 void ConnectionToHost::Connect(SignalStrategy* signal_strategy,
70 const std::string& host_jid,
71 const std::string& host_public_key,
72 scoped_ptr<TransportFactory> transport_factory,
73 scoped_ptr<Authenticator> authenticator,
74 HostEventCallback* event_callback,
75 ClientStub* client_stub,
76 ClipboardStub* clipboard_stub,
77 VideoStub* video_stub,
78 AudioStub* audio_stub) {
79 signal_strategy_ = signal_strategy;
80 event_callback_ = event_callback;
81 client_stub_ = client_stub;
82 clipboard_stub_ = clipboard_stub;
83 video_stub_ = video_stub;
84 audio_stub_ = audio_stub;
85 authenticator_ = authenticator.Pass();
87 // Save jid of the host. The actual connection is created later after
88 // |signal_strategy_| is connected.
89 host_jid_ = host_jid;
90 host_public_key_ = host_public_key;
92 signal_strategy_->AddListener(this);
93 signal_strategy_->Connect();
95 session_manager_.reset(new JingleSessionManager(transport_factory.Pass()));
96 session_manager_->Init(signal_strategy_, this);
98 SetState(CONNECTING, OK);
101 const SessionConfig& ConnectionToHost::config() {
102 return session_->config();
105 void ConnectionToHost::OnSignalStrategyStateChange(
106 SignalStrategy::State state) {
107 DCHECK(CalledOnValidThread());
108 DCHECK(event_callback_);
110 if (state == SignalStrategy::CONNECTED) {
111 VLOG(1) << "Connected as: " << signal_strategy_->GetLocalJid();
112 } else if (state == SignalStrategy::DISCONNECTED) {
113 VLOG(1) << "Connection closed.";
114 CloseOnError(SIGNALING_ERROR);
118 bool ConnectionToHost::OnSignalStrategyIncomingStanza(
119 const buzz::XmlElement* stanza) {
120 return false;
123 void ConnectionToHost::OnSessionManagerReady() {
124 DCHECK(CalledOnValidThread());
126 // After SessionManager is initialized we can try to connect to the host.
127 scoped_ptr<CandidateSessionConfig> candidate_config =
128 CandidateSessionConfig::CreateDefault();
129 if (!audio_stub_)
130 CandidateSessionConfig::DisableAudioChannel(candidate_config.get());
132 session_ = session_manager_->Connect(
133 host_jid_, authenticator_.Pass(), candidate_config.Pass());
134 session_->SetEventHandler(this);
137 void ConnectionToHost::OnIncomingSession(
138 Session* session,
139 SessionManager::IncomingSessionResponse* response) {
140 DCHECK(CalledOnValidThread());
141 // Client always rejects incoming sessions.
142 *response = SessionManager::DECLINE;
145 void ConnectionToHost::OnSessionStateChange(
146 Session::State state) {
147 DCHECK(CalledOnValidThread());
148 DCHECK(event_callback_);
150 switch (state) {
151 case Session::INITIALIZING:
152 case Session::CONNECTING:
153 case Session::ACCEPTING:
154 case Session::CONNECTED:
155 // Don't care about these events.
156 break;
158 case Session::AUTHENTICATED:
159 SetState(AUTHENTICATED, OK);
161 control_dispatcher_.reset(new ClientControlDispatcher());
162 control_dispatcher_->Init(
163 session_.get(), session_->config().control_config(),
164 base::Bind(&ConnectionToHost::OnChannelInitialized,
165 base::Unretained(this)));
166 control_dispatcher_->set_client_stub(client_stub_);
167 control_dispatcher_->set_clipboard_stub(clipboard_stub_);
169 event_dispatcher_.reset(new ClientEventDispatcher());
170 event_dispatcher_->Init(
171 session_.get(), session_->config().event_config(),
172 base::Bind(&ConnectionToHost::OnChannelInitialized,
173 base::Unretained(this)));
175 video_reader_ = VideoReader::Create(session_->config());
176 video_reader_->Init(session_.get(), video_stub_, base::Bind(
177 &ConnectionToHost::OnChannelInitialized, base::Unretained(this)));
179 audio_reader_ = AudioReader::Create(session_->config());
180 if (audio_reader_.get()) {
181 audio_reader_->Init(
182 session_.get(), session_->config().audio_config(),
183 base::Bind(&ConnectionToHost::OnChannelInitialized,
184 base::Unretained(this)));
185 audio_reader_->set_audio_stub(audio_stub_);
187 break;
189 case Session::CLOSED:
190 CloseChannels();
191 SetState(CLOSED, OK);
192 break;
194 case Session::FAILED:
195 // If we were connected then treat signaling timeout error as if
196 // the connection was closed by the peer.
198 // TODO(sergeyu): This logic belongs to the webapp, but we
199 // currently don't expose this error code to the webapp, and it
200 // would be hard to add it because client plugin and webapp
201 // versions may not be in sync. It should be easy to do after we
202 // are finished moving the client plugin to NaCl.
203 if (state_ == CONNECTED && session_->error() == SIGNALING_TIMEOUT) {
204 CloseChannels();
205 SetState(CLOSED, OK);
206 } else {
207 CloseOnError(session_->error());
209 break;
213 void ConnectionToHost::OnSessionRouteChange(const std::string& channel_name,
214 const TransportRoute& route) {
215 event_callback_->OnRouteChanged(channel_name, route);
218 void ConnectionToHost::OnSessionChannelReady(const std::string& channel_name,
219 bool ready) {
220 if (ready) {
221 not_ready_channels_.erase(channel_name);
222 } else if (!ready) {
223 not_ready_channels_.insert(channel_name);
226 event_callback_->OnConnectionReady(not_ready_channels_.empty());
229 ConnectionToHost::State ConnectionToHost::state() const {
230 return state_;
233 void ConnectionToHost::OnChannelInitialized(bool successful) {
234 if (!successful) {
235 LOG(ERROR) << "Failed to connect video channel";
236 CloseOnError(CHANNEL_CONNECTION_ERROR);
237 return;
240 NotifyIfChannelsReady();
243 void ConnectionToHost::NotifyIfChannelsReady() {
244 if (!control_dispatcher_.get() || !control_dispatcher_->is_connected())
245 return;
246 if (!event_dispatcher_.get() || !event_dispatcher_->is_connected())
247 return;
248 if (!video_reader_.get() || !video_reader_->is_connected())
249 return;
250 if ((!audio_reader_.get() || !audio_reader_->is_connected()) &&
251 session_->config().is_audio_enabled()) {
252 return;
254 if (state_ != AUTHENTICATED)
255 return;
257 // Start forwarding clipboard and input events.
258 clipboard_forwarder_.set_clipboard_stub(control_dispatcher_.get());
259 event_forwarder_.set_input_stub(event_dispatcher_.get());
260 SetState(CONNECTED, OK);
263 void ConnectionToHost::CloseOnError(ErrorCode error) {
264 CloseChannels();
265 SetState(FAILED, error);
268 void ConnectionToHost::CloseChannels() {
269 control_dispatcher_.reset();
270 event_dispatcher_.reset();
271 clipboard_forwarder_.set_clipboard_stub(NULL);
272 event_forwarder_.set_input_stub(NULL);
273 video_reader_.reset();
274 audio_reader_.reset();
277 void ConnectionToHost::SetState(State state, ErrorCode error) {
278 DCHECK(CalledOnValidThread());
279 // |error| should be specified only when |state| is set to FAILED.
280 DCHECK(state == FAILED || error == OK);
282 if (state != state_) {
283 state_ = state;
284 error_ = error;
285 event_callback_->OnConnectionState(state_, error_);
289 } // namespace protocol
290 } // namespace remoting