From aa0efc88965440baa14a977572b185371b113a7f Mon Sep 17 00:00:00 2001 From: Johannes Emerich Date: Tue, 19 Dec 2023 16:47:20 +0100 Subject: [PATCH 1/3] Add key combo for hard refresh in kiosk Add a conventional key combination to perform a hard refresh, useful for recovering if corrupted assets end up being cached. By using the Ctrl-Shift-R combination common in browsers for Windows and Linux, this may be discoverable by a savvy admin without the need to be told explicitly. --- controller/Changelog.md | 1 + docs/user-manual/Readme.org | 4 ++++ kiosk/kiosk_browser/browser_widget.py | 15 +++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/controller/Changelog.md b/controller/Changelog.md index 4120774c..c1b9c491 100644 --- a/controller/Changelog.md +++ b/controller/Changelog.md @@ -2,6 +2,7 @@ ## Added +- kiosk: Add a key combination to perform hard refresh (Ctrl-Shift-R) - os: Added localization options for Polish and Czech # [2023.9.0] - 2023-09-12 diff --git a/docs/user-manual/Readme.org b/docs/user-manual/Readme.org index beeea1b1..ef93a0bd 100644 --- a/docs/user-manual/Readme.org +++ b/docs/user-manual/Readme.org @@ -131,6 +131,10 @@ View what has been added, changed and fixed with each version. A status screen providing a brief status report about various components of the system is available on ~tty8~. It can be accessed using the key combination ~Ctrl-Alt-F8~. To get back from the status screen to the graphical interface, use ~Ctrl-Alt-F7~. +** Clearing kiosk browser cache + +At times network issues may cause corrupted media resources to end up in the kiosk browser's cache. In this case it may be helpful to clear this cache to force all resources being downloaded again. A hard refresh can be triggered using ~Ctrl-Shift-R~, which clears the cache and then reloads the kiosk application. The hard refresh does not affect user sessions or preferences, so it can be performed without the user having to log in again. + ** Wiping user data Certain user data such as Play login credentials, wireless settings are stored persistently on the computer's disk. diff --git a/kiosk/kiosk_browser/browser_widget.py b/kiosk/kiosk_browser/browser_widget.py index f62a4a19..06be7701 100644 --- a/kiosk/kiosk_browser/browser_widget.py +++ b/kiosk/kiosk_browser/browser_widget.py @@ -61,6 +61,9 @@ def __init__(self, url, get_current_proxy, parent): # Shortcut to manually reload self.reload_shortcut = QtWidgets.QShortcut('CTRL+R', self) self.reload_shortcut.activated.connect(self.reload) + # Shortcut to perform a hard refresh + self.hard_refresh_shortcut = QtWidgets.QShortcut('CTRL+SHIFT+R', self) + self.hard_refresh_shortcut.activated.connect(self.hard_refresh) # Prepare reload timer self._reload_timer = QtCore.QTimer(self) @@ -77,6 +80,18 @@ def reload(self): if self._reload_timer.isActive(): self._reload_timer.stop() + def hard_refresh(self): + """ Clear cache, then reload. + + Does not affect cookies or localstorage contents. + + NOTE This clears the entire HTTP cache, assumed to be OK as the kiosk targets a specific page. + """ + logging.info(f"Clearing HTTP cache (hard refresh)") + QtWebEngineWidgets.QWebEngineProfile.defaultProfile().clearHttpCache() + + self.reload() + def load(self, url: str): """ Load specific URL. """ From e68cb1c7ee4ab5b0afcc6738a44a22a2d38c9c1e Mon Sep 17 00:00:00 2001 From: Johannes Emerich Date: Wed, 3 Jan 2024 13:19:04 +0100 Subject: [PATCH 2/3] Use private member naming convention --- kiosk/kiosk_browser/browser_widget.py | 32 +++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/kiosk/kiosk_browser/browser_widget.py b/kiosk/kiosk_browser/browser_widget.py index 06be7701..42f759d5 100644 --- a/kiosk/kiosk_browser/browser_widget.py +++ b/kiosk/kiosk_browser/browser_widget.py @@ -59,11 +59,11 @@ def __init__(self, url, get_current_proxy, parent): self._webview.loadFinished.connect(self._load_finished) # Shortcut to manually reload - self.reload_shortcut = QtWidgets.QShortcut('CTRL+R', self) - self.reload_shortcut.activated.connect(self.reload) + self._reload_shortcut = QtWidgets.QShortcut('CTRL+R', self) + self._reload_shortcut.activated.connect(self.reload) # Shortcut to perform a hard refresh - self.hard_refresh_shortcut = QtWidgets.QShortcut('CTRL+SHIFT+R', self) - self.hard_refresh_shortcut.activated.connect(self.hard_refresh) + self._hard_refresh_shortcut = QtWidgets.QShortcut('CTRL+SHIFT+R', self) + self._hard_refresh_shortcut.activated.connect(self._hard_refresh) # Prepare reload timer self._reload_timer = QtCore.QTimer(self) @@ -80,18 +80,6 @@ def reload(self): if self._reload_timer.isActive(): self._reload_timer.stop() - def hard_refresh(self): - """ Clear cache, then reload. - - Does not affect cookies or localstorage contents. - - NOTE This clears the entire HTTP cache, assumed to be OK as the kiosk targets a specific page. - """ - logging.info(f"Clearing HTTP cache (hard refresh)") - QtWebEngineWidgets.QWebEngineProfile.defaultProfile().clearHttpCache() - - self.reload() - def load(self, url: str): """ Load specific URL. """ @@ -108,6 +96,18 @@ def _load_finished(self, success): self._view(Status.NETWORK_ERROR) self._reload_timer.start(reload_on_network_error_after) + def _hard_refresh(self): + """ Clear cache, then reload. + + Does not affect cookies or localstorage contents. + + NOTE This clears the entire HTTP cache, assumed to be OK as the kiosk targets a specific page. + """ + logging.info(f"Clearing HTTP cache (hard refresh)") + self._webview.page().profile().clearHttpCache() + + self.reload() + def _proxy_auth(self, get_current_proxy, url, auth, proxyHost): proxy = get_current_proxy() if proxy is not None and proxy.credentials is not None: From 6cc13db2059e93194a838a5a59689459944f1dd3 Mon Sep 17 00:00:00 2001 From: Johannes Emerich Date: Wed, 3 Jan 2024 13:29:03 +0100 Subject: [PATCH 3/3] Add workaround for Qt race condition This works around the race condition described in https://bugreports.qt.io/browse/QTBUG-111541 by waiting an instant after triggering the clearing of profile cache. If a load is triggered while the clearing is not finished, it can cause the load to hang. This can be recovered from by triggering a simple reload, but is better to avoid. A signal for observing the progress of cache clearing should be available for future versions of Qt 6: https://github.com/qt/qtwebengine/commit/37430020d373304f812fa822651cf2a0ccf636d3 I considered adding a more involved mechanism to detect stalled loads and automatically trigger a new load, but concluded that a sleep should be overall simpler and easy enough to understand. As a relatively short sleep is chosen to avoid user confusion, this may still fail, but a hanging load is relatively harmless and should be recoverable by the user. --- kiosk/kiosk_browser/browser_widget.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kiosk/kiosk_browser/browser_widget.py b/kiosk/kiosk_browser/browser_widget.py index 42f759d5..d494c5d8 100644 --- a/kiosk/kiosk_browser/browser_widget.py +++ b/kiosk/kiosk_browser/browser_widget.py @@ -2,6 +2,7 @@ from enum import Enum, auto import logging import re +import time from kiosk_browser import system @@ -106,6 +107,11 @@ def _hard_refresh(self): logging.info(f"Clearing HTTP cache (hard refresh)") self._webview.page().profile().clearHttpCache() + # Sleep before triggering reload to avoid a possible race condition. + # Future Qt versions may provide a signal on `QWebEngineProfile` to + # allow to queue the reload on clear-cache completion instead. + # https://bugreports.qt.io/browse/QTBUG-111541 + time.sleep(0.25) self.reload() def _proxy_auth(self, get_current_proxy, url, auth, proxyHost):