Skip to content

Commit

Permalink
Master (#4)
Browse files Browse the repository at this point in the history
* - Reorganized folder hierarchy
- Added Solution file
- Updated Nuget

* - Updated AssemblyInfo
- Updated XML Comments
- Added Nuspec file to create a NuGet package

* - Added test application
  • Loading branch information
JereckNET authored Jul 5, 2020
1 parent 0474488 commit 527bec8
Show file tree
Hide file tree
Showing 19 changed files with 617 additions and 125 deletions.
410 changes: 360 additions & 50 deletions .gitignore

Large diffs are not rendered by default.

19 changes: 0 additions & 19 deletions Exceptions.cs

This file was deleted.

31 changes: 31 additions & 0 deletions NativeMessaging.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30204.135
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NativeMessaging", "NativeMessaging\NativeMessaging.csproj", "{7617A281-576A-4478-BFA7-44530AC2ED6C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NativeMessagingTest", "NativeMessagingTest\NativeMessagingTest.csproj", "{4F8A86C0-4F0B-469E-BF2A-B1E6A3C403BF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7617A281-576A-4478-BFA7-44530AC2ED6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7617A281-576A-4478-BFA7-44530AC2ED6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7617A281-576A-4478-BFA7-44530AC2ED6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7617A281-576A-4478-BFA7-44530AC2ED6C}.Release|Any CPU.Build.0 = Release|Any CPU
{4F8A86C0-4F0B-469E-BF2A-B1E6A3C403BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F8A86C0-4F0B-469E-BF2A-B1E6A3C403BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F8A86C0-4F0B-469E-BF2A-B1E6A3C403BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F8A86C0-4F0B-469E-BF2A-B1E6A3C403BF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CC8CA26A-ABD0-4BEC-90D4-19A6DAB2D068}
EndGlobalSection
EndGlobal
22 changes: 22 additions & 0 deletions NativeMessaging/Exceptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NativeMessaging
{
/// <summary>
/// Exception raised when trying to interact with chrome while the extension is not registered in the Windows Registry
/// </summary>
[Serializable]
public class NotRegisteredWithChromeException : Exception
{
internal NotRegisteredWithChromeException() { }
internal NotRegisteredWithChromeException(string message) : base(message) { }
internal NotRegisteredWithChromeException(string message, Exception inner) : base(message, inner) { }
internal NotRegisteredWithChromeException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
}
29 changes: 19 additions & 10 deletions Host.cs → NativeMessaging/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@

namespace NativeMessaging
{
/// <summary>
/// Abstract class that should be extended to communicate with Chrome
/// </summary>
public abstract class Host
{
/// <summary>
/// Name of the Native Messaging Host
/// </summary>
public abstract string Hostname { get; }

private readonly bool SendConfirmationReceipt;
Expand All @@ -24,8 +30,7 @@ public abstract class Host
/// <summary>
/// Creates the Host Object
/// </summary>
/// <param name="hostname"></param>
/// <param name="sendConfirmationReceipt"></param>
/// <param name="sendConfirmationReceipt"><see langword="true" /> for the host to automatically send message confirmation receipt.</param>
public Host(bool sendConfirmationReceipt = true)
{
SendConfirmationReceipt = sendConfirmationReceipt;
Expand Down Expand Up @@ -65,7 +70,7 @@ private JObject Read()
/// <summary>
/// Sends a message to Chrome, note that the message might not be able to reach Chrome if the stdIn / stdOut aren't properly configured (i.e. Process needs to be started by Chrome)
/// </summary>
/// <param name="data"></param>
/// <param name="data">A <see cref="JObject"/> containing the data to be sent.</param>
public void SendMessage(JObject data)
{
Utils.LogMessage("Sending Message:" + JsonConvert.SerializeObject(data));
Expand All @@ -79,14 +84,17 @@ public void SendMessage(JObject data)
stdout.Flush();
}


/// <summary>
/// Override this method in your extended <see cref="Host"/> to process messages received from Chrome.
/// </summary>
/// <param name="data">A <see cref="JObject"/> containing the data received.</param>
protected abstract void ProcessReceivedMessage(JObject data);

/// <summary>
/// Generates the manifest & saves it to the correct location.
/// Generates the manifest and saves it to the correct location.
/// </summary>
/// <param name="description"></param>
/// <param name="allowedOrigins"></param>
/// <param name="description">Short application description to be included in the manifest.</param>
/// <param name="allowedOrigins">List of extensions that should have access to the native messaging host.<br />Wildcards such as <code>chrome-extension://*/*</code> are not allowed.</param>
public void GenerateManifest(string description, string[] allowedOrigins)
{
Utils.LogMessage("Generating Manifest");
Expand All @@ -111,6 +119,10 @@ public void Register()
Utils.LogMessage("Registered:" + Hostname);
}

/// <summary>
/// Checks if the host is registered with Chrome
/// </summary>
/// <returns><see langword="true"/> if the required information is present in the registry.</returns>
public bool IsRegisteredWithChrome()
{
var regHostnameKeyLocation = RegKeyBaseLocation + Hostname;
Expand All @@ -121,7 +133,6 @@ public bool IsRegisteredWithChrome()
return false;
}


/// <summary>
/// De-register the application to open with Chrome.
/// </summary>
Expand All @@ -135,6 +146,4 @@ public void UnRegister()
Utils.LogMessage("Unregistered:" + Hostname);
}
}


}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\NativeMessaging.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -28,20 +29,14 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\NativeMessaging.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exceptions.cs" />
Expand All @@ -52,6 +47,7 @@
<Compile Include="Utils.cs" />
</ItemGroup>
<ItemGroup>
<None Include="NativeMessaging.nuspec" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
16 changes: 16 additions & 0 deletions NativeMessaging/NativeMessaging.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>Alex Newton Levey</authors>
<owners>anewtonlevey</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="expression">MIT</license>
<description>C# Chome Native Messaging Library</description>
<copyright>$copyright$</copyright>
<tags>Chrome Extension Chromium Native Messaging</tags>
<repository type="git" url="https://github.com/anewtonlevey/NativeMessaging" />
</metadata>
</package>
14 changes: 14 additions & 0 deletions NativeMessaging/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Chromium Native Messaging Host")]
[assembly: AssemblyDescription("C# Chome Native Messaging Library")]
[assembly: AssemblyProduct("Chromium Native Messaging Host")]
[assembly: AssemblyCopyright("Copyright © Alex Newton Levey 2017")]

[assembly: ComVisible(false)]

[assembly: Guid("7617a281-576a-4478-bfa7-44530ac2ed6c")]

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion packages.config → NativeMessaging/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net452" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net452" />
</packages>
6 changes: 6 additions & 0 deletions NativeMessagingTest/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
23 changes: 23 additions & 0 deletions NativeMessagingTest/MyHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NativeMessaging;
using Newtonsoft.Json.Linq;

namespace NativeMessagingTest {
public class MyHost : Host {
// This can currently be used with the example app provided here:
// https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/nativeMessaging

private const bool SendConfirmationReceipt = true;

public override string Hostname {
get { return "com.google.chrome.example.echo"; }
}

public MyHost() : base(SendConfirmationReceipt) {

}

protected override void ProcessReceivedMessage(JObject data) {
SendMessage(data);
}
}
}
58 changes: 58 additions & 0 deletions NativeMessagingTest/NativeMessagingTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4F8A86C0-4F0B-469E-BF2A-B1E6A3C403BF}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>NativeMessagingTest</RootNamespace>
<AssemblyName>NativeMessagingTest</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="MyHost.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NativeMessaging\NativeMessaging.csproj">
<Project>{7617a281-576a-4478-bfa7-44530ac2ed6c}</Project>
<Name>NativeMessaging</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
43 changes: 43 additions & 0 deletions NativeMessagingTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using NativeMessaging;

namespace NativeMessagingTest {
class Program {
static public string AssemblyLoadDirectory {
get {
string codeBase = Assembly.GetEntryAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}

static public string AssemblyExecuteablePath {
get {
string codeBase = Assembly.GetEntryAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
return Uri.UnescapeDataString(uri.Path);
}
}

static Host Host;

static string[] AllowedOrigins = new string[] { "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/" };
static string Description = "Description Goes Here";

static void Main(string[] args) {
Host = new MyHost();
if (args.Contains("--register")) {
Host.GenerateManifest(Description, AllowedOrigins);
Host.Register();
} else if (args.Contains("--unregister")) {
Host.UnRegister();
} else {
Host.Listen();
}
}
}
}
15 changes: 15 additions & 0 deletions NativeMessagingTest/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Chromium Native Messaging Host Test Project")]
[assembly: AssemblyDescription("C# Chome Native Messaging Library")]
[assembly: AssemblyProduct("Chromium Native Messaging Host")]
[assembly: AssemblyCopyright("Copyright © Alex Newton Levey 2017")]

[assembly: ComVisible(false)]

[assembly: Guid("4f8a86c0-4f0b-469e-bf2a-b1e6a3c403bf")]

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

4 changes: 4 additions & 0 deletions NativeMessagingTest/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net452" />
</packages>
Loading

0 comments on commit 527bec8

Please sign in to comment.