Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add steps to destroy documents that are ineligible for receiving message when posting message through broadcast channel #8972

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
107 changes: 75 additions & 32 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -99976,6 +99976,20 @@ location.href = '#foo';</code></pre>
data-x="window-navigation-api">navigation API</span>, <var>entriesForNavigationAPI</var>, and
<var>reactivatedEntry</var>.</p></li>

<li><p>Let <var>window</var> be the <code>Window</code> whose <span data-x="concept-document-window">
lozy219 marked this conversation as resolved.
Show resolved Hide resolved
associated <code>Document</code></span> is <var>document</var>.</p></li>

<li><p>Let <var>channels</var> be a list of <code>BroadcastChannel</code> objects whose
<span>relevant global object</span> is <var>window</var>.</p></li>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In step 9 of postMessage, it looks like the channels are sorted by creation order. Should we add that step here as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think when broadcasting a message to multiple channel, we have to sort them in order to reserve the order, but here it's flushing all the messages received on different channels on page reactivation, it would be a bit subtle to define what's the correct order of the channel given the messages could be received from different time.

Also, the sorting behavior seems not compulsory with this sentence:

This does not define a complete ordering. Within this constraint, user agents may sort the list in any implementation-defined manner.

I will leave this open first, and change the content if necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The messages from each channel's queue might be from different times, yes. But for each message, the ordering of the channel that got them should be the same right? (e.g. message X is received and there are channels A & B, and X should be received by the older channel first, regardless if X is sent immediately or later on after being queued). I think it would be good to preserve that property in this case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I have added a similar sentence here. Since all the channels here should have the same "relevant agents" so maybe we just need to mention the order. Is that good enough?


<li><p>For each <var>channel</var> in <var>channels</var>:</p>
<ul>
<li><p>For each string <var>serialized</var> from <var>channel</var>'s <span>message queue</span>,
<span data-x="dom-BroadcastChannel-post-a-message">post a message</span> with <var>serialized</var>
to <var>channel</var>.</p></li>
lozy219 marked this conversation as resolved.
Show resolved Hide resolved
</ul>
</li>

<li>
<p>If <var>document</var>'s <span>current document readiness</span> is "<code
data-x="">complete</code>", and <var>document</var>'s <span>page showing</span> flag is false,
Expand Down Expand Up @@ -113696,8 +113710,9 @@ interface <dfn interface>BroadcastChannel</dfn> : <span>EventTarget</span> {

<div w-nodev>

<p>A <code>BroadcastChannel</code> object has a <dfn>channel name</dfn> and a <dfn
data-x="concept-BroadcastChannel-closed">closed flag</dfn>.</p>
<p>A <code>BroadcastChannel</code> object has a <dfn>channel name</dfn>, a
<dfn data-x="concept-BroadcastChannel-closed">closed flag</dfn> and an initially empty <span>queue</span>
<dfn>message queue</dfn>.

<p>The <dfn><code data-x="dom-BroadcastChannel">new BroadcastChannel(<var>name</var>)</code></dfn>
constructor steps are:</p>
Expand Down Expand Up @@ -113725,6 +113740,37 @@ interface <dfn interface>BroadcastChannel</dfn> : <span>EventTarget</span> {
<span>worker</span> is not a <span>suspendable worker</span>.</p></li>
</ul>

<p>To <dfn data-x="dom-BroadcastChannel-post-a-message">post a serialized message</dfn>
given a string <var>serialized</var> and a <code>BroadcastChannel</code> <var>channel</var>,
run these steps:</p>

<ol>
lozy219 marked this conversation as resolved.
Show resolved Hide resolved
<li><p>If <var>channel</var>'s <span data-x="concept-BroadcastChannel-closed">closed
flag</span> is true, then abort these steps.</p></li>

lozy219 marked this conversation as resolved.
Show resolved Hide resolved
<li><p>Let <var>targetRealm</var> be <var>destination</var>'s <span
data-x="concept-relevant-realm">relevant realm</span>.</p></li>

<li>
<p>Let <var>data</var> be <span>StructuredDeserialize</span>(<var>serialized</var>,
<var>targetRealm</var>).</p>

<p>If this throws an exception, catch it, <span data-x="concept-event-fire">fire an
event</span> named <code data-x="event-messageerror">messageerror</code> at
<var>destination</var>, using <code>MessageEvent</code>, with the <code
data-x="dom-MessageEvent-origin">origin</code> attribute initialized to the <span
data-x="serialization of an origin">serialization</span> of <var>sourceOrigin</var>, and then
abort these steps.</p>
</li>

<li><p><span data-x="concept-event-fire">Fire an event</span> named <code
data-x="event-message">message</code> at <var>channel</var>, using <code>MessageEvent</code>,
with the <code data-x="dom-MessageEvent-data">data</code> attribute initialized to <var>data</var>
and the <code data-x="dom-MessageEvent-origin">origin</code> attribute initialized to the
<span data-x="serialization of an origin">serialization</span> of <var>sourceOrigin</var>.</p></li>
</ol>


<p>The <dfn method for="BroadcastChannel"><code
data-x="dom-BroadcastChannel-postMessage">postMessage(<var>message</var>)</code></dfn> method
steps are:</p>
Expand All @@ -113751,8 +113797,6 @@ interface <dfn interface>BroadcastChannel</dfn> : <span>EventTarget</span> {
match the following criteria:</p>

<ul>
<li><p>They are <span>eligible for messaging</span>.</p></li>

<li><p>The result of running <span>obtain a storage key for non-storage purposes</span> with
their <span>relevant settings object</span> <span data-x="storage key equal">equals</span>
<var>sourceStorageKey</var>.</p></li>
Expand All @@ -113764,6 +113808,31 @@ interface <dfn interface>BroadcastChannel</dfn> : <span>EventTarget</span> {

<li><p>Remove <var>source</var> from <var>destinations</var>.</p></li>

<li>
<p>For each <var>destination</var> in <var>destinations</var>, if <var>destination</var> is
not <span>eligible for messaging</span>, perform the following steps:</p>
<ul>
<li>
<p>If <var>destination</var>'s <span>relevant global object</span> is a <code>Window</code>
<var>window</var>, <span>enqueue</span> <var>serialized</var> on <span>message queue</span>.</p>
</li>

<li>
lozy219 marked this conversation as resolved.
Show resolved Hide resolved
<p>If the <span data-x="list size">size</span> of <var>destination</var>'s <span>message queue</span>
lozy219 marked this conversation as resolved.
Show resolved Hide resolved
is above a certain threshold, the user agent may <span data-x="destroy a document">destroy</span>
<var>window</var>'s <span data-x="concept-document-window">associated <code>Document</code></span>.</p>

<p class="note">The user agents can set their own threshold (possibly 0) to trigger the
<code>Document</code> destruction.</p>

<p class="note">The <code>Document</code> to be destroyed here is not <span>fully active</span>
(either detached or in <a href="#note-bfcache">bfcache</a>).</p>
</li>

<li><p>Remove <var>destination</var> from <var>destinations</var>.</p></li>
</ul>
</li>

<li><p>Sort <var>destinations</var> such that all <code>BroadcastChannel</code> objects whose
<span data-x="relevant agent">relevant agents</span> are the same are sorted in creation order,
oldest first. (This does not define a complete ordering. Within this constraint, user agents may
Expand All @@ -113772,34 +113841,8 @@ interface <dfn interface>BroadcastChannel</dfn> : <span>EventTarget</span> {
<li>
<p>For each <var>destination</var> in <var>destinations</var>, <span>queue a global task</span>
on the <span>DOM manipulation task source</span> given <var>destination</var>'s <span>relevant
global object</span> to perform the following steps:</p>

<ol>
<li><p>If <var>destination</var>'s <span data-x="concept-BroadcastChannel-closed">closed
flag</span> is true, then abort these steps.</p></li>

<li><p>Let <var>targetRealm</var> be <var>destination</var>'s <span
data-x="concept-relevant-realm">relevant realm</span>.</p></li>

<li>
<p>Let <var>data</var> be <span>StructuredDeserialize</span>(<var>serialized</var>,
<var>targetRealm</var>).</p>

<p>If this throws an exception, catch it, <span data-x="concept-event-fire">fire an
event</span> named <code data-x="event-messageerror">messageerror</code> at
<var>destination</var>, using <code>MessageEvent</code>, with the <code
data-x="dom-MessageEvent-origin">origin</code> attribute initialized to the <span
data-x="serialization of an origin">serialization</span> of <var>sourceOrigin</var>, and then
abort these steps.</p>
</li>

<li><p><span data-x="concept-event-fire">Fire an event</span> named <code
data-x="event-message">message</code> at <var>destination</var>, using
<code>MessageEvent</code>, with the <code data-x="dom-MessageEvent-data">data</code> attribute
initialized to <var>data</var> and the <code data-x="dom-MessageEvent-origin">origin</code>
attribute initialized to the <span data-x="serialization of an origin">serialization</span> of
<var>sourceOrigin</var>.</p></li>
</ol>
global object</span> to <span data-x="dom-BroadcastChannel-post-a-message">post a message</span>
with <var>serialized</var> to <var>destination</var>.</p>
</li>
</ol>

Expand Down