Roll tools/swarming_client/ to b61a1802f5ef4bb8c7b81060cc80add47e6cf302.
[chromium-blink-merge.git] / remoting / protocol / jingle_session.h
blobeb30e50ffdc9c4c9e5cce6912394db1ac025b8b9
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 net {
26 class Socket;
27 class StreamSocket;
28 } // namespace net
30 namespace remoting {
31 namespace protocol {
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,
42 public Session,
43 public DatagramChannelFactory,
44 public Transport::EventHandler {
45 public:
46 ~JingleSession() override;
48 // Session interface.
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(const 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 OnTransportCandidate(Transport* transport,
66 const cricket::Candidate& candidate) override;
67 void OnTransportRouteChange(Transport* transport,
68 const TransportRoute& route) override;
69 void OnTransportFailed(Transport* transport) override;
70 void OnTransportDeleted(Transport* transport) override;
72 private:
73 friend class JingleSessionManager;
75 typedef std::map<std::string, Transport*> ChannelsMap;
76 typedef base::Callback<void(JingleMessageReply::ErrorType)> ReplyCallback;
78 explicit JingleSession(JingleSessionManager* session_manager);
80 // Start connection by sending session-initiate message.
81 void StartConnection(const std::string& peer_jid,
82 scoped_ptr<Authenticator> authenticator,
83 scoped_ptr<CandidateSessionConfig> config);
85 // Adds to a new channel the remote candidates received before it was created.
86 void AddPendingRemoteCandidates(Transport* channel, const std::string& name);
88 // Called by JingleSessionManager for incoming connections.
89 void InitializeIncomingConnection(const JingleMessage& initiate_message,
90 scoped_ptr<Authenticator> authenticator);
91 void AcceptIncomingConnection(const JingleMessage& initiate_message);
93 // Sends |message| to the peer. The session is closed if the send fails or no
94 // response is received within a reasonable time. All other responses are
95 // ignored.
96 void SendMessage(const JingleMessage& message);
98 // Iq response handler.
99 void OnMessageResponse(JingleMessage::ActionType request_type,
100 IqRequest* request,
101 const buzz::XmlElement* response);
103 // Sends transport-info message with candidates from |pending_candidates_|.
104 void SendTransportInfo();
106 // Response handler for transport-info responses. Transport-info timeouts are
107 // ignored and don't terminate connection.
108 void OnTransportInfoResponse(IqRequest* request,
109 const buzz::XmlElement* response);
111 // Called by JingleSessionManager on incoming |message|. Must call
112 // |reply_callback| to send reply message before sending any other
113 // messages.
114 void OnIncomingMessage(const JingleMessage& message,
115 const ReplyCallback& reply_callback);
117 // Message handlers for incoming messages.
118 void OnAccept(const JingleMessage& message,
119 const ReplyCallback& reply_callback);
120 void OnSessionInfo(const JingleMessage& message,
121 const ReplyCallback& reply_callback);
122 void OnTerminate(const JingleMessage& message,
123 const ReplyCallback& reply_callback);
124 void ProcessTransportInfo(const JingleMessage& message);
126 // Called from OnAccept() to initialize session config.
127 bool InitializeConfigFromDescription(const ContentDescription* description);
129 // Called after the initial incoming authenticator message is processed.
130 void ContinueAcceptIncomingConnection();
132 // Called after subsequent authenticator messages are processed.
133 void ProcessAuthenticationStep();
135 // Called after the authenticating step is finished.
136 void ContinueAuthenticationStep();
138 // Called when authentication is finished.
139 void OnAuthenticated();
141 // Terminates the session and sends session-terminate if it is
142 // necessary. |error| specifies the error code in case when the
143 // session is being closed due to an error.
144 void CloseInternal(ErrorCode error);
146 // Sets |state_| to |new_state| and calls state change callback.
147 void SetState(State new_state);
149 // Returns true if the state of the session is not CLOSED or FAILED
150 bool is_session_active();
152 JingleSessionManager* session_manager_;
153 std::string peer_jid_;
154 scoped_ptr<CandidateSessionConfig> candidate_config_;
155 Session::EventHandler* event_handler_;
157 std::string session_id_;
158 State state_;
159 ErrorCode error_;
161 SessionConfig config_;
162 bool config_is_set_;
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_;
177 base::OneShotTimer<JingleSession> transport_infos_timer_;
178 std::list<JingleMessage::NamedCandidate> pending_candidates_;
180 // Pending remote candidates, received before the local channels were created.
181 std::list<JingleMessage::NamedCandidate> pending_remote_candidates_;
183 base::WeakPtrFactory<JingleSession> weak_factory_;
185 DISALLOW_COPY_AND_ASSIGN(JingleSession);
188 } // namespace protocol
189 } // namespace remoting
191 #endif // REMOTING_PROTOCOL_JINGLE_SESSION_H_