How to register custom ClientHandler::Delegate class?

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

How to register custom ClientHandler::Delegate class?

Postby rkcef » Wed Mar 09, 2022 3:27 am

Hi,

I'm building my application on top of the cefclient example project.

I would like to listen to browser events like NotifyBrowserClosing, NotifyBrowserClosed, etc.
I've seen that I need to inherit and implement ClientHandler::Delegate from "client_handler.h" in my own class.

In the header file "client_handler.h" it states:

// Implement this interface to receive notification of ClientHandler
// events. The methods of this class will be called on the main thread unless
// otherwise indicated.


What I don't seem to understand is how/where I can register my class as a new "delegate" so I can receive these events.

Can someone point me in the right direction?

Thanks very much :)
rkcef
Techie
 
Posts: 27
Joined: Fri Feb 26, 2021 9:11 am

Re: How to register custom ClientHandler::Delegate class?

Postby rkcef » Wed Mar 09, 2022 6:26 am

Basically I want to add my own delegate class to the list of browser-event-listeners like it's possible with the message router in ClientHandler:

Code: Select all
void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
  ...

  if (!message_router_)
  {
    // Create the browser-side router for query handling.
    CefMessageRouterConfig config;
    message_router_ = CefMessageRouterBrowserSide::Create(config);

    // Register handlers with the router.
    test_runner::CreateMessageHandlers(message_handler_set_);

    MessageHandlerSet::const_iterator it = message_handler_set_.begin();
    for (; it != message_handler_set_.end(); ++it)
    {
      message_router_->AddHandler(*(it), false);
    }
  }

  ...
}


But it seems like there must be some other kind of mechanism for the browser-event-listeners.

How can I listen to browser events like "NotifyBrowserClosing" in my own delegate class?
rkcef
Techie
 
Posts: 27
Joined: Fri Feb 26, 2021 9:11 am

Re: How to register custom ClientHandler::Delegate class?

Postby magreenblatt » Wed Mar 09, 2022 10:32 am

In cefclient the delegates are created here: https://github.com/chromiumembedded/cef ... ser.cc#L58
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: How to register custom ClientHandler::Delegate class?

Postby rkcef » Thu Mar 10, 2022 2:27 am

magreenblatt wrote:In cefclient the delegates are created here: https://github.com/chromiumembedded/cef ... ser.cc#L58


Thank you for the hint :) This looks promising but I still can't seem to understand how I can react on certain
events like NotifyBrowserClosing() from client::ClientHandler, which then calls

Code: Select all
delegate_->OnBrowserClosed(browser);


Maybe I should rephrase my question:

I want to be able to react on certain browser events like closing the application. It's a single window
application so there will be never more than one browser window (instance).

CefLifeSpanHandler has the method DoClose() which gets fired when the browser is being closed,
which in turn calls the NotifyBrowserClosing() in client::ClientHandler and kicks off the process
of notifying all delegates.

This is where I want my own class to be notified also when the user wants to close the browser window.

Maybe I don't need to implement client::ClientHandler::Delegate but CefLifeSpanHandler::DoClose()
together with client::ClientAppBrowser::Delegate and register my own class in
client::ClientAppBrowser::CreateDelegates() (in cefclient\browser\client_app_delegates_browser.cc) ?

Would this approach work to be able to react on a closing event?
rkcef
Techie
 
Posts: 27
Joined: Fri Feb 26, 2021 9:11 am

Re: How to register custom ClientHandler::Delegate class?

Postby magreenblatt » Thu Mar 10, 2022 11:36 am

What, specifically, are you seeking to do on notification of browser close?
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: How to register custom ClientHandler::Delegate class?

Postby rkcef » Wed May 18, 2022 5:19 am

magreenblatt wrote:What, specifically, are you seeking to do on notification of browser close?


Please excuse my late reply. I had to take care of other things and couldn't come back to my issue earlier.

As I wrote in my initial comment, I'm building an application (Windows only) on top of the cefclient example project. I think this is not only an example but a general starting point for many applications that want to integrate CEF.

To explain why I'm looking for a way to register custom delegates, I'll give you an overview of my project's structure. My project directory looks something like the following (abbreviated):

Code: Select all
my-project/
  - libs/
    - cef_binary_101.0.15+gca159c5+chromium-101.0.4951.54_windows64/
  - src/
    - cefclient/
    - shared/
    - custom_code/
    - cefclient_win.cc


I copied over the directories cefclient/ and shared/ from the libs/cef_binary_101.0.15+gca159c5+chromium-101.0.4951.54_windows64/tests/ directory (the automated builds you can download from https://cef-builds.spotifycdn.com/index.html#windows64) and modified the include statements to reference the local paths. The file cefclient_win.cc was moved from the cefclient/ directory to my src/ directory. This is the entry point for my application. My whole project is configured with CMake.

There are some small modifications in the cefclient/ and shared/ directory I had to do but other than that I try to put all my application specific code in the custom_code/ directory. This way I can relatively easily update the CEF version and update any files that has been changed in cefclient/ and shared/ directory. This approach helps me to keep my project manageable and updateable.

With this in mind I am looking for a way to register a new delegate to receive browser events/notifications. The delegate implementation can go in its own file under custom_code/ and is separated cleanly from the cefclient code.

Maybe the cefclient example project isn't designed in this way.

Would you agree with my approach of using the cefclient example project or would you suggest a different project structure?

I hope my explenations make sense to you.
rkcef
Techie
 
Posts: 27
Joined: Fri Feb 26, 2021 9:11 am

Re: How to register custom ClientHandler::Delegate class?

Postby magreenblatt » Wed May 18, 2022 6:09 am

Using cefclient as the basis for other applications is fine.

In cefclient there is a 1:1 relationship between implementation object and Delegate for the purposes of platform abstraction (e.g. Win, Mac, Linux variants). RootWindowManager owns the RootWindow (implements RootWindow::Delegate), RootWindow owns the BrowserWindow (impements BrowserWindow::Delegate), and BrowserWindow owns the ClientHandler (implements ClientHandler::Delegate).

If you wish to be notified outside of this Delegate ownership pattern then you will need to add your own Observer pattern on one of these classes. For example, you could define a RootWindow::Observer interface, add AddObserver/RemoveObserver methods to RootWindow, and then call those Observer methods from the BrowserWindow::Delegate callbacks implemented by RootWindow (OnBrowserWindowClosing, etc).
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: How to register custom ClientHandler::Delegate class?

Postby rkcef » Wed May 18, 2022 6:33 am

Thanks for your quick reply.

I'll take a look at this structure and try to implement it the way you described it.
rkcef
Techie
 
Posts: 27
Joined: Fri Feb 26, 2021 9:11 am


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 37 guests