Skip to content

Commit 36a3f1e

Browse files
committed
feat: runtime.sendNativeMessage
1 parent d804d6f commit 36a3f1e

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,25 @@ export class NativeMessagingHost {
4242
private connectionId: string
4343
private connected: boolean = false
4444
private pending?: any[]
45+
private keepAlive: boolean
46+
private resolveResponse?: (message: any) => void
47+
48+
ready?: Promise<void>
4549

4650
constructor(
4751
extensionId: string,
4852
sender: ExtensionSender,
4953
connectionId: string,
5054
application: string,
55+
keepAlive: boolean = true,
5156
) {
57+
this.keepAlive = keepAlive
5258
this.sender = sender
53-
this.sender.ipc.on(`crx-native-msg-${connectionId}`, this.receiveExtensionMessage)
59+
if (keepAlive) {
60+
this.sender.ipc.on(`crx-native-msg-${connectionId}`, this.receiveExtensionMessage)
61+
}
5462
this.connectionId = connectionId
55-
this.launch(application, extensionId)
63+
this.ready = this.launch(application, extensionId)
5664
}
5765

5866
destroy() {
@@ -62,8 +70,10 @@ export class NativeMessagingHost {
6270
this.process.kill()
6371
this.process = undefined
6472
}
65-
this.sender.ipc.off(`crx-native-msg-${this.connectionId}`, this.receiveExtensionMessage)
66-
this.sender.send(`crx-native-msg-${this.connectionId}-disconnect`)
73+
if (this.keepAlive) {
74+
this.sender.ipc.off(`crx-native-msg-${this.connectionId}`, this.receiveExtensionMessage)
75+
this.sender.send(`crx-native-msg-${this.connectionId}-disconnect`)
76+
}
6777
}
6878

6979
private async launch(application: string, extensionId: string) {
@@ -127,6 +137,17 @@ export class NativeMessagingHost {
127137
const length = data.readUInt32LE(0)
128138
const message = JSON.parse(data.subarray(4, 4 + length).toString())
129139
d('receive: %s', message)
130-
this.sender.send(`crx-native-msg-${this.connectionId}`, message)
140+
if (this.keepAlive) {
141+
this.sender.send(`crx-native-msg-${this.connectionId}`, message)
142+
} else {
143+
this.resolveResponse?.(message)
144+
}
145+
}
146+
147+
sendAndReceive(message: any) {
148+
this.send(message)
149+
return new Promise((resolve) => {
150+
this.resolveResponse = resolve
151+
})
131152
}
132153
}

packages/electron-chrome-extensions/src/browser/api/runtime.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class RuntimeAPI extends EventEmitter {
1414
handle('runtime.connectNative', this.connectNative, { permission: 'nativeMessaging' })
1515
handle('runtime.disconnectNative', this.disconnectNative, { permission: 'nativeMessaging' })
1616
handle('runtime.openOptionsPage', this.openOptionsPage)
17+
handle('runtime.sendNativeMessage', this.sendNativeMessage, { permission: 'nativeMessaging' })
1718
}
1819

1920
private connectNative = async (
@@ -35,6 +36,19 @@ export class RuntimeAPI extends EventEmitter {
3536
this.hostMap[connectionId] = undefined
3637
}
3738

39+
private sendNativeMessage = async (event: ExtensionEvent, application: string, message: any) => {
40+
const connectionId = crypto.randomUUID()
41+
const host = new NativeMessagingHost(
42+
event.extension.id,
43+
event.sender!,
44+
connectionId,
45+
application,
46+
false,
47+
)
48+
await host.ready
49+
return await host.sendAndReceive(message)
50+
}
51+
3852
private openOptionsPage = async ({ extension }: ExtensionEvent) => {
3953
// TODO: options page shouldn't appear in Tabs API
4054
// https://developer.chrome.com/extensions/options#tabs-api

packages/electron-chrome-extensions/src/renderer/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ export const injectExtensionAPIs = () => {
517517
return port
518518
},
519519
openOptionsPage: invokeExtension('runtime.openOptionsPage'),
520+
sendNativeMessage: invokeExtension('runtime.sendNativeMessage'),
520521
}
521522
},
522523
},

0 commit comments

Comments
 (0)