Port Qt WebEngine related features to Qt 6.2.0 (beta2)

This commit is contained in:
Martchus 2021-08-07 18:05:30 +02:00
parent f2db5e737c
commit d21c7cdf2a
3 changed files with 49 additions and 10 deletions

View File

@ -18,7 +18,6 @@
#include <QWebEngineCertificateError>
#include <QWebEngineSettings>
#include <QWebEngineView>
#include <QtWebEngineWidgetsVersion>
#elif defined(SYNCTHINGWIDGETS_USE_WEBKIT)
#include <QNetworkRequest>
#include <QSslError>
@ -47,6 +46,9 @@ WebPage::WebPage(WebViewDialog *dlg, SYNCTHINGWIDGETS_WEB_VIEW *view)
{
#ifdef SYNCTHINGWIDGETS_USE_WEBENGINE
settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, true);
#if (QTWEBENGINEWIDGETS_VERSION >= QT_VERSION_CHECK(6, 0, 0))
connect(this, &WebPage::certificateError, this, &WebPage::handleCertificateError);
#endif
connect(
this, &WebPage::authenticationRequired, this, static_cast<void (WebPage::*)(const QUrl &, QAuthenticator *)>(&WebPage::supplyCredentials));
#else // SYNCTHINGWIDGETS_USE_WEBKIT
@ -101,14 +103,16 @@ SYNCTHINGWIDGETS_WEB_PAGE *WebPage::createWindow(SYNCTHINGWIDGETS_WEB_PAGE::WebW
}
#ifdef SYNCTHINGWIDGETS_USE_WEBENGINE
/*!
* \brief Accepts self-signed certificates used by the Syncthing GUI as configured.
* \remarks Before Qt 5.14 any self-signed certificates are accepted.
*/
bool WebPage::certificateError(const QWebEngineCertificateError &certificateError)
bool WebPage::canIgnoreCertificateError(const QWebEngineCertificateError &certificateError) const
{
// never ignore errors other than CertificateCommonNameInvalid and CertificateAuthorityInvalid
switch (certificateError.error()) {
switch (certificateError
#if (QTWEBENGINEWIDGETS_VERSION >= QT_VERSION_CHECK(6, 0, 0))
.type()
#else
.error()
#endif
) {
case QWebEngineCertificateError::CertificateCommonNameInvalid:
case QWebEngineCertificateError::CertificateAuthorityInvalid:
break;
@ -148,6 +152,31 @@ bool WebPage::certificateError(const QWebEngineCertificateError &certificateErro
return true;
}
/*!
* \brief Accepts self-signed certificates used by the Syncthing GUI as configured.
* \remarks
* - Before Qt 5.14 any self-signed certificates are accepted.
* - The const_cast used in the Qt 6 version is most likely wrong and not how to the API is
* supposed to be used. The [documentation](https://doc-snapshots.qt.io/qt6-dev/qwebenginepage.html#certificateError)
* mentions one is able to ignore certificate errors but does not state how.
*/
#if (QTWEBENGINEWIDGETS_VERSION >= QT_VERSION_CHECK(6, 0, 0))
void WebPage::handleCertificateError(const QWebEngineCertificateError &certificateError)
{
auto &error = const_cast<QWebEngineCertificateError &>(certificateError);
if (canIgnoreCertificateError(certificateError)) {
error.acceptCertificate();
} else {
error.rejectCertificate();
}
}
#else
bool WebPage::certificateError(const QWebEngineCertificateError &certificateError)
{
return canIgnoreCertificateError(certificateError);
}
#endif
/*!
* \brief Accepts navigation requests only on the same page.
*/

View File

@ -30,11 +30,13 @@ public:
protected:
SYNCTHINGWIDGETS_WEB_PAGE *createWindow(WebWindowType type) override;
#ifdef SYNCTHINGWIDGETS_USE_WEBENGINE
bool certificateError(const QWebEngineCertificateError &certificateError) override;
#if (QTWEBENGINEWIDGETS_VERSION < QT_VERSION_CHECK(6, 0, 0))
bool certificateError(const QWebEngineCertificateError &certificateError) override; // signal in Qt >= 6
#endif
bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame) override;
void javaScriptConsoleMessage(
QWebEnginePage::JavaScriptConsoleMessageLevel level, const QString &message, int lineNumber, const QString &sourceID) override;
#else
#else // SYNCTHINGWIDGETS_USE_WEBKIT
bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type) override;
void javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID) override;
#endif
@ -44,7 +46,11 @@ private Q_SLOTS:
void supplyCredentials(const QUrl &requestUrl, QAuthenticator *authenticator);
void supplyCredentials(QNetworkReply *reply, QAuthenticator *authenticator);
void supplyCredentials(QAuthenticator *authenticator);
#ifdef SYNCTHINGWIDGETS_USE_WEBKIT
#ifdef SYNCTHINGWIDGETS_USE_WEBENGINE
#if (QTWEBENGINEWIDGETS_VERSION >= QT_VERSION_CHECK(6, 0, 0))
void handleCertificateError(const QWebEngineCertificateError &certificateError);
#endif
#else // SYNCTHINGWIDGETS_USE_WEBKIT
void handleSslErrors(QNetworkReply *, const QList<QSslError> &errors);
#endif
void injectJavaScripts(bool ok);
@ -53,6 +59,9 @@ private Q_SLOTS:
void showFolderPathSelection(const QString &defaultDir);
private:
#ifdef SYNCTHINGWIDGETS_USE_WEBENGINE
bool canIgnoreCertificateError(const QWebEngineCertificateError &certificateError) const;
#endif
static bool handleNavigationRequest(const QUrl &currentUrl, const QUrl &url);
WebViewDialog *m_dlg;

View File

@ -9,6 +9,7 @@
#if defined(SYNCTHINGWIDGETS_USE_WEBENGINE)
# include <QWebEngineView>
# include <QWebEnginePage>
# include <QtWebEngineWidgetsVersion>
#elif defined(SYNCTHINGWIDGETS_USE_WEBKIT)
# include <QWebView>
# include <QWebPage>