Skip to content

Commit fb81c70

Browse files
committed
Refactor in preparation for adding WPF implementation
1 parent c106d9f commit fb81c70

12 files changed

Lines changed: 103 additions & 28 deletions
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<RootNamespace>CefSharp.OutOfProcess</RootNamespace>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="PInvoke.Kernel32" Version="0.7.104" />
10+
<PackageReference Include="PInvoke.User32" Version="0.7.104" />
11+
<PackageReference Include="StreamJsonRpc" Version="2.11.35" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace CefSharp.OutOfProcess
4+
{
5+
/// <summary>
6+
/// OutOfProcess ChromiumWebBrowser
7+
/// </summary>
8+
public interface IChromiumWebBrowser
9+
{
10+
/// <summary>
11+
/// Identifier
12+
/// </summary>
13+
int Id { get; set; }
14+
15+
/// <summary>
16+
/// Set the browser Hwnd
17+
/// </summary>
18+
/// <param name="hwnd">Hwnd</param>
19+
void SetBrowserHwnd(IntPtr hwnd);
20+
}
21+
}

CefSharp.OutOfProcess.Example/OutOfProcessHost.cs renamed to CefSharp.OutOfProcess.Core/OutOfProcessHost.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
using System;
44
using System.Collections.Concurrent;
55
using System.Diagnostics;
6+
using System.IO;
67
using System.Threading.Tasks;
78

8-
namespace CefSharp.OutOfProcess.Example
9+
namespace CefSharp.OutOfProcess
910
{
1011
public class OutOfProcessHost : IDisposable
1112
{
@@ -14,15 +15,16 @@ public class OutOfProcessHost : IDisposable
1415
private int _uiThreadId;
1516
private int _remoteuiThreadId;
1617
private int _browserIdentifier = 1;
17-
private ConcurrentDictionary<int, ChromiumWebBrowser> _browsers = new ConcurrentDictionary<int, ChromiumWebBrowser>();
18+
private string _outofProcessFilePath;
19+
private ConcurrentDictionary<int, IChromiumWebBrowser> _browsers = new ConcurrentDictionary<int, IChromiumWebBrowser>();
1820
private TaskCompletionSource<OutOfProcessHost> _processInitialized = new TaskCompletionSource<OutOfProcessHost>(TaskCreationOptions.RunContinuationsAsynchronously);
1921

20-
private OutOfProcessHost()
22+
private OutOfProcessHost(string path)
2123
{
22-
24+
_outofProcessFilePath = path;
2325
}
2426

25-
public bool CreateBrowser(ChromiumWebBrowser browser, IntPtr handle, string url)
27+
public bool CreateBrowser(IChromiumWebBrowser browser, IntPtr handle, string url)
2628
{
2729
var id = _browserIdentifier++;
2830
_ = _jsonRpc.NotifyAsync("CreateBrowser", handle.ToInt32(), url, id);
@@ -43,7 +45,7 @@ private void Init()
4345

4446
var args = $"--parentProcessId={currentProcess.Id}";
4547

46-
_browserProcess = Process.Start(new ProcessStartInfo("CefSharp.OutOfProcess.BrowserProcess.exe", args)
48+
_browserProcess = Process.Start(new ProcessStartInfo(_outofProcessFilePath, args)
4749
{
4850
RedirectStandardInput = true,
4951
RedirectStandardOutput = true,
@@ -76,7 +78,7 @@ private void Init()
7678
_jsonRpc.AllowModificationWhileListening = false;
7779
}
7880

79-
internal void CloseBrowser(int id)
81+
public void CloseBrowser(int id)
8082
{
8183
_ = _jsonRpc?.NotifyAsync("CloseBrowser", id);
8284
}
@@ -88,9 +90,9 @@ public void Dispose()
8890
_jsonRpc = null;
8991
}
9092

91-
public static Task<OutOfProcessHost> CreateAsync()
93+
public static Task<OutOfProcessHost> CreateAsync(string path = "CefSharp.OutOfProcess.BrowserProcess.exe")
9294
{
93-
var host = new OutOfProcessHost();
95+
var host = new OutOfProcessHost(Path.GetFullPath(path));
9496

9597
host.Init();
9698

CefSharp.OutOfProcess.Example/CefSharp.OutOfProcess.Example.csproj renamed to CefSharp.OutOfProcess.WinForms.Example/CefSharp.OutOfProcess.WinForms.Example.csproj

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="PInvoke.Kernel32" Version="0.7.104" />
14-
<PackageReference Include="PInvoke.User32" Version="0.7.104" />
15-
</ItemGroup>
16-
17-
<ItemGroup>
18-
<ProjectReference Include="..\CefSharp.OutOfProcess.BrowserProcess\CefSharp.OutOfProcess.BrowserProcess.csproj" />
13+
<ProjectReference Include="..\CefSharp.OutOfProcess.WinForms\CefSharp.OutOfProcess.WinForms.csproj" />
1914
</ItemGroup>
2015

2116
</Project>

CefSharp.OutOfProcess.Example/HostForm.Designer.cs renamed to CefSharp.OutOfProcess.WinForms.Example/HostForm.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CefSharp.OutOfProcess.Example/HostForm.cs renamed to CefSharp.OutOfProcess.WinForms.Example/HostForm.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System;
1+
using CefSharp.OutOfProcess;
2+
using System;
23
using System.Windows.Forms;
34

4-
namespace CefSharp.OutOfProcess.Example
5+
namespace CefSharp.OutOfProcess.WinForms.Example
56
{
67
public partial class HostForm : Form
78
{
File renamed without changes.

CefSharp.OutOfProcess.Example/Program.cs renamed to CefSharp.OutOfProcess.WinForms.Example/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Windows.Forms;
33

4-
namespace CefSharp.OutOfProcess.Example
4+
namespace CefSharp.OutOfProcess.WinForms.Example
55
{
66
static class Program
77
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<UseWindowsForms>true</UseWindowsForms>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="PInvoke.Kernel32" Version="0.7.104" />
10+
<PackageReference Include="PInvoke.User32" Version="0.7.104" />
11+
<PackageReference Include="StreamJsonRpc" Version="2.11.35" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\CefSharp.OutOfProcess.Core\CefSharp.OutOfProcess.Core.csproj" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<Compile Update="ChromiumWebBrowser.cs">
20+
<SubType>Component</SubType>
21+
</Compile>
22+
</ItemGroup>
23+
24+
</Project>

CefSharp.OutOfProcess.Example/ChromiumWebBrowser.cs renamed to CefSharp.OutOfProcess.WinForms/ChromiumWebBrowser.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
using System;
22
using System.Drawing;
33
using System.Windows.Forms;
4+
using CefSharp.OutOfProcess;
45
using PInvoke;
56

6-
namespace CefSharp.OutOfProcess.Example
7+
namespace CefSharp.OutOfProcess.WinForms
78
{
8-
public class ChromiumWebBrowser : Control
9+
public class ChromiumWebBrowser : Control, IChromiumWebBrowser
910
{
1011
private IntPtr _browserHwnd = IntPtr.Zero;
1112
private int _remoteThreadId = -1;
1213
private int _uiThreadId = -1;
1314
private bool _disposed;
1415
private OutOfProcessHost _host;
1516
private readonly string _initialAddress;
17+
private int _id;
1618

17-
internal int Id { get; set; }
19+
int IChromiumWebBrowser.Id
20+
{
21+
get { return _id; }
22+
set { _id = value; }
23+
}
1824

1925
public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress)
2026
{
2127
_host = host;
2228
_initialAddress = initialAddress;
2329
}
2430

25-
internal void SetBrowserHwnd(IntPtr hwnd)
31+
void IChromiumWebBrowser.SetBrowserHwnd(IntPtr hwnd)
2632
{
2733
_browserHwnd = hwnd;
2834
}
@@ -62,7 +68,7 @@ protected override void Dispose(bool disposing)
6268
_browserHwnd = IntPtr.Zero;
6369
_disposed = true;
6470

65-
_host?.CloseBrowser(Id);
71+
_host?.CloseBrowser(_id);
6672
_host = null;
6773
}
6874
}

0 commit comments

Comments
 (0)