diff --git a/CefSharp.Core/Cef.h b/CefSharp.Core/Cef.h index 16f89516c..0968035a4 100644 --- a/CefSharp.Core/Cef.h +++ b/CefSharp.Core/Cef.h @@ -43,12 +43,6 @@ namespace CefSharp } public: - /// - /// Called on the CEF UI thread immediately after the CEF context has been initialized. - /// You can now access the Global RequestContext through Cef.GetGlobalRequestContext() - this is the - /// first place you can set Preferences (e.g. proxy settings, spell check dictionaries). - /// - static property Action^ OnContextInitialized; static property TaskFactory^ UIThreadTaskFactory; static property TaskFactory^ IOThreadTaskFactory; @@ -149,7 +143,7 @@ namespace CefSharp /// true if successful; otherwise, false. static bool Initialize(CefSettings^ cefSettings) { - return Initialize(cefSettings, true, false); + return Initialize(cefSettings, false, nullptr); } /// @@ -157,11 +151,9 @@ namespace CefSharp /// This function should be called on the main application thread to initialize the CEF browser process. /// /// CefSharp configuration settings. - /// When the Current AppDomain (relative to the thread called on) - /// Exits(ProcessExit event) then Shudown will be called. /// Check that all relevant dependencies avaliable, throws exception if any are missing /// true if successful; otherwise, false. - static bool Initialize(CefSettings^ cefSettings, bool shutdownOnProcessExit, bool performDependencyCheck) + static bool Initialize(CefSettings^ cefSettings, bool performDependencyCheck, IBrowserProcessHandler^ browserProcessHandler) { if (IsInitialized) { @@ -184,7 +176,7 @@ namespace CefSharp FileThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_FILE)); CefMainArgs main_args; - CefRefPtr app(new CefSharpApp(cefSettings, OnContextInitialized)); + CefRefPtr app(new CefSharpApp(cefSettings, browserProcessHandler)); auto success = CefInitialize(main_args, *(cefSettings->_cefSettings), app.get(), NULL); @@ -202,12 +194,20 @@ namespace CefSharp return success; } - /// 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. + /// + /// Perform a single iteration of CEF message loop processing.This function is + /// provided for cases where the CEF message loop must be integrated into an + /// existing application message loop. Use of this function is not recommended + /// for most users; use CefSettings.MultiThreadedMessageLoop if possible (the deault). + /// When using this function care must be taken to balance performance + /// against excessive CPU usage. It is recommended to enable the + /// CefSettings.ExternalMessagePump option when using + /// this function so that IBrowserProcessHandler.OnScheduleMessagePumpWork() + /// callbacks can facilitate the scheduling process. This function should only be + /// called on the main application thread and only if Cef.Initialize() is called + /// with a CefSettings.MultiThreadedMessageLoop value of false. This function + /// will not block. + /// static void DoMessageLoopWork() { CefDoMessageLoopWork(); @@ -343,8 +343,6 @@ namespace CefSharp { if (IsInitialized) { - OnContextInitialized = nullptr; - msclr::lock l(_sync); UIThreadTaskFactory = nullptr; diff --git a/CefSharp.Core/CefSettings.h b/CefSharp.Core/CefSettings.h index 65b810fac..dcacea90a 100644 --- a/CefSharp.Core/CefSettings.h +++ b/CefSharp.Core/CefSettings.h @@ -94,6 +94,22 @@ namespace CefSharp void set(bool value) { _cefSettings->command_line_args_disabled = value; } } + /// + /// Set to true to control browser process main (UI) thread message pump + /// scheduling via the IBrowserProcessHandler.OnScheduleMessagePumpWork + /// callback. This option is recommended for use in combination with the + /// Cef.DoMessageLoopWork() function in cases where the CEF message loop must be + /// integrated into an existing application message loop (see additional + /// comments and warnings on Cef.DoMessageLoopWork). Enabling this option is not + /// recommended for most users; leave this option disabled and use either + /// MultiThreadedMessageLoop (the default) if possible. + /// + property bool ExternalMessagePump + { + bool get() { return _cefSettings->external_message_pump == 1; } + void set(bool value) { _cefSettings->external_message_pump = value; } + } + /// //// Set to true to have the browser process message loop run in a separate /// thread. If false than the CefDoMessageLoopWork() function must be diff --git a/CefSharp.Core/Internals/CefSharpApp.h b/CefSharp.Core/Internals/CefSharpApp.h index 648345ee9..f74e26419 100644 --- a/CefSharp.Core/Internals/CefSharpApp.h +++ b/CefSharp.Core/Internals/CefSharpApp.h @@ -15,19 +15,19 @@ namespace CefSharp public CefBrowserProcessHandler { gcroot _cefSettings; - gcroot _onContextInitialized; + gcroot _browserProcessHandler; public: - CefSharpApp(CefSettings^ cefSettings, Action^ onContextInitialized) : + CefSharpApp(CefSettings^ cefSettings, IBrowserProcessHandler^ browserProcessHandler) : _cefSettings(cefSettings), - _onContextInitialized(onContextInitialized) + _browserProcessHandler(browserProcessHandler) { } ~CefSharpApp() { _cefSettings = nullptr; - _onContextInitialized = nullptr; + _browserProcessHandler = nullptr; } virtual CefRefPtr GetBrowserProcessHandler() OVERRIDE @@ -37,9 +37,9 @@ namespace CefSharp virtual void OnContextInitialized() OVERRIDE { - if (!Object::ReferenceEquals(_onContextInitialized, nullptr)) + if (!Object::ReferenceEquals(_browserProcessHandler, nullptr)) { - _onContextInitialized->Invoke(); + _browserProcessHandler->OnContextInitialized(); } } diff --git a/CefSharp.Example/CefExample.cs b/CefSharp.Example/CefExample.cs index 2f2d4114b..546a7c77b 100644 --- a/CefSharp.Example/CefExample.cs +++ b/CefSharp.Example/CefExample.cs @@ -6,6 +6,7 @@ using System; using System.Diagnostics; using System.Text; using System.Threading.Tasks; +using CefSharp.Example.Handlers; using CefSharp.Example.Properties; using CefSharp.Example.Proxy; using CefSharp.Internals; @@ -165,24 +166,7 @@ namespace CefSharp.Example settings.FocusedNodeChangedEnabled = true; - //The Request Context has been initialized, you can now set preferences, like proxy server settings - Cef.OnContextInitialized = delegate - { - var cookieManager = Cef.GetGlobalCookieManager(); - cookieManager.SetStoragePath("cookies", true); - cookieManager.SetSupportedSchemes("custom"); - - //Dispose of context when finished - preferable not to keep a reference if possible. - using (var context = Cef.GetGlobalRequestContext()) - { - string errorMessage; - //You can set most preferences using a `.` notation rather than having to create a complex set of dictionaries. - //The default is true, you can change to false to disable - context.SetPreference("webkit.webprefs.plugins_enabled", true, out errorMessage); - } - }; - - if (!Cef.Initialize(settings, shutdownOnProcessExit: true, performDependencyCheck: !DebuggingSubProcess)) + if (!Cef.Initialize(settings, performDependencyCheck: !DebuggingSubProcess, browserProcessHandler: new BrowserProcessHandler())) { throw new Exception("Unable to Initialize Cef"); } diff --git a/CefSharp.Example/CefSharp.Example.csproj b/CefSharp.Example/CefSharp.Example.csproj index 0ee0ed61e..29e2bb4e9 100644 --- a/CefSharp.Example/CefSharp.Example.csproj +++ b/CefSharp.Example/CefSharp.Example.csproj @@ -77,6 +77,7 @@ + diff --git a/CefSharp.Example/Handlers/BrowserProcessHandler.cs b/CefSharp.Example/Handlers/BrowserProcessHandler.cs new file mode 100644 index 000000000..0499a08f0 --- /dev/null +++ b/CefSharp.Example/Handlers/BrowserProcessHandler.cs @@ -0,0 +1,31 @@ +// 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. + +namespace CefSharp.Example.Handlers +{ + public class BrowserProcessHandler : IBrowserProcessHandler + { + void IBrowserProcessHandler.OnContextInitialized() + { + //The Request Context has been initialized, you can now set preferences, like proxy server settings + var cookieManager = Cef.GetGlobalCookieManager(); + cookieManager.SetStoragePath("cookies", true); + cookieManager.SetSupportedSchemes("custom"); + + //Dispose of context when finished - preferable not to keep a reference if possible. + using (var context = Cef.GetGlobalRequestContext()) + { + string errorMessage; + //You can set most preferences using a `.` notation rather than having to create a complex set of dictionaries. + //The default is true, you can change to false to disable + context.SetPreference("webkit.webprefs.plugins_enabled", true, out errorMessage); + } + } + + void IBrowserProcessHandler.OnScheduleMessagePumpWork(long delay) + { + //TODO: Schedule work on the UI thread - call Cef.DoMessageLoopWork + } + } +} diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 9d2169987..0b5035c92 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -87,6 +87,7 @@ + diff --git a/CefSharp/IBrowserProcessHandler.cs b/CefSharp/IBrowserProcessHandler.cs new file mode 100644 index 000000000..3bc13701a --- /dev/null +++ b/CefSharp/IBrowserProcessHandler.cs @@ -0,0 +1,32 @@ +// 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. + +namespace CefSharp +{ + public interface IBrowserProcessHandler + { + /// + /// Called on the CEF UI thread immediately after the CEF context has been initialized. + /// You can now access the Global RequestContext through Cef.GetGlobalRequestContext() - this is the + /// first place you can set Preferences (e.g. proxy settings, spell check dictionaries). + /// + void OnContextInitialized(); + + /// + /// Called from any thread when work has been scheduled for the browser process + /// main (UI) thread. This callback is used in combination with CefSettings. + /// ExternalMessagePump and Cef.DoMessageLoopWork() in cases where the CEF + /// message loop must be integrated into an existing application message loop + /// (see additional comments and warnings on Cef.DoMessageLoopWork). This + /// callback should schedule a Cef.DoMessageLoopWork() call to happen on the + /// main (UI) thread. + /// + /// is the requested delay in milliseconds. If + /// delay is <= 0 then the call should happen reasonably soon. If + /// delay is > 0 then the call should be scheduled to happen after the + /// specified delay and any currently pending scheduled call should be + /// cancelled. + void OnScheduleMessagePumpWork(long delay); + } +}