Closed Bug 878598 Opened 11 years ago Closed 7 years ago

PageMod not detached when tab location (url) changed

Categories

(Add-on SDK Graveyard :: General, defect, P2)

defect

Tracking

(Not tracked)

RESOLVED INCOMPLETE

People

(Reporter: fxrock2002, Unassigned)

References

Details

(Whiteboard: [Needs Investigation])

User Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.70 Safari/537.17

Steps to reproduce:

Added a page-mod like this:

var blockWorkers = [];

...


    PageMod({
        include: ["*.youtube.com", "*.reddit.com"],
        contentScriptWhen: 'start',
        onAttach: function(worker) {
            Util.warn('adding worker ' + worker.tab.url);
            blockWorkers.push(worker);

            for (var i = 0; i < blockWorkers.length; i++) {
                try {
                    Util.warn('current blockworker: ' + i + ' ' + blockWorkers[i].tab.url);
                } catch (e) { 

                }
            }

            worker.on('detach', function() {
                var index = blockWorkers.indexOf(worker);
                if (~index)
                {
                    Util.warn('DETACHED WORKER');
                    try {
                        Util.warn('detaching worker ' + worker.tab.url);
                    }
                    catch (e) {}
                    blockWorkers.splice(index, 1);
                }
            });
        }
    });



Opened new tab "reddit.com" then changed location of that tab via address bar to "youtube.com" Console log looks like this: 

warn: idderall: adding worker http://www.reddit.com/
warn: idderall: current blockworker: 0 http://www.reddit.com/
warn: idderall: adding worker http://www.youtube.com/
warn: idderall: current blockworker: 0 http://www.youtube.com/
warn: idderall: current blockworker: 1 http://www.youtube.com/
warn: idderall: adding worker http://www.youtube.com/
warn: idderall: current blockworker: 0 http://www.youtube.com/
warn: idderall: current blockworker: 1 http://www.youtube.com/
warn: idderall: current blockworker: 2 http://www.youtube.com/




Actual results:

The worker for reddit never has "detach" called as evidenced by the "DETACHED WORKER" message never logging. It appears that the "reddit.com" worker is simply recycled, since in my blockWorkers array, the first element has it's url changed to "youtube.com"


Expected results:

The "reddit.com" worker should have had detach called. From the SDK page-mod documentation: "Workers generate a detach event when their associated document is closed: that is, when the tab is closed or the tab's location changes. If you are maintaining a list of workers belonging to a page-mod, you can use this event to remove workers that are no longer valid."
Do they detach when you close the tab? The reddit worker probably hangs around in the bfcache in the event that you go back to that page with the back button. It should be suspended, though, so nothing will happen in it.
(In reply to Wes Kocher (:KWierso) from comment #1)
> Do they detach when you close the tab? The reddit worker probably hangs
> around in the bfcache in the event that you go back to that page with the
> back button. It should be suspended, though, so nothing will happen in it.

Yes, they detach when the tab is closed. It seems the reddit.com worker gets "recycled". Notice in the blockWorkers array, the first worker has its url changed to youtube.com after the youtube is loaded. So we end up with three workers, all with url "youtube.com" even though only two were added when the location was changed to "youtube.com" 

If I want to detach when reddit.com is navigated away from... I have to roll my own? This isn't how things are described in the docs (as mentioned in the original bug report)
Flags: needinfo?(dtownsend+bugmail)
There are also pageshow and pagehide events which will are emitted when document enters / leaves bgcache. If you wish to destroy reddit worker as soon as it enters bfcach you can listen to "pageshow" event on it and call .destroy(). More details on bfcache can be found on this mdn link:
https://developer.mozilla.org/en-US/docs/Working_with_BFCache
This might work better as a documentation bug.
Component: General → Documentation
Priority: -- → P2
(In reply to Wes Kocher (:KWierso) from comment #4)
> This might work better as a documentation bug.

The URL showing up incorrectly does suggest some kind of issue. I bet it's that the worker just looks at the tab's current url to show that though which isn't a big problem. I'd be interested to see what those urls look like after clicking the back button.
Flags: needinfo?(dtownsend+bugmail)
Assignee: nobody → jsantell
Also seeing the multiple attachment for reddit.com in that example, even after back/forwarding. Looking into it
This bug has just bitten me with version 1.15. I keep an array of the attached workers. The worker.url property returns the window.document.location.href value which is the wrong one if the worker's page is sitting in the bfcache.

I need to be able to identify all of the workers that are active in the activeTab. This almost works since each worker reports the URL from window.document which is the same for all iframe's et al. Alas it also
picks up the workers for hidden pages in the bfcache since they are still associated with the window.
An attempt to send a message to a hidden page bombs.

It would be nice if there was a property on the worker which tells me if it is hidden. I can't access _frozen.
Whiteboard: kanbanzilla[Needs Investigation]
Unassigning myself from bugs I haven't gotten around to
Assignee: jsantell → nobody
Component: Documentation → General
Whiteboard: kanbanzilla[Needs Investigation] → [Needs Investigation]
Blocks: sdk/page-mod
Summary: Page worker not detached when tab location (url) changed → PageMod not detached when tab location (url) changed
https://bugzilla.mozilla.org/show_bug.cgi?id=1399562
Status: UNCONFIRMED → RESOLVED
Closed: 7 years ago
Resolution: --- → INCOMPLETE
You need to log in before you can comment on or make changes to this bug.