Skip to content

Implement beforeunload event and infrastructure #10787

Closed

Description

@Ms2ger

Blocks #2968.

This is not an easy bug.

Activity

  1. added
    A-content/dom
    I-enhancement
    on Apr 21, 2016
  2. added
    E-very-complex
    on Apr 21, 2016
  3. gterzian commented on Jan 26, 2018

    @gterzian
    Member

    If this one isn't worked on by someone else already, I am interested in taking it on.

  4. jdm commented on Jan 26, 2018

    @jdm
    Member

    Nobody is working on it. You're welcome to tackle it, given your track record :D

  5. gterzian commented on Jan 27, 2018

    @gterzian
    Member

    thanks, ok I'll get started then...

  6. gterzian commented on Mar 18, 2018

    @gterzian
    Member

    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:

    1. 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.
    2. 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...
    3. 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.
    4. 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...

  7. gterzian commented on Mar 26, 2018

    @gterzian
    Member

    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.

    1. 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.
    2. 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.
    3. 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.
    4. 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.
    5. 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.
    6. 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

    What do you think?

  8. jdm commented on Mar 26, 2018

    @jdm
    Member

    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.

  9. paulrouget commented on Mar 26, 2018

    @paulrouget
    Contributor

    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.

    That makes sense to me.

  10. cbrewster commented on Apr 5, 2018

    @cbrewster
    Contributor

    @gterzian here's some thoughts I have, hope they can help:

    Prompting to unload happens in these 2 spots(possibly others too):

    1. Navigating to a new document
    2. Traversing by a delta

    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 Load message 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 beforeunload event to be triggered during a history traversal in Firefox.

  11. gterzian commented on Apr 6, 2018

    @gterzian
    Member

    @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-ffcaa7c334fe34498bc7362e376be445R1555

    Re Traversing:

    1. 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).
    2. 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'.
    3. In servo, WindowProxy.active is a pipeline_id.
    4. 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).
    5. 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?

  12. cbrewster commented on Apr 6, 2018

    @cbrewster
    Contributor

    @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_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.

    I'd be glad to give a review once its ready!

  13. cbrewster commented on Apr 6, 2018

    @cbrewster
    Contributor
  14. added 4 commits that reference this issue on Apr 18, 2018
    10a3418
    172d4eb
    acccc13
    4629070
  15. added 3 commits that reference this issue on May 1, 2018
    884851b
    ae80a01
    1d8283e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/domE-very-complexI-enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions