Skip to content

Commit

Permalink
Wait for CEF close event for docks
Browse files Browse the repository at this point in the history
The original solution works fine when not using the Qt event loop,
however any method of waiting with the Qt event loop causes all
CEF events to go on hold too, resulting in a hang or crash.

Instead, wait for CEF to announce it's ready, then delete the widget.
  • Loading branch information
WizardCM authored and RytoEX committed Oct 18, 2024
1 parent 31fd647 commit 910617b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
7 changes: 7 additions & 0 deletions panel/browser-panel-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ bool QCefBrowserClient::OnBeforePopup(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>
return true;
}

void QCefBrowserClient::OnBeforeClose(CefRefPtr<CefBrowser>)
{
if (widget) {
emit widget->CloseSafely();
}
}

bool QCefBrowserClient::OnSetFocus(CefRefPtr<CefBrowser>, CefFocusHandler::FocusSource source)
{
/* Don't steal focus when the webpage navigates. This is especially
Expand Down
2 changes: 2 additions & 0 deletions panel/browser-panel-client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class QCefBrowserClient : public CefClient,
CefRefPtr<CefClient> &client, CefBrowserSettings &settings,
CefRefPtr<CefDictionaryValue> &extra_info, bool *no_javascript_access) override;

virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;

/* CefFocusHandler */
virtual bool OnSetFocus(CefRefPtr<CefBrowser> browser, CefFocusHandler::FocusSource source) override;

Expand Down
4 changes: 4 additions & 0 deletions panel/browser-panel-internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class QCefWidgetInternal : public QCefWidget {
virtual bool zoomPage(int direction) override;
virtual void executeJavaScript(const std::string &script) override;

void CloseSafely();
void Resize();

#ifdef __linux__
Expand All @@ -61,4 +62,7 @@ class QCefWidgetInternal : public QCefWidget {

public slots:
void Init();

signals:
void readyToClose();
};
24 changes: 16 additions & 8 deletions panel/browser-panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,24 @@ void QCefWidgetInternal::closeBrowser()
{
CefRefPtr<CefBrowser> browser = cefBrowser;
if (!!browser) {
auto destroyBrowser = [](CefRefPtr<CefBrowser> cefBrowser) {
auto destroyBrowser = [=](CefRefPtr<CefBrowser> cefBrowser) {
CefRefPtr<CefClient> client = cefBrowser->GetHost()->GetClient();
QCefBrowserClient *bc = reinterpret_cast<QCefBrowserClient *>(client.get());

if (bc) {
bc->widget = nullptr;
}

cefBrowser->GetHost()->CloseBrowser(true);

#if !defined(_WIN32) && !defined(__APPLE__) && CHROME_VERSION_BUILD >= 6533
while (cefBrowser && cefBrowser->IsValid()) {
os_sleep_ms(10);
}
QEventLoop loop;

connect(this, &QCefWidgetInternal::readyToClose, &loop, &QEventLoop::quit);

QTimer::singleShot(1000, &loop, &QEventLoop::quit);

loop.exec();
#endif
if (bc) {
bc->widget = nullptr;
}
};

/* So you're probably wondering what's going on here. If you
Expand Down Expand Up @@ -398,6 +401,11 @@ void QCefWidgetInternal::Resize()
#endif
}

void QCefWidgetInternal::CloseSafely()
{
emit readyToClose();
}

void QCefWidgetInternal::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);
Expand Down

0 comments on commit 910617b

Please sign in to comment.