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_manager.h" | 5 #include "remoting/protocol/jingle_session_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "remoting/jingle_glue/iq_sender.h" | 8 #include "remoting/jingle_glue/iq_sender.h" |
9 #include "remoting/jingle_glue/jingle_info_request.h" | |
10 #include "remoting/jingle_glue/signal_strategy.h" | 9 #include "remoting/jingle_glue/signal_strategy.h" |
11 #include "remoting/protocol/authenticator.h" | 10 #include "remoting/protocol/authenticator.h" |
12 #include "remoting/protocol/content_description.h" | 11 #include "remoting/protocol/content_description.h" |
13 #include "remoting/protocol/jingle_messages.h" | 12 #include "remoting/protocol/jingle_messages.h" |
14 #include "remoting/protocol/jingle_session.h" | 13 #include "remoting/protocol/jingle_session.h" |
15 #include "remoting/protocol/transport.h" | 14 #include "remoting/protocol/transport.h" |
16 #include "remoting/protocol/transport_config.h" | |
17 #include "third_party/libjingle/source/talk/base/socketaddress.h" | 15 #include "third_party/libjingle/source/talk/base/socketaddress.h" |
18 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 16 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
19 | 17 |
20 using buzz::QName; | 18 using buzz::QName; |
21 | 19 |
22 namespace remoting { | 20 namespace remoting { |
23 namespace protocol { | 21 namespace protocol { |
24 | 22 |
25 JingleSessionManager::JingleSessionManager( | 23 JingleSessionManager::JingleSessionManager( |
26 scoped_ptr<TransportFactory> transport_factory, | 24 scoped_ptr<TransportFactory> transport_factory) |
27 bool fetch_stun_relay_config) | |
28 : transport_factory_(transport_factory.Pass()), | 25 : transport_factory_(transport_factory.Pass()), |
29 fetch_stun_relay_config_(fetch_stun_relay_config), | |
30 signal_strategy_(NULL), | 26 signal_strategy_(NULL), |
31 listener_(NULL), | 27 listener_(NULL), |
32 ready_(false) { | 28 ready_(false) { |
33 } | 29 } |
34 | 30 |
35 JingleSessionManager::~JingleSessionManager() { | 31 JingleSessionManager::~JingleSessionManager() { |
36 Close(); | 32 Close(); |
37 } | 33 } |
38 | 34 |
39 void JingleSessionManager::Init( | 35 void JingleSessionManager::Init( |
40 SignalStrategy* signal_strategy, | 36 SignalStrategy* signal_strategy, |
41 SessionManager::Listener* listener) { | 37 SessionManager::Listener* listener) { |
42 listener_ = listener; | 38 listener_ = listener; |
43 signal_strategy_ = signal_strategy; | 39 signal_strategy_ = signal_strategy; |
44 iq_sender_.reset(new IqSender(signal_strategy_)); | 40 iq_sender_.reset(new IqSender(signal_strategy_)); |
45 | 41 |
46 signal_strategy_->AddListener(this); | 42 signal_strategy_->AddListener(this); |
47 | 43 |
48 OnSignalStrategyStateChange(signal_strategy_->GetState()); | 44 OnSignalStrategyStateChange(signal_strategy_->GetState()); |
49 } | 45 } |
50 | 46 |
51 void JingleSessionManager::OnJingleInfo( | |
52 const std::string& relay_token, | |
53 const std::vector<std::string>& relay_hosts, | |
54 const std::vector<talk_base::SocketAddress>& stun_hosts) { | |
55 DCHECK(CalledOnValidThread()); | |
56 | |
57 // TODO(sergeyu): Add support for multiple STUN/relay servers when | |
58 // it's implemented in libjingle and P2P Transport API. | |
59 TransportConfig config; | |
60 config.stun_server = stun_hosts[0].ToString(); | |
61 config.relay_server = relay_hosts[0]; | |
62 config.relay_token = relay_token; | |
63 transport_factory_->SetTransportConfig(config); | |
64 | |
65 VLOG(1) << "STUN server: " << config.stun_server | |
66 << " Relay server: " << config.relay_server | |
67 << " Relay token: " << config.relay_token; | |
68 | |
69 | |
70 if (!ready_) { | |
71 ready_ = true; | |
72 listener_->OnSessionManagerReady(); | |
73 } | |
74 } | |
75 | |
76 scoped_ptr<Session> JingleSessionManager::Connect( | 47 scoped_ptr<Session> JingleSessionManager::Connect( |
77 const std::string& host_jid, | 48 const std::string& host_jid, |
78 scoped_ptr<Authenticator> authenticator, | 49 scoped_ptr<Authenticator> authenticator, |
79 scoped_ptr<CandidateSessionConfig> config) { | 50 scoped_ptr<CandidateSessionConfig> config) { |
51 // Notify |transport_factory_| that it may be used soon. | |
52 transport_factory_->PrepareTokens(); | |
53 | |
80 scoped_ptr<JingleSession> session(new JingleSession(this)); | 54 scoped_ptr<JingleSession> session(new JingleSession(this)); |
81 session->StartConnection(host_jid, authenticator.Pass(), config.Pass()); | 55 session->StartConnection(host_jid, authenticator.Pass(), config.Pass()); |
82 sessions_[session->session_id_] = session.get(); | 56 sessions_[session->session_id_] = session.get(); |
83 return session.PassAs<Session>(); | 57 return session.PassAs<Session>(); |
84 } | 58 } |
85 | 59 |
86 void JingleSessionManager::Close() { | 60 void JingleSessionManager::Close() { |
87 DCHECK(CalledOnValidThread()); | 61 DCHECK(CalledOnValidThread()); |
88 | 62 |
89 // Close() can be called only after all sessions are destroyed. | 63 // Close() can be called only after all sessions are destroyed. |
90 DCHECK(sessions_.empty()); | 64 DCHECK(sessions_.empty()); |
91 | 65 |
92 listener_ = NULL; | 66 listener_ = NULL; |
93 jingle_info_request_.reset(); | |
94 | 67 |
95 if (signal_strategy_) { | 68 if (signal_strategy_) { |
96 signal_strategy_->RemoveListener(this); | 69 signal_strategy_->RemoveListener(this); |
97 signal_strategy_ = NULL; | 70 signal_strategy_ = NULL; |
98 } | 71 } |
99 } | 72 } |
100 | 73 |
101 void JingleSessionManager::set_authenticator_factory( | 74 void JingleSessionManager::set_authenticator_factory( |
102 scoped_ptr<AuthenticatorFactory> authenticator_factory) { | 75 scoped_ptr<AuthenticatorFactory> authenticator_factory) { |
103 DCHECK(CalledOnValidThread()); | 76 DCHECK(CalledOnValidThread()); |
104 authenticator_factory_ = authenticator_factory.Pass(); | 77 authenticator_factory_ = authenticator_factory.Pass(); |
105 } | 78 } |
106 | 79 |
107 void JingleSessionManager::OnSignalStrategyStateChange( | 80 void JingleSessionManager::OnSignalStrategyStateChange( |
108 SignalStrategy::State state) { | 81 SignalStrategy::State state) { |
109 if (state == SignalStrategy::CONNECTED) { | 82 if (state == SignalStrategy::CONNECTED && !ready_) { |
110 // Request STUN/Relay info if necessary. | 83 ready_ = true; |
111 if (fetch_stun_relay_config_) { | 84 listener_->OnSessionManagerReady(); |
rmsousa
2013/12/13 22:10:40
Should we invalidate the transport factory tokens
Sergey Ulanov
2013/12/13 23:54:34
AFAICT token lifetime is not connected to IP or JI
| |
112 jingle_info_request_.reset(new JingleInfoRequest(signal_strategy_)); | |
113 jingle_info_request_->Send(base::Bind(&JingleSessionManager::OnJingleInfo, | |
114 base::Unretained(this))); | |
115 } else if (!ready_) { | |
116 ready_ = true; | |
117 listener_->OnSessionManagerReady(); | |
118 } | |
119 } | 85 } |
120 } | 86 } |
121 | 87 |
122 bool JingleSessionManager::OnSignalStrategyIncomingStanza( | 88 bool JingleSessionManager::OnSignalStrategyIncomingStanza( |
123 const buzz::XmlElement* stanza) { | 89 const buzz::XmlElement* stanza) { |
124 if (!JingleMessage::IsJingleMessage(stanza)) | 90 if (!JingleMessage::IsJingleMessage(stanza)) |
125 return false; | 91 return false; |
126 | 92 |
127 JingleMessage message; | 93 JingleMessage message; |
128 std::string error; | 94 std::string error; |
129 if (!message.ParseXml(stanza, &error)) { | 95 if (!message.ParseXml(stanza, &error)) { |
130 SendReply(stanza, JingleMessageReply::BAD_REQUEST); | 96 SendReply(stanza, JingleMessageReply::BAD_REQUEST); |
131 return true; | 97 return true; |
132 } | 98 } |
133 | 99 |
134 if (message.action == JingleMessage::SESSION_INITIATE) { | 100 if (message.action == JingleMessage::SESSION_INITIATE) { |
135 // Description must be present in session-initiate messages. | 101 // Description must be present in session-initiate messages. |
136 DCHECK(message.description.get()); | 102 DCHECK(message.description.get()); |
137 | 103 |
138 SendReply(stanza, JingleMessageReply::NONE); | 104 SendReply(stanza, JingleMessageReply::NONE); |
139 | 105 |
106 // Notify |transport_factory_| that it may be used soon. | |
107 transport_factory_->PrepareTokens(); | |
108 | |
140 scoped_ptr<Authenticator> authenticator = | 109 scoped_ptr<Authenticator> authenticator = |
141 authenticator_factory_->CreateAuthenticator( | 110 authenticator_factory_->CreateAuthenticator( |
142 signal_strategy_->GetLocalJid(), message.from, | 111 signal_strategy_->GetLocalJid(), message.from, |
143 message.description->authenticator_message()); | 112 message.description->authenticator_message()); |
144 | 113 |
145 JingleSession* session = new JingleSession(this); | 114 JingleSession* session = new JingleSession(this); |
146 session->InitializeIncomingConnection(message, authenticator.Pass()); | 115 session->InitializeIncomingConnection(message, authenticator.Pass()); |
147 sessions_[session->session_id_] = session; | 116 sessions_[session->session_id_] = session; |
148 | 117 |
149 IncomingSessionResponse response = SessionManager::DECLINE; | 118 IncomingSessionResponse response = SessionManager::DECLINE; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 signal_strategy_->SendStanza( | 164 signal_strategy_->SendStanza( |
196 JingleMessageReply(error).ToXml(original_stanza)); | 165 JingleMessageReply(error).ToXml(original_stanza)); |
197 } | 166 } |
198 | 167 |
199 void JingleSessionManager::SessionDestroyed(JingleSession* session) { | 168 void JingleSessionManager::SessionDestroyed(JingleSession* session) { |
200 sessions_.erase(session->session_id_); | 169 sessions_.erase(session->session_id_); |
201 } | 170 } |
202 | 171 |
203 } // namespace protocol | 172 } // namespace protocol |
204 } // namespace remoting | 173 } // namespace remoting |
OLD | NEW |