Refactor OnContextInitialized out as part of IBrowserProcessHandler interface

Add OnScheduleMessagePumpWork for use with integrating into existing message loop - probably better for WinForms integration, will look at providing an example later
This commit is contained in:
amaitland
2016-07-20 13:08:47 +10:00
parent 20503f2289
commit 63ef6beb5f
8 changed files with 106 additions and 43 deletions
+17 -19
View File
@@ -43,12 +43,6 @@ namespace CefSharp
}
public:
/// <summary>
/// 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).
/// </summary>
static property Action^ OnContextInitialized;
static property TaskFactory^ UIThreadTaskFactory;
static property TaskFactory^ IOThreadTaskFactory;
@@ -149,7 +143,7 @@ namespace CefSharp
/// <return>true if successful; otherwise, false.</return>
static bool Initialize(CefSettings^ cefSettings)
{
return Initialize(cefSettings, true, false);
return Initialize(cefSettings, false, nullptr);
}
/// <summary>
@@ -157,11 +151,9 @@ namespace CefSharp
/// This function should be called on the main application thread to initialize the CEF browser process.
/// </summary>
/// <param name="cefSettings">CefSharp configuration settings.</param>
/// <param name="shutdownOnProcessExit">When the Current AppDomain (relative to the thread called on)
/// Exits(ProcessExit event) then Shudown will be called.</param>
/// <param name="performDependencyCheck">Check that all relevant dependencies avaliable, throws exception if any are missing</param>
/// <return>true if successful; otherwise, false.</return>
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<CefSharpApp> app(new CefSharpApp(cefSettings, OnContextInitialized));
CefRefPtr<CefSharpApp> app(new CefSharpApp(cefSettings, browserProcessHandler));
auto success = CefInitialize(main_args, *(cefSettings->_cefSettings), app.get(), NULL);
@@ -202,12 +194,20 @@ namespace CefSharp
return success;
}
/// <summary>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.</summary>
/// <summary>
/// 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.
/// </summary>
static void DoMessageLoopWork()
{
CefDoMessageLoopWork();
@@ -343,8 +343,6 @@ namespace CefSharp
{
if (IsInitialized)
{
OnContextInitialized = nullptr;
msclr::lock l(_sync);
UIThreadTaskFactory = nullptr;
+16
View File
@@ -94,6 +94,22 @@ namespace CefSharp
void set(bool value) { _cefSettings->command_line_args_disabled = value; }
}
/// <summary>
/// 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.
/// </summary>
property bool ExternalMessagePump
{
bool get() { return _cefSettings->external_message_pump == 1; }
void set(bool value) { _cefSettings->external_message_pump = value; }
}
/// <summary>
//// Set to true to have the browser process message loop run in a separate
/// thread. If false than the CefDoMessageLoopWork() function must be
+6 -6
View File
@@ -15,19 +15,19 @@ namespace CefSharp
public CefBrowserProcessHandler
{
gcroot<CefSettings^> _cefSettings;
gcroot<Action^> _onContextInitialized;
gcroot<IBrowserProcessHandler^> _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<CefBrowserProcessHandler> 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();
}
}
+2 -18
View File
@@ -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");
}
+1
View File
@@ -77,6 +77,7 @@
<Compile Include="FlashResourceHandler.cs" />
<Compile Include="FlashResourceHandlerFactory.cs" />
<Compile Include="Filters\PassThruResponseFilter.cs" />
<Compile Include="Handlers\BrowserProcessHandler.cs" />
<Compile Include="InMemorySchemeAndResourceHandlerFactory.cs" />
<Compile Include="ScriptedMethodsBoundObject.cs" />
<Compile Include="ExceptionTestBoundObject.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
}
}
}
+1
View File
@@ -87,6 +87,7 @@
<Compile Include="DynamicDictionary.cs" />
<Compile Include="FilterStatus.cs" />
<Compile Include="Geoposition.cs" />
<Compile Include="IBrowserProcessHandler.cs" />
<Compile Include="IBrowserSettings.cs" />
<Compile Include="IDomNode.cs" />
<Compile Include="IFindHandler.cs" />
+32
View File
@@ -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
{
/// <summary>
/// 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).
/// </summary>
void OnContextInitialized();
/// <summary>
/// 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.
/// </summary>
/// <param name="delay">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.</param>
void OnScheduleMessagePumpWork(long delay);
}
}