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.
5 #ifndef REMOTING_PROTOCOL_JINGLE_SESSION_H_
6 #define REMOTING_PROTOCOL_JINGLE_SESSION_H_
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/protocol/authenticator.h"
18 #include "remoting/protocol/datagram_channel_factory.h"
19 #include "remoting/protocol/jingle_messages.h"
20 #include "remoting/protocol/session.h"
21 #include "remoting/protocol/session_config.h"
22 #include "remoting/protocol/transport.h"
23 #include "remoting/signaling/iq_sender.h"
33 class SecureChannelFactory
;
34 class ChannelMultiplexer
;
35 class JingleSessionManager
;
36 class PseudoTcpChannelFactory
;
38 // JingleSessionManager and JingleSession implement the subset of the
39 // Jingle protocol used in Chromoting. Instances of this class are
40 // created by the JingleSessionManager.
41 class JingleSession
: public base::NonThreadSafe
,
43 public DatagramChannelFactory
,
44 public Transport::EventHandler
{
46 ~JingleSession() override
;
49 void SetEventHandler(Session::EventHandler
* event_handler
) override
;
50 ErrorCode
error() override
;
51 const std::string
& jid() override
;
52 const CandidateSessionConfig
* candidate_config() override
;
53 const SessionConfig
& config() override
;
54 void set_config(scoped_ptr
<SessionConfig
> config
) override
;
55 StreamChannelFactory
* GetTransportChannelFactory() override
;
56 StreamChannelFactory
* GetMultiplexedChannelFactory() override
;
57 void Close() override
;
59 // DatagramChannelFactory interface.
60 void CreateChannel(const std::string
& name
,
61 const ChannelCreatedCallback
& callback
) override
;
62 void CancelChannelCreation(const std::string
& name
) override
;
64 // Transport::EventHandler interface.
65 void OnTransportIceCredentials(Transport
* transport
,
66 const std::string
& ufrag
,
67 const std::string
& password
) override
;
68 void OnTransportCandidate(Transport
* transport
,
69 const cricket::Candidate
& candidate
) override
;
70 void OnTransportRouteChange(Transport
* transport
,
71 const TransportRoute
& route
) override
;
72 void OnTransportFailed(Transport
* transport
) override
;
73 void OnTransportDeleted(Transport
* transport
) override
;
76 friend class JingleSessionManager
;
78 typedef std::map
<std::string
, Transport
*> ChannelsMap
;
79 typedef base::Callback
<void(JingleMessageReply::ErrorType
)> ReplyCallback
;
81 explicit JingleSession(JingleSessionManager
* session_manager
);
83 // Start connection by sending session-initiate message.
84 void StartConnection(const std::string
& peer_jid
,
85 scoped_ptr
<Authenticator
> authenticator
,
86 scoped_ptr
<CandidateSessionConfig
> config
);
88 // Passes transport info to a new |channel| in case it was received before the
89 // channel was created.
90 void AddPendingRemoteTransportInfo(Transport
* channel
);
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
);
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
100 void SendMessage(const JingleMessage
& message
);
102 // Iq response handler.
103 void OnMessageResponse(JingleMessage::ActionType request_type
,
105 const buzz::XmlElement
* response
);
107 // Creates empty |pending_transport_info_message_| and schedules timer for
108 // SentTransportInfo() to sent the message later.
109 void EnsurePendingTransportInfoMessage();
111 // Sends transport-info message with candidates from |pending_candidates_|.
112 void SendTransportInfo();
114 // Response handler for transport-info responses. Transport-info timeouts are
115 // ignored and don't terminate connection.
116 void OnTransportInfoResponse(IqRequest
* request
,
117 const buzz::XmlElement
* response
);
119 // Called by JingleSessionManager on incoming |message|. Must call
120 // |reply_callback| to send reply message before sending any other
122 void OnIncomingMessage(const JingleMessage
& message
,
123 const ReplyCallback
& reply_callback
);
125 // Message handlers for incoming messages.
126 void OnAccept(const JingleMessage
& message
,
127 const ReplyCallback
& reply_callback
);
128 void OnSessionInfo(const JingleMessage
& message
,
129 const ReplyCallback
& reply_callback
);
130 void OnTerminate(const JingleMessage
& message
,
131 const ReplyCallback
& reply_callback
);
132 void ProcessTransportInfo(const JingleMessage
& message
);
134 // Called from OnAccept() to initialize session config.
135 bool InitializeConfigFromDescription(const ContentDescription
* description
);
137 // Called after the initial incoming authenticator message is processed.
138 void ContinueAcceptIncomingConnection();
140 // Called after subsequent authenticator messages are processed.
141 void ProcessAuthenticationStep();
143 // Called after the authenticating step is finished.
144 void ContinueAuthenticationStep();
146 // Called when authentication is finished.
147 void OnAuthenticated();
149 // Terminates the session and sends session-terminate if it is
150 // necessary. |error| specifies the error code in case when the
151 // session is being closed due to an error.
152 void CloseInternal(ErrorCode error
);
154 // Sets |state_| to |new_state| and calls state change callback.
155 void SetState(State new_state
);
157 // Returns true if the state of the session is not CLOSED or FAILED
158 bool is_session_active();
160 JingleSessionManager
* session_manager_
;
161 std::string peer_jid_
;
162 scoped_ptr
<CandidateSessionConfig
> candidate_config_
;
163 Session::EventHandler
* event_handler_
;
165 std::string session_id_
;
169 scoped_ptr
<SessionConfig
> config_
;
171 scoped_ptr
<Authenticator
> authenticator_
;
173 // Pending Iq requests. Used for all messages except transport-info.
174 std::set
<IqRequest
*> pending_requests_
;
176 // Pending transport-info requests.
177 std::list
<IqRequest
*> transport_info_requests_
;
179 ChannelsMap channels_
;
180 scoped_ptr
<PseudoTcpChannelFactory
> pseudotcp_channel_factory_
;
181 scoped_ptr
<SecureChannelFactory
> secure_channel_factory_
;
182 scoped_ptr
<ChannelMultiplexer
> channel_multiplexer_
;
184 scoped_ptr
<JingleMessage
> pending_transport_info_message_
;
185 base::OneShotTimer
<JingleSession
> transport_info_timer_
;
187 // Pending remote transport info received before the local channels were
189 std::list
<JingleMessage::IceCredentials
> pending_remote_ice_credentials_
;
190 std::list
<JingleMessage::NamedCandidate
> pending_remote_candidates_
;
192 base::WeakPtrFactory
<JingleSession
> weak_factory_
;
194 DISALLOW_COPY_AND_ASSIGN(JingleSession
);
197 } // namespace protocol
198 } // namespace remoting
200 #endif // REMOTING_PROTOCOL_JINGLE_SESSION_H_