| 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/protocol/jingle_session.h" | 5 #include "remoting/protocol/jingle_session.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "remoting/base/constants.h" | 19 #include "remoting/base/constants.h" |
| 20 #include "remoting/protocol/authenticator.h" | 20 #include "remoting/protocol/authenticator.h" |
| 21 #include "remoting/protocol/content_description.h" | 21 #include "remoting/protocol/content_description.h" |
| 22 #include "remoting/protocol/jingle_messages.h" | 22 #include "remoting/protocol/jingle_messages.h" |
| 23 #include "remoting/protocol/jingle_session_manager.h" | 23 #include "remoting/protocol/jingle_session_manager.h" |
| 24 #include "remoting/protocol/session_config.h" | 24 #include "remoting/protocol/session_config.h" |
| 25 #include "remoting/protocol/transport.h" | 25 #include "remoting/protocol/transport.h" |
| 26 #include "remoting/signaling/iq_sender.h" | 26 #include "remoting/signaling/iq_sender.h" |
| 27 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 27 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 28 #include "third_party/webrtc/libjingle/xmpp/constants.h" |
| 28 #include "third_party/webrtc/p2p/base/candidate.h" | 29 #include "third_party/webrtc/p2p/base/candidate.h" |
| 29 | 30 |
| 30 using buzz::XmlElement; | 31 using buzz::XmlElement; |
| 31 | 32 |
| 32 namespace remoting { | 33 namespace remoting { |
| 33 namespace protocol { | 34 namespace protocol { |
| 34 | 35 |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 // How long we should wait for a response from the other end. This value is used | 38 // How long we should wait for a response from the other end. This value is used |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 214 |
| 214 void JingleSession::SendTransportInfo( | 215 void JingleSession::SendTransportInfo( |
| 215 std::unique_ptr<buzz::XmlElement> transport_info) { | 216 std::unique_ptr<buzz::XmlElement> transport_info) { |
| 216 DCHECK(thread_checker_.CalledOnValidThread()); | 217 DCHECK(thread_checker_.CalledOnValidThread()); |
| 217 DCHECK_EQ(state_, AUTHENTICATED); | 218 DCHECK_EQ(state_, AUTHENTICATED); |
| 218 | 219 |
| 219 std::unique_ptr<JingleMessage> message(new JingleMessage( | 220 std::unique_ptr<JingleMessage> message(new JingleMessage( |
| 220 peer_address_, JingleMessage::TRANSPORT_INFO, session_id_)); | 221 peer_address_, JingleMessage::TRANSPORT_INFO, session_id_)); |
| 221 message->transport_info = std::move(transport_info); | 222 message->transport_info = std::move(transport_info); |
| 222 | 223 |
| 224 std::unique_ptr<buzz::XmlElement> stanza = message->ToXml(); |
| 225 stanza->AddAttr(buzz::QN_ID, message_queue_.GetNextOutgoingId()); |
| 226 |
| 223 auto request = session_manager_->iq_sender()->SendIq( | 227 auto request = session_manager_->iq_sender()->SendIq( |
| 224 message->ToXml(), base::Bind(&JingleSession::OnTransportInfoResponse, | 228 std::move(stanza), base::Bind(&JingleSession::OnTransportInfoResponse, |
| 225 base::Unretained(this))); | 229 base::Unretained(this))); |
| 226 if (request) { | 230 if (request) { |
| 227 request->SetTimeout(base::TimeDelta::FromSeconds(kTransportInfoTimeout)); | 231 request->SetTimeout(base::TimeDelta::FromSeconds(kTransportInfoTimeout)); |
| 228 transport_info_requests_.push_back(std::move(request)); | 232 transport_info_requests_.push_back(std::move(request)); |
| 229 } else { | 233 } else { |
| 230 LOG(ERROR) << "Failed to send a transport-info message"; | 234 LOG(ERROR) << "Failed to send a transport-info message"; |
| 231 } | 235 } |
| 232 } | 236 } |
| 233 | 237 |
| 234 void JingleSession::Close(protocol::ErrorCode error) { | 238 void JingleSession::Close(protocol::ErrorCode error) { |
| 235 DCHECK(thread_checker_.CalledOnValidThread()); | 239 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 SetState(FAILED); | 280 SetState(FAILED); |
| 277 } else { | 281 } else { |
| 278 SetState(CLOSED); | 282 SetState(CLOSED); |
| 279 } | 283 } |
| 280 } | 284 } |
| 281 } | 285 } |
| 282 | 286 |
| 283 void JingleSession::SendMessage(std::unique_ptr<JingleMessage> message) { | 287 void JingleSession::SendMessage(std::unique_ptr<JingleMessage> message) { |
| 284 DCHECK(thread_checker_.CalledOnValidThread()); | 288 DCHECK(thread_checker_.CalledOnValidThread()); |
| 285 | 289 |
| 290 std::unique_ptr<buzz::XmlElement> stanza = message->ToXml(); |
| 291 stanza->AddAttr(buzz::QN_ID, message_queue_.GetNextOutgoingId()); |
| 292 |
| 286 auto request = session_manager_->iq_sender()->SendIq( | 293 auto request = session_manager_->iq_sender()->SendIq( |
| 287 message->ToXml(), base::Bind(&JingleSession::OnMessageResponse, | 294 std::move(stanza), base::Bind(&JingleSession::OnMessageResponse, |
| 288 base::Unretained(this), message->action)); | 295 base::Unretained(this), message->action)); |
| 289 | 296 |
| 290 int timeout = kDefaultMessageTimeout; | 297 int timeout = kDefaultMessageTimeout; |
| 291 if (message->action == JingleMessage::SESSION_INITIATE || | 298 if (message->action == JingleMessage::SESSION_INITIATE || |
| 292 message->action == JingleMessage::SESSION_ACCEPT) { | 299 message->action == JingleMessage::SESSION_ACCEPT) { |
| 293 timeout = kSessionInitiateAndAcceptTimeout; | 300 timeout = kSessionInitiateAndAcceptTimeout; |
| 294 } | 301 } |
| 295 if (request) { | 302 if (request) { |
| 296 request->SetTimeout(base::TimeDelta::FromSeconds(timeout)); | 303 request->SetTimeout(base::TimeDelta::FromSeconds(timeout)); |
| 297 pending_requests_.insert(std::move(request)); | 304 pending_requests_.insert(std::move(request)); |
| 298 } else { | 305 } else { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 } | 369 } |
| 363 | 370 |
| 364 const std::string& type = response->Attr(buzz::QName(std::string(), "type")); | 371 const std::string& type = response->Attr(buzz::QName(std::string(), "type")); |
| 365 if (type != "result") { | 372 if (type != "result") { |
| 366 LOG(ERROR) << "Received error in response to transport-info message: \"" | 373 LOG(ERROR) << "Received error in response to transport-info message: \"" |
| 367 << response->Str() << "\". Terminating the session."; | 374 << response->Str() << "\". Terminating the session."; |
| 368 Close(PEER_IS_OFFLINE); | 375 Close(PEER_IS_OFFLINE); |
| 369 } | 376 } |
| 370 } | 377 } |
| 371 | 378 |
| 372 void JingleSession::OnIncomingMessage(std::unique_ptr<JingleMessage> message, | 379 void JingleSession::OnIncomingMessage(std::string id, |
| 380 std::unique_ptr<JingleMessage> message, |
| 373 const ReplyCallback& reply_callback) { | 381 const ReplyCallback& reply_callback) { |
| 382 std::unique_ptr<QueueItem> item( |
| 383 new QueueItem(std::move(message), reply_callback)); |
| 384 std::list<std::unique_ptr<QueueItem>> ordered = |
| 385 message_queue_.OnIncomingMessage(id, std::move(item)); |
| 386 for (auto it = ordered.begin(); it != ordered.end(); it++) { |
| 387 OnIncomingMessageInOrder(std::move(it->get()->message), |
| 388 it->get()->reply_callback); |
| 389 } |
| 390 } |
| 391 |
| 392 void JingleSession::OnIncomingMessageInOrder( |
| 393 std::unique_ptr<JingleMessage> message, |
| 394 const ReplyCallback& reply_callback) { |
| 374 DCHECK(thread_checker_.CalledOnValidThread()); | 395 DCHECK(thread_checker_.CalledOnValidThread()); |
| 375 | 396 |
| 376 if (peer_address_ != message->from) { | 397 if (peer_address_ != message->from) { |
| 377 // Ignore messages received from a different Jid. | 398 // Ignore messages received from a different Jid. |
| 378 reply_callback.Run(JingleMessageReply::INVALID_SID); | 399 reply_callback.Run(JingleMessageReply::INVALID_SID); |
| 379 return; | 400 return; |
| 380 } | 401 } |
| 381 | 402 |
| 382 switch (message->action) { | 403 switch (message->action) { |
| 383 case JingleMessage::SESSION_ACCEPT: | 404 case JingleMessage::SESSION_ACCEPT: |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 if (event_handler_) | 623 if (event_handler_) |
| 603 event_handler_->OnSessionStateChange(new_state); | 624 event_handler_->OnSessionStateChange(new_state); |
| 604 } | 625 } |
| 605 } | 626 } |
| 606 | 627 |
| 607 bool JingleSession::is_session_active() { | 628 bool JingleSession::is_session_active() { |
| 608 return state_ == CONNECTING || state_ == ACCEPTING || state_ == ACCEPTED || | 629 return state_ == CONNECTING || state_ == ACCEPTING || state_ == ACCEPTED || |
| 609 state_ == AUTHENTICATING || state_ == AUTHENTICATED; | 630 state_ == AUTHENTICATING || state_ == AUTHENTICATED; |
| 610 } | 631 } |
| 611 | 632 |
| 633 JingleSession::QueueItem::QueueItem(std::unique_ptr<JingleMessage> message, |
| 634 const ReplyCallback& reply_callback) |
| 635 : message(std::move(message)), reply_callback(reply_callback) {} |
| 636 |
| 637 JingleSession::QueueItem::~QueueItem() {} |
| 638 |
| 612 } // namespace protocol | 639 } // namespace protocol |
| 613 } // namespace remoting | 640 } // namespace remoting |
| OLD | NEW |