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/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 ChannelMultiplexer
;
34 class JingleSessionManager
;
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
{
43 virtual ~JingleSession();
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
;
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
;
65 // Transport::EventHandler interface.
66 virtual void OnTransportCandidate(
68 const cricket::Candidate
& candidate
) OVERRIDE
;
69 virtual void OnTransportRouteChange(Transport
* transport
,
70 const TransportRoute
& route
) OVERRIDE
;
71 virtual void OnTransportFailed(Transport
* transport
) OVERRIDE
;
72 virtual void OnTransportDeleted(Transport
* transport
) OVERRIDE
;
75 friend class JingleSessionManager
;
77 typedef std::map
<std::string
, Transport
*> ChannelsMap
;
78 typedef base::Callback
<void(JingleMessageReply::ErrorType
)> ReplyCallback
;
80 explicit JingleSession(JingleSessionManager
* session_manager
);
82 // Start connection by sending session-initiate message.
83 void StartConnection(const std::string
& peer_jid
,
84 scoped_ptr
<Authenticator
> authenticator
,
85 scoped_ptr
<CandidateSessionConfig
> config
);
87 // Adds to a new channel the remote candidates received before it was created.
88 void AddPendingRemoteCandidates(Transport
* channel
, const std::string
& name
);
90 // Called by JingleSessionManager for incoming connections.
91 void InitializeIncomingConnection(const JingleMessage
& initiate_message
,
92 scoped_ptr
<Authenticator
> authenticator
);
93 void AcceptIncomingConnection(const JingleMessage
& initiate_message
);
95 // Sends |message| to the peer. The session is closed if the send fails or no
96 // response is received within a reasonable time. All other responses are
98 void SendMessage(const JingleMessage
& message
);
100 // Iq response handler.
101 void OnMessageResponse(JingleMessage::ActionType request_type
,
103 const buzz::XmlElement
* response
);
105 // Sends transport-info message with candidates from |pending_candidates_|.
106 void SendTransportInfo();
108 // Response handler for transport-info responses. Transport-info timeouts are
109 // ignored and don't terminate connection.
110 void OnTransportInfoResponse(IqRequest
* request
,
111 const buzz::XmlElement
* response
);
113 // Called by JingleSessionManager on incoming |message|. Must call
114 // |reply_callback| to send reply message before sending any other
116 void OnIncomingMessage(const JingleMessage
& message
,
117 const ReplyCallback
& reply_callback
);
119 // Message handlers for incoming messages.
120 void OnAccept(const JingleMessage
& message
,
121 const ReplyCallback
& reply_callback
);
122 void OnSessionInfo(const JingleMessage
& message
,
123 const ReplyCallback
& reply_callback
);
124 void OnTerminate(const JingleMessage
& message
,
125 const ReplyCallback
& reply_callback
);
126 void ProcessTransportInfo(const JingleMessage
& message
);
128 // Called from OnAccept() to initialize session config.
129 bool InitializeConfigFromDescription(const ContentDescription
* description
);
131 // Called after the initial incoming authenticator message is processed.
132 void ContinueAcceptIncomingConnection();
134 // Called after subsequent authenticator messages are processed.
135 void ProcessAuthenticationStep();
137 // Called after the authenticating step is finished.
138 void ContinueAuthenticationStep();
140 // Terminates the session and sends session-terminate if it is
141 // necessary. |error| specifies the error code in case when the
142 // session is being closed due to an error.
143 void CloseInternal(ErrorCode error
);
145 // Sets |state_| to |new_state| and calls state change callback.
146 void SetState(State new_state
);
148 // Returns true if the state of the session is not CLOSED or FAILED
149 bool is_session_active();
151 JingleSessionManager
* session_manager_
;
152 std::string peer_jid_
;
153 scoped_ptr
<CandidateSessionConfig
> candidate_config_
;
154 Session::EventHandler
* event_handler_
;
156 std::string session_id_
;
160 SessionConfig config_
;
163 scoped_ptr
<Authenticator
> authenticator_
;
165 // Pending Iq requests. Used for all messages except transport-info.
166 std::set
<IqRequest
*> pending_requests_
;
168 // Pending transport-info requests.
169 std::list
<IqRequest
*> transport_info_requests_
;
171 ChannelsMap channels_
;
172 scoped_ptr
<ChannelMultiplexer
> channel_multiplexer_
;
174 base::OneShotTimer
<JingleSession
> transport_infos_timer_
;
175 std::list
<JingleMessage::NamedCandidate
> pending_candidates_
;
177 // Pending remote candidates, received before the local channels were created.
178 std::list
<JingleMessage::NamedCandidate
> pending_remote_candidates_
;
180 base::WeakPtrFactory
<JingleSession
> weak_factory_
;
182 DISALLOW_COPY_AND_ASSIGN(JingleSession
);
185 } // namespace protocol
186 } // namespace remoting
188 #endif // REMOTING_PROTOCOL_JINGLE_SESSION_H_