Skip to content

Commit 656ea7a

Browse files
committed
feat: add additional native config search paths
1 parent 7c4e735 commit 656ea7a

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"start": "yarn build:context-menu && yarn build:extensions && yarn build:chrome-web-store && yarn --cwd ./packages/shell start",
1919
"start:debug": "cross-env SHELL_DEBUG=true DEBUG='electron*' yarn start",
2020
"start:electron-dev": "cross-env ELECTRON_OVERRIDE_DIST_PATH=$(e show out --path) ELECTRON_ENABLE_LOGGING=1 yarn start",
21-
"start:electron-dev:debug": "cross-env SHELL_DEBUG=true DEBUG='electron*' yarn start:electron-dev",
21+
"start:electron-dev:debug": "cross-env DEBUG='electron*' yarn start:electron-dev",
2222
"start:electron-dev:trace": "cross-env ELECTRON_OVERRIDE_DIST_PATH=$(e show out --path) ELECTRON_ENABLE_LOGGING=1 yarn --cwd ./packages/shell start:trace",
2323
"start:skip-build": "cross-env SHELL_DEBUG=true DEBUG='electron-chrome-extensions*' yarn --cwd ./packages/shell start",
2424
"test": "yarn test:extensions",

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { spawn } from 'node:child_process'
22
import { promises as fs } from 'node:fs'
3+
import * as os from 'node:os'
34
import * as path from 'node:path'
45
import { app } from 'electron'
56
import { ExtensionSender } from '../../router'
@@ -22,26 +23,35 @@ function isValidConfig(config: any): config is NativeConfig {
2223
typeof config.name === 'string' &&
2324
typeof config.description === 'string' &&
2425
typeof config.path === 'string' &&
25-
config.stdio === 'stdio' &&
26+
config.type === 'stdio' &&
2627
Array.isArray(config.allowed_origins)
2728
)
2829
}
2930

30-
async function readNativeMessagingHostConfig(
31-
application: string,
32-
): Promise<NativeConfig | undefined> {
31+
async function getConfigSearchPaths(application: string) {
32+
const appJson = `${application}.json`
3333
let searchPaths: string[]
3434
switch (process.platform) {
3535
case 'darwin':
3636
searchPaths = [
37-
path.join(app.getPath('userData'), 'NativeMessagingHosts', `${application}.json`),
38-
path.join('/Library/Google/Chrome/NativeMessagingHosts', `${application}.json`),
37+
path.join('/Library/Google/Chrome/NativeMessagingHosts', appJson),
38+
// Also look under Chrome's directory since some apps only install their
39+
// config there
40+
path.join(
41+
os.homedir(),
42+
'Library',
43+
'Application Support',
44+
'Google/Chrome/NativeMessagingHosts',
45+
appJson,
46+
),
47+
path.join(app.getPath('userData'), 'NativeMessagingHosts', appJson),
3948
]
4049
break
4150
case 'linux':
4251
searchPaths = [
43-
path.join(app.getPath('userData'), 'NativeMessagingHosts', `${application}.json`),
44-
path.join('/etc/opt/chrome/native-messaging-hosts/', `${application}.json`),
52+
path.join('/etc/opt/chrome/native-messaging-hosts/', appJson),
53+
path.join(os.homedir(), '.config/google-chrome/NativeMessagingHosts/', appJson),
54+
path.join(app.getPath('userData'), 'NativeMessagingHosts', appJson),
4555
]
4656
break
4757
case 'win32': {
@@ -58,14 +68,29 @@ async function readNativeMessagingHostConfig(
5868
default:
5969
throw new Error('Unsupported platform')
6070
}
71+
return searchPaths
72+
}
6173

74+
async function readNativeMessagingHostConfig(
75+
application: string,
76+
): Promise<NativeConfig | undefined> {
77+
const searchPaths = await getConfigSearchPaths(application)
6278
for (const filePath of searchPaths) {
6379
try {
6480
const data = await fs.readFile(filePath)
6581
const config = JSON.parse(data.toString())
66-
if (isValidConfig(config)) return config
82+
if (isValidConfig(config)) {
83+
d('read config in %s', filePath, config)
84+
return config
85+
} else {
86+
d('%s contained invalid config', filePath, config)
87+
}
6788
} catch (error) {
68-
d('readNativeMessagingHostConfig: unable to read %s', filePath, error)
89+
if ((error as NodeJS.ErrnoException)?.code === 'ENOENT') {
90+
d('unable to read %s', filePath)
91+
} else {
92+
d('unknown error', error)
93+
}
6994
continue
7095
}
7196
}

0 commit comments

Comments
 (0)