Before popup event in WPF

222 views
Skip to first unread message

Sai Prasad

unread,
Oct 19, 2016, 9:08:30 AM10/19/16
to CefGlue
Dear Dmitry:

Am using xilium.cefglue of 2623 branch, do we have an any similar option in cefglue for beforepopup event in WPF like windows forms with sender and BeforePopupEventArgs as an argument.

or do we have an option to identify the popup and to block the popup in wpf.


Regards,
Sai Prasad

Dmitry Azaraev

unread,
Oct 19, 2016, 11:21:41 AM10/19/16
to CefGlue
Feel free to add any required events. WPF control is just sample CEF client, which can/should handle required events. So you need expose it by handling CefLifeSpanHandler::OnBeforePopup.

--
You received this message because you are subscribed to the Google Groups "CefGlue" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cefglue+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sai Prasad

unread,
Nov 28, 2016, 11:40:23 PM11/28/16
to CefGlue
Thanks Dmitry will try with your suggestion.

Megha CS

unread,
Jan 23, 2017, 3:53:47 AM1/23/17
to CefGlue, dmitry....@gmail.com
Hi Dmitry,

       Am using xilium.cefglue of 2623 branch, we have created MyCefLifeSpanHandler class to override OnBeforePopup method of CefLifeSpanHandler in WPF to block the popup.
       But event is not triggering.

public partial class WpfBrowser : UserControl
    {
         string url1 = string.Empty;
        void WpfBrowser_Loaded(object sender, RoutedEventArgs e)
        {
            browser.Focus();
        }

        public WpfBrowser(string url)
        {
            InitializeComponent();
            MyCefLifeSpanHandler handler=new MyCefLifeSpanHandler(this);
            browser.StartUrl=url;
            this.Loaded += WpfBrowser_Loaded;
        }       


       
       public class MyCefLifeSpanHandler : CefLifeSpanHandler
    {
        private readonly WpfBrowser _browser;


        public MyCefLifeSpanHandler (WpfBrowser browser)
        {
            _browser = browser;
          
        }
        protected override bool OnBeforePopup(CefBrowser browser, CefFrame frame, string targetUrl, string targetFrameName, CefWindowOpenDisposition targetDisposition, bool userGesture, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, ref CefClient client, CefBrowserSettings settings, ref bool noJavascriptAccess)
        {
            return true;
          
        }
    }

     Please suggest on this.

    
     Regards,
     Megha CS

Megha CS

unread,
Jan 23, 2017, 5:52:12 AM1/23/17
to CefGlue, dmitry....@gmail.com
      Hi Dmitry,
      This is the modified code.

<Grid Name="webGrid">
        <wpf:WpfCefBrowser Name="browser" ></wpf:WpfCefBrowser>
  </Grid>
     
browser.StartUrl="http://google.co.in";

MyCefLifeSpanHandler lifeSpanHandler = new MyCefLifeSpanHandler (browser);

public class MyCefLifeSpanHandler : CefLifeSpanHandler
    {
        private readonly WpfCefBrowser  _browser;


        public MyCefLifeSpanHandler (WpfCefBrowser browser)
        {
            _browser = browser;
          
        }

Dmitry Azaraev

unread,
Jan 23, 2017, 8:43:18 AM1/23/17
to CefGlue

According to your first message - you create MyCefLifeSpanHandler but not attach it to anything. Take wpf sources and modify existing client/lifespanhandler.


--

Megha CS

unread,
Jan 24, 2017, 9:29:05 AM1/24/17
to CefGlue, dmitry....@gmail.com
Hi Dmitry,

I have tried like this. but still its not triggering.

<Grid Name="webGrid">
        <wpf:WpfCefBrowser Name="browser" ></wpf:WpfCefBrowser>
 </Grid>

     public WpfBrowserWindow(string url)
        {
            InitializeComponent();
            SBCefClient client=new SBCefClient(browser);
            browser.StartUrl=url;
        }

-------------------------------------

     public class MyCefLifeSpanHandler : CefLifeSpanHandler
    {
        
        private readonly WpfCefBrowser  _browser;


        public MyCefLifeSpanHandler(WpfCefBrowser browser)
        {
            _browser = browser;
          
        }
        protected override void OnBeforePopup(CefBrowser browser, CefFrame frame, string targetUrl, string targetFrameName, CefWindowOpenDisposition targetDisposition, bool userGesture, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, ref CefClient client, CefBrowserSettings settings, ref bool noJavascriptAccess)
        {
             return true;
          
        }
        
        
    }
    public class MyCefClient : CefClient
    {
        private WpfCefBrowser _owner;
        private readonly SBCefLifeSpanHandler _lifeSpanHandler;

        public MyCefClient(WpfCefBrowser owner)
        {
            if (owner == null) throw new ArgumentNullException("owner");

            _owner = owner;
       
            _lifeSpanHandler = new MyCefLifeSpanHandler(owner);
          
        }
        protected override CefLifeSpanHandler GetLifeSpanHandler()
        {
            return _lifeSpanHandler;
        }
    }


Hence could you please suggest the sample code if u have.

Thanks & Regards,
Megha CS

Dmitry Azaraev

unread,
Jan 24, 2017, 9:37:41 AM1/24/17
to CefGlue

Client should be passed to CreateBrowser call. Looks like you just create objects without relying on that - as result they are not used.


--

Megha CS

unread,
Jan 25, 2017, 2:11:29 AM1/25/17
to CefGlue, dmitry....@gmail.com
Sorry for the mistake..This is the modified code. But still its not working.

<Grid Name="webGrid">
        <wpf:WpfCefBrowser Name="browser" ></wpf:WpfCefBrowser>
 </Grid>

     public WpfBrowserWindow(string url)
        {
            InitializeComponent();
            MyCefClient client=new MyCefClient (browser);
            browser.StartUrl=url;
        }
-------------------------------------------

    public class MyCefLifeSpanHandler : CefLifeSpanHandler
    {
        
        private readonly WpfCefBrowser  _browser;


        public MyCefLifeSpanHandler(WpfCefBrowser browser)
        {
            _browser = browser;
          
        }
        protected override void OnBeforePopup(CefBrowser browser, CefFrame frame, string targetUrl, string targetFrameName, CefWindowOpenDisposition targetDisposition, bool userGesture, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, ref CefClient client, CefBrowserSettings settings, ref bool noJavascriptAccess)
        {
             return true;
          
        }
        
        
    }
    public class MyCefClient : CefClient
    {
        private WpfCefBrowser _owner;
        private readonly MyCefLifeSpanHandler _lifeSpanHandler;

        public MyCefClient(WpfCefBrowser owner)
        {
            if (owner == nullthrow new ArgumentNullException("owner");

            _owner = owner;
       
            _lifeSpanHandler = new MyCefLifeSpanHandler(owner);
          
        }
        protected override CefLifeSpanHandler GetLifeSpanHandler()
        {
            return _lifeSpanHandler;
        }
    }

Please suggest on this..

Dmitry Azaraev

unread,
Jan 25, 2017, 5:55:29 AM1/25/17
to CefGlue

I'm already suggest.

public WpfBrowserWindow(string url)
        {
            InitializeComponent();
          MyCefClient client=new MyCefClient (browser); // there is no any sense at ths line
            browser.StartUrl=url;
        }


--

Megha CS

unread,
Jan 25, 2017, 8:06:14 AM1/25/17
to CefGlue, dmitry....@gmail.com
Hi Dmitry,

In your recent post "Client should be passed to CreateBrowser call", can you please provide Demo or Sample code to achieve this as i am new to cef Browser.

Regards,
Megha CS


Dmitry Azaraev

unread,
Jan 25, 2017, 9:38:07 AM1/25/17
to CefGlue
From my understanding you already take WpfOsr sample. It uses CefGlue.WPF assembly, which _sample_ implementation of off-screen (windowless) rendering browser. If you stick with it - get it and modify it as you need.

WpfCefBrowser.cs already calls CefBrowserHost.CreateBrowser and pass CefClient. And it is already holds WpfCefClient class, which you can modify as you need. Or provide own.

If you need browser to perform actual browsing - i'm recommend use windowed browser (winforms - do same thing). It is faster by design. You of course should find a way to integrate it into WPF application, if you stick with WPF. Use WPF's winforms host is possible.

Megha CS

unread,
Jan 30, 2017, 7:42:06 AM1/30/17
to CefGlue, dmitry....@gmail.com
Thank you Dmitry.Its working fine.:-)

Megha CS

unread,
Mar 10, 2017, 7:43:44 AM3/10/17
to CefGlue, dmitry....@gmail.com
Hi Dmitry,
As per your suggestion,written this code.It blocks popup but main url is not rendered in main Window. Please suggest how to render it in main window.

<Grid Name="webGrid">
        <wpf:WpfCefBrowser Name="browser" ></wpf:WpfCefBrowser>
 </Grid>

     public WpfBrowserWindow(string url)
        {
          InitializeComponent();

            this.Loaded += WpfBrowser_Loaded;
          
        }

void WpfBrowser_Loaded(object sender, RoutedEventArgs e)
        {
            Window parentWnd = FindParentOfType<Window>(this);
             if (parentWnd != null)
                        {
                          IntPtr hParentWnd = new WindowInteropHelper(parentWnd).Handle;
            
                          var windowInfo = CefWindowInfo.Create();
                      
                           var settings = new CefBrowserSettings();
                            _cefClient = new MyCefClient(browser);

                          
                          CefBrowserHost.CreateBrowser(windowInfo, _cefClient, settings, (!string.IsNullOrEmpty(browser.StartUrl)) ?                 "http://www.popuptest.com/popuptest1.html" : "about:blank");

                           
                        }
        
            browser.Focus();
            
        }
------------------------------------------------------------------

Dmitry Azaraev

unread,
Mar 10, 2017, 8:48:52 AM3/10/17
to CefGlue
Your question is not understandable. WPF sample already renders browser exactly in main window.

Megha CS

unread,
Mar 10, 2017, 9:11:40 AM3/10/17
to cef...@googlegroups.com
In loaded function,
If i written "windowInfo.SetAsChild(hParentWnd,rect)" statement,main url render it in child window. 
If i removed that main url is not rendering and getting blank page.
I don't know where i made mistake. Please suggest on this.

void WpfBrowser_Loaded(object sender, RoutedEventArgs e)
        {
            Window parentWnd = FindParentOfType<Window>(this);
             if (parentWnd != null)
                        {
                          IntPtr hParentWnd = new WindowInteropHelper(parentWnd).Handle;
            
                          var windowInfo = CefWindowInfo.Create();

                            CefRectangle rect=new CefRectangle();
                           rect.X=10;
                           rect.Y=10;
                           rect.Width=1000;
                           rect.Height=1000;

                            windowInfo.SetAsChild(hParentWnd,rect);    
To unsubscribe from this group and stop receiving emails from it, send an email to cefglue+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "CefGlue" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cefglue/un7hBPA3rt4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cefglue+unsubscribe@googlegroups.com.

Dmitry Azaraev

unread,
Mar 10, 2017, 9:18:40 AM3/10/17
to cef...@googlegroups.com
What you try to achieve?

To unsubscribe from this group and stop receiving emails from it, send an email to cefglue+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "CefGlue" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cefglue/un7hBPA3rt4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cefglue+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "CefGlue" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cefglue+u...@googlegroups.com.

Megha CS

unread,
Mar 10, 2017, 9:23:10 AM3/10/17
to cef...@googlegroups.com
I need to render url in wpfcefbrowser.If that url contains popup,it should be blocked.



To unsubscribe from this group and stop receiving emails from it, send an email to cefglue+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "CefGlue" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cefglue/un7hBPA3rt4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cefglue+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "CefGlue" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cefglue+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "CefGlue" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cefglue/un7hBPA3rt4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cefglue+unsubscribe@googlegroups.com.

Megha CS

unread,
Mar 10, 2017, 9:29:43 AM3/10/17
to cef...@googlegroups.com
Here "WpfBrowserWindow" class is UserControl. 

Dmitry Azaraev

unread,
Mar 10, 2017, 9:36:45 AM3/10/17
to cef...@googlegroups.com
So whats happens instead? From you code - browser should be created as child of given window handle, with given starting url. Check URL which used. Check events.

To unsubscribe from this group and stop receiving emails from it, send an email to cefglue+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "CefGlue" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cefglue/un7hBPA3rt4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cefglue+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "CefGlue" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cefglue+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "CefGlue" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cefglue/un7hBPA3rt4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cefglue+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "CefGlue" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cefglue+u...@googlegroups.com.

Megha CS

unread,
Mar 14, 2017, 9:58:45 AM3/14/17
to CefGlue, dmitry....@gmail.com
Hi Dmitry,
I have written code like this. I am getting exception. Please suggest on this.

void WpfBrowser_Loaded(object sender, RoutedEventArgs e)
        {
            Window parentWnd = FindParentOfType<Window>(this);
             if (parentWnd != null)
                        {
                          IntPtr hParentWnd = new WindowInteropHelper(parentWnd).Handle;
            
                          var windowInfo = CefWindowInfo.Create();


                              windowInfo.SetAsWindowless(hParentWnd,true); 

                           var settings = new CefBrowserSettings();
                            _cefClient = new MyCefClient(browser);

                          
                          CefBrowserHost.CreateBrowser(windowInfo, _cefClient, settings, (!string.IsNullOrEmpty(browser.StartUrl)) ?                 "http://www.popuptest.com/popuptest1.html" : "about:blank");

                           
                        }
        
            browser.Focus();
            
        }

------------------
System.InvalidOperationException: Failed to create browser.

   at Xilium.CefGlue.CefBrowserHost.CreateBrowser(CefWindowInfo windowInfo, CefClient client, CefBrowserSettings settings, String url, CefRequestContext requestContext)
   at Xilium.CefGlue.CefBrowserHost.CreateBrowser(CefWindowInfo windowInfo, CefClient client, CefBrowserSettings settings, String url)
   at Browser.WpfBrowserWindow.WpfBrowser_Loaded(Object sender, RoutedEventArgs e) in d:\GIT\New\Browser\SourceCode\WPF\WpfBrowserWindow.xaml.cs:line 65

   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.RouteItem.InvokeHandler(RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.EventRoute.InvokeHandlers(Object source, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
   at System.Windows.FrameworkElement.OnLoaded(RoutedEventArgs args)
   at MS.Internal.FrameworkObject.OnLoaded(RoutedEventArgs args)
   at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
   at System.Windows.BroadcastEventHelper.BroadcastLoadedSynchronously(DependencyObject rootDO, Boolean isLoaded)
   at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
   at MS.Internal.LoadedOrUnloadedOperation.DoWork()
   at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
   at System.Windows.Interop.HwndTarget.OnResize()
   at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.HwndSubclass.CallOldWindowProc(IntPtr oldWndProc, IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

Dmitry Azaraev

unread,
Mar 14, 2017, 10:06:21 AM3/14/17
to CefGlue
Your CefClient should provide render handler or browser creation will fail.

Reply all
Reply to author
Forward
0 new messages