Permalink
Browse files

Refactor websettings default handling

With per-domain settings, having a getter for a setting gets really complicated,
as there isn't one true value for a setting.

The only reason we needed those getters is to save away the default values for
some settings where we were unsure what the defaults are.

- For font setters, we can get the defaults from QFont, like QtWeb{Kit,Engine}
  do.
- For font sizes, we hardcode the defaults QtWeb{Kit,Engine} hardcodes too.
- For maximum-page-in-cache, we hardcode 0, just like QtWebKit.
- For default-encoding, we hardcode iso-8559-1, like QtWeb{Kit,Engine}
- For offline-storage-default-quota, we hardcode 5MB, like QtWebKit
- For offline-web-application-cache-quota, we hardcode MAXINT as default value,
  but we still keep the empty value in the config. It means "no quota"
  internally in QtWebKit, but it's a too confusing value to have in the config.
- For object-cache-capacities it's a bit more complicated (the defaults are
  calculated based on disk space), but let's just get rid of the setting
  altogether in the next commit (see #1751).

Closes #2639.
  • Loading branch information...
The-Compiler committed Jun 6, 2017
1 parent 2f5756e commit 1785b72393f6f4b0d4f8bd02ab8a45931efbe7ff
@@ -30,6 +30,7 @@
import os
import logging
from PyQt5.QtGui import QFont
from PyQt5.QtWebEngineWidgets import (QWebEngineSettings, QWebEngineProfile,
QWebEngineScript)
@@ -62,25 +63,41 @@ class Attribute(Base, websettings.Attribute):
class Setter(Base, websettings.Setter):
"""A setting set via QWebEngineSettings getter/setter methods."""
"""A setting set via a QWebEngineSettings setter method."""
pass
class FontFamilySetter(Base, websettings.FontFamilySetter):
"""A setter for a font family.
Gets the default value from QFont.
"""
def __init__(self, font):
# Mapping from WebEngineSettings::initDefaults in
# qtwebengine/src/core/web_engine_settings.cpp
font_to_qfont = {
QWebEngineSettings.StandardFont: QFont.Serif,
QWebEngineSettings.FixedFont: QFont.Monospace,
QWebEngineSettings.SerifFont: QFont.Serif,
QWebEngineSettings.SansSerifFont: QFont.SansSerif,
QWebEngineSettings.CursiveFont: QFont.Cursive,
QWebEngineSettings.FantasyFont: QFont.Fantasy,
}
super().__init__(setter=QWebEngineSettings.setFontFamily, font=font,
qfont=font_to_qfont[font])
class DefaultProfileSetter(websettings.Base):
"""A setting set on the QWebEngineProfile."""
def __init__(self, getter, setter):
super().__init__()
self._getter = getter
def __init__(self, setter, default=websettings.UNSET):
super().__init__(default)
self._setter = setter
def get(self, settings=None):
utils.unused(settings)
getter = getattr(default_profile, self._getter)
return getter()
def _set(self, value, settings=None):
utils.unused(settings)
setter = getattr(default_profile, self._setter)
@@ -92,8 +109,7 @@ class PersistentCookiePolicy(DefaultProfileSetter):
"""The cookies -> store setting is different from other settings."""
def __init__(self):
super().__init__(getter='persistentCookiesPolicy',
setter='setPersistentCookiesPolicy')
super().__init__('setPersistentCookiesPolicy')
def get(self, settings=None):
utils.unused(settings)
@@ -256,44 +272,28 @@ def shutdown():
},
'fonts': {
'web-family-standard':
Setter(getter=QWebEngineSettings.fontFamily,
setter=QWebEngineSettings.setFontFamily,
args=[QWebEngineSettings.StandardFont]),
FontFamilySetter(QWebEngineSettings.StandardFont),
'web-family-fixed':
Setter(getter=QWebEngineSettings.fontFamily,
setter=QWebEngineSettings.setFontFamily,
args=[QWebEngineSettings.FixedFont]),
FontFamilySetter(QWebEngineSettings.FixedFont),
'web-family-serif':
Setter(getter=QWebEngineSettings.fontFamily,
setter=QWebEngineSettings.setFontFamily,
args=[QWebEngineSettings.SerifFont]),
FontFamilySetter(QWebEngineSettings.SerifFont),
'web-family-sans-serif':
Setter(getter=QWebEngineSettings.fontFamily,
setter=QWebEngineSettings.setFontFamily,
args=[QWebEngineSettings.SansSerifFont]),
FontFamilySetter(QWebEngineSettings.SansSerifFont),
'web-family-cursive':
Setter(getter=QWebEngineSettings.fontFamily,
setter=QWebEngineSettings.setFontFamily,
args=[QWebEngineSettings.CursiveFont]),
FontFamilySetter(QWebEngineSettings.CursiveFont),
'web-family-fantasy':
Setter(getter=QWebEngineSettings.fontFamily,
setter=QWebEngineSettings.setFontFamily,
args=[QWebEngineSettings.FantasyFont]),
FontFamilySetter(QWebEngineSettings.FantasyFont),
'web-size-minimum':
Setter(getter=QWebEngineSettings.fontSize,
setter=QWebEngineSettings.setFontSize,
Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.MinimumFontSize]),
'web-size-minimum-logical':
Setter(getter=QWebEngineSettings.fontSize,
setter=QWebEngineSettings.setFontSize,
Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.MinimumLogicalFontSize]),
'web-size-default':
Setter(getter=QWebEngineSettings.fontSize,
setter=QWebEngineSettings.setFontSize,
Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.DefaultFontSize]),
'web-size-default-fixed':
Setter(getter=QWebEngineSettings.fontSize,
setter=QWebEngineSettings.setFontSize,
Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.DefaultFixedFontSize]),
},
'ui': {
@@ -304,15 +304,14 @@ def shutdown():
'local-storage':
Attribute(QWebEngineSettings.LocalStorageEnabled),
'cache-size':
DefaultProfileSetter(getter='httpCacheMaximumSize',
setter='setHttpCacheMaximumSize')
# 0: automatically managed by QtWebEngine
DefaultProfileSetter('setHttpCacheMaximumSize', default=0),
},
'general': {
'xss-auditing':
Attribute(QWebEngineSettings.XSSAuditingEnabled),
'default-encoding':
Setter(getter=QWebEngineSettings.defaultTextEncoding,
setter=QWebEngineSettings.setDefaultTextEncoding),
Setter(QWebEngineSettings.setDefaultTextEncoding),
}
}
@@ -29,6 +29,7 @@
import os.path
from PyQt5.QtGui import QFont
from PyQt5.QtWebKit import QWebSettings
from qutebrowser.config import config, websettings
@@ -53,25 +54,48 @@ class Attribute(Base, websettings.Attribute):
class Setter(Base, websettings.Setter):
"""A setting set via QWebSettings getter/setter methods."""
"""A setting set via a QWebSettings setter method."""
pass
class NullStringSetter(Base, websettings.NullStringSetter):
class NullStringSetter(Base, websettings.Setter):
"""A setter for settings requiring a null QString as default."""
pass
def set_default(self, settings=None):
self._set(None, settings=settings)
class StaticSetter(Base, websettings.StaticSetter):
"""A setting set via static QWebSettings getter/setter methods."""
"""A setting set via a static QWebSettings setter method."""
pass
class FontFamilySetter(Base, websettings.FontFamilySetter):
"""A setter for a font family.
Gets the default value from QFont.
"""
def __init__(self, font):
# Mapping from QWebSettings::QWebSettings() in
# qtwebkit/Source/WebKit/qt/Api/qwebsettings.cpp
font_to_qfont = {
QWebSettings.StandardFont: QFont.Serif,
QWebSettings.FixedFont: QFont.Monospace,
QWebSettings.SerifFont: QFont.Serif,
QWebSettings.SansSerifFont: QFont.SansSerif,
QWebSettings.CursiveFont: QFont.Cursive,
QWebSettings.FantasyFont: QFont.Fantasy,
}
super().__init__(setter=QWebSettings.setFontFamily, font=font,
qfont=font_to_qfont[font])
class CookiePolicy(websettings.Base):
"""The ThirdPartyCookiePolicy setting is different from other settings."""
@@ -83,9 +107,6 @@ class CookiePolicy(websettings.Base):
'no-unknown-3rdparty': QWebSettings.AllowThirdPartyWithExistingCookies,
}
def get(self, settings=None):
return config.get('content', 'cookies-accept')
def _set(self, value, settings=None):
QWebSettings.globalSettings().setThirdPartyCookiePolicy(
self.MAPPING[value])
@@ -179,44 +200,28 @@ def shutdown():
},
'fonts': {
'web-family-standard':
Setter(getter=QWebSettings.fontFamily,
setter=QWebSettings.setFontFamily,
args=[QWebSettings.StandardFont]),
FontFamilySetter(QWebSettings.StandardFont),
'web-family-fixed':
Setter(getter=QWebSettings.fontFamily,
setter=QWebSettings.setFontFamily,
args=[QWebSettings.FixedFont]),
FontFamilySetter(QWebSettings.FixedFont),
'web-family-serif':
Setter(getter=QWebSettings.fontFamily,
setter=QWebSettings.setFontFamily,
args=[QWebSettings.SerifFont]),
FontFamilySetter(QWebSettings.SerifFont),
'web-family-sans-serif':
Setter(getter=QWebSettings.fontFamily,
setter=QWebSettings.setFontFamily,
args=[QWebSettings.SansSerifFont]),
FontFamilySetter(QWebSettings.SansSerifFont),
'web-family-cursive':
Setter(getter=QWebSettings.fontFamily,
setter=QWebSettings.setFontFamily,
args=[QWebSettings.CursiveFont]),
FontFamilySetter(QWebSettings.CursiveFont),
'web-family-fantasy':
Setter(getter=QWebSettings.fontFamily,
setter=QWebSettings.setFontFamily,
args=[QWebSettings.FantasyFont]),
FontFamilySetter(QWebSettings.FantasyFont),
'web-size-minimum':
Setter(getter=QWebSettings.fontSize,
setter=QWebSettings.setFontSize,
Setter(QWebSettings.setFontSize,
args=[QWebSettings.MinimumFontSize]),
'web-size-minimum-logical':
Setter(getter=QWebSettings.fontSize,
setter=QWebSettings.setFontSize,
Setter(QWebSettings.setFontSize,
args=[QWebSettings.MinimumLogicalFontSize]),
'web-size-default':
Setter(getter=QWebSettings.fontSize,
setter=QWebSettings.setFontSize,
Setter(QWebSettings.setFontSize,
args=[QWebSettings.DefaultFontSize]),
'web-size-default-fixed':
Setter(getter=QWebSettings.fontSize,
setter=QWebSettings.setFontSize,
Setter(QWebSettings.setFontSize,
args=[QWebSettings.DefaultFixedFontSize]),
},
'ui': {
@@ -226,8 +231,7 @@ def shutdown():
Attribute(QWebSettings.FrameFlatteningEnabled),
# user-stylesheet is handled separately
'css-media-type':
NullStringSetter(getter=QWebSettings.cssMediaType,
setter=QWebSettings.setCSSMediaType),
NullStringSetter(QWebSettings.setCSSMediaType),
'smooth-scrolling':
Attribute(QWebSettings.ScrollAnimatorEnabled),
#'accelerated-compositing':
@@ -243,19 +247,17 @@ def shutdown():
'local-storage':
Attribute(QWebSettings.LocalStorageEnabled),
'maximum-pages-in-cache':
StaticSetter(getter=QWebSettings.maximumPagesInCache,
setter=QWebSettings.setMaximumPagesInCache),
StaticSetter(QWebSettings.setMaximumPagesInCache),
'object-cache-capacities':
StaticSetter(getter=None,
setter=QWebSettings.setObjectCacheCapacities,
StaticSetter(QWebSettings.setObjectCacheCapacities,
unpack=True),
'offline-storage-default-quota':
StaticSetter(getter=QWebSettings.offlineStorageDefaultQuota,
setter=QWebSettings.setOfflineStorageDefaultQuota),
StaticSetter(QWebSettings.setOfflineStorageDefaultQuota),
# Default from ApplicationCacheStorage::ApplicationCacheStorage in
# qtwebkit/Source/WebCore/loader/appcache/ApplicationCacheStorage.cpp
'offline-web-application-cache-quota':
StaticSetter(
getter=QWebSettings.offlineWebApplicationCacheQuota,
setter=QWebSettings.setOfflineWebApplicationCacheQuota),
StaticSetter(QWebSettings.setOfflineWebApplicationCacheQuota,
default=qtutils.MAXVALS['int64']), # no quota
},
'general': {
'developer-extras':
@@ -267,7 +269,6 @@ def shutdown():
'site-specific-quirks':
Attribute(QWebSettings.SiteSpecificQuirksEnabled),
'default-encoding':
Setter(getter=QWebSettings.defaultTextEncoding,
setter=QWebSettings.setDefaultTextEncoding),
Setter(QWebSettings.setDefaultTextEncoding),
}
}
@@ -444,7 +444,22 @@ class ConfigManager(QObject):
'html > ::-webkit-scrollbar { width: 0px; height: 0px; }': '',
'::-webkit-scrollbar { width: 0px; height: 0px; }': '',
}),
('contents', 'cache-size'): _get_value_transformer({'52428800': ''}),
('general', 'default-encoding'):
_get_value_transformer({'': 'iso-8859-1'}),
('contents', 'cache-size'):
_get_value_transformer({'52428800': ''}),
('storage', 'maximum-pages-in-cache'):
_get_value_transformer({'': '0'}),
('storage', 'offline-storage-default-quota'):
_get_value_transformer({'': str(5 * 1024 * 1024)}),
('fonts', 'web-size-minimum'):
_get_value_transformer({'': '0'}),
('fonts', 'web-size-minimum-logical'):
_get_value_transformer({'': '6'}),
('fonts', 'web-size-default'):
_get_value_transformer({'': '16'}),
('fonts', 'web-size-default-fixed'):
_get_value_transformer({'': '13'}),
}
changed = pyqtSignal(str, str)
Oops, something went wrong.

0 comments on commit 1785b72

Please sign in to comment.