Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / remoting / protocol / jingle_session.h
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
25 namespace net {
26 class Socket;
27 class StreamSocket;
28 }  // namespace net
29
30 namespace remoting {
31 namespace protocol {
32
33 class SecureChannelFactory;
34 class ChannelMultiplexer;
35 class JingleSessionManager;
36 class 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.
41 class JingleSession : public base::NonThreadSafe,
42                       public Session,
43                       public DatagramChannelFactory,
44                       public Transport::EventHandler {
45  public:
46   ~JingleSession() override;
47
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;
58
59   // DatagramChannelFactory interface.
60   void CreateChannel(const std::string& name,
61                      const ChannelCreatedCallback& callback) override;
62   void CancelChannelCreation(const std::string& name) override;
63
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;
71
72  private:
73   friend class JingleSessionManager;
74
75   typedef std::map<std::string, Transport*> ChannelsMap;
76   typedef base::Callback<void(JingleMessageReply::ErrorType)> ReplyCallback;
77
78   explicit JingleSession(JingleSessionManager* session_manager);
79
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);
84
85   // Adds to a new channel the remote candidates received before it was created.
86   void AddPendingRemoteCandidates(Transport* channel, const std::string& name);
87
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);
92
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);
97
98   // Iq response handler.
99   void OnMessageResponse(JingleMessage::ActionType request_type,
100                          IqRequest* request,
101                          const buzz::XmlElement* response);
102
103   // Sends transport-info message with candidates from |pending_candidates_|.
104   void SendTransportInfo();
105
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);
110
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);
116
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);
125
126   // Called from OnAccept() to initialize session config.
127   bool InitializeConfigFromDescription(const ContentDescription* description);
128
129   // Called after the initial incoming authenticator message is processed.
130   void ContinueAcceptIncomingConnection();
131
132   // Called after subsequent authenticator messages are processed.
133   void ProcessAuthenticationStep();
134
135   // Called after the authenticating step is finished.
136   void ContinueAuthenticationStep();
137
138   // Called when authentication is finished.
139   void OnAuthenticated();
140
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);
145
146   // Sets |state_| to |new_state| and calls state change callback.
147   void SetState(State new_state);
148
149   // Returns true if the state of the session is not CLOSED or FAILED
150   bool is_session_active();
151
152   JingleSessionManager* session_manager_;
153   std::string peer_jid_;
154   scoped_ptr<CandidateSessionConfig> candidate_config_;
155   Session::EventHandler* event_handler_;
156
157   std::string session_id_;
158   State state_;
159   ErrorCode error_;
160
161   SessionConfig config_;
162   bool config_is_set_;
163
164   scoped_ptr<Authenticator> authenticator_;
165
166   // Pending Iq requests. Used for all messages except transport-info.
167   std::set<IqRequest*> pending_requests_;
168
169   // Pending transport-info requests.
170   std::list<IqRequest*> transport_info_requests_;
171
172   ChannelsMap channels_;
173   scoped_ptr<PseudoTcpChannelFactory> pseudotcp_channel_factory_;
174   scoped_ptr<SecureChannelFactory> secure_channel_factory_;
175   scoped_ptr<ChannelMultiplexer> channel_multiplexer_;
176
177   base::OneShotTimer<JingleSession> transport_infos_timer_;
178   std::list<JingleMessage::NamedCandidate> pending_candidates_;
179
180   // Pending remote candidates, received before the local channels were created.
181   std::list<JingleMessage::NamedCandidate> pending_remote_candidates_;
182
183   base::WeakPtrFactory<JingleSession> weak_factory_;
184
185   DISALLOW_COPY_AND_ASSIGN(JingleSession);
186 };
187
188 }  // namespace protocol
189 }  // namespace remoting
190
191 #endif  // REMOTING_PROTOCOL_JINGLE_SESSION_H_