Skip to content

Commit

Permalink
WPF/WinForms/OffScreen - Reduce Code Duplication Initial
Browse files Browse the repository at this point in the history
- Add ChromiumWebBrowser.Partial.cs and include in all three projects
- Remove InternalsVisibleTo from Cefsharp.dll start moving code into the partial class
  (We don't want to publish our internals as part of our Ref Assembly so this goings together)

#3082
#3197
  • Loading branch information
amaitland committed Aug 7, 2020
1 parent 2a96ebc commit 0183c42
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 54 deletions.
11 changes: 7 additions & 4 deletions CefSharp.OffScreen/CefSharp.OffScreen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>DEBUG;TRACE;OFFSCREEN</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
Expand All @@ -27,7 +27,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;OFFSCREEN</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
Expand All @@ -39,7 +39,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>DEBUG;TRACE;OFFSCREEN</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
Expand All @@ -50,7 +50,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;OFFSCREEN</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
Expand All @@ -75,6 +75,9 @@
<Compile Include="BitmapBuffer.cs" />
<Compile Include="CefSettings.cs" />
<Compile Include="ChromiumWebBrowser.cs" />
<Compile Include="..\CefSharp\Internals\Partial\ChromiumWebBrowser.Partial.cs">
<Link>ChromiumWebBrowser.Partial.cs</Link>
</Compile>
<Compile Include="DefaultRenderHandler.cs" />
<Compile Include="IRenderHandler.cs" />
<Compile Include="OnPaintEventArgs.cs" />
Expand Down
5 changes: 5 additions & 0 deletions CefSharp.OffScreen/CefSharp.OffScreen.netcore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\CefSharp.snk</AssemblyOriginatorKeyFile>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<DefineConstants>OFFSCREEN</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand All @@ -33,5 +34,9 @@
<Compile Remove="obj/**/*.*" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\CefSharp\Internals\Partial\ChromiumWebBrowser.Partial.cs" Link="ChromiumWebBrowser.Partial.cs" />
</ItemGroup>

<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.WindowsDesktop" />
</Project>
2 changes: 1 addition & 1 deletion CefSharp.OffScreen/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace CefSharp.OffScreen
/// An offscreen instance of Chromium that you can use to take
/// snapshots or evaluate JavaScript.
/// </summary>
public class ChromiumWebBrowser : IRenderWebBrowser
public partial class ChromiumWebBrowser : IRenderWebBrowser
{
/// <summary>
/// The managed cef browser adapter
Expand Down
8 changes: 8 additions & 0 deletions CefSharp.WinForms/CefSharp.WinForms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PlatformTarget>x64</PlatformTarget>
<OutputPath>bin\x64\Debug\</OutputPath>
<Prefer32Bit>false</Prefer32Bit>
<DefineConstants>TRACE;DEBUG;WINFORMS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<PlatformTarget>x64</PlatformTarget>
Expand All @@ -27,6 +28,7 @@
<DebugSymbols>true</DebugSymbols>
<Optimize>true</Optimize>
<Prefer32Bit>false</Prefer32Bit>
<DefineConstants>TRACE;WINFORMS</DefineConstants>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
Expand All @@ -40,6 +42,7 @@
<PlatformTarget>x86</PlatformTarget>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
<DefineConstants>TRACE;DEBUG;WINFORMS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
Expand All @@ -50,6 +53,7 @@
<DebugSymbols>true</DebugSymbols>
<DocumentationFile>bin\x86\Release\CefSharp.WinForms.XML</DocumentationFile>
<Prefer32Bit>false</Prefer32Bit>
<DefineConstants>TRACE;WINFORMS</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand All @@ -65,6 +69,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\CefSharp\Internals\Partial\ChromiumWebBrowser.Partial.cs">
<Link>ChromiumWebBrowser.Partial.cs</Link>
<SubType>Component</SubType>
</Compile>
<Compile Include="CefSettings.cs" />
<Compile Include="ChromiumWebBrowserDesigner.cs" />
<Compile Include="Internals\DefaultFocusHandler.cs" />
Expand Down
5 changes: 5 additions & 0 deletions CefSharp.WinForms/CefSharp.WinForms.netcore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\CefSharp.snk</AssemblyOriginatorKeyFile>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<DefineConstants>WINFORMS</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand All @@ -41,5 +42,9 @@
<Compile Remove="obj/**/*.*" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\CefSharp\Internals\Partial\ChromiumWebBrowser.Partial.cs" Link="ChromiumWebBrowser.Partial.cs" />
</ItemGroup>

<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.WindowsDesktop" />
</Project>
2 changes: 1 addition & 1 deletion CefSharp.WinForms/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace CefSharp.WinForms
[Docking(DockingBehavior.AutoDock), DefaultEvent("LoadingStateChanged"), ToolboxBitmap(typeof(ChromiumWebBrowser)),
Description("CefSharp ChromiumWebBrowser - Chromium Embedded Framework .Net wrapper. https://github.com/cefsharp/CefSharp"),
Designer(typeof(ChromiumWebBrowserDesigner))]
public class ChromiumWebBrowser : Control, IWebBrowserInternal, IWinFormsWebBrowser
public partial class ChromiumWebBrowser : Control, IWebBrowserInternal, IWinFormsWebBrowser
{
//TODO: If we start adding more consts then extract them into a common class
//Possibly in the CefSharp assembly and move the WPF ones into there as well.
Expand Down
11 changes: 7 additions & 4 deletions CefSharp.Wpf/CefSharp.Wpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG;WPF</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
Expand All @@ -35,7 +35,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;WPF</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
Expand All @@ -47,7 +47,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG;WPF</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
Expand All @@ -56,7 +56,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;WPF</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
Expand All @@ -78,6 +78,9 @@
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\CefSharp\Internals\Partial\ChromiumWebBrowser.Partial.cs">
<Link>ChromiumWebBrowser.Partial.cs</Link>
</Compile>
<Compile Include="CefSettings.cs" />
<Compile Include="Experimental\Accessibility\AccessibilityHandler.cs" />
<Compile Include="Experimental\Accessibility\AccessibilityNode.cs" />
Expand Down
5 changes: 5 additions & 0 deletions CefSharp.Wpf/CefSharp.Wpf.netcore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\CefSharp.snk</AssemblyOriginatorKeyFile>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<DefineConstants>WPF</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand All @@ -33,5 +34,9 @@
<Compile Remove="obj/**/*.*" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\CefSharp\Internals\Partial\ChromiumWebBrowser.Partial.cs" Link="ChromiumWebBrowser.Partial.cs" />
</ItemGroup>

<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.WindowsDesktop" />
</Project>
2 changes: 1 addition & 1 deletion CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace CefSharp.Wpf
/// <seealso cref="CefSharp.Wpf.IWpfWebBrowser" />
[TemplatePart(Name = PartImageName, Type = typeof(Image))]
[TemplatePart(Name = PartPopupImageName, Type = typeof(Image))]
public class ChromiumWebBrowser : Control, IRenderWebBrowser, IWpfWebBrowser
public partial class ChromiumWebBrowser : Control, IRenderWebBrowser, IWpfWebBrowser
{
/// <summary>
/// TemplatePart Name constant for the Image used to represent the browser
Expand Down
4 changes: 3 additions & 1 deletion CefSharp/CefSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@
<Compile Include="Internals\JavascriptObjectRepository.cs" />
<Compile Include="Internals\JavascriptProperty.cs" />
<Compile Include="CefCustomScheme.cs" />
<Compile Include="Internals\InternalWebBrowserExtensions.cs" />
<Compile Include="Visitor\ICookieVisitor.cs" />
<Compile Include="Visitor\INavigationEntryVisitor.cs" />
<Compile Include="Visitor\IStringVisitor.cs" />
Expand All @@ -336,6 +335,9 @@
<Compile Include="Web\HtmlString.cs" />
<Compile Include="Web\JsonString.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Internals\Partial\Readme.md" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
4 changes: 4 additions & 0 deletions CefSharp/CefSharp.netcore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@
<Compile Remove="obj/**/*.*" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Internals\Partial\ChromiumWebBrowser.Partial.cs" />
</ItemGroup>

<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>
4 changes: 2 additions & 2 deletions CefSharp/DevToolsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public static Task<int> ExecuteDevToolsMethodAsync(this IBrowser browser, int me
/// unsuccessfully submitted for validation, this value will be 0.</returns>
public static Task<int> ExecuteDevToolsMethodAsync(this IWebBrowser chromiumWebBrowser, int messageId, string method, IDictionary<string, object> parameters = null)
{
chromiumWebBrowser.ThrowExceptionIfDisposed();
chromiumWebBrowser.ThrowExceptionIfBrowserNotInitialized();
((IWebBrowserInternal)chromiumWebBrowser).ThrowExceptionIfDisposed();
((IWebBrowserInternal)chromiumWebBrowser).ThrowExceptionIfBrowserNotInitialized();

var browser = chromiumWebBrowser.GetBrowser();

Expand Down
2 changes: 1 addition & 1 deletion CefSharp/Internals/IntPtrExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace CefSharp.Internals
{
internal static class IntPtrExtensions
public static class IntPtrExtensions
{
/// <summary>
/// Do an unchecked conversion from IntPtr to int
Expand Down
27 changes: 0 additions & 27 deletions CefSharp/Internals/InternalWebBrowserExtensions.cs

This file was deleted.

36 changes: 36 additions & 0 deletions CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright © 2020 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.


#if OFFSCREEN
namespace CefSharp.OffScreen
#elif WPF
namespace CefSharp.Wpf
#elif WINFORMS
namespace CefSharp.WinForms
#endif
{
//ChromiumWebBrowser Partial class implementation shared between the
//WPF, Winforms and Offscreen
public partial class ChromiumWebBrowser
{
private void SetHandlersToNullExceptLifeSpan()
{
AudioHandler = null;
DialogHandler = null;
FindHandler = null;
RequestHandler = null;
DisplayHandler = null;
LoadHandler = null;
KeyboardHandler = null;
JsDialogHandler = null;
DragHandler = null;
DownloadHandler = null;
MenuHandler = null;
FocusHandler = null;
ResourceRequestHandlerFactory = null;
RenderProcessMessageHandler = null;
}
}
}
3 changes: 3 additions & 0 deletions CefSharp/Internals/Partial/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The ChromiumWebBrowser class in this folder is not directly included in CefSharp.dll
it is shared between the WPF, OffScreen and WinForms implementations with #if def's
to allow for the partial class to be used between the three implementations.
9 changes: 0 additions & 9 deletions CefSharp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using CefSharp;

Expand All @@ -18,14 +17,6 @@
[assembly: AssemblyFileVersion(AssemblyInfo.AssemblyFileVersion)]
[assembly: CLSCompliant(AssemblyInfo.ClsCompliant)]

[assembly: InternalsVisibleTo(AssemblyInfo.CefSharpCoreProject)]
[assembly: InternalsVisibleTo(AssemblyInfo.CefSharpBrowserSubprocessProject)]
[assembly: InternalsVisibleTo(AssemblyInfo.CefSharpBrowserSubprocessCoreProject)]
[assembly: InternalsVisibleTo(AssemblyInfo.CefSharpWpfProject)]
[assembly: InternalsVisibleTo(AssemblyInfo.CefSharpWinFormsProject)]
[assembly: InternalsVisibleTo(AssemblyInfo.CefSharpOffScreenProject)]
[assembly: InternalsVisibleTo(AssemblyInfo.CefSharpTestProject)]

namespace CefSharp
{
/// <exclude />
Expand Down
6 changes: 3 additions & 3 deletions CefSharp/WebBrowserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace CefSharp
/// </summary>
public static class WebBrowserExtensions
{
internal const string BrowserNotInitializedExceptionErrorMessage =
public const string BrowserNotInitializedExceptionErrorMessage =
"The ChromiumWebBrowser instance creates the underlying Chromium Embedded Framework (CEF) browser instance in an async fashion. " +
"The undelying CefBrowser instance is not yet initialized. Use the IsBrowserInitializedChanged event and check " +
"the IsBrowserInitialized property to determine when the browser has been initialized.";
Expand Down Expand Up @@ -1169,7 +1169,7 @@ public static string GetScriptForJavascriptMethodWithArgs(string methodName, obj
/// </remarks>
/// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
/// <param name="browser">The ChromiumWebBrowser instance this method extends.</param>
internal static void ThrowExceptionIfBrowserNotInitialized(this IWebBrowser browser)
public static void ThrowExceptionIfBrowserNotInitialized(this IWebBrowserInternal browser)
{
if (!browser.IsBrowserInitialized)
{
Expand All @@ -1182,7 +1182,7 @@ internal static void ThrowExceptionIfBrowserNotInitialized(this IWebBrowser brow
/// </summary>
/// <exception cref="ObjectDisposedException">Thrown when a supplied object has been disposed.</exception>
/// <param name="browser">The ChromiumWebBrowser instance this method extends.</param>
internal static void ThrowExceptionIfDisposed(this IWebBrowser browser)
public static void ThrowExceptionIfDisposed(this IWebBrowserInternal browser)
{
if (browser.IsDisposed)
{
Expand Down

0 comments on commit 0183c42

Please sign in to comment.