diff options
| author | Mike Skec <skec@protonmail.ch> | 2021-01-03 13:01:47 +1100 |
|---|---|---|
| committer | Felix Queißner <felix@ib-queissner.de> | 2021-01-06 10:51:18 +0100 |
| commit | d815c8badabb347537f26612a5edab3a71cba866 (patch) | |
| tree | 31cad5baf317fbe1bad63ae7b9a0cd3104317d42 /src | |
| parent | 522184bf155d22728d1a829e5f30847ae3e7aeb7 (diff) | |
| download | kristall-d815c8badabb347537f26612a5edab3a71cba866.tar.gz | |
Basic caching functionality implemented
Diffstat (limited to 'src')
| -rw-r--r-- | src/browsertab.cpp | 60 | ||||
| -rw-r--r-- | src/browsertab.hpp | 7 | ||||
| -rw-r--r-- | src/mainwindow.cpp | 34 | ||||
| -rw-r--r-- | src/mainwindow.hpp | 27 |
4 files changed, 116 insertions, 12 deletions
diff --git a/src/browsertab.cpp b/src/browsertab.cpp index 3d52659..7fa0ad2 100644 --- a/src/browsertab.cpp +++ b/src/browsertab.cpp @@ -125,7 +125,7 @@ BrowserTab::~BrowserTab() delete ui; } -void BrowserTab::navigateTo(const QUrl &url, PushToHistory mode) +void BrowserTab::navigateTo(const QUrl &url, PushToHistory mode, bool no_read_cache) { if (kristall::protocols.isSchemeSupported(url.scheme()) != ProtocolSetup::Enabled) { @@ -143,7 +143,7 @@ void BrowserTab::navigateTo(const QUrl &url, PushToHistory mode) this->successfully_loaded = false; this->timer.start(); - if(not this->startRequest(url, ProtocolHandler::Default)) { + if(not this->startRequest(url, ProtocolHandler::Default, no_read_cache)) { QMessageBox::critical(this, "Kristall", QString("Failed to execute request to %1").arg(url.toString())); return; } @@ -185,7 +185,7 @@ void BrowserTab::scrollToAnchor(QString const &anchor) void BrowserTab::reloadPage() { if (current_location.isValid()) - this->navigateTo(this->current_location, DontPush); + this->navigateTo(this->current_location, DontPush, true); } void BrowserTab::focusUrlBar() @@ -418,12 +418,17 @@ 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); +} + +void BrowserTab::on_requestCompleteMime(const QByteArray &ref_data, const MimeType &mime) +{ + QByteArray data; + this->ui->media_browser->stopPlaying(); this->network_timeout_timer.stop(); - QByteArray data = ref_data; - MimeType mime = MimeParser::parse(mime_text); - qDebug() << "Loaded" << ref_data.length() << "bytes of type" << mime.type << "/" << mime.subtype; // for(auto & key : mime.parameters.keys()) { // qDebug() << key << mime.parameters[key]; @@ -449,6 +454,10 @@ void BrowserTab::on_requestComplete(const QByteArray &ref_data, const QString &m } } } + else + { + data = ref_data; + } this->successfully_loaded = true; this->page_title = ""; @@ -465,6 +474,14 @@ void BrowserTab::on_requestComplete(const QByteArray &ref_data, const QString &m emit this->fileLoaded(this->current_stats); this->updateMouseCursor(false); + + // Finally, put file in cache if we are not in an internal + // location. Don't cache if we read this page from cache. + if (!this->is_internal_location && + !this->was_read_from_cache) + { + this->mainWindow->cachePage(this->current_location, data, mime); + } } void BrowserTab::renderPage(const QByteArray &data, const MimeType &mime) @@ -1248,12 +1265,14 @@ 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 BrowserTab::startRequest(const QUrl &url, ProtocolHandler::RequestOptions options, bool no_read_cache) { this->updateMouseCursor(true); this->current_server_certificate = QSslCertificate { }; + this->was_read_from_cache = false; + this->current_handler = nullptr; for(auto & ptr : this->protocol_handlers) { @@ -1339,13 +1358,34 @@ bool BrowserTab::startRequest(const QUrl &url, ProtocolHandler::RequestOptions o if(not try_enable_certificate()) return false; - this->is_internal_location = (url.scheme() == "about"); + QString urlstr = url.toString(QUrl::FullyEncoded); + + this->is_internal_location = (url.scheme() == "about" || url.scheme() == "file"); this->current_location = url; - this->setUrlBarText(url.toString(QUrl::FormattingOptions(QUrl::FullyEncoded))); + this->setUrlBarText(urlstr); this->network_timeout_timer.start(kristall::options.network_timeout); - return this->current_handler->startRequest(url.adjusted(QUrl::RemoveFragment), options); + const auto req = [this, &url, &options]() + { + return this->current_handler->startRequest(url.adjusted(QUrl::RemoveFragment), options); + }; + + if (no_read_cache) 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) + { + qDebug() << "Reading page from cache"; + this->was_read_from_cache = true; + this->on_requestCompleteMime(pg->body, pg->mime); + return true; + } + else + { + return req(); + } } void BrowserTab::updateMouseCursor(bool waiting) diff --git a/src/browsertab.hpp b/src/browsertab.hpp index 42b86eb..aec41af 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); + void navigateTo(QUrl const & url, PushToHistory mode, bool no_read_cache = false); void navigateBack(const QModelIndex &history_index); @@ -135,6 +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_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); @@ -164,7 +165,7 @@ private: this->addProtocolHandler(std::make_unique<T>()); } - bool startRequest(QUrl const & url, ProtocolHandler::RequestOptions options); + bool startRequest(QUrl const & url, ProtocolHandler::RequestOptions options, bool no_read_cache = false); void updateMouseCursor(bool waiting); @@ -211,6 +212,8 @@ public: QString page_title; bool no_url_style = false; + + bool was_read_from_cache = false; }; #endif // BROWSERTAB_HPP diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f46ed75..4552520 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -209,6 +209,40 @@ 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 e02ecfd..cfc35bf 100644 --- a/src/mainwindow.hpp +++ b/src/mainwindow.hpp @@ -21,6 +21,24 @@ class BrowserTab; enum class UIDensity : int; +struct CachedPage +{ + QUrl url; + + QByteArray body; + + MimeType mime; + + // TODO: last scroll position + + // 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) + {} +}; + class MainWindow : public QMainWindow { Q_OBJECT @@ -44,6 +62,12 @@ 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); @@ -115,5 +139,8 @@ 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 |
