1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

FalkonSchemeHandler: Make sure to load data on main thread

QIODevice::read is called on WebEngine IO thread.
This commit is contained in:
David Rosca 2018-07-22 15:17:04 +02:00
parent 95e2232d39
commit a7d2f87579
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
2 changed files with 23 additions and 20 deletions

View File

@ -89,8 +89,7 @@ FalkonSchemeReply::FalkonSchemeReply(QWebEngineUrlRequestJob *job, QObject *pare
{ {
m_pageName = m_job->requestUrl().path(); m_pageName = m_job->requestUrl().path();
open(QIODevice::ReadOnly); QTimer::singleShot(0, this, &FalkonSchemeReply::loadPage);
m_buffer.open(QIODevice::ReadWrite);
} }
void FalkonSchemeReply::loadPage() void FalkonSchemeReply::loadPage()
@ -98,38 +97,40 @@ void FalkonSchemeReply::loadPage()
if (m_loaded) if (m_loaded)
return; return;
QTextStream stream(&m_buffer); QString contents;
stream.setCodec("UTF-8");
if (m_pageName == QLatin1String("about")) { if (m_pageName == QLatin1String("about")) {
stream << aboutPage(); contents = aboutPage();
} } else if (m_pageName == QLatin1String("start")) {
else if (m_pageName == QLatin1String("start")) { contents = startPage();
stream << startPage(); } else if (m_pageName == QLatin1String("speeddial")) {
} contents = speeddialPage();
else if (m_pageName == QLatin1String("speeddial")) { } else if (m_pageName == QLatin1String("config")) {
stream << speeddialPage(); contents = configPage();
} } else if (m_pageName == QLatin1String("restore")) {
else if (m_pageName == QLatin1String("config")) { contents = restorePage();
stream << configPage();
}
else if (m_pageName == QLatin1String("restore")) {
stream << restorePage();
} }
stream.flush(); QMutexLocker lock(&m_mutex);
m_buffer.reset(); m_buffer.setData(contents.toUtf8());
m_buffer.open(QIODevice::ReadOnly);
lock.unlock();
open(QIODevice::ReadOnly);
emit readyRead();
m_loaded = true; m_loaded = true;
} }
qint64 FalkonSchemeReply::bytesAvailable() const qint64 FalkonSchemeReply::bytesAvailable() const
{ {
QMutexLocker lock(&m_mutex);
return m_buffer.bytesAvailable(); return m_buffer.bytesAvailable();
} }
qint64 FalkonSchemeReply::readData(char *data, qint64 maxSize) qint64 FalkonSchemeReply::readData(char *data, qint64 maxSize)
{ {
loadPage(); QMutexLocker lock(&m_mutex);
return m_buffer.read(data, maxSize); return m_buffer.read(data, maxSize);
} }

View File

@ -18,6 +18,7 @@
#ifndef FALKONSCHEMEHANDLER_H #ifndef FALKONSCHEMEHANDLER_H
#define FALKONSCHEMEHANDLER_H #define FALKONSCHEMEHANDLER_H
#include <QMutex>
#include <QBuffer> #include <QBuffer>
#include <QIODevice> #include <QIODevice>
#include <QWebEngineUrlSchemeHandler> #include <QWebEngineUrlSchemeHandler>
@ -60,6 +61,7 @@ private:
QBuffer m_buffer; QBuffer m_buffer;
QString m_pageName; QString m_pageName;
QWebEngineUrlRequestJob *m_job; QWebEngineUrlRequestJob *m_job;
mutable QMutex m_mutex;
}; };
#endif // FALKONSCHEMEHANDLER_H #endif // FALKONSCHEMEHANDLER_H