Skip to content

Commit e035c39

Browse files
committed
Core - Add Default Handler implementations
Inherit and override only the required methods rather than implement the whole interface
1 parent fd1afdd commit e035c39

22 files changed

+1854
-47
lines changed

CefSharp.Example/Handlers/BrowserProcessHandler.cs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace CefSharp.Example.Handlers
1111
{
12-
public class BrowserProcessHandler : IBrowserProcessHandler
12+
public class BrowserProcessHandler : CefSharp.Handler.BrowserProcessHandler
1313
{
1414
/// <summary>
1515
/// The interval between calls to Cef.DoMessageLoopWork
@@ -20,7 +20,7 @@ public class BrowserProcessHandler : IBrowserProcessHandler
2020
/// </summary>
2121
protected const int ThirtyTimesPerSecond = 1000 / 30; //30fps
2222

23-
void IBrowserProcessHandler.OnContextInitialized()
23+
protected override void OnContextInitialized()
2424
{
2525
//The Global CookieManager has been initialized, you can now set cookies
2626
var cookieManager = Cef.GetGlobalCookieManager();
@@ -106,26 +106,5 @@ void IBrowserProcessHandler.OnContextInitialized()
106106
context.RegisterSchemeHandlerFactory("https", "cefsharp.example", folderSchemeHandlerExample);
107107
}
108108
}
109-
110-
void IBrowserProcessHandler.OnScheduleMessagePumpWork(long delay)
111-
{
112-
//If the delay is greater than the Maximum then use ThirtyTimesPerSecond
113-
//instead - we do this to achieve a minimum number of FPS
114-
if (delay > ThirtyTimesPerSecond)
115-
{
116-
delay = ThirtyTimesPerSecond;
117-
}
118-
OnScheduleMessagePumpWork((int)delay);
119-
}
120-
121-
protected virtual void OnScheduleMessagePumpWork(int delay)
122-
{
123-
//TODO: Schedule work on the UI thread - call Cef.DoMessageLoopWork
124-
}
125-
126-
public virtual void Dispose()
127-
{
128-
129-
}
130109
}
131110
}

CefSharp.WinForms.Example/Handlers/ScheduleMessagePumpBrowserProcessHandler.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,15 @@ private void TimerTick(object sender, EventArgs e)
3939
factory.StartNew(() => Cef.DoMessageLoopWork());
4040
}
4141

42-
protected override void OnScheduleMessagePumpWork(int delay)
42+
protected override void OnScheduleMessagePumpWork(long delay)
4343
{
44+
//If the delay is greater than the Maximum then use ThirtyTimesPerSecond
45+
//instead - we do this to achieve a minimum number of FPS
46+
if (delay > ThirtyTimesPerSecond)
47+
{
48+
delay = ThirtyTimesPerSecond;
49+
}
50+
4451
//when delay <= 0 queue the Task up for execution on the UI thread.
4552
if (delay <= 0)
4653
{
@@ -49,14 +56,19 @@ protected override void OnScheduleMessagePumpWork(int delay)
4956
}
5057
}
5158

52-
public override void Dispose()
59+
protected override void Dispose(bool disposing)
5360
{
54-
if (timer != null)
61+
if(disposing)
5562
{
56-
timer.Stop();
57-
timer.Dispose();
58-
timer = null;
63+
if (timer != null)
64+
{
65+
timer.Stop();
66+
timer.Dispose();
67+
timer = null;
68+
}
5969
}
70+
71+
base.Dispose(disposing);
6072
}
6173
}
6274
}

CefSharp.WinForms.Example/Handlers/WinFormsBrowserProcessHandler.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,24 @@ private void TimerTick(object sender, EventArgs e)
3333
Cef.DoMessageLoopWork();
3434
}
3535

36-
protected override void OnScheduleMessagePumpWork(int delay)
36+
protected override void OnScheduleMessagePumpWork(long delay)
3737
{
3838
//NOOP - Only enabled when CefSettings.ExternalMessagePump
3939
}
4040

41-
public override void Dispose()
41+
protected override void Dispose(bool disposing)
4242
{
43-
if (timer != null)
43+
if(disposing)
4444
{
45-
timer.Stop();
46-
timer.Dispose();
47-
timer = null;
45+
if (timer != null)
46+
{
47+
timer.Stop();
48+
timer.Dispose();
49+
timer = null;
50+
}
4851
}
52+
53+
base.Dispose(disposing);
4954
}
5055
}
5156
}

CefSharp.Wpf.Example/Handlers/WpfBrowserProcessHandler.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,15 @@ private void TimerTick(object sender, EventArgs e)
4444
dispatcher.BeginInvoke((Action)(() => Cef.DoMessageLoopWork()), DispatcherPriority.Render);
4545
}
4646

47-
protected override void OnScheduleMessagePumpWork(int delay)
47+
protected override void OnScheduleMessagePumpWork(long delay)
4848
{
49+
//If the delay is greater than the Maximum then use ThirtyTimesPerSecond
50+
//instead - we do this to achieve a minimum number of FPS
51+
if (delay > ThirtyTimesPerSecond)
52+
{
53+
delay = ThirtyTimesPerSecond;
54+
}
55+
4956
//When delay <= 0 we'll execute Cef.DoMessageLoopWork immediately
5057
// if it's greater than we'll just let the Timer which fires 30 times per second
5158
// care of the call
@@ -55,20 +62,25 @@ protected override void OnScheduleMessagePumpWork(int delay)
5562
}
5663
}
5764

58-
public override void Dispose()
65+
protected override void Dispose(bool disposing)
5966
{
60-
if (dispatcher != null)
67+
if(disposing)
6168
{
62-
dispatcher.ShutdownStarted -= DispatcherShutdownStarted;
63-
dispatcher = null;
64-
}
69+
if (dispatcher != null)
70+
{
71+
dispatcher.ShutdownStarted -= DispatcherShutdownStarted;
72+
dispatcher = null;
73+
}
6574

66-
if (timer != null)
67-
{
68-
timer.Stop();
69-
timer.Dispose();
70-
timer = null;
75+
if (timer != null)
76+
{
77+
timer.Stop();
78+
timer.Dispose();
79+
timer = null;
80+
}
7181
}
82+
83+
base.Dispose(disposing);
7284
}
7385
}
7486
}

CefSharp/CefSharp.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@
104104
<Compile Include="Enums\CookiePriority.cs" />
105105
<Compile Include="Enums\CookieSameSite.cs" />
106106
<Compile Include="Enums\SchemeOptions.cs" />
107+
<Compile Include="Handler\AccessibilityHandler.cs" />
108+
<Compile Include="Handler\AudioHandler.cs" />
109+
<Compile Include="Handler\BrowserProcessHandler.cs" />
110+
<Compile Include="Handler\ContextMenuHandler.cs" />
111+
<Compile Include="Handler\CookieAccessFilter.cs" />
112+
<Compile Include="Handler\DialogHandler.cs" />
113+
<Compile Include="Handler\DisplayHandler.cs" />
114+
<Compile Include="Handler\DownloadHandler.cs" />
115+
<Compile Include="Handler\DragHandler .cs" />
116+
<Compile Include="Handler\ExtensionHandler .cs" />
117+
<Compile Include="Handler\FindHandler.cs" />
118+
<Compile Include="Handler\FocusHandler.cs" />
119+
<Compile Include="Handler\LoadHandler.cs" />
120+
<Compile Include="Handler\KeyboardHandler.cs" />
121+
<Compile Include="Handler\JsDialogHandler.cs" />
122+
<Compile Include="Handler\LifeSpanHandler.cs" />
107123
<Compile Include="Handler\RequestContextHandler.cs" />
108124
<Compile Include="Internals\CefThread.cs" />
109125
<Compile Include="Internals\FreezableBase.cs" />

CefSharp/DefaultApp.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

5+
using System;
56
using System.Collections.Generic;
67

78
namespace CefSharp
@@ -10,8 +11,10 @@ namespace CefSharp
1011
/// Default implementation of <see cref="IApp"/> which represents the CefApp class.
1112
/// </summary>
1213
/// <seealso cref="T:CefSharp.IApp"/>
13-
public class DefaultApp : IApp
14+
public class DefaultApp : IApp, IDisposable
1415
{
16+
private bool isDisposed;
17+
1518
/// <summary>
1619
/// Return the handler for functionality specific to the browser process. This method is called on multiple threads.
1720
/// </summary>
@@ -88,5 +91,31 @@ protected virtual void OnRegisterCustomSchemes(ISchemeRegistrar registrar)
8891
}
8992
}
9093
}
94+
95+
/// <summary>
96+
/// Releases unmanaged and managed resources
97+
/// </summary>
98+
/// <param name="disposing"><see langword="true" /> to release both managed and unmanaged resources; <see langword="false" /> to release only unmanaged resources.</param>
99+
protected virtual void Dispose(bool disposing)
100+
{
101+
if (!isDisposed)
102+
{
103+
if (disposing)
104+
{
105+
BrowserProcessHandler?.Dispose();
106+
BrowserProcessHandler = null;
107+
}
108+
109+
isDisposed = true;
110+
}
111+
}
112+
113+
/// <inheritdoc/>
114+
void IDisposable.Dispose()
115+
{
116+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
117+
Dispose(disposing: true);
118+
GC.SuppressFinalize(this);
119+
}
91120
}
92121
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright © 2021 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.Handler
6+
{
7+
/// <summary>
8+
/// Inherit from this class to receive accessibility notification when accessibility events have been registered.
9+
/// It's important to note that the methods of this interface are called on a CEF UI thread,
10+
/// which by default is not the same as your application UI thread.
11+
/// </summary>
12+
public class AccessibilityHandler : IAccessibilityHandler
13+
{
14+
/// <inheritdoc/>
15+
void IAccessibilityHandler.OnAccessibilityLocationChange(IValue value)
16+
{
17+
OnAccessibilityLocationChange(value);
18+
}
19+
20+
/// <summary>
21+
/// Called after renderer process sends accessibility location changes to the browser process.
22+
/// </summary>
23+
/// <param name="value">Updated location info.</param>
24+
protected virtual void OnAccessibilityLocationChange(IValue value)
25+
{
26+
27+
}
28+
29+
/// <inheritdoc/>
30+
void IAccessibilityHandler.OnAccessibilityTreeChange(IValue value)
31+
{
32+
OnAccessibilityTreeChange(value);
33+
}
34+
35+
/// <summary>
36+
/// Called after renderer process sends accessibility tree changes to the browser process.
37+
/// </summary>
38+
/// <param name="value">Updated tree info.</param>
39+
protected virtual void OnAccessibilityTreeChange(IValue value)
40+
{
41+
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)