How to corectly handle pop-ups without crashing Delphi

Post Reply
Lu53
Posts: 8
Joined: Mon Jan 20, 2020 1:02 pm

How to corectly handle pop-ups without crashing Delphi

Post by Lu53 » Thu Feb 13, 2020 6:42 pm

Dear All,

the website I have to deal with uses pop-ups. In the demos I looked into, pop-ups are simply disabled:

Code: Select all

Procedure TMainForm.Chromium1BeforePopup(....);
  Result := (targetDisposition in [WOD_NEW_FOREGROUND_TAB, WOD_NEW_BACKGROUND_TAB, WOD_NEW_POPUP, WOD_NEW_WINDOW]);
I have no idea what exactly this Result := line does, but, in order to allow pop-ups, I found out that just commenting out the Result := ... line does the job. So now I get pop-ups. Fine!

When the user later on closes a popup, I get a Chromium1Close followed (!!) by a Chromium1BeforeClose. In the demos, these events are handled like this;

Code: Select all

Procedure TMainForm.Chromium1Close(...);
   PostMessage(Handle, CEF_DESTROY, 0, 0);
   aAction := cbaDelay;
 
and 

Procedure TMainForm.Chromium1BeforeClose(...);
     CEFSentinel1.Start;
Unfortunately, this shuts down the whole app.

What I did to help myself was this:

Code: Select all

Procedure TMainForm.Chromium1Close(...);
   If browser=Chromium1.Browser then
    Begin   
     aAction := cbaDelay;
     PostMessage(Handle, CEF_DESTROY, 0, 0);
    End
   Else 
     aAction := cbaClose;
 
and 

Procedure TMainForm.Chromium1BeforeClose(...);
   If browser=Chromium1.Browser then
     CEFSentinel1.Start;
This SEEMS to work in that the pop-ups close and the program continues, but somehow this destabilizes Delphi, in particular in my case, the exception handling mechanism. In any try .. except on E:Exception do ... end following the close, that occurs anywhere in the code, I get an AV when accessing E.

My question is: How do I have to correctly handle pop-ups and not mess the rest of my program?

Thank you very much for any idea on this!

Lu

dilfich
Posts: 144
Joined: Thu Nov 30, 2017 1:17 am

Re: How to corectly handle pop-ups without crashing Delphi

Post by dilfich » Thu Feb 13, 2020 7:08 pm

Code: Select all

  Chromium1.LoadURL(targetUrl);  
  Result := True;
Or is this option not suitable?

Lu53
Posts: 8
Joined: Mon Jan 20, 2020 1:02 pm

Re: How to corectly handle pop-ups without crashing Delphi

Post by Lu53 » Thu Feb 13, 2020 7:42 pm

Thank you for your input, Dilfich!
Unfortunately, I'm not sure to where you are trying to point me to.
Maybe this extra info helps: The website I have to deal with is not under my control, so I have to live with the popups.

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

Re: How to corectly handle pop-ups without crashing Delphi

Post by salvadordf » Fri Feb 14, 2020 7:46 am

Hi,

Most of the demos block popup windows because they show how to do one or two things. Adding popup handling to all of them would complicate the code.

There are 2 ways to handle popup windows :
  • Letting CEF create the new popup windows : This option is used by the MiniBrowser demo.
  • Creating the popup windows from Delphi code : This option is used by the PopupBrowser2 demo.
If you don't need to create Delphi forms for the popups then use the code in the MiniBrowser demo. Notice that many events in that demo call the Chromium1.IsSameBrowser function because using this option for the popup windows will use the same TChromium component for your main browser AND the browser in the new popup windows. This means that all the browser events in the popup windows will trigger TChromium events in your main form too.

If you need absolute control of all the forms created by the browser then you have to use the code in the PopupBrowser2 demo. The challenge with this solution is that you have to create and destroy the popup forms in the main application thread but the TChromium events are executed in a CEF thread. This is the reason why this demo creates a hidden form that will be used as a popup form when CEF needs to show a popup window. All that process has to be protected by critical sections and coordinated with windows messages.

Dilfich mentioned an alternative way to show popup windows. You would only need to block the popup windows and copy their URL to load them with your TChromium components. This is much easier but you will have issues if the web page that created the popup window tries to modify the browser in the popup window and you'll also have issues handling POST requests with this solution.

Lu53
Posts: 8
Joined: Mon Jan 20, 2020 1:02 pm

Re: How to corectly handle pop-ups without crashing Delphi

Post by Lu53 » Fri Feb 14, 2020 12:12 pm

Thank you Salvador for your explanations!
I'll checkk the resources you mentioned.
Regards, Lu

Post Reply