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/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/task.h" | 11 #include "base/task.h" |
12 #include "remoting/base/constants.h" | 12 #include "remoting/base/constants.h" |
13 #include "remoting/jingle_glue/http_port_allocator.h" | 13 #include "remoting/jingle_glue/http_port_allocator.h" |
14 #include "remoting/jingle_glue/jingle_info_request.h" | 14 #include "remoting/jingle_glue/jingle_info_request.h" |
15 #include "remoting/jingle_glue/jingle_signaling_connector.h" | 15 #include "remoting/jingle_glue/jingle_signaling_connector.h" |
16 #include "remoting/jingle_glue/signal_strategy.h" | 16 #include "remoting/jingle_glue/signal_strategy.h" |
17 #include "third_party/libjingle/source/talk/base/basicpacketsocketfactory.h" | 17 #include "third_party/libjingle/source/talk/base/basicpacketsocketfactory.h" |
18 #include "third_party/libjingle/source/talk/p2p/base/sessionmanager.h" | 18 #include "third_party/libjingle/source/talk/p2p/base/sessionmanager.h" |
19 #include "third_party/libjingle/source/talk/p2p/base/transport.h" | 19 #include "third_party/libjingle/source/talk/p2p/base/transport.h" |
20 #include "third_party/libjingle/source/talk/p2p/base/constants.h" | 20 #include "third_party/libjingle/source/talk/p2p/base/constants.h" |
21 #include "third_party/libjingle/source/talk/p2p/base/transport.h" | 21 #include "third_party/libjingle/source/talk/p2p/base/transport.h" |
22 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 22 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
23 | 23 |
24 using buzz::XmlElement; | 24 using buzz::XmlElement; |
25 | 25 |
26 namespace remoting { | 26 namespace remoting { |
27 namespace protocol { | 27 namespace protocol { |
28 | 28 |
| 29 // static |
| 30 JingleSessionManager* JingleSessionManager::CreateNotSandboxed() { |
| 31 return new JingleSessionManager(NULL, NULL, NULL); |
| 32 } |
| 33 |
| 34 // static |
| 35 JingleSessionManager* JingleSessionManager::CreateSandboxed( |
| 36 talk_base::NetworkManager* network_manager, |
| 37 talk_base::PacketSocketFactory* socket_factory, |
| 38 PortAllocatorSessionFactory* port_allocator_session_factory) { |
| 39 return new JingleSessionManager(network_manager, socket_factory, |
| 40 port_allocator_session_factory); |
| 41 } |
| 42 |
29 JingleSessionManager::JingleSessionManager( | 43 JingleSessionManager::JingleSessionManager( |
30 talk_base::NetworkManager* network_manager, | 44 talk_base::NetworkManager* network_manager, |
31 talk_base::PacketSocketFactory* socket_factory, | 45 talk_base::PacketSocketFactory* socket_factory, |
32 PortAllocatorSessionFactory* port_allocator_session_factory) | 46 PortAllocatorSessionFactory* port_allocator_session_factory) |
33 : network_manager_(network_manager), | 47 : network_manager_(network_manager), |
34 socket_factory_(socket_factory), | 48 socket_factory_(socket_factory), |
35 port_allocator_session_factory_(port_allocator_session_factory), | 49 port_allocator_session_factory_(port_allocator_session_factory), |
36 signal_strategy_(NULL), | 50 signal_strategy_(NULL), |
37 enable_nat_traversing_(false), | 51 enable_nat_traversing_(false), |
38 allow_local_ips_(false), | 52 allow_local_ips_(false), |
39 http_port_allocator_(NULL), | 53 http_port_allocator_(NULL), |
40 closed_(false), | 54 closed_(false), |
41 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { | 55 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { |
42 } | 56 } |
43 | 57 |
44 JingleSessionManager::~JingleSessionManager() { | 58 JingleSessionManager::~JingleSessionManager() { |
45 DCHECK(closed_); | 59 Close(); |
46 } | 60 } |
47 | 61 |
48 void JingleSessionManager::Init( | 62 void JingleSessionManager::Init( |
49 const std::string& local_jid, | 63 const std::string& local_jid, |
50 SignalStrategy* signal_strategy, | 64 SignalStrategy* signal_strategy, |
51 IncomingSessionCallback* incoming_session_callback, | 65 IncomingSessionCallback* incoming_session_callback, |
52 crypto::RSAPrivateKey* private_key, | 66 crypto::RSAPrivateKey* private_key, |
53 scoped_refptr<net::X509Certificate> certificate) { | 67 scoped_refptr<net::X509Certificate> certificate) { |
54 DCHECK(CalledOnValidThread()); | 68 DCHECK(CalledOnValidThread()); |
55 | 69 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 CandidateSessionConfig* candidate_config, | 134 CandidateSessionConfig* candidate_config, |
121 Session::StateChangeCallback* state_change_callback) { | 135 Session::StateChangeCallback* state_change_callback) { |
122 DCHECK(CalledOnValidThread()); | 136 DCHECK(CalledOnValidThread()); |
123 | 137 |
124 // Can be called from any thread. | 138 // Can be called from any thread. |
125 JingleSession* jingle_session = | 139 JingleSession* jingle_session = |
126 JingleSession::CreateClientSession(this, host_public_key); | 140 JingleSession::CreateClientSession(this, host_public_key); |
127 jingle_session->set_candidate_config(candidate_config); | 141 jingle_session->set_candidate_config(candidate_config); |
128 jingle_session->set_receiver_token(receiver_token); | 142 jingle_session->set_receiver_token(receiver_token); |
129 | 143 |
130 // TODO(sergeyu): Does this need to be asynchronous? | |
131 MessageLoop::current()->PostTask(FROM_HERE, task_factory_.NewRunnableMethod( | |
132 &JingleSessionManager::DoConnect, jingle_session, host_jid, | |
133 host_public_key, receiver_token, state_change_callback)); | |
134 return jingle_session; | |
135 } | |
136 | |
137 void JingleSessionManager::DoConnect( | |
138 JingleSession* jingle_session, | |
139 const std::string& host_jid, | |
140 const std::string& host_public_key, | |
141 const std::string& receiver_token, | |
142 Session::StateChangeCallback* state_change_callback) { | |
143 DCHECK(CalledOnValidThread()); | |
144 | |
145 cricket::Session* cricket_session = cricket_session_manager_->CreateSession( | 144 cricket::Session* cricket_session = cricket_session_manager_->CreateSession( |
146 local_jid_, kChromotingXmlNamespace); | 145 local_jid_, kChromotingXmlNamespace); |
147 | 146 |
148 // Initialize connection object before we send initiate stanza. | 147 // Initialize connection object before we send initiate stanza. |
149 jingle_session->SetStateChangeCallback(state_change_callback); | 148 jingle_session->SetStateChangeCallback(state_change_callback); |
150 jingle_session->Init(cricket_session); | 149 jingle_session->Init(cricket_session); |
151 sessions_.push_back(jingle_session); | 150 sessions_.push_back(jingle_session); |
152 | 151 |
153 cricket_session->Initiate(host_jid, CreateClientSessionDescription( | 152 cricket_session->Initiate(host_jid, CreateClientSessionDescription( |
154 jingle_session->candidate_config()->Clone(), receiver_token, | 153 jingle_session->candidate_config()->Clone(), receiver_token, |
155 jingle_session->GetEncryptedMasterKey())); | 154 jingle_session->GetEncryptedMasterKey())); |
| 155 |
| 156 return jingle_session; |
156 } | 157 } |
157 | 158 |
158 void JingleSessionManager::OnSessionCreate( | 159 void JingleSessionManager::OnSessionCreate( |
159 cricket::Session* cricket_session, bool incoming) { | 160 cricket::Session* cricket_session, bool incoming) { |
160 DCHECK(CalledOnValidThread()); | 161 DCHECK(CalledOnValidThread()); |
161 | 162 |
162 // Allow local connections if neccessary. | 163 // Allow local connections if neccessary. |
163 cricket_session->set_allow_local_ips(allow_local_ips_); | 164 cricket_session->set_allow_local_ips(allow_local_ips_); |
164 | 165 |
165 // If this is an outcoming session the session object is already created. | 166 // If this is an outcoming session the session object is already created. |
(...skipping 13 matching lines...) Expand all Loading... |
179 | 180 |
180 std::list<JingleSession*>::iterator it; | 181 std::list<JingleSession*>::iterator it; |
181 for (it = sessions_.begin(); it != sessions_.end(); ++it) { | 182 for (it = sessions_.begin(); it != sessions_.end(); ++it) { |
182 if ((*it)->HasSession(cricket_session)) { | 183 if ((*it)->HasSession(cricket_session)) { |
183 (*it)->ReleaseSession(); | 184 (*it)->ReleaseSession(); |
184 return; | 185 return; |
185 } | 186 } |
186 } | 187 } |
187 } | 188 } |
188 | 189 |
189 void JingleSessionManager::AcceptConnection( | 190 bool JingleSessionManager::AcceptConnection( |
190 JingleSession* jingle_session, | 191 JingleSession* jingle_session, |
191 cricket::Session* cricket_session) { | 192 cricket::Session* cricket_session) { |
192 DCHECK(CalledOnValidThread()); | 193 DCHECK(CalledOnValidThread()); |
193 | 194 |
194 // Reject connection if we are closed. | 195 // Reject connection if we are closed. |
195 if (closed_) { | 196 if (closed_) { |
196 cricket_session->Reject(cricket::STR_TERMINATE_DECLINE); | 197 cricket_session->Reject(cricket::STR_TERMINATE_DECLINE); |
197 return; | 198 return false; |
198 } | 199 } |
199 | 200 |
200 const cricket::SessionDescription* session_description = | 201 const cricket::SessionDescription* session_description = |
201 cricket_session->remote_description(); | 202 cricket_session->remote_description(); |
202 const cricket::ContentInfo* content = | 203 const cricket::ContentInfo* content = |
203 session_description->FirstContentByType(kChromotingXmlNamespace); | 204 session_description->FirstContentByType(kChromotingXmlNamespace); |
204 | 205 |
205 CHECK(content); | 206 CHECK(content); |
206 | 207 |
207 const ContentDescription* content_description = | 208 const ContentDescription* content_description = |
(...skipping 15 matching lines...) Expand all Loading... |
223 CandidateSessionConfig* candidate_config = | 224 CandidateSessionConfig* candidate_config = |
224 CandidateSessionConfig::CreateFrom(jingle_session->config()); | 225 CandidateSessionConfig::CreateFrom(jingle_session->config()); |
225 cricket_session->Accept( | 226 cricket_session->Accept( |
226 CreateHostSessionDescription(candidate_config, | 227 CreateHostSessionDescription(candidate_config, |
227 jingle_session->local_certificate())); | 228 jingle_session->local_certificate())); |
228 break; | 229 break; |
229 } | 230 } |
230 | 231 |
231 case protocol::SessionManager::INCOMPATIBLE: { | 232 case protocol::SessionManager::INCOMPATIBLE: { |
232 cricket_session->Reject(cricket::STR_TERMINATE_INCOMPATIBLE_PARAMETERS); | 233 cricket_session->Reject(cricket::STR_TERMINATE_INCOMPATIBLE_PARAMETERS); |
233 jingle_session->ReleaseSession(); | 234 return false; |
234 jingle_session->Close(); | |
235 delete jingle_session; | |
236 break; | |
237 } | 235 } |
238 | 236 |
239 case protocol::SessionManager::DECLINE: { | 237 case protocol::SessionManager::DECLINE: { |
240 cricket_session->Reject(cricket::STR_TERMINATE_DECLINE); | 238 cricket_session->Reject(cricket::STR_TERMINATE_DECLINE); |
241 jingle_session->ReleaseSession(); | 239 return false; |
242 jingle_session->Close(); | |
243 delete jingle_session; | |
244 break; | |
245 } | 240 } |
246 | 241 |
247 default: { | 242 default: { |
248 NOTREACHED(); | 243 NOTREACHED(); |
249 } | 244 } |
250 } | 245 } |
| 246 |
| 247 return true; |
251 } | 248 } |
252 | 249 |
253 void JingleSessionManager::SessionDestroyed(JingleSession* jingle_session) { | 250 void JingleSessionManager::SessionDestroyed(JingleSession* jingle_session) { |
254 std::list<JingleSession*>::iterator it = | 251 std::list<JingleSession*>::iterator it = |
255 std::find(sessions_.begin(), sessions_.end(), jingle_session); | 252 std::find(sessions_.begin(), sessions_.end(), jingle_session); |
256 CHECK(it != sessions_.end()); | 253 CHECK(it != sessions_.end()); |
257 cricket::Session* cricket_session = jingle_session->ReleaseSession(); | 254 cricket::Session* cricket_session = jingle_session->ReleaseSession(); |
258 cricket_session_manager_->DestroySession(cricket_session); | 255 cricket_session_manager_->DestroySession(cricket_session); |
259 sessions_.erase(it); | 256 sessions_.erase(it); |
260 } | 257 } |
261 | 258 |
262 void JingleSessionManager::OnJingleInfo( | 259 void JingleSessionManager::OnJingleInfo( |
263 const std::string& token, | 260 const std::string& token, |
264 const std::vector<std::string>& relay_hosts, | 261 const std::vector<std::string>& relay_hosts, |
265 const std::vector<talk_base::SocketAddress>& stun_hosts) { | 262 const std::vector<talk_base::SocketAddress>& stun_hosts) { |
266 DCHECK(CalledOnValidThread()); | 263 DCHECK(CalledOnValidThread()); |
267 | 264 |
268 if (port_allocator_.get()) { | 265 if (http_port_allocator_) { |
269 // TODO(ajwong): Avoid string processing if log-level is low. | 266 // TODO(ajwong): Avoid string processing if log-level is low. |
270 std::string stun_servers; | 267 std::string stun_servers; |
271 for (size_t i = 0; i < stun_hosts.size(); ++i) { | 268 for (size_t i = 0; i < stun_hosts.size(); ++i) { |
272 stun_servers += stun_hosts[i].ToString() + "; "; | 269 stun_servers += stun_hosts[i].ToString() + "; "; |
273 } | 270 } |
274 LOG(INFO) << "Configuring with relay token: " << token | 271 LOG(INFO) << "Configuring with relay token: " << token |
275 << ", relays: " << JoinString(relay_hosts, ';') | 272 << ", relays: " << JoinString(relay_hosts, ';') |
276 << ", stun: " << stun_servers; | 273 << ", stun: " << stun_servers; |
277 http_port_allocator_->SetRelayToken(token); | 274 http_port_allocator_->SetRelayToken(token); |
278 http_port_allocator_->SetStunHosts(stun_hosts); | 275 http_port_allocator_->SetStunHosts(stun_hosts); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 scoped_refptr<net::X509Certificate> certificate) { | 331 scoped_refptr<net::X509Certificate> certificate) { |
335 cricket::SessionDescription* desc = new cricket::SessionDescription(); | 332 cricket::SessionDescription* desc = new cricket::SessionDescription(); |
336 desc->AddContent( | 333 desc->AddContent( |
337 JingleSession::kChromotingContentName, kChromotingXmlNamespace, | 334 JingleSession::kChromotingContentName, kChromotingXmlNamespace, |
338 new ContentDescription(config, "", "", certificate)); | 335 new ContentDescription(config, "", "", certificate)); |
339 return desc; | 336 return desc; |
340 } | 337 } |
341 | 338 |
342 } // namespace protocol | 339 } // namespace protocol |
343 } // namespace remoting | 340 } // namespace remoting |
OLD | NEW |