summaryrefslogtreecommitdiff
path: root/examples/webenginewidgets/simplebrowser/webview.cpp
diff options
context:
space:
mode:
authorMichal Klocek <michal.klocek@qt.io>2020-10-19 09:16:44 +0200
committerMichal Klocek <michal.klocek@qt.io>2020-11-20 15:52:40 +0100
commite3381218acdbec1e9c207ef3eb35371e22941257 (patch)
treee8bf073b707c51f7cdd5efe55f7e4962de90980f /examples/webenginewidgets/simplebrowser/webview.cpp
parenta54d219c3e3cb7c73be03413741bf8d2ef70c9fa (diff)
downloadqtwebengine-e3381218acdbec1e9c207ef3eb35371e22941257.tar.gz
Remove qwebengineview setter from qwebenginepage
Change-Id: Ifaac7287d24e38e04e217cae65f6e5294e8bdd9e Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'examples/webenginewidgets/simplebrowser/webview.cpp')
-rw-r--r--examples/webenginewidgets/simplebrowser/webview.cpp150
1 files changed, 150 insertions, 0 deletions
diff --git a/examples/webenginewidgets/simplebrowser/webview.cpp b/examples/webenginewidgets/simplebrowser/webview.cpp
index 28ae8eecd..5bd0a5ef4 100644
--- a/examples/webenginewidgets/simplebrowser/webview.cpp
+++ b/examples/webenginewidgets/simplebrowser/webview.cpp
@@ -54,11 +54,15 @@
#include "webpage.h"
#include "webpopupwindow.h"
#include "webview.h"
+#include "ui_certificateerrordialog.h"
+#include "ui_passworddialog.h"
#include <QContextMenuEvent>
#include <QDebug>
#include <QMenu>
#include <QMessageBox>
+#include <QAuthenticator>
#include <QTimer>
+#include <QStyle>
WebView::WebView(QWidget *parent)
: QWebEngineView(parent)
@@ -104,13 +108,56 @@ WebView::WebView(QWidget *parent)
});
}
+inline QString questionForFeature(QWebEnginePage::Feature feature)
+{
+ switch (feature) {
+ case QWebEnginePage::Geolocation:
+ return QObject::tr("Allow %1 to access your location information?");
+ case QWebEnginePage::MediaAudioCapture:
+ return QObject::tr("Allow %1 to access your microphone?");
+ case QWebEnginePage::MediaVideoCapture:
+ return QObject::tr("Allow %1 to access your webcam?");
+ case QWebEnginePage::MediaAudioVideoCapture:
+ return QObject::tr("Allow %1 to access your microphone and webcam?");
+ case QWebEnginePage::MouseLock:
+ return QObject::tr("Allow %1 to lock your mouse cursor?");
+ case QWebEnginePage::DesktopVideoCapture:
+ return QObject::tr("Allow %1 to capture video of your desktop?");
+ case QWebEnginePage::DesktopAudioVideoCapture:
+ return QObject::tr("Allow %1 to capture audio and video of your desktop?");
+ case QWebEnginePage::Notifications:
+ return QObject::tr("Allow %1 to show notification on your desktop?");
+ }
+ return QString();
+}
+
void WebView::setPage(WebPage *page)
{
+ WebPage *oldPage = qobject_cast<WebPage *>(QWebEngineView::page());
+ disconnect(oldPage, &WebPage::createCertificateErrorDialog, this,
+ &WebView::handleCertificateError);
+ disconnect(oldPage, &QWebEnginePage::authenticationRequired, this,
+ &WebView::handleAuthenticationRequired);
+ disconnect(oldPage, &QWebEnginePage::featurePermissionRequested, this,
+ &WebView::handleFeaturePermissionRequested);
+ disconnect(oldPage, &QWebEnginePage::proxyAuthenticationRequired, this,
+ &WebView::handleProxyAuthenticationRequired);
+ disconnect(oldPage, &QWebEnginePage::registerProtocolHandlerRequested, this,
+ &WebView::handleRegisterProtocolHandlerRequested);
createWebActionTrigger(page,QWebEnginePage::Forward);
createWebActionTrigger(page,QWebEnginePage::Back);
createWebActionTrigger(page,QWebEnginePage::Reload);
createWebActionTrigger(page,QWebEnginePage::Stop);
QWebEngineView::setPage(page);
+ connect(page, &WebPage::createCertificateErrorDialog, this, &WebView::handleCertificateError);
+ connect(page, &QWebEnginePage::authenticationRequired, this,
+ &WebView::handleAuthenticationRequired);
+ connect(page, &QWebEnginePage::featurePermissionRequested, this,
+ &WebView::handleFeaturePermissionRequested);
+ connect(page, &QWebEnginePage::proxyAuthenticationRequired, this,
+ &WebView::handleProxyAuthenticationRequired);
+ connect(page, &QWebEnginePage::registerProtocolHandlerRequested, this,
+ &WebView::handleRegisterProtocolHandlerRequested);
}
int WebView::loadProgress() const
@@ -196,3 +243,106 @@ void WebView::contextMenuEvent(QContextMenuEvent *event)
menu->popup(event->globalPos());
}
+void WebView::handleCertificateError(QWebEngineCertificateError error)
+{
+ QDialog dialog(window());
+ dialog.setModal(true);
+ dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
+
+ Ui::CertificateErrorDialog certificateDialog;
+ certificateDialog.setupUi(&dialog);
+ certificateDialog.m_iconLabel->setText(QString());
+ QIcon icon(window()->style()->standardIcon(QStyle::SP_MessageBoxWarning, 0, window()));
+ certificateDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32));
+ certificateDialog.m_errorLabel->setText(error.description());
+ dialog.setWindowTitle(tr("Certificate Error"));
+
+ if (dialog.exec() == QDialog::Accepted)
+ error.acceptCertificate();
+ else
+ error.rejectCertificate();
+}
+
+void WebView::handleAuthenticationRequired(const QUrl &requestUrl, QAuthenticator *auth)
+{
+ QDialog dialog(window());
+ dialog.setModal(true);
+ dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
+
+ Ui::PasswordDialog passwordDialog;
+ passwordDialog.setupUi(&dialog);
+
+ passwordDialog.m_iconLabel->setText(QString());
+ QIcon icon(window()->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, window()));
+ passwordDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32));
+
+ QString introMessage(tr("Enter username and password for \"%1\" at %2")
+ .arg(auth->realm())
+ .arg(requestUrl.toString().toHtmlEscaped()));
+ passwordDialog.m_infoLabel->setText(introMessage);
+ passwordDialog.m_infoLabel->setWordWrap(true);
+
+ if (dialog.exec() == QDialog::Accepted) {
+ auth->setUser(passwordDialog.m_userNameLineEdit->text());
+ auth->setPassword(passwordDialog.m_passwordLineEdit->text());
+ } else {
+ // Set authenticator null if dialog is cancelled
+ *auth = QAuthenticator();
+ }
+}
+
+void WebView::handleFeaturePermissionRequested(const QUrl &securityOrigin,
+ QWebEnginePage::Feature feature)
+{
+ QString title = tr("Permission Request");
+ QString question = questionForFeature(feature).arg(securityOrigin.host());
+ if (!question.isEmpty() && QMessageBox::question(window(), title, question) == QMessageBox::Yes)
+ page()->setFeaturePermission(securityOrigin, feature,
+ QWebEnginePage::PermissionGrantedByUser);
+ else
+ page()->setFeaturePermission(securityOrigin, feature,
+ QWebEnginePage::PermissionDeniedByUser);
+}
+
+void WebView::handleProxyAuthenticationRequired(const QUrl &, QAuthenticator *auth,
+ const QString &proxyHost)
+{
+ QDialog dialog(window());
+ dialog.setModal(true);
+ dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
+
+ Ui::PasswordDialog passwordDialog;
+ passwordDialog.setupUi(&dialog);
+
+ passwordDialog.m_iconLabel->setText(QString());
+ QIcon icon(window()->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, window()));
+ passwordDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32));
+
+ QString introMessage = tr("Connect to proxy \"%1\" using:");
+ introMessage = introMessage.arg(proxyHost.toHtmlEscaped());
+ passwordDialog.m_infoLabel->setText(introMessage);
+ passwordDialog.m_infoLabel->setWordWrap(true);
+
+ if (dialog.exec() == QDialog::Accepted) {
+ auth->setUser(passwordDialog.m_userNameLineEdit->text());
+ auth->setPassword(passwordDialog.m_passwordLineEdit->text());
+ } else {
+ // Set authenticator null if dialog is cancelled
+ *auth = QAuthenticator();
+ }
+}
+
+//! [registerProtocolHandlerRequested]
+void WebView::handleRegisterProtocolHandlerRequested(
+ QWebEngineRegisterProtocolHandlerRequest request)
+{
+ auto answer = QMessageBox::question(window(), tr("Permission Request"),
+ tr("Allow %1 to open all %2 links?")
+ .arg(request.origin().host())
+ .arg(request.scheme()));
+ if (answer == QMessageBox::Yes)
+ request.accept();
+ else
+ request.reject();
+}
+//! [registerProtocolHandlerRequested]