Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / protocol / jingle_session.h
blob41b5e833811528033133aa23d230198b3eb1c205
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_
8 #include <list>
9 #include <map>
10 #include <set>
11 #include <string>
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"
25 namespace remoting {
26 namespace protocol {
28 class ChannelMultiplexer;
29 class JingleSessionManager;
30 class PseudoTcpChannelFactory;
31 class QuicChannelFactory;
32 class SecureChannelFactory;
34 // JingleSessionManager and JingleSession implement the subset of the
35 // Jingle protocol used in Chromoting. Instances of this class are
36 // created by the JingleSessionManager.
37 class JingleSession : public base::NonThreadSafe,
38 public Session,
39 public DatagramChannelFactory,
40 public Transport::EventHandler {
41 public:
42 ~JingleSession() override;
44 // Session interface.
45 void SetEventHandler(Session::EventHandler* event_handler) override;
46 ErrorCode error() override;
47 const std::string& jid() override;
48 const SessionConfig& config() override;
49 StreamChannelFactory* GetTransportChannelFactory() override;
50 StreamChannelFactory* GetMultiplexedChannelFactory() override;
51 StreamChannelFactory* GetQuicChannelFactory() 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;
70 private:
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);
82 // Passes transport info to a new |channel| in case it was received before the
83 // channel was created.
84 void AddPendingRemoteTransportInfo(Transport* channel);
86 // Called by JingleSessionManager for incoming connections.
87 void InitializeIncomingConnection(const JingleMessage& initiate_message,
88 scoped_ptr<Authenticator> authenticator);
89 void AcceptIncomingConnection(const JingleMessage& initiate_message);
91 // Sends |message| to the peer. The session is closed if the send fails or no
92 // response is received within a reasonable time. All other responses are
93 // ignored.
94 void SendMessage(const JingleMessage& message);
96 // Iq response handler.
97 void OnMessageResponse(JingleMessage::ActionType request_type,
98 IqRequest* request,
99 const buzz::XmlElement* response);
101 // Creates empty |pending_transport_info_message_| and schedules timer for
102 // SentTransportInfo() to sent the message later.
103 void EnsurePendingTransportInfoMessage();
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
115 // messages.
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 // Called when authentication is finished.
141 void OnAuthenticated();
143 // Terminates the session and sends session-terminate if it is
144 // necessary. |error| specifies the error code in case when the
145 // session is being closed due to an error.
146 void CloseInternal(ErrorCode error);
148 // Sets |state_| to |new_state| and calls state change callback.
149 void SetState(State new_state);
151 // Returns true if the state of the session is not CLOSED or FAILED
152 bool is_session_active();
154 JingleSessionManager* session_manager_;
155 std::string peer_jid_;
156 Session::EventHandler* event_handler_;
158 std::string session_id_;
159 State state_;
160 ErrorCode error_;
162 scoped_ptr<SessionConfig> config_;
164 scoped_ptr<Authenticator> authenticator_;
166 // Pending Iq requests. Used for all messages except transport-info.
167 std::set<IqRequest*> pending_requests_;
169 // Pending transport-info requests.
170 std::list<IqRequest*> transport_info_requests_;
172 ChannelsMap channels_;
173 scoped_ptr<PseudoTcpChannelFactory> pseudotcp_channel_factory_;
174 scoped_ptr<SecureChannelFactory> secure_channel_factory_;
175 scoped_ptr<ChannelMultiplexer> channel_multiplexer_;
176 scoped_ptr<QuicChannelFactory> quic_channel_factory_;
178 scoped_ptr<JingleMessage> pending_transport_info_message_;
179 base::OneShotTimer<JingleSession> transport_info_timer_;
181 // Pending remote transport info received before the local channels were
182 // created.
183 std::list<JingleMessage::IceCredentials> pending_remote_ice_credentials_;
184 std::list<JingleMessage::NamedCandidate> pending_remote_candidates_;
186 base::WeakPtrFactory<JingleSession> weak_factory_;
188 DISALLOW_COPY_AND_ASSIGN(JingleSession);
191 } // namespace protocol
192 } // namespace remoting
194 #endif // REMOTING_PROTOCOL_JINGLE_SESSION_H_