1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 09:32:12 +01:00

Add a little framework for webAttributes

Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
This commit is contained in:
Juraj Oravec 2022-09-15 04:24:34 +02:00
parent 4c9213bc30
commit a2380e0b24
Signed by: SGOrava
GPG Key ID: 13660A3F1D9F093B
3 changed files with 112 additions and 3 deletions

View File

@ -26,6 +26,32 @@ SiteSettingsManager::SiteSettingsManager ( QObject* parent )
: QObject(parent)
{
loadSettings();
supportedAttribute.append(QWebEngineSettings::AutoLoadImages);
supportedAttribute.append(QWebEngineSettings::JavascriptEnabled);
// supportedAttribute.append(QWebEngineSettings::JavascriptCanOpenWindows);
// supportedAttribute.append(QWebEngineSettings::JavascriptCanAccessClipboard);
// supportedAttribute.append(QWebEngineSettings::JavascriptCanPaste);
// supportedAttribute.append(QWebEngineSettings::AllowWindowActivationFromJavaScript);
// supportedAttribute.append(QWebEngineSettings::LocalStorageEnabled);
// supportedAttribute.append(QWebEngineSettings::ScrollAnimatorEnabled);
// supportedAttribute.append(QWebEngineSettings::FullScreenSupportEnabled);
// supportedAttribute.append(QWebEngineSettings::AllowRunningInsecureContent);
// supportedAttribute.append(QWebEngineSettings::AllowGeolocationOnInsecureOrigins);
// supportedAttribute.append(QWebEngineSettings::PlaybackRequiresUserGesture);
// supportedAttribute.append(QWebEngineSettings::WebRTCPublicInterfacesOnly);
attributesSql = QSL("SELECT ");
for (int i = 0; i < supportedAttribute.size(); ++i) {
if (i > 0) {
attributesSql.append(QSL(", "));
}
attributesSql.append(webAttributeToSqlColumn(supportedAttribute[i]));
}
attributesSql.append(QSL(" FROM site_settings WHERE server=?"));
}
SiteSettingsManager::~SiteSettingsManager() noexcept
@ -128,6 +154,32 @@ SiteWebEngineSettings SiteSettingsManager::getWebEngineSettings(const QUrl& url)
return settings;
}
QHash<QWebEngineSettings::WebAttribute, bool> SiteSettingsManager::getWebAttributes2(const QUrl& url)
{
QHash<QWebEngineSettings::WebAttribute, bool> attributes;
QSqlQuery query(SqlDatabase::instance()->database());
query.prepare(attributesSql);
query.addBindValue(url.host());
query.exec();
if (query.next()) {
for (int i = 0; i < query.record().count(); ++i) {
Permission perm = intToPermission(query.value(i).toInt());
QWebEngineSettings::WebAttribute attribute = supportedAttribute[i];
if (perm == Allow) {
attributes[attribute] = true;
}
else if (perm == Deny) {
attributes[attribute] = false;
}
}
}
return attributes;
}
QHash<QWebEngineSettings::WebAttribute, bool> SiteSettingsManager::getWebAttributes(const QUrl& url)
{
QHash<QWebEngineSettings::WebAttribute, bool> attributes;
@ -494,3 +546,43 @@ QWebEngineSettings::WebAttribute SiteSettingsManager::optionToAttribute(const Si
return QWebEngineSettings::JavascriptEnabled;
}
}
QString SiteSettingsManager::webAttributeToSqlColumn(const QWebEngineSettings::WebAttribute& attribute)
{
switch (attribute) {
case QWebEngineSettings::AutoLoadImages:
return QSL("allow_images");
// return QSL("wa_autoload_images");
case QWebEngineSettings::JavascriptEnabled:
return QSL("allow_javascript");
// return QSL("wa_js_enabled");
case QWebEngineSettings::JavascriptCanOpenWindows:
return QSL("wa_js_open_windows");
case QWebEngineSettings::JavascriptCanAccessClipboard:
return QSL("wa_js_access_clipboard");
case QWebEngineSettings::JavascriptCanPaste:
return QSL("wa_js_can_paste");
case QWebEngineSettings::AllowWindowActivationFromJavaScript:
return QSL("wa_js_window_activation");
case QWebEngineSettings::LocalStorageEnabled:
return QSL("wa_local_storage");
case QWebEngineSettings::ScrollAnimatorEnabled:
return QSL("wa_acroll_animator");
case QWebEngineSettings::FullScreenSupportEnabled:
return QSL("wa_fullscreen_support");
case QWebEngineSettings::AllowRunningInsecureContent:
return QSL("wa_run_insecure_content");
case QWebEngineSettings::AllowGeolocationOnInsecureOrigins:
return QSL("wa_insecure_geolocation");
case QWebEngineSettings::PlaybackRequiresUserGesture:
return QSL("wa_playback_needs_gesture");
case QWebEngineSettings::WebRTCPublicInterfacesOnly:
return QSL("wa_webrtc_public_interface_only");
default:
qWarning() << "Unknown attribute: " << attribute;
return QSL("wa_js_enabled");
}
}

View File

@ -101,6 +101,7 @@ public:
*/
SiteWebEngineSettings getWebEngineSettings(const QUrl &url);
QHash<QWebEngineSettings::WebAttribute, bool> getWebAttributes2(const QUrl &url);
QHash<QWebEngineSettings::WebAttribute, bool> getWebAttributes(const QUrl &url);
void setJavascript(const QUrl &url, const int value);
@ -126,6 +127,7 @@ public:
QString getPermissionName(const Permission permission);
private:
QString webAttributeToSqlColumn(const QWebEngineSettings::WebAttribute &attribute);
QString optionToSqlColumn(const PageOptions &option);
PageOptions optionFromWebEngineFeature(const QWebEnginePage::Feature &feature) const;
Permission testAttribute(const QWebEngineSettings::WebAttribute attribute) const;
@ -133,6 +135,8 @@ private:
QWebEngineSettings::WebAttribute optionToAttribute(const PageOptions &option) const;
QMap<PageOptions, Permission> m_defaults;
QList<QWebEngineSettings::WebAttribute> supportedAttribute;
QString attributesSql;
};

View File

@ -452,9 +452,22 @@ bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::Navigatio
if (result) {
if (isMainFrame) {
const bool isWeb = url.scheme() == QL1S("http") || url.scheme() == QL1S("https") || url.scheme() == QL1S("file");
const SiteWebEngineSettings siteSettings = mApp->siteSettingsManager()->getWebEngineSettings(url);
settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, isWeb ? siteSettings.allowJavaScript : true);
settings()->setAttribute(QWebEngineSettings::AutoLoadImages, isWeb ? siteSettings.allowImages : true);
if (isWeb) {
auto webAttributes = mApp->siteSettingsManager()->getWebAttributes2(url);
if (!webAttributes.empty()) {
QHash<QWebEngineSettings::WebAttribute, bool>::iterator it;
for (it = webAttributes.begin(); it != webAttributes.end(); ++it) {
settings()->setAttribute(it.key(), it.value());
}
}
}
// const SiteWebEngineSettings siteSettings = mApp->siteSettingsManager()->getWebEngineSettings(url);
// settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, isWeb ? siteSettings.allowJavaScript : true);
// settings()->setAttribute(QWebEngineSettings::AutoLoadImages, isWeb ? siteSettings.allowImages : true);
}
Q_EMIT navigationRequestAccepted(url, type, isMainFrame);
}