Move history (de)serializing to a private_api object

This commit is contained in:
Florian Bruhin 2018-11-30 08:23:52 +01:00
parent 27ee3280b2
commit 5bf0dffa95
6 changed files with 92 additions and 67 deletions

View File

@ -592,6 +592,27 @@ class AbstractScroller(QObject):
raise NotImplementedError 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: class AbstractHistory:
"""The history attribute of a AbstractTab.""" """The history attribute of a AbstractTab."""
@ -599,6 +620,7 @@ class AbstractHistory:
def __init__(self, tab: 'AbstractTab') -> None: def __init__(self, tab: 'AbstractTab') -> None:
self._tab = tab self._tab = tab
self._history = None self._history = None
self.private_api = AbstractHistoryPrivate(tab)
def __len__(self) -> int: def __len__(self) -> int:
raise NotImplementedError raise NotImplementedError
@ -646,18 +668,6 @@ class AbstractHistory:
def _go_to_item(self, item: typing.Any) -> None: def _go_to_item(self, item: typing.Any) -> None:
raise NotImplementedError 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: class AbstractElements:
@ -885,6 +895,7 @@ class AbstractTab(QWidget):
self._widget = widget self._widget = widget
self._layout.wrap(self, widget) self._layout.wrap(self, widget)
self.history._history = widget.history() self.history._history = widget.history()
self.history.private_api._history = widget.history()
self.scroller._init_widget(widget) self.scroller._init_widget(widget)
self.caret._widget = widget self.caret._widget = widget
self.zoom._widget = widget self.zoom._widget = widget

View File

@ -463,7 +463,7 @@ class CommandDispatcher:
cur_title = self._tabbed_browser.widget.page_title( cur_title = self._tabbed_browser.widget.page_title(
self._current_index()) self._current_index())
try: try:
history = curtab.history.serialize() history = curtab.history.private_api.serialize()
except browsertab.WebTabError as e: except browsertab.WebTabError as e:
raise cmdutils.CommandError(e) raise cmdutils.CommandError(e)
@ -486,7 +486,7 @@ class CommandDispatcher:
new_tabbed_browser.widget.window().setWindowIcon(curtab.icon()) new_tabbed_browser.widget.window().setWindowIcon(curtab.icon())
newtab.data.keep_icon = True newtab.data.keep_icon = True
newtab.history.deserialize(history) newtab.history.private_api.deserialize(history)
newtab.zoom.set_factor(curtab.zoom.factor()) newtab.zoom.set_factor(curtab.zoom.factor())
new_tabbed_browser.widget.set_tab_pinned(newtab, curtab.data.pinned) new_tabbed_browser.widget.set_tab_pinned(newtab, curtab.data.pinned)
return newtab return newtab

View File

@ -518,31 +518,7 @@ class WebEngineScroller(browsertab.AbstractScroller):
return self._at_bottom return self._at_bottom
class WebEngineHistory(browsertab.AbstractHistory): class WebEngineHistoryPrivate(browsertab.AbstractHistoryPrivate):
"""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)
def serialize(self): def serialize(self):
if not qtutils.version_check('5.9', compiled=False): 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) 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): class WebEngineZoom(browsertab.AbstractZoom):
"""QtWebEngine implementations related to zooming.""" """QtWebEngine implementations related to zooming."""

View File

@ -509,31 +509,7 @@ class WebKitScroller(browsertab.AbstractScroller):
return self.pos_px().y() >= frame.scrollBarMaximum(Qt.Vertical) return self.pos_px().y() >= frame.scrollBarMaximum(Qt.Vertical)
class WebKitHistory(browsertab.AbstractHistory): class WebKitHistoryPrivate(browsertab.AbstractHistoryPrivate):
"""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)
def serialize(self): def serialize(self):
return qtutils.serialize(self._history) return qtutils.serialize(self._history)
@ -559,6 +535,37 @@ class WebKitHistory(browsertab.AbstractHistory):
self._tab.scroller.to_point, cur_data['scroll-pos'])) 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): class WebKitElements(browsertab.AbstractElements):
"""QtWebKit implemementations related to elements on the page.""" """QtWebKit implemementations related to elements on the page."""

View File

@ -345,7 +345,7 @@ class TabbedBrowser(QWidget):
urlutils.invalid_url_error(tab.url(), "saving tab") urlutils.invalid_url_error(tab.url(), "saving tab")
elif add_undo: elif add_undo:
try: try:
history_data = tab.history.serialize() history_data = tab.history.private_api.serialize()
except browsertab.WebTabError: except browsertab.WebTabError:
pass # special URL pass # special URL
else: else:
@ -391,7 +391,7 @@ class TabbedBrowser(QWidget):
else: else:
newtab = self.tabopen(background=False, idx=entry.index) 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) self.widget.set_tab_pinned(newtab, entry.pinned)
@pyqtSlot('QUrl', bool) @pyqtSlot('QUrl', bool)

View File

@ -401,7 +401,7 @@ class SessionManager(QObject):
new_tab.title_changed.emit(histentry['title']) new_tab.title_changed.emit(histentry['title'])
try: try:
new_tab.history.load_items(entries) new_tab.history.private_api.load_items(entries)
except ValueError as e: except ValueError as e:
raise SessionError(e) raise SessionError(e)