First prototype of QtWebKit-NG history/session support
This commit is contained in:
parent
348a50ad69
commit
dfbcb75313
@ -21,21 +21,41 @@
|
||||
|
||||
|
||||
from PyQt5.QtCore import QByteArray, QDataStream, QIODevice, QUrl
|
||||
from PyQt5.QtWebKit import qWebKitVersion # FIXME can we guarantee WebKit is available here?
|
||||
|
||||
from qutebrowser.utils import qtutils
|
||||
|
||||
|
||||
HISTORY_STREAM_VERSION = 2
|
||||
BACK_FORWARD_TREE_VERSION = 2
|
||||
|
||||
|
||||
def _encode_url(url):
|
||||
"""Encode a QUrl suitable to pass to QWebHistory."""
|
||||
data = bytes(QUrl.toPercentEncoding(url.toString(), b':/#?&+=@%*'))
|
||||
return data.decode('ascii')
|
||||
|
||||
|
||||
def _serialize_item(i, item, stream):
|
||||
def _serialize_item_ng(i, item):
|
||||
data = {
|
||||
'children': [],
|
||||
'documentSequenceNumber': i + 1, # FIXME what to pass here?
|
||||
'documentState': [],
|
||||
'formContentType': '',
|
||||
'itemSequenceNumber': i + 1, # FIXME what to pass here?
|
||||
'originalURLString': item.original_url.toString(), # FIXME encoding?
|
||||
'pageScaleFactor': 0.0,
|
||||
'referrer': '',
|
||||
'scrollPosition': {'x': 0, 'y': 0},
|
||||
'target': '',
|
||||
'title': item.title,
|
||||
'urlString': item.url.toString(), # FIXME encoding?
|
||||
}
|
||||
try:
|
||||
data['scrollPosition']['x'] = item.user_data['scroll-pos'].x()
|
||||
data['scrollPosition']['y'] = item.user_data['scroll-pos'].y()
|
||||
except (KeyError, TypeError):
|
||||
pass
|
||||
return data
|
||||
|
||||
|
||||
def _serialize_item_old(i, item, stream):
|
||||
"""Serialize a single WebHistoryItem into a QDataStream.
|
||||
|
||||
Args:
|
||||
@ -53,7 +73,7 @@ def _serialize_item(i, item, stream):
|
||||
|
||||
### Source/WebCore/history/HistoryItem.cpp decodeBackForwardTree
|
||||
## backForwardTreeEncodingVersion
|
||||
stream.writeUInt32(BACK_FORWARD_TREE_VERSION)
|
||||
stream.writeUInt32(2)
|
||||
## size (recursion stack)
|
||||
stream.writeUInt64(0)
|
||||
## node->m_documentSequenceNumber
|
||||
@ -101,6 +121,39 @@ def _serialize_item(i, item, stream):
|
||||
stream.writeBool(False)
|
||||
|
||||
|
||||
def _serialize_old(items, current_idx, stream):
|
||||
### Source/WebKit/qt/Api/qwebhistory.cpp operator<<
|
||||
stream.writeInt(2) # history stream version
|
||||
stream.writeInt(len(items))
|
||||
stream.writeInt(current_idx)
|
||||
|
||||
for i, item in enumerate(items):
|
||||
_serialize_item_old(i, item, stream)
|
||||
user_data.append(item.user_data)
|
||||
|
||||
|
||||
def _serialize_ng(items, current_idx, stream):
|
||||
# {'currentItemIndex': 0,
|
||||
# 'history': [{'children': [],
|
||||
# 'documentSequenceNumber': 1485030525573123,
|
||||
# 'documentState': [],
|
||||
# 'formContentType': '',
|
||||
# 'itemSequenceNumber': 1485030525573122,
|
||||
# 'originalURLString': 'about:blank',
|
||||
# 'pageScaleFactor': 0.0,
|
||||
# 'referrer': '',
|
||||
# 'scrollPosition': {'x': 0, 'y': 0},
|
||||
# 'target': '',
|
||||
# 'title': '',
|
||||
# 'urlString': 'about:blank'}]}
|
||||
data = {'currentItemIndex': current_idx, 'history': []}
|
||||
for i, item in enumerate(items):
|
||||
data['history'].append(_serialize_item_ng(i, item))
|
||||
|
||||
stream.writeInt(3) # history stream version
|
||||
stream.writeQVariantMap(data)
|
||||
|
||||
|
||||
def serialize(items):
|
||||
"""Serialize a list of QWebHistoryItems to a data stream.
|
||||
|
||||
@ -137,13 +190,12 @@ def serialize(items):
|
||||
else:
|
||||
current_idx = 0
|
||||
|
||||
### Source/WebKit/qt/Api/qwebhistory.cpp operator<<
|
||||
stream.writeInt(HISTORY_STREAM_VERSION)
|
||||
stream.writeInt(len(items))
|
||||
stream.writeInt(current_idx)
|
||||
if qWebKitVersion() == '538.1': # FIXME better comparison
|
||||
_serialize_old(items, current_idx, stream)
|
||||
else:
|
||||
_serialize_ng(items, current_idx, stream)
|
||||
|
||||
for i, item in enumerate(items):
|
||||
_serialize_item(i, item, stream)
|
||||
for i, item in enumerate(items): # FIXME easier way?
|
||||
user_data.append(item.user_data)
|
||||
|
||||
stream.device().reset()
|
||||
|
@ -23,7 +23,7 @@
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
from PyQt5.QtCore import QUrl
|
||||
from PyQt5.QtCore import QUrl, QDataStream, QIODevice, QByteArray
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
|
||||
try:
|
||||
@ -49,6 +49,15 @@ def parse_args():
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def dump_history(wv):
|
||||
ba = QByteArray()
|
||||
ds = QDataStream(ba, QIODevice.ReadWrite)
|
||||
ds << wv.history()
|
||||
ds2 = QDataStream(ba)
|
||||
assert ds2.readInt() == 3
|
||||
print(ds2.readQVariantMap())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
args = parse_args()
|
||||
app = QApplication(sys.argv)
|
||||
@ -73,6 +82,7 @@ if __name__ == '__main__':
|
||||
wv.loadStarted.connect(lambda: print("Loading started"))
|
||||
wv.loadProgress.connect(lambda p: print("Loading progress: {}%".format(p)))
|
||||
wv.loadFinished.connect(lambda: print("Loading finished"))
|
||||
wv.loadFinished.connect(lambda: dump_history(wv))
|
||||
|
||||
if args.plugins and not using_webengine:
|
||||
from PyQt5.QtWebKit import QWebSettings
|
||||
|
Loading…
Reference in New Issue
Block a user