| 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_manager.h" | 5 #include "remoting/protocol/jingle_session_manager.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 using buzz::XmlElement; | 26 using buzz::XmlElement; |
| 27 | 27 |
| 28 namespace remoting { | 28 namespace remoting { |
| 29 namespace protocol { | 29 namespace protocol { |
| 30 | 30 |
| 31 JingleSessionManager::JingleSessionManager( | 31 JingleSessionManager::JingleSessionManager( |
| 32 base::MessageLoopProxy* message_loop) | 32 base::MessageLoopProxy* message_loop) |
| 33 : message_loop_(message_loop), | 33 : message_loop_(message_loop), |
| 34 signal_strategy_(NULL), | 34 signal_strategy_(NULL), |
| 35 allow_nat_traversal_(false), | 35 allow_nat_traversal_(false), |
| 36 ready_(false), |
| 36 http_port_allocator_(NULL), | 37 http_port_allocator_(NULL), |
| 37 closed_(false), | 38 closed_(false), |
| 38 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { | 39 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { |
| 39 } | 40 } |
| 40 | 41 |
| 41 JingleSessionManager::~JingleSessionManager() { | 42 JingleSessionManager::~JingleSessionManager() { |
| 42 // Session manager can be destroyed only after all sessions are destroyed. | 43 // Session manager can be destroyed only after all sessions are destroyed. |
| 43 DCHECK(sessions_.empty()); | 44 DCHECK(sessions_.empty()); |
| 44 Close(); | 45 Close(); |
| 45 } | 46 } |
| 46 | 47 |
| 47 void JingleSessionManager::Init( | 48 void JingleSessionManager::Init( |
| 48 const std::string& local_jid, | |
| 49 SignalStrategy* signal_strategy, | 49 SignalStrategy* signal_strategy, |
| 50 Listener* listener, | 50 SessionManager::Listener* listener, |
| 51 bool allow_nat_traversal) { | 51 bool allow_nat_traversal) { |
| 52 DCHECK(CalledOnValidThread()); | 52 DCHECK(CalledOnValidThread()); |
| 53 | 53 |
| 54 DCHECK(signal_strategy); | 54 DCHECK(signal_strategy); |
| 55 DCHECK(listener); | 55 DCHECK(listener); |
| 56 | 56 |
| 57 local_jid_ = local_jid; | |
| 58 signal_strategy_ = signal_strategy; | 57 signal_strategy_ = signal_strategy; |
| 59 listener_ = listener; | 58 listener_ = listener; |
| 60 allow_nat_traversal_ = allow_nat_traversal; | 59 allow_nat_traversal_ = allow_nat_traversal; |
| 61 | 60 |
| 61 signal_strategy_->AddListener(this); |
| 62 |
| 62 if (!network_manager_.get()) { | 63 if (!network_manager_.get()) { |
| 63 VLOG(1) << "Creating talk_base::NetworkManager."; | 64 VLOG(1) << "Creating talk_base::NetworkManager."; |
| 64 network_manager_.reset(new talk_base::BasicNetworkManager()); | 65 network_manager_.reset(new talk_base::BasicNetworkManager()); |
| 65 } | 66 } |
| 66 if (!socket_factory_.get()) { | 67 if (!socket_factory_.get()) { |
| 67 VLOG(1) << "Creating talk_base::BasicPacketSocketFactory."; | 68 VLOG(1) << "Creating talk_base::BasicPacketSocketFactory."; |
| 68 socket_factory_.reset(new talk_base::BasicPacketSocketFactory( | 69 socket_factory_.reset(new talk_base::BasicPacketSocketFactory( |
| 69 talk_base::Thread::Current())); | 70 talk_base::Thread::Current())); |
| 70 } | 71 } |
| 71 | 72 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 90 port_allocator_->set_flags(port_allocator_flags); | 91 port_allocator_->set_flags(port_allocator_flags); |
| 91 | 92 |
| 92 // Initialize |cricket_session_manager_|. | 93 // Initialize |cricket_session_manager_|. |
| 93 cricket_session_manager_.reset( | 94 cricket_session_manager_.reset( |
| 94 new cricket::SessionManager(port_allocator_.get())); | 95 new cricket::SessionManager(port_allocator_.get())); |
| 95 cricket_session_manager_->AddClient(kChromotingXmlNamespace, this); | 96 cricket_session_manager_->AddClient(kChromotingXmlNamespace, this); |
| 96 | 97 |
| 97 jingle_signaling_connector_.reset(new JingleSignalingConnector( | 98 jingle_signaling_connector_.reset(new JingleSignalingConnector( |
| 98 signal_strategy_, cricket_session_manager_.get())); | 99 signal_strategy_, cricket_session_manager_.get())); |
| 99 | 100 |
| 100 // If NAT traversal is enabled then we need to request STUN/Relay info. | 101 OnSignalStrategyStateChange(signal_strategy_->GetState()); |
| 101 if (allow_nat_traversal) { | |
| 102 jingle_info_request_.reset(new JingleInfoRequest(signal_strategy_)); | |
| 103 jingle_info_request_->Send(base::Bind( | |
| 104 &JingleSessionManager::OnJingleInfo, base::Unretained(this))); | |
| 105 } else { | |
| 106 listener_->OnSessionManagerInitialized(); | |
| 107 } | |
| 108 } | 102 } |
| 109 | 103 |
| 110 void JingleSessionManager::Close() { | 104 void JingleSessionManager::Close() { |
| 111 DCHECK(CalledOnValidThread()); | 105 DCHECK(CalledOnValidThread()); |
| 112 | 106 |
| 113 if (!closed_) { | 107 if (!closed_) { |
| 114 jingle_info_request_.reset(); | 108 jingle_info_request_.reset(); |
| 115 cricket_session_manager_->RemoveClient(kChromotingXmlNamespace); | 109 cricket_session_manager_->RemoveClient(kChromotingXmlNamespace); |
| 116 jingle_signaling_connector_.reset(); | 110 jingle_signaling_connector_.reset(); |
| 111 signal_strategy_->RemoveListener(this); |
| 117 closed_ = true; | 112 closed_ = true; |
| 118 } | 113 } |
| 119 } | 114 } |
| 120 | 115 |
| 121 void JingleSessionManager::set_authenticator_factory( | 116 void JingleSessionManager::set_authenticator_factory( |
| 122 AuthenticatorFactory* authenticator_factory) { | 117 AuthenticatorFactory* authenticator_factory) { |
| 123 DCHECK(CalledOnValidThread()); | 118 DCHECK(CalledOnValidThread()); |
| 124 authenticator_factory_.reset(authenticator_factory); | 119 authenticator_factory_.reset(authenticator_factory); |
| 125 } | 120 } |
| 126 | 121 |
| 127 Session* JingleSessionManager::Connect( | 122 Session* JingleSessionManager::Connect( |
| 128 const std::string& host_jid, | 123 const std::string& host_jid, |
| 129 Authenticator* authenticator, | 124 Authenticator* authenticator, |
| 130 CandidateSessionConfig* candidate_config, | 125 CandidateSessionConfig* candidate_config, |
| 131 const Session::StateChangeCallback& state_change_callback) { | 126 const Session::StateChangeCallback& state_change_callback) { |
| 132 DCHECK(CalledOnValidThread()); | 127 DCHECK(CalledOnValidThread()); |
| 133 | 128 |
| 134 cricket::Session* cricket_session = cricket_session_manager_->CreateSession( | 129 cricket::Session* cricket_session = cricket_session_manager_->CreateSession( |
| 135 local_jid_, kChromotingXmlNamespace); | 130 signal_strategy_->GetLocalJid(), kChromotingXmlNamespace); |
| 136 cricket_session->set_remote_name(host_jid); | 131 cricket_session->set_remote_name(host_jid); |
| 137 | 132 |
| 138 JingleSession* jingle_session = | 133 JingleSession* jingle_session = |
| 139 new JingleSession(this, cricket_session, authenticator); | 134 new JingleSession(this, cricket_session, authenticator); |
| 140 jingle_session->set_candidate_config(candidate_config); | 135 jingle_session->set_candidate_config(candidate_config); |
| 141 jingle_session->SetStateChangeCallback(state_change_callback); | 136 jingle_session->SetStateChangeCallback(state_change_callback); |
| 142 sessions_.push_back(jingle_session); | 137 sessions_.push_back(jingle_session); |
| 143 | 138 |
| 144 jingle_session->SendSessionInitiate(); | 139 jingle_session->SendSessionInitiate(); |
| 145 | 140 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 165 | 160 |
| 166 std::list<JingleSession*>::iterator it; | 161 std::list<JingleSession*>::iterator it; |
| 167 for (it = sessions_.begin(); it != sessions_.end(); ++it) { | 162 for (it = sessions_.begin(); it != sessions_.end(); ++it) { |
| 168 if ((*it)->HasSession(cricket_session)) { | 163 if ((*it)->HasSession(cricket_session)) { |
| 169 (*it)->ReleaseSession(); | 164 (*it)->ReleaseSession(); |
| 170 return; | 165 return; |
| 171 } | 166 } |
| 172 } | 167 } |
| 173 } | 168 } |
| 174 | 169 |
| 170 void JingleSessionManager::OnSignalStrategyStateChange( |
| 171 SignalStrategy::State state) { |
| 172 if (state == SignalStrategy::CONNECTED) { |
| 173 // If NAT traversal is enabled then we need to request STUN/Relay info. |
| 174 if (allow_nat_traversal_) { |
| 175 jingle_info_request_.reset(new JingleInfoRequest(signal_strategy_)); |
| 176 jingle_info_request_->Send(base::Bind( |
| 177 &JingleSessionManager::OnJingleInfo, base::Unretained(this))); |
| 178 } else if (!ready_) { |
| 179 ready_ = true; |
| 180 listener_->OnSessionManagerReady(); |
| 181 } |
| 182 } |
| 183 } |
| 184 |
| 175 SessionManager::IncomingSessionResponse JingleSessionManager::AcceptConnection( | 185 SessionManager::IncomingSessionResponse JingleSessionManager::AcceptConnection( |
| 176 JingleSession* jingle_session) { | 186 JingleSession* jingle_session) { |
| 177 DCHECK(CalledOnValidThread()); | 187 DCHECK(CalledOnValidThread()); |
| 178 | 188 |
| 179 // Reject connection if we are closed. | 189 // Reject connection if we are closed. |
| 180 if (closed_) | 190 if (closed_) |
| 181 return SessionManager::DECLINE; | 191 return SessionManager::DECLINE; |
| 182 | 192 |
| 183 IncomingSessionResponse response = SessionManager::DECLINE; | 193 IncomingSessionResponse response = SessionManager::DECLINE; |
| 184 listener_->OnIncomingSession(jingle_session, &response); | 194 listener_->OnIncomingSession(jingle_session, &response); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 VLOG(1) << "Configuring with relay token: " << token | 228 VLOG(1) << "Configuring with relay token: " << token |
| 219 << ", relays: " << JoinString(relay_hosts, ';') | 229 << ", relays: " << JoinString(relay_hosts, ';') |
| 220 << ", stun: " << stun_servers; | 230 << ", stun: " << stun_servers; |
| 221 http_port_allocator_->SetRelayToken(token); | 231 http_port_allocator_->SetRelayToken(token); |
| 222 http_port_allocator_->SetStunHosts(stun_hosts); | 232 http_port_allocator_->SetStunHosts(stun_hosts); |
| 223 http_port_allocator_->SetRelayHosts(relay_hosts); | 233 http_port_allocator_->SetRelayHosts(relay_hosts); |
| 224 } else { | 234 } else { |
| 225 LOG(WARNING) << "Jingle info found but no port allocator."; | 235 LOG(WARNING) << "Jingle info found but no port allocator."; |
| 226 } | 236 } |
| 227 | 237 |
| 228 listener_->OnSessionManagerInitialized(); | 238 if (!ready_) { |
| 239 ready_ = true; |
| 240 listener_->OnSessionManagerReady(); |
| 241 } |
| 229 } | 242 } |
| 230 | 243 |
| 231 // Parse content description generated by WriteContent(). | 244 // Parse content description generated by WriteContent(). |
| 232 bool JingleSessionManager::ParseContent( | 245 bool JingleSessionManager::ParseContent( |
| 233 cricket::SignalingProtocol protocol, | 246 cricket::SignalingProtocol protocol, |
| 234 const XmlElement* element, | 247 const XmlElement* element, |
| 235 const cricket::ContentDescription** content, | 248 const cricket::ContentDescription** content, |
| 236 cricket::ParseError* error) { | 249 cricket::ParseError* error) { |
| 237 *content = ContentDescription::ParseXml(element); | 250 *content = ContentDescription::ParseXml(element); |
| 238 return *content != NULL; | 251 return *content != NULL; |
| 239 } | 252 } |
| 240 | 253 |
| 241 bool JingleSessionManager::WriteContent( | 254 bool JingleSessionManager::WriteContent( |
| 242 cricket::SignalingProtocol protocol, | 255 cricket::SignalingProtocol protocol, |
| 243 const cricket::ContentDescription* content, | 256 const cricket::ContentDescription* content, |
| 244 XmlElement** elem, | 257 XmlElement** elem, |
| 245 cricket::WriteError* error) { | 258 cricket::WriteError* error) { |
| 246 const ContentDescription* desc = | 259 const ContentDescription* desc = |
| 247 static_cast<const ContentDescription*>(content); | 260 static_cast<const ContentDescription*>(content); |
| 248 | 261 |
| 249 *elem = desc->ToXml(); | 262 *elem = desc->ToXml(); |
| 250 return true; | 263 return true; |
| 251 } | 264 } |
| 252 | 265 |
| 253 } // namespace protocol | 266 } // namespace protocol |
| 254 } // namespace remoting | 267 } // namespace remoting |
| OLD | NEW |