使用CefGlue从网址

时间:2016-04-27 05:19:17

标签: c# chromium-embedded cefglue

我正在尝试为以下(原型)方法编写实现:

var result = browser.GetHtml(string url);

我需要这个的原因是因为有很多页面将大量的Javascript推送到浏览器,然后Javascript呈现页面。可靠地检索此类页面的唯一方法是在检索生成的HTML之前允许Javascript在浏览器环境中执行。

我目前的尝试是使用CefGlue。下载this project并将其与this answer中的代码相结合后,我想出了以下代码(此处包含完整性):

using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Xilium.CefGlue;

namespace OffScreenCefGlue
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            // Load CEF. This checks for the correct CEF version.
            CefRuntime.Load();

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

            // This is where the code path divereges for child processes.
            if (CefRuntime.ExecuteProcess(cefMainArgs, cefApp) != -1)
            {
                Console.Error.WriteLine("CefRuntime could not create 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);

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

            // 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 = "http://www.reddit.com/";
            CefBrowserHost.CreateBrowser(cefWindowInfo, cefClient, cefBrowserSettings, url);

            // Hang, to let the browser do its work.
            Console.Read();

            // Clean up CEF.
            CefRuntime.Shutdown();
        }
    }

    internal class DemoCefApp : CefApp
    {
    }

    internal class DemoCefClient : CefClient
    {
        private readonly DemoCefLoadHandler _loadHandler;
        private readonly DemoCefRenderHandler _renderHandler;

        public DemoCefClient(int windowWidth, int windowHeight)
        {
            _renderHandler = new DemoCefRenderHandler(windowWidth, windowHeight);
            _loadHandler = new DemoCefLoadHandler();
        }

        protected override CefRenderHandler GetRenderHandler()
        {
            return _renderHandler;
        }

        protected override CefLoadHandler GetLoadHandler()
        {
            return _loadHandler;
        }
    }

    internal class DemoCefLoadHandler : CefLoadHandler
    {
        public string Html { get; private set; }

        protected override void OnLoadStart(CefBrowser browser, CefFrame frame)
        {
            // A single CefBrowser instance can handle multiple requests
            //   for a single URL if there are frames (i.e. <FRAME>, <IFRAME>).
            if (frame.IsMain)
            {
                Console.WriteLine("START: {0}", browser.GetMainFrame().Url);
            }
        }

        protected override async void OnLoadEnd(CefBrowser browser, CefFrame frame, int httpStatusCode)
        {
            if (frame.IsMain)
            {
                Html = await browser.GetSourceAsync();
                Console.WriteLine("END: {0}, {1}", browser.GetMainFrame().Url, httpStatusCode);
            }
        }
    }

    internal class DemoCefRenderHandler : CefRenderHandler
    {
        private readonly int _windowHeight;
        private readonly int _windowWidth;

        public DemoCefRenderHandler(int windowWidth, int windowHeight)
        {
            _windowWidth = windowWidth;
            _windowHeight = windowHeight;
        }

        protected override bool GetRootScreenRect(CefBrowser browser, ref CefRectangle rect)
        {
            return GetViewRect(browser, ref rect);
        }

        protected override bool GetScreenPoint(CefBrowser browser, int viewX, int viewY, ref int screenX, ref int screenY)
        {
            screenX = viewX;
            screenY = viewY;
            return true;
        }

        protected override bool GetViewRect(CefBrowser browser, ref CefRectangle rect)
        {
            rect.X = 0;
            rect.Y = 0;
            rect.Width = _windowWidth;
            rect.Height = _windowHeight;
            return true;
        }

        protected override bool GetScreenInfo(CefBrowser browser, CefScreenInfo screenInfo)
        {
            return false;
        }

        protected override void OnPopupSize(CefBrowser browser, CefRectangle rect)
        {
        }

        protected override void OnPaint(CefBrowser browser, CefPaintElementType type, CefRectangle[] dirtyRects, IntPtr buffer, int width, int height)
        {
            // Save the provided buffer (a bitmap image) as a PNG.
            var bitmap = new Bitmap(width, height, width*4, PixelFormat.Format32bppRgb, buffer);
            bitmap.Save("LastOnPaint.png", ImageFormat.Png);
        }

        protected override void OnCursorChange(CefBrowser browser, IntPtr cursorHandle)
        {
        }

        protected override void OnScrollOffsetChanged(CefBrowser browser)
        {
        }
    }

    public class TaskStringVisitor : CefStringVisitor
    {
        private readonly TaskCompletionSource<string> taskCompletionSource;

        public TaskStringVisitor()
        {
            taskCompletionSource = new TaskCompletionSource<string>();
        }

        protected override void Visit(string value)
        {
            taskCompletionSource.SetResult(value);
        }

        public Task<string> Task
        {
            get { return taskCompletionSource.Task; }
        }
    }

    public static class CEFExtensions
    {
        public static Task<string> GetSourceAsync(this CefBrowser browser)
        {
            TaskStringVisitor taskStringVisitor = new TaskStringVisitor();
            browser.GetMainFrame().GetSource(taskStringVisitor);
            return taskStringVisitor.Task;
        }
    }
}

相关的代码位在这里:

protected override async void OnLoadEnd(CefBrowser browser, CefFrame frame, int httpStatusCode)
{
    if (frame.IsMain)
    {
        Html = await browser.GetSourceAsync();
        Console.WriteLine("END: {0}, {1}", browser.GetMainFrame().Url, httpStatusCode);
    }
}

这实际上似乎有效;您可以使用调试器检查Html变量,并且其中有一个HTML页面。问题是,Html变量对我的回调方法没有好处;它在一个类层次结构中埋藏了三层深,我需要在不尝试创建Schroedinbug的方法中返回它。

(尝试从string Html属性获取结果,包括尝试使用调试器中的Html可视化工具查看它,似乎会导致死锁,这是我真正想要避免的,特别是这段代码将在服务器上运行)。

如何安全可靠地实现var result = browser.GetHtml(string url);

加分问题:上述代码中的回调机制是否可以使用this technique转换为任务?那会是什么样的?

1 个答案:

答案 0 :(得分:0)

请记住,当前的CefGlue版本没有提供任何同步上下文,因此大多数时候你不应该在回调中使用async / await,除非你确定你做了什么。

&#34;可靠&#34;代码应该是异步的,因为大多数CEF调用都是异步的(提供或不提供回调)。 Async / await大大简化了这项任务,因此我假设这个问题可以简化为:&#34;如何正确编写GetSourceAsync方法?&#34;。这也是您的红利问题,简单的答案当然是否定的,this technique应该被认为是有害的,因为如果不了解底层代码会导致不同的影响。

因此,无论使用GetSourceAsync方法,特别是TaskStringVisitor,我都建议你永远不要直接执行TaskCompletionSource的方法,因为它同步执行continuation(在.NET 4.6中,它可以选择异步执行continuation,但我个人没有检查4.6内部是如何完成的。这需要尽快释放CEF线程之一。否则最终你可以获得大的延续树,循环或等待,实际上是什么阻塞浏览器的线程永远。另请注意,this kind extensions are also harmful,因为它们有上述相同的问题 - 处理的唯一选择是真正的异步延续。

protected override void Visit(string value)
{
    System.Threading.Tasks.Task.Run(() => taskCompletionSource.SetResult(value));
}

某些CEF API是混合的:如果我们已经没有在必需的线程上或者同步执行,它们会将任务排队到所需的线程。对于这种情况,应该简化处理,在这种情况下最好避免异步。再次,只是为了避免同步延续,因为它们可能导致重入问题和/或只是你获得不必要的堆栈帧(希望只在很短的时间内,并且代码没有卡在某处)。

最简单的示例之一是,但对于其他一些API调用也是如此:

internal static class CefTaskHelper
{
    public static Task RunAsync(CefThreadId threadId, Action action)
    {
        if (CefRuntime.CurrentlyOn(threadId))
        {
            action();
            return TaskHelpers.Completed();
        }
        else
        {
            var tcs = new TaskCompletionSource<FakeVoid>();
            StartNew(threadId, () =>
            {
                try
                {
                    action();
                    tcs.SetResultAsync(default(FakeVoid));
                }
                catch (Exception e)
                {
                    tcs.SetExceptionAsync(e);
                }
            });
            return tcs.Task;
        }
    }

    public static void StartNew(CefThreadId threadId, Action action)
    {
        CefRuntime.PostTask(threadId, new CefActionTask(action));
    }
}

<强>更新

  

这实际上似乎有效;你可以检查Html变量   调试器,那里有一个HTML页面。问题是,   Html变量对我的回调方法没有好处;它被埋没了   类层次结构中有三层深层,我需要在它中返回它   方法I尝试在不创建Schroedinbug的情况下编写。

您只需要实现CefLifeSpanHandler,然后一旦创建它就可以直接访问CefBrowser(它是异步创建的)。存在CreateBrowserSync调用,但不是优先方式。

PS:我在CefGlue下一代的路上,但现在还没有准备好使用。计划更好的异步/等待集成。我个人在服务器端环境中密切使用async / await东西。