Allow to restrict qute:* pages to a backend
This commit is contained in:
parent
71bc5bb943
commit
a1527f35d4
@ -52,10 +52,6 @@ class QuteSchemeOSError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def add_handler(name):
|
||||
"""Add a handler to the qute: scheme."""
|
||||
def namedecorator(function):
|
||||
_HANDLERS[name] = function
|
||||
class QuteSchemeError(Exception):
|
||||
|
||||
"""Exception to signal that a handler should return an ErrorReply.
|
||||
@ -74,8 +70,40 @@ class QuteSchemeError(Exception):
|
||||
super().__init__(errorstring)
|
||||
|
||||
|
||||
class add_handler: # pylint: disable=invalid-name
|
||||
|
||||
"""Decorator to register a qute:* URL handler.
|
||||
|
||||
Attributes:
|
||||
_name: The 'foo' part of qute:foo
|
||||
backend: Limit which backends the handler can run with.
|
||||
"""
|
||||
|
||||
def __init__(self, name, backend=None):
|
||||
self._name = name
|
||||
self._backend = backend
|
||||
self._function = None
|
||||
|
||||
def __call__(self, function):
|
||||
self._function = function
|
||||
_HANDLERS[self._name] = self.wrapper
|
||||
return function
|
||||
return namedecorator
|
||||
|
||||
def wrapper(self, *args, **kwargs):
|
||||
used_backend = usertypes.arg2backend[objreg.get('args').backend]
|
||||
if self._backend is not None and used_backend != self._backend:
|
||||
return self.wrong_backend_handler(*args, **kwargs)
|
||||
else:
|
||||
return self._function(*args, **kwargs)
|
||||
|
||||
def wrong_backend_handler(self, url):
|
||||
html = jinja.render('error.html',
|
||||
title="Error while opening qute:url",
|
||||
url=url.toDisplayString(),
|
||||
error='{} is not available with this '
|
||||
'backend'.format(url.toDisplayString()),
|
||||
icon='')
|
||||
return html.encode('UTF-8', errors='xmlcharrefreplace')
|
||||
|
||||
|
||||
def data_for_url(url):
|
||||
|
@ -56,7 +56,7 @@ class QuteSchemeHandler(QWebEngineUrlSchemeHandler):
|
||||
# FIXME:qtwebengine how do we show a better error here?
|
||||
log.misc.exception("OSError while handling qute:* URL")
|
||||
job.fail(QWebEngineUrlRequestJob.UrlNotFound)
|
||||
except QuteSchemeError as e:
|
||||
except qutescheme.QuteSchemeError as e:
|
||||
# FIXME:qtwebengine how do we show a better error here?
|
||||
log.misc.exception("Error while handling qute:* URL")
|
||||
job.fail(QWebEngineUrlRequestJob.RequestFailed)
|
||||
|
@ -27,28 +27,10 @@ from PyQt5.QtNetwork import QNetworkReply
|
||||
|
||||
from qutebrowser.browser import pdfjs, qutescheme
|
||||
from qutebrowser.browser.webkit.network import schemehandler, networkreply
|
||||
from qutebrowser.utils import jinja, log, message, objreg
|
||||
from qutebrowser.utils import jinja, log, message, objreg, usertypes
|
||||
from qutebrowser.config import configexc, configdata
|
||||
|
||||
|
||||
class QuteSchemeError(Exception):
|
||||
|
||||
"""Exception to signal that a handler should return an ErrorReply.
|
||||
|
||||
Attributes correspond to the arguments in
|
||||
networkreply.ErrorNetworkReply.
|
||||
|
||||
Attributes:
|
||||
errorstring: Error string to print.
|
||||
error: Numerical error value.
|
||||
"""
|
||||
|
||||
def __init__(self, errorstring, error):
|
||||
self.errorstring = errorstring
|
||||
self.error = error
|
||||
super().__init__(errorstring)
|
||||
|
||||
|
||||
class QuteSchemeHandler(schemehandler.SchemeHandler):
|
||||
|
||||
"""Scheme handler for qute: URLs."""
|
||||
@ -103,7 +85,7 @@ class JSBridge(QObject):
|
||||
message.error('current', e)
|
||||
|
||||
|
||||
@qutescheme.add_handler('settings')
|
||||
@qutescheme.add_handler('settings', backend=usertypes.Backend.QtWebKit)
|
||||
def qute_settings(_url):
|
||||
"""Handler for qute:settings. View/change qute configuration."""
|
||||
config_getter = functools.partial(objreg.get('config').get, raw=True)
|
||||
@ -112,7 +94,7 @@ def qute_settings(_url):
|
||||
return html.encode('UTF-8', errors='xmlcharrefreplace')
|
||||
|
||||
|
||||
@qutescheme.add_handler('pdfjs')
|
||||
@qutescheme.add_handler('pdfjs', backend=usertypes.Backend.QtWebKit)
|
||||
def qute_pdfjs(url):
|
||||
"""Handler for qute://pdfjs. Return the pdf.js viewer."""
|
||||
try:
|
||||
|
Loading…
Reference in New Issue
Block a user