forked from intersimone999/qtws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtwswebpage.cpp
64 lines (54 loc) · 2.47 KB
/
qtwswebpage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "qtwswebpage.h"
#include "qtws.h"
#include <QDesktopServices>
#include <QUrl>
#include <QWebEnginePage>
QtWSWebPage::QtWSWebPage(QtWS *config) {
configHandler = config;
// connect(this, SIGNAL(featurePermissionRequested(QUrl,QWebEnginePage::Feature)), this, SLOT(checkPermission(QUrl&, QWebEnginePage::Feature)));
connect(this, &QWebEnginePage::featurePermissionRequested, this, [this](const QUrl &url, QWebEnginePage::Feature permission) {
if (this->configHandler->hasPermission(permission)) {
qWarning() << QString("Automatically granted permission ") + permission;
this->setFeaturePermission(url, permission, QWebEnginePage::PermissionGrantedByUser);
} else {
qWarning() << QString("Automatically refused permission ") + permission;
this->setFeaturePermission(url, permission, QWebEnginePage::PermissionDeniedByUser);
}
});
}
QWebEnginePage *QtWSWebPage::createWindow(QWebEnginePage::WebWindowType type) {
QWebEnginePage *fakePage = new QWebEnginePage(this);
connect(fakePage, &QWebEnginePage::urlChanged, this, [this, fakePage](QUrl url) {
if (!this->configHandler->isInScope(url)) {
if (url.scheme() != "about" && url.scheme() != "") {
QDesktopServices::openUrl(url);
fakePage->triggerAction(QWebEnginePage::Stop);
fakePage->triggerAction(QWebEnginePage::RequestClose);
}
} else {
this->setUrl(url);
}
});
connect(fakePage, &QWebEnginePage::windowCloseRequested, this, []() {
qWarning() << tr("CLOSED!");
});
return fakePage;
}
bool QtWSWebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) {
if (type == QWebEnginePage::NavigationTypeLinkClicked &&
!this->configHandler->isInScope(url) &&
isMainFrame) {
QDesktopServices::openUrl(url);
return false;
}
return true;
}
void QtWSWebPage::checkPermission(QUrl &url, QWebEnginePage::Feature permission) {
if (this->configHandler->hasPermission(permission)) {
qWarning() << QString("Automatically granted permission ") + permission;
this->setFeaturePermission(url, permission, QWebEnginePage::PermissionGrantedByUser);
} else {
qWarning() << QString("Automatically refused permission ") + permission;
this->setFeaturePermission(url, permission, QWebEnginePage::PermissionDeniedByUser);
}
}