Skip to content

Commit

Permalink
Support close from web
Browse files Browse the repository at this point in the history
  • Loading branch information
tishion committed Oct 1, 2023
1 parent 37f6ae5 commit b86fece
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 40 deletions.
16 changes: 0 additions & 16 deletions example/QCefViewTest/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,6 @@ MainWindow::onBtnNewBrowserClicked()
w->show();
}

void
MainWindow::closeEvent(QCloseEvent* event)
{
if (m_pLeftCefViewWidget) {
m_pLeftCefViewWidget->deleteLater();
m_pLeftCefViewWidget = nullptr;
}

if (m_pRightCefViewWidget) {
m_pRightCefViewWidget->deleteLater();
m_pRightCefViewWidget = nullptr;
}

event->accept();
}

#ifndef Q_OS_MACOS
void
MainWindow::setupWindow()
Expand Down
3 changes: 0 additions & 3 deletions example/QCefViewTest/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ protected slots:

void onBtnNewBrowserClicked();

private:
void closeEvent(QCloseEvent* event) override;

private:
Ui::MainWindow m_ui;

Expand Down
4 changes: 4 additions & 0 deletions example/QCefViewTest/index.in.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ <h1>Dragging area</h1>
<br />
<a href="#" onClick="window.open('#','QCefView Popup','width=800, height=600'); return false;">Popup Browser By Script</a>

<br />
<a href="#" onClick="window.close();">Close Current Window</a>


<p>An iframe with default borders:</p>
<iframe src="/tutorial.html" width="100%" height="300"> </iframe>
</div>
Expand Down
6 changes: 6 additions & 0 deletions include/QCefView.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,12 @@ class QCEFVIEW_EXPORT QCefView : public QWidget
/// <param name="item">The download item</param>
virtual void onUpdateDownloadItem(const QSharedPointer<QCefDownloadItem>& item);

/// <summary>
/// Gets called on close request from web
/// </summary>
/// <returns>True to allow the close, false to cancel the close</returns>
virtual bool onRequestCloseFromWeb();

#pragma region QWidget
public slots:
/// <summary>
Expand Down
13 changes: 10 additions & 3 deletions src/QCefView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ QCefView::QCefView(const QString& url,
: QWidget(parent, f)
, d_ptr(new QCefViewPrivate(QCefContext::instance()->d_func(), this, url, setting))
{

if (d_ptr->isOSRModeEnabled()) {
// OSR mode
setBackgroundRole(QPalette::Window);
Expand Down Expand Up @@ -318,12 +318,20 @@ QCefView::onUpdateDownloadItem(const QSharedPointer<QCefDownloadItem>& item)
{
}

bool
QCefView::onRequestCloseFromWeb()
{
// delete self
deleteLater();

return true;
}

QVariant
QCefView::inputMethodQuery(Qt::InputMethodQuery query) const
{
Q_D(const QCefView);


if (d->isOSRModeEnabled()) {
// OSR mode
auto r = d->onViewInputMethodQuery(query);
Expand All @@ -347,7 +355,6 @@ QCefView::paintEvent(QPaintEvent* event)
// for NCW mode, this makes sure QCefView will not be treated as transparent background
painter.fillRect(rect(), palette().color(backgroundRole()));


if (d->isOSRModeEnabled()) {
// OSR mode
// 3. paint widget with its stylesheet
Expand Down
6 changes: 3 additions & 3 deletions src/details/CCefClientDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ CCefClientDelegate::processQueryRequest(CefRefPtr<CefBrowser>& browser,

auto browserId = browser->GetIdentifier();
auto req = QString::fromStdString(request);
pCefViewPrivate_->q_ptr->cefQueryRequest(browserId, frameId, QCefQuery(req, query_id));
emit pCefViewPrivate_->q_ptr->cefQueryRequest(browserId, frameId, QCefQuery(req, query_id));
}

void
Expand Down Expand Up @@ -72,7 +72,7 @@ CCefClientDelegate::invokeMethodNotify(CefRefPtr<CefBrowser>& browser,
}

auto browserId = browser->GetIdentifier();
pCefViewPrivate_->q_ptr->invokeMethod(browserId, frameId, m, args);
emit pCefViewPrivate_->q_ptr->invokeMethod(browserId, frameId, m, args);
}

void
Expand All @@ -88,5 +88,5 @@ CCefClientDelegate::reportJSResult(CefRefPtr<CefBrowser>& browser,
QVariant qV;
ValueConvertor::CefValueToQVariant(&qV, result.get());
auto c = QString::fromStdString(context);
pCefViewPrivate_->q_ptr->reportJavascriptResult(browserId, frameId, c, qV);
emit pCefViewPrivate_->q_ptr->reportJavascriptResult(browserId, frameId, c, qV);
}
1 change: 1 addition & 0 deletions src/details/CCefClientDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class CCefClientDelegate
bool& disableJavascriptAccess) override;
virtual void onAfterCreate(CefRefPtr<CefBrowser>& browser) override;
virtual bool doClose(CefRefPtr<CefBrowser> browser) override;
virtual bool requestClose(CefRefPtr<CefBrowser> browser) override;
virtual void onBeforeClose(CefRefPtr<CefBrowser> browser) override;

// LoadHandler
Expand Down
14 changes: 7 additions & 7 deletions src/details/CCefClientDelegate_DisplayHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ CCefClientDelegate::addressChanged(CefRefPtr<CefBrowser>& browser, int64_t frame
return;

auto u = QString::fromStdString(url);
pCefViewPrivate_->q_ptr->addressChanged(frameId, u);
emit pCefViewPrivate_->q_ptr->addressChanged(frameId, u);
}

void
Expand All @@ -82,7 +82,7 @@ CCefClientDelegate::titleChanged(CefRefPtr<CefBrowser>& browser, const std::stri
return;

auto t = QString::fromStdString(title);
pCefViewPrivate_->q_ptr->titleChanged(t);
emit pCefViewPrivate_->q_ptr->titleChanged(t);
}

void
Expand All @@ -96,7 +96,7 @@ CCefClientDelegate::faviconURLChanged(CefRefPtr<CefBrowser> browser, const std::
urls.append(QString::fromStdString(iconUrl.ToString()));
}

pCefViewPrivate_->q_ptr->faviconURLChanged(urls);
emit pCefViewPrivate_->q_ptr->faviconURLChanged(urls);
}

void
Expand All @@ -105,7 +105,7 @@ CCefClientDelegate::fullscreenModeChanged(CefRefPtr<CefBrowser>& browser, bool f
if (!IsValidBrowser(browser))
return;

pCefViewPrivate_->q_ptr->fullscreenModeChanged(fullscreen);
emit pCefViewPrivate_->q_ptr->fullscreenModeChanged(fullscreen);
}

bool
Expand All @@ -122,7 +122,7 @@ CCefClientDelegate::statusMessage(CefRefPtr<CefBrowser>& browser, const std::str
return;

auto msg = QString::fromStdString(value);
pCefViewPrivate_->q_ptr->statusMessage(msg);
emit pCefViewPrivate_->q_ptr->statusMessage(msg);
}

void
Expand All @@ -132,7 +132,7 @@ CCefClientDelegate::consoleMessage(CefRefPtr<CefBrowser>& browser, const std::st
return;

auto msg = QString::fromStdString(message);
pCefViewPrivate_->q_ptr->consoleMessage(msg, level);
emit pCefViewPrivate_->q_ptr->consoleMessage(msg, level);
}

void
Expand All @@ -141,7 +141,7 @@ CCefClientDelegate::loadingProgressChanged(CefRefPtr<CefBrowser>& browser, doubl
if (!IsValidBrowser(browser))
return;

pCefViewPrivate_->q_ptr->loadingProgressChanged(progress);
emit pCefViewPrivate_->q_ptr->loadingProgressChanged(progress);
}

bool
Expand Down
2 changes: 1 addition & 1 deletion src/details/CCefClientDelegate_DragHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ CCefClientDelegate::draggableRegionChanged(CefRefPtr<CefBrowser>& browser,
}
}

pCefViewPrivate_->q_ptr->draggableRegionChanged(draggableRegion, nonDraggableRegion);
emit pCefViewPrivate_->q_ptr->draggableRegionChanged(draggableRegion, nonDraggableRegion);
}
26 changes: 26 additions & 0 deletions src/details/CCefClientDelegate_LifeSpanHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "CCefClientDelegate.h"

#include <QThread>
#include <QDebug>

#include "QCefSettingPrivate.h"
#include "QCefViewPrivate.h"
Expand Down Expand Up @@ -40,6 +41,8 @@ CCefClientDelegate::onBeforePopup(CefRefPtr<CefBrowser>& browser,
QCefSettingPrivate::CopyFromCefBrowserSettings(&s, &settings);

if (targetDisposition == CefLifeSpanHandler::WindowOpenDisposition::WOD_NEW_POPUP) {
// the new browser was created from javascript, we need to conform the CEF pop-up browser lifecycle
// because CEF need to return the new browser identity to javascript context
Qt::ConnectionType c = pCefViewPrivate_->q_ptr->thread() == QThread::currentThread() ? Qt::DirectConnection
: Qt::BlockingQueuedConnection;

Expand All @@ -61,6 +64,7 @@ CCefClientDelegate::onBeforePopup(CefRefPtr<CefBrowser>& browser,
},
c);
} else {
// the new browser was created from non-javascript, we create a new browser instead
cancel = true;
QMetaObject::invokeMethod(
pCefViewPrivate_,
Expand Down Expand Up @@ -122,9 +126,31 @@ CCefClientDelegate::onAfterCreate(CefRefPtr<CefBrowser>& browser)
bool
CCefClientDelegate::doClose(CefRefPtr<CefBrowser> browser)
{
qDebug() << "destroy browser from native";

return false;
}

bool
CCefClientDelegate::requestClose(CefRefPtr<CefBrowser> browser)
{
qDebug() << "destroy browser request from web";

Qt::ConnectionType c =
pCefViewPrivate_->q_ptr->thread() == QThread::currentThread() ? Qt::DirectConnection : Qt::BlockingQueuedConnection;

bool ignoreClose = false;
QMetaObject::invokeMethod(
pCefViewPrivate_,
[&]() {
//
ignoreClose = !(pCefViewPrivate_->requestCloseFromWeb(browser));
},
c);

return ignoreClose;
}

void
CCefClientDelegate::onBeforeClose(CefRefPtr<CefBrowser> browser)
{
Expand Down
6 changes: 3 additions & 3 deletions src/details/CCefClientDelegate_LoadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CCefClientDelegate::loadingStateChanged(CefRefPtr<CefBrowser>& browser,
if (!IsValidBrowser(browser))
return;

pCefViewPrivate_->q_ptr->loadingStateChanged(browser->GetIdentifier(), isLoading, canGoBack, canGoForward);
emit pCefViewPrivate_->q_ptr->loadingStateChanged(browser->GetIdentifier(), isLoading, canGoBack, canGoForward);
}

void
Expand All @@ -20,7 +20,7 @@ CCefClientDelegate::loadStart(CefRefPtr<CefBrowser>& browser, CefRefPtr<CefFrame
if (!IsValidBrowser(browser))
return;

pCefViewPrivate_->q_ptr->loadStart(
emit pCefViewPrivate_->q_ptr->loadStart(
browser->GetIdentifier(), frame->GetIdentifier(), frame->IsMain(), transitionType);
}

Expand All @@ -30,7 +30,7 @@ CCefClientDelegate::loadEnd(CefRefPtr<CefBrowser>& browser, CefRefPtr<CefFrame>&
if (!IsValidBrowser(browser))
return;

pCefViewPrivate_->q_ptr->loadEnd(browser->GetIdentifier(), frame->GetIdentifier(), frame->IsMain(), httpStatusCode);
emit pCefViewPrivate_->q_ptr->loadEnd(browser->GetIdentifier(), frame->GetIdentifier(), frame->IsMain(), httpStatusCode);
}

void
Expand Down
16 changes: 13 additions & 3 deletions src/details/QCefViewPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ QCefViewPrivate::createCefBrowser(QCefView* view, const QString& url, const QCef
void
QCefViewPrivate::destroyCefBrowser()
{
qDebug() << "destroy browser from native";

if (!pClient_)
return;

Expand Down Expand Up @@ -226,7 +228,7 @@ QCefViewPrivate::onCefBrowserCreated(CefRefPtr<CefBrowser> browser, QWindow* win
browser->GetHost()->CloseBrowser(true);
return;
}

// adjust size/mask and attach to cef window
ncw.qBrowserWindow_->applyMask(q_ptr->mask());

Expand Down Expand Up @@ -323,13 +325,21 @@ QCefViewPrivate::handleLoadError(CefRefPtr<CefBrowser>& browser,
if (q->receivers(SIGNAL(loadError(int, qint64, bool, int, const QString&, const QString&))) > 0) {
auto msg = QString::fromStdString(errorMsg);
auto url = QString::fromStdString(failedUrl);
q->loadError(browser->GetIdentifier(), frame->GetIdentifier(), frame->IsMain(), errorCode, msg, url);
emit q->loadError(browser->GetIdentifier(), frame->GetIdentifier(), frame->IsMain(), errorCode, msg, url);
return true;
}

return false;
}

bool
QCefViewPrivate::requestCloseFromWeb(CefRefPtr<CefBrowser>& browser)
{
Q_Q(QCefView);

return q->onRequestCloseFromWeb();
}

void
QCefViewPrivate::onAppFocusChanged(QWidget* old, QWidget* now)
{
Expand Down Expand Up @@ -918,7 +928,7 @@ QCefViewPrivate::onViewWheelEvent(QWheelEvent* event)

// angleDelta().y() provides the angle through which the common vertical mouse wheel was rotated since the previous
// event. angleDelta().x() provides the angle through which the horizontal mouse wheel was rotated, if the mouse has
// a horizontal wheel; otherwise it stays at zero.
// a horizontal wheel; otherwise it stays at zero.
pCefBrowser_->GetHost()->SendMouseWheelEvent(
e, m & Qt::ShiftModifier ? d.x() : 0, m & Qt::ShiftModifier ? d.y() : d.y());
}
Expand Down
2 changes: 2 additions & 0 deletions src/details/QCefViewPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ class QCefViewPrivate : public QObject
const std::string& errorMsg,
const std::string& failedUrl);

bool requestCloseFromWeb(CefRefPtr<CefBrowser>& browser);

public slots:
void onAppFocusChanged(QWidget* old, QWidget* now);

Expand Down

0 comments on commit b86fece

Please sign in to comment.