1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 #include "browserwindow.h"
52 #include "tabwidget.h"
53 #include "ui_certificateerrordialog.h"
54 #include "ui_passworddialog.h"
55 #include "webpage.h"
56 #include "webview.h"
57 #include <QAuthenticator>
58 #include <QMessageBox>
59 #include <QStyle>
60 #include <QWebEngineCertificateError>
61
WebPage(QWebEngineProfile * profile,QObject * parent)62 WebPage::WebPage(QWebEngineProfile *profile, QObject *parent)
63 : QWebEnginePage(profile, parent)
64 {
65 connect(this, &QWebEnginePage::authenticationRequired, this, &WebPage::handleAuthenticationRequired);
66 connect(this, &QWebEnginePage::featurePermissionRequested, this, &WebPage::handleFeaturePermissionRequested);
67 connect(this, &QWebEnginePage::proxyAuthenticationRequired, this, &WebPage::handleProxyAuthenticationRequired);
68 connect(this, &QWebEnginePage::registerProtocolHandlerRequested, this, &WebPage::handleRegisterProtocolHandlerRequested);
69 #if !defined(QT_NO_SSL) || QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
70 connect(this, &QWebEnginePage::selectClientCertificate, this, &WebPage::handleSelectClientCertificate);
71 #endif
72 }
73
certificateError(const QWebEngineCertificateError & error)74 bool WebPage::certificateError(const QWebEngineCertificateError &error)
75 {
76 QWidget *mainWindow = view()->window();
77 if (error.isOverridable()) {
78 QDialog dialog(mainWindow);
79 dialog.setModal(true);
80 dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
81 Ui::CertificateErrorDialog certificateDialog;
82 certificateDialog.setupUi(&dialog);
83 certificateDialog.m_iconLabel->setText(QString());
84 QIcon icon(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxWarning, 0, mainWindow));
85 certificateDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32));
86 certificateDialog.m_errorLabel->setText(error.errorDescription());
87 dialog.setWindowTitle(tr("Certificate Error"));
88 return dialog.exec() == QDialog::Accepted;
89 }
90
91 QMessageBox::critical(mainWindow, tr("Certificate Error"), error.errorDescription());
92 return false;
93 }
94
handleAuthenticationRequired(const QUrl & requestUrl,QAuthenticator * auth)95 void WebPage::handleAuthenticationRequired(const QUrl &requestUrl, QAuthenticator *auth)
96 {
97 QWidget *mainWindow = view()->window();
98 QDialog dialog(mainWindow);
99 dialog.setModal(true);
100 dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
101
102 Ui::PasswordDialog passwordDialog;
103 passwordDialog.setupUi(&dialog);
104
105 passwordDialog.m_iconLabel->setText(QString());
106 QIcon icon(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow));
107 passwordDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32));
108
109 QString introMessage(tr("Enter username and password for \"%1\" at %2")
110 .arg(auth->realm()).arg(requestUrl.toString().toHtmlEscaped()));
111 passwordDialog.m_infoLabel->setText(introMessage);
112 passwordDialog.m_infoLabel->setWordWrap(true);
113
114 if (dialog.exec() == QDialog::Accepted) {
115 auth->setUser(passwordDialog.m_userNameLineEdit->text());
116 auth->setPassword(passwordDialog.m_passwordLineEdit->text());
117 } else {
118 // Set authenticator null if dialog is cancelled
119 *auth = QAuthenticator();
120 }
121 }
122
questionForFeature(QWebEnginePage::Feature feature)123 inline QString questionForFeature(QWebEnginePage::Feature feature)
124 {
125 switch (feature) {
126 case QWebEnginePage::Geolocation:
127 return WebPage::tr("Allow %1 to access your location information?");
128 case QWebEnginePage::MediaAudioCapture:
129 return WebPage::tr("Allow %1 to access your microphone?");
130 case QWebEnginePage::MediaVideoCapture:
131 return WebPage::tr("Allow %1 to access your webcam?");
132 case QWebEnginePage::MediaAudioVideoCapture:
133 return WebPage::tr("Allow %1 to access your microphone and webcam?");
134 case QWebEnginePage::MouseLock:
135 return WebPage::tr("Allow %1 to lock your mouse cursor?");
136 case QWebEnginePage::DesktopVideoCapture:
137 return WebPage::tr("Allow %1 to capture video of your desktop?");
138 case QWebEnginePage::DesktopAudioVideoCapture:
139 return WebPage::tr("Allow %1 to capture audio and video of your desktop?");
140 case QWebEnginePage::Notifications:
141 return QString();
142 }
143 return QString();
144 }
145
handleFeaturePermissionRequested(const QUrl & securityOrigin,Feature feature)146 void WebPage::handleFeaturePermissionRequested(const QUrl &securityOrigin, Feature feature)
147 {
148 QString title = tr("Permission Request");
149 QString question = questionForFeature(feature).arg(securityOrigin.host());
150 if (!question.isEmpty() && QMessageBox::question(view()->window(), title, question) == QMessageBox::Yes)
151 setFeaturePermission(securityOrigin, feature, PermissionGrantedByUser);
152 else
153 setFeaturePermission(securityOrigin, feature, PermissionDeniedByUser);
154 }
155
handleProxyAuthenticationRequired(const QUrl &,QAuthenticator * auth,const QString & proxyHost)156 void WebPage::handleProxyAuthenticationRequired(const QUrl &, QAuthenticator *auth, const QString &proxyHost)
157 {
158 QWidget *mainWindow = view()->window();
159 QDialog dialog(mainWindow);
160 dialog.setModal(true);
161 dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
162
163 Ui::PasswordDialog passwordDialog;
164 passwordDialog.setupUi(&dialog);
165
166 passwordDialog.m_iconLabel->setText(QString());
167 QIcon icon(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow));
168 passwordDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32));
169
170 QString introMessage = tr("Connect to proxy \"%1\" using:");
171 introMessage = introMessage.arg(proxyHost.toHtmlEscaped());
172 passwordDialog.m_infoLabel->setText(introMessage);
173 passwordDialog.m_infoLabel->setWordWrap(true);
174
175 if (dialog.exec() == QDialog::Accepted) {
176 auth->setUser(passwordDialog.m_userNameLineEdit->text());
177 auth->setPassword(passwordDialog.m_passwordLineEdit->text());
178 } else {
179 // Set authenticator null if dialog is cancelled
180 *auth = QAuthenticator();
181 }
182 }
183
184 //! [registerProtocolHandlerRequested]
handleRegisterProtocolHandlerRequested(QWebEngineRegisterProtocolHandlerRequest request)185 void WebPage::handleRegisterProtocolHandlerRequested(QWebEngineRegisterProtocolHandlerRequest request)
186 {
187 auto answer = QMessageBox::question(
188 view()->window(),
189 tr("Permission Request"),
190 tr("Allow %1 to open all %2 links?")
191 .arg(request.origin().host())
192 .arg(request.scheme()));
193 if (answer == QMessageBox::Yes)
194 request.accept();
195 else
196 request.reject();
197 }
198 //! [registerProtocolHandlerRequested]
199
200 #if !defined(QT_NO_SSL) || QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
handleSelectClientCertificate(QWebEngineClientCertificateSelection selection)201 void WebPage::handleSelectClientCertificate(QWebEngineClientCertificateSelection selection)
202 {
203 // Just select one.
204 selection.select(selection.certificates().at(0));
205 }
206 #endif
207