aboutsummaryrefslogtreecommitdiffstats
path: root/background_scripts
diff options
context:
space:
mode:
authorStephen Blott2016-04-05 14:42:46 +0100
committerStephen Blott2016-04-05 14:42:48 +0100
commitadce73cb68f7ca3e3e01ca6fbb08a1008c9c8b90 (patch)
tree600a7a790925f838e17d6b6a1f1594e0a937631f /background_scripts
parent17a24b4b3df24c5b3a354dd195e2f4fe3e4bfb54 (diff)
downloadvimium-adce73cb68f7ca3e3e01ca6fbb08a1008c9c8b90.tar.bz2
Better close-down for global link hints.
- Unregister frames with the background-page hint coordinator. - Better page cleanup in the content scripts. - Require documentReady before harvesting hints. There is still an issue in some cases when link-hints are activated as a page is transitioning, but that problem case seems to have existed in 1.54. I'll continue to investigate.
Diffstat (limited to 'background_scripts')
-rw-r--r--background_scripts/main.coffee34
1 files changed, 9 insertions, 25 deletions
diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee
index 6dd3906f..addbf514 100644
--- a/background_scripts/main.coffee
+++ b/background_scripts/main.coffee
@@ -300,11 +300,12 @@ Frames =
frameIdsForTab[tabId].push frameId unless frameId in frameIdsForTab[tabId] ?= []
(portsForTab[tabId] ?= {})[frameId] = port
- unregsterFrame: ({tabId, frameId}) ->
+ unregisterFrame: ({tabId, frameId}) ->
if tabId of frameIdsForTab
frameIdsForTab[tabId] = (fId for fId in frameIdsForTab[tabId] when fId != frameId)
if tabId of portsForTab
delete portsForTab[tabId][frameId]
+ HintCoordinator.unregisterFrame tabId, frameId
isEnabledForUrl: ({request, tabId, port}) ->
urlForTab[tabId] = request.url if request.frameIsFocused
@@ -345,11 +346,9 @@ cycleToFrame = (frames, frameId, count = 0) ->
[frames[count..]..., frames[0...count]...]
HintCoordinator =
- debug: false
tabState: {}
onMessage: (tabId, frameId, request) ->
- console.log "onMessage", tabId, frameId, "[#{request.messageType}]" if @debug
if request.messageType of this
this[request.messageType] tabId, frameId, request
else
@@ -357,46 +356,31 @@ HintCoordinator =
@sendMessage request.messageType, tabId, request
sendMessage: (messageType, tabId, request = {}) ->
- console.log "sendMessage", tabId, "[#{messageType}] [#{@tabState[tabId].ports.length}]" if @debug
extend request, {handler: "linkHintsMessage", messageType}
- for own frameId, port of @tabState[tabId].ports
- try
- port.postMessage request
- catch
- # A frame has gone away; remove it from consideration.
- delete @tabState[tabId].ports[frameId]
- # It could be that we're expecting hints from this frame; schedule "sending" dummy/empty hints instead.
- Utils.nextTick =>
- @postHintDescriptors tabId, frameId, hintDescriptors: []
- # We can delete the tab state when we see an "exit" message, that's the last message in the sequence.
- delete @tabState[tabId] if messageType == "exit"
+ port.postMessage request for own _, port of @tabState[tabId].ports
prepareToActivateMode: (tabId, originatingFrameId, {modeIndex}) ->
- console.log "" if @debug
- console.log "prepareToActivateMode", tabId, "[#{frameIdsForTab[tabId].length}]" if @debug
@tabState[tabId] = {frameIds: frameIdsForTab[tabId][..], hintDescriptors: [], originatingFrameId, modeIndex}
@tabState[tabId].ports = extend {}, portsForTab[tabId]
@sendMessage "getHintDescriptors", tabId, {modeIndex}
- # FIXME(smblott) This should not be necessary; it's a backstop to mitigate against the possibility that,
- # for some reason, we do not hear back from a frame.
- unless @debug
- for frameId in frameIdsForTab[tabId]
- do (frameId) =>
- Utils.setTimeout 400, =>
- @postHintDescriptors tabId, frameId, hintDescriptors: []
# Receive hint descriptors from all frames and activate link-hints mode when we have them all.
postHintDescriptors: (tabId, frameId, {hintDescriptors}) ->
if frameId in @tabState[tabId].frameIds
@tabState[tabId].hintDescriptors.push hintDescriptors...
@tabState[tabId].frameIds = @tabState[tabId].frameIds.filter (fId) -> fId != frameId
- console.log "postHintDescriptors", tabId, frameId, "[#{@tabState[tabId].frameIds.length}]" if @debug
if @tabState[tabId].frameIds.length == 0
@sendMessage "activateMode", tabId,
originatingFrameId: @tabState[tabId].originatingFrameId
hintDescriptors: @tabState[tabId].hintDescriptors
modeIndex: @tabState[tabId].modeIndex
+ # If an unregistering frame is participating in link-hints mode, then we need to tidy up after it.
+ unregisterFrame: (tabId, frameId) ->
+ delete @tabState[tabId]?.ports?[frameId]
+ # We fake "postHintDescriptors" for an unregistering frame, if necessary.
+ @postHintDescriptors tabId, frameId, hintDescriptors: [] if @tabState[tabId]?.frameIds
+
# Port handler mapping
portHandlers =
completions: handleCompletions