Skip to content

Commit c1a19f5

Browse files
committed
fix: native messaging on Windows
1 parent a61d1ca commit c1a19f5

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
crxtesthost
22
crxtesthost.blob
3+
crxtesthost.exe

packages/electron-chrome-extensions/script/native-messaging-host/build.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,48 @@ async function createSEA() {
4242
async function installConfig(extensionIds) {
4343
console.info(`Installing config…`)
4444

45-
const name = 'com.crx.test'
46-
const config = {
47-
name,
45+
const hostName = 'com.crx.test'
46+
const manifest = {
47+
name: hostName,
4848
description: 'electron-chrome-extensions test',
4949
path: path.join(outDir, exeName),
5050
type: 'stdio',
5151
allowed_origins: extensionIds.map((id) => `chrome-extension://${id}/`),
5252
}
5353

54+
const writeManifest = async (manifestPath) => {
55+
await fs.mkdir(manifestPath, { recursive: true })
56+
const filePath = path.join(manifestPath, `${hostName}.json`)
57+
const data = Buffer.from(JSON.stringify(manifest, null, 2))
58+
await fs.writeFile(filePath, data)
59+
return filePath
60+
}
61+
5462
switch (process.platform) {
5563
case 'darwin': {
56-
const configPath = path.join(
64+
const manifestDir = path.join(
5765
os.homedir(),
5866
'Library',
5967
'Application Support',
6068
'Electron',
6169
'NativeMessagingHosts',
6270
)
63-
await fs.mkdir(configPath, { recursive: true })
64-
const filePath = path.join(configPath, `${name}.json`)
65-
const data = Buffer.from(JSON.stringify(config, null, 2))
66-
await fs.writeFile(filePath, data)
71+
await writeManifest(manifestDir)
72+
break
73+
}
74+
case 'win32': {
75+
const manifestDir = path.join(
76+
os.homedir(),
77+
'AppData',
78+
'Roaming',
79+
'Electron',
80+
'NativeMessagingHosts',
81+
)
82+
const manifestPath = await writeManifest(manifestDir)
83+
const registryKey = `HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts\\${hostName}`
84+
await exec(`reg add "${registryKey}" /ve /t REG_SZ /d "${manifestPath}" /f`, {
85+
stdio: 'inherit',
86+
})
6787
break
6888
}
6989
default:

packages/electron-chrome-extensions/spec/chrome-nativeMessaging-spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const exec = promisify(cp.exec)
88
import { useExtensionBrowser, useServer } from './hooks'
99
import { getExtensionId } from './crx-helpers'
1010

11-
// TODO: figure out why CI can't connect to native host
11+
// TODO: build crxtesthost on Linux (see script/native-messaging-host/build.js)
1212
if (process.platform !== 'linux') {
1313
describe('nativeMessaging', () => {
1414
const server = useServer()
@@ -21,7 +21,8 @@ if (process.platform !== 'linux') {
2121
before(async () => {
2222
const extensionId = await getExtensionId('rpc')
2323
const nativeMessagingPath = path.join(__dirname, '..', 'script', 'native-messaging-host')
24-
await exec(`${path.join(nativeMessagingPath, 'build.js')} ${extensionId}`)
24+
const buildScript = path.join(nativeMessagingPath, 'build.js')
25+
await exec(`node ${buildScript} ${extensionId}`)
2526
})
2627

2728
describe('sendNativeMessage()', () => {

packages/electron-chrome-extensions/src/browser/api/lib/native-messaging-host.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ async function getConfigSearchPaths(application: string) {
5858
case 'win32': {
5959
searchPaths = (
6060
await Promise.allSettled([
61-
readRegistryKey('HKLM', '\\Software\\Google\\Chrome\\NativeMessagingHosts', application),
62-
readRegistryKey('HKCU', '\\Software\\Google\\Chrome\\NativeMessagingHosts', application),
61+
readRegistryKey('HKLM', `Software\\Google\\Chrome\\NativeMessagingHosts\\${application}`),
62+
readRegistryKey('HKCU', `Software\\Google\\Chrome\\NativeMessagingHosts\\${application}`),
6363
])
6464
)
6565
.map((result) => (result.status === 'fulfilled' ? result.value : undefined))
@@ -76,6 +76,7 @@ async function readNativeMessagingHostConfig(
7676
application: string,
7777
): Promise<NativeConfig | undefined> {
7878
const searchPaths = await getConfigSearchPaths(application)
79+
d('searching', searchPaths)
7980
for (const filePath of searchPaths) {
8081
try {
8182
const data = await fs.readFile(filePath)

packages/electron-chrome-extensions/src/browser/api/lib/winreg.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { spawn } from 'child_process'
2+
import debug from 'debug'
3+
4+
const d = debug('electron-chrome-extensions:winreg')
25

36
export function readRegistryKey(hive: string, path: string, key?: string) {
47
if (process.platform !== 'win32') {
58
return Promise.reject('Unsupported platform')
69
}
710

811
return new Promise<string | null>((resolve, reject) => {
9-
const child = spawn('reg', ['query', `${hive}\\${path}`, ...(key ? ['/v', key] : [])])
12+
const args = ['query', `${hive}\\${path}`, ...(key ? ['/v', key] : [])]
13+
d('reg %s', args.join(' '))
14+
const child = spawn('reg', args)
1015

1116
let output = ''
1217
let error = ''

0 commit comments

Comments
 (0)