Skip to content

Commit

Permalink
Closes mozilla-mobile#9550: Check port instance before disconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
csadilek authored and mergify[bot] committed Jan 29, 2021
1 parent d3579e0 commit a8a2833
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GeckoWebExtension(
val runtime: GeckoRuntime
) : WebExtension(nativeExtension.id, nativeExtension.location, true) {

private val connectedPorts: MutableMap<PortId, Port> = mutableMapOf()
private val connectedPorts: MutableMap<PortId, GeckoPort> = mutableMapOf()
private val logger = Logger("GeckoWebExtension")

/**
Expand All @@ -57,8 +57,11 @@ class GeckoWebExtension(
}

override fun onDisconnect(port: GeckoNativeWebExtension.Port) {
connectedPorts.remove(PortId(name))
messageHandler.onPortDisconnected(GeckoPort(port))
val connectedPort = connectedPorts[PortId(name)]
if (connectedPort != null && connectedPort.nativePort == port) {
connectedPorts.remove(PortId(name))
messageHandler.onPortDisconnected(GeckoPort(port))
}
}
}

Expand Down Expand Up @@ -96,9 +99,11 @@ class GeckoWebExtension(
}

override fun onDisconnect(port: GeckoNativeWebExtension.Port) {
val geckoPort = GeckoPort(port, session)
connectedPorts.remove(PortId(name, session))
messageHandler.onPortDisconnected(geckoPort)
val connectedPort = connectedPorts[PortId(name, session)]
if (connectedPort != null && connectedPort.nativePort == port) {
connectedPorts.remove(PortId(name, session))
messageHandler.onPortDisconnected(connectedPort)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ class GeckoWebExtensionTest {
verify(messageHandler).onPortMessage(eq(portMessage), portCaptor.capture())
assertSame(port, (portCaptor.value as GeckoPort).nativePort)

// Verify disconnected port is forwarded to message handler
// Verify disconnected port is forwarded to message handler if connected
portDelegate.onDisconnect(mock())
verify(messageHandler, never()).onPortDisconnected(portCaptor.capture())

portDelegate.onDisconnect(port)
verify(messageHandler).onPortDisconnected(portCaptor.capture())
assertSame(port, (portCaptor.value as GeckoPort).nativePort)
Expand Down Expand Up @@ -145,7 +148,10 @@ class GeckoWebExtensionTest {
assertSame(port, (portCaptor.value as GeckoPort).nativePort)
assertSame(session, (portCaptor.value as GeckoPort).engineSession)

// Verify disconnected port is forwarded to message handler
// Verify disconnected port is forwarded to message handler if connected
portDelegate.onDisconnect(mock())
verify(messageHandler, never()).onPortDisconnected(portCaptor.capture())

portDelegate.onDisconnect(port)
verify(messageHandler).onPortDisconnected(portCaptor.capture())
assertSame(port, (portCaptor.value as GeckoPort).nativePort)
Expand Down

0 comments on commit a8a2833

Please sign in to comment.