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 <string>
9 #include <vector>
10 
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/rand_util.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/threading/thread_checker.h"
16 #include "base/timer/timer.h"
17 #include "crypto/rsa_private_key.h"
18 #include "remoting/protocol/authenticator.h"
19 #include "remoting/protocol/datagram_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/signaling/iq_sender.h"
24 
25 namespace remoting {
26 namespace protocol {
27 
28 class JingleSessionManager;
29 class Transport;
30 
31 // JingleSessionManager and JingleSession implement the subset of the
32 // Jingle protocol used in Chromoting. Instances of this class are
33 // created by the JingleSessionManager.
34 class JingleSession : public Session {
35  public:
36   ~JingleSession() override;
37 
38   // Session interface.
39   void SetEventHandler(Session::EventHandler* event_handler) override;
40   ErrorCode error() override;
41   const std::string& jid() override;
42   const SessionConfig& config() override;
43   void SetTransport(Transport* transport) override;
44   void Close(protocol::ErrorCode error) override;
45   void AddPlugin(SessionPlugin* plugin) override;
46 
47  private:
48   friend class JingleSessionManager;
49 
50   typedef base::RepeatingCallback<void(JingleMessageReply::ErrorType)>
51       ReplyCallback;
52 
53   explicit JingleSession(JingleSessionManager* session_manager);
54 
55   // Start connection by sending session-initiate message.
56   void StartConnection(const SignalingAddress& peer_address,
57                        std::unique_ptr<Authenticator> authenticator);
58 
59   // Called by JingleSessionManager for incoming connections.
60   void InitializeIncomingConnection(
61       const std::string& message_id,
62       const JingleMessage& initiate_message,
63       std::unique_ptr<Authenticator> authenticator);
64   void AcceptIncomingConnection(const JingleMessage& initiate_message);
65 
66   // Callback for Transport interface to send transport-info messages.
67   void SendTransportInfo(std::unique_ptr<jingle_xmpp::XmlElement> transport_info);
68 
69   // Sends |message| to the peer. The session is closed if the send fails or no
70   // response is received within a reasonable time. All other responses are
71   // ignored.
72   void SendMessage(std::unique_ptr<JingleMessage> message);
73 
74   // Iq response handler.
75   void OnMessageResponse(JingleMessage::ActionType request_type,
76                          IqRequest* request,
77                          const jingle_xmpp::XmlElement* response);
78 
79   // Response handler for transport-info responses. Transport-info timeouts are
80   // ignored and don't terminate connection.
81   void OnTransportInfoResponse(IqRequest* request,
82                                const jingle_xmpp::XmlElement* response);
83 
84   // Called by JingleSessionManager on incoming |message|. Must call
85   // |reply_callback| to send reply message before sending any other
86   // messages.
87   void OnIncomingMessage(const std::string& id,
88                          std::unique_ptr<JingleMessage> message,
89                          const ReplyCallback& reply_callback);
90 
91   // Called by OnIncomingMessage() to process the incoming Jingle messages
92   // in the same order that they are sent.
93   void ProcessIncomingMessage(std::unique_ptr<JingleMessage> message,
94                               const ReplyCallback& reply_callback);
95 
96   // Message handlers for incoming messages.
97   void OnAccept(std::unique_ptr<JingleMessage> message,
98                 const ReplyCallback& reply_callback);
99   void OnSessionInfo(std::unique_ptr<JingleMessage> message,
100                      const ReplyCallback& reply_callback);
101   void OnTransportInfo(std::unique_ptr<JingleMessage> message,
102                        const ReplyCallback& reply_callback);
103   void OnTerminate(std::unique_ptr<JingleMessage> message,
104                    const ReplyCallback& reply_callback);
105 
106   // Called from OnAccept() to initialize session config.
107   bool InitializeConfigFromDescription(const ContentDescription* description);
108 
109   // Called after the initial incoming authenticator message is processed.
110   void ContinueAcceptIncomingConnection();
111 
112   // Called after subsequent authenticator messages are processed.
113   void ProcessAuthenticationStep();
114 
115   // Called when authentication is finished.
116   void OnAuthenticated();
117 
118   // Sets |state_| to |new_state| and calls state change callback.
119   void SetState(State new_state);
120 
121   // Returns true if the state of the session is not CLOSED or FAILED
122   bool is_session_active();
123 
124   // Executes all plugins against incoming JingleMessage.
125   void ProcessIncomingPluginMessage(const JingleMessage& message);
126 
127   // Executes all plugins against outgoing JingleMessage.
128   void AddPluginAttachments(JingleMessage* message);
129 
130   // Sends session-initiate message.
131   void SendSessionInitiateMessage();
132 
133   // Returns the value of the ID attribute of the next outgoing set IQ with the
134   // sequence ID encoded.
135   std::string GetNextOutgoingId();
136 
137   base::ThreadChecker thread_checker_;
138 
139   JingleSessionManager* session_manager_;
140   SignalingAddress peer_address_;
141   Session::EventHandler* event_handler_;
142 
143   std::string session_id_;
144   State state_;
145   ErrorCode error_;
146 
147   std::unique_ptr<SessionConfig> config_;
148 
149   std::unique_ptr<Authenticator> authenticator_;
150 
151   Transport* transport_ = nullptr;
152 
153   // Pending Iq requests. Used for all messages except transport-info.
154   std::vector<std::unique_ptr<IqRequest>> pending_requests_;
155 
156   // Pending transport-info requests.
157   std::vector<std::unique_ptr<IqRequest>> transport_info_requests_;
158 
159   struct PendingMessage {
160     PendingMessage();
161     PendingMessage(PendingMessage&& moved);
162     PendingMessage(std::unique_ptr<JingleMessage> message,
163                    const ReplyCallback& reply_callback);
164     ~PendingMessage();
165     PendingMessage& operator=(PendingMessage&& moved);
166     std::unique_ptr<JingleMessage> message;
167     ReplyCallback reply_callback;
168   };
169 
170   // A message queue to guarantee the incoming messages are processed in order.
171   class OrderedMessageQueue;
172   std::unique_ptr<OrderedMessageQueue> message_queue_;
173 
174   // This prefix is necessary to disambiguate between the ID's sent from the
175   // client and the ID's sent from the host.
176   std::string outgoing_id_prefix_ = base::NumberToString(base::RandUint64());
177   int next_outgoing_id_ = 0;
178 
179   // Transport info messages that are received while the session is being
180   // authenticated.
181   std::vector<PendingMessage> pending_transport_info_;
182 
183   // The SessionPlugins attached to this session.
184   std::vector<SessionPlugin*> plugins_;
185 
186   base::WeakPtrFactory<JingleSession> weak_factory_{this};
187 
188   DISALLOW_COPY_AND_ASSIGN(JingleSession);
189 };
190 
191 }  // namespace protocol
192 }  // namespace remoting
193 
194 #endif  // REMOTING_PROTOCOL_JINGLE_SESSION_H_
195