Skip to content

Commit

Permalink
Ensure that a port can only unregister its frameId if it's associated
Browse files Browse the repository at this point in the history
This is a more complete fix for issue #2125.
  • Loading branch information
mrmr1993 committed Dec 2, 2017
1 parent b3fae09 commit ff615ef
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions background_scripts/main.coffee
Expand Up @@ -301,7 +301,7 @@ for icon in [ENABLED_ICON, DISABLED_ICON, PARTIAL_ICON]
Frames =
onConnect: (sender, port) ->
[tabId, frameId] = [sender.tab.id, sender.frameId]
port.onDisconnect.addListener -> Frames.unregisterFrame {tabId, frameId}
port.onDisconnect.addListener -> Frames.unregisterFrame {tabId, frameId, port}
port.postMessage handler: "registerFrameId", chromeFrameId: frameId
(portsForTab[tabId] ?= {})[frameId] = port

Expand All @@ -312,11 +312,11 @@ Frames =
registerFrame: ({tabId, frameId, port}) ->
frameIdsForTab[tabId].push frameId unless frameId in frameIdsForTab[tabId] ?= []

unregisterFrame: ({tabId, frameId}) ->
# FrameId 0 is the top/main frame. We never unregister that frame. If the tab is closing, then we tidy
# up elsewhere. If the tab is navigating to a new page, then a new top frame will be along soon.
# This mitigates against the unregister and register messages arriving out of order. See #2125.
if 0 < frameId
unregisterFrame: ({tabId, frameId, port}) ->
# Check that the port trying to unregister the frame hasn't already been replaced by a new frame
# registering. See #2125.
registeredPort = portsForTab[tabId]?[frameId]
if registeredPort == port or not registeredPort
if tabId of frameIdsForTab
frameIdsForTab[tabId] = (fId for fId in frameIdsForTab[tabId] when fId != frameId)
if tabId of portsForTab
Expand Down

0 comments on commit ff615ef

Please sign in to comment.