From 4518afbde2e4e44ae948ec762050bcaa74c22c6f Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 6 Feb 2017 12:46:28 +0100 Subject: [PATCH] Add support for storage -> cache-size with QtWebEngine --- CHANGELOG.asciidoc | 1 + .../browser/webengine/webenginesettings.py | 31 +++++++++++++++++-- qutebrowser/browser/webkit/cache.py | 11 +++++-- qutebrowser/config/config.py | 1 + qutebrowser/config/configdata.py | 6 ++-- 5 files changed, 43 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index aead63169..644af7ad5 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -26,6 +26,7 @@ Added - Printing support for QtWebEngine with Qt >= 5.8 - Proxy support for QtWebEngine with Qt >= 5.8 - Support for the `content -> cookies-store` option with QtWebEngine +- Support for the `storage -> cache-size` option with QtWebEngine Changed ~~~~~~~ diff --git a/qutebrowser/browser/webengine/webenginesettings.py b/qutebrowser/browser/webengine/webenginesettings.py index d428fb371..9db3e9d05 100644 --- a/qutebrowser/browser/webengine/webenginesettings.py +++ b/qutebrowser/browser/webengine/webenginesettings.py @@ -65,16 +65,40 @@ class StaticSetter(websettings.StaticSetter): GLOBAL_SETTINGS = QWebEngineSettings.globalSettings -class PersistentCookiePolicy(websettings.Base): +class ProfileSetter(websettings.Base): + + """A setting set on the QWebEngineProfile.""" + + def __init__(self, getter, setter): + super().__init__() + profile = QWebEngineProfile.defaultProfile() + self._getter = getattr(profile, getter) + self._setter = getattr(profile, setter) + + def get(self, settings=None): + utils.unused(settings) + return self._getter() + + def _set(self, value, settings=None): + utils.unused(settings) + self._setter(value) + + +class PersistentCookiePolicy(ProfileSetter): """The cookies -> store setting is different from other settings.""" + def __init__(self): + super().__init__(getter='persistentCookiesPolicy', + setter='setPersistentCookiesPolicy') + def get(self, settings=None): + utils.unused(settings) return config.get('content', 'cookies-store') def _set(self, value, settings=None): utils.unused(settings) - QWebEngineProfile.defaultProfile().setPersistentCookiesPolicy( + self._setter( QWebEngineProfile.AllowPersistentCookies if value else QWebEngineProfile.NoPersistentCookies ) @@ -246,6 +270,9 @@ MAPPINGS = { 'storage': { 'local-storage': Attribute(QWebEngineSettings.LocalStorageEnabled), + 'cache-size': + ProfileSetter(getter='httpCacheMaximumSize', + setter='setHttpCacheMaximumSize') }, 'general': { 'xss-auditing': diff --git a/qutebrowser/browser/webkit/cache.py b/qutebrowser/browser/webkit/cache.py index bc774c250..860a532b0 100644 --- a/qutebrowser/browser/webkit/cache.py +++ b/qutebrowser/browser/webkit/cache.py @@ -48,6 +48,13 @@ class DiskCache(QNetworkDiskCache): maxsize=self.maximumCacheSize(), path=self.cacheDirectory()) + def _set_cache_size(self): + """Set the cache size based on the config.""" + size = config.get('storage', 'cache-size') + if size is None: + size = 1024 * 1024 * 50 # default from QNetworkDiskCachePrivate + self.setMaximumCacheSize(size) + def _maybe_activate(self): """Activate/deactivate the cache based on the config.""" if config.get('general', 'private-browsing'): @@ -55,13 +62,13 @@ class DiskCache(QNetworkDiskCache): else: self._activated = True self.setCacheDirectory(os.path.join(self._cache_dir, 'http')) - self.setMaximumCacheSize(config.get('storage', 'cache-size')) + self._set_cache_size() @pyqtSlot(str, str) def on_config_changed(self, section, option): """Update cache size/activated if the config was changed.""" if (section, option) == ('storage', 'cache-size'): - self.setMaximumCacheSize(config.get('storage', 'cache-size')) + self._set_cache_size() elif (section, option) == ('general', # pragma: no branch 'private-browsing'): self._maybe_activate() diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index a4be0dc53..05d71605f 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -443,6 +443,7 @@ class ConfigManager(QObject): 'html > ::-webkit-scrollbar { width: 0px; height: 0px; }': '', '::-webkit-scrollbar { width: 0px; height: 0px; }': '', }), + ('contents', 'cache-size'): _get_value_transformer({'52428800': ''}), } changed = pyqtSignal(str, str) diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index 6a5f9d6d8..b99e97e0c 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -795,9 +795,9 @@ def data(readonly=False): "enabled."), ('cache-size', - SettingValue(typ.Int(minval=0, maxval=MAXVALS['int64']), - '52428800'), - "Size of the HTTP network cache."), + SettingValue(typ.Int(none_ok=True, minval=0, + maxval=MAXVALS['int64']), ''), + "Size of the HTTP network cache. Empty to use the default value."), readonly=readonly )),