Skip to content

Commit 63ef6be

Browse files
committed
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
1 parent 20503f2 commit 63ef6be

File tree

8 files changed

+106
-43
lines changed

8 files changed

+106
-43
lines changed

CefSharp.Core/Cef.h

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ namespace CefSharp
4343
}
4444

4545
public:
46-
/// <summary>
47-
/// Called on the CEF UI thread immediately after the CEF context has been initialized.
48-
/// You can now access the Global RequestContext through Cef.GetGlobalRequestContext() - this is the
49-
/// first place you can set Preferences (e.g. proxy settings, spell check dictionaries).
50-
/// </summary>
51-
static property Action^ OnContextInitialized;
5246

5347
static property TaskFactory^ UIThreadTaskFactory;
5448
static property TaskFactory^ IOThreadTaskFactory;
@@ -149,19 +143,17 @@ namespace CefSharp
149143
/// <return>true if successful; otherwise, false.</return>
150144
static bool Initialize(CefSettings^ cefSettings)
151145
{
152-
return Initialize(cefSettings, true, false);
146+
return Initialize(cefSettings, false, nullptr);
153147
}
154148

155149
/// <summary>
156150
/// Initializes CefSharp with user-provided settings.
157151
/// This function should be called on the main application thread to initialize the CEF browser process.
158152
/// </summary>
159153
/// <param name="cefSettings">CefSharp configuration settings.</param>
160-
/// <param name="shutdownOnProcessExit">When the Current AppDomain (relative to the thread called on)
161-
/// Exits(ProcessExit event) then Shudown will be called.</param>
162154
/// <param name="performDependencyCheck">Check that all relevant dependencies avaliable, throws exception if any are missing</param>
163155
/// <return>true if successful; otherwise, false.</return>
164-
static bool Initialize(CefSettings^ cefSettings, bool shutdownOnProcessExit, bool performDependencyCheck)
156+
static bool Initialize(CefSettings^ cefSettings, bool performDependencyCheck, IBrowserProcessHandler^ browserProcessHandler)
165157
{
166158
if (IsInitialized)
167159
{
@@ -184,7 +176,7 @@ namespace CefSharp
184176
FileThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_FILE));
185177

186178
CefMainArgs main_args;
187-
CefRefPtr<CefSharpApp> app(new CefSharpApp(cefSettings, OnContextInitialized));
179+
CefRefPtr<CefSharpApp> app(new CefSharpApp(cefSettings, browserProcessHandler));
188180

189181
auto success = CefInitialize(main_args, *(cefSettings->_cefSettings), app.get(), NULL);
190182

@@ -202,12 +194,20 @@ namespace CefSharp
202194
return success;
203195
}
204196

205-
/// <summary>Perform a single iteration of CEF message loop processing. This function is
206-
/// used to integrate the CEF message loop into an existing application message
207-
/// loop. Care must be taken to balance performance against excessive CPU usage.
208-
/// This function should only be called on the main application thread and only
209-
/// if CefInitialize() is called with a CefSettings.multi_threaded_message_loop
210-
/// value of false. This function will not block.</summary>
197+
/// <summary>
198+
/// Perform a single iteration of CEF message loop processing.This function is
199+
/// provided for cases where the CEF message loop must be integrated into an
200+
/// existing application message loop. Use of this function is not recommended
201+
/// for most users; use CefSettings.MultiThreadedMessageLoop if possible (the deault).
202+
/// When using this function care must be taken to balance performance
203+
/// against excessive CPU usage. It is recommended to enable the
204+
/// CefSettings.ExternalMessagePump option when using
205+
/// this function so that IBrowserProcessHandler.OnScheduleMessagePumpWork()
206+
/// callbacks can facilitate the scheduling process. This function should only be
207+
/// called on the main application thread and only if Cef.Initialize() is called
208+
/// with a CefSettings.MultiThreadedMessageLoop value of false. This function
209+
/// will not block.
210+
/// </summary>
211211
static void DoMessageLoopWork()
212212
{
213213
CefDoMessageLoopWork();
@@ -343,8 +343,6 @@ namespace CefSharp
343343
{
344344
if (IsInitialized)
345345
{
346-
OnContextInitialized = nullptr;
347-
348346
msclr::lock l(_sync);
349347

350348
UIThreadTaskFactory = nullptr;

CefSharp.Core/CefSettings.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ namespace CefSharp
9494
void set(bool value) { _cefSettings->command_line_args_disabled = value; }
9595
}
9696

97+
/// <summary>
98+
/// Set to true to control browser process main (UI) thread message pump
99+
/// scheduling via the IBrowserProcessHandler.OnScheduleMessagePumpWork
100+
/// callback. This option is recommended for use in combination with the
101+
/// Cef.DoMessageLoopWork() function in cases where the CEF message loop must be
102+
/// integrated into an existing application message loop (see additional
103+
/// comments and warnings on Cef.DoMessageLoopWork). Enabling this option is not
104+
/// recommended for most users; leave this option disabled and use either
105+
/// MultiThreadedMessageLoop (the default) if possible.
106+
/// </summary>
107+
property bool ExternalMessagePump
108+
{
109+
bool get() { return _cefSettings->external_message_pump == 1; }
110+
void set(bool value) { _cefSettings->external_message_pump = value; }
111+
}
112+
97113
/// <summary>
98114
//// Set to true to have the browser process message loop run in a separate
99115
/// thread. If false than the CefDoMessageLoopWork() function must be

CefSharp.Core/Internals/CefSharpApp.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ namespace CefSharp
1515
public CefBrowserProcessHandler
1616
{
1717
gcroot<CefSettings^> _cefSettings;
18-
gcroot<Action^> _onContextInitialized;
18+
gcroot<IBrowserProcessHandler^> _browserProcessHandler;
1919

2020
public:
21-
CefSharpApp(CefSettings^ cefSettings, Action^ onContextInitialized) :
21+
CefSharpApp(CefSettings^ cefSettings, IBrowserProcessHandler^ browserProcessHandler) :
2222
_cefSettings(cefSettings),
23-
_onContextInitialized(onContextInitialized)
23+
_browserProcessHandler(browserProcessHandler)
2424
{
2525
}
2626

2727
~CefSharpApp()
2828
{
2929
_cefSettings = nullptr;
30-
_onContextInitialized = nullptr;
30+
_browserProcessHandler = nullptr;
3131
}
3232

3333
virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() OVERRIDE
@@ -37,9 +37,9 @@ namespace CefSharp
3737

3838
virtual void OnContextInitialized() OVERRIDE
3939
{
40-
if (!Object::ReferenceEquals(_onContextInitialized, nullptr))
40+
if (!Object::ReferenceEquals(_browserProcessHandler, nullptr))
4141
{
42-
_onContextInitialized->Invoke();
42+
_browserProcessHandler->OnContextInitialized();
4343
}
4444
}
4545

CefSharp.Example/CefExample.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics;
77
using System.Text;
88
using System.Threading.Tasks;
9+
using CefSharp.Example.Handlers;
910
using CefSharp.Example.Properties;
1011
using CefSharp.Example.Proxy;
1112
using CefSharp.Internals;
@@ -165,24 +166,7 @@ public static void Init(bool osr, bool multiThreadedMessageLoop)
165166

166167
settings.FocusedNodeChangedEnabled = true;
167168

168-
//The Request Context has been initialized, you can now set preferences, like proxy server settings
169-
Cef.OnContextInitialized = delegate
170-
{
171-
var cookieManager = Cef.GetGlobalCookieManager();
172-
cookieManager.SetStoragePath("cookies", true);
173-
cookieManager.SetSupportedSchemes("custom");
174-
175-
//Dispose of context when finished - preferable not to keep a reference if possible.
176-
using (var context = Cef.GetGlobalRequestContext())
177-
{
178-
string errorMessage;
179-
//You can set most preferences using a `.` notation rather than having to create a complex set of dictionaries.
180-
//The default is true, you can change to false to disable
181-
context.SetPreference("webkit.webprefs.plugins_enabled", true, out errorMessage);
182-
}
183-
};
184-
185-
if (!Cef.Initialize(settings, shutdownOnProcessExit: true, performDependencyCheck: !DebuggingSubProcess))
169+
if (!Cef.Initialize(settings, performDependencyCheck: !DebuggingSubProcess, browserProcessHandler: new BrowserProcessHandler()))
186170
{
187171
throw new Exception("Unable to Initialize Cef");
188172
}

CefSharp.Example/CefSharp.Example.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<Compile Include="FlashResourceHandler.cs" />
7878
<Compile Include="FlashResourceHandlerFactory.cs" />
7979
<Compile Include="Filters\PassThruResponseFilter.cs" />
80+
<Compile Include="Handlers\BrowserProcessHandler.cs" />
8081
<Compile Include="InMemorySchemeAndResourceHandlerFactory.cs" />
8182
<Compile Include="ScriptedMethodsBoundObject.cs" />
8283
<Compile Include="ExceptionTestBoundObject.cs" />
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright © 2010-2016 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
namespace CefSharp.Example.Handlers
6+
{
7+
public class BrowserProcessHandler : IBrowserProcessHandler
8+
{
9+
void IBrowserProcessHandler.OnContextInitialized()
10+
{
11+
//The Request Context has been initialized, you can now set preferences, like proxy server settings
12+
var cookieManager = Cef.GetGlobalCookieManager();
13+
cookieManager.SetStoragePath("cookies", true);
14+
cookieManager.SetSupportedSchemes("custom");
15+
16+
//Dispose of context when finished - preferable not to keep a reference if possible.
17+
using (var context = Cef.GetGlobalRequestContext())
18+
{
19+
string errorMessage;
20+
//You can set most preferences using a `.` notation rather than having to create a complex set of dictionaries.
21+
//The default is true, you can change to false to disable
22+
context.SetPreference("webkit.webprefs.plugins_enabled", true, out errorMessage);
23+
}
24+
}
25+
26+
void IBrowserProcessHandler.OnScheduleMessagePumpWork(long delay)
27+
{
28+
//TODO: Schedule work on the UI thread - call Cef.DoMessageLoopWork
29+
}
30+
}
31+
}

CefSharp/CefSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<Compile Include="DynamicDictionary.cs" />
8888
<Compile Include="FilterStatus.cs" />
8989
<Compile Include="Geoposition.cs" />
90+
<Compile Include="IBrowserProcessHandler.cs" />
9091
<Compile Include="IBrowserSettings.cs" />
9192
<Compile Include="IDomNode.cs" />
9293
<Compile Include="IFindHandler.cs" />

CefSharp/IBrowserProcessHandler.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright © 2010-2016 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
namespace CefSharp
6+
{
7+
public interface IBrowserProcessHandler
8+
{
9+
/// <summary>
10+
/// Called on the CEF UI thread immediately after the CEF context has been initialized.
11+
/// You can now access the Global RequestContext through Cef.GetGlobalRequestContext() - this is the
12+
/// first place you can set Preferences (e.g. proxy settings, spell check dictionaries).
13+
/// </summary>
14+
void OnContextInitialized();
15+
16+
/// <summary>
17+
/// Called from any thread when work has been scheduled for the browser process
18+
/// main (UI) thread. This callback is used in combination with CefSettings.
19+
/// ExternalMessagePump and Cef.DoMessageLoopWork() in cases where the CEF
20+
/// message loop must be integrated into an existing application message loop
21+
/// (see additional comments and warnings on Cef.DoMessageLoopWork). This
22+
/// callback should schedule a Cef.DoMessageLoopWork() call to happen on the
23+
/// main (UI) thread.
24+
/// </summary>
25+
/// <param name="delay">is the requested delay in milliseconds. If
26+
/// delay is <= 0 then the call should happen reasonably soon. If
27+
/// delay is > 0 then the call should be scheduled to happen after the
28+
/// specified delay and any currently pending scheduled call should be
29+
/// cancelled.</param>
30+
void OnScheduleMessagePumpWork(long delay);
31+
}
32+
}

0 commit comments

Comments
 (0)