Upstream version 7.36.149.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/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"
24
25 namespace net {
26 class Socket;
27 class StreamSocket;
28 }  // namespace net
29
30 namespace remoting {
31 namespace protocol {
32
33 class ChannelMultiplexer;
34 class JingleSessionManager;
35
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();
44
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;
55
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;
64
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 OnTransportFailed(Transport* transport) OVERRIDE;
72   virtual void OnTransportDeleted(Transport* transport) OVERRIDE;
73
74  private:
75   friend class JingleSessionManager;
76
77   typedef std::map<std::string, Transport*> ChannelsMap;
78   typedef base::Callback<void(JingleMessageReply::ErrorType)> ReplyCallback;
79
80   explicit JingleSession(JingleSessionManager* session_manager);
81
82   // Start connection by sending session-initiate message.
83   void StartConnection(const std::string& peer_jid,
84                        scoped_ptr<Authenticator> authenticator,
85                        scoped_ptr<CandidateSessionConfig> config);
86
87   // Adds to a new channel the remote candidates received before it was created.
88   void AddPendingRemoteCandidates(Transport* channel, const std::string& name);
89
90   // Called by JingleSessionManager for incoming connections.
91   void InitializeIncomingConnection(const JingleMessage& initiate_message,
92                                     scoped_ptr<Authenticator> authenticator);
93   void AcceptIncomingConnection(const JingleMessage& initiate_message);
94
95   // Sends |message| to the peer. The session is closed if the send fails or no
96   // response is received within a reasonable time. All other responses are
97   // ignored.
98   void SendMessage(const JingleMessage& message);
99
100   // Iq response handler.
101   void OnMessageResponse(JingleMessage::ActionType request_type,
102                          IqRequest* request,
103                          const buzz::XmlElement* response);
104
105   // Sends transport-info message with candidates from |pending_candidates_|.
106   void SendTransportInfo();
107
108   // Response handler for transport-info responses. Transport-info timeouts are
109   // ignored and don't terminate connection.
110   void OnTransportInfoResponse(IqRequest* request,
111                                const buzz::XmlElement* response);
112
113   // Called by JingleSessionManager on incoming |message|. Must call
114   // |reply_callback| to send reply message before sending any other
115   // messages.
116   void OnIncomingMessage(const JingleMessage& message,
117                          const ReplyCallback& reply_callback);
118
119   // Message handlers for incoming messages.
120   void OnAccept(const JingleMessage& message,
121                 const ReplyCallback& reply_callback);
122   void OnSessionInfo(const JingleMessage& message,
123                      const ReplyCallback& reply_callback);
124   void OnTerminate(const JingleMessage& message,
125                    const ReplyCallback& reply_callback);
126   void ProcessTransportInfo(const JingleMessage& message);
127
128   // Called from OnAccept() to initialize session config.
129   bool InitializeConfigFromDescription(const ContentDescription* description);
130
131   // Called after the initial incoming authenticator message is processed.
132   void ContinueAcceptIncomingConnection();
133
134   // Called after subsequent authenticator messages are processed.
135   void ProcessAuthenticationStep();
136
137   // Called after the authenticating step is finished.
138   void ContinueAuthenticationStep();
139
140   // Terminates the session and sends session-terminate if it is
141   // necessary. |error| specifies the error code in case when the
142   // session is being closed due to an error.
143   void CloseInternal(ErrorCode error);
144
145   // Sets |state_| to |new_state| and calls state change callback.
146   void SetState(State new_state);
147
148   // Returns true if the state of the session is not CLOSED or FAILED
149   bool is_session_active();
150
151   JingleSessionManager* session_manager_;
152   std::string peer_jid_;
153   scoped_ptr<CandidateSessionConfig> candidate_config_;
154   Session::EventHandler* event_handler_;
155
156   std::string session_id_;
157   State state_;
158   ErrorCode error_;
159
160   SessionConfig config_;
161   bool config_is_set_;
162
163   scoped_ptr<Authenticator> authenticator_;
164
165   // Pending Iq requests. Used for all messages except transport-info.
166   std::set<IqRequest*> pending_requests_;
167
168   // Pending transport-info requests.
169   std::list<IqRequest*> transport_info_requests_;
170
171   ChannelsMap channels_;
172   scoped_ptr<ChannelMultiplexer> channel_multiplexer_;
173
174   base::OneShotTimer<JingleSession> transport_infos_timer_;
175   std::list<JingleMessage::NamedCandidate> pending_candidates_;
176
177   // Pending remote candidates, received before the local channels were created.
178   std::list<JingleMessage::NamedCandidate> pending_remote_candidates_;
179
180   base::WeakPtrFactory<JingleSession> weak_factory_;
181
182   DISALLOW_COPY_AND_ASSIGN(JingleSession);
183 };
184
185 }  // namespace protocol
186 }  // namespace remoting
187
188 #endif  // REMOTING_PROTOCOL_JINGLE_SESSION_H_