From 9fbde82ef0abcf649f438843e5713166e33bbc3a Mon Sep 17 00:00:00 2001 From: amaitland Date: Thu, 1 Sep 2016 16:51:20 +1000 Subject: [PATCH] Experimental implementation of IBrowserProcessHandler.OnScheduleMessagePumpWork NOT READY FOR PRODUCTION USE --- CefSharp.Example/CefExample.cs | 5 +- .../Handlers/BrowserProcessHandler.cs | 23 +++++++ CefSharp.OffScreen.Example/Program.cs | 3 +- .../CefSharp.WinForms.Example.csproj | 1 + .../Handlers/WinFormsBrowserProcessHandler.cs | 65 +++++++++++++++++++ CefSharp.WinForms.Example/Program.cs | 34 ++++++---- CefSharp.Wpf.Example/App.xaml.cs | 3 +- 7 files changed, 117 insertions(+), 17 deletions(-) create mode 100644 CefSharp.WinForms.Example/Handlers/WinFormsBrowserProcessHandler.cs diff --git a/CefSharp.Example/CefExample.cs b/CefSharp.Example/CefExample.cs index bba04acb5..0a74bd922 100644 --- a/CefSharp.Example/CefExample.cs +++ b/CefSharp.Example/CefExample.cs @@ -33,7 +33,7 @@ namespace CefSharp.Example private static readonly bool DebuggingSubProcess = Debugger.IsAttached; private static string PluginInformation = ""; - public static void Init(bool osr, bool multiThreadedMessageLoop) + public static void Init(bool osr, bool multiThreadedMessageLoop, IBrowserProcessHandler browserProcessHandler) { // Set Google API keys, used for Geolocation requests sans GPS. See http://www.chromium.org/developers/how-tos/api-keys // Environment.SetEnvironmentVariable("GOOGLE_API_KEY", ""); @@ -88,6 +88,7 @@ namespace CefSharp.Example //settings.CefCommandLineArgs.Add("disable-direct-write", "1"); settings.MultiThreadedMessageLoop = multiThreadedMessageLoop; + settings.ExternalMessagePump = !multiThreadedMessageLoop; // Off Screen rendering (WPF/Offscreen) if(osr) @@ -161,7 +162,7 @@ namespace CefSharp.Example settings.FocusedNodeChangedEnabled = true; - if (!Cef.Initialize(settings, performDependencyCheck: !DebuggingSubProcess, browserProcessHandler: new BrowserProcessHandler())) + if (!Cef.Initialize(settings, performDependencyCheck: !DebuggingSubProcess, browserProcessHandler: browserProcessHandler)) { throw new Exception("Unable to Initialize Cef"); } diff --git a/CefSharp.Example/Handlers/BrowserProcessHandler.cs b/CefSharp.Example/Handlers/BrowserProcessHandler.cs index 0499a08f0..a08f909f1 100644 --- a/CefSharp.Example/Handlers/BrowserProcessHandler.cs +++ b/CefSharp.Example/Handlers/BrowserProcessHandler.cs @@ -2,10 +2,17 @@ // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. +using System; + namespace CefSharp.Example.Handlers { public class BrowserProcessHandler : IBrowserProcessHandler { + /// + /// The maximum number of milliseconds we're willing to wait between calls to OnScheduleMessagePumpWork(). + /// + protected const int MaxTimerDelay = 1000 / 30; // 30fps + void IBrowserProcessHandler.OnContextInitialized() { //The Request Context has been initialized, you can now set preferences, like proxy server settings @@ -24,8 +31,24 @@ namespace CefSharp.Example.Handlers } void IBrowserProcessHandler.OnScheduleMessagePumpWork(long delay) + { + //If the delay is greater than the Maximum then use MaxTimerDelay + //instead - we do this to achieve a minimum number of FPS + if(delay > MaxTimerDelay) + { + delay = MaxTimerDelay; + } + OnScheduleMessagePumpWork((int)delay); + } + + protected virtual void OnScheduleMessagePumpWork(int delay) { //TODO: Schedule work on the UI thread - call Cef.DoMessageLoopWork } + + public virtual void Dispose() + { + + } } } diff --git a/CefSharp.OffScreen.Example/Program.cs b/CefSharp.OffScreen.Example/Program.cs index e8d6db089..ff8313598 100644 --- a/CefSharp.OffScreen.Example/Program.cs +++ b/CefSharp.OffScreen.Example/Program.cs @@ -9,6 +9,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using CefSharp.Example; +using CefSharp.Example.Handlers; using CefSharp.Internals; namespace CefSharp.OffScreen.Example @@ -24,7 +25,7 @@ namespace CefSharp.OffScreen.Example Console.WriteLine(); // You need to replace this with your own call to Cef.Initialize(); - CefExample.Init(true, multiThreadedMessageLoop:true); + CefExample.Init(true, multiThreadedMessageLoop:true, browserProcessHandler: new BrowserProcessHandler()); MainAsync("cachePath1", 1.0); //Demo showing Zoom Level of 3.0 diff --git a/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj b/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj index 6f9f270b6..1e8526452 100644 --- a/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj +++ b/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj @@ -85,6 +85,7 @@ + Form diff --git a/CefSharp.WinForms.Example/Handlers/WinFormsBrowserProcessHandler.cs b/CefSharp.WinForms.Example/Handlers/WinFormsBrowserProcessHandler.cs new file mode 100644 index 000000000..c188fd724 --- /dev/null +++ b/CefSharp.WinForms.Example/Handlers/WinFormsBrowserProcessHandler.cs @@ -0,0 +1,65 @@ +// Copyright © 2010-2016 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +using System; +using System.Threading.Tasks; +using CefSharp.Example.Handlers; +using System.Timers; + +namespace CefSharp.WinForms.Example.Handlers +{ + /// + /// EXPERIMENTAL - this implementation is very simplistic and not ready for production use + /// See the following link for the CEF reference implementation. + /// https://bitbucket.org/chromiumembedded/cef/commits/1ff26aa02a656b3bc9f0712591c92849c5909e04?at=2785 + /// + public class WinFormsBrowserProcessHandler : BrowserProcessHandler + { + private Timer timer; + private TaskFactory factory; + + public WinFormsBrowserProcessHandler(TaskScheduler scheduler) + { + factory = new TaskFactory(scheduler); + timer = new Timer { Interval = MaxTimerDelay, AutoReset = true }; + timer.Start(); + timer.Elapsed += TimerTick; + } + + private void TimerTick(object sender, EventArgs e) + { + //Reset the timer interval to maximum + timer.Interval = MaxTimerDelay; + + //Execute DoMessageLoopWork on UI thread + factory.StartNew(() => Cef.DoMessageLoopWork()); + } + + protected override void OnScheduleMessagePumpWork(int delay) + { + //Basically execute Cef.DoMessageLoopWork 30 times per second, + //when delay <= 0 set the timer interval to really small so it's executed + // relatively quickly. + if(delay <= 0) + { + //Update the timer to execute almost immediately + timer.Interval = 1; + } + else + { + timer.Interval = delay; + } + } + + public override void Dispose() + { + if(timer != null) + { + timer.Stop(); + timer.Dispose(); + timer = null; + } + } + } +} diff --git a/CefSharp.WinForms.Example/Program.cs b/CefSharp.WinForms.Example/Program.cs index 0e87a04e1..78129ab5f 100644 --- a/CefSharp.WinForms.Example/Program.cs +++ b/CefSharp.WinForms.Example/Program.cs @@ -3,8 +3,12 @@ // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System; +using System.Threading; +using System.Threading.Tasks; using System.Windows.Forms; using CefSharp.Example; +using CefSharp.Example.Handlers; +using CefSharp.WinForms.Example.Handlers; using CefSharp.WinForms.Example.Minimal; namespace CefSharp.WinForms.Example @@ -56,23 +60,27 @@ namespace CefSharp.WinForms.Example #endif const bool multiThreadedMessageLoop = true; - CefExample.Init(false, multiThreadedMessageLoop: multiThreadedMessageLoop); - - if (multiThreadedMessageLoop == false) - { - //http://magpcss.org/ceforum/apidocs3/projects/%28default%29/%28_globals%29.html#CefDoMessageLoopWork%28%29 - //Perform a single iteration of CEF message loop processing. - //This function is used to integrate the CEF message loop into an existing application message loop. - //Care must be taken to balance performance against excessive CPU usage. - //This function should only be called on the main application thread and only if CefInitialize() is called with a CefSettings.multi_threaded_message_loop value of false. - //This function will not block. - - Application.Idle += (s, e) => Cef.DoMessageLoopWork(); - } var browser = new BrowserForm(); //var browser = new SimpleBrowserForm(); //var browser = new TabulationDemoForm(); + + IBrowserProcessHandler browserProcessHandler; + + if(multiThreadedMessageLoop) + { + browserProcessHandler = new BrowserProcessHandler(); + } + else + { + //Get the current taskScheduler (must be called after the form is created) + var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); + + browserProcessHandler = new WinFormsBrowserProcessHandler(scheduler); + } + + CefExample.Init(osr: false, multiThreadedMessageLoop: multiThreadedMessageLoop, browserProcessHandler: browserProcessHandler); + Application.Run(browser); } diff --git a/CefSharp.Wpf.Example/App.xaml.cs b/CefSharp.Wpf.Example/App.xaml.cs index d18144d55..8d66b29d1 100644 --- a/CefSharp.Wpf.Example/App.xaml.cs +++ b/CefSharp.Wpf.Example/App.xaml.cs @@ -4,6 +4,7 @@ using System.Windows; using CefSharp.Example; +using CefSharp.Example.Handlers; namespace CefSharp.Wpf.Example { @@ -19,7 +20,7 @@ namespace CefSharp.Wpf.Example } #endif - CefExample.Init(true, multiThreadedMessageLoop: true); + CefExample.Init(true, multiThreadedMessageLoop: true, browserProcessHandler: new BrowserProcessHandler()); base.OnStartup(e); }