/thirdparty/qxt/qxtweb-standalone/qxtweb/qxtabstractwebsessionmanager.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 201 lines · 55 code · 15 blank · 131 comment · 4 complexity · a629864401c7ea7ff622a96155cf4503 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) Qxt Foundation. Some rights reserved.
  4. **
  5. ** This file is part of the QxtWeb module of the Qxt library.
  6. **
  7. ** This library is free software; you can redistribute it and/or modify it
  8. ** under the terms of the Common Public License, version 1.0, as published
  9. ** by IBM, and/or under the terms of the GNU Lesser General Public License,
  10. ** version 2.1, as published by the Free Software Foundation.
  11. **
  12. ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
  13. ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
  14. ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
  15. ** FITNESS FOR A PARTICULAR PURPOSE.
  16. **
  17. ** You should have received a copy of the CPL and the LGPL along with this
  18. ** file. See the LICENSE file and the cpl1.0.txt/lgpl-2.1.txt files
  19. ** included with the source distribution for more information.
  20. ** If you did not receive a copy of the licenses, contact the Qxt Foundation.
  21. **
  22. ** <http://libqxt.org> <foundation@libqxt.org>
  23. **
  24. ****************************************************************************/
  25. /*!
  26. \class QxtAbstractWebSessionManager
  27. \inmodule QxtWeb
  28. \brief The QxtAbstractWebSessionManager class is a base class for QxtWeb session managers
  29. QxtAbstractWebSessionManager is the base class for all QxtWeb session managers.
  30. Session managers are responsible for managing connections between web browsers
  31. and web services, for creating sessions and their corresponding service objects,
  32. and for managing and dispatching events between browsers and services.
  33. Note that the session manager is not responsible for destroying service objects.
  34. A service object that wishes to end its corresponding session may destroy itself
  35. (see QObject::deleteLater()) and QxtAbstractWebSessionManager will automatically
  36. clean up its internal session tracking data.
  37. \sa QxtAbstractWebService
  38. */
  39. /*!
  40. * \typedef QxtAbstractWebSessionManager::ServiceFactory
  41. * \brief Pointer to a function that generates QxtAbstractWebService objects
  42. *
  43. * \bold TYPEDEF: The ServiceFactory type represents a pointer to a function that takes two
  44. * parameters -- a QxtAbstractWebSessionManager* pointer and an int session ID.
  45. * The function must return a QxtAbstractWebService* pointer.
  46. *
  47. * Usually, an application providing web services will instantiate one
  48. * QxtAbstractWebService object for each session. For services that do not
  49. * require session management, such as those that serve only static pages, a
  50. * single service object may be shared for all requests, or it may use some
  51. * more exotic scheme. See QxtAbstractWebService for more details.
  52. */
  53. #include "qxtabstractwebsessionmanager.h"
  54. #include "qxtabstractwebsessionmanager_p.h"
  55. #include "qxtabstractwebservice.h"
  56. #include "qxtmetaobject.h"
  57. #include <QtDebug>
  58. #ifndef QXT_DOXYGEN_RUN
  59. QxtAbstractWebSessionManagerPrivate::QxtAbstractWebSessionManagerPrivate() : factory(0), maxID(1)
  60. {
  61. // initializers only
  62. }
  63. void QxtAbstractWebSessionManagerPrivate::sessionDestroyed(int sessionID)
  64. {
  65. if (sessions.contains(sessionID))
  66. {
  67. freeList.enqueue(sessionID);
  68. sessions.remove(sessionID);
  69. }
  70. }
  71. int QxtAbstractWebSessionManagerPrivate::getNextID()
  72. {
  73. if (freeList.empty())
  74. {
  75. int next = maxID;
  76. maxID++;
  77. return next;
  78. }
  79. return freeList.dequeue();
  80. }
  81. #endif
  82. /*!
  83. * Creates a QxtAbstractWebSessionManager with the specified \a parent.
  84. *
  85. * Note that this is an abstract class and cannot be instantiated directly.
  86. */
  87. QxtAbstractWebSessionManager::QxtAbstractWebSessionManager(QObject* parent) : QObject(parent)
  88. {
  89. QXT_INIT_PRIVATE(QxtAbstractWebSessionManager);
  90. }
  91. /*!
  92. * Sets the service \a factory for the session manager.
  93. *
  94. * The service factory is invoked every time the session manager creates a new
  95. * session. Usually, an application providing web services will instantiate one
  96. * QxtAbstractWebService object for each session. For services that do not
  97. * require separate sessions, such as those that serve only static pages, the
  98. * factory may return a pointer to the same object for multiple requests.
  99. *
  100. * \sa QxtAbstractWebSessionManager::ServiceFactory
  101. */
  102. void QxtAbstractWebSessionManager::setServiceFactory(ServiceFactory* factory)
  103. {
  104. qxt_d().factory = factory;
  105. }
  106. /*!
  107. * Returns the service factory in use by the session manager.
  108. *
  109. * \sa setServiceFactory(ServiceFactory*)
  110. */
  111. QxtAbstractWebSessionManager::ServiceFactory* QxtAbstractWebSessionManager::serviceFactory() const
  112. {
  113. return qxt_d().factory;
  114. }
  115. /*!
  116. * Returns the service object corresponding to the provided \a sessionID.
  117. */
  118. QxtAbstractWebService* QxtAbstractWebSessionManager::session(int sessionID) const
  119. {
  120. if (qxt_d().sessions.contains(sessionID))
  121. return qxt_d().sessions[sessionID];
  122. return 0;
  123. }
  124. /*!
  125. * Creates a new session and returns its session ID.
  126. *
  127. * This function uses the serviceFactory() to request an instance of the web service.
  128. * \sa serviceFactory()
  129. */
  130. int QxtAbstractWebSessionManager::createService()
  131. {
  132. int sessionID = qxt_d().getNextID();
  133. if (!qxt_d().factory) return sessionID;
  134. QxtAbstractWebService* service = serviceFactory()(this, sessionID);
  135. qxt_d().sessions[sessionID] = service;
  136. // Using QxtBoundFunction to bind the sessionID to the slot invocation
  137. QxtMetaObject::connect(service, SIGNAL(destroyed()), QxtMetaObject::bind(&qxt_d(), SLOT(sessionDestroyed(int)), Q_ARG(int, sessionID)), Qt::QueuedConnection);
  138. return sessionID; // you can always get the service with this
  139. }
  140. /*!
  141. * \fn virtual bool QxtAbstractWebSessionManager::start()
  142. * Starts the session manager.
  143. *
  144. * Session managers should not create sessions before start() is invoked.
  145. * Subclasses are encouraged to refrain from accepting connections until the
  146. * session manager is started.
  147. */
  148. /*!
  149. * \fn virtual void QxtAbstractWebSessionManager::postEvent(QxtWebEvent* event)
  150. * Adds the event to the event queue for its associated session.
  151. *
  152. * Since different protocols may require different event processing behavior,
  153. * there is no default implementation in QxtAbstractWebSessionManager. Subclasses
  154. * are responsible for maintaining event queues and deciding when and where to
  155. * dispatch events.
  156. *
  157. * Depending on the subclass's implementation posted events may not be dispatched
  158. * for some time, and is is possible that an event may never be dispatched if
  159. * the session is terminated before the event is handled.
  160. *
  161. * \sa QxtWebEvent
  162. */
  163. /*!
  164. * \fn virtual void QxtAbstractWebSessionManager::processEvents()
  165. * Processes pending events for all sessions.
  166. *
  167. * Since different protocols may require different event processing behavior,
  168. * there is no default implementation in QxtAbstractWebSessionManager. Subclasses
  169. * are responsible for maintaining event queues and deciding when and where to
  170. * dispatch events.
  171. *
  172. * processEvents() is not required to dispatch all events immediately. In
  173. * particular, some events may require certain conditions to be met before
  174. * they may be fully processed. (For example, because HTTP cookies are sent
  175. * as response headers, QxtHttpServerConnector may not dispatch a
  176. * QxtWebStoreCookieEvent until a QxtWebPageEvent for the same session is
  177. * available.) Unprocessed events may remain in the event queue.
  178. *
  179. * \sa QxtWebEvent
  180. */