aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Skec <skec@protonmail.ch>2021-01-06 19:37:26 +1100
committerFelix Queißner <felix@ib-queissner.de>2021-01-06 10:51:18 +0100
commit2a9bb4fa6121de62d9e6ba06d9a109ba6d57f14c (patch)
tree7f2b49ca97071b003697ffeaaa904ce1dfe166d6 /src
parent24086fdfe92814c38da6d219916ee9d45d8ba581 (diff)
downloadkristall-2a9bb4fa6121de62d9e6ba06d9a109ba6d57f14c.tar.gz
cache code refactor
Diffstat (limited to 'src')
-rw-r--r--src/browsertab.cpp38
-rw-r--r--src/browsertab.hpp6
-rw-r--r--src/cachehandler.cpp53
-rw-r--r--src/cachehandler.hpp48
-rw-r--r--src/kristall.hpp3
-rw-r--r--src/kristall.pro6
-rw-r--r--src/main.cpp1
-rw-r--r--src/mainwindow.cpp34
-rw-r--r--src/mainwindow.hpp28
-rw-r--r--src/protocols/abouthandler.cpp22
10 files changed, 154 insertions, 85 deletions
diff --git a/src/browsertab.cpp b/src/browsertab.cpp
index 7746e79..0cedb35 100644
--- a/src/browsertab.cpp
+++ b/src/browsertab.cpp
@@ -128,7 +128,7 @@ BrowserTab::~BrowserTab()
delete ui;
}
-void BrowserTab::navigateTo(const QUrl &url, PushToHistory mode, bool no_read_cache)
+void BrowserTab::navigateTo(const QUrl &url, PushToHistory mode, bool no_cache_read)
{
if (kristall::protocols.isSchemeSupported(url.scheme()) != ProtocolSetup::Enabled)
{
@@ -142,19 +142,17 @@ void BrowserTab::navigateTo(const QUrl &url, PushToHistory mode, bool no_read_ca
return;
}
- // If this page is in cache, update the scroll position
- QString urlstr = this->current_location.toString(QUrl::FullyEncoded | QUrl::RemoveFragment);
- if (std::shared_ptr<CachedPage> pg = mainWindow->cacheFind(urlstr); pg != nullptr)
+ // If this page is in cache, store the scroll position
+ if (auto pg = kristall::cache.find(this->current_location); pg != nullptr)
{
pg->scroll_pos = this->ui->text_browser->verticalScrollBar()->value();
- qDebug() << "SETTING TO " << pg->scroll_pos;
}
this->redirection_count = 0;
this->successfully_loaded = false;
this->timer.start();
- if(not this->startRequest(url, ProtocolHandler::Default, no_read_cache)) {
+ if(not this->startRequest(url, ProtocolHandler::Default, no_cache_read)) {
QMessageBox::critical(this, "Kristall", QString("Failed to execute request to %1").arg(url.toString()));
return;
}
@@ -430,10 +428,10 @@ static QByteArray convertToUtf8(QByteArray const & input, QString const & charSe
void BrowserTab::on_requestComplete(const QByteArray &ref_data, const QString &mime_text)
{
MimeType mime = MimeParser::parse(mime_text);
- this->on_requestCompleteMime(ref_data, mime);
+ this->on_requestComplete(ref_data, mime);
}
-void BrowserTab::on_requestCompleteMime(const QByteArray &ref_data, const MimeType &mime)
+void BrowserTab::on_requestComplete(const QByteArray &ref_data, const MimeType &mime)
{
QByteArray data;
@@ -553,6 +551,11 @@ void BrowserTab::renderPage(const QByteArray &data, const MimeType &mime)
document->setHtml(page_html);
page_title = document->metaInformation(QTextDocument::DocumentTitle);
+
+ // For now we don't cache HTML pages, because they will most
+ // of the time clog up the cache.
+ // TODO: preference for this? protocol-specific cache limits?
+ will_cache = false;
}
else if (not plaintext_only and mime.is("text","x-kristall-theme"))
{
@@ -707,11 +710,12 @@ void BrowserTab::renderPage(const QByteArray &data, const MimeType &mime)
// Put file in cache if we are not in an internal
// location. Don't cache if we read this page from cache.
// We also do not cache if user has a client certificate enabled.
- if (!this->is_internal_location &&
+ if (will_cache &&
+ !this->is_internal_location &&
!this->was_read_from_cache &&
!this->current_identity.isValid())
{
- this->mainWindow->cachePage(this->current_location, data, mime);
+ kristall::cache.push(this->current_location, data, mime);
}
}
@@ -1282,7 +1286,8 @@ void BrowserTab::resetClientCertificate()
void BrowserTab::addProtocolHandler(std::unique_ptr<ProtocolHandler> &&handler)
{
connect(handler.get(), &ProtocolHandler::requestProgress, this, &BrowserTab::on_requestProgress);
- connect(handler.get(), &ProtocolHandler::requestComplete, this, &BrowserTab::on_requestComplete);
+ connect(handler.get(), &ProtocolHandler::requestComplete, this,
+ qOverload<QByteArray const &, QString const &>(&BrowserTab::on_requestComplete));
connect(handler.get(), &ProtocolHandler::redirected, this, &BrowserTab::on_redirected);
connect(handler.get(), &ProtocolHandler::inputRequired, this, &BrowserTab::on_inputRequired);
connect(handler.get(), &ProtocolHandler::networkError, this, &BrowserTab::on_networkError);
@@ -1292,7 +1297,7 @@ void BrowserTab::addProtocolHandler(std::unique_ptr<ProtocolHandler> &&handler)
this->protocol_handlers.emplace_back(std::move(handler));
}
-bool BrowserTab::startRequest(const QUrl &url, ProtocolHandler::RequestOptions options, bool no_read_cache)
+bool BrowserTab::startRequest(const QUrl &url, ProtocolHandler::RequestOptions options, bool no_cache_read)
{
this->updateMouseCursor(true);
@@ -1398,23 +1403,20 @@ bool BrowserTab::startRequest(const QUrl &url, ProtocolHandler::RequestOptions o
return this->current_handler->startRequest(url.adjusted(QUrl::RemoveFragment), options);
};
- if (no_read_cache || this->current_identity.isValid())
+ if (no_cache_read || this->current_identity.isValid())
return req();
// Check if we have the page in our cache.
- urlstr = url.toString(QUrl::FullyEncoded | QUrl::RemoveFragment);
- if (std::shared_ptr<CachedPage> pg = mainWindow->cacheFind(urlstr); pg != nullptr)
+ if (auto pg = kristall::cache.find(url); pg != nullptr)
{
qDebug() << "Reading page from cache";
this->was_read_from_cache = true;
- this->on_requestCompleteMime(pg->body, pg->mime);
+ this->on_requestComplete(pg->body, pg->mime);
// Move scrollbar to cached position
if (pg->scroll_pos != -1)
this->ui->text_browser->verticalScrollBar()->setValue(pg->scroll_pos);
- qDebug() << "SCROLL" << pg->scroll_pos;
-
return true;
}
else
diff --git a/src/browsertab.hpp b/src/browsertab.hpp
index aec41af..595e846 100644
--- a/src/browsertab.hpp
+++ b/src/browsertab.hpp
@@ -53,7 +53,7 @@ public:
explicit BrowserTab(MainWindow * mainWindow);
~BrowserTab();
- void navigateTo(QUrl const & url, PushToHistory mode, bool no_read_cache = false);
+ void navigateTo(QUrl const & url, PushToHistory mode, bool no_cache_read = false);
void navigateBack(const QModelIndex &history_index);
@@ -135,7 +135,7 @@ private: // network slots
void on_requestProgress(qint64 transferred);
void on_requestComplete(QByteArray const & data, QString const & mime);
- void on_requestCompleteMime(QByteArray const & data, MimeType const & mime);
+ void on_requestComplete(QByteArray const & data, MimeType const & mime);
void on_redirected(QUrl uri, bool is_permanent);
void on_inputRequired(QString const & user_query, bool is_sensitive);
void on_networkError(ProtocolHandler::NetworkError error, QString const & reason);
@@ -165,7 +165,7 @@ private:
this->addProtocolHandler(std::make_unique<T>());
}
- bool startRequest(QUrl const & url, ProtocolHandler::RequestOptions options, bool no_read_cache = false);
+ bool startRequest(QUrl const & url, ProtocolHandler::RequestOptions options, bool no_cache_read = false);
void updateMouseCursor(bool waiting);
diff --git a/src/cachehandler.cpp b/src/cachehandler.cpp
new file mode 100644
index 0000000..0bd7f36
--- /dev/null
+++ b/src/cachehandler.cpp
@@ -0,0 +1,53 @@
+#include "cachehandler.hpp"
+#include "kristall.hpp"
+
+#include <QDebug>
+
+void CacheHandler::push(const QUrl &url, const QByteArray &body, const MimeType &mime)
+{
+ QString urlstr = url.toString(QUrl::FullyEncoded | QUrl::RemoveFragment);
+
+ if (this->page_cache.find(urlstr) != this->page_cache.end())
+ {
+ qDebug() << "Updating cached page";
+ auto pg = this->page_cache[urlstr];
+ pg->body = body;
+ pg->mime = mime;
+ return;
+ }
+
+ this->page_cache[urlstr] = std::make_shared<CachedPage>(url, body, mime);
+
+ qDebug() << "Pushed page to cache: " << url;
+
+ return;
+}
+
+std::shared_ptr<CachedPage> CacheHandler::find(const QString &url)
+{
+ if (this->page_cache.find(url) != this->page_cache.end())
+ {
+ return page_cache[url];
+ }
+ return nullptr;
+}
+
+std::shared_ptr<CachedPage> CacheHandler::find(const QUrl &url)
+{
+ return this->find(url.toString(QUrl::FullyEncoded | QUrl::RemoveFragment));
+}
+
+bool CacheHandler::contains(const QString &url) const
+{
+ return this->page_cache.find(url) != this->page_cache.end();
+}
+
+bool CacheHandler::contains(const QUrl &url) const
+{
+ return this->contains(url.toString(QUrl::FullyEncoded | QUrl::RemoveFragment));
+}
+
+CacheMap const& CacheHandler::getPages() const
+{
+ return this->page_cache;
+}
diff --git a/src/cachehandler.hpp b/src/cachehandler.hpp
new file mode 100644
index 0000000..ca782bb
--- /dev/null
+++ b/src/cachehandler.hpp
@@ -0,0 +1,48 @@
+#ifndef CACHEHANDLER_HPP
+#define CACHEHANDLER_HPP
+
+#include "mimeparser.hpp"
+#include <memory>
+#include <unordered_map>
+#include <QUrl>
+#include <QByteArray>
+
+struct CachedPage
+{
+ QUrl url;
+
+ QByteArray body;
+
+ MimeType mime;
+
+ int scroll_pos;
+
+ // also: maybe compress page contents? May test
+ // to see if it's worth it
+
+ CachedPage(const QUrl &url, const QByteArray &body, const MimeType &mime)
+ : url(url), body(body), mime(mime), scroll_pos(-1)
+ {}
+};
+
+typedef std::unordered_map<QString, std::shared_ptr<CachedPage>> CacheMap;
+
+class CacheHandler
+{
+public:
+ void push(QUrl const & url, QByteArray const & body, MimeType const & mime);
+
+ std::shared_ptr<CachedPage> find(QString const &url);
+ std::shared_ptr<CachedPage> find(QUrl const &url);
+
+ bool contains(QString const & url) const;
+ bool contains(QUrl const & url) const;
+
+ CacheMap const& getPages() const;
+
+private:
+ // In-memory cache storage.
+ CacheMap page_cache;
+};
+
+#endif
diff --git a/src/kristall.hpp b/src/kristall.hpp
index e3403c0..cd863dd 100644
--- a/src/kristall.hpp
+++ b/src/kristall.hpp
@@ -11,6 +11,7 @@
#include "favouritecollection.hpp"
#include "protocolsetup.hpp"
#include "documentstyle.hpp"
+#include "cachehandler.hpp"
enum class Theme : int
{
@@ -92,6 +93,8 @@ namespace kristall
extern DocumentStyle document_style;
+ extern CacheHandler cache;
+
namespace trust {
extern SslTrust gemini;
extern SslTrust https;
diff --git a/src/kristall.pro b/src/kristall.pro
index 7941b6b..7746022 100644
--- a/src/kristall.pro
+++ b/src/kristall.pro
@@ -120,7 +120,8 @@ SOURCES += \
widgets/searchbar.cpp \
widgets/ssltrusteditor.cpp \
widgets/favouritepopup.cpp \
- widgets/favouritebutton.cpp
+ widgets/favouritebutton.cpp \
+ cachehandler.cpp
HEADERS += \
../lib/luis-l-gist/interactiveview.hpp \
@@ -165,7 +166,8 @@ HEADERS += \
widgets/searchbar.hpp \
widgets/ssltrusteditor.hpp \
widgets/favouritepopup.hpp \
- widgets/favouritebutton.hpp
+ widgets/favouritebutton.hpp \
+ cachehandler.hpp
FORMS += \
browsertab.ui \
diff --git a/src/main.cpp b/src/main.cpp
index 3b215f3..5200ce3 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -19,6 +19,7 @@ SslTrust kristall::trust::https;
FavouriteCollection kristall::favourites;
GenericSettings kristall::options;
DocumentStyle kristall::document_style(false);
+CacheHandler kristall::cache;
QString kristall::default_font_family;
QString kristall::default_font_family_fixed;
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index 4552520..f46ed75 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -209,40 +209,6 @@ void MainWindow::mousePressEvent(QMouseEvent *event)
}
}
-std::shared_ptr<CachedPage> MainWindow::cacheFind(QString const &url)
-{
- if (this->page_cache.find(url) != this->page_cache.end())
- {
- return page_cache[url];
- }
- return nullptr;
-}
-
-bool MainWindow::cacheContains(const QUrl &url) const
-{
- QString urlstr = url.toString(QUrl::FullyEncoded | QUrl::RemoveFragment);
- return this->page_cache.find(urlstr) != this->page_cache.end();
-}
-
-void MainWindow::cachePage(const QUrl &url, const QByteArray &body, const MimeType &mime)
-{
- QString urlstr = url.toString(QUrl::FullyEncoded | QUrl::RemoveFragment);
- if (this->page_cache.find(urlstr) != this->page_cache.end())
- {
- qDebug() << "Updating cached page";
- auto pg = this->page_cache[urlstr];
- pg->body = body;
- pg->mime = mime;
- return;
- }
-
- this->page_cache[urlstr] = std::make_shared<CachedPage>(url, body, mime);
-
- qDebug() << "Cached page : " << url;
-
- return;
-}
-
void MainWindow::on_browser_tabs_currentChanged(int index)
{
if(index >= 0) {
diff --git a/src/mainwindow.hpp b/src/mainwindow.hpp
index 1868eaf..ae86588 100644
--- a/src/mainwindow.hpp
+++ b/src/mainwindow.hpp
@@ -5,7 +5,6 @@
#include <QLabel>
#include <QSettings>
-
#include "favouritecollection.hpp"
#include "renderers/geminirenderer.hpp"
@@ -21,24 +20,6 @@ class BrowserTab;
enum class UIDensity : int;
-struct CachedPage
-{
- QUrl url;
-
- QByteArray body;
-
- MimeType mime;
-
- int scroll_pos;
-
- // also: maybe compress page contents? May test
- // to see if it's worth it
-
- CachedPage(const QUrl &url, const QByteArray &body, const MimeType &mime)
- : url(url), body(body), mime(mime), scroll_pos(-1)
- {}
-};
-
class MainWindow : public QMainWindow
{
Q_OBJECT
@@ -62,12 +43,6 @@ public:
void mousePressEvent(QMouseEvent *event) override;
- std::shared_ptr<CachedPage> cacheFind(QString const &url);
-
- bool cacheContains(QUrl const & url) const;
-
- void cachePage(QUrl const & url, QByteArray const & body, MimeType const & mime);
-
private slots:
void on_browser_tabs_currentChanged(int index);
@@ -139,8 +114,5 @@ private:
QLabel * file_size;
QLabel * file_mime;
QLabel * load_time;
-
- // This is where we store our current in-memory cache.
- std::unordered_map<QString, std::shared_ptr<CachedPage>> page_cache;
};
#endif // MAINWINDOW_HPP
diff --git a/src/protocols/abouthandler.cpp b/src/protocols/abouthandler.cpp
index 4ab789b..784e1bb 100644
--- a/src/protocols/abouthandler.cpp
+++ b/src/protocols/abouthandler.cpp
@@ -1,5 +1,6 @@
#include "abouthandler.hpp"
#include "kristall.hpp"
+#include "ioutil.hpp"
#include <QUrl>
#include <QFile>
@@ -48,6 +49,27 @@ bool AboutHandler::startRequest(const QUrl &url, ProtocolHandler::RequestOptions
emit this->requestComplete(document, "text/gemini");
}
+ else if (url.path() == "cache")
+ {
+ QByteArray document;
+ document.append("# Cache information\n");
+
+ auto& cache = kristall::cache.getPages();
+ long unsigned cache_usage = 0;
+ int cached_count = 0;
+ for (auto it = cache.begin(); it != cache.end(); ++it, ++cached_count)
+ {
+ cache_usage += (long unsigned)it->second->body.size();
+ }
+
+ document.append(QString(
+ "In-memory cache usage:\n"
+ "* %1 used\n"
+ "* %2 pages in cache\n")
+ .arg(IoUtil::size_human(cache_usage), QString::number(cached_count)));
+
+ emit this->requestComplete(document, "text/gemini");
+ }
else
{
QFile file(QString(":/about/%1.gemini").arg(url.path()));