Skip to content

Commit

Permalink
Code Cleanup - Minor code cleanup
Browse files Browse the repository at this point in the history
- Add Microsoft.CodeAnalysis.NetAnalyzers package
- Make some classes sealed
- Fix/Add xml doc
  • Loading branch information
amaitland committed Feb 1, 2022
1 parent 12d28b6 commit 3b1c46a
Show file tree
Hide file tree
Showing 24 changed files with 129 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static bool TryFindHandle(IBrowser browser, out IntPtr chromerRenderWidge
/// Helper function used to find the child HWND with the ClassName matching <paramref name="chromeRenderWidgetHostClassName"/>
/// Chromium's message-loop Window isn't created synchronously, so this may not find it.
/// If so, you need to wait and try again later.
/// In most cases you should use the <see cref="TryFindHandle(ChromiumWebBrowser, out IntPtr)"/> overload.
/// In most cases you should use the <see cref="TryFindHandle(IWebBrowser, out IntPtr)"/> overload.
/// </summary>
/// <param name="chromiumWebBrowserHandle"><see cref="ChromiumWebBrowser"/> control Handle</param>
/// <param name="chromeRenderWidgetHostClassName">class name used to match</param>
Expand Down
15 changes: 11 additions & 4 deletions CefSharp.WinForms/Handler/LifeSpanHandlerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ public class LifeSpanHandlerBuilder
{
private readonly LifeSpanHandler handler;

/// <summary>
/// LifeSpanHandlerBuilder
/// </summary>
/// <param name="chromiumHostControlCreatedDelegate">
/// When specified the delegate will be used to create the <see cref="Host.ChromiumHostControl"/>
/// instance. Allowing users to create their own custom instance that extends <see cref="Host.ChromiumHostControl"/>
/// </param>
public LifeSpanHandlerBuilder(CreatePopupChromiumHostControl chromiumHostControlCreatedDelegate)
{
handler = new LifeSpanHandler(chromiumHostControlCreatedDelegate);
Expand All @@ -30,9 +37,9 @@ public LifeSpanHandlerBuilder OnBeforePopupCreated(OnBeforePopupCreatedDelegate
}

/// <summary>
/// The <see cref="OnPopupCreatedDelegate"/> will be called when the<see cref="ChromiumHostControl"/> has been
/// The <see cref="OnPopupCreatedDelegate"/> will be called when the<see cref="Host.ChromiumHostControl"/> has been
/// created. When the <see cref="OnPopupCreatedDelegate"/> is called you must add the control to it's intended parent
/// so the <see cref="Control.ClientRectangle"/> can be calculated to set the initial
/// so the <see cref="System.Windows.Forms.Control.ClientRectangle"/> can be calculated to set the initial
/// size correctly.
/// </summary>
/// <param name="onPopupCreated">Action to be invoked when the Popup is to be destroyed.</param>
Expand Down Expand Up @@ -60,9 +67,9 @@ public LifeSpanHandlerBuilder OnPopupBrowserCreated(OnPopupBrowserCreatedDelegat
}

/// <summary>
/// The <see cref="OnPopupDestroyedDelegate"/> will be called when the <see cref="ChromiumHostControl"/> is to be
/// The <see cref="OnPopupDestroyedDelegate"/> will be called when the <see cref="Host.ChromiumHostControl"/> is to be
/// removed from it's parent.
/// When the <see cref="OnPopupDestroyedDelegate"/> is called you must remove/dispose of the <see cref="ChromiumHostControl"/>.
/// When the <see cref="OnPopupDestroyedDelegate"/> is called you must remove/dispose of the <see cref="Host.ChromiumHostControl"/>.
/// </summary>
/// <param name="onPopupDestroyed">Action to be invoked when the Popup is to be destroyed.</param>
/// <returns><see cref="LifeSpanHandlerBuilder"/> instance allowing you to chain method calls together</returns>
Expand Down
2 changes: 2 additions & 0 deletions CefSharp.WinForms/Host/ChromiumHostControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public void LoadUrl(string url)
}
}

/// <inheritdoc/>
public Task<LoadUrlAsyncResponse> LoadUrlAsync(string url)
{
//LoadUrlAsync is actually a static method so that CefSharp.Wpf.HwndHost can reuse the code
Expand All @@ -264,6 +265,7 @@ public IFrame GetMainFrame()
return browser.MainFrame;
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (disposing)
Expand Down
4 changes: 4 additions & 0 deletions CefSharp.WinForms/WebBrowserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public static bool DestroyWindow(this IChromiumWebBrowserBase chromiumWebBrowser
/// </summary>
/// <param name="chromiumWebBrowser"><see cref="ChromiumWebBrowser"/> instance</param>
/// <param name="parentControl">Control used as the parent for DevTools (a custom control will be added to the <see cref="Control.Controls"/> collection)</param>
/// <param name="controlName">Control name</param>
/// <param name="dockStyle">Dock Style</param>
/// <param name="inspectElementAtX">x coordinate (used for inspectElement)</param>
/// <param name="inspectElementAtY">y coordinate (used for inspectElement)</param>
/// <returns>Returns the <see cref="Control"/> that hosts the DevTools instance if successful, otherwise returns null on error.</returns>
Expand All @@ -92,6 +94,8 @@ public static Control ShowDevToolsDocked(this IChromiumWebBrowserBase chromiumWe
/// Action that is Invoked when the DevTools Host Control has been created and needs to be added to it's parent.
/// It's important the control is added to it's intended parent at this point so the <see cref="Control.ClientRectangle"/>
/// can be calculated to set the initial display size.</param>
/// <param name="controlName">control name</param>
/// <param name="dockStyle">Dock Style</param>
/// <param name="inspectElementAtX">x coordinate (used for inspectElement)</param>
/// <param name="inspectElementAtY">y coordinate (used for inspectElement)</param>
/// <returns>Returns the <see cref="Control"/> that hosts the DevTools instance if successful, otherwise returns null on error.</returns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace CefSharp.Wpf.Rendering
{
/// <summary>
/// AllocHGlobalWritableBitmapRenderHandler - creates/updates an WritableBitmap
/// Uses <see cref="Marshal.AllocHGlobal"/> to allocate memory for
/// Uses <see cref="Marshal.AllocHGlobal(int)"/> to allocate memory for
/// double buffering when the size matches or creates a new WritableBitmap
/// when required.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public class CompositionTargetRenderHandler : IRenderHandler
private readonly object lockObj = new object();
private ChromiumWebBrowser browser;

/// <summary>
/// Default constructor
/// </summary>
/// <param name="browser">ChromiumWebBrowser instance</param>
/// <param name="dpiX">DPI X</param>
/// <param name="dpiY">DPI Y</param>
public CompositionTargetRenderHandler(ChromiumWebBrowser browser, double dpiX, double dpiY)
{
this.browser = browser;
Expand Down
5 changes: 4 additions & 1 deletion CefSharp/Callback/NoOpCompletionCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ namespace CefSharp.Callback
/// that does nothing with complete.
/// Added to workaround a CEF bug as per https://github.com/cefsharp/CefSharp/issues/2957#issuecomment-555285400
/// </summary>
public class NoOpCompletionCallback : ICompletionCallback
public sealed class NoOpCompletionCallback : ICompletionCallback
{
/// <inheritdoc/>
void ICompletionCallback.OnComplete()
{

}

/// <inheritdoc/>
bool ICompletionCallback.IsDisposed
{
//For now we return false
get { return false; }
}

/// <inheritdoc/>
void IDisposable.Dispose()
{

Expand Down
2 changes: 1 addition & 1 deletion CefSharp/Callback/TaskCompletionCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace CefSharp
/// <summary>
/// Provides a callback implementation of <see cref="ICompletionCallback"/>.
/// </summary>
public class TaskCompletionCallback : ICompletionCallback
public sealed class TaskCompletionCallback : ICompletionCallback
{
private readonly TaskCompletionSource<bool> taskCompletionSource;
private volatile bool isDisposed;
Expand Down
4 changes: 2 additions & 2 deletions CefSharp/CefLibraryHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public CefLibraryHandle(string path) : base(IntPtr.Zero, true)
/// </value>
public override bool IsInvalid
{
get { return this.handle == IntPtr.Zero; }
get { return handle == IntPtr.Zero; }
}

/// <summary>
Expand All @@ -67,7 +67,7 @@ public override bool IsInvalid
/// </returns>
protected override bool ReleaseHandle()
{
return FreeLibrary(this.handle);
return FreeLibrary(handle);
}

[DllImport("kernel32.dll", SetLastError = true)]
Expand Down
4 changes: 4 additions & 0 deletions CefSharp/CefSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Remove="DevTools\DevToolsClient.Generated.netcore.cs" />
Expand Down
4 changes: 4 additions & 0 deletions CefSharp/CefSharp.netcore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Text.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion CefSharp/DevTools/DevToolsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ private static T DeserializeJson<T>(Stream stream)
/// </summary>
/// <param name="type">Object type</param>
/// <param name="stream">JSON stream</param>
/// <returns>object of type <typeparamref name="type"/></returns>
/// <returns>object of type <paramref name="type"/></returns>
private static object DeserializeJson(Type type, Stream stream)
{
#if NETCOREAPP
Expand Down
29 changes: 29 additions & 0 deletions CefSharp/DevTools/DevToolsDomainBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,28 @@

namespace CefSharp.DevTools
{
/// <summary>
/// DevTools Domain base class
/// Provides some basic helper methods
/// </summary>
public abstract class DevToolsDomainBase
{
#if NETCOREAPP
/// <summary>
/// Convert Enum to String
/// </summary>
/// <param name="val">enum</param>
/// <returns>string</returns>
protected string EnumToString(Enum val)
{
return Internals.Json.JsonEnumConverterFactory.ConvertEnumToString(val);
}

/// <summary>
/// Enum to string
/// </summary>
/// <param name="values">array of type <see cref="CefSharp.DevTools.Network.ContentEncoding"/></param>
/// <returns>enumerable string</returns>
protected IEnumerable<string> EnumToString(CefSharp.DevTools.Network.ContentEncoding[] values)
{
foreach (var val in values)
Expand All @@ -27,6 +41,11 @@ protected IEnumerable<string> EnumToString(CefSharp.DevTools.Network.ContentEnco
}
}

/// <summary>
/// Enum to string
/// </summary>
/// <param name="values">array of type <see cref="CefSharp.DevTools.Emulation.DisabledImageType"/></param>
/// <returns>enumerable string</returns>
protected IEnumerable<string> EnumToString(CefSharp.DevTools.Emulation.DisabledImageType[] values)
{
foreach (var val in values)
Expand All @@ -35,6 +54,11 @@ protected IEnumerable<string> EnumToString(CefSharp.DevTools.Emulation.DisabledI
}
}

/// <summary>
/// Enum to string
/// </summary>
/// <param name="values">array of type <see cref="PermissionType"/></param>
/// <returns>enumerable string</returns>
protected IEnumerable<string> EnumToString(PermissionType[] values)
{
foreach (var val in values)
Expand All @@ -43,6 +67,11 @@ protected IEnumerable<string> EnumToString(PermissionType[] values)
}
}

/// <summary>
/// Enum to string
/// </summary>
/// <param name="values">array of type <see cref="CefSharp.DevTools.DOMDebugger.CSPViolationType"/></param>
/// <returns>enumerable string</returns>
protected IEnumerable<string> EnumToString(CefSharp.DevTools.DOMDebugger.CSPViolationType[] values)
{
foreach (var val in values)
Expand Down
3 changes: 3 additions & 0 deletions CefSharp/DevTools/DevToolsDomainEventArgsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace CefSharp.DevTools
{
/// <summary>
/// DevToolsDomainEventArgsBase
/// </summary>
[DataContract]
public abstract class DevToolsDomainEventArgsBase : EventArgs
{
Expand Down
8 changes: 8 additions & 0 deletions CefSharp/DevTools/DevToolsDomainResponseBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@

namespace CefSharp.DevTools
{
/// <summary>
/// DevToolsDomainResponseBase
/// </summary>
[System.Runtime.Serialization.DataContractAttribute]
public abstract class DevToolsDomainResponseBase
{
/// <summary>
/// Convert from string to base64 byte array
/// </summary>
/// <param name="data">string data</param>
/// <returns>byte array</returns>
public byte[] Convert(string data)
{
return System.Convert.FromBase64String(data);
Expand Down
4 changes: 4 additions & 0 deletions CefSharp/DevTools/Headers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public Headers() : base(StringComparer.OrdinalIgnoreCase)
{
}

/// <summary>
/// Returns itself
/// </summary>
/// <returns>Dictionary of headers</returns>
public Dictionary<string, string> ToDictionary()
{
return this;
Expand Down
3 changes: 3 additions & 0 deletions CefSharp/DevTools/MemoryDumpConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace CefSharp.DevTools.Tracing
{
/// <summary>
/// MemoryDumpConfig
/// </summary>
public class MemoryDumpConfig
{
}
Expand Down
4 changes: 2 additions & 2 deletions CefSharp/Handler/IRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public interface IRequestHandler
{
/// <summary>
/// Called before browser navigation.
/// If the navigation is allowed <see cref="IWebBrowser.FrameLoadStart"/> and <see cref="IWebBrowser.FrameLoadEnd"/>
/// will be called. If the navigation is canceled <see cref="IWebBrowser.LoadError"/> will be called with an ErrorCode
/// If the navigation is allowed <see cref="IChromiumWebBrowserBase.FrameLoadStart"/> and <see cref="IChromiumWebBrowserBase.FrameLoadEnd"/>
/// will be called. If the navigation is canceled <see cref="IChromiumWebBrowserBase.LoadError"/> will be called with an ErrorCode
/// value of <see cref="CefErrorCode.Aborted"/>.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
Expand Down
6 changes: 3 additions & 3 deletions CefSharp/Handler/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ bool IRequestHandler.OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser bro
}

/// <summary>
/// Called before browser navigation. If the navigation is allowed <see cref="IWebBrowser.FrameLoadStart"/> and
/// <see cref="IWebBrowser.FrameLoadEnd"/>
/// will be called. If the navigation is canceled <see cref="IWebBrowser.LoadError"/> will be called with an ErrorCode value of
/// Called before browser navigation. If the navigation is allowed <see cref="IChromiumWebBrowserBase.FrameLoadStart"/> and
/// <see cref="IChromiumWebBrowserBase.FrameLoadEnd"/>
/// will be called. If the navigation is canceled <see cref="IChromiumWebBrowserBase.LoadError"/> will be called with an ErrorCode value of
/// <see cref="CefErrorCode.Aborted"/>.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control.</param>
Expand Down
13 changes: 10 additions & 3 deletions CefSharp/Internals/ConcurrentMethodRunnerQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,32 @@ namespace CefSharp.Internals
/// the we use ContinueWith to be notified of completion then
/// raise the MethodInvocationComplete event
/// </summary>
public class ConcurrentMethodRunnerQueue : IMethodRunnerQueue
public sealed class ConcurrentMethodRunnerQueue : IMethodRunnerQueue
{
private static Type VoidTaskResultType = Type.GetType("System.Threading.Tasks.VoidTaskResult");
private static readonly Type VoidTaskResultType = Type.GetType("System.Threading.Tasks.VoidTaskResult");
private readonly IJavascriptObjectRepositoryInternal repository;
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

/// <inheritdoc/>
public event EventHandler<MethodInvocationCompleteArgs> MethodInvocationComplete;

/// <summary>
/// Default constructor
/// </summary>
/// <param name="repository">javascript object repository</param>
public ConcurrentMethodRunnerQueue(IJavascriptObjectRepositoryInternal repository)
{
this.repository = repository;
}

/// <inheritdoc/>
public void Dispose()
{
MethodInvocationComplete = null;
cancellationTokenSource.Cancel();
}

/// <inheritdoc/>
public void Enqueue(MethodInvocation methodInvocation)
{
if (cancellationTokenSource.IsCancellationRequested)
Expand Down
10 changes: 10 additions & 0 deletions CefSharp/Internals/IMethodRunnerQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@

namespace CefSharp.Internals
{
/// <summary>
/// Run (execute) calls made from Javascript to .Net methods
/// </summary>
public interface IMethodRunnerQueue : IDisposable
{
/// <summary>
/// Method invocation was completed.
/// </summary>
event EventHandler<MethodInvocationCompleteArgs> MethodInvocationComplete;

/// <summary>
/// Enqueue a method invocation
/// </summary>
/// <param name="methodInvocation">method invocation</param>
void Enqueue(MethodInvocation methodInvocation);
}
}
Loading

0 comments on commit 3b1c46a

Please sign in to comment.