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 3 // found in the LICENSE file. 4 5 #ifndef REMOTING_PROTOCOL_JINGLE_SESSION_H_ 6 #define REMOTING_PROTOCOL_JINGLE_SESSION_H_ 7 8 #include <list> 9 #include <map> 10 #include <set> 11 #include <string> 12 13 #include "base/memory/ref_counted.h" 14 #include "base/timer/timer.h" 15 #include "crypto/rsa_private_key.h" 16 #include "net/base/completion_callback.h" 17 #include "remoting/jingle_glue/iq_sender.h" 18 #include "remoting/protocol/authenticator.h" 19 #include "remoting/protocol/channel_factory.h" 20 #include "remoting/protocol/jingle_messages.h" 21 #include "remoting/protocol/session.h" 22 #include "remoting/protocol/session_config.h" 23 #include "remoting/protocol/transport.h" 24 25 namespace net { 26 class Socket; 27 class StreamSocket; 28 } // namespace net 29 30 namespace remoting { 31 namespace protocol { 32 33 class ChannelMultiplexer; 34 class JingleSessionManager; 35 36 // JingleSessionManager and JingleSession implement the subset of the 37 // Jingle protocol used in Chromoting. Instances of this class are 38 // created by the JingleSessionManager. 39 class JingleSession : public Session, 40 public ChannelFactory, 41 public Transport::EventHandler { 42 public: 43 virtual ~JingleSession(); 44 45 // Session interface. 46 virtual void SetEventHandler(Session::EventHandler* event_handler) OVERRIDE; 47 virtual ErrorCode error() OVERRIDE; 48 virtual const std::string& jid() OVERRIDE; 49 virtual const CandidateSessionConfig* candidate_config() OVERRIDE; 50 virtual const SessionConfig& config() OVERRIDE; 51 virtual void set_config(const SessionConfig& config) OVERRIDE; 52 virtual ChannelFactory* GetTransportChannelFactory() OVERRIDE; 53 virtual ChannelFactory* GetMultiplexedChannelFactory() OVERRIDE; 54 virtual void Close() OVERRIDE; 55 56 // ChannelFactory interface. 57 virtual void CreateStreamChannel( 58 const std::string& name, 59 const StreamChannelCallback& callback) OVERRIDE; 60 virtual void CreateDatagramChannel( 61 const std::string& name, 62 const DatagramChannelCallback& callback) OVERRIDE; 63 virtual void CancelChannelCreation(const std::string& name) OVERRIDE; 64 65 // Transport::EventHandler interface. 66 virtual void OnTransportCandidate( 67 Transport* transport, 68 const cricket::Candidate& candidate) OVERRIDE; 69 virtual void OnTransportRouteChange(Transport* transport, 70 const TransportRoute& route) OVERRIDE; 71 virtual void OnTransportReady(Transport* transport, 72 bool ready) OVERRIDE; 73 virtual void OnTransportFailed(Transport* transport) OVERRIDE; 74 virtual void OnTransportDeleted(Transport* transport) OVERRIDE; 75 76 private: 77 friend class JingleSessionManager; 78 79 typedef std::map<std::string, Transport*> ChannelsMap; 80 typedef base::Callback<void(JingleMessageReply::ErrorType)> ReplyCallback; 81 82 explicit JingleSession(JingleSessionManager* session_manager); 83 84 // Start connection by sending session-initiate message. 85 void StartConnection(const std::string& peer_jid, 86 scoped_ptr<Authenticator> authenticator, 87 scoped_ptr<CandidateSessionConfig> config); 88 89 // Adds to a new channel the remote candidates received before it was created. 90 void AddPendingRemoteCandidates(Transport* channel, const std::string& name); 91 92 // Called by JingleSessionManager for incoming connections. 93 void InitializeIncomingConnection(const JingleMessage& initiate_message, 94 scoped_ptr<Authenticator> authenticator); 95 void AcceptIncomingConnection(const JingleMessage& initiate_message); 96 97 // Sends |message| to the peer. The session is closed if the send fails or no 98 // response is received within a reasonable time. All other responses are 99 // ignored. 100 void SendMessage(const JingleMessage& message); 101 102 // Iq response handler. 103 void OnMessageResponse(JingleMessage::ActionType request_type, 104 IqRequest* request, 105 const buzz::XmlElement* response); 106 107 // Sends transport-info message with candidates from |pending_candidates_|. 108 void SendTransportInfo(); 109 110 // Response handler for transport-info responses. Transport-info timeouts are 111 // ignored and don't terminate connection. 112 void OnTransportInfoResponse(IqRequest* request, 113 const buzz::XmlElement* response); 114 115 // Called by JingleSessionManager on incoming |message|. Must call 116 // |reply_callback| to send reply message before sending any other 117 // messages. 118 void OnIncomingMessage(const JingleMessage& message, 119 const ReplyCallback& reply_callback); 120 121 // Message handlers for incoming messages. 122 void OnAccept(const JingleMessage& message, 123 const ReplyCallback& reply_callback); 124 void OnSessionInfo(const JingleMessage& message, 125 const ReplyCallback& reply_callback); 126 void OnTerminate(const JingleMessage& message, 127 const ReplyCallback& reply_callback); 128 void ProcessTransportInfo(const JingleMessage& message); 129 130 // Called from OnAccept() to initialize session config. 131 bool InitializeConfigFromDescription(const ContentDescription* description); 132 133 // Called after the initial incoming authenticator message is processed. 134 void ContinueAcceptIncomingConnection(); 135 // Called after subsequent authenticator messages are processed. 136 void ProcessAuthenticationStep(); 137 138 // Terminates the session and sends session-terminate if it is 139 // necessary. |error| specifies the error code in case when the 140 // session is being closed due to an error. 141 void CloseInternal(ErrorCode error); 142 143 // Sets |state_| to |new_state| and calls state change callback. 144 void SetState(State new_state); 145 146 JingleSessionManager* session_manager_; 147 std::string peer_jid_; 148 scoped_ptr<CandidateSessionConfig> candidate_config_; 149 Session::EventHandler* event_handler_; 150 151 std::string session_id_; 152 State state_; 153 ErrorCode error_; 154 155 SessionConfig config_; 156 bool config_is_set_; 157 158 scoped_ptr<Authenticator> authenticator_; 159 160 // Pending Iq requests. Used for all messages except transport-info. 161 std::set<IqRequest*> pending_requests_; 162 163 // Pending transport-info requests. 164 std::list<IqRequest*> transport_info_requests_; 165 166 ChannelsMap channels_; 167 scoped_ptr<ChannelMultiplexer> channel_multiplexer_; 168 169 base::OneShotTimer<JingleSession> transport_infos_timer_; 170 std::list<JingleMessage::NamedCandidate> pending_candidates_; 171 172 // Pending remote candidates, received before the local channels were created. 173 std::list<JingleMessage::NamedCandidate> pending_remote_candidates_; 174 175 DISALLOW_COPY_AND_ASSIGN(JingleSession); 176 }; 177 178 } // namespace protocol 179 } // namespace remoting 180 181 #endif // REMOTING_PROTOCOL_JINGLE_SESSION_H_ 182