OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/protocol/jingle_session.h" | 5 #include "remoting/protocol/jingle_session.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 DCHECK(CalledOnValidThread()); | 75 DCHECK(CalledOnValidThread()); |
76 | 76 |
77 cricket_session_ = cricket_session; | 77 cricket_session_ = cricket_session; |
78 jid_ = cricket_session_->remote_name(); | 78 jid_ = cricket_session_->remote_name(); |
79 cricket_session_->SignalState.connect( | 79 cricket_session_->SignalState.connect( |
80 this, &JingleSession::OnSessionState); | 80 this, &JingleSession::OnSessionState); |
81 cricket_session_->SignalError.connect( | 81 cricket_session_->SignalError.connect( |
82 this, &JingleSession::OnSessionError); | 82 this, &JingleSession::OnSessionError); |
83 } | 83 } |
84 | 84 |
85 void JingleSession::CloseInternal(int result, bool failed) { | 85 void JingleSession::CloseInternal(int result, Error error) { |
86 DCHECK(CalledOnValidThread()); | 86 DCHECK(CalledOnValidThread()); |
87 | 87 |
88 if (state_ != FAILED && state_ != CLOSED && !closing_) { | 88 if (state_ != FAILED && state_ != CLOSED && !closing_) { |
89 closing_ = true; | 89 closing_ = true; |
90 | 90 |
91 control_channel_socket_.reset(); | 91 control_channel_socket_.reset(); |
92 event_channel_socket_.reset(); | 92 event_channel_socket_.reset(); |
93 STLDeleteContainerPairSecondPointers(channel_connectors_.begin(), | 93 STLDeleteContainerPairSecondPointers(channel_connectors_.begin(), |
94 channel_connectors_.end()); | 94 channel_connectors_.end()); |
95 | 95 |
96 // Tear down the cricket session, including the cricket transport channels. | 96 // Tear down the cricket session, including the cricket transport channels. |
97 if (cricket_session_) { | 97 if (cricket_session_) { |
98 cricket_session_->Terminate(); | 98 std::string reason; |
| 99 switch (error) { |
| 100 case OK: |
| 101 reason = cricket::STR_TERMINATE_SUCCESS; |
| 102 break; |
| 103 case PEER_IS_OFFLINE: |
| 104 reason = cricket::STR_TERMINATE_ERROR; |
| 105 break; |
| 106 case SESSION_REJECTED: |
| 107 reason = cricket::STR_TERMINATE_DECLINE; |
| 108 break; |
| 109 case INCOMPATIBLE_PROTOCOL: |
| 110 reason = cricket::STR_TERMINATE_INCOMPATIBLE_PARAMETERS; |
| 111 break; |
| 112 case CHANNEL_CONNECTION_ERROR: |
| 113 reason = cricket::STR_TERMINATE_ERROR; |
| 114 break; |
| 115 } |
| 116 cricket_session_->TerminateWithReason(reason); |
99 cricket_session_->SignalState.disconnect(this); | 117 cricket_session_->SignalState.disconnect(this); |
100 } | 118 } |
101 | 119 |
| 120 error_ = error; |
| 121 |
102 // Inform the StateChangeCallback, so calling code knows not to | 122 // Inform the StateChangeCallback, so calling code knows not to |
103 // touch any channels. Needs to be done in the end because the | 123 // touch any channels. Needs to be done in the end because the |
104 // session may be deleted in response to this event. | 124 // session may be deleted in response to this event. |
105 if (failed) | 125 if (error != OK) { |
106 SetState(FAILED); | 126 SetState(FAILED); |
107 else | 127 } else { |
108 SetState(CLOSED); | 128 SetState(CLOSED); |
| 129 } |
109 } | 130 } |
110 } | 131 } |
111 | 132 |
112 bool JingleSession::HasSession(cricket::Session* cricket_session) { | 133 bool JingleSession::HasSession(cricket::Session* cricket_session) { |
113 DCHECK(CalledOnValidThread()); | 134 DCHECK(CalledOnValidThread()); |
114 return cricket_session_ == cricket_session; | 135 return cricket_session_ == cricket_session; |
115 } | 136 } |
116 | 137 |
117 cricket::Session* JingleSession::ReleaseSession() { | 138 cricket::Session* JingleSession::ReleaseSession() { |
118 DCHECK(CalledOnValidThread()); | 139 DCHECK(CalledOnValidThread()); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 | 250 |
230 const std::string& JingleSession::shared_secret() { | 251 const std::string& JingleSession::shared_secret() { |
231 DCHECK(CalledOnValidThread()); | 252 DCHECK(CalledOnValidThread()); |
232 return shared_secret_; | 253 return shared_secret_; |
233 } | 254 } |
234 | 255 |
235 | 256 |
236 void JingleSession::Close() { | 257 void JingleSession::Close() { |
237 DCHECK(CalledOnValidThread()); | 258 DCHECK(CalledOnValidThread()); |
238 | 259 |
239 CloseInternal(net::ERR_CONNECTION_CLOSED, false); | 260 CloseInternal(net::ERR_CONNECTION_CLOSED, OK); |
240 } | 261 } |
241 | 262 |
242 void JingleSession::OnSessionState( | 263 void JingleSession::OnSessionState( |
243 BaseSession* session, BaseSession::State state) { | 264 BaseSession* session, BaseSession::State state) { |
244 DCHECK(CalledOnValidThread()); | 265 DCHECK(CalledOnValidThread()); |
245 DCHECK_EQ(cricket_session_, session); | 266 DCHECK_EQ(cricket_session_, session); |
246 | 267 |
247 if (state_ == FAILED || state_ == CLOSED) { | 268 if (state_ == FAILED || state_ == CLOSED) { |
248 // Don't do anything if we already closed. | 269 // Don't do anything if we already closed. |
249 return; | 270 return; |
(...skipping 27 matching lines...) Expand all Loading... |
277 break; | 298 break; |
278 } | 299 } |
279 } | 300 } |
280 | 301 |
281 void JingleSession::OnSessionError( | 302 void JingleSession::OnSessionError( |
282 BaseSession* session, BaseSession::Error error) { | 303 BaseSession* session, BaseSession::Error error) { |
283 DCHECK(CalledOnValidThread()); | 304 DCHECK(CalledOnValidThread()); |
284 DCHECK_EQ(cricket_session_, session); | 305 DCHECK_EQ(cricket_session_, session); |
285 | 306 |
286 if (error != cricket::Session::ERROR_NONE) { | 307 if (error != cricket::Session::ERROR_NONE) { |
287 CloseInternal(net::ERR_CONNECTION_ABORTED, true); | 308 // TODO(sergeyu): Report different errors depending on |error|. |
| 309 CloseInternal(net::ERR_CONNECTION_ABORTED, CHANNEL_CONNECTION_ERROR); |
288 } | 310 } |
289 } | 311 } |
290 | 312 |
291 void JingleSession::OnInitiate() { | 313 void JingleSession::OnInitiate() { |
292 DCHECK(CalledOnValidThread()); | 314 DCHECK(CalledOnValidThread()); |
293 jid_ = cricket_session_->remote_name(); | 315 jid_ = cricket_session_->remote_name(); |
294 | 316 |
295 if (!cricket_session_->initiator()) { | 317 if (!cricket_session_->initiator()) { |
296 const protocol::ContentDescription* content_description = | 318 const protocol::ContentDescription* content_description = |
297 static_cast<const protocol::ContentDescription*>( | 319 static_cast<const protocol::ContentDescription*>( |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 } | 372 } |
351 | 373 |
352 void JingleSession::OnAccept() { | 374 void JingleSession::OnAccept() { |
353 DCHECK(CalledOnValidThread()); | 375 DCHECK(CalledOnValidThread()); |
354 | 376 |
355 // If we initiated the session, store the candidate configuration that the | 377 // If we initiated the session, store the candidate configuration that the |
356 // host responded with, to refer to later. | 378 // host responded with, to refer to later. |
357 if (cricket_session_->initiator()) { | 379 if (cricket_session_->initiator()) { |
358 if (!InitializeConfigFromDescription( | 380 if (!InitializeConfigFromDescription( |
359 cricket_session_->remote_description())) { | 381 cricket_session_->remote_description())) { |
360 CloseInternal(net::ERR_CONNECTION_FAILED, true); | 382 CloseInternal(net::ERR_CONNECTION_FAILED, INCOMPATIBLE_PROTOCOL); |
361 return; | 383 return; |
362 } | 384 } |
363 } | 385 } |
364 | 386 |
365 CreateChannels(); | 387 CreateChannels(); |
366 | 388 |
367 SetState(CONNECTED); | 389 SetState(CONNECTED); |
368 } | 390 } |
369 | 391 |
370 void JingleSession::OnTerminate() { | 392 void JingleSession::OnTerminate() { |
371 DCHECK(CalledOnValidThread()); | 393 DCHECK(CalledOnValidThread()); |
372 CloseInternal(net::ERR_CONNECTION_ABORTED, false); | 394 CloseInternal(net::ERR_CONNECTION_ABORTED, OK); |
373 } | 395 } |
374 | 396 |
375 void JingleSession::AcceptConnection() { | 397 void JingleSession::AcceptConnection() { |
376 SetState(CONNECTING); | 398 SetState(CONNECTING); |
377 | 399 |
378 if (!jingle_session_manager_->AcceptConnection(this, cricket_session_)) { | 400 if (!jingle_session_manager_->AcceptConnection(this, cricket_session_)) { |
379 Close(); | 401 Close(); |
380 // Release session so that JingleSessionManager::SessionDestroyed() | 402 // Release session so that JingleSessionManager::SessionDestroyed() |
381 // doesn't try to call cricket::SessionManager::DestroySession() for it. | 403 // doesn't try to call cricket::SessionManager::DestroySession() for it. |
382 ReleaseSession(); | 404 ReleaseSession(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 base::Bind(&JingleSession::OnChannelConnected, | 449 base::Bind(&JingleSession::OnChannelConnected, |
428 base::Unretained(this), &event_channel_socket_)); | 450 base::Unretained(this), &event_channel_socket_)); |
429 } | 451 } |
430 | 452 |
431 void JingleSession::OnChannelConnected( | 453 void JingleSession::OnChannelConnected( |
432 scoped_ptr<net::Socket>* socket_container, | 454 scoped_ptr<net::Socket>* socket_container, |
433 net::StreamSocket* socket) { | 455 net::StreamSocket* socket) { |
434 if (!socket) { | 456 if (!socket) { |
435 LOG(ERROR) << "Failed to connect control or events channel. " | 457 LOG(ERROR) << "Failed to connect control or events channel. " |
436 << "Terminating connection"; | 458 << "Terminating connection"; |
437 CloseInternal(net::ERR_CONNECTION_CLOSED, true); | 459 CloseInternal(net::ERR_CONNECTION_CLOSED, CHANNEL_CONNECTION_ERROR); |
438 return; | 460 return; |
439 } | 461 } |
440 | 462 |
441 socket_container->reset(socket); | 463 socket_container->reset(socket); |
442 | 464 |
443 if (control_channel_socket_.get() && event_channel_socket_.get()) | 465 if (control_channel_socket_.get() && event_channel_socket_.get()) |
444 SetState(CONNECTED_CHANNELS); | 466 SetState(CONNECTED_CHANNELS); |
445 } | 467 } |
446 | 468 |
447 const cricket::ContentInfo* JingleSession::GetContentInfo() const { | 469 const cricket::ContentInfo* JingleSession::GetContentInfo() const { |
(...skipping 19 matching lines...) Expand all Loading... |
467 DCHECK_NE(state_, FAILED); | 489 DCHECK_NE(state_, FAILED); |
468 | 490 |
469 state_ = new_state; | 491 state_ = new_state; |
470 if (!state_change_callback_.is_null()) | 492 if (!state_change_callback_.is_null()) |
471 state_change_callback_.Run(new_state); | 493 state_change_callback_.Run(new_state); |
472 } | 494 } |
473 } | 495 } |
474 | 496 |
475 } // namespace protocol | 497 } // namespace protocol |
476 } // namespace remoting | 498 } // namespace remoting |
OLD | NEW |