Skip to content

Commit

Permalink
Core - Add Default Handler implementations
Browse files Browse the repository at this point in the history
Inherit and override only the required methods rather than implement the whole interface
  • Loading branch information
amaitland committed May 11, 2021
1 parent fd1afdd commit e035c39
Show file tree
Hide file tree
Showing 22 changed files with 1,854 additions and 47 deletions.
25 changes: 2 additions & 23 deletions CefSharp.Example/Handlers/BrowserProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace CefSharp.Example.Handlers
{
public class BrowserProcessHandler : IBrowserProcessHandler
public class BrowserProcessHandler : CefSharp.Handler.BrowserProcessHandler
{
/// <summary>
/// The interval between calls to Cef.DoMessageLoopWork
Expand All @@ -20,7 +20,7 @@ public class BrowserProcessHandler : IBrowserProcessHandler
/// </summary>
protected const int ThirtyTimesPerSecond = 1000 / 30; //30fps

void IBrowserProcessHandler.OnContextInitialized()
protected override void OnContextInitialized()
{
//The Global CookieManager has been initialized, you can now set cookies
var cookieManager = Cef.GetGlobalCookieManager();
Expand Down Expand Up @@ -106,26 +106,5 @@ void IBrowserProcessHandler.OnContextInitialized()
context.RegisterSchemeHandlerFactory("https", "cefsharp.example", folderSchemeHandlerExample);
}
}

void IBrowserProcessHandler.OnScheduleMessagePumpWork(long delay)
{
//If the delay is greater than the Maximum then use ThirtyTimesPerSecond
//instead - we do this to achieve a minimum number of FPS
if (delay > ThirtyTimesPerSecond)
{
delay = ThirtyTimesPerSecond;
}
OnScheduleMessagePumpWork((int)delay);
}

protected virtual void OnScheduleMessagePumpWork(int delay)
{
//TODO: Schedule work on the UI thread - call Cef.DoMessageLoopWork
}

public virtual void Dispose()
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ private void TimerTick(object sender, EventArgs e)
factory.StartNew(() => Cef.DoMessageLoopWork());
}

protected override void OnScheduleMessagePumpWork(int delay)
protected override void OnScheduleMessagePumpWork(long delay)
{
//If the delay is greater than the Maximum then use ThirtyTimesPerSecond
//instead - we do this to achieve a minimum number of FPS
if (delay > ThirtyTimesPerSecond)
{
delay = ThirtyTimesPerSecond;
}

//when delay <= 0 queue the Task up for execution on the UI thread.
if (delay <= 0)
{
Expand All @@ -49,14 +56,19 @@ protected override void OnScheduleMessagePumpWork(int delay)
}
}

public override void Dispose()
protected override void Dispose(bool disposing)
{
if (timer != null)
if(disposing)
{
timer.Stop();
timer.Dispose();
timer = null;
if (timer != null)
{
timer.Stop();
timer.Dispose();
timer = null;
}
}

base.Dispose(disposing);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,24 @@ private void TimerTick(object sender, EventArgs e)
Cef.DoMessageLoopWork();
}

protected override void OnScheduleMessagePumpWork(int delay)
protected override void OnScheduleMessagePumpWork(long delay)
{
//NOOP - Only enabled when CefSettings.ExternalMessagePump
}

public override void Dispose()
protected override void Dispose(bool disposing)
{
if (timer != null)
if(disposing)
{
timer.Stop();
timer.Dispose();
timer = null;
if (timer != null)
{
timer.Stop();
timer.Dispose();
timer = null;
}
}

base.Dispose(disposing);
}
}
}
34 changes: 23 additions & 11 deletions CefSharp.Wpf.Example/Handlers/WpfBrowserProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ private void TimerTick(object sender, EventArgs e)
dispatcher.BeginInvoke((Action)(() => Cef.DoMessageLoopWork()), DispatcherPriority.Render);
}

protected override void OnScheduleMessagePumpWork(int delay)
protected override void OnScheduleMessagePumpWork(long delay)
{
//If the delay is greater than the Maximum then use ThirtyTimesPerSecond
//instead - we do this to achieve a minimum number of FPS
if (delay > ThirtyTimesPerSecond)
{
delay = ThirtyTimesPerSecond;
}

//When delay <= 0 we'll execute Cef.DoMessageLoopWork immediately
// if it's greater than we'll just let the Timer which fires 30 times per second
// care of the call
Expand All @@ -55,20 +62,25 @@ protected override void OnScheduleMessagePumpWork(int delay)
}
}

public override void Dispose()
protected override void Dispose(bool disposing)
{
if (dispatcher != null)
if(disposing)
{
dispatcher.ShutdownStarted -= DispatcherShutdownStarted;
dispatcher = null;
}
if (dispatcher != null)
{
dispatcher.ShutdownStarted -= DispatcherShutdownStarted;
dispatcher = null;
}

if (timer != null)
{
timer.Stop();
timer.Dispose();
timer = null;
if (timer != null)
{
timer.Stop();
timer.Dispose();
timer = null;
}
}

base.Dispose(disposing);
}
}
}
16 changes: 16 additions & 0 deletions CefSharp/CefSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@
<Compile Include="Enums\CookiePriority.cs" />
<Compile Include="Enums\CookieSameSite.cs" />
<Compile Include="Enums\SchemeOptions.cs" />
<Compile Include="Handler\AccessibilityHandler.cs" />
<Compile Include="Handler\AudioHandler.cs" />
<Compile Include="Handler\BrowserProcessHandler.cs" />
<Compile Include="Handler\ContextMenuHandler.cs" />
<Compile Include="Handler\CookieAccessFilter.cs" />
<Compile Include="Handler\DialogHandler.cs" />
<Compile Include="Handler\DisplayHandler.cs" />
<Compile Include="Handler\DownloadHandler.cs" />
<Compile Include="Handler\DragHandler .cs" />
<Compile Include="Handler\ExtensionHandler .cs" />
<Compile Include="Handler\FindHandler.cs" />
<Compile Include="Handler\FocusHandler.cs" />
<Compile Include="Handler\LoadHandler.cs" />
<Compile Include="Handler\KeyboardHandler.cs" />
<Compile Include="Handler\JsDialogHandler.cs" />
<Compile Include="Handler\LifeSpanHandler.cs" />
<Compile Include="Handler\RequestContextHandler.cs" />
<Compile Include="Internals\CefThread.cs" />
<Compile Include="Internals\FreezableBase.cs" />
Expand Down
31 changes: 30 additions & 1 deletion CefSharp/DefaultApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using System.Collections.Generic;

namespace CefSharp
Expand All @@ -10,8 +11,10 @@ namespace CefSharp
/// Default implementation of <see cref="IApp"/> which represents the CefApp class.
/// </summary>
/// <seealso cref="T:CefSharp.IApp"/>
public class DefaultApp : IApp
public class DefaultApp : IApp, IDisposable
{
private bool isDisposed;

/// <summary>
/// Return the handler for functionality specific to the browser process. This method is called on multiple threads.
/// </summary>
Expand Down Expand Up @@ -88,5 +91,31 @@ protected virtual void OnRegisterCustomSchemes(ISchemeRegistrar registrar)
}
}
}

/// <summary>
/// Releases unmanaged and managed resources
/// </summary>
/// <param name="disposing"><see langword="true" /> to release both managed and unmanaged resources; <see langword="false" /> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
if (disposing)
{
BrowserProcessHandler?.Dispose();
BrowserProcessHandler = null;
}

isDisposed = true;
}
}

/// <inheritdoc/>
void IDisposable.Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
44 changes: 44 additions & 0 deletions CefSharp/Handler/AccessibilityHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright © 2021 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.Handler
{
/// <summary>
/// Inherit from this class to receive accessibility notification when accessibility events have been registered.
/// It's important to note that the methods of this interface are called on a CEF UI thread,
/// which by default is not the same as your application UI thread.
/// </summary>
public class AccessibilityHandler : IAccessibilityHandler
{
/// <inheritdoc/>
void IAccessibilityHandler.OnAccessibilityLocationChange(IValue value)
{
OnAccessibilityLocationChange(value);
}

/// <summary>
/// Called after renderer process sends accessibility location changes to the browser process.
/// </summary>
/// <param name="value">Updated location info.</param>
protected virtual void OnAccessibilityLocationChange(IValue value)
{

}

/// <inheritdoc/>
void IAccessibilityHandler.OnAccessibilityTreeChange(IValue value)
{
OnAccessibilityTreeChange(value);
}

/// <summary>
/// Called after renderer process sends accessibility tree changes to the browser process.
/// </summary>
/// <param name="value">Updated tree info.</param>
protected virtual void OnAccessibilityTreeChange(IValue value)
{

}
}
}
Loading

0 comments on commit e035c39

Please sign in to comment.