| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "remoting/base/constants.h" | 14 #include "remoting/base/constants.h" |
| 15 #include "remoting/protocol/authenticator.h" | 15 #include "remoting/protocol/authenticator.h" |
| 16 #include "remoting/protocol/channel_authenticator.h" | 16 #include "remoting/protocol/channel_authenticator.h" |
| 17 #include "remoting/protocol/channel_multiplexer.h" | 17 #include "remoting/protocol/channel_multiplexer.h" |
| 18 #include "remoting/protocol/content_description.h" | 18 #include "remoting/protocol/content_description.h" |
| 19 #include "remoting/protocol/jingle_messages.h" | 19 #include "remoting/protocol/jingle_messages.h" |
| 20 #include "remoting/protocol/jingle_session_manager.h" | 20 #include "remoting/protocol/jingle_session_manager.h" |
| 21 #include "remoting/protocol/pseudotcp_channel_factory.h" | |
| 22 #include "remoting/protocol/secure_channel_factory.h" | |
| 23 #include "remoting/protocol/session_config.h" | 21 #include "remoting/protocol/session_config.h" |
| 24 #include "remoting/protocol/stream_channel_factory.h" | |
| 25 #include "remoting/signaling/iq_sender.h" | 22 #include "remoting/signaling/iq_sender.h" |
| 26 #include "third_party/libjingle/source/talk/p2p/base/candidate.h" | 23 #include "third_party/libjingle/source/talk/p2p/base/candidate.h" |
| 27 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 24 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 28 | 25 |
| 29 using buzz::XmlElement; | 26 using buzz::XmlElement; |
| 30 | 27 |
| 31 namespace remoting { | 28 namespace remoting { |
| 32 namespace protocol { | 29 namespace protocol { |
| 33 | 30 |
| 34 namespace { | 31 namespace { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 config_is_set_(false), | 74 config_is_set_(false), |
| 78 weak_factory_(this) { | 75 weak_factory_(this) { |
| 79 } | 76 } |
| 80 | 77 |
| 81 JingleSession::~JingleSession() { | 78 JingleSession::~JingleSession() { |
| 82 channel_multiplexer_.reset(); | 79 channel_multiplexer_.reset(); |
| 83 STLDeleteContainerPointers(pending_requests_.begin(), | 80 STLDeleteContainerPointers(pending_requests_.begin(), |
| 84 pending_requests_.end()); | 81 pending_requests_.end()); |
| 85 STLDeleteContainerPointers(transport_info_requests_.begin(), | 82 STLDeleteContainerPointers(transport_info_requests_.begin(), |
| 86 transport_info_requests_.end()); | 83 transport_info_requests_.end()); |
| 87 DCHECK(channels_.empty()); | 84 STLDeleteContainerPairSecondPointers(channels_.begin(), channels_.end()); |
| 88 session_manager_->SessionDestroyed(this); | 85 session_manager_->SessionDestroyed(this); |
| 89 } | 86 } |
| 90 | 87 |
| 91 void JingleSession::SetEventHandler(Session::EventHandler* event_handler) { | 88 void JingleSession::SetEventHandler(Session::EventHandler* event_handler) { |
| 92 DCHECK(CalledOnValidThread()); | 89 DCHECK(CalledOnValidThread()); |
| 93 DCHECK(event_handler); | 90 DCHECK(event_handler); |
| 94 event_handler_ = event_handler; | 91 event_handler_ = event_handler; |
| 95 } | 92 } |
| 96 | 93 |
| 97 ErrorCode JingleSession::error() { | 94 ErrorCode JingleSession::error() { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 | 180 |
| 184 message.description.reset( | 181 message.description.reset( |
| 185 new ContentDescription(CandidateSessionConfig::CreateFrom(config_), | 182 new ContentDescription(CandidateSessionConfig::CreateFrom(config_), |
| 186 auth_message.Pass())); | 183 auth_message.Pass())); |
| 187 SendMessage(message); | 184 SendMessage(message); |
| 188 | 185 |
| 189 // Update state. | 186 // Update state. |
| 190 SetState(CONNECTED); | 187 SetState(CONNECTED); |
| 191 | 188 |
| 192 if (authenticator_->state() == Authenticator::ACCEPTED) { | 189 if (authenticator_->state() == Authenticator::ACCEPTED) { |
| 193 OnAuthenticated(); | 190 SetState(AUTHENTICATED); |
| 194 } else { | 191 } else { |
| 195 DCHECK_EQ(authenticator_->state(), Authenticator::WAITING_MESSAGE); | 192 DCHECK_EQ(authenticator_->state(), Authenticator::WAITING_MESSAGE); |
| 196 if (authenticator_->started()) { | 193 if (authenticator_->started()) { |
| 197 SetState(AUTHENTICATING); | 194 SetState(AUTHENTICATING); |
| 198 } | 195 } |
| 199 } | 196 } |
| 200 } | 197 } |
| 201 | 198 |
| 202 const std::string& JingleSession::jid() { | 199 const std::string& JingleSession::jid() { |
| 203 DCHECK(CalledOnValidThread()); | 200 DCHECK(CalledOnValidThread()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 214 return config_; | 211 return config_; |
| 215 } | 212 } |
| 216 | 213 |
| 217 void JingleSession::set_config(const SessionConfig& config) { | 214 void JingleSession::set_config(const SessionConfig& config) { |
| 218 DCHECK(CalledOnValidThread()); | 215 DCHECK(CalledOnValidThread()); |
| 219 DCHECK(!config_is_set_); | 216 DCHECK(!config_is_set_); |
| 220 config_ = config; | 217 config_ = config; |
| 221 config_is_set_ = true; | 218 config_is_set_ = true; |
| 222 } | 219 } |
| 223 | 220 |
| 224 StreamChannelFactory* JingleSession::GetTransportChannelFactory() { | 221 ChannelFactory* JingleSession::GetTransportChannelFactory() { |
| 225 DCHECK(CalledOnValidThread()); | 222 DCHECK(CalledOnValidThread()); |
| 226 return secure_channel_factory_.get(); | 223 return this; |
| 227 } | 224 } |
| 228 | 225 |
| 229 StreamChannelFactory* JingleSession::GetMultiplexedChannelFactory() { | 226 ChannelFactory* JingleSession::GetMultiplexedChannelFactory() { |
| 230 DCHECK(CalledOnValidThread()); | 227 DCHECK(CalledOnValidThread()); |
| 231 if (!channel_multiplexer_.get()) { | 228 if (!channel_multiplexer_.get()) |
| 232 channel_multiplexer_.reset( | 229 channel_multiplexer_.reset(new ChannelMultiplexer(this, kMuxChannelName)); |
| 233 new ChannelMultiplexer(GetTransportChannelFactory(), kMuxChannelName)); | |
| 234 } | |
| 235 return channel_multiplexer_.get(); | 230 return channel_multiplexer_.get(); |
| 236 } | 231 } |
| 237 | 232 |
| 238 void JingleSession::Close() { | 233 void JingleSession::Close() { |
| 239 DCHECK(CalledOnValidThread()); | 234 DCHECK(CalledOnValidThread()); |
| 240 | 235 |
| 241 CloseInternal(OK); | 236 CloseInternal(OK); |
| 242 } | 237 } |
| 243 | 238 |
| 244 void JingleSession::AddPendingRemoteCandidates(Transport* channel, | 239 void JingleSession::AddPendingRemoteCandidates(Transport* channel, |
| 245 const std::string& name) { | 240 const std::string& name) { |
| 246 std::list<JingleMessage::NamedCandidate>::iterator it = | 241 std::list<JingleMessage::NamedCandidate>::iterator it = |
| 247 pending_remote_candidates_.begin(); | 242 pending_remote_candidates_.begin(); |
| 248 while(it != pending_remote_candidates_.end()) { | 243 while(it != pending_remote_candidates_.end()) { |
| 249 if (it->name == name) { | 244 if (it->name == name) { |
| 250 channel->AddRemoteCandidate(it->candidate); | 245 channel->AddRemoteCandidate(it->candidate); |
| 251 it = pending_remote_candidates_.erase(it); | 246 it = pending_remote_candidates_.erase(it); |
| 252 } else { | 247 } else { |
| 253 ++it; | 248 ++it; |
| 254 } | 249 } |
| 255 } | 250 } |
| 256 } | 251 } |
| 257 | 252 |
| 258 void JingleSession::CreateChannel(const std::string& name, | 253 void JingleSession::CreateChannel(const std::string& name, |
| 259 const ChannelCreatedCallback& callback) { | 254 const ChannelCreatedCallback& callback) { |
| 260 DCHECK(!channels_[name]); | 255 DCHECK(!channels_[name]); |
| 261 | 256 |
| 262 scoped_ptr<Transport> channel = | 257 scoped_ptr<ChannelAuthenticator> channel_authenticator = |
| 263 session_manager_->transport_factory_->CreateTransport(); | 258 authenticator_->CreateChannelAuthenticator(); |
| 264 channel->Connect(name, this, callback); | 259 scoped_ptr<StreamTransport> channel = |
| 260 session_manager_->transport_factory_->CreateStreamTransport(); |
| 261 channel->Initialize(name, this, channel_authenticator.Pass()); |
| 262 channel->Connect(callback); |
| 265 AddPendingRemoteCandidates(channel.get(), name); | 263 AddPendingRemoteCandidates(channel.get(), name); |
| 266 channels_[name] = channel.release(); | 264 channels_[name] = channel.release(); |
| 267 } | 265 } |
| 268 | 266 |
| 269 void JingleSession::CancelChannelCreation(const std::string& name) { | 267 void JingleSession::CancelChannelCreation(const std::string& name) { |
| 270 ChannelsMap::iterator it = channels_.find(name); | 268 ChannelsMap::iterator it = channels_.find(name); |
| 271 if (it != channels_.end()) { | 269 if (it != channels_.end() && !it->second->is_connected()) { |
| 272 DCHECK(!it->second->is_connected()); | |
| 273 delete it->second; | 270 delete it->second; |
| 274 DCHECK(!channels_[name]); | 271 DCHECK(!channels_[name]); |
| 275 } | 272 } |
| 276 } | 273 } |
| 277 | 274 |
| 278 void JingleSession::OnTransportCandidate(Transport* transport, | 275 void JingleSession::OnTransportCandidate(Transport* transport, |
| 279 const cricket::Candidate& candidate) { | 276 const cricket::Candidate& candidate) { |
| 280 pending_candidates_.push_back(JingleMessage::NamedCandidate( | 277 pending_candidates_.push_back(JingleMessage::NamedCandidate( |
| 281 transport->name(), candidate)); | 278 transport->name(), candidate)); |
| 282 | 279 |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 base::Bind(&JingleSession::ContinueAuthenticationStep, | 591 base::Bind(&JingleSession::ContinueAuthenticationStep, |
| 595 weak_factory_.GetWeakPtr())); | 592 weak_factory_.GetWeakPtr())); |
| 596 | 593 |
| 597 if (authenticator_->started()) { | 594 if (authenticator_->started()) { |
| 598 SetState(AUTHENTICATING); | 595 SetState(AUTHENTICATING); |
| 599 } | 596 } |
| 600 } | 597 } |
| 601 | 598 |
| 602 void JingleSession::ContinueAuthenticationStep() { | 599 void JingleSession::ContinueAuthenticationStep() { |
| 603 if (authenticator_->state() == Authenticator::ACCEPTED) { | 600 if (authenticator_->state() == Authenticator::ACCEPTED) { |
| 604 OnAuthenticated(); | 601 SetState(AUTHENTICATED); |
| 605 } else if (authenticator_->state() == Authenticator::REJECTED) { | 602 } else if (authenticator_->state() == Authenticator::REJECTED) { |
| 606 CloseInternal(AuthRejectionReasonToErrorCode( | 603 CloseInternal(AuthRejectionReasonToErrorCode( |
| 607 authenticator_->rejection_reason())); | 604 authenticator_->rejection_reason())); |
| 608 } | 605 } |
| 609 } | 606 } |
| 610 | 607 |
| 611 void JingleSession::OnAuthenticated() { | |
| 612 pseudotcp_channel_factory_.reset(new PseudoTcpChannelFactory(this)); | |
| 613 secure_channel_factory_.reset( | |
| 614 new SecureChannelFactory(pseudotcp_channel_factory_.get(), | |
| 615 authenticator_.get())); | |
| 616 | |
| 617 SetState(AUTHENTICATED); | |
| 618 } | |
| 619 | |
| 620 void JingleSession::CloseInternal(ErrorCode error) { | 608 void JingleSession::CloseInternal(ErrorCode error) { |
| 621 DCHECK(CalledOnValidThread()); | 609 DCHECK(CalledOnValidThread()); |
| 622 | 610 |
| 623 if (is_session_active()) { | 611 if (is_session_active()) { |
| 624 // Send session-terminate message with the appropriate error code. | 612 // Send session-terminate message with the appropriate error code. |
| 625 JingleMessage::Reason reason; | 613 JingleMessage::Reason reason; |
| 626 switch (error) { | 614 switch (error) { |
| 627 case OK: | 615 case OK: |
| 628 reason = JingleMessage::SUCCESS; | 616 reason = JingleMessage::SUCCESS; |
| 629 break; | 617 break; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 } | 659 } |
| 672 } | 660 } |
| 673 | 661 |
| 674 bool JingleSession::is_session_active() { | 662 bool JingleSession::is_session_active() { |
| 675 return state_ == CONNECTING || state_ == ACCEPTING || state_ == CONNECTED || | 663 return state_ == CONNECTING || state_ == ACCEPTING || state_ == CONNECTED || |
| 676 state_ == AUTHENTICATING || state_ == AUTHENTICATED; | 664 state_ == AUTHENTICATING || state_ == AUTHENTICATED; |
| 677 } | 665 } |
| 678 | 666 |
| 679 } // namespace protocol | 667 } // namespace protocol |
| 680 } // namespace remoting | 668 } // namespace remoting |
| OLD | NEW |