[Android] Fixed chrome://history's overlapped search and clear(x) icons on RTL
[chromium-blink-merge.git] / remoting / protocol / jingle_session.h
blob1b7f17e36d68cd0854e205f7a89d55705e8a8beb
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.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 OnTransportDeleted(Transport* transport) OVERRIDE;
75 private:
76 friend class JingleSessionManager;
78 typedef std::map<std::string, Transport*> ChannelsMap;
79 typedef base::Callback<void(JingleMessageReply::ErrorType)> ReplyCallback;
81 explicit JingleSession(JingleSessionManager* session_manager);
83 // Start connection by sending session-initiate message.
84 void StartConnection(const std::string& peer_jid,
85 scoped_ptr<Authenticator> authenticator,
86 scoped_ptr<CandidateSessionConfig> config);
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);
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);
98 // Iq response handler.
99 void OnMessageResponse(JingleMessage::ActionType request_type,
100 IqRequest* request,
101 const buzz::XmlElement* response);
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();
131 // Called after subsequent authenticator messages are processed.
132 void ProcessAuthenticationStep();
134 // Terminates the session and sends session-terminate if it is
135 // necessary. |error| specifies the error code in case when the
136 // session is being closed due to an error.
137 void CloseInternal(ErrorCode error);
139 // Sets |state_| to |new_state| and calls state change callback.
140 void SetState(State new_state);
142 JingleSessionManager* session_manager_;
143 std::string peer_jid_;
144 scoped_ptr<CandidateSessionConfig> candidate_config_;
145 Session::EventHandler* event_handler_;
147 std::string session_id_;
148 State state_;
149 ErrorCode error_;
151 SessionConfig config_;
152 bool config_is_set_;
154 scoped_ptr<Authenticator> authenticator_;
156 // Pending Iq requests. Used for all messages except transport-info.
157 std::set<IqRequest*> pending_requests_;
159 // Pending transport-info requests.
160 std::list<IqRequest*> transport_info_requests_;
162 ChannelsMap channels_;
163 scoped_ptr<ChannelMultiplexer> channel_multiplexer_;
165 base::OneShotTimer<JingleSession> transport_infos_timer_;
166 std::list<JingleMessage::NamedCandidate> pending_candidates_;
168 DISALLOW_COPY_AND_ASSIGN(JingleSession);
171 } // namespace protocol
172 } // namespace remoting
174 #endif // REMOTING_PROTOCOL_JINGLE_SESSION_H_