Creating wpfCefBrowser programmatically

683 views
Skip to first unread message

nicolas husson

unread,
Jul 15, 2013, 8:40:38 AM7/15/13
to cef...@googlegroups.com
Hi,
I tried to use cefglue.sample.wpfOsr and it worked fine, now i'm trying to create a wpfcefBrowser each time a user click on a button.
At the beginning of the constructor of my mainWindow i had this :

" CefRuntime.Load();
            var mainArgs = new CefMainArgs(new string[] { });
            var app = new SampleCefApp();
            var exitCode = CefRuntime.ExecuteProcess(mainArgs, app);
            if (exitCode != -1)
                return; // FMT: throw exception??
            CefSettings cefSettings = new CefSettings
            {
                SingleProcess = false,
                MultiThreadedMessageLoop = true,
                LogSeverity = CefLogSeverity.Verbose,
                LogFile = "cef.log",
            };

            CefRuntime.Initialize(mainArgs, cefSettings, app); "

where SampleCefApp looks like this :
"     class SampleCefApp : CefApp
    {
        public SampleCefApp()
        {
        }
    }"

and when the user click on the button in order to create a new browser, the constructor of my class browserWindow is called :

"
      public WpfCefBrowser webview;

        public BrowserWindow()
        {
            webview = new WpfCefBrowser();
            webview.StartUrl = "http://www.google.com";
             initializeWindow(webview, 550, 500, 0, 0, 20, 20);
            MainWindow.Current.mainGrid.Children.Add(webview);
        }


        public void initializeWindow(WpfCefBrowser webview,int height,int width,int left,int top,int right,int bottom)
        {
            webview.Height = height;
            webview.Width = width;
            webview.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            webview.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
            webview.Margin = new System.Windows.Thickness(left, top, right,bottom);
        }
"

But nothing happen on my app, and no error message...any idea?

Dmitry Azaraev

unread,
Jul 15, 2013, 9:30:52 AM7/15/13
to cef...@googlegroups.com
            if (exitCode != -1)
                return; // FMT: throw exception??

There is correct code. This is part which used when same executable spawned as child process.


About why it is not actually created - try to debug. The underlying browser must be created and when browser will be actually ready - you must see that actual drawing happens...

With minimal sample it can be more easy to answer.





--
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/groups/opt_out.
 
 



--
Best regards,
   Dmitry
Message has been deleted

Dmitry Azaraev

unread,
Jul 15, 2013, 10:53:20 AM7/15/13
to nicolas husson, cef...@googlegroups.com
Your's sample are working. If you want debug your's application - *disable* in project options Debug -> Enable the Visual Studio hosting process. Once it will be disabled - you can debug your's applicatoin, and CEF also will work.


On Mon, Jul 15, 2013 at 5:42 PM, nicolas husson <nick...@gmail.com> wrote:
Thank you for the fast answer.
In attach you will find my code, if you want to run it, add libcef.dll in bin/Debug I deleted it because it was quite big.
I change the singleProcess to true, and it works fine by the way.




--
Best regards,
   Dmitry

Dmitry Azaraev

unread,
Jul 15, 2013, 10:57:46 AM7/15/13
to nicolas husson, cef...@googlegroups.com
And in addition - follow Main method layout, as in sample application.
You are not correctly shutting down CEF.
You of course can perform CEF initialization later than in sample, but you need correctly shutdown it.
Message has been deleted

Dmitry Azaraev

unread,
Jul 15, 2013, 11:29:05 AM7/15/13
to cef...@googlegroups.com
CEF3 works in two modes:
- multi-process architecture - when child processes spawned like do it chrome (for example plugin process, renderer process, etc). renderer process spawning policy is relative simple, but main that you must known - that each site *can* be spawned in own renderer process, but it is absolutely not required. For example when renderer's limit reach - it will no create additional subprocesses.
- single-process architecture (non-officially supported by chrome, sometimes buggy) - all-in-one process.

You must initialize CEF once, and shutdown it before application exists. In dependence of CefSettings ones of described above behaviors will be applied. It also contains path to subprocess. By default it is points to exactly same executable (i.e. like chrome.exe spawn chrome.exe). So if you use multi-process arch - it is important to call cef much more earlier without unrequired (WPF) stuff. So initialization and creating CEF from MainWindow is usually are invalid way.

>  - also, each time a wpfCefBrowser is create a process of the app ( sampleCefApp ) is also created, am I right? and the 
> Main method will be called with the mainArgs in argument?
Yes, CefApp exists in each process, but all browser-related stuff is not used, and used only renderer-related stuff, returned by CefApp.GetRenderProcessHandler.




On Mon, Jul 15, 2013 at 6:17 PM, nicolas husson <nick...@gmail.com> wrote:
Damn your right i didn't pay attention, thank you.
I've got few more questions :
- with my implementation each wpfCefBrowser will have the same local path ? And I think that we can initialize CefRuntime only one time. So it have to be handle in the process of the app ( sampleCefApp ).
- also, each time a wpfCefBrowser is create a process of the app ( sampleCefApp ) is also created, am I right? and the Main method will be called with the mainArgs in argument?
Those questions must looks like quite easy for you, but I didn't find a lot of documentation on cefGlue

Thanks again

 

--
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/groups/opt_out.
 
 



--
Best regards,
   Dmitry

nicolas husson

unread,
Jul 16, 2013, 4:12:42 AM7/16/13
to cef...@googlegroups.com
Hi again, i've done all the things you said and everything works perfectly.
Now, i want to permit each user to login on same websites. So first of all I will have to manage the cookie, with CefRequestHandler.getCookieManager . I saw that it has to be called from the IO thread but this is not clear enough for me. If i understand i will have to create a class wich extends from cefRequestHandler, and from this class i could access to getcookieManager but when i'm triing to access to cookieManager with the method getCookieManager i don't know what i can put in first argument because, it's waiting a cefBrowser objet and i'm using wpfCefBrowser ( the cast is impossible ).

Tanks.

Dmitry Azaraev

unread,
Jul 16, 2013, 4:23:33 AM7/16/13
to cef...@googlegroups.com
Hi.

I'm not sure that i'm understand you completely.

Method CefRequestHandler.GetCookieManager will be called by CEF (by browser), it is not designed to be called directly from your's code.
The common easy way:
Each browser instance have own CefClient instance with own CefRequestHandler instance. So you need only create CefCookieManager for each browser instance, and return it from CefRequestHandler.
About IO thread - CEF have some number of 'named' threads listed in https://bitbucket.org/xilium/xilium.cefglue/src/b22787699e5791899e022bf97cf1b55156ea9728/CefGlue/Enums/CefThreadId.cs?at=default . CefRequestHandler.GetCookieManager called on CEF IO thread. Some operations can be performed only on specific threads. For example CefCookieManager.SetCookie method can be called only on IO thread, so don't miss read doc comments.

PS: You can extend WpfCefBrowser with CookieManager property and use it whenever you need (and of course return it from GetCookieManager.



--
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/groups/opt_out.
 
 



--
Best regards,
   Dmitry

nicolas husson

unread,
Jul 16, 2013, 5:33:01 AM7/16/13
to cef...@googlegroups.com, dmitry....@gmail.com

Sorry about that my english is not very good, but don't worry you answer well.

When you instanciate a wpfCefBrowser, there is no new Browser which is create behind?! ( As i can see in WpfCefBrowser.cs ) so a new Browser has to be create and pass to the method HandleAfterCreated(CefBrowser browser) in order to link a new browser with the wpfBrowser.
If i'm wrong, and a browser is created when wpfCefBrowser is instanciate how to access to it?

Dmitry Azaraev

unread,
Jul 16, 2013, 5:39:30 AM7/16/13
to nicolas husson, cef...@googlegroups.com
Your's english good enough for me. My english also not good.

WpfCefBrowser request to create new instance of CefBrowser in ArrangeOverride method (look on CefBrowserHost.CreateBrowser call in this method). Once it created (HandleAfterCreated) - it stored in _browser private field. So if you need raw access to CefBrowser, you need modify WpfCefBrowser, (for example by adding CefBrowser property which will return _browser). But note that you can access to it, only after it will be created (i.e. after HandleAfterCreated was been called).

--
Best regards,
   Dmitry

nicolas husson

unread,
Jul 16, 2013, 6:57:54 AM7/16/13
to cef...@googlegroups.com, nicolas husson, dmitry....@gmail.com
Thanks I understand... almost everything.
You wrote juste before " create CefCookieManager for each browser instance, and return it from CefRequestHandler." i know how to create the cefCookieManager this is not too hard but i don't know how to return it from CefRequestHandler. Can you write few lines to show me?

Dmitry Azaraev

unread,
Jul 16, 2013, 7:04:51 AM7/16/13
to cef...@googlegroups.com, nicolas husson
You need provide own implementation of CefRequestHandler.

1. I.e. somewhere in WpfCefBrowser you will create _cookieManager;
2. In WpfCefClient.cs you add GetRequestHandler method and return handler from here.
3. Make own class which will derived from CefRequestHandler, similar to already implemented classes, like WpfCefLifeSpanHandler.
In your's created MyRequestHandler: you just: return _owner._cookieManager; from GetCookieManager method. In this case you can just ignore method's arguments, 'cause your's cookie manager already associated (logically) with one browser instance.



On Tue, Jul 16, 2013 at 1:57 PM, nicolas husson <nick...@gmail.com> wrote:
Thanks I understand... almost everything.
You wrote juste before " create CefCookieManager for each browser instance, and return it from CefRequestHandler." i know how to create the cefCookieManager this is not too hard but i don't know how to return it from CefRequestHandler. Can you write few lines to show me?

--
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/groups/opt_out.
 
 



--
Best regards,
   Dmitry

nicolas husson

unread,
Jul 16, 2013, 7:50:38 AM7/16/13
to cef...@googlegroups.com, nicolas husson, dmitry....@gmail.com
YEAAhhh
It works!!! of course it's logic, the goal is just to override the method getcookiemanager. Thank you so much. 

Alexander Fialko

unread,
Mar 20, 2014, 5:27:16 AM3/20/14
to cef...@googlegroups.com, nicolas husson, dmitry....@gmail.com
Hello.
It seems that GetCookieManager was eliminated from CefRequestHandler in the current version of code CefGlue/3.It exists only in the CefRequestContextHandler, and can not be overriden since CefRequestContext(the class that ) is a sealed class. Can we return back method GetCookieManager to CefRequestHandler? 

Thank you.


вторник, 16 июля 2013 г., 14:04:51 UTC+3 пользователь Dmitry Azaraev написал:

Dmitry Azaraev

unread,
Mar 20, 2014, 8:27:03 AM3/20/14
to cef...@googlegroups.com
CefRequestContextHandler is abstract class, so GetCookieManager can be overriden in it.

> Can we return back method GetCookieManager to CefRequestHandler? 
No, there is part of request context handler. CefRequestContextHandler can be used together with CefCrequestContext, which should be passed to creating browser (see at CefBrowserHost.CreateBrowser* methods).



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



--
Best regards,
   Dmitry

Alex Ivanov

unread,
Mar 20, 2014, 2:56:48 PM3/20/14
to cef...@googlegroups.com, dmitry....@gmail.com
Hello,
Thank you for quick replay. Yes, i saw them, but still no luck - 

So i have created context handler, that return In-Memory cookie manager (path is empty), but it seems in that case cookies are ignored (i will do more tests tomorrow and post update)

    public class RequestContextHandler : CefRequestContextHandler
    {
        private CefCookieManager cefCookieManager;

        public RequestContextHandler()
        {
            cefCookieManager = CefCookieManager.Create("", true);
        }

        protected override CefCookieManager GetCookieManager()
        {
            return cefCookieManager;
        }
    }


четверг, 20 марта 2014 г., 15:27:03 UTC+3 пользователь Dmitry Azaraev написал:

Dmitry Azaraev

unread,
Mar 20, 2014, 5:53:16 PM3/20/14
to cef...@googlegroups.com
Hard to say anything.
If you feeling bug, you probably need to prepare reproduction sample.
In general - there is CEF-related question, not related to CefGlue (i.e. not related to binding to CEF).
If you provide repro sample - i can see on it. Thanks.
Reply all
Reply to author
Forward
0 new messages