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.

CloseBrowser safely

User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CloseBrowser safely

Post by salvadordf »

If you need several browsers create an array of TDTBrowser and destroy all of them.
If you create browsers with a reference count then it's possible that only the first one will be properly destroyed.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CloseBrowser safely

Post by salvadordf »

Winexcel wrote: Sat Feb 17, 2018 2:57 pm
If you create browsers with a reference count then it's possible that only the first one will be properly destroyed.
FBrowserRefCount is a variable which show how much popup Browsers this browser have.
When popup Browser is closing it invoke TChromium.Browser.CloseBrowser(True) of Main Browser (which created this popup), after that main browser check FBrowserRefCount and if FBrowserRefCount = 0 then it will be destroyed.

I'm developing this mechanism in order to work easy with popup windows and close main browser correctly(when all popup which was created this main browser were closed)

I'm interested, Is it possible?
To be honest, I'm not sure. You're using a custom browser class with a nil owner and that means that you must destroy all the browsers manually.
Using this reference count destruction you would always need to destroy the popups browsers first and then the main browser.
Winexcel wrote: Sat Feb 17, 2018 2:57 pmTo call TChromium.Browser.Host.CloseBrowser(True) more than one time?
Each browser can call CloseBrowser only one time.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CloseBrowser safely

Post by salvadordf »

Winexcel wrote: Sat Feb 17, 2018 4:17 pm In method doOnClose i call inherited method only one time(when the FBrowserRefCount = 0), but the method
TChromium.Browser.Host.CloseBrowser(True) is triggered method doOnClose many times which may called by popup browsers. It seems to work but I'm not sure that it's a right solution.
When you let CEF create the popups it creates new ICefBrowser instances but CEF uses the same ICefClient and the same TCefBrowserSettings for the new ICefBrowser.

ICefClient is responsible for calling the doOnClose method and all other "doOn..." methods in TChromium. This means that the popup browsers will also call the "doOn..." methods in the main TChromium. In your case, when CEF destroys a popup browser it will call "popupbrowser.host.closebrowser" and this will call the doOnClose method in your TChromium because it has the same ICefClient.

You must return the default values in doOnClose and all other "doOn..." methods if the "browser" parameter is a popup.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CloseBrowser safely

Post by salvadordf »

Please check that CreateClientHandler returns True or you will be using the default ICefClient, which is the ICefClient from the main form.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CloseBrowser safely

Post by salvadordf »

I had some difficulties testing "testpopup.rar" and I couldn't make it show a browser.

Anyway, this is a very complicated issue that needs a new demo so I just uploaded the "PopupBrowser" demo showing all the steps to handle popup windows with custom forms.

Please, read the code comments in the *.pas files.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CloseBrowser safely

Post by salvadordf »

Winexcel wrote: Mon Feb 19, 2018 2:21 pmWhat couldn't you do?
I clicked the "CreateBrowser" button and then the "Load" button but I couldn't see a browser window to click the "Sign in via FaceBook" button.
Winexcel wrote: Mon Feb 19, 2018 2:21 pm I had rewrote your example on FMX, the example is based on TDTBrowser, I think that it is really more simple to realise counting popup browsers like I did with the variable FBrowsersCount. I'll post my example later, it isn't clean at the moment.
All bug reports, bug fixes and optimizations are welcome! :D
Winexcel wrote: Mon Feb 19, 2018 2:21 pm I don't know why, but sometimes my solution on FMX crashes in closing time, but It helps:

Code: Select all

LoadURL('about:blank');
I invoke it before CloseBrowser(True).
Loading "about:blank" is one of the steps to close TWebBrowser but CEF3 has a different destruction sequence.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CloseBrowser safely

Post by salvadordf »

Thanks for the demo! :D
I'll take a look as soon as I can.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CloseBrowser safely

Post by salvadordf »

Sorry for the delay.

I took a look at PopupFMX and it seems to destroy the browsers correctly.
To be honest, I'm not sure why it crashes when you don't load "about:blank". :?

Perhaps CEF3 doesn't support "WindowInfoAsClipWindow" or there's some kind of problem using TThread.Synchronize.

Sorry but I don't know the cause of this problem.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CloseBrowser safely

Post by salvadordf »

Feel free to attach it if you want to share your work with the rest of us.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CloseBrowser safely

Post by salvadordf »

Check that there are no blocked threads in the "Thread Status" tab when you debug your app in Delphi and close a popup browser.
Click on "Program Pause" in the "Run" menu to see what threads are blocked.

Also check that the BrowserOnBeforeClose function only destroys the popup browser.
Post Reply