Commit 194204a8 authored by Stefano Crocco's avatar Stefano Crocco Committed by David Faure
Browse files

Improve feature permission request

QtWebEngine only seems to support site-wide rather than page-wide
feature permissions, since the URL passed by the
featurePermissionRequest signal always has / as path. Since the
permission bar was only shown if this URL is equal to the page's URL
(which includes the path), they were almost never shown and the feature
request was automatically denied (except for notifications). This is now
fixed by comparing the request URL with an URL with / as path.

Besides, the feature permission bar always had the same label (referring
to geolocalization). Now the label has a different text according to the
feature.

Multiple permission bars can now be displayed at once.
parent ce77d0c5
......@@ -53,21 +53,54 @@ QWebEnginePage::Feature FeaturePermissionBar::feature() const
return m_feature;
}
QUrl FeaturePermissionBar::url() const
{
return m_url;
}
void FeaturePermissionBar::setUrl(const QUrl& url)
{
m_url = url;
}
QString FeaturePermissionBar::labelForFeature(QWebEnginePage::Feature feature)
{
switch (feature) {
case QWebEnginePage::Notifications:
return i18n("<html>Do you want to allow the site to send you notifications?");
case QWebEnginePage::Geolocation:
return i18n("<html>Do you want to grant the site access to information about your current physical location?");
case QWebEnginePage::MediaAudioCapture:
return i18n("<html>Do you want to allow the site to access your microphone and other audio capture devices?");
case QWebEnginePage::MediaVideoCapture:
return i18n("<html>Do you want to allow the site to access your camera and other video capture devices?");
case QWebEnginePage::MediaAudioVideoCapture:
return i18n("<html>Do you want to allow the site to access to your microphone, camera and other audio and video capture devices?");
case QWebEnginePage::MouseLock:
return i18n("<html>Do you want to allow the site to lock your mouse inside the web page?");
case QWebEnginePage::DesktopVideoCapture:
return i18n("<html>Do you want to allow the site to record your screen?");
case QWebEnginePage::DesktopAudioVideoCapture:
return i18n("<html>Do you want to allow the site to record your screen and your audio?");
}
}
void FeaturePermissionBar::setFeature (QWebEnginePage::Feature feature)
{
m_feature = feature;
setText(labelForFeature(feature));
}
void FeaturePermissionBar::onDeniedButtonClicked()
{
animatedHide();
emit permissionDenied(m_feature);
emit permissionPolicyChosen(m_feature, QWebEnginePage::PermissionDeniedByUser);
emit done();
}
void FeaturePermissionBar::onGrantedButtonClicked()
{
animatedHide();
emit permissionGranted(m_feature);
emit permissionPolicyChosen(m_feature, QWebEnginePage::PermissionGrantedByUser);
emit done();
}
......@@ -23,8 +23,10 @@
#define FEATUREPERMISSIONBAR_H
#include <KMessageWidget>
#include <KLocalizedString>
#include <QWebEnginePage>
#include <QUrl>
class FeaturePermissionBar : public KMessageWidget
......@@ -35,20 +37,25 @@ public:
~FeaturePermissionBar() override;
QWebEnginePage::Feature feature() const;
QUrl url() const;
void setFeature(QWebEnginePage::Feature);
void setUrl(const QUrl &url);
Q_SIGNALS:
void permissionGranted(QWebEnginePage::Feature);
void permissionDenied(QWebEnginePage::Feature);
void permissionPolicyChosen(QWebEnginePage::Feature feature, QWebEnginePage::PermissionPolicy policy);
void done();
private Q_SLOTS:
void onDeniedButtonClicked();
void onGrantedButtonClicked();
private:
static QString labelForFeature(QWebEnginePage::Feature feature);
private:
QWebEnginePage::Feature m_feature;
QUrl m_url;
};
#endif // FEATUREPERMISSIONBAR_H
......@@ -489,8 +489,11 @@ void WebEnginePage::slotUnsupportedContent(QNetworkReply* reply)
}
void WebEnginePage::slotFeaturePermissionRequested(const QUrl& url, QWebEnginePage::Feature feature)
{
if (url == this->url()) {
part()->slotShowFeaturePermissionBar(feature);
//url.path() is always / (meaning that permissions should be granted site-wide and not per page)
QUrl thisUrl(this->url());
thisUrl.setPath("/");
if (url == thisUrl) {
part()->slotShowFeaturePermissionBar(url, feature);
return;
}
switch(feature) {
......
......@@ -116,7 +116,6 @@ WebEnginePart::WebEnginePart(QWidget *parentWidget, QObject *parent,
m_statusBarWalletLabel(nullptr),
m_searchBar(nullptr),
m_passwordBar(nullptr),
m_featurePermissionBar(nullptr),
m_wallet(nullptr)
{
initWebEngineUrlSchemes();
......@@ -899,44 +898,32 @@ void WebEnginePart::slotWindowCloseRequested()
this->deleteLater();
}
void WebEnginePart::slotShowFeaturePermissionBar(QWebEnginePage::Feature feature)
void WebEnginePart::slotShowFeaturePermissionBar(const QUrl &origin, QWebEnginePage::Feature feature)
{
// FIXME: Allow multiple concurrent feature permission requests.
if (m_featurePermissionBar && m_featurePermissionBar->isVisible())
return;
if (!m_featurePermissionBar) {
m_featurePermissionBar = new FeaturePermissionBar(widget());
connect(m_featurePermissionBar, &FeaturePermissionBar::permissionGranted,
this, &WebEnginePart::slotFeaturePermissionGranted);
connect(m_featurePermissionBar, &FeaturePermissionBar::permissionDenied,
this, &WebEnginePart::slotFeaturePermissionDenied);
connect(m_passwordBar, &PasswordBar::done,
this, &WebEnginePart::slotSaveFormDataDone);
QBoxLayout* lay = qobject_cast<QBoxLayout*>(widget()->layout());
if (lay)
lay->insertWidget(0, m_featurePermissionBar);
FeaturePermissionBar *bar = new FeaturePermissionBar(widget());
auto policyLambda = [this, bar](QWebEnginePage::Feature feature, QWebEnginePage::PermissionPolicy policy) {
slotFeaturePolicyChosen(bar, feature, policy);
};
connect(bar, &FeaturePermissionBar::permissionPolicyChosen, this, policyLambda);
connect(bar, &FeaturePermissionBar::done, this, [this, bar](){deleteFeaturePermissionBar(bar);});
QBoxLayout* lay = qobject_cast<QBoxLayout*>(widget()->layout());
if (lay) {
lay->insertWidget(0, bar);
}
m_featurePermissionBar->setFeature(feature);
// m_featurePermissionBar->setText(i18n("<html>Do you want to grant the site <b>%1</b> "
// "access to information about your current physical location?",
// url.host()));
m_featurePermissionBar->setText(i18n("<html>Do you want to grant the site "
"access to information about your current physical location?"));
m_featurePermissionBar->animatedShow();
bar->setUrl(origin);
bar->setFeature(feature);
bar->animatedShow();
}
void WebEnginePart::slotFeaturePermissionGranted(QWebEnginePage::Feature feature)
void WebEnginePart::slotFeaturePolicyChosen(FeaturePermissionBar* bar, QWebEnginePage::Feature feature, QWebEnginePage::PermissionPolicy policy)
{
Q_ASSERT(m_featurePermissionBar && m_featurePermissionBar->feature() == feature);
page()->setFeaturePermission(page()->url(), feature, QWebEnginePage::PermissionGrantedByUser);
Q_ASSERT(bar && bar->feature() == feature);
page()->setFeaturePermission(bar->url(), feature, policy);
}
void WebEnginePart::slotFeaturePermissionDenied(QWebEnginePage::Feature feature)
void WebEnginePart::deleteFeaturePermissionBar(FeaturePermissionBar *bar)
{
Q_ASSERT(m_featurePermissionBar && m_featurePermissionBar->feature() == feature);
page()->setFeaturePermission(page()->url(), feature, QWebEnginePage::PermissionDeniedByUser);
bar->deleteLater();
}
void WebEnginePart::slotSaveFormDataRequested (const QString& key, const QUrl& url)
......
......@@ -98,7 +98,7 @@ public:
*/
void connectWebEnginePageSignals(WebEnginePage* page);
void slotShowFeaturePermissionBar(QWebEnginePage::Feature);
void slotShowFeaturePermissionBar(const QUrl &origin, QWebEnginePage::Feature);
void setWallet(WebEngineWallet* wallet);
......@@ -148,8 +148,8 @@ private Q_SLOTS:
void slotWalletSavedForms(const QUrl &url, bool success);
void slotFillFormRequestCompleted(bool);
void slotFeaturePermissionGranted(QWebEnginePage::Feature);
void slotFeaturePermissionDenied(QWebEnginePage::Feature);
void slotFeaturePolicyChosen(FeaturePermissionBar *bar, QWebEnginePage::Feature feature, QWebEnginePage::PermissionPolicy policy);
void deleteFeaturePermissionBar(FeaturePermissionBar *bar);
void updateWalletStatusBarIcon();
void walletFinishedFormDetection(const QUrl &url, bool found, bool autoFillableFound);
......@@ -184,7 +184,6 @@ private:
KUrlLabel* m_statusBarWalletLabel;
SearchBar* m_searchBar;
PasswordBar* m_passwordBar;
FeaturePermissionBar* m_featurePermissionBar;
WebEngineBrowserExtension* m_browserExtension;
KParts::StatusBarExtension* m_statusBarExtension;
WebEngineView* m_webView;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment