From 5bf0dffa9558b7f0da5ba763bb87c5f380ff0261 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 30 Nov 2018 08:23:52 +0100 Subject: [PATCH] Move history (de)serializing to a private_api object --- qutebrowser/browser/browsertab.py | 35 ++++++++---- qutebrowser/browser/commands.py | 4 +- qutebrowser/browser/webengine/webenginetab.py | 57 +++++++++++-------- qutebrowser/browser/webkit/webkittab.py | 57 +++++++++++-------- qutebrowser/mainwindow/tabbedbrowser.py | 4 +- qutebrowser/misc/sessions.py | 2 +- 6 files changed, 92 insertions(+), 67 deletions(-) diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index 175c1e60d..311843964 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -592,6 +592,27 @@ class AbstractScroller(QObject): raise NotImplementedError +class AbstractHistoryPrivate: + + """Private API related to the history.""" + + def __init__(self, tab: 'AbstractTab'): + self._tab = tab + self._history = None + + def serialize(self) -> bytes: + """Serialize into an opaque format understood by self.deserialize.""" + raise NotImplementedError + + def deserialize(self, data: bytes) -> None: + """Deserialize from a format produced by self.serialize.""" + raise NotImplementedError + + def load_items(self, items: typing.Sequence) -> None: + """Deserialize from a list of WebHistoryItems.""" + raise NotImplementedError + + class AbstractHistory: """The history attribute of a AbstractTab.""" @@ -599,6 +620,7 @@ class AbstractHistory: def __init__(self, tab: 'AbstractTab') -> None: self._tab = tab self._history = None + self.private_api = AbstractHistoryPrivate(tab) def __len__(self) -> int: raise NotImplementedError @@ -646,18 +668,6 @@ class AbstractHistory: def _go_to_item(self, item: typing.Any) -> None: raise NotImplementedError - def serialize(self) -> bytes: - """Serialize into an opaque format understood by self.deserialize.""" - raise NotImplementedError - - def deserialize(self, data: bytes) -> None: - """Deserialize from a format produced by self.serialize.""" - raise NotImplementedError - - def load_items(self, items: typing.Sequence) -> None: - """Deserialize from a list of WebHistoryItems.""" - raise NotImplementedError - class AbstractElements: @@ -885,6 +895,7 @@ class AbstractTab(QWidget): self._widget = widget self._layout.wrap(self, widget) self.history._history = widget.history() + self.history.private_api._history = widget.history() self.scroller._init_widget(widget) self.caret._widget = widget self.zoom._widget = widget diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 56a951a9f..01542cdea 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -463,7 +463,7 @@ class CommandDispatcher: cur_title = self._tabbed_browser.widget.page_title( self._current_index()) try: - history = curtab.history.serialize() + history = curtab.history.private_api.serialize() except browsertab.WebTabError as e: raise cmdutils.CommandError(e) @@ -486,7 +486,7 @@ class CommandDispatcher: new_tabbed_browser.widget.window().setWindowIcon(curtab.icon()) newtab.data.keep_icon = True - newtab.history.deserialize(history) + newtab.history.private_api.deserialize(history) newtab.zoom.set_factor(curtab.zoom.factor()) new_tabbed_browser.widget.set_tab_pinned(newtab, curtab.data.pinned) return newtab diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index f167b6b8c..fbd168fee 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -518,31 +518,7 @@ class WebEngineScroller(browsertab.AbstractScroller): return self._at_bottom -class WebEngineHistory(browsertab.AbstractHistory): - - """QtWebEngine implementations related to page history.""" - - def __len__(self): - return len(self._history) - - def __iter__(self): - return iter(self._history.items()) - - def current_idx(self): - return self._history.currentItemIndex() - - def can_go_back(self): - return self._history.canGoBack() - - def can_go_forward(self): - return self._history.canGoForward() - - def _item_at(self, i): - return self._history.itemAt(i) - - def _go_to_item(self, item): - self._tab.predicted_navigation.emit(item.url()) - self._history.goToItem(item) +class WebEngineHistoryPrivate(browsertab.AbstractHistoryPrivate): def serialize(self): if not qtutils.version_check('5.9', compiled=False): @@ -579,6 +555,37 @@ class WebEngineHistory(browsertab.AbstractHistory): self._tab.load_finished.connect(_on_load_finished) +class WebEngineHistory(browsertab.AbstractHistory): + + """QtWebEngine implementations related to page history.""" + + def __init__(self, tab): + super().__init__(tab) + self.private_api = WebEngineHistoryPrivate(tab) + + def __len__(self): + return len(self._history) + + def __iter__(self): + return iter(self._history.items()) + + def current_idx(self): + return self._history.currentItemIndex() + + def can_go_back(self): + return self._history.canGoBack() + + def can_go_forward(self): + return self._history.canGoForward() + + def _item_at(self, i): + return self._history.itemAt(i) + + def _go_to_item(self, item): + self._tab.predicted_navigation.emit(item.url()) + self._history.goToItem(item) + + class WebEngineZoom(browsertab.AbstractZoom): """QtWebEngine implementations related to zooming.""" diff --git a/qutebrowser/browser/webkit/webkittab.py b/qutebrowser/browser/webkit/webkittab.py index 5a989e81e..df8abe254 100644 --- a/qutebrowser/browser/webkit/webkittab.py +++ b/qutebrowser/browser/webkit/webkittab.py @@ -509,31 +509,7 @@ class WebKitScroller(browsertab.AbstractScroller): return self.pos_px().y() >= frame.scrollBarMaximum(Qt.Vertical) -class WebKitHistory(browsertab.AbstractHistory): - - """QtWebKit implementations related to page history.""" - - def __len__(self): - return len(self._history) - - def __iter__(self): - return iter(self._history.items()) - - def current_idx(self): - return self._history.currentItemIndex() - - def can_go_back(self): - return self._history.canGoBack() - - def can_go_forward(self): - return self._history.canGoForward() - - def _item_at(self, i): - return self._history.itemAt(i) - - def _go_to_item(self, item): - self._tab.predicted_navigation.emit(item.url()) - self._history.goToItem(item) +class WebKitHistoryPrivate(browsertab.AbstractHistoryPrivate): def serialize(self): return qtutils.serialize(self._history) @@ -559,6 +535,37 @@ class WebKitHistory(browsertab.AbstractHistory): self._tab.scroller.to_point, cur_data['scroll-pos'])) +class WebKitHistory(browsertab.AbstractHistory): + + """QtWebKit implementations related to page history.""" + + def __init__(self, tab): + super().__init__(tab) + self.private_api = WebKitHistoryPrivate(tab) + + def __len__(self): + return len(self._history) + + def __iter__(self): + return iter(self._history.items()) + + def current_idx(self): + return self._history.currentItemIndex() + + def can_go_back(self): + return self._history.canGoBack() + + def can_go_forward(self): + return self._history.canGoForward() + + def _item_at(self, i): + return self._history.itemAt(i) + + def _go_to_item(self, item): + self._tab.predicted_navigation.emit(item.url()) + self._history.goToItem(item) + + class WebKitElements(browsertab.AbstractElements): """QtWebKit implemementations related to elements on the page.""" diff --git a/qutebrowser/mainwindow/tabbedbrowser.py b/qutebrowser/mainwindow/tabbedbrowser.py index 5530b0e37..07a1122d3 100644 --- a/qutebrowser/mainwindow/tabbedbrowser.py +++ b/qutebrowser/mainwindow/tabbedbrowser.py @@ -345,7 +345,7 @@ class TabbedBrowser(QWidget): urlutils.invalid_url_error(tab.url(), "saving tab") elif add_undo: try: - history_data = tab.history.serialize() + history_data = tab.history.private_api.serialize() except browsertab.WebTabError: pass # special URL else: @@ -391,7 +391,7 @@ class TabbedBrowser(QWidget): else: newtab = self.tabopen(background=False, idx=entry.index) - newtab.history.deserialize(entry.history) + newtab.history.private_api.deserialize(entry.history) self.widget.set_tab_pinned(newtab, entry.pinned) @pyqtSlot('QUrl', bool) diff --git a/qutebrowser/misc/sessions.py b/qutebrowser/misc/sessions.py index 4f80ad2b0..357d5be64 100644 --- a/qutebrowser/misc/sessions.py +++ b/qutebrowser/misc/sessions.py @@ -401,7 +401,7 @@ class SessionManager(QObject): new_tab.title_changed.emit(histentry['title']) try: - new_tab.history.load_items(entries) + new_tab.history.private_api.load_items(entries) except ValueError as e: raise SessionError(e)