Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / remoting / protocol / connection_to_host.cc
blobfaf8108ff110ccf956bd78ba141c32f65c0cbf2e
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"
26 namespace remoting {
27 namespace protocol {
29 ConnectionToHost::ConnectionToHost(
30 bool allow_nat_traversal)
31 : allow_nat_traversal_(allow_nat_traversal),
32 event_callback_(NULL),
33 client_stub_(NULL),
34 clipboard_stub_(NULL),
35 video_stub_(NULL),
36 audio_stub_(NULL),
37 signal_strategy_(NULL),
38 state_(INITIALIZING),
39 error_(OK) {
42 ConnectionToHost::~ConnectionToHost() {
43 CloseChannels();
45 if (session_.get())
46 session_.reset();
48 if (session_manager_.get())
49 session_manager_.reset();
51 if (signal_strategy_)
52 signal_strategy_->RemoveListener(this);
55 ClipboardStub* ConnectionToHost::clipboard_stub() {
56 return &clipboard_forwarder_;
59 HostStub* ConnectionToHost::host_stub() {
60 // TODO(wez): Add a HostFilter class, equivalent to input filter.
61 return control_dispatcher_.get();
64 InputStub* ConnectionToHost::input_stub() {
65 return &event_forwarder_;
68 void ConnectionToHost::Connect(SignalStrategy* signal_strategy,
69 const std::string& host_jid,
70 const std::string& host_public_key,
71 scoped_ptr<TransportFactory> transport_factory,
72 scoped_ptr<Authenticator> authenticator,
73 HostEventCallback* event_callback,
74 ClientStub* client_stub,
75 ClipboardStub* clipboard_stub,
76 VideoStub* video_stub,
77 AudioStub* audio_stub) {
78 signal_strategy_ = signal_strategy;
79 event_callback_ = event_callback;
80 client_stub_ = client_stub;
81 clipboard_stub_ = clipboard_stub;
82 video_stub_ = video_stub;
83 audio_stub_ = audio_stub;
84 authenticator_ = authenticator.Pass();
86 // Save jid of the host. The actual connection is created later after
87 // |signal_strategy_| is connected.
88 host_jid_ = host_jid;
89 host_public_key_ = host_public_key;
91 signal_strategy_->AddListener(this);
92 signal_strategy_->Connect();
94 session_manager_.reset(new JingleSessionManager(transport_factory.Pass()));
95 session_manager_->Init(signal_strategy_, this);
97 SetState(CONNECTING, OK);
100 const SessionConfig& ConnectionToHost::config() {
101 return session_->config();
104 void ConnectionToHost::OnSignalStrategyStateChange(
105 SignalStrategy::State state) {
106 DCHECK(CalledOnValidThread());
107 DCHECK(event_callback_);
109 if (state == SignalStrategy::CONNECTED) {
110 VLOG(1) << "Connected as: " << signal_strategy_->GetLocalJid();
111 } else if (state == SignalStrategy::DISCONNECTED) {
112 VLOG(1) << "Connection closed.";
113 CloseOnError(SIGNALING_ERROR);
117 bool ConnectionToHost::OnSignalStrategyIncomingStanza(
118 const buzz::XmlElement* stanza) {
119 return false;
122 void ConnectionToHost::OnSessionManagerReady() {
123 DCHECK(CalledOnValidThread());
125 // After SessionManager is initialized we can try to connect to the host.
126 scoped_ptr<CandidateSessionConfig> candidate_config =
127 CandidateSessionConfig::CreateDefault();
128 if (!audio_stub_)
129 CandidateSessionConfig::DisableAudioChannel(candidate_config.get());
131 session_ = session_manager_->Connect(
132 host_jid_, authenticator_.Pass(), candidate_config.Pass());
133 session_->SetEventHandler(this);
136 void ConnectionToHost::OnIncomingSession(
137 Session* session,
138 SessionManager::IncomingSessionResponse* response) {
139 DCHECK(CalledOnValidThread());
140 // Client always rejects incoming sessions.
141 *response = SessionManager::DECLINE;
144 void ConnectionToHost::OnSessionStateChange(
145 Session::State state) {
146 DCHECK(CalledOnValidThread());
147 DCHECK(event_callback_);
149 switch (state) {
150 case Session::INITIALIZING:
151 case Session::CONNECTING:
152 case Session::ACCEPTING:
153 case Session::CONNECTED:
154 case Session::AUTHENTICATING:
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