Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / remoting / protocol / jingle_session.h
blob3b704fb359b91272bc3123429739f1ebfe6aaae6
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/jingle_glue/iq_sender.h"
18 #include "remoting/protocol/authenticator.h"
19 #include "remoting/protocol/channel_factory.h"
20 #include "remoting/protocol/jingle_messages.h"
21 #include "remoting/protocol/session.h"
22 #include "remoting/protocol/session_config.h"
23 #include "remoting/protocol/transport.h"
25 namespace net {
26 class Socket;
27 class StreamSocket;
28 } // namespace net
30 namespace remoting {
31 namespace protocol {
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 {
42 public:
43 virtual ~JingleSession();
45 // Session interface.
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(
67 Transport* transport,
68 const cricket::Candidate& candidate) OVERRIDE;
69 virtual void OnTransportRouteChange(Transport* transport,
70 const TransportRoute& route) OVERRIDE;
71 virtual void OnTransportReady(Transport* transport,
72 bool ready) OVERRIDE;
73 virtual void OnTransportFailed(Transport* transport) OVERRIDE;
74 virtual void OnTransportDeleted(Transport* transport) OVERRIDE;
76 private:
77 friend class JingleSessionManager;
79 typedef std::map<std::string, Transport*> ChannelsMap;
80 typedef base::Callback<void(JingleMessageReply::ErrorType)> ReplyCallback;
82 explicit JingleSession(JingleSessionManager* session_manager);
84 // Start connection by sending session-initiate message.
85 void StartConnection(const std::string& peer_jid,
86 scoped_ptr<Authenticator> authenticator,
87 scoped_ptr<CandidateSessionConfig> config);
89 // Adds to a new channel the remote candidates received before it was created.
90 void AddPendingRemoteCandidates(Transport* channel, const std::string& name);
92 // Called by JingleSessionManager for incoming connections.
93 void InitializeIncomingConnection(const JingleMessage& initiate_message,
94 scoped_ptr<Authenticator> authenticator);
95 void AcceptIncomingConnection(const JingleMessage& initiate_message);
97 // Sends |message| to the peer. The session is closed if the send fails or no
98 // response is received within a reasonable time. All other responses are
99 // ignored.
100 void SendMessage(const JingleMessage& message);
102 // Iq response handler.
103 void OnMessageResponse(JingleMessage::ActionType request_type,
104 IqRequest* request,
105 const buzz::XmlElement* response);
107 // Sends transport-info message with candidates from |pending_candidates_|.
108 void SendTransportInfo();
110 // Response handler for transport-info responses. Transport-info timeouts are
111 // ignored and don't terminate connection.
112 void OnTransportInfoResponse(IqRequest* request,
113 const buzz::XmlElement* response);
115 // Called by JingleSessionManager on incoming |message|. Must call
116 // |reply_callback| to send reply message before sending any other
117 // messages.
118 void OnIncomingMessage(const JingleMessage& message,
119 const ReplyCallback& reply_callback);
121 // Message handlers for incoming messages.
122 void OnAccept(const JingleMessage& message,
123 const ReplyCallback& reply_callback);
124 void OnSessionInfo(const JingleMessage& message,
125 const ReplyCallback& reply_callback);
126 void OnTerminate(const JingleMessage& message,
127 const ReplyCallback& reply_callback);
128 void ProcessTransportInfo(const JingleMessage& message);
130 // Called from OnAccept() to initialize session config.
131 bool InitializeConfigFromDescription(const ContentDescription* description);
133 // Called after the initial incoming authenticator message is processed.
134 void ContinueAcceptIncomingConnection();
136 // Called after subsequent authenticator messages are processed.
137 void ProcessAuthenticationStep();
139 // Called after the authenticating step is finished.
140 void ContinueAuthenticationStep();
142 // Terminates the session and sends session-terminate if it is
143 // necessary. |error| specifies the error code in case when the
144 // session is being closed due to an error.
145 void CloseInternal(ErrorCode error);
147 // Sets |state_| to |new_state| and calls state change callback.
148 void SetState(State new_state);
150 // Returns true if the state of the session is not CLOSED or FAILED
151 bool is_session_active();
153 JingleSessionManager* session_manager_;
154 std::string peer_jid_;
155 scoped_ptr<CandidateSessionConfig> candidate_config_;
156 Session::EventHandler* event_handler_;
158 std::string session_id_;
159 State state_;
160 ErrorCode error_;
162 SessionConfig config_;
163 bool config_is_set_;
165 scoped_ptr<Authenticator> authenticator_;
167 // Pending Iq requests. Used for all messages except transport-info.
168 std::set<IqRequest*> pending_requests_;
170 // Pending transport-info requests.
171 std::list<IqRequest*> transport_info_requests_;
173 ChannelsMap channels_;
174 scoped_ptr<ChannelMultiplexer> channel_multiplexer_;
176 base::OneShotTimer<JingleSession> transport_infos_timer_;
177 std::list<JingleMessage::NamedCandidate> pending_candidates_;
179 // Pending remote candidates, received before the local channels were created.
180 std::list<JingleMessage::NamedCandidate> pending_remote_candidates_;
182 base::WeakPtrFactory<JingleSession> weak_factory_;
184 DISALLOW_COPY_AND_ASSIGN(JingleSession);
187 } // namespace protocol
188 } // namespace remoting
190 #endif // REMOTING_PROTOCOL_JINGLE_SESSION_H_