Skip to content

Commit

Permalink
Fix loading state signals (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
tishion authored Jun 8, 2022
1 parent 88c3468 commit 5d4998d
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CefViewCore
39 changes: 38 additions & 1 deletion example/QCefViewTest/MainWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "MainWindow.h"
#include "MainWindow.h"

#include <QCoreApplication>
#include <QDir>
Expand Down Expand Up @@ -74,6 +74,10 @@ MainWindow::createCefView()
connect(cefViewWidget, &QCefView::draggableRegionChanged, this, &MainWindow::onDraggableRegionChanged);

connect(cefViewWidget, &QCefView::reportJavascriptResult, this, &MainWindow::onJavascriptResult);

connect(cefViewWidget, &QCefView::loadStart, this, &MainWindow::onLoadStart);
connect(cefViewWidget, &QCefView::loadEnd, this, &MainWindow::onLoadEnd);
connect(cefViewWidget, &QCefView::loadError, this, &MainWindow::onLoadError);
//*/
}

Expand Down Expand Up @@ -144,6 +148,39 @@ MainWindow::onJavascriptResult(int browserId, int64_t frameId, int64_t context,
QMessageBox::information(this->window(), title, text);
}

void
MainWindow::onLoadingStateChanged(int browserId, bool isLoading, bool canGoBack, bool canGoForward)
{
qDebug() << "onLoadingStateChanged, browserId:" << browserId << ", isLoading:" << isLoading
<< ", canGoBack:" << canGoBack << ", canGoForward:" << canGoForward;
}

void
MainWindow::onLoadStart(int browserId, qint64 frameId, bool isMainFrame, int transition_type)
{
qDebug() << "onLoadStart, browserId:" << browserId << ", frameId:" << frameId << ", isMainFrame:" << isMainFrame
<< ", transitionType:" << transition_type;
}

void
MainWindow::onLoadEnd(int browserId, qint64 frameId, bool isMainFrame, int httpStatusCode)
{
qDebug() << "onLoadEnd, browserId:" << browserId << ", frameId:" << frameId << ", isMainFrame:" << isMainFrame
<< ", httpStatusCode:" << httpStatusCode;
}

void
MainWindow::onLoadError(int browserId,
qint64 frameId,
bool isMainFrame,
int errorCode,
const QString& errorMsg,
const QString& failedUrl)
{
qDebug() << "onLoadError, browserId:" << browserId << ", frameId:" << frameId << ", isMainFrame:" << isMainFrame
<< ", errorCode:" << errorCode;
}

void
MainWindow::onBtnRecreateClicked()
{
Expand Down
15 changes: 14 additions & 1 deletion example/QCefViewTest/MainWindow.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef QCEFVIEWTEST_H
#ifndef QCEFVIEWTEST_H
#define QCEFVIEWTEST_H

#include <QMainWindow>
Expand Down Expand Up @@ -28,6 +28,19 @@ protected slots:

void onJavascriptResult(int browserId, int64_t frameId, int64_t context, const QVariant& result);

void onLoadingStateChanged(int browserId, bool isLoading, bool canGoBack, bool canGoForward);

void onLoadStart(int browserId, qint64 frameId, bool isMainFrame, int transition_type);

void onLoadEnd(int browserId, qint64 frameId, bool isMainFrame, int httpStatusCode);

void onLoadError(int browserId,
qint64 frameId,
bool isMainFrame,
int errorCode,
const QString& errorMsg,
const QString& failedUrl);

// ui slots
protected slots:
void onBtnRecreateClicked();
Expand Down
18 changes: 8 additions & 10 deletions include/QCefView.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,36 +206,34 @@ class QCEFVIEW_EXPORT QCefView : public QWidget
/// </summary>
/// <param name="browserId">Indicates the browser id</param>
/// <param name="frameId">Indicates the frame id</param>
/// <param name="frameIsMain">Indicates the whether this is the main frame</param>
/// <param name="isMainFrame">Indicates the whether this is the main frame</param>
/// <param name="transition_type">transition type</param>
void loadStart(int browserId, qint64 frameId, bool frameIsMain, int transition_type);
void loadStart(int browserId, qint64 frameId, bool isMainFrame, int transition_type);

/// <summary>
/// Gets called on loading ends
/// </summary>
/// <param name="browserId">Indicates the browser id</param>
/// <param name="frameId">Indicates the frame id</param>
/// <param name="frameIsMain">Indicates the whether this is the main frame</param>
/// <param name="isMainFrame">Indicates the whether this is the main frame</param>
/// <param name="httpStatusCode">The HTTP status code</param>
void loadEnd(int browserId, qint64 frameId, bool frameIsMain, int httpStatusCode);
void loadEnd(int browserId, qint64 frameId, bool isMainFrame, int httpStatusCode);

/// <summary>
/// Gets called on loading failed due to error
/// </summary>
/// <param name="browserId">Indicates the browser id</param>
/// <param name="frameId">Indicates the frame id</param>
/// <param name="frameIsMain">Indicates the whether this is the main frame</param>
/// <param name="isMainFrame">Indicates the whether this is the main frame</param>
/// <param name="errorCode">The error code</param>
/// <param name="errorMsg">The error message</param>
/// <param name="failedUrl">The url caused the failure</param>
/// <param name="handled">Sets this parameter to indicates whether this error was handled or not</param>
void loadError(int browserId,
qint64 frameId,
bool frameIsMain,
bool isMainFrame,
int errorCode,
const QString& errorMsg,
const QString& failedUrl,
bool& handled);
const QString& failedUrl);

/// <summary>
/// Gets called on draggable region changed
Expand Down Expand Up @@ -267,7 +265,7 @@ class QCEFVIEW_EXPORT QCefView : public QWidget
/// Gets called on title changed
/// </summary>
/// <param name="icon">The icon</param>
void faviconChanged(const QIcon &icon);
void faviconChanged(const QIcon& icon);

/// <summary>
/// Gets called on fullscreen mode changed
Expand Down
10 changes: 4 additions & 6 deletions src/details/CCefClientDelegate_LoadHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "CCefClientDelegate.h"
#include "CCefClientDelegate.h"

#include "QCefViewPrivate.h"

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

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

void
Expand All @@ -44,8 +44,6 @@ CCefClientDelegate::loadError(CefRefPtr<CefBrowser>& browser,
if (!IsValidBrowser(browser))
return;

auto msg = QString::fromStdString(errorMsg);
auto url = QString::fromStdString(failedUrl);
pCefViewPrivate_->q_ptr->loadError(
browser->GetIdentifier(), frame->GetIdentifier(), frame->IsMain(), errorCode, msg, url, handled);
handled = pCefViewPrivate_->handleLoadError(browser, frame, errorCode, errorMsg, failedUrl);
return;
}
22 changes: 21 additions & 1 deletion src/details/QCefViewPrivate.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "QCefViewPrivate.h"
#include "QCefViewPrivate.h"

#pragma region std_headers
#include <stdexcept>
Expand Down Expand Up @@ -260,6 +260,26 @@ QCefViewPrivate::setCefWindowFocus(bool focus)
}
}

bool
QCefViewPrivate::handleLoadError(CefRefPtr<CefBrowser>& browser,
CefRefPtr<CefFrame>& frame,
int errorCode,
const std::string& errorMsg,
const std::string& failedUrl)
{
Q_Q(QCefView);

// If the signal was connected then emit the signal and set handled with true to skip the default handler
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(), errorCode, frame->IsMain(), msg, url);
return true;
}

return false;
}

void
QCefViewPrivate::onAppFocusChanged(QWidget* old, QWidget* now)
{
Expand Down
8 changes: 7 additions & 1 deletion src/details/QCefViewPrivate.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#pragma once
#pragma region qt_headers
#include <QMutex>
#include <QSet>
Expand Down Expand Up @@ -146,6 +146,12 @@ class QCefViewPrivate : public QObject

void setCefWindowFocus(bool focus);

bool handleLoadError(CefRefPtr<CefBrowser>& browser,
CefRefPtr<CefFrame>& frame,
int errorCode,
const std::string& errorMsg,
const std::string& failedUrl);

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

Expand Down

0 comments on commit 5d4998d

Please sign in to comment.