@@ -3,6 +3,7 @@ import { promises as fs } from 'node:fs'
33import * as path from 'node:path'
44import { app } from 'electron'
55import { ExtensionSender } from '../../router'
6+ import { readRegistryKey } from './winreg'
67
78const d = require ( 'debug' ) ( 'electron-chrome-extensions:nativeMessaging' )
89
@@ -17,21 +18,41 @@ interface NativeConfig {
1718async function readNativeMessagingHostConfig (
1819 application : string ,
1920) : Promise < NativeConfig | undefined > {
20- let searchPaths = [ path . join ( app . getPath ( 'userData' ) , 'NativeMessagingHosts' ) ]
21+ let searchPaths : string [ ]
2122 switch ( process . platform ) {
2223 case 'darwin' :
23- searchPaths . push ( '/Library/Google/Chrome/NativeMessagingHosts' )
24+ searchPaths = [
25+ path . join ( app . getPath ( 'userData' ) , 'NativeMessagingHosts' , `${ application } .json` ) ,
26+ path . join ( '/Library/Google/Chrome/NativeMessagingHosts' , `${ application } .json` ) ,
27+ ]
2428 break
29+ case 'linux' :
30+ searchPaths = [
31+ path . join ( app . getPath ( 'userData' ) , 'NativeMessagingHosts' , `${ application } .json` ) ,
32+ path . join ( '/etc/opt/chrome/native-messaging-hosts/' , `${ application } .json` ) ,
33+ ]
34+ break
35+ case 'win32' : {
36+ searchPaths = (
37+ await Promise . allSettled ( [
38+ readRegistryKey ( 'HKLM' , '\\Software\\Google\\Chrome\\NativeMessagingHosts' , application ) ,
39+ readRegistryKey ( 'HKCU' , '\\Software\\Google\\Chrome\\NativeMessagingHosts' , application ) ,
40+ ] )
41+ )
42+ . map ( ( result ) => ( result . status === 'fulfilled' ? result . value : undefined ) )
43+ . filter ( Boolean ) as string [ ]
44+ break
45+ }
2546 default :
2647 throw new Error ( 'Unsupported platform' )
2748 }
2849
29- for ( const basePath of searchPaths ) {
30- const filePath = path . join ( basePath , `${ application } .json` )
50+ for ( const filePath of searchPaths ) {
3151 try {
3252 const data = await fs . readFile ( filePath )
3353 return JSON . parse ( data . toString ( ) )
34- } catch {
54+ } catch ( error ) {
55+ d ( 'readNativeMessagingHostConfig: unable to read %s' , filePath , error )
3556 continue
3657 }
3758 }
0 commit comments