how can I close beforepopup new tab?

User avatar
Posts: 6
Joined: Mon Dec 14, 2020 8:20 pm

Re: how can I close beforepopup new tab?

Post by crucifyer »

I am making it again with cefsharp, but I think this is a difficult problem.
dcef3 was not properly handled, so I left a new window open and got the request information and reproduced it in a new tab.
cefsharp had to do that too.
I see that all of the projects have problems, so it seems that this project also has problems, so please check it when you have time.
In cef4delphi, new tabs other than onbeforepopup can be freed without events or postmessage, but I think that is normal.
Thank you for making a good project, please keep it for a long time. :D
User avatar
Posts: 2966
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain

Re: how can I close beforepopup new tab?

Post by salvadordf »

Creating new custom tabs or custom popup windows from the TChromium.OnBeforePopup event is never easy with any of the CEF wrappers if you set GlobalCEFApp.MultiThreadedMessageLoop to True, which is the default value and the recommended setting for CEF browsers in Windows.

It would be slightly easier if you use set GlobalCEFApp.ExternalMessagePump to True and GlobalCEFApp.MultiThreadedMessageLoop to False but then you need to use a TCEFWorkScheduler as you can see in the ExternalPumpBrowser demo. With that configuration you should be able to create and destroy all Windows controls like tabs and popup windows inside the TChromium.OnBeforePopup event.
Posts: 1
Joined: Thu Mar 11, 2021 4:27 am

Re: how can I close beforepopup new tab?

Post by edgaringl »

Hi, try open the tab with PostMessage, It worked for me, here part of my code, I take and old demo of tabbedbrowser for the example

declare a constant

Code: Select all

  NEW_TAB                     = WM_APP + $104;
declare the functions

Code: Select all

    { Public declarations }
    procedure newTabMsg(var aMessage : TMessage); message NEW_TAB;
    procedure newTab();
the functions

Code: Select all

procedure TMainForm.newTabMsg(var aMessage : TMessage); //process message

procedure TMainForm.newTab(); // create new tab
  TempSheet        : TTabSheet;
  TempWindowParent : TCEFWindowParent;
  TempChromium     : TChromium;
  ButtonPnl.Enabled    := False;
  PageControl1.Enabled := False;

  TempSheet             := TTabSheet.Create(PageControl1);
  TempSheet.Caption     := 'New tab';
  TempSheet.PageControl := PageControl1;

  TempWindowParent        := TCEFWindowParent.Create(TempSheet);
  TempWindowParent.Parent := TempSheet;
  TempWindowParent.Color  := clWhite;
  TempWindowParent.Align  := alClient;

  TempChromium                   := TChromium.Create(TempSheet);
  TempChromium.OnAfterCreated    := Chromium_OnAfterCreated;
  TempChromium.OnAddressChange   := Chromium_OnAddressChange;
  TempChromium.OnTitleChange     := Chromium_OnTitleChange;
  TempChromium.OnClose           := Chromium_OnClose;
  TempChromium.OnBeforeClose     := Chromium_OnBeforeClose;
  TempChromium.OnBeforePopup     := Chromium_OnBeforePopup;
  TempChromium.OnBeforeDownload  := Chromium_OnBeforeDownload;
  TempChromium.OnDownloadUpdated := Chromium_OnDownloadUpdated;
  TempChromium.CreateBrowser(TempWindowParent, '');
And finally make the call from beforePopup, globalUrl will be used when the browser are ready, so just catch the targetUrl

Code: Select all

procedure TMainForm.Chromium_OnBeforePopup(Sender: TObject;
  const browser: ICefBrowser; const frame: ICefFrame; const targetUrl,
  targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition;
  userGesture: Boolean; const popupFeatures: TCefPopupFeatures;
  var windowInfo: TCefWindowInfo; var client: ICefClient;
  var settings: TCefBrowserSettings; var noJavascriptAccess: Boolean;
  var Result: Boolean);
  PostMessage(MainForm.Handle, NEW_TAB, 0, 0);
  Result := true;
here open url

Code: Select all

procedure TMainForm.BrowserCreatedMsg(var aMessage : TMessage);
  TempWindowParent : TCEFWindowParent;
  TempChromium     : TChromium;
  ButtonPnl.Enabled    := True;
  PageControl1.Enabled := True;

  if SearchWindowParent(aMessage.lParam, TempWindowParent) then

  if SearchChromium(aMessage.lParam, TempChromium) then
    TempChromium.LoadURL( globalUrl );
Now the tab or app can close without errors. Al least not this "System error 5 has occurred. Access is denied."
Post Reply