Skip to content

Commit 910617b

Browse files
WizardCMRytoEX
authored andcommitted
Wait for CEF close event for docks
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.
1 parent 31fd647 commit 910617b

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

panel/browser-panel-client.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ bool QCefBrowserClient::OnBeforePopup(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>
213213
return true;
214214
}
215215

216+
void QCefBrowserClient::OnBeforeClose(CefRefPtr<CefBrowser>)
217+
{
218+
if (widget) {
219+
emit widget->CloseSafely();
220+
}
221+
}
222+
216223
bool QCefBrowserClient::OnSetFocus(CefRefPtr<CefBrowser>, CefFocusHandler::FocusSource source)
217224
{
218225
/* Don't steal focus when the webpage navigates. This is especially

panel/browser-panel-client.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class QCefBrowserClient : public CefClient,
5757
CefRefPtr<CefClient> &client, CefBrowserSettings &settings,
5858
CefRefPtr<CefDictionaryValue> &extra_info, bool *no_javascript_access) override;
5959

60+
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;
61+
6062
/* CefFocusHandler */
6163
virtual bool OnSetFocus(CefRefPtr<CefBrowser> browser, CefFocusHandler::FocusSource source) override;
6264

panel/browser-panel-internal.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class QCefWidgetInternal : public QCefWidget {
5151
virtual bool zoomPage(int direction) override;
5252
virtual void executeJavaScript(const std::string &script) override;
5353

54+
void CloseSafely();
5455
void Resize();
5556

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

6263
public slots:
6364
void Init();
65+
66+
signals:
67+
void readyToClose();
6468
};

panel/browser-panel.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,24 @@ void QCefWidgetInternal::closeBrowser()
160160
{
161161
CefRefPtr<CefBrowser> browser = cefBrowser;
162162
if (!!browser) {
163-
auto destroyBrowser = [](CefRefPtr<CefBrowser> cefBrowser) {
163+
auto destroyBrowser = [=](CefRefPtr<CefBrowser> cefBrowser) {
164164
CefRefPtr<CefClient> client = cefBrowser->GetHost()->GetClient();
165165
QCefBrowserClient *bc = reinterpret_cast<QCefBrowserClient *>(client.get());
166166

167-
if (bc) {
168-
bc->widget = nullptr;
169-
}
170-
171167
cefBrowser->GetHost()->CloseBrowser(true);
172168

173169
#if !defined(_WIN32) && !defined(__APPLE__) && CHROME_VERSION_BUILD >= 6533
174-
while (cefBrowser && cefBrowser->IsValid()) {
175-
os_sleep_ms(10);
176-
}
170+
QEventLoop loop;
171+
172+
connect(this, &QCefWidgetInternal::readyToClose, &loop, &QEventLoop::quit);
173+
174+
QTimer::singleShot(1000, &loop, &QEventLoop::quit);
175+
176+
loop.exec();
177177
#endif
178+
if (bc) {
179+
bc->widget = nullptr;
180+
}
178181
};
179182

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

404+
void QCefWidgetInternal::CloseSafely()
405+
{
406+
emit readyToClose();
407+
}
408+
401409
void QCefWidgetInternal::showEvent(QShowEvent *event)
402410
{
403411
QWidget::showEvent(event);

0 commit comments

Comments
 (0)