You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Implement beforeunload event and infrastructure #10787
A-content/domInteracting with the DOM from web contentE-very-complexVery difficult. Do not attempt without significant relevant experience and motivation.I-enhancementNo impact; the issue is a missing or proposed feature.
Ok I've finally got around to starting to look into this one, sorry for the delay...
Just a few thoughts on the 'masterplan' so far:
It seems there are a whole bunch of (new?) tests under tests/wpt/html/browsers/browsing-the-web/unloading-documents/, so I'm using those as a foundation.
I can see there is already quite a bit of infrastructure in place for the BeforeUnloadEvent, in terms of the actual event, and adding handlers and so on, but the event itself doesn't seem to fire just yet, I guess that is where I come in...
I propose adding two new methods to Document, one prompt_to_unload_document, and one unload_document(maybe I should drop the "document" from the name), to contain the logic, and then call them from various places. I think Window.load_url for example, is a place where we would need to both prompt and unload, and that could be done by calling those methods. Currently some tests are failing because those rely on manipulating windown.location.href, and the event isn't fired in response yet. I guess I'll find more opportunities like that as I get to know the rest of the suite.
The good news is I got some tests passing already, just by handling beforeunload events from a CustomEvent, used in some of the tests, which fits the "otherwise" clause of the spec.
So I don't have a complete understanding of the domain yet, but it seemed to me that those new methods on Document would be a good way to put the logic and dispatch the event, and that those would need to be called from various places, and I intend to figure those places out from looking at the test suite...
I have a question on how to plug 'unloading' and 'beforeunloading' into various existing workflows, namely navigation and history traversal, with a little prelude of facts.
So far, I was using Window.load_url as one entry point from which to call document.prompt_to_unload, a new method I added.
Then I realized the actual 'unloading' which would follow, cannot take place in that same method, because it is done in various places, mostly inside Constellation, in response to sending/receiving messages.
So I switched my focus on doing 'prompt to unload' and 'unload' inside the ScriptThread, in response to a message coming from Constellation, containing the pipeline id.
I've noticed there is a ConstellationControlMsg::ExitPipeline message. That message however is sent to the ScriptThread from a Pipeline, and only at the end of Constellation.close_pipeline. I was thinking of 'prompting to unload' and 'unloading' the document in response to receiving that message. The problem with that, to me, seems to be that when this message is received by the ScriptThread, the browsing context has already been removed from the constellation, so the 'unloading' would be final in all cases, and we'd have to leave out the 'prompt and ask user for a comfirmation' part.
Therefore, I was thinking that to preserve the spirit of the spec(even if we don't even want to ever prompt the user, since it's optional), we might need a new message, something like ConstellationControlMsg::Unload(PipelineId), to be sent to the ScriptThread by the constellation before calling, or at the beginning of, constellation.close_pipeline. Then the thread could use the pipeline id to find the right document, and call the prompt_to_unload and 'unload' methods of it, and then send a new ScriptMsg::UnloadComplete message to the constellation, or perhaps just respond on a channel that would be send as part of ConstellationControlMsg::Unload(PipelineId, IpcSender).
So basically I'm suggesting to add an extra step to the 'close pipeline' workflow.
Lastly, I was also thinking we might need a new EmbedderMsg to be sent as part of 'prompt to unload', so that the embedder can actually decide whether to prompt or not. (The thing to note is that the whole process is supposed to 'pause' while prompting, but it also seems optional to prompt, so that step could be skipped, still it might be worth it to have a hook in there and send a message to the embedder). @paulrouget
I agree with your thoughts on adding the new message. The constellation is not supposed to block on waiting for a response from web content, so we should avoid the IpcSender argument if it's at all possible. Giving the embedding the ability to actually show the prompt would be a useful addition, in line with #19992.
So basically I'm suggesting to add an extra step to the 'close pipeline' workflow.
So we would go through the script thread to request to close the pipeline, block that thread instead of the constellation while sending an EmbedderMsg.
it seems the spec has this concept of the 'active document' of a browsing context(defined as the 'active' document of the WindowProxy of that browsing context).
The spec wants us to 'unload' this active document of an entry, when we traverse to that entry, and the 'document' of that entry is not the same at this 'active document'.
In servo, WindowProxy.active is a pipeline_id.
Looking at the new session history code, I'm thinking, when traversing to a SessionHistoryDiff::BrowsingContextDiff, is the relation "reloader"(pipepline_id) always equal to browing_context > window proxy> active (pipeline_id)?
In the negative case, it seems the spec would want us to unload this active(pipeline_id), because we would be navigating to a different pipeline/document(the one represented by reloader).
If "reloader" and "active" are always the same pipeline for a given browsing context in an entry, I guess we can skip that step since unloading will never be necessary?
I'll rebase off your work, and add some .ini files for the tests that are expected as fail(currently all "unloading_documents" tests are passing expect those blocked by #14720, #20496, and #13241) perhaps you would want to give a first review after?
That's the correct way to handle the navigation case.
For traversing I'm not entirely sure what to do.
In the case of traversing by +/-1 the spec makes sense, since we will be changing the active pipeline of at most 1 browsing context. In the case where the delta is > 1 or < -1, multiple browsing contexts may have their active pipeline changed. We should do some more research into what we should do here. It might be worth opening an issue at https://github.com/whatwg/html (to clarify the spec) and exploring what other browsers do.
The active document for the current entry is just the pipeline_id of a browsing_context in the constellation. I wouldn't go through the WindowProxy to find this pipeline_id because WindowProxys don't store pipeline_ids for documents in other script threads.
For now, a PR that just handles the Navigation case would be good and then we can handle the traversing case in a followup.
A-content/domInteracting with the DOM from web contentE-very-complexVery difficult. Do not attempt without significant relevant experience and motivation.I-enhancementNo impact; the issue is a missing or proposed feature.
Activity
gterzian commentedon Jan 26, 2018
If this one isn't worked on by someone else already, I am interested in taking it on.
jdm commentedon Jan 26, 2018
Nobody is working on it. You're welcome to tackle it, given your track record :D
gterzian commentedon Jan 27, 2018
thanks, ok I'll get started then...
gterzian commentedon Mar 18, 2018
Ok I've finally got around to starting to look into this one, sorry for the delay...
Just a few thoughts on the 'masterplan' so far:
tests/wpt/html/browsers/browsing-the-web/unloading-documents/, so I'm using those as a foundation.BeforeUnloadEvent, in terms of the actual event, and adding handlers and so on, but the event itself doesn't seem to fire just yet, I guess that is where I come in...Document, oneprompt_to_unload_document, and oneunload_document(maybe I should drop the "document" from the name), to contain the logic, and then call them from various places. I thinkWindow.load_urlfor example, is a place where we would need to both prompt and unload, and that could be done by calling those methods. Currently some tests are failing because those rely on manipulatingwindown.location.href, and the event isn't fired in response yet. I guess I'll find more opportunities like that as I get to know the rest of the suite.beforeunloadevents from aCustomEvent, used in some of the tests, which fits the "otherwise" clause of the spec.So I don't have a complete understanding of the domain yet, but it seemed to me that those new methods on Document would be a good way to put the logic and dispatch the event, and that those would need to be called from various places, and I intend to figure those places out from looking at the test suite...
gterzian commentedon Mar 26, 2018
I have a question on how to plug 'unloading' and 'beforeunloading' into various existing workflows, namely navigation and history traversal, with a little prelude of facts.
Window.load_urlas one entry point from which to calldocument.prompt_to_unload, a new method I added.ConstellationControlMsg::ExitPipelinemessage. That message however is sent to the ScriptThread from a Pipeline, and only at the end ofConstellation.close_pipeline. I was thinking of 'prompting to unload' and 'unloading' the document in response to receiving that message. The problem with that, to me, seems to be that when this message is received by the ScriptThread, the browsing context has already been removed from the constellation, so the 'unloading' would be final in all cases, and we'd have to leave out the 'prompt and ask user for a comfirmation' part.ConstellationControlMsg::Unload(PipelineId), to be sent to the ScriptThread by the constellation before calling, or at the beginning of,constellation.close_pipeline. Then the thread could use the pipeline id to find the right document, and call theprompt_to_unloadand 'unload' methods of it, and then send a newScriptMsg::UnloadCompletemessage to the constellation, or perhaps just respond on a channel that would be send as part ofConstellationControlMsg::Unload(PipelineId, IpcSender).So basically I'm suggesting to add an extra step to the 'close pipeline' workflow.
EmbedderMsgto be sent as part of 'prompt to unload', so that the embedder can actually decide whether to prompt or not. (The thing to note is that the whole process is supposed to 'pause' while prompting, but it also seems optional to prompt, so that step could be skipped, still it might be worth it to have a hook in there and send a message to the embedder). @paulrougetWhat do you think?
jdm commentedon Mar 26, 2018
I agree with your thoughts on adding the new message. The constellation is not supposed to block on waiting for a response from web content, so we should avoid the IpcSender argument if it's at all possible. Giving the embedding the ability to actually show the prompt would be a useful addition, in line with #19992.
paulrouget commentedon Mar 26, 2018
So we would go through the script thread to request to close the pipeline, block that thread instead of the constellation while sending an EmbedderMsg.
That makes sense to me.
cbrewster commentedon Apr 5, 2018
@gterzian here's some thoughts I have, hope they can help:
Prompting to unload happens in these 2 spots(possibly others too):
Navigating to a new document
https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate Step 7
I think this can be accomplished in the script thread before sending the
Loadmessage to the constellation. This only cares about the browsing context that is navigating, so we don't need to worry about prompting to unload child iframes.Traversing by a delta
https://html.spec.whatwg.org/multipage/history.html#traverse-the-history-by-a-delta Step 5.2
This one seems tricky and I'm not entirely sure how to follow the spec here. The spec seems to refer to an entry of the joint session history as having a single browsing context. It really makes more sense to think of an entry of the joint session history as a tree where the nodes are browsing contexts and documents. Because of this, I don't know what the specified browsing context should be.
For what its worth, I can't seem to get the
beforeunloadevent to be triggered during a history traversal in Firefox.gterzian commentedon Apr 6, 2018
@cbrewster Thanks for looking into this.
Re Navigating, I agree, currently I'm doing Step 7 inside
window.load_url, see https://github.com/servo/servo/pull/20329/files#diff-ffcaa7c334fe34498bc7362e376be445R1555Re Traversing:
SessionHistoryDiff::BrowsingContextDiff, is the relation "reloader"(pipepline_id) always equal to browing_context > window proxy> active (pipeline_id)?In the negative case, it seems the spec would want us to unload this active(pipeline_id), because we would be navigating to a different pipeline/document(the one represented by reloader).
I'll rebase off your work, and add some .ini files for the tests that are expected as fail(currently all "unloading_documents" tests are passing expect those blocked by #14720, #20496, and #13241) perhaps you would want to give a first review after?
cbrewster commentedon Apr 6, 2018
@gterzian Thanks for working on this!
That's the correct way to handle the navigation case.
For traversing I'm not entirely sure what to do.
In the case of traversing by +/-1 the spec makes sense, since we will be changing the active pipeline of at most 1 browsing context. In the case where the delta is > 1 or < -1, multiple browsing contexts may have their active pipeline changed. We should do some more research into what we should do here. It might be worth opening an issue at https://github.com/whatwg/html (to clarify the spec) and exploring what other browsers do.
The active document for the current entry is just the
pipeline_idof abrowsing_contextin the constellation. I wouldn't go through theWindowProxyto find thispipeline_idbecauseWindowProxys don't storepipeline_ids for documents in other script threads.For now, a PR that just handles the
Navigationcase would be good and then we can handle the traversing case in a followup.I'd be glad to give a review once its ready!
cbrewster commentedon Apr 6, 2018
@gterzian I opened whatwg/html#3615 to get some clarification on the spec and I also created https://servo-experiments.surge.sh/beforeunload-parent.html to see what other browsers do.
Auto merge of #20329 - gterzian:before_unload, r=<try>
Auto merge of #20329 - gterzian:before_unload, r=<try>
Auto merge of #20329 - gterzian:before_unload, r=<try>
Auto merge of #20329 - gterzian:before_unload, r=<try>
Auto merge of #20329 - gterzian:before_unload, r=cbrewster
Auto merge of #20329 - gterzian:before_unload, r=cbrewster
Auto merge of #20329 - gterzian:before_unload, r=cbrewster