Skip to content

Commit d804d6f

Browse files
committed
disconnect native host
1 parent 2000d57 commit d804d6f

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ export class NativeMessagingHost {
5656
}
5757

5858
destroy() {
59+
if (!this.connected) return
5960
this.connected = false
6061
if (this.process) {
61-
this.process.disconnect()
62+
this.process.kill()
6263
this.process = undefined
6364
}
6465
this.sender.ipc.off(`crx-native-msg-${this.connectionId}`, this.receiveExtensionMessage)
65-
// TODO: send disconnect
66+
this.sender.send(`crx-native-msg-${this.connectionId}-disconnect`)
6667
}
6768

6869
private async launch(application: string, extensionId: string) {
@@ -83,8 +84,14 @@ export class NativeMessagingHost {
8384
this.process.stderr!.on('data', (data) => {
8485
d('stderr: %s', data.toString())
8586
})
86-
this.process.on('error', (err) => d('error: %s', err))
87-
this.process.on('exit', (code) => d('exited %d', code))
87+
this.process.on('error', (err) => {
88+
d('error: %s', err)
89+
this.destroy()
90+
})
91+
this.process.on('exit', (code) => {
92+
d('exited %d', code)
93+
this.destroy()
94+
})
8895

8996
this.connected = true
9097

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,20 @@ export const injectExtensionAPIs = () => {
5656
extensionId: string,
5757
application: string,
5858
receive: (message: any) => void,
59+
disconnect: () => void,
5960
callback: ConnectNativeCallback,
6061
) => {
6162
const connectionId = (contextBridge as any).executeInMainWorld({
6263
func: () => crypto.randomUUID(),
6364
})
6465
invokeExtension(extensionId, 'runtime.connectNative', {}, connectionId, application)
65-
ipcRenderer.on(`crx-native-msg-${connectionId}`, (_event, message) => {
66+
const onMessage = (_event: Electron.IpcRendererEvent, message: any) => {
6667
receive(message)
68+
}
69+
ipcRenderer.on(`crx-native-msg-${connectionId}`, onMessage)
70+
ipcRenderer.once(`crx-native-msg-${connectNative}-disconnect`, () => {
71+
ipcRenderer.off(`crx-native-msg-${connectionId}`, onMessage)
72+
disconnect()
6773
})
6874
const send = (message: any) => {
6975
ipcRenderer.send(`crx-native-msg-${connectionId}`, message)
@@ -182,39 +188,43 @@ export const injectExtensionAPIs = () => {
182188

183189
class NativePort implements chrome.runtime.Port {
184190
private connectionId: string = ''
191+
private connected = false
185192
private pending: any[] = []
186193

187-
name: string = 'NativePort'
194+
name: string = ''
188195

189-
_init(connectionId: string, send: (message: any) => void) {
196+
_init = (connectionId: string, send: (message: any) => void) => {
197+
this.connected = true
190198
this.connectionId = connectionId
191199
this._send = send
192200

193-
this.pending.forEach((msg) => {
194-
console.log('sending pending', JSON.stringify(msg))
195-
this.postMessage(msg)
196-
})
201+
this.pending.forEach((msg) => this.postMessage(msg))
197202
this.pending = []
198203

199-
console.log('***NativePort._init')
204+
Object.defineProperty(this, '_init', { value: undefined })
200205
}
201206

202207
_send(message: any) {
203208
this.pending.push(message)
204209
}
205210

206211
_receive(message: any) {
207-
console.log('NativePort received', JSON.stringify(message))
208212
;(this.onMessage as any)._emit(message)
209213
}
210214

215+
_disconnect() {
216+
this.disconnect()
217+
}
218+
211219
postMessage(message: any) {
212-
console.log('***NativePort.postMessage', JSON.stringify(message))
213220
this._send(message)
214221
}
215222
disconnect() {
216-
electron.disconnectNative(extensionId, this.connectionId)
217-
;(this.onDisconnect as any)._emit()
223+
if (this.connected) {
224+
electron.disconnectNative(extensionId, this.connectionId)
225+
;(this.onDisconnect as any)._emit()
226+
this.connected = false
227+
}
218228
}
219229
onMessage: chrome.runtime.PortMessageEvent = new Event() as any
220230
onDisconnect: chrome.runtime.PortDisconnectEvent = new Event() as any
@@ -499,10 +509,11 @@ export const injectExtensionAPIs = () => {
499509
connectNative: (application: string) => {
500510
const port = new NativePort()
501511
const receive = port._receive.bind(port)
512+
const disconnect = port._disconnect.bind(port)
502513
const callback: ConnectNativeCallback = (connectionId, send) => {
503514
port._init(connectionId, send)
504515
}
505-
electron.connectNative(extensionId, application, receive, callback)
516+
electron.connectNative(extensionId, application, receive, disconnect, callback)
506517
return port
507518
},
508519
openOptionsPage: invokeExtension('runtime.openOptionsPage'),

0 commit comments

Comments
 (0)