Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / p2p / base / sessionmanager.h
1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #ifndef WEBRTC_P2P_BASE_SESSIONMANAGER_H_
12 #define WEBRTC_P2P_BASE_SESSIONMANAGER_H_
13
14 #include <map>
15 #include <string>
16 #include <utility>
17 #include <vector>
18
19 #include "webrtc/p2p/base/portallocator.h"
20 #include "webrtc/p2p/base/transportdescriptionfactory.h"
21 #include "webrtc/base/sigslot.h"
22 #include "webrtc/base/thread.h"
23
24 namespace buzz {
25 class QName;
26 class XmlElement;
27 }
28
29 namespace cricket {
30
31 class Session;
32 class BaseSession;
33 class SessionClient;
34
35 // SessionManager manages session instances.
36 class SessionManager : public sigslot::has_slots<> {
37  public:
38   SessionManager(PortAllocator *allocator,
39                  rtc::Thread *worker_thread = NULL);
40   virtual ~SessionManager();
41
42   PortAllocator *port_allocator() const { return allocator_; }
43   rtc::Thread *worker_thread() const { return worker_thread_; }
44   rtc::Thread *signaling_thread() const { return signaling_thread_; }
45
46   int session_timeout() const { return timeout_; }
47   void set_session_timeout(int timeout) { timeout_ = timeout; }
48
49   // Set what transport protocol we want to default to.
50   void set_transport_protocol(TransportProtocol proto) {
51      transport_desc_factory_.set_protocol(proto);
52   }
53
54   // Control use of DTLS. An identity must be supplied if DTLS is enabled.
55   void set_secure(SecurePolicy policy) {
56     transport_desc_factory_.set_secure(policy);
57   }
58   void set_identity(rtc::SSLIdentity* identity) {
59     transport_desc_factory_.set_identity(identity);
60   }
61   const TransportDescriptionFactory* transport_desc_factory() const {
62     return &transport_desc_factory_;
63   }
64
65   // Registers support for the given client.  If we receive an initiate
66   // describing a session of the given type, we will automatically create a
67   // Session object and notify this client.  The client may then accept or
68   // reject the session.
69   void AddClient(const std::string& content_type, SessionClient* client);
70   void RemoveClient(const std::string& content_type);
71   SessionClient* GetClient(const std::string& content_type);
72
73   // Creates a new session.  The given name is the JID of the client on whose
74   // behalf we initiate the session.
75   Session *CreateSession(const std::string& local_name,
76                          const std::string& content_type);
77
78   Session *CreateSession(const std::string& id,
79                          const std::string& local_name,
80                          const std::string& content_type);
81
82   // Destroys the given session.
83   void DestroySession(Session *session);
84
85   // Returns the session with the given ID or NULL if none exists.
86   Session *GetSession(const std::string& sid);
87
88   // Terminates all of the sessions created by this manager.
89   void TerminateAll();
90
91   // These are signaled whenever the set of existing sessions changes.
92   sigslot::signal2<Session *, bool> SignalSessionCreate;
93   sigslot::signal1<Session *> SignalSessionDestroy;
94
95   // Determines whether the given stanza is intended for some session.
96   bool IsSessionMessage(const buzz::XmlElement* stanza);
97
98   // Given a sid, initiator, and remote_name, this finds the matching Session
99   Session* FindSession(const std::string& sid,
100                        const std::string& remote_name);
101
102   // Called when we receive a stanza for which IsSessionMessage is true.
103   void OnIncomingMessage(const buzz::XmlElement* stanza);
104
105   // Called when we get a response to a message that we sent.
106   void OnIncomingResponse(const buzz::XmlElement* orig_stanza,
107                           const buzz::XmlElement* response_stanza);
108
109   // Called if an attempted to send times out or an error is returned.  In the
110   // timeout case error_stanza will be NULL
111   void OnFailedSend(const buzz::XmlElement* orig_stanza,
112                     const buzz::XmlElement* error_stanza);
113
114   // Signalled each time a session generates a signaling message to send.
115   // Also signalled on errors, but with a NULL session.
116   sigslot::signal2<SessionManager*,
117                    const buzz::XmlElement*> SignalOutgoingMessage;
118
119   // Signaled before sessions try to send certain signaling messages.  The
120   // client should call OnSignalingReady once it is safe to send them.  These
121   // steps are taken so that we don't send signaling messages trying to
122   // re-establish the connectivity of a session when the client cannot send
123   // the messages (and would probably just drop them on the floor).
124   //
125   // Note: you can connect this directly to OnSignalingReady(), if a signalling
126   // check is not supported.
127   sigslot::signal0<> SignalRequestSignaling;
128   void OnSignalingReady();
129
130   // Signaled when this SessionManager is deleted.
131   sigslot::signal0<> SignalDestroyed;
132
133  private:
134   typedef std::map<std::string, Session*> SessionMap;
135   typedef std::map<std::string, SessionClient*> ClientMap;
136
137   // Helper function for CreateSession.  This is also invoked when we receive
138   // a message attempting to initiate a session with this client.
139   Session *CreateSession(const std::string& local_name,
140                          const std::string& initiator,
141                          const std::string& sid,
142                          const std::string& content_type,
143                          bool received_initiate);
144
145   // Attempts to find a registered session type whose description appears as
146   // a child of the session element.  Such a child should be present indicating
147   // the application they hope to initiate.
148   std::string FindClient(const buzz::XmlElement* session);
149
150   // Sends a message back to the other client indicating that we found an error
151   // in the stanza they sent.  name identifies the error, type is one of the
152   // standard XMPP types (cancel, continue, modify, auth, wait), and text is a
153   // description for debugging purposes.
154   void SendErrorMessage(const buzz::XmlElement* stanza,
155                         const buzz::QName& name,
156                         const std::string& type,
157                         const std::string& text,
158                         const buzz::XmlElement* extra_info);
159
160   // Creates and returns an error message from the given components.  The
161   // caller is responsible for deleting this.
162   buzz::XmlElement* CreateErrorMessage(
163       const buzz::XmlElement* stanza,
164       const buzz::QName& name,
165       const std::string& type,
166       const std::string& text,
167       const buzz::XmlElement* extra_info);
168
169   // Called each time a session requests signaling.
170   void OnRequestSignaling(Session* session);
171
172   // Called each time a session has an outgoing message.
173   void OnOutgoingMessage(Session* session, const buzz::XmlElement* stanza);
174
175   // Called each time a session has an error to send.
176   void OnErrorMessage(BaseSession* session,
177                       const buzz::XmlElement* stanza,
178                       const buzz::QName& name,
179                       const std::string& type,
180                       const std::string& text,
181                       const buzz::XmlElement* extra_info);
182
183   PortAllocator *allocator_;
184   rtc::Thread *signaling_thread_;
185   rtc::Thread *worker_thread_;
186   int timeout_;
187   TransportDescriptionFactory transport_desc_factory_;
188   SessionMap session_map_;
189   ClientMap client_map_;
190 };
191
192 }  // namespace cricket
193
194 #endif  // WEBRTC_P2P_BASE_SESSIONMANAGER_H_