File indexing completed on 2024-06-16 04:39:30

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "ui_html5permissionsnotification.h"
0019 #include "html5permissionsnotification.h"
0020 #include "html5permissionsmanager.h"
0021 #include "mainapplication.h"
0022 #include "iconprovider.h"
0023 
0024 #include <QTimer>
0025 #include <QWebEnginePage>
0026 #include <QWebEngineView>
0027 #include <QtWebEngineWidgetsVersion>
0028 
0029 
0030 HTML5PermissionsNotification::HTML5PermissionsNotification(const QUrl &origin, QWebEnginePage* page, const QWebEnginePage::Feature &feature)
0031     : AnimatedWidget(AnimatedWidget::Down, 300, nullptr)
0032     , ui(new Ui::HTML5PermissionsNotification)
0033     , m_origin(origin)
0034     , m_page(page)
0035     , m_feature(feature)
0036 {
0037     setAutoFillBackground(true);
0038     ui->setupUi(widget());
0039 
0040     ui->close->setIcon(IconProvider::standardIcon(QStyle::SP_DialogCloseButton));
0041 
0042     const QString site = m_origin.host().isEmpty() ? tr("this site") : QSL("<b>%1</b>").arg(m_origin.host());
0043 
0044     switch (feature) {
0045     case QWebEnginePage::Notifications:
0046         ui->textLabel->setText(tr("Allow %1 to show desktop notifications?").arg(site));
0047         break;
0048 
0049     case QWebEnginePage::Geolocation:
0050         ui->textLabel->setText(tr("Allow %1 to locate your position?").arg(site));
0051         break;
0052 
0053     case QWebEnginePage::MediaAudioCapture:
0054         ui->textLabel->setText(tr("Allow %1 to use your microphone?").arg(site));
0055         break;
0056 
0057     case QWebEnginePage::MediaVideoCapture:
0058         ui->textLabel->setText(tr("Allow %1 to use your camera?").arg(site));
0059         break;
0060 
0061     case QWebEnginePage::MediaAudioVideoCapture:
0062         ui->textLabel->setText(tr("Allow %1 to use your microphone and camera?").arg(site));
0063         break;
0064 
0065     case QWebEnginePage::MouseLock:
0066         ui->textLabel->setText(tr("Allow %1 to hide your pointer?").arg(site));
0067         break;
0068 
0069     case QWebEnginePage::DesktopVideoCapture:
0070         ui->textLabel->setText(tr("Allow %1 to capture your screen?").arg(site));
0071         break;
0072 
0073     case QWebEnginePage::DesktopAudioVideoCapture:
0074         ui->textLabel->setText(tr("Allow %1 to capture your screen and audio?").arg(site));
0075         break;
0076     default:
0077         qWarning() << "Unknown feature" << feature;
0078         break;
0079     }
0080 
0081     connect(ui->allow, &QAbstractButton::clicked, this, &HTML5PermissionsNotification::grantPermissions);
0082     connect(ui->deny, &QAbstractButton::clicked, this, &HTML5PermissionsNotification::denyPermissions);
0083     connect(ui->close, &QAbstractButton::clicked, this, &HTML5PermissionsNotification::denyPermissions);
0084 
0085     connect(m_page.data(), &QWebEnginePage::loadStarted, this, &QObject::deleteLater);
0086     connect(m_page.data(), &QWebEnginePage::featurePermissionRequestCanceled, this, [this](const QUrl &origin, QWebEnginePage::Feature feature) {
0087         if (origin == m_origin && feature == m_feature) {
0088             deleteLater();
0089         }
0090     });
0091 
0092     startAnimation();
0093 }
0094 
0095 void HTML5PermissionsNotification::grantPermissions()
0096 {
0097     if (!m_page) {
0098         return;
0099     }
0100 
0101     QTimer::singleShot(0, this, [this]() {
0102         // We need to have cursor inside view to correctly grab mouse
0103         if (m_feature == QWebEnginePage::MouseLock) {
0104             QWidget *view = QWebEngineView::forPage(m_page);
0105             QCursor::setPos(view->mapToGlobal(view->rect().center()));
0106         }
0107 
0108         m_page->setFeaturePermission(m_origin, m_feature, QWebEnginePage::PermissionGrantedByUser);
0109     });
0110 
0111     if (ui->remember->isChecked()) {
0112         mApp->html5PermissionsManager()->rememberPermissions(m_origin, m_feature, QWebEnginePage::PermissionGrantedByUser);
0113     }
0114 
0115     hide();
0116 }
0117 
0118 void HTML5PermissionsNotification::denyPermissions()
0119 {
0120     if (!m_page) {
0121         return;
0122     }
0123 
0124     m_page->setFeaturePermission(m_origin, m_feature, QWebEnginePage::PermissionDeniedByUser);
0125 
0126     if (ui->remember->isChecked()) {
0127         mApp->html5PermissionsManager()->rememberPermissions(m_origin, m_feature, QWebEnginePage::PermissionDeniedByUser);
0128     }
0129 
0130     hide();
0131 }
0132 
0133 HTML5PermissionsNotification::~HTML5PermissionsNotification()
0134 {
0135     delete ui;
0136 }