BVB Source Codes

capybara-webkit Show WebPageManager.cpp Source code

Return Download capybara-webkit: download WebPageManager.cpp Source code - Download capybara-webkit Source code - Type:.cpp
  1. #include "WebPageManager.h"
  2. #include "WebPage.h"
  3. #include "NetworkCookieJar.h"
  4. #include "NetworkAccessManager.h"
  5. #include "BlacklistedRequestHandler.h"
  6. #include "CustomHeadersRequestHandler.h"
  7. #include "MissingContentHeaderRequestHandler.h"
  8. #include "UnknownUrlHandler.h"
  9. #include "NetworkRequestFactory.h"
  10.  
  11. WebPageManager::WebPageManager(QObject *parent) : QObject(parent) {
  12.   m_ignoreSslErrors = false;
  13.   m_cookieJar = new NetworkCookieJar(this);
  14.   m_success = true;
  15.   m_loggingEnabled = false;
  16.   m_ignoredOutput = new QFile(this);
  17.   m_timeout = -1;
  18.   m_customHeadersRequestHandler = new CustomHeadersRequestHandler(
  19.     new MissingContentHeaderRequestHandler(
  20.       new NetworkRequestFactory(this),
  21.       this
  22.     ),
  23.     this
  24.   );
  25.   m_unknownUrlHandler =
  26.     new UnknownUrlHandler(m_customHeadersRequestHandler, this);
  27.   m_blacklistedRequestHandler =
  28.     new BlacklistedRequestHandler(m_unknownUrlHandler, this);
  29.   m_networkAccessManager =
  30.     new NetworkAccessManager(m_blacklistedRequestHandler, this);
  31.   m_networkAccessManager->setCookieJar(m_cookieJar);
  32.  
  33.   createPage()->setFocus();
  34. }
  35.  
  36. NetworkAccessManager *WebPageManager::networkAccessManager() {
  37.   return m_networkAccessManager;
  38. }
  39.  
  40. void WebPageManager::append(WebPage *value) {
  41.   m_pages.append(value);
  42. }
  43.  
  44. QList<WebPage *> WebPageManager::pages() const {
  45.   return m_pages;
  46. }
  47.  
  48. void WebPageManager::setCurrentPage(WebPage *page) {
  49.   m_currentPage = page;
  50. }
  51.  
  52. WebPage *WebPageManager::currentPage() const {
  53.   return m_currentPage;
  54. }
  55.  
  56. WebPage *WebPageManager::createPage() {
  57.   WebPage *page = new WebPage(this);
  58.   connect(page, SIGNAL(loadStarted()),
  59.           this, SLOT(emitLoadStarted()));
  60.   connect(page, SIGNAL(pageFinished(bool)),
  61.           this, SLOT(setPageStatus(bool)));
  62.   connect(page, SIGNAL(requestCreated(QByteArray &, QNetworkReply *)),
  63.           this, SLOT(requestCreated(QByteArray &, QNetworkReply *)));
  64.   append(page);
  65.   return page;
  66. }
  67.  
  68. void WebPageManager::removePage(WebPage *page) {
  69.   m_pages.removeOne(page);
  70.   page->deleteLater();
  71.   if (m_pages.isEmpty())
  72.     createPage()->setFocus();
  73.   else if (page == m_currentPage)
  74.     m_pages.first()->setFocus();
  75. }
  76.  
  77. void WebPageManager::emitLoadStarted() {
  78.   if (m_started.empty()) {
  79.     logger() << "Load started";
  80.     emit loadStarted();
  81.   }
  82.   m_started += qobject_cast<WebPage *>(sender());
  83. }
  84.  
  85. void WebPageManager::requestCreated(QByteArray &url, QNetworkReply *reply) {
  86.   logger() << "Started request to" << url;
  87.   if (reply->isFinished())
  88.     replyFinished(reply);
  89.   else {
  90.     m_pendingReplies.append(reply);
  91.     connect(reply, SIGNAL(finished()), SLOT(handleReplyFinished()));
  92.     connect(
  93.       reply,
  94.       SIGNAL(destroyed(QObject *)),
  95.       SLOT(replyDestroyed(QObject *))
  96.     );
  97.   }
  98. }
  99.  
  100. void WebPageManager::handleReplyFinished() {
  101.   QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
  102.   disconnect(reply, SIGNAL(finished()), this, SLOT(handleReplyFinished()));
  103.   replyFinished(reply);
  104. }
  105.  
  106. void WebPageManager::replyFinished(QNetworkReply *reply) {
  107.   int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
  108.   logger() << "Received" << status << "from" << reply->url().toString();
  109.   m_pendingReplies.removeAll(reply);
  110. }
  111.  
  112. void WebPageManager::replyDestroyed(QObject *reply) {
  113.   m_pendingReplies.removeAll((QNetworkReply *) reply);
  114. }
  115.  
  116. void WebPageManager::setPageStatus(bool success) {
  117.   logger() << "Page finished with" << success;
  118.   m_started.remove(qobject_cast<WebPage *>(sender()));
  119.   m_success = success && m_success;
  120.   if (m_started.empty()) {
  121.     emitPageFinished();
  122.   }
  123. }
  124.  
  125. void WebPageManager::emitPageFinished() {
  126.   logger() << "Load finished";
  127.   emit pageFinished(m_success);
  128.   m_success = true;
  129. }
  130.  
  131. void WebPageManager::setIgnoreSslErrors(bool value) {
  132.   m_ignoreSslErrors = value;
  133. }
  134.  
  135. bool WebPageManager::ignoreSslErrors() {
  136.   return m_ignoreSslErrors;
  137. }
  138.  
  139. int WebPageManager::getTimeout() {
  140.   return m_timeout;
  141. }
  142.  
  143. void WebPageManager::setTimeout(int timeout) {
  144.   m_timeout = timeout;
  145. }
  146.  
  147. void WebPageManager::reset() {
  148.   foreach(QNetworkReply *reply, m_pendingReplies) {
  149.     logger() << "Aborting request to" << reply->url().toString();
  150.     reply->abort();
  151.   }
  152.   m_pendingReplies.clear();
  153.  
  154.   while (!m_pages.isEmpty()) {
  155.     WebPage *page = m_pages.takeFirst();
  156.     page->deleteLater();
  157.   }
  158.  
  159.   m_timeout = -1;
  160.   m_cookieJar->clearCookies();
  161.   m_networkAccessManager->reset();
  162.   m_customHeadersRequestHandler->reset();
  163.   m_currentPage->resetLocalStorage();
  164.   m_blacklistedRequestHandler->reset();
  165.   m_unknownUrlHandler->reset();
  166.  
  167.  
  168.   qint64 size = QWebSettings::offlineWebApplicationCacheQuota();
  169.   // No public function was found to wrap the empty() call to
  170.   // WebCore::cacheStorage().empty()
  171.   QWebSettings::setOfflineWebApplicationCacheQuota(size);
  172.  
  173.   createPage()->setFocus();
  174. }
  175.  
  176. NetworkCookieJar *WebPageManager::cookieJar() {
  177.   return m_cookieJar;
  178. }
  179.  
  180. bool WebPageManager::isLoading() const {
  181.   foreach(WebPage *page, pages()) {
  182.     if (page->isLoading()) {
  183.       return true;
  184.     }
  185.   }
  186.   return false;
  187. }
  188.  
  189. QDebug WebPageManager::logger() const {
  190.   if (m_loggingEnabled) {
  191.     return qCritical();
  192.   } else {
  193.     return QDebug(m_ignoredOutput);
  194.   }
  195. }
  196.  
  197. void WebPageManager::enableLogging() {
  198.   m_loggingEnabled = true;
  199. }
  200.  
  201. void WebPageManager::setUrlBlacklist(const QStringList &urls) {
  202.   m_blacklistedRequestHandler->setUrlBlacklist(urls);
  203. }
  204.  
  205. void WebPageManager::addHeader(QString key, QString value) {
  206.   m_customHeadersRequestHandler->addHeader(key, value);
  207. }
  208.  
  209. void WebPageManager::setUnknownUrlMode(UnknownUrlHandler::Mode mode) {
  210.   m_unknownUrlHandler->setMode(mode);
  211. }
  212.  
  213. void WebPageManager::allowUrl(const QString &url) {
  214.   m_unknownUrlHandler->allowUrl(url);
  215. }
  216.  
  217. void WebPageManager::blockUrl(const QString &url) {
  218.   m_blacklistedRequestHandler->blockUrl(url);
  219. }
  220.  
downloadWebPageManager.cpp Source code - Download capybara-webkit Source code
Related Source Codes/Software:
dat.gui - dat.gui is a lightweight controller library for Ja... 2017-04-16
tmate - Instant Terminal Sharing ht... 2017-04-16
WNXHuntForCity - City find By Objective - C 2017-04-16
st2 - StackStorm (aka IFTTT for Ops) is event-driven aut... 2017-04-16
css-in-js - React: CSS in JS techniques comparison. 2017-04-16
fullstackpython.com - Full Stack Python source with Pelican, Bootstrap a... 2017-04-16
naxsi - NAXSI is an open-source, high performance, low rul... 2017-04-16
ToGL - Direct3D to OpenGL abstraction layer 2017-04-16
LiveScript - LiveScript is a language which compiles to JavaScr... 2017-04-16
clair - Vulnerability Static Analysis for Container 2017-04-17
pipfile - 2017-04-23
MaterialSearchView - Express it in the library to implement SearchView ... 2017-04-23
ptpython - A better Python REPL 2017-04-23
Laravel-4-Bootstrap-Starter-Site - Laravel 4 Starter Site is a basic blog application... 2017-04-23
official-images - Primary source of truth for the Docker "Official I... 2017-04-23
awesome-chrome-devtools - Awesome tooling and resources in the Chrome DevToo... 2017-04-23
EventStore - The open-source, functional database with Complex ... 2017-04-23
screencat - 2017-04-23
viz.js - A hack to put Graphviz on the web. ... 2017-04-23
JKeyboardPanelSwitch - For resolve the layout conflict when keybord & amp... 2017-04-23

 Back to top