Add long running gmail memory benchmark for background tab.
[chromium-blink-merge.git] / remoting / protocol / jingle_session.h
blobe3c661c0150ff70cca76d0997a78649dcb5ffb91
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 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,
37 public Session,
38 public DatagramChannelFactory,
39 public Transport::EventHandler {
40 public:
41 ~JingleSession() override;
43 // Session interface.
44 void SetEventHandler(Session::EventHandler* event_handler) override;
45 ErrorCode error() override;
46 const std::string& jid() override;
47 const SessionConfig& config() override;
48 StreamChannelFactory* GetTransportChannelFactory() override;
49 StreamChannelFactory* GetMultiplexedChannelFactory() override;
50 void Close() override;
52 // DatagramChannelFactory interface.
53 void CreateChannel(const std::string& name,
54 const ChannelCreatedCallback& callback) override;
55 void CancelChannelCreation(const std::string& name) override;
57 // Transport::EventHandler interface.
58 void OnTransportIceCredentials(Transport* transport,
59 const std::string& ufrag,
60 const std::string& password) override;
61 void OnTransportCandidate(Transport* transport,
62 const cricket::Candidate& candidate) override;
63 void OnTransportRouteChange(Transport* transport,
64 const TransportRoute& route) override;
65 void OnTransportFailed(Transport* transport) override;
66 void OnTransportDeleted(Transport* transport) override;
68 private:
69 friend class JingleSessionManager;
71 typedef std::map<std::string, Transport*> ChannelsMap;
72 typedef base::Callback<void(JingleMessageReply::ErrorType)> ReplyCallback;
74 explicit JingleSession(JingleSessionManager* session_manager);
76 // Start connection by sending session-initiate message.
77 void StartConnection(const std::string& peer_jid,
78 scoped_ptr<Authenticator> authenticator);
80 // Passes transport info to a new |channel| in case it was received before the
81 // channel was created.
82 void AddPendingRemoteTransportInfo(Transport* channel);
84 // Called by JingleSessionManager for incoming connections.
85 void InitializeIncomingConnection(const JingleMessage& initiate_message,
86 scoped_ptr<Authenticator> authenticator);
87 void AcceptIncomingConnection(const JingleMessage& initiate_message);
89 // Sends |message| to the peer. The session is closed if the send fails or no
90 // response is received within a reasonable time. All other responses are
91 // ignored.
92 void SendMessage(const JingleMessage& message);
94 // Iq response handler.
95 void OnMessageResponse(JingleMessage::ActionType request_type,
96 IqRequest* request,
97 const buzz::XmlElement* response);
99 // Creates empty |pending_transport_info_message_| and schedules timer for
100 // SentTransportInfo() to sent the message later.
101 void EnsurePendingTransportInfoMessage();
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 Session::EventHandler* event_handler_;
156 std::string session_id_;
157 State state_;
158 ErrorCode error_;
160 scoped_ptr<SessionConfig> config_;
162 scoped_ptr<Authenticator> authenticator_;
164 // Pending Iq requests. Used for all messages except transport-info.
165 std::set<IqRequest*> pending_requests_;
167 // Pending transport-info requests.
168 std::list<IqRequest*> transport_info_requests_;
170 ChannelsMap channels_;
171 scoped_ptr<PseudoTcpChannelFactory> pseudotcp_channel_factory_;
172 scoped_ptr<SecureChannelFactory> secure_channel_factory_;
173 scoped_ptr<ChannelMultiplexer> channel_multiplexer_;
175 scoped_ptr<JingleMessage> pending_transport_info_message_;
176 base::OneShotTimer<JingleSession> transport_info_timer_;
178 // Pending remote transport info received before the local channels were
179 // created.
180 std::list<JingleMessage::IceCredentials> pending_remote_ice_credentials_;
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_