Browse Source

fix QWebEngine crash on exit

Sonnix 5 years ago
parent
commit
9738aca3cd
4 changed files with 71 additions and 44 deletions
  1. 6 12
      mainwindow.cpp
  2. 57 30
      qspwebbox.cpp
  3. 7 2
      qspwebbox.h
  4. 1 0
      qspwebbox_webkit.h

+ 6 - 12
mainwindow.cpp

@@ -732,12 +732,6 @@ void MainWindow::CreateDockWindows()
 #ifdef _WEBBOX
     _mainDescTextBox = new QspWebBox(this);
     connect(_mainDescTextBox, SIGNAL(qspLinkClicked(QUrl)), this, SLOT(OnLinkClicked(QUrl)));
-    _mainDescTextBox->page()->load(QUrl("qsp:/"));
-    {
-        QEventLoop loop;
-        connect(_mainDescTextBox->page(), SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
-        loop.exec();
-    }
 #endif
 #ifdef _WEBBOX_WEBKIT
     _mainDescTextBox = new QspWebBox(this);
@@ -791,12 +785,6 @@ void MainWindow::CreateDockWindows()
 #ifdef _WEBBOX
     _descTextBox = new QspWebBox(this);
     connect(_descTextBox, SIGNAL(qspLinkClicked(QUrl)), this, SLOT(OnLinkClicked(QUrl)));
-    _descTextBox->page()->load(QUrl("qsp:/"));
-    {
-        QEventLoop loop;
-        connect(_descTextBox->page(), SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
-        loop.exec();
-    }
 #endif
 #ifdef _WEBBOX_WEBKIT
     _descTextBox = new QspWebBox(this);
@@ -834,6 +822,12 @@ void MainWindow::CreateDockWindows()
 
 void MainWindow::closeEvent(QCloseEvent *event)
 {
+#ifdef _WEBBOX
+    _mainDescTextBox->Quit();
+    _descTextBox->Quit();
+    //delete _mainDescTextBox;
+    //delete _descTextBox;
+#endif
     if(!m_configPath.isEmpty())
         SaveSettings(m_configPath);
     QFileInfo settingsFile(QApplication::applicationDirPath() + "/" + QSP_CUSTOM_CONFIG);

+ 57 - 30
qspwebbox.cpp

@@ -5,21 +5,21 @@
 #include <QAbstractScrollArea>
 #include <QScrollBar>
 #include <QPainter>
-#include <QWebEngineProfile>
 #include <QWebEngineSettings>
 //#include <QTimer>
 #include <QEventLoop>
 
 #include "comtools.h"
 
-#include "qspwebengineurlrequestinterceptor.h"
-#include "qspwebengineurlschemehandler.h"
-#include "qspexecwebengineurlschemehandler.h"
+//#include "qspwebengineurlrequestinterceptor.h"
 
 QspWebBox::QspWebBox(QWidget *parent) : QWebEngineView(parent)
 {
+    m_isQuit = false;
     settings()->setDefaultTextEncoding("utf-8");
     setFocusPolicy(Qt::NoFocus);
+    settings()->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, false);
+    settings()->setUnknownUrlSchemePolicy(QWebEngineSettings::AllowAllUnknownUrlSchemes);
     //setFrameStyle(QFrame::NoFrame);
     //setFrameShadow(QFrame::Plain);
     setContextMenuPolicy( Qt::NoContextMenu );
@@ -29,19 +29,26 @@ QspWebBox::QspWebBox(QWidget *parent) : QWebEngineView(parent)
     m_videoFix = true;
     m_font = font();
     //setOpenLinks(false);
-    profile = new QWebEngineProfile(this);
     //QspWebEngineUrlRequestInterceptor *qwuri = new QspWebEngineUrlRequestInterceptor(this);
     //profile->setRequestInterceptor(qwuri);
-    qweush = new QspWebEngineUrlSchemeHandler(this);
-    profile->installUrlSchemeHandler(QByteArray("qsp"), qweush);
-    QspExecWebEngineUrlSchemeHandler *qeweush = new QspExecWebEngineUrlSchemeHandler(this);
-    profile->installUrlSchemeHandler(QByteArray("exec"), qeweush);
-    QWebEnginePage * page = new QWebEnginePage(profile, this);
-    setPage(page);
-    QWebChannel *channel = new QWebChannel(page);
+    profile.installUrlSchemeHandler(QByteArray("qsp"), &qweush);
+    profile.installUrlSchemeHandler(QByteArray("exec"), &qeweush);
+    page()->triggerAction(QWebEnginePage::Stop);
+    page()->deleteLater();
+    QWebEnginePage * newpage = new QWebEnginePage(&profile, this);
+    QWebChannel *channel = new QWebChannel(newpage);
     channel->registerObject(QStringLiteral("qsp"), &qspJS);
-    page->setWebChannel(channel);
-    connect(qeweush, SIGNAL(qspLinkClicked(QUrl)), this, SLOT(OnQspLinkClicked(QUrl)));
+    newpage->setWebChannel(channel);
+    newpage->settings()->setDefaultTextEncoding("utf-8");
+    newpage->settings()->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, false);
+    newpage->settings()->setUnknownUrlSchemePolicy(QWebEngineSettings::AllowAllUnknownUrlSchemes);
+    page()->deleteLater();
+    setPage(newpage);
+    connect(&qeweush, SIGNAL(qspLinkClicked(QUrl)), this, SLOT(OnQspLinkClicked(QUrl)));
+    QEventLoop loop;
+    connect(page(), SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
+    page()->load(QUrl("qsp:/"));
+    loop.exec();
 }
 
 QspWebBox::~QspWebBox()
@@ -60,6 +67,8 @@ void QspWebBox::SetIsHtml(bool isHtml)
 
 void QspWebBox::RefreshUI(bool isScroll)
 {
+    if(m_isQuit)
+        return;
     QString color(QSPTools::GetHexColor(GetForegroundColor()));
     QString str = m_text;
     QString text;
@@ -117,21 +126,26 @@ void QspWebBox::RefreshUI(bool isScroll)
 
     text = QSPTools::HtmlizeWhitespaces(m_isUseHtml ? text : QSPTools::ProceedAsPlain(text));
     if(showPlainText)
-        qweush->SetPlainText(text);
+        qweush.SetPlainText(text);
     else
-        qweush->SetHtml(text);
+        qweush.SetHtml(text);
 
     QString url_str = QByteArray::fromPercentEncoding(url().toString().toUtf8());
     if(url_str.compare("qsp:" , Qt::CaseInsensitive) != 0 && url_str.compare("qsp:/" , Qt::CaseInsensitive) != 0)
     {
-        QWebEnginePage * newpage = new QWebEnginePage(profile, this);
+        page()->triggerAction(QWebEnginePage::Stop);
+        page()->deleteLater();
+        QWebEnginePage * newpage = new QWebEnginePage(&profile, this);
+        QWebChannel *channel = new QWebChannel(newpage);
+        channel->registerObject(QStringLiteral("qsp"), &qspJS);
+        newpage->setWebChannel(channel);
+        newpage->settings()->setDefaultTextEncoding("utf-8");
+        newpage->settings()->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, false);
+        newpage->settings()->setUnknownUrlSchemePolicy(QWebEngineSettings::AllowAllUnknownUrlSchemes);
         setPage(newpage);
         QEventLoop loop;
         connect(page(), SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
         page()->load(QUrl("qsp:/"));
-        QWebChannel *channel = new QWebChannel(newpage);
-        channel->registerObject(QStringLiteral("qsp"), &qspJS);
-        newpage->setWebChannel(channel);
         loop.exec();
     }
     page()->triggerAction(QWebEnginePage::ReloadAndBypassCache);
@@ -152,7 +166,7 @@ void QspWebBox::RefreshUI(bool isScroll)
 
 void QspWebBox::LoadBackImage(const QString& fileName)
 {
-    qweush->SetBackgroundImage(fileName);
+    qweush.SetBackgroundImage(fileName);
     QFileInfo file(m_path + fileName);
     QString path(file.absoluteFilePath());
 }
@@ -176,7 +190,7 @@ void QspWebBox::SetTextFont(const QFont& new_font)
     if (m_font != new_font)
     {
         m_font = new_font;
-        qweush->SetTextFont(new_font);
+        qweush.SetTextFont(new_font);
     }
 }
 
@@ -185,7 +199,7 @@ bool QspWebBox::SetLinkColor(const QColor &color)
     if(m_linkColor != color)
     {
         m_linkColor = color;
-        qweush->SetLinkColor(color);
+        qweush.SetLinkColor(color);
         RefreshUI();
         return true;
     }
@@ -195,7 +209,7 @@ bool QspWebBox::SetLinkColor(const QColor &color)
 void QspWebBox::SetGamePath(const QString &path)
 {
     m_path = path;
-    qweush->SetGamePath(path);
+    qweush.SetGamePath(path);
 }
 
 //Returns the background color of the window.
@@ -216,7 +230,7 @@ bool QspWebBox::SetBackgroundColor(const QColor &color)
     if(m_backColor != color)
     {
         m_backColor = color;
-        qweush->SetBackgroundColor(color);
+        qweush.SetBackgroundColor(color);
         RefreshUI();
         return true;
     }
@@ -228,7 +242,7 @@ bool QspWebBox::SetForegroundColor(const QColor &color)
     if(m_fontColor != color)
     {
         m_fontColor = color;
-        qweush->SetForegroundColor(color);
+        qweush.SetForegroundColor(color);
         RefreshUI();
         return true;
     }
@@ -251,7 +265,7 @@ void QspWebBox::SetHead(const QString &head)
     if(m_head != head)
     {
         m_head = head;
-        qweush->SetHead(head);
+        qweush.SetHead(head);
         RefreshUI();
     }
 }
@@ -261,7 +275,7 @@ void QspWebBox::SetFontType(const int fontType)
     if(m_fontType != fontType)
     {
         m_fontType = fontType;
-        qweush->SetFontType(fontType);
+        qweush.SetFontType(fontType);
         RefreshUI();
     }
 }
@@ -271,14 +285,27 @@ void QspWebBox::SetSizeType(const int sizeType)
     if(m_sizeType != sizeType)
     {
         m_sizeType = sizeType;
-        qweush->SetSizeType(sizeType);
+        qweush.SetSizeType(sizeType);
         RefreshUI();
     }
 }
 
 void QspWebBox::SetCustomCSS(bool customCSS)
 {
-    qweush->SetCustomCSS(customCSS);
+    qweush.SetCustomCSS(customCSS);
+}
+
+void QspWebBox::Quit()
+{
+    m_isQuit = true;
+    page()->triggerAction(QWebEnginePage::Stop);
+    page()->deleteLater();
+    QWebEnginePage * newpage = new QWebEnginePage(this);
+    setPage(newpage);
+    QEventLoop loop;
+    connect(page(), SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
+    page()->load(QUrl("about:blank"));
+    loop.exec();
 }
 
 void QspWebBox::OnQspLinkClicked(QUrl url)

+ 7 - 2
qspwebbox.h

@@ -9,8 +9,10 @@
 #include <QVariant>
 #include <QUrl>
 #include <QWebChannel>
+#include <QWebEngineProfile>
 
 #include "qspwebengineurlschemehandler.h"
+#include "qspexecwebengineurlschemehandler.h"
 #include "qspwebchannel.h"
 
 namespace Ui {
@@ -48,6 +50,7 @@ public:
     void SetFontType(const int fontType);
     void SetSizeType(const int sizeType);
     void SetCustomCSS(bool customCSS);
+    void Quit();
 
 private:
     // Fields
@@ -67,10 +70,12 @@ private:
     bool m_videoFix;
     int m_fontType;
     int m_sizeType;
-    QspWebEngineUrlSchemeHandler *qweush;
-    QWebEngineProfile *profile;
+    QspWebEngineUrlSchemeHandler qweush;
+    QspExecWebEngineUrlSchemeHandler qeweush;
+    QWebEngineProfile profile;
     QWebChannel *channel;
     QspWebChannel qspJS;
+    bool m_isQuit;
 
 signals:
     void qspLinkClicked(QUrl url);

+ 1 - 0
qspwebbox_webkit.h

@@ -47,6 +47,7 @@ public:
     void SetFontType(const int fontType) {};
     void SetSizeType(const int sizeType) {};
     void SetCustomCSS(bool customCSS) {};
+    void Quit();
 
 private:
     // Fields