Skip to content

Commit

Permalink
BugFix Crash Scenario When Switch Tab
Browse files Browse the repository at this point in the history
Qt's WebChannel's signal JsonObject references are unreliable, so use QString for parsing. Also added guard against a potential nullptr access.
  • Loading branch information
ShaopengLin committed Nov 13, 2024
1 parent 0beea2d commit b5d3ff0
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions resources/js/headerAnchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function anchorHeaderElements(headers)
});
}

function getHeaders()
function getHeadersJSONStr()
{
const headerInfo = { url: window.location.href.replace(location.hash,""), headers: [] };

Expand All @@ -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.sendHeadersJSONStr(getHeadersJSONStr());
kiwixObj.navigationRequested.connect(function(url, anchor) {
if (window.location.href.replace(location.hash,"") == url)
document.getElementById(anchor).scrollIntoView();
Expand Down
4 changes: 2 additions & 2 deletions src/kiwixwebchannelobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 sendHeadersJSONStr(const QString& headersJSONStr) { emit headersChanged(headersJSONStr); };

signals:
void headersChanged(const QJsonObject& headers);
void headersChanged(const QString& headersJSONStr);
void navigationRequested(const QString& url, const QString& anchor);
};

Expand Down
5 changes: 4 additions & 1 deletion src/tableofcontentbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ void WebView::onCurrentTitleChanged()
emit headersChanged(m_headers);
}

void WebView::onHeadersReceived(const QJsonObject& headers)
void WebView::onHeadersReceived(const QString& headersJSONStr)
{
const auto tabbar = KiwixApp::instance()->getTabWidget();
m_headers = QJsonObject(headers);
m_headers = QJsonDocument::fromJson(headersJSONStr.toUtf8()).object();

if (tabbar->currentWebView() == this)
emit headersChanged(m_headers);
Expand Down
2 changes: 1 addition & 1 deletion src/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public slots:
private slots:
void gotoTriggeredHistoryItemAction();
void onCurrentTitleChanged();
void onHeadersReceived(const QJsonObject& headers);
void onHeadersReceived(const QString& headersJSONStr);
void onNavigationRequested(const QString& url, const QString& anchor);

private:
Expand Down

0 comments on commit b5d3ff0

Please sign in to comment.