/* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original * location: https://neueland.iserlohn-fortress.net/smolbote.hg * * SPDX-License-Identifier: GPL-3.0 */ #include "webpage.h" #include #include #include #include QString tr_terminationStatus(QWebEnginePage::RenderProcessTerminationStatus status) { switch (status) { case QWebEnginePage::NormalTerminationStatus: return QObject::tr("The render process terminated normally."); case QWebEnginePage::AbnormalTerminationStatus: return QObject::tr("The render process terminated with with a non-zero exit status."); case QWebEnginePage::CrashedTerminationStatus: return QObject::tr("The render process crashed, for example because of a segmentation fault."); case QWebEnginePage::KilledTerminationStatus: return QObject::tr("The render process was killed, for example by SIGKILL or task manager kill."); } } QString feature_toString(QWebEnginePage::Feature feature) { switch(feature) { case QWebEnginePage::Notifications: return QObject::tr("Notifications"); case QWebEnginePage::Geolocation: return QObject::tr("Geolocation"); case QWebEnginePage::MediaAudioCapture: return QObject::tr("Audio Capture"); case QWebEnginePage::MediaVideoCapture: return QObject::tr("Video Capture"); case QWebEnginePage::MediaAudioVideoCapture: return QObject::tr("Audio and Video Capture"); case QWebEnginePage::MouseLock: return QObject::tr("Mouse Lock"); case QWebEnginePage::DesktopVideoCapture: return QObject::tr("Desktop Video Capture"); case QWebEnginePage::DesktopAudioVideoCapture: return QObject::tr("Desktop Audio and Video Capture"); } } WebPage::WebPage(QWebEngineProfile *profile, QObject *parent) : QWebEnginePage(profile, parent) { connect(this, &WebPage::fullScreenRequested, this, [](QWebEngineFullScreenRequest request) { request.accept(); }); connect(this, &QWebEnginePage::featurePermissionRequested, this, &WebPage::featurePermissionDialog); connect(this, &QWebEnginePage::renderProcessTerminated, this, &WebPage::renderProcessCrashed); } bool WebPage::certificateError(const QWebEngineCertificateError &certificateError) { QMessageBox messageBox; messageBox.setWindowTitle(tr("SSL Error")); if(certificateError.isOverridable()) messageBox.setIcon(QMessageBox::Warning); else messageBox.setIcon(QMessageBox::Critical); messageBox.setText(tr("An SSL error has occurred on %1").arg(certificateError.url().toString())); messageBox.setInformativeText(tr("

%1

" "

This error %2 be overridden.

") .arg(certificateError.errorDescription(), certificateError.isOverridable() ? tr("can") : tr("cannot"))); messageBox.setDetailedText(tr("Error code: %1").arg(certificateError.error())); if(certificateError.isOverridable()) { messageBox.setStandardButtons(QMessageBox::Ignore | QMessageBox::Abort); messageBox.setDefaultButton(QMessageBox::Ignore); } else messageBox.setStandardButtons(QMessageBox::Abort); auto resp = messageBox.exec(); return resp == QMessageBox::Ignore; } #ifdef QTBUG_68224_WORKAROUND bool WebPage::acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame) { /* Workaround for https://bugreports.qt.io/browse/QTBUG-68224 * Only affects 5.11.0; should be fixed in 5.11.1 */ auto *layout = this->view()->layout(); auto count = layout->count(); if(count > 1) { for(int i = 0; i < count; ++i) { auto *item = layout->itemAt(i); if(item == nullptr) continue; auto *widget = item->widget(); if(widget != this->view()->focusProxy()) { //qDebug("Removing widget"); layout->removeWidget(widget); } } } return true; } #endif void WebPage::featurePermissionDialog(const QUrl &securityOrigin, QWebEnginePage::Feature feature) { QMessageBox messageBox; messageBox.setWindowTitle(tr("Feature permission request")); messageBox.setIcon(QMessageBox::Question); messageBox.setText(tr("

The webpage %1 has requested permission to access: %2

" "

Allow this feature?

") .arg(securityOrigin.toString(), feature_toString(feature))); messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); messageBox.setDefaultButton(QMessageBox::No); if(messageBox.exec() == QMessageBox::Yes) setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser); else setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionDeniedByUser); } void WebPage::renderProcessCrashed(QWebEnginePage::RenderProcessTerminationStatus terminationStatus, int exitCode) { if(terminationStatus != QWebEnginePage::NormalTerminationStatus) { #ifdef QT_DEBUG qDebug("render process terminated: [%i] %i", terminationStatus, exitCode); #endif QString page = "

This tab has crashed!

%message%"; page.replace(QLatin1String("%message%"), QString("

%1
Exit code is %2.

" "

Press here to reload this tab.

").arg(tr_terminationStatus(terminationStatus), QString::number(exitCode), this->url().toEncoded())); QTimer::singleShot(0, this, [=]() { setHtml(page.toUtf8(), url()); }); } }