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.
4
5#ifndef REMOTING_PROTOCOL_JINGLE_SESSION_H_
6#define REMOTING_PROTOCOL_JINGLE_SESSION_H_
7
8#include <list>
9#include <map>
10#include <set>
11#include <string>
12
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"
24
25namespace net {
26class Socket;
27class StreamSocket;
28}  // namespace net
29
30namespace remoting {
31namespace protocol {
32
33class SecureChannelFactory;
34class ChannelMultiplexer;
35class JingleSessionManager;
36class PseudoTcpChannelFactory;
37
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.
41class JingleSession : public base::NonThreadSafe,
42                      public Session,
43                      public DatagramChannelFactory,
44                      public Transport::EventHandler {
45 public:
46  virtual ~JingleSession();
47
48  // Session interface.
49  virtual void SetEventHandler(Session::EventHandler* event_handler) OVERRIDE;
50  virtual ErrorCode error() OVERRIDE;
51  virtual const std::string& jid() OVERRIDE;
52  virtual const CandidateSessionConfig* candidate_config() OVERRIDE;
53  virtual const SessionConfig& config() OVERRIDE;
54  virtual void set_config(const SessionConfig& config) OVERRIDE;
55  virtual StreamChannelFactory* GetTransportChannelFactory() OVERRIDE;
56  virtual StreamChannelFactory* GetMultiplexedChannelFactory() OVERRIDE;
57  virtual void Close() OVERRIDE;
58
59  // DatagramChannelFactory interface.
60  virtual void CreateChannel(const std::string& name,
61                             const ChannelCreatedCallback& callback) OVERRIDE;
62  virtual void CancelChannelCreation(const std::string& name) OVERRIDE;
63
64  // Transport::EventHandler interface.
65  virtual void OnTransportCandidate(
66      Transport* transport,
67      const cricket::Candidate& candidate) OVERRIDE;
68  virtual void OnTransportRouteChange(Transport* transport,
69                                      const TransportRoute& route) OVERRIDE;
70  virtual void OnTransportFailed(Transport* transport) OVERRIDE;
71  virtual void OnTransportDeleted(Transport* transport) OVERRIDE;
72
73 private:
74  friend class JingleSessionManager;
75
76  typedef std::map<std::string, Transport*> ChannelsMap;
77  typedef base::Callback<void(JingleMessageReply::ErrorType)> ReplyCallback;
78
79  explicit JingleSession(JingleSessionManager* session_manager);
80
81  // Start connection by sending session-initiate message.
82  void StartConnection(const std::string& peer_jid,
83                       scoped_ptr<Authenticator> authenticator,
84                       scoped_ptr<CandidateSessionConfig> config);
85
86  // Adds to a new channel the remote candidates received before it was created.
87  void AddPendingRemoteCandidates(Transport* channel, const std::string& name);
88
89  // Called by JingleSessionManager for incoming connections.
90  void InitializeIncomingConnection(const JingleMessage& initiate_message,
91                                    scoped_ptr<Authenticator> authenticator);
92  void AcceptIncomingConnection(const JingleMessage& initiate_message);
93
94  // Sends |message| to the peer. The session is closed if the send fails or no
95  // response is received within a reasonable time. All other responses are
96  // ignored.
97  void SendMessage(const JingleMessage& message);
98
99  // Iq response handler.
100  void OnMessageResponse(JingleMessage::ActionType request_type,
101                         IqRequest* request,
102                         const buzz::XmlElement* response);
103
104  // Sends transport-info message with candidates from |pending_candidates_|.
105  void SendTransportInfo();
106
107  // Response handler for transport-info responses. Transport-info timeouts are
108  // ignored and don't terminate connection.
109  void OnTransportInfoResponse(IqRequest* request,
110                               const buzz::XmlElement* response);
111
112  // Called by JingleSessionManager on incoming |message|. Must call
113  // |reply_callback| to send reply message before sending any other
114  // messages.
115  void OnIncomingMessage(const JingleMessage& message,
116                         const ReplyCallback& reply_callback);
117
118  // Message handlers for incoming messages.
119  void OnAccept(const JingleMessage& message,
120                const ReplyCallback& reply_callback);
121  void OnSessionInfo(const JingleMessage& message,
122                     const ReplyCallback& reply_callback);
123  void OnTerminate(const JingleMessage& message,
124                   const ReplyCallback& reply_callback);
125  void ProcessTransportInfo(const JingleMessage& message);
126
127  // Called from OnAccept() to initialize session config.
128  bool InitializeConfigFromDescription(const ContentDescription* description);
129
130  // Called after the initial incoming authenticator message is processed.
131  void ContinueAcceptIncomingConnection();
132
133  // Called after subsequent authenticator messages are processed.
134  void ProcessAuthenticationStep();
135
136  // Called after the authenticating step is finished.
137  void ContinueAuthenticationStep();
138
139  // Called when authentication is finished.
140  void OnAuthenticated();
141
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);
146
147  // Sets |state_| to |new_state| and calls state change callback.
148  void SetState(State new_state);
149
150  // Returns true if the state of the session is not CLOSED or FAILED
151  bool is_session_active();
152
153  JingleSessionManager* session_manager_;
154  std::string peer_jid_;
155  scoped_ptr<CandidateSessionConfig> candidate_config_;
156  Session::EventHandler* event_handler_;
157
158  std::string session_id_;
159  State state_;
160  ErrorCode error_;
161
162  SessionConfig config_;
163  bool config_is_set_;
164
165  scoped_ptr<Authenticator> authenticator_;
166
167  // Pending Iq requests. Used for all messages except transport-info.
168  std::set<IqRequest*> pending_requests_;
169
170  // Pending transport-info requests.
171  std::list<IqRequest*> transport_info_requests_;
172
173  ChannelsMap channels_;
174  scoped_ptr<PseudoTcpChannelFactory> pseudotcp_channel_factory_;
175  scoped_ptr<SecureChannelFactory> secure_channel_factory_;
176  scoped_ptr<ChannelMultiplexer> channel_multiplexer_;
177
178  base::OneShotTimer<JingleSession> transport_infos_timer_;
179  std::list<JingleMessage::NamedCandidate> pending_candidates_;
180
181  // Pending remote candidates, received before the local channels were created.
182  std::list<JingleMessage::NamedCandidate> pending_remote_candidates_;
183
184  base::WeakPtrFactory<JingleSession> weak_factory_;
185
186  DISALLOW_COPY_AND_ASSIGN(JingleSession);
187};
188
189}  // namespace protocol
190}  // namespace remoting
191
192#endif  // REMOTING_PROTOCOL_JINGLE_SESSION_H_
193