Skip to content

Commit 01b7a43

Browse files
committed
WinForms - Add LifeSpanHandler.OnBeforePopupCreated
- Allow for cancelling of popup creation browser.LifeSpanHandler = CefSharp.WinForms.Handler.LifeSpanHandler .Create() .OnBeforePopupCreated((chromiumWebBrowser, b, frame, targetUrl, targetFrameName, targetDisposition, userGesture, browserSettings) => { //Can cancel opening popup based on Url if required. if(targetUrl?.StartsWith(CefExample.BaseUrl + "/cancelme.html") == true) { return PopupCreation.Cancel; } return PopupCreation.Continue; })
1 parent 4ade0b8 commit 01b7a43

File tree

4 files changed

+100
-6
lines changed

4 files changed

+100
-6
lines changed

CefSharp.WinForms.Example/BrowserTabUserControl.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,18 @@ public BrowserTabUserControl(Action<string, int?> openNewTab, string url, bool m
6969
//The CefSharp.WinForms.Handler.LifeSpanHandler implementation
7070
//allows for Popups to be hosted in Controls/Tabs
7171
//This example also demonstrates docking DevTools in a SplitPanel
72-
browser.LifeSpanHandler = LifeSpanHandler
72+
browser.LifeSpanHandler = CefSharp.WinForms.Handler.LifeSpanHandler
7373
.Create()
74+
.OnBeforePopupCreated((chromiumWebBrowser, b, frame, targetUrl, targetFrameName, targetDisposition, userGesture, browserSettings) =>
75+
{
76+
//Can cancel opening popup based on Url if required.
77+
if(targetUrl?.StartsWith(CefExample.BaseUrl + "/cancelme.html") == true)
78+
{
79+
return PopupCreation.Cancel;
80+
}
81+
82+
return PopupCreation.Continue;
83+
})
7484
.OnPopupCreated((ctrl, targetUrl) =>
7585
{
7686
//Don't try using ctrl.FindForm() here as

CefSharp.WinForms/Handler/LifeSpanHandler.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@
1010

1111
namespace CefSharp.WinForms.Handler
1212
{
13+
/// <summary>
14+
/// Called <b>before</b>the popup is created, can be used to cancel popup creation if required
15+
/// or modify <see cref="IBrowserSettings"/>.
16+
/// It's important to note that the methods of this interface are called on a CEF UI thread,
17+
/// which by default is not the same as your application UI thread.
18+
/// </summary>
19+
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
20+
/// <param name="browser">The browser instance that launched this popup.</param>
21+
/// <param name="frame">The HTML frame that launched this popup.</param>
22+
/// <param name="targetUrl">The URL of the popup content. (This may be empty/null)</param>
23+
/// <param name="targetFrameName">The name of the popup. (This may be empty/null)</param>
24+
/// <param name="targetDisposition">The value indicates where the user intended to
25+
/// open the popup (e.g. current tab, new tab, etc)</param>
26+
/// <param name="userGesture">The value will be true if the popup was opened via explicit user gesture
27+
/// (e.g. clicking a link) or false if the popup opened automatically (e.g. via the DomContentLoaded event).</param>
28+
/// <param name="browserSettings">browser settings, defaults to source browsers</param>
29+
/// <returns>To cancel creation of the popup return true otherwise return false.</returns>
30+
public delegate PopupCreation OnBeforePopupCreatedDelegate(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IBrowserSettings browserSettings);
31+
1332
/// <summary>
1433
/// Called when the <see cref="ChromiumHostControl"/> has been created.
1534
/// When called you must add the control to it's intended parent
@@ -53,6 +72,7 @@ namespace CefSharp.WinForms.Handler
5372
public class LifeSpanHandler : CefSharp.Handler.LifeSpanHandler
5473
{
5574
private readonly Dictionary<int, ParentFormMessageInterceptor> popupParentFormMessageInterceptors = new Dictionary<int, ParentFormMessageInterceptor>();
75+
private OnBeforePopupCreatedDelegate onBeforePopupCreated;
5676
private OnPopupDestroyedDelegate onPopupDestroyed;
5777
private OnPopupBrowserCreatedDelegate onPopupBrowserCreated;
5878
private OnPopupCreatedDelegate onPopupCreated;
@@ -159,8 +179,21 @@ protected override bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser b
159179
{
160180
newBrowser = null;
161181

182+
PopupCreation userAction = onBeforePopupCreated?.Invoke(chromiumWebBrowser, browser, frame, targetUrl, targetFrameName, targetDisposition, userGesture, browserSettings) ?? PopupCreation.Continue;
183+
184+
//Cancel popup creation
185+
if(userAction == PopupCreation.Cancel)
186+
{
187+
return true;
188+
}
189+
190+
if(userAction == PopupCreation.ContinueWithJavascriptDisabled)
191+
{
192+
noJavascriptAccess = true;
193+
}
194+
162195
//No action so we'll go with the default behaviour.
163-
if(onPopupCreated == null)
196+
if (onPopupCreated == null)
164197
{
165198
return false;
166199
}
@@ -204,13 +237,26 @@ protected override bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser b
204237
return false;
205238
}
206239

240+
/// <summary>
241+
/// The <see cref="OnBeforePopupCreatedDelegate"/> will be called <b>before</b> the popup has been created and
242+
/// can be used to cancel popup creation if required or modify <see cref="IBrowserSettings"/>.
243+
/// </summary>
244+
/// <param name="onBeforePopupCreated">Action to be invoked before popup is created.</param>
245+
/// <returns><see cref="LifeSpanHandler"/> instance allowing you to chain method calls together</returns>
246+
public LifeSpanHandler OnBeforePopupCreated(OnBeforePopupCreatedDelegate onBeforePopupCreated)
247+
{
248+
this.onBeforePopupCreated = onBeforePopupCreated;
249+
250+
return this;
251+
}
252+
207253
/// <summary>
208254
/// The <see cref="OnPopupCreatedDelegate"/> will be called when the<see cref="ChromiumHostControl"/> has been
209255
/// created. When the <see cref="OnPopupCreatedDelegate"/> is called you must add the control to it's intended parent
210256
/// so the <see cref="Control.ClientRectangle"/> can be calculated to set the initial
211257
/// size correctly.
212258
/// </summary>
213-
/// <param name="onPopupCreated">Action to be invoked when the Popup host has been created and is ready to be attached to it's parent..</param>
259+
/// <param name="onPopupCreated">Action to be invoked when the Popup host has been created and is ready to be attached to it's parent.</param>
214260
/// <returns><see cref="LifeSpanHandler"/> instance allowing you to chain method calls together</returns>
215261
public LifeSpanHandler OnPopupCreated(OnPopupCreatedDelegate onPopupCreated)
216262
{

CefSharp.WinForms/Handler/LifeSpanHandlerBuilder.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,27 @@ public LifeSpanHandlerBuilder(CreatePopupChromiumHostControl chromiumHostControl
1616
handler = new LifeSpanHandler(chromiumHostControlCreatedDelegate);
1717
}
1818

19+
/// <summary>
20+
/// The <see cref="OnBeforePopupCreatedDelegate"/> will be called <b>before</b> the popup has been created and
21+
/// can be used to cancel popup creation if required, modify <see cref="IBrowserSettings"/> and disable javascript.
22+
/// </summary>
23+
/// <param name="onBeforePopupCreated">Action to be invoked before popup is created.</param>
24+
/// <returns><see cref="LifeSpanHandlerBuilder"/> instance allowing you to chain method calls together</returns>
25+
public LifeSpanHandlerBuilder OnBeforePopupCreated(OnBeforePopupCreatedDelegate onBeforePopupCreated)
26+
{
27+
handler.OnBeforePopupCreated(onBeforePopupCreated);
28+
29+
return this;
30+
}
31+
1932
/// <summary>
2033
/// The <see cref="OnPopupCreatedDelegate"/> will be called when the<see cref="ChromiumHostControl"/> has been
2134
/// created. When the <see cref="OnPopupCreatedDelegate"/> is called you must add the control to it's intended parent
2235
/// so the <see cref="Control.ClientRectangle"/> can be calculated to set the initial
2336
/// size correctly.
2437
/// </summary>
2538
/// <param name="onPopupCreated">Action to be invoked when the Popup is to be destroyed.</param>
26-
/// <returns><see cref="LifeSpanHandler"/> instance allowing you to chain method calls together</returns>
39+
/// <returns><see cref="LifeSpanHandlerBuilder"/> instance allowing you to chain method calls together</returns>
2740
public LifeSpanHandlerBuilder OnPopupCreated(OnPopupCreatedDelegate onPopupCreated)
2841
{
2942
handler.OnPopupCreated(onPopupCreated);
@@ -38,7 +51,7 @@ public LifeSpanHandlerBuilder OnPopupCreated(OnPopupCreatedDelegate onPopupCreat
3851
/// perform navigation (via frame) etc.
3952
/// </summary>
4053
/// <param name="onPopupBrowserCreated">Action to be invoked when the <see cref="IBrowser"/> has been created.</param>
41-
/// <returns><see cref="LifeSpanHandler"/> instance allowing you to chain method calls together</returns>
54+
/// <returns><see cref="LifeSpanHandlerBuilder"/> instance allowing you to chain method calls together</returns>
4255
public LifeSpanHandlerBuilder OnPopupBrowserCreated(OnPopupBrowserCreatedDelegate onPopupBrowserCreated)
4356
{
4457
handler.OnPopupBrowserCreated(onPopupBrowserCreated);
@@ -52,7 +65,7 @@ public LifeSpanHandlerBuilder OnPopupBrowserCreated(OnPopupBrowserCreatedDelegat
5265
/// When the <see cref="OnPopupDestroyedDelegate"/> is called you must remove/dispose of the <see cref="ChromiumHostControl"/>.
5366
/// </summary>
5467
/// <param name="onPopupDestroyed">Action to be invoked when the Popup is to be destroyed.</param>
55-
/// <returns><see cref="LifeSpanHandler"/> instance allowing you to chain method calls together</returns>
68+
/// <returns><see cref="LifeSpanHandlerBuilder"/> instance allowing you to chain method calls together</returns>
5669
public LifeSpanHandlerBuilder OnPopupDestroyed(OnPopupDestroyedDelegate onPopupDestroyed)
5770
{
5871
handler.OnPopupDestroyed(onPopupDestroyed);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright © 2022 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
namespace CefSharp.WinForms.Handler
6+
{
7+
/// <summary>
8+
/// Popup Creation options
9+
/// </summary>
10+
public enum PopupCreation
11+
{
12+
/// <summary>
13+
/// Popup creation is cancled, no further action will occur
14+
/// </summary>
15+
Cancel = 0,
16+
/// <summary>
17+
/// Popup creation will continue as per normal.
18+
/// </summary>
19+
Continue,
20+
/// <summary>
21+
/// Popup creation will continue with javascript disabled.
22+
/// </summary>
23+
ContinueWithJavascriptDisabled
24+
}
25+
}

0 commit comments

Comments
 (0)