Skip to content

Commit

Permalink
qt: force text/html as the mimetype for index.html
Browse files Browse the repository at this point in the history
Fixes #3061.

Without this, on linux the local mimetype database (influenced by
various packages installed on the system) can mess with the mimetype
resolution of our .html file served locally, resulting in a blank
page. Notably, FireFox on linux sets application/x-extension-html as
the mimetype for .html files when made the default browser.

This problem is new in Qt 6, on Qt 5.15 it worked fine for some
reason.

The general solution is to hardcode mime-types in a custom url scheme
handler, but that requires using a different scheme to `qrc:`, which
we can't do right now as Moonpay whitelisted only `qrc:`.
  • Loading branch information
benma committed Nov 21, 2024
1 parent 67352e1 commit 08dc862
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# 4.46.1
- Fix Android app crash on old Android versions
- Fix Linux blank screen issue related to the local mimetype database

# 4.46.0
- Android: enable export logs feature
Expand Down
19 changes: 17 additions & 2 deletions frontends/qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <singleapplication.h>
#include <QApplication>
#include <QCoreApplication>
#include <QFile>
#include <QWebEngineView>
#include <QWebEngineProfile>
#include <QWebEnginePage>
Expand Down Expand Up @@ -116,7 +117,7 @@ class RequestInterceptor : public QWebEngineUrlRequestInterceptor {
explicit RequestInterceptor() : QWebEngineUrlRequestInterceptor() { }
void interceptRequest(QWebEngineUrlRequestInfo& info) override {
// Do not block qrc:/ local pages or js blobs
if (info.requestUrl().scheme() == "qrc" || info.requestUrl().scheme() == "blob") {
if (info.requestUrl().scheme() == "qrc" || info.requestUrl().scheme() == "blob" || info.requestUrl().scheme() == "data") {
return;
}

Expand Down Expand Up @@ -204,6 +205,16 @@ class WebEngineView : public QWebEngineView {
}
};

static QString loadHtmlFromQrc(const QString &filePath) {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
std::cerr << "Failed to load file:" << filePath.toStdString() << std::endl;
return QString("Failed to load file");
}
QTextStream stream(&file);
return stream.readAll();
}

int main(int argc, char *argv[])
{
// Enable auto HiDPI scaling to correctly manage scale factor on bigger screens
Expand Down Expand Up @@ -385,7 +396,11 @@ int main(int argc, char *argv[])
channel.registerObject("backend", webClass);
view->page()->setWebChannel(&channel);
view->show();
view->load(QUrl("qrc:/index.html"));
// We use setHtml instead of load() because on linux, the local mimetype database is changed
// when FireFox sets itself as the default browser, and .html files are delivered as
// "application/x-extension-html", making the webview show a blank page instead of our app.
// The qrc:/ base name is so Moonpay can load (they whitelisted this base domain :o).
view->setHtml(loadHtmlFromQrc(":/index.html"), QUrl("qrc:/"));

// Create TrayIcon
{
Expand Down

0 comments on commit 08dc862

Please sign in to comment.