|
10 | 10 |
|
11 | 11 | namespace CefSharp.WinForms.Handler
|
12 | 12 | {
|
| 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 | + |
13 | 32 | /// <summary>
|
14 | 33 | /// Called when the <see cref="ChromiumHostControl"/> has been created.
|
15 | 34 | /// When called you must add the control to it's intended parent
|
@@ -53,6 +72,7 @@ namespace CefSharp.WinForms.Handler
|
53 | 72 | public class LifeSpanHandler : CefSharp.Handler.LifeSpanHandler
|
54 | 73 | {
|
55 | 74 | private readonly Dictionary<int, ParentFormMessageInterceptor> popupParentFormMessageInterceptors = new Dictionary<int, ParentFormMessageInterceptor>();
|
| 75 | + private OnBeforePopupCreatedDelegate onBeforePopupCreated; |
56 | 76 | private OnPopupDestroyedDelegate onPopupDestroyed;
|
57 | 77 | private OnPopupBrowserCreatedDelegate onPopupBrowserCreated;
|
58 | 78 | private OnPopupCreatedDelegate onPopupCreated;
|
@@ -159,8 +179,21 @@ protected override bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser b
|
159 | 179 | {
|
160 | 180 | newBrowser = null;
|
161 | 181 |
|
| 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 | + |
162 | 195 | //No action so we'll go with the default behaviour.
|
163 |
| - if(onPopupCreated == null) |
| 196 | + if (onPopupCreated == null) |
164 | 197 | {
|
165 | 198 | return false;
|
166 | 199 | }
|
@@ -204,13 +237,26 @@ protected override bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser b
|
204 | 237 | return false;
|
205 | 238 | }
|
206 | 239 |
|
| 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 | + |
207 | 253 | /// <summary>
|
208 | 254 | /// The <see cref="OnPopupCreatedDelegate"/> will be called when the<see cref="ChromiumHostControl"/> has been
|
209 | 255 | /// created. When the <see cref="OnPopupCreatedDelegate"/> is called you must add the control to it's intended parent
|
210 | 256 | /// so the <see cref="Control.ClientRectangle"/> can be calculated to set the initial
|
211 | 257 | /// size correctly.
|
212 | 258 | /// </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> |
214 | 260 | /// <returns><see cref="LifeSpanHandler"/> instance allowing you to chain method calls together</returns>
|
215 | 261 | public LifeSpanHandler OnPopupCreated(OnPopupCreatedDelegate onPopupCreated)
|
216 | 262 | {
|
|
0 commit comments