Page 1 of 1

TChromiumWindow.CloseBrowser and onbeforeunload

Posted: Thu May 25, 2023 10:04 pm
by jchoover
https://www.briskbard.com/forum/viewtopic.php?p=156#p156

If I don't care about onbeforeunload, is there any harm in not calling CloseBrowser, and allowing the immediate natural destruction sequence?

TLDR, in my application I have a TFrame based component which hosts the TChromiumWindow. I embed this frame in a modal form for previewing PDF's. If I don't add the CloseBrowser call, I am not noticing any negative impacts using both the debug and release builds of CEF.

In other portions of the application, the same component is used as is (not in a form that we close). When I navigate via an external control to different area's, the container will simply call Object.Free to destroy the old "area browser", to replace that with another component, which may or may not be another TChromiumWindow frame. In this use case, If I do call close browser, then there is a race condition. Because I've been triggered by Free, I would be stuck with the dreaded Application.ProcessMessages.

Re: TChromiumWindow.CloseBrowser and onbeforeunload

Posted: Fri May 26, 2023 10:37 am
by salvadordf
Hi,

Following the destruction steps is the recommended way to destroy a CEF browser.
See the SimpleBrowser demo for all the details.

In the case of TChromiumWindow you need to do this :
  • Implement TForm.OnCloseQuery event, set CanClose to False and call TChromiumWindow.CloseBrowser, which triggers the TChromiumWindow.OnClose event.
  • The TChromiumWindow.OnClose event sets FCanClose to true and sends WM_CLOSE to the form.
If you use TChromiumWindow inside a frame you will have to wait until the TChromiumWindow.OnClose event is triggered before destroying the frame.

Even if simply destroying the browser seems to work sometimes I would not recommend doing that.