Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.
I'm trying to understand the logic of the shutdown sequence. From the SimpleFMXBrowser example:
1. FormCloseQuery sets CanClose to FALSE calls TFMXChromium.CloseBrowser which triggers the TFMXChromium.OnClose event.
This implies that we have to close the browser before the form is closed.
2. TFMXChromium.OnClose sends a CEFBROWSER_DESTROY message to destroy CEFWindowParent1 in the main thread, which triggers the TFMXChromium.OnBeforeClose event.
3. TFMXChromium.OnBeforeClose sets FCanClose := True and sends WM_CLOSE to the form.
Given that the event handler is OnBeforeClose, step 3 implies that we have to close the form before the browser.
The names can be misleading but the sequence is this in CEF4Delphi :
1. Call TChromium.CloseBrowser
2. Event TChromium.OnClose is triggered
3. Event TChromium.OnBeforeClose is triggered
4. The application can be closed now
These are the original CEF code comments for the OnClose event when you need user confirmation before closing the application :
1. User clicks the window close button which sends a close notification to the application's top-level window.
2. Application's top-level window receives the close notification and:
A. Calls CefBrowserHost::CloseBrowser(false).
B. Cancels the window close.
3. JavaScript 'onbeforeunload' handler executes and shows the close confirmation dialog (which can be overridden via CefJSDialogHandler::OnBeforeUnloadDialog()).
4. User approves the close.
5. JavaScript 'onunload' handler executes.
6. Application's do_close() handler is called. Application will:
A. Set a flag to indicate that the next close attempt will be allowed.
B. Return false.
7. CEF sends an close notification to the application's top-level window.
8. Application's top-level window receives the close notification and allows the window to close based on the flag from #6B.
9. Application's top-level window is destroyed.
10. Application's on_before_close() handler is called and the browser object is destroyed.
11. Application exits by calling cef_quit_message_loop() if no other browsers exist.
The names change a little bit between C and Delphi but the logic is the same
"CefBrowserHost::CloseBrowser(false)" is equivalent to TChromium.CloseBrowser in Delphi
The "do_close() handler" is the TChromium.OnClose event
The "on_before_close() handler" is the TChromium.OnBeforeClose event
"cef_quit_message_loop()" is part of the GlobalCEFApp destruction.
It's necessary to follow these steps to close all the browsers before closing the application.