From 2897e7d705fc2a415d396d38c3240c15c03d8020 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Wed, 13 Nov 2024 15:59:54 -0500 Subject: [PATCH] BugFix Crash Scenario When Switch Tab Qt's WebChannel's signal JsonObject references are unreliable, so use QString for parsing. Also added guard against a potential nullptr access. --- resources/js/headerAnchor.js | 6 +++--- src/kiwixwebchannelobject.h | 4 ++-- src/tableofcontentbar.cpp | 5 ++++- src/webview.cpp | 4 ++-- src/webview.h | 2 +- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/resources/js/headerAnchor.js b/resources/js/headerAnchor.js index aec9ce91..ede8d1ff 100644 --- a/resources/js/headerAnchor.js +++ b/resources/js/headerAnchor.js @@ -31,7 +31,7 @@ function anchorHeaderElements(headers) }); } -function getHeaders() +function getHeadersJSON() { const headerInfo = { url: window.location.href.replace(location.hash,""), headers: [] }; @@ -40,12 +40,12 @@ function getHeaders() const headers = getDOMElementsPreorderDFS(document.body, isHeaderElement); headerInfo.headers = anchorHeaderElements(headers); } - return headerInfo; + return JSON.stringify(headerInfo); } new QWebChannel(qt.webChannelTransport, function(channel) { var kiwixObj = channel.objects.kiwixChannelObj; - kiwixObj.sendHeaders(getHeaders()); + kiwixObj.sendHeadersJSON(getHeadersJSON()); kiwixObj.navigationRequested.connect(function(url, anchor) { if (window.location.href.replace(location.hash,"") == url) document.getElementById(anchor).scrollIntoView(); diff --git a/src/kiwixwebchannelobject.h b/src/kiwixwebchannelobject.h index 538c0da6..a4afaf22 100644 --- a/src/kiwixwebchannelobject.h +++ b/src/kiwixwebchannelobject.h @@ -10,10 +10,10 @@ class KiwixWebChannelObject : public QObject public: explicit KiwixWebChannelObject(QObject *parent = nullptr) : QObject(parent) {}; - Q_INVOKABLE void sendHeaders(const QJsonObject& headers) { emit headersChanged(headers); }; + Q_INVOKABLE void sendHeadersJSON(const QString& headersJSON) { emit headersChanged(headersJSON); }; signals: - void headersChanged(const QJsonObject& headers); + void headersChanged(const QString& headersJSON); void navigationRequested(const QString& url, const QString& anchor); }; diff --git a/src/tableofcontentbar.cpp b/src/tableofcontentbar.cpp index 97c2f881..78531db3 100644 --- a/src/tableofcontentbar.cpp +++ b/src/tableofcontentbar.cpp @@ -85,8 +85,11 @@ void createSubTree(QTreeWidgetItem* parent, QString parentNo, QJsonArray& header void TableOfContentBar::setupTree(const QJsonObject& headers) { - const auto headerUrl = headers["url"].toString(); const auto webView = KiwixApp::instance()->getTabWidget()->currentWebView(); + if (!webView) + return; + + const auto headerUrl = headers["url"].toString(); const auto currentUrl = webView->url().url(QUrl::RemoveFragment); if (headerUrl != currentUrl) return; diff --git a/src/webview.cpp b/src/webview.cpp index 006ce733..4b44faa4 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -221,10 +221,10 @@ void WebView::onCurrentTitleChanged() emit headersChanged(m_headers); } -void WebView::onHeadersReceived(const QJsonObject& headers) +void WebView::onHeadersReceived(const QString& headersJSON) { const auto tabbar = KiwixApp::instance()->getTabWidget(); - m_headers = QJsonObject(headers); + m_headers = QJsonDocument::fromJson(headersJSON.toUtf8()).object(); if (tabbar->currentWebView() == this) emit headersChanged(m_headers); diff --git a/src/webview.h b/src/webview.h index 6ec8b5bf..793136b4 100644 --- a/src/webview.h +++ b/src/webview.h @@ -71,7 +71,7 @@ public slots: private slots: void gotoTriggeredHistoryItemAction(); void onCurrentTitleChanged(); - void onHeadersReceived(const QJsonObject& headers); + void onHeadersReceived(const QString& headersJSON); void onNavigationRequested(const QString& url, const QString& anchor); private: