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"
28 class SecureChannelFactory
;
29 class ChannelMultiplexer
;
30 class JingleSessionManager
;
31 class PseudoTcpChannelFactory
;
33 // JingleSessionManager and JingleSession implement the subset of the
34 // Jingle protocol used in Chromoting. Instances of this class are
35 // created by the JingleSessionManager.
36 class JingleSession
: public base::NonThreadSafe
,
38 public DatagramChannelFactory
,
39 public Transport::EventHandler
{
41 ~JingleSession() override
;
44 void SetEventHandler(Session::EventHandler
* event_handler
) override
;
45 ErrorCode
error() override
;
46 const std::string
& jid() override
;
47 const CandidateSessionConfig
* candidate_config() override
;
48 const SessionConfig
& config() override
;
49 void set_config(scoped_ptr
<SessionConfig
> config
) override
;
50 StreamChannelFactory
* GetTransportChannelFactory() override
;
51 StreamChannelFactory
* GetMultiplexedChannelFactory() override
;
52 void Close() override
;
54 // DatagramChannelFactory interface.
55 void CreateChannel(const std::string
& name
,
56 const ChannelCreatedCallback
& callback
) override
;
57 void CancelChannelCreation(const std::string
& name
) override
;
59 // Transport::EventHandler interface.
60 void OnTransportIceCredentials(Transport
* transport
,
61 const std::string
& ufrag
,
62 const std::string
& password
) override
;
63 void OnTransportCandidate(Transport
* transport
,
64 const cricket::Candidate
& candidate
) override
;
65 void OnTransportRouteChange(Transport
* transport
,
66 const TransportRoute
& route
) override
;
67 void OnTransportFailed(Transport
* transport
) override
;
68 void OnTransportDeleted(Transport
* transport
) override
;
71 friend class JingleSessionManager
;
73 typedef std::map
<std::string
, Transport
*> ChannelsMap
;
74 typedef base::Callback
<void(JingleMessageReply::ErrorType
)> ReplyCallback
;
76 explicit JingleSession(JingleSessionManager
* session_manager
);
78 // Start connection by sending session-initiate message.
79 void StartConnection(const std::string
& peer_jid
,
80 scoped_ptr
<Authenticator
> authenticator
,
81 scoped_ptr
<CandidateSessionConfig
> config
);
83 // Passes transport info to a new |channel| in case it was received before the
84 // channel was created.
85 void AddPendingRemoteTransportInfo(Transport
* channel
);
87 // Called by JingleSessionManager for incoming connections.
88 void InitializeIncomingConnection(const JingleMessage
& initiate_message
,
89 scoped_ptr
<Authenticator
> authenticator
);
90 void AcceptIncomingConnection(const JingleMessage
& initiate_message
);
92 // Sends |message| to the peer. The session is closed if the send fails or no
93 // response is received within a reasonable time. All other responses are
95 void SendMessage(const JingleMessage
& message
);
97 // Iq response handler.
98 void OnMessageResponse(JingleMessage::ActionType request_type
,
100 const buzz::XmlElement
* response
);
102 // Creates empty |pending_transport_info_message_| and schedules timer for
103 // SentTransportInfo() to sent the message later.
104 void EnsurePendingTransportInfoMessage();
106 // Sends transport-info message with candidates from |pending_candidates_|.
107 void SendTransportInfo();
109 // Response handler for transport-info responses. Transport-info timeouts are
110 // ignored and don't terminate connection.
111 void OnTransportInfoResponse(IqRequest
* request
,
112 const buzz::XmlElement
* response
);
114 // Called by JingleSessionManager on incoming |message|. Must call
115 // |reply_callback| to send reply message before sending any other
117 void OnIncomingMessage(const JingleMessage
& message
,
118 const ReplyCallback
& reply_callback
);
120 // Message handlers for incoming messages.
121 void OnAccept(const JingleMessage
& message
,
122 const ReplyCallback
& reply_callback
);
123 void OnSessionInfo(const JingleMessage
& message
,
124 const ReplyCallback
& reply_callback
);
125 void OnTerminate(const JingleMessage
& message
,
126 const ReplyCallback
& reply_callback
);
127 void ProcessTransportInfo(const JingleMessage
& message
);
129 // Called from OnAccept() to initialize session config.
130 bool InitializeConfigFromDescription(const ContentDescription
* description
);
132 // Called after the initial incoming authenticator message is processed.
133 void ContinueAcceptIncomingConnection();
135 // Called after subsequent authenticator messages are processed.
136 void ProcessAuthenticationStep();
138 // Called after the authenticating step is finished.
139 void ContinueAuthenticationStep();
141 // Called when authentication is finished.
142 void OnAuthenticated();
144 // Terminates the session and sends session-terminate if it is
145 // necessary. |error| specifies the error code in case when the
146 // session is being closed due to an error.
147 void CloseInternal(ErrorCode error
);
149 // Sets |state_| to |new_state| and calls state change callback.
150 void SetState(State new_state
);
152 // Returns true if the state of the session is not CLOSED or FAILED
153 bool is_session_active();
155 JingleSessionManager
* session_manager_
;
156 std::string peer_jid_
;
157 scoped_ptr
<CandidateSessionConfig
> candidate_config_
;
158 Session::EventHandler
* event_handler_
;
160 std::string session_id_
;
164 scoped_ptr
<SessionConfig
> config_
;
166 scoped_ptr
<Authenticator
> authenticator_
;
168 // Pending Iq requests. Used for all messages except transport-info.
169 std::set
<IqRequest
*> pending_requests_
;
171 // Pending transport-info requests.
172 std::list
<IqRequest
*> transport_info_requests_
;
174 ChannelsMap channels_
;
175 scoped_ptr
<PseudoTcpChannelFactory
> pseudotcp_channel_factory_
;
176 scoped_ptr
<SecureChannelFactory
> secure_channel_factory_
;
177 scoped_ptr
<ChannelMultiplexer
> channel_multiplexer_
;
179 scoped_ptr
<JingleMessage
> pending_transport_info_message_
;
180 base::OneShotTimer
<JingleSession
> transport_info_timer_
;
182 // Pending remote transport info received before the local channels were
184 std::list
<JingleMessage::IceCredentials
> pending_remote_ice_credentials_
;
185 std::list
<JingleMessage::NamedCandidate
> pending_remote_candidates_
;
187 base::WeakPtrFactory
<JingleSession
> weak_factory_
;
189 DISALLOW_COPY_AND_ASSIGN(JingleSession
);
192 } // namespace protocol
193 } // namespace remoting
195 #endif // REMOTING_PROTOCOL_JINGLE_SESSION_H_