cookiejar.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "config.h"
  28. #include "cookiejar.h"
  29. #include <QStandardPaths>
  30. #include <QDir>
  31. #include <QTextStream>
  32. TestBrowserCookieJar::TestBrowserCookieJar(QObject* parent)
  33. : QNetworkCookieJar(parent)
  34. , m_storageEnabled(false)
  35. {
  36. // We use a timer for the real disk write to avoid multiple IO
  37. // syscalls in sequence (when loading pages which set multiple cookies).
  38. m_timer.setInterval(10000);
  39. m_timer.setSingleShot(true);
  40. connect(&m_timer, SIGNAL(timeout()), this, SLOT(saveToDisk()));
  41. #ifndef QT_NO_DESKTOPSERVICES
  42. QString path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
  43. #else
  44. QString path = QDir::homePath() + "/.QtTestBrowser";
  45. #endif
  46. QDir().mkpath(path);
  47. m_file.setFileName(path + "/cookieJar");
  48. }
  49. TestBrowserCookieJar::~TestBrowserCookieJar()
  50. {
  51. if (m_storageEnabled) {
  52. extractRawCookies();
  53. saveToDisk();
  54. }
  55. }
  56. bool TestBrowserCookieJar::setCookiesFromUrl(const QList<QNetworkCookie>& cookieList, const QUrl& url)
  57. {
  58. bool status = QNetworkCookieJar::setCookiesFromUrl(cookieList, url);
  59. if (status && m_storageEnabled)
  60. scheduleSaveToDisk();
  61. return status;
  62. }
  63. void TestBrowserCookieJar::setDiskStorageEnabled(bool enabled)
  64. {
  65. m_storageEnabled = enabled;
  66. if (enabled && allCookies().isEmpty())
  67. loadFromDisk();
  68. // When disabling, save current cookies.
  69. if (!enabled && !allCookies().isEmpty())
  70. scheduleSaveToDisk();
  71. }
  72. void TestBrowserCookieJar::scheduleSaveToDisk()
  73. {
  74. // We extract the raw cookies here because the user may
  75. // enable/disable/clear cookies while the timer is running.
  76. extractRawCookies();
  77. m_timer.start();
  78. }
  79. void TestBrowserCookieJar::extractRawCookies()
  80. {
  81. QList<QNetworkCookie> cookies = allCookies();
  82. m_rawCookies.clear();
  83. foreach (const QNetworkCookie &cookie, cookies) {
  84. if (!cookie.isSessionCookie())
  85. m_rawCookies.append(cookie.toRawForm());
  86. }
  87. }
  88. void TestBrowserCookieJar::saveToDisk()
  89. {
  90. m_timer.stop();
  91. if (m_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  92. QTextStream out(&m_file);
  93. foreach (const QByteArray &cookie, m_rawCookies)
  94. out << cookie + "\n";
  95. m_file.close();
  96. } else
  97. qWarning("IO error handling cookiejar file");
  98. }
  99. void TestBrowserCookieJar::loadFromDisk()
  100. {
  101. if (!m_file.exists())
  102. return;
  103. QList<QNetworkCookie> cookies;
  104. if (m_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
  105. QTextStream in(&m_file);
  106. while (!in.atEnd())
  107. cookies.append(QNetworkCookie::parseCookies(in.readLine().toUtf8()));
  108. m_file.close();
  109. } else
  110. qWarning("IO error handling cookiejar file");
  111. setAllCookies(cookies);
  112. }
  113. void TestBrowserCookieJar::reset()
  114. {
  115. setAllCookies(QList<QNetworkCookie>());
  116. if (m_storageEnabled)
  117. scheduleSaveToDisk();
  118. }