| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/protocol/pepper_session_manager.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "remoting/jingle_glue/jingle_info_request.h" |
| 9 #include "remoting/jingle_glue/signal_strategy.h" |
| 10 #include "remoting/protocol/jingle_messages.h" |
| 11 #include "remoting/protocol/pepper_session.h" |
| 12 #include "third_party/libjingle/source/talk/base/socketaddress.h" |
| 13 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| 14 |
| 15 using buzz::QName; |
| 16 |
| 17 namespace remoting { |
| 18 namespace protocol { |
| 19 |
| 20 PepperSessionManager::PepperSessionManager(pp::Instance* pp_instance) |
| 21 : pp_instance_(pp_instance), |
| 22 signal_strategy_(NULL), |
| 23 listener_(NULL), |
| 24 allow_nat_traversal_(false) { |
| 25 } |
| 26 |
| 27 PepperSessionManager::~PepperSessionManager() { |
| 28 Close(); |
| 29 } |
| 30 |
| 31 // Initializes the session client. |
| 32 void PepperSessionManager::Init( |
| 33 const std::string& local_jid, |
| 34 SignalStrategy* signal_strategy, |
| 35 SessionManager::Listener* listener, |
| 36 crypto::RSAPrivateKey* private_key, |
| 37 const std::string& certificate, |
| 38 bool allow_nat_traversal) { |
| 39 listener_ = listener; |
| 40 local_jid_ = local_jid; |
| 41 signal_strategy_ = signal_strategy; |
| 42 private_key_.reset(private_key); |
| 43 certificate_ = certificate; |
| 44 allow_nat_traversal_ = allow_nat_traversal; |
| 45 |
| 46 signal_strategy_->SetListener(this); |
| 47 |
| 48 // If NAT traversal is enabled then we need to request STUN/Relay info. |
| 49 if (allow_nat_traversal) { |
| 50 jingle_info_request_.reset( |
| 51 new JingleInfoRequest(signal_strategy_->CreateIqRequest(), NULL)); |
| 52 jingle_info_request_->Send(base::Bind( |
| 53 &PepperSessionManager::OnJingleInfo, base::Unretained(this))); |
| 54 } else { |
| 55 listener_->OnSessionManagerInitialized(); |
| 56 } |
| 57 } |
| 58 |
| 59 void PepperSessionManager::OnJingleInfo( |
| 60 const std::string& relay_token, |
| 61 const std::vector<std::string>& relay_hosts, |
| 62 const std::vector<talk_base::SocketAddress>& stun_hosts) { |
| 63 DCHECK(CalledOnValidThread()); |
| 64 |
| 65 // Here we use only the first STUN server and the first relay server. |
| 66 // |
| 67 // TODO(sergeyu): Add support for multiple STUN/relay servers when |
| 68 // it's implemented in libjingle and P2P Transport API. |
| 69 transport_config_.stun_server = stun_hosts[0].ToString(); |
| 70 transport_config_.relay_server = relay_hosts[0]; |
| 71 transport_config_.relay_token = relay_token; |
| 72 LOG(INFO) << "STUN server: " << transport_config_.stun_server |
| 73 << " Relay server: " << transport_config_.relay_server |
| 74 << " Relay token: " << transport_config_.relay_token; |
| 75 |
| 76 listener_->OnSessionManagerInitialized(); |
| 77 } |
| 78 |
| 79 Session* PepperSessionManager::Connect( |
| 80 const std::string& host_jid, |
| 81 const std::string& host_public_key, |
| 82 const std::string& client_token, |
| 83 CandidateSessionConfig* config, |
| 84 Session::StateChangeCallback* state_change_callback) { |
| 85 PepperSession* session = new PepperSession(this); |
| 86 session->StartConnection(host_jid, host_public_key, client_token, |
| 87 config, state_change_callback); |
| 88 sessions_[session->sid_] = session; |
| 89 return session; |
| 90 } |
| 91 |
| 92 void PepperSessionManager::Close() { |
| 93 DCHECK(CalledOnValidThread()); |
| 94 |
| 95 // Close() can be called only after all sessions are destroyed. |
| 96 DCHECK(sessions_.empty()); |
| 97 |
| 98 listener_ = NULL; |
| 99 jingle_info_request_.reset(); |
| 100 |
| 101 signal_strategy_->SetListener(NULL); |
| 102 } |
| 103 |
| 104 bool PepperSessionManager::OnIncomingStanza(const buzz::XmlElement* stanza) { |
| 105 if (!JingleMessage::IsJingleMessage(stanza)) |
| 106 return false; |
| 107 |
| 108 JingleMessage message; |
| 109 std::string error; |
| 110 JingleMessageReply reply; |
| 111 |
| 112 if (!message.ParseXml(stanza, &error)) { |
| 113 reply = JingleMessageReply(JingleMessageReply::BAD_REQUEST); |
| 114 } else if (message.action == JingleMessage::SESSION_INITIATE) { |
| 115 reply = JingleMessageReply(JingleMessageReply::NOT_IMPLEMENTED, |
| 116 "Can't accept sessions on the client"); |
| 117 } else { |
| 118 SessionsMap::iterator it = sessions_.find(message.sid); |
| 119 if (it == sessions_.end()) { |
| 120 reply = JingleMessageReply(JingleMessageReply::INVALID_SID); |
| 121 } else { |
| 122 it->second->OnIncomingMessage(message, &reply); |
| 123 } |
| 124 } |
| 125 |
| 126 SendReply(stanza, reply); |
| 127 |
| 128 return true; |
| 129 } |
| 130 |
| 131 IqRequest* PepperSessionManager::CreateIqRequest() { |
| 132 return signal_strategy_->CreateIqRequest(); |
| 133 } |
| 134 |
| 135 void PepperSessionManager::SendReply(const buzz::XmlElement* original_stanza, |
| 136 const JingleMessageReply& reply) { |
| 137 buzz::XmlElement* stanza = reply.ToXml(original_stanza); |
| 138 signal_strategy_->SendStanza(stanza); |
| 139 } |
| 140 |
| 141 void PepperSessionManager::SessionDestroyed(PepperSession* session) { |
| 142 sessions_.erase(session->sid_); |
| 143 } |
| 144 |
| 145 } // namespace protocol |
| 146 } // namespace remoting |
| OLD | NEW |