UrlLoader.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
  3. * Copyright (C) 2009 University of Szeged
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  24. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "UrlLoader.h"
  29. #include "private/qquickwebview_p.h"
  30. #include "private/qwebloadrequest_p.h"
  31. #include <QDebug>
  32. #include <QFile>
  33. UrlLoader::UrlLoader(BrowserWindow* browserWindow, const QString& inputFileName, int timeoutSeconds, int extraTimeSeconds)
  34. : m_browserWindow(browserWindow)
  35. , m_stdOut(stdout)
  36. , m_loaded(0)
  37. , m_numFramesLoading(0)
  38. {
  39. m_checkIfFinishedTimer.setInterval(200);
  40. m_checkIfFinishedTimer.setSingleShot(true);
  41. connect(&m_checkIfFinishedTimer, SIGNAL(timeout()), this, SLOT(checkIfFinished()));
  42. // loadStarted and loadFinished on QWebPage is emitted for each frame/sub-frame
  43. connect(m_browserWindow->webView(), SIGNAL(loadingChanged(QWebLoadRequest*)), this, SLOT(loadingChanged(QWebLoadRequest*)));
  44. connect(this, SIGNAL(loadStarted()), this, SLOT(frameLoadStarted()));
  45. connect(this, SIGNAL(loadFinished()), this, SLOT(frameLoadFinished()));
  46. if (timeoutSeconds) {
  47. m_timeoutTimer.setInterval(timeoutSeconds * 1000);
  48. m_timeoutTimer.setSingleShot(true);
  49. connect(this, SIGNAL(loadStarted()), &m_timeoutTimer, SLOT(start()));
  50. connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(loadNext()));
  51. }
  52. if (extraTimeSeconds) {
  53. m_extraTimeTimer.setInterval(extraTimeSeconds * 1000);
  54. m_extraTimeTimer.setSingleShot(true);
  55. connect(this, SIGNAL(pageLoadFinished()), &m_extraTimeTimer, SLOT(start()));
  56. connect(&m_extraTimeTimer, SIGNAL(timeout()), this, SLOT(loadNext()));
  57. } else
  58. connect(this, SIGNAL(pageLoadFinished()), this, SLOT(loadNext()));
  59. loadUrlList(inputFileName);
  60. }
  61. void UrlLoader::loadNext()
  62. {
  63. m_timeoutTimer.stop();
  64. m_extraTimeTimer.stop();
  65. m_checkIfFinishedTimer.stop();
  66. m_numFramesLoading = 0;
  67. QString qstr;
  68. if (getUrl(qstr)) {
  69. QUrl url(qstr, QUrl::StrictMode);
  70. if (url.isValid()) {
  71. m_stdOut << "Loading " << qstr << " ......" << ++m_loaded << endl;
  72. m_browserWindow->load(url.toString());
  73. } else
  74. loadNext();
  75. } else
  76. disconnect(m_browserWindow, 0, this, 0);
  77. }
  78. void UrlLoader::checkIfFinished()
  79. {
  80. if (!m_numFramesLoading)
  81. emit pageLoadFinished();
  82. }
  83. void UrlLoader::frameLoadStarted()
  84. {
  85. ++m_numFramesLoading;
  86. m_checkIfFinishedTimer.stop();
  87. }
  88. void UrlLoader::frameLoadFinished()
  89. {
  90. Q_ASSERT(m_numFramesLoading > 0);
  91. --m_numFramesLoading;
  92. // Once our frame has finished loading, wait a moment to call loadNext for cases
  93. // where a sub-frame starts loading or another frame is loaded through JavaScript.
  94. m_checkIfFinishedTimer.start();
  95. }
  96. void UrlLoader::loadUrlList(const QString& inputFileName)
  97. {
  98. QFile inputFile(inputFileName);
  99. if (inputFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
  100. QTextStream stream(&inputFile);
  101. QString line;
  102. while (true) {
  103. line = stream.readLine();
  104. if (line.isNull())
  105. break;
  106. m_urls.append(line);
  107. }
  108. } else {
  109. qDebug() << "Can't open list file";
  110. exit(0);
  111. }
  112. m_index = 0;
  113. inputFile.close();
  114. }
  115. bool UrlLoader::getUrl(QString& qstr)
  116. {
  117. if (m_index == m_urls.size())
  118. return false;
  119. qstr = m_urls[m_index++];
  120. return true;
  121. }
  122. void UrlLoader::loadingChanged(QWebLoadRequest* loadRequest)
  123. {
  124. switch (loadRequest->status()) {
  125. case QQuickWebView::LoadStartedStatus:
  126. emit loadStarted();
  127. break;
  128. case QQuickWebView::LoadStoppedStatus:
  129. case QQuickWebView::LoadSucceededStatus:
  130. case QQuickWebView::LoadFailedStatus:
  131. default:
  132. emit loadFinished();
  133. break;
  134. }
  135. }