Skip to content

Commit

Permalink
Better close browsers #41
Browse files Browse the repository at this point in the history
  • Loading branch information
Lecrapouille committed Dec 2, 2024
1 parent 16991f3 commit 6581391
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 deletions.
2 changes: 1 addition & 1 deletion addons/gdcef/gdcef/src/gdbrowser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class GDBrowserView: public godot::Node
// ---------------------------------------------------------------------
//! \brief Destructor
// ---------------------------------------------------------------------
~Impl();
virtual ~Impl();

private: // CefClient::CefBaseRefCounted interfaces

Expand Down
85 changes: 70 additions & 15 deletions addons/gdcef/gdcef/src/gdcef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@
#include "helper_config.hpp"
#include "helper_files.hpp"

// Godot 4
#include <gdextension_interface.h>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/godot.hpp>

// Chromium Embedded Framework
#include "base/cef_callback.h"
#include "wrapper/cef_closure_task.h"

//------------------------------------------------------------------------------
// List of file libraries and artifacts mandatory to make CEF working
#if defined(_WIN32)
Expand Down Expand Up @@ -127,9 +132,34 @@ void GDCef::_bind_methods()
}

//------------------------------------------------------------------------------
// Replaced by GDCef::initialize(xxx)
void GDCef::_init()
{
// Replaced by GDCef::initialize(xxx)
GDCEF_DEBUG("");
}

//------------------------------------------------------------------------------
void GDCef::_exit_tree()
{
GDCEF_DEBUG("");
shutdown();
}

//------------------------------------------------------------------------------
void GDCef::shutdown()
{
GDCEF_DEBUG("");

if (m_impl != nullptr)
{
GDCEF_DEBUG("Closing all browsers");
m_impl->closeAllBrowsers(true);

m_impl = nullptr;

GDCEF_DEBUG("CefQuitMessageLoop");
CefQuitMessageLoop();
}
}

//------------------------------------------------------------------------------
Expand All @@ -141,6 +171,7 @@ bool GDCef::initialize(godot::Dictionary config)
return false;
}
m_impl = new GDCef::Impl(*this);
assert((m_impl != nullptr) && "Failed allocating GDCef");

// Folder path in which your application and CEF artifacts are present.
fs::path cef_folder_path;
Expand Down Expand Up @@ -485,18 +516,10 @@ static void configureBrowser(CefBrowserSettings& browser_settings,
//------------------------------------------------------------------------------
GDCef::~GDCef()
{
shutdown();
}

//------------------------------------------------------------------------------
void GDCef::shutdown()
{
GDCEF_DEBUG("");
if (m_impl != nullptr)
{
CefQuitMessageLoop();
shutdown();
}
m_impl = nullptr;
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -584,16 +607,48 @@ void GDCef::Impl::OnBeforeClose(CefRefPtr<CefBrowser> browser)
while (i--)
{
godot::Node* node = m_owner.get_child(i);
GDBrowserView* b = reinterpret_cast<GDBrowserView*>(node);
if ((b != nullptr) && (b->id() == browser->GetIdentifier()))
GDBrowserView* bv = reinterpret_cast<GDBrowserView*>(node);
if ((bv != nullptr) && (bv->id() == browser->GetIdentifier()))
{
GDCEF_DEBUG("Removed browser ID " << b->id());
m_owner.remove_child(node);
node->queue_free();
GDCEF_DEBUG("Removing browser ID " << bv->id());
bv->close(/*force_close*/);
}
m_owner.remove_child(node);
node->queue_free();
}
}

//------------------------------------------------------------------------------
void GDCef::Impl::closeAllBrowsers(bool force_close)
{
if (!CefCurrentlyOn(TID_UI))
{
// Execute on the UI thread.
CefPostTask(
TID_UI,
base::BindOnce(&GDCef::Impl::closeAllBrowsers, this, force_close));
return;
}

// Close all browsers (stored as Godot child nodes).
int64_t i = m_owner.get_child_count();
GDCEF_DEBUG("Removing " << i << " browsers as Godot child nodes");
while (i--)
{
godot::Node* node = m_owner.get_child(i);
GDBrowserView* browser = reinterpret_cast<GDBrowserView*>(node);
if (browser != nullptr)
{
GDCEF_DEBUG("Removing browser ID " << browser->id());
browser->close(/*force_close*/);
}
m_owner.remove_child(node);
node->queue_free();
}

GDCEF_DEBUG("Remaining " << m_owner.get_child_count() << " browser nodes");
}

//------------------------------------------------------------------------------
void GDCef::Impl::OnBeforeCommandLineProcessing(
const CefString& ProcessType,
Expand Down
7 changes: 7 additions & 0 deletions addons/gdcef/gdcef/src/gdcef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class GDCef: public godot::Node
// -------------------------------------------------------------------------
void _init();

void _exit_tree() override;

// -------------------------------------------------------------------------
//! \brief Called to initialize CEF with runtime settings.
//! \note Since _init() does not accept parameters, you have to call this
Expand Down Expand Up @@ -163,6 +165,11 @@ class GDCef: public godot::Node
CefShutdown();
}

// -------------------------------------------------------------------------
//! \brief Close all browsers.
// -------------------------------------------------------------------------
void closeAllBrowsers(bool force_close);

private: // CefClient::CefBaseRefCounted interfaces.

// ---------------------------------------------------------------------
Expand Down

0 comments on commit 6581391

Please sign in to comment.