Create windowless browser with CefGlue

Having problems with building or using the CefGlue .NET/Mono binding? Ask your questions here.

Moderator: fddima

Create windowless browser with CefGlue

Postby kirasangoku » Wed Nov 16, 2016 5:08 am

I'm working on a MVC web application in which I need to scrapp a website. I'm trying to use CefGlue library . I need to create a browser with the url of the website. However, the browser I'm getting doesn't contain any frame and I'm not getting any errors :

CefRuntime.Load();

// Start the secondary CEF process.
var cefMainArgs = new CefMainArgs(new string[0]);
var cefApp = new PCefApp();

// This is where the code path divereges for child processes.
if (CefRuntime.ExecuteProcess(cefMainArgs, cefApp, IntPtr.Zero) != -1)
{
Debug.WriteLine("CefRuntime could not the secondary process.");
}

// Settings for all of CEF (e.g. process management and control).
var cefSettings = new CefSettings
{
SingleProcess = false,
MultiThreadedMessageLoop = true
};

// Start the browser process (a child process).
CefRuntime.Initialize(cefMainArgs, cefSettings, cefApp, IntPtr.Zero);

// Instruct CEF to not render to a window at all.
CefWindowInfo cefWindowInfo = CefWindowInfo.Create();
cefWindowInfo..SetAsWindowless();

// Settings for the browser window itself (e.g. should JavaScript be enabled?).
var cefBrowserSettings = new CefBrowserSettings();

// Initialize some the cust interactions with the browser process.
// The browser window will be 1280 x 720 (pixels).
var cefClient = new DemoCefClient(1280, 720);


// Start up the browser instance.
string url = "https://www.website.com";
var browser = CefBrowserHost.CreateBrowserSync(cefWindowInfo, cefClient, cefBrowserSettings, url);
Debug.WriteLine(browser.GetFrameNames().Count());
Runtime.Shutdown();
I can post DemoCefRenderHandler , DemoCefLoadHandler, PCefApp codes if needed.

browser.GetFrameNames().Count() is returning 0

Any help is appreciated
kirasangoku
Newbie
 
Posts: 1
Joined: Wed Nov 16, 2016 5:06 am

Re: Create windowless browser with CefGlue

Postby fddima » Wed Nov 16, 2016 5:39 pm

Integration CEF into foreign applications

There is different possibilities exists: do it directly, or indirectly (implement out-of-process browser). I'm recommend always go with out-of-browser with foreign applications, especially if it is "pluggable" apps like Outlook or Visual Studio or whatever else, when you can easily disrupt host application stability or other plugin can disrupt your plugin.

With ASP.NET situation even worse: application hosted in own app domain, life time is unknown (controlled externally by admin and app recycling policy), some thread stuff, so you even never be able to shutdown CEF correctly. Also CEF can do some things what will be not expected in asp.net hosting process. I'm already answer on similar question here, in short, it is easier with better net result is create separate browser process and interact with it. This is what i'm exactly do for Outlook before, and this is what i'm exactly do for tasks very similar to your's. Otherwise, for example, if browser crashed - it will crash with whole ASP.NET application, what will cause hard to debug errors.

UPD, i'm just quote related part from link above:
fddima wrote:2. Implement out-of-process browser. I.e. plugin creates child process. Child process via IPC interact with plugin, and inserts native browser window by using window reparenting. Plugin simple wraps this. You can unify or leave unique each plugin. Hard way. But it guaranteed to work. There is how i'm make add-id for outlook. Additionally this can help break x86/x64 differences, while out of process browser can be simple x86 and able to run by x86 and x64 process. For outlook case this is also removes any process instability, so plugin does not conflict with anything, does not crash plugin host, and more important (with outlook case - no problems with stealed focus on new popup windows, no need to hook main hwnd (which finally almost impossible in outlook), something else...).

1-st way are easier as for me.
2-nd more reliable, but usually MUST be used if you not own host app.


I'm also wrote about somewhere at this forum, but general idea should be clear.


CEF usage general points

1. Read CEF wiki about CEF usage. It should give you understanding of CEF possibilities and common usage scenarios. CefGlue provides very similar API but some specifics exist. But i'm doesn't see that you need to know about specifics just now.

2. Create minimal sample: in sense that it should be regular application (can be console - no matter) which do all work. It will be easier to getting started.

3. You can inspect CefGlue's WpfOsr sample which actually uses windowless rendering (and then shows content to wpf's control). cefclient (C++) also supports windowless rendering (can be enabled by switch), so it can be used as reference sample implementation, but i'm recommend focus on something simplest - WpfOsr much more simpler (and limited) in comparison to cefclient, but this should be more than enough to get started.

4. Enable at least error logging in CefSettings - sometimes it says something useful.


Problems in your code (which i'm see with closed eyes)

1. You should enable windowless rendering by using CefSettings::WindowlessRenderingEnabled.

2. After browser created it should do lot of things, like fetch data from url via network, parse data and many other background things. So frame object is not appears immediately, and actually some time it still has zero frames. You should wait for example page finish load.


General suggestions

1. Avoid to using MultiThreadedMessageLoop without real need. Actually whenever you start new development, you not needed in this option. This option is windows only, and used to integrate CEF into existing applications, when you can't control message loop. Also current CEF versions supports external message pump mechanism for same reason, but without separate thread. But normally, you should use built-in message loop. It provides best performance, and better stability.

2. Avoid to use CreateBrowserSync in real code. It is may be ok for sample code, but in all cases you want be truly asynchronous, what handled by CreateBrowser (creates browser asynchronously - and this is normal operation mode).
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am


Return to CefGlue Forum

Who is online

Users browsing this forum: No registered users and 19 guests