I've build a simple form using CefSharp tabbed in a tabcontrol. Application start with 2 tabpages with 2 urls. One of this site can open new popup in new tabpages, but closing new tabs not kill parent subprocess.
In the main method call
EnablePopup popupok = new EnablePopup();
browser2.LifeSpanHandler = popupok;
popupok.popup += Popupallowed;
In the LifeSpanHander I used
public class EnablePopup : ILifeSpanHandler
{
public event Action<string> popup;
public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
return true;
}
public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
public bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
if (targetUrl.Contains(" my conditions ") == true)
{
if (popup != null)
popup(targetUrl);
}
newBrowser = null;
return true;
}
}
In the handler I use
private void Popupallowed(string obj)
{
Invoke(new Action(() => {
TabPage pop = new TabPage(title);
prtTabControl.TabPages.Add(pop);
ChromiumWebBrowser popup = new ChromiumWebBrowser(obj);
popup.RequestHandler = new DisableNavigate();
popup.MenuHandler = new DisableMenu();
ClosePopup exitpopup = new ClosePopup();
popup.LifeSpanHandler = exitpopup;
exitpopup.close += Leavepop;
popup.Parent = prtTabControl.SelectedTab;
pop.Controls.Add(popup);
popup.Dock = DockStyle.Fill;
popup.Show();
prtTabControl.SelectedTab = pop;
}));
}
Intercept other popup
public class ClosePopup : ILifeSpanHandler
{
public event Action<IBrowser> close; //need to intercept javascript window.close to avoid cef.shutdown and application exit
public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
if (close != null)
close(browser);
return true;
}
public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
public bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
browser.MainFrame.LoadUrl(targetUrl); //allow navigate in the new tabpage
newBrowser = null;
return true;
}
}
If fired window.close Leavepop exit tabpage with
private void Leavepop(IBrowser obj)
{
Invoke(new Action(()=> {
prtTabControl.TabPages.RemoveAt(prtTabControl.SelectedIndex);
prtTabControl.SelectedTab = tabSito2;
}));
}
How I can kill supbrocess with tabpage? Thank you so much.
Found! Tabpages must to be disposed instead be removed for kill BrowserSubProcess.exe!