| 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/pepper_session.h" | 5 #include "remoting/protocol/pepper_session.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "remoting/base/constants.h" | 11 #include "remoting/base/constants.h" |
| 12 #include "remoting/jingle_glue/iq_sender.h" | 12 #include "remoting/jingle_glue/iq_sender.h" |
| 13 #include "remoting/protocol/authenticator.h" | 13 #include "remoting/protocol/authenticator.h" |
| 14 #include "remoting/protocol/channel_authenticator.h" |
| 14 #include "remoting/protocol/content_description.h" | 15 #include "remoting/protocol/content_description.h" |
| 15 #include "remoting/protocol/jingle_messages.h" | 16 #include "remoting/protocol/jingle_messages.h" |
| 16 #include "remoting/protocol/pepper_session_manager.h" | 17 #include "remoting/protocol/pepper_session_manager.h" |
| 17 #include "remoting/protocol/pepper_stream_channel.h" | |
| 18 #include "third_party/libjingle/source/talk/p2p/base/candidate.h" | 18 #include "third_party/libjingle/source/talk/p2p/base/candidate.h" |
| 19 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 19 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| 20 | 20 |
| 21 using buzz::XmlElement; | 21 using buzz::XmlElement; |
| 22 | 22 |
| 23 namespace remoting { | 23 namespace remoting { |
| 24 namespace protocol { | 24 namespace protocol { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 // Delay after candidate creation before sending transport-info | 27 // Delay after candidate creation before sending transport-info |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 CloseInternal(true); | 114 CloseInternal(true); |
| 115 } | 115 } |
| 116 | 116 |
| 117 void PepperSession::CreateStreamChannel( | 117 void PepperSession::CreateStreamChannel( |
| 118 const std::string& name, | 118 const std::string& name, |
| 119 const StreamChannelCallback& callback) { | 119 const StreamChannelCallback& callback) { |
| 120 DCHECK(!channels_[name]); | 120 DCHECK(!channels_[name]); |
| 121 | 121 |
| 122 scoped_ptr<ChannelAuthenticator> channel_authenticator = | 122 scoped_ptr<ChannelAuthenticator> channel_authenticator = |
| 123 authenticator_->CreateChannelAuthenticator(); | 123 authenticator_->CreateChannelAuthenticator(); |
| 124 PepperStreamChannel* channel = new PepperStreamChannel( | 124 scoped_ptr<StreamTransport> channel = |
| 125 this, name, callback); | 125 session_manager_->transport_factory_->CreateStreamTransport(); |
| 126 channels_[name] = channel; | 126 channel->Initialize(name, session_manager_->transport_config_, |
| 127 channel->Connect(session_manager_->pp_instance_, | 127 this, channel_authenticator.Pass()); |
| 128 session_manager_->transport_config_, | 128 channel->Connect(callback); |
| 129 channel_authenticator.Pass()); | 129 channels_[name] = channel.release(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 void PepperSession::CreateDatagramChannel( | 132 void PepperSession::CreateDatagramChannel( |
| 133 const std::string& name, | 133 const std::string& name, |
| 134 const DatagramChannelCallback& callback) { | 134 const DatagramChannelCallback& callback) { |
| 135 // TODO(sergeyu): Implement datagram channel support. | 135 DCHECK(!channels_[name]); |
| 136 NOTREACHED(); | 136 |
| 137 scoped_ptr<ChannelAuthenticator> channel_authenticator = |
| 138 authenticator_->CreateChannelAuthenticator(); |
| 139 scoped_ptr<DatagramTransport> channel = |
| 140 session_manager_->transport_factory_->CreateDatagramTransport(); |
| 141 channel->Initialize(name, session_manager_->transport_config_, |
| 142 this, channel_authenticator.Pass()); |
| 143 channel->Connect(callback); |
| 144 channels_[name] = channel.release(); |
| 137 } | 145 } |
| 138 | 146 |
| 139 void PepperSession::CancelChannelCreation(const std::string& name) { | 147 void PepperSession::CancelChannelCreation(const std::string& name) { |
| 140 ChannelsMap::iterator it = channels_.find(name); | 148 ChannelsMap::iterator it = channels_.find(name); |
| 141 if (it != channels_.end() && !it->second->is_connected()) { | 149 if (it != channels_.end() && !it->second->is_connected()) { |
| 142 delete it->second; | 150 delete it->second; |
| 143 DCHECK(!channels_[name]); | 151 DCHECK(!channels_[name]); |
| 144 } | 152 } |
| 145 } | 153 } |
| 146 | 154 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 173 JingleMessage message(peer_jid_, JingleMessage::SESSION_TERMINATE, | 181 JingleMessage message(peer_jid_, JingleMessage::SESSION_TERMINATE, |
| 174 session_id_); | 182 session_id_); |
| 175 scoped_ptr<IqRequest> terminate_request( | 183 scoped_ptr<IqRequest> terminate_request( |
| 176 session_manager_->iq_sender()->SendIq( | 184 session_manager_->iq_sender()->SendIq( |
| 177 message.ToXml(), IqSender::ReplyCallback())); | 185 message.ToXml(), IqSender::ReplyCallback())); |
| 178 } | 186 } |
| 179 | 187 |
| 180 CloseInternal(false); | 188 CloseInternal(false); |
| 181 } | 189 } |
| 182 | 190 |
| 191 void PepperSession::OnChannelCandidate(Transport* channel, |
| 192 const cricket::Candidate& candidate) { |
| 193 pending_candidates_.push_back(candidate); |
| 194 |
| 195 if (!transport_infos_timer_.IsRunning()) { |
| 196 // Delay sending the new candidates in case we get more candidates |
| 197 // that we can send in one message. |
| 198 transport_infos_timer_.Start( |
| 199 FROM_HERE, base::TimeDelta::FromMilliseconds(kTransportInfoSendDelayMs), |
| 200 this, &PepperSession::SendTransportInfo); |
| 201 } |
| 202 } |
| 203 |
| 204 void PepperSession::OnChannelDeleted(Transport* channel) { |
| 205 ChannelsMap::iterator it = channels_.find(channel->name()); |
| 206 DCHECK_EQ(it->second, channel); |
| 207 channels_.erase(it); |
| 208 } |
| 209 |
| 183 void PepperSession::OnIncomingMessage(const JingleMessage& message, | 210 void PepperSession::OnIncomingMessage(const JingleMessage& message, |
| 184 JingleMessageReply* reply) { | 211 JingleMessageReply* reply) { |
| 185 DCHECK(CalledOnValidThread()); | 212 DCHECK(CalledOnValidThread()); |
| 186 | 213 |
| 187 if (message.from != peer_jid_) { | 214 if (message.from != peer_jid_) { |
| 188 // Ignore messages received from a different Jid. | 215 // Ignore messages received from a different Jid. |
| 189 *reply = JingleMessageReply(JingleMessageReply::INVALID_SID); | 216 *reply = JingleMessageReply(JingleMessageReply::INVALID_SID); |
| 190 return; | 217 return; |
| 191 } | 218 } |
| 192 | 219 |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 void PepperSession::OnSessionInfoResponse(const buzz::XmlElement* response) { | 394 void PepperSession::OnSessionInfoResponse(const buzz::XmlElement* response) { |
| 368 const std::string& type = response->Attr(buzz::QName("", "type")); | 395 const std::string& type = response->Attr(buzz::QName("", "type")); |
| 369 if (type != "result") { | 396 if (type != "result") { |
| 370 LOG(ERROR) << "Received error in response to session-info message: \"" | 397 LOG(ERROR) << "Received error in response to session-info message: \"" |
| 371 << response->Str() | 398 << response->Str() |
| 372 << "\". Terminating the session."; | 399 << "\". Terminating the session."; |
| 373 OnError(INCOMPATIBLE_PROTOCOL); | 400 OnError(INCOMPATIBLE_PROTOCOL); |
| 374 } | 401 } |
| 375 } | 402 } |
| 376 | 403 |
| 377 void PepperSession::AddLocalCandidate(const cricket::Candidate& candidate) { | |
| 378 pending_candidates_.push_back(candidate); | |
| 379 | |
| 380 if (!transport_infos_timer_.IsRunning()) { | |
| 381 // Delay sending the new candidates in case we get more candidates | |
| 382 // that we can send in one message. | |
| 383 transport_infos_timer_.Start( | |
| 384 FROM_HERE, base::TimeDelta::FromMilliseconds(kTransportInfoSendDelayMs), | |
| 385 this, &PepperSession::SendTransportInfo); | |
| 386 } | |
| 387 } | |
| 388 | |
| 389 void PepperSession::OnTransportInfoResponse(const buzz::XmlElement* response) { | 404 void PepperSession::OnTransportInfoResponse(const buzz::XmlElement* response) { |
| 390 const std::string& type = response->Attr(buzz::QName("", "type")); | 405 const std::string& type = response->Attr(buzz::QName("", "type")); |
| 391 if (type != "result") { | 406 if (type != "result") { |
| 392 LOG(ERROR) << "Received error in response to session-initiate message: \"" | 407 LOG(ERROR) << "Received error in response to session-initiate message: \"" |
| 393 << response->Str() | 408 << response->Str() |
| 394 << "\". Terminating the session."; | 409 << "\". Terminating the session."; |
| 395 | 410 |
| 396 if (state_ == CONNECTING) { | 411 if (state_ == CONNECTING) { |
| 397 OnError(PEER_IS_OFFLINE); | 412 OnError(PEER_IS_OFFLINE); |
| 398 } else { | 413 } else { |
| 399 // Host has disconnected without sending session-terminate message. | 414 // Host has disconnected without sending session-terminate message. |
| 400 CloseInternal(false); | 415 CloseInternal(false); |
| 401 } | 416 } |
| 402 } | 417 } |
| 403 } | 418 } |
| 404 | 419 |
| 405 void PepperSession::OnDeleteChannel(PepperChannel* channel) { | |
| 406 ChannelsMap::iterator it = channels_.find(channel->name()); | |
| 407 DCHECK_EQ(it->second, channel); | |
| 408 channels_.erase(it); | |
| 409 } | |
| 410 | |
| 411 void PepperSession::SendTransportInfo() { | 420 void PepperSession::SendTransportInfo() { |
| 412 JingleMessage message(peer_jid_, JingleMessage::TRANSPORT_INFO, session_id_); | 421 JingleMessage message(peer_jid_, JingleMessage::TRANSPORT_INFO, session_id_); |
| 413 message.candidates.swap(pending_candidates_); | 422 message.candidates.swap(pending_candidates_); |
| 414 transport_info_request_.reset(session_manager_->iq_sender()->SendIq( | 423 transport_info_request_.reset(session_manager_->iq_sender()->SendIq( |
| 415 message.ToXml(), base::Bind( | 424 message.ToXml(), base::Bind( |
| 416 &PepperSession::OnTransportInfoResponse, | 425 &PepperSession::OnTransportInfoResponse, |
| 417 base::Unretained(this)))); | 426 base::Unretained(this)))); |
| 418 } | 427 } |
| 419 | 428 |
| 420 | 429 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 437 DCHECK_NE(state_, FAILED); | 446 DCHECK_NE(state_, FAILED); |
| 438 | 447 |
| 439 state_ = new_state; | 448 state_ = new_state; |
| 440 if (!state_change_callback_.is_null()) | 449 if (!state_change_callback_.is_null()) |
| 441 state_change_callback_.Run(new_state); | 450 state_change_callback_.Run(new_state); |
| 442 } | 451 } |
| 443 } | 452 } |
| 444 | 453 |
| 445 } // namespace protocol | 454 } // namespace protocol |
| 446 } // namespace remoting | 455 } // namespace remoting |
| OLD | NEW |