Skip to content

Commit

Permalink
Update StructuredCloneWithTransfer() usage
Browse files Browse the repository at this point in the history
Fixes w3c#1089.
  • Loading branch information
domenic committed Apr 4, 2017
1 parent 7079952 commit 173475e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions docs/index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
1. Let |targetRealm| be |destination|'s [=global object/Realm=].
1. Let |incumbentSettings| be the <a>incumbent settings object</a>, and |incumbentGlobal| its [=environment settings object/global object=].
1. Let |cloneRecord| be <a abstract-op>StructuredCloneWithTransfer</a>(|message|, |transfer|, |targetRealm|). If this <a>throws</a> an exception, <a lt="throw">rethrow</a> that exception and abort these steps.
1. Let |clonedMessage| be |cloneRecord|.\[[Clone]].
1. Let |newPorts| be a new [=frozen array type|frozen array=] consisting of all {{MessagePort}} objects in |cloneRecord|.\[[TransferList]], if any, maintaining their relative order.
1. Let |clonedMessage| be |cloneRecord|.\[[Deserialized]].
1. Let |newPorts| be a new [=frozen array type|frozen array=] consisting of all {{MessagePort}} objects in |cloneRecord|.\[[TransferredValues]], if any, maintaining their relative order.
1. <a>Queue a task</a> that runs the following steps:
1. Create an event |e| that uses the {{ExtendableMessageEvent}} interface, with the event type {{message!!event}}, which does not bubble and is not cancelable.
1. Let the {{ExtendableMessageEvent/data}} attribute of |e| be initialized to |clonedMessage|.
Expand Down Expand Up @@ -1019,8 +1019,8 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
1. If |destination| is null, <a>throw</a> an "{{InvalidStateError}}" exception.
1. Let |targetRealm| be |destination|'s <a>relevant Realm</a>.
1. Let |cloneRecord| be <a abstract-op>StructuredCloneWithTransfer</a>(|message|, |transfer|, |targetRealm|). If this <a>throws</a> an exception, <a lt="throw">rethrow</a> that exception and abort these steps.
1. Let |clonedMessage| be |cloneRecord|.\[[Clone]].
1. Let |newPorts| be a new [=frozen array type|frozen array=] consisting of all {{MessagePort}} objects in |cloneRecord|.\[[TransferList]], if any, maintaining their relative order.
1. Let |clonedMessage| be |cloneRecord|.\[[Deserialized]].
1. Let |newPorts| be a new [=frozen array type|frozen array=] consisting of all {{MessagePort}} objects in |cloneRecord|.\[[TransferredValues]], if any, maintaining their relative order.
1. Add a <a>task</a> that runs the following steps to |destination|'s [=ServiceWorkerContainer/client message queue=]:
1. Create an event |e| that uses the {{MessageEvent}} interface, with the event type {{Window/message!!event}}, which does not bubble and is not cancelable.
1. Let the <a attribute for="MessageEvent">data</a> attribute of |e| be initialized to |clonedMessage|.
Expand Down
32 changes: 16 additions & 16 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@
<div class="head">
<p data-fill-with="logo"><a class="logo" href="https://www.w3.org/"> <img alt="W3C" height="48" src="https://www.w3.org/StyleSheets/TR/2016/logos/W3C" width="72"> </a> </p>
<h1 class="p-name no-ref" id="title">Service Workers Nightly</h1>
<h2 class="no-num no-toc no-ref heading settled" id="subtitle"><span class="content">Editor’s Draft, <time class="dt-updated" datetime="2017-04-02">2 April 2017</time></span></h2>
<h2 class="no-num no-toc no-ref heading settled" id="subtitle"><span class="content">Editor’s Draft, <time class="dt-updated" datetime="2017-04-04">4 April 2017</time></span></h2>
<div data-fill-with="spec-metadata">
<dl>
<dt>This version:
Expand Down Expand Up @@ -1856,12 +1856,12 @@ <h2 class="heading settled" data-level="3" id="document-context"><span class="se
<a class="self-link" href="#example-7e94092e"></a> Bootstrapping with a service worker:
<pre class="highlight"><span class="c1">// scope defaults to the path the script sits in
</span><span class="c1">// "/" in this example
</span>navigator<span class="p">.</span>serviceWorker<span class="p">.</span>register<span class="p">(</span><span class="s2">"/serviceworker.js"</span><span class="p">)</span><span class="p">.</span>then<span class="p">(</span>registration <span class="o">=></span> <span class="p">{</span>
</span>navigator<span class="p">.</span>serviceWorker<span class="p">.</span>register<span class="p">(</span><span class="s2">"/serviceworker.js"</span><span class="p">)</span><span class="p">.</span>then<span class="p">(</span>registration <span class="p">=></span> <span class="p">{</span>
console<span class="p">.</span>log<span class="p">(</span><span class="s2">"success!"</span><span class="p">)</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span>registration<span class="p">.</span>installing<span class="p">)</span> <span class="p">{</span>
registration<span class="p">.</span>installing<span class="p">.</span>postMessage<span class="p">(</span><span class="s2">"Howdy from your installing page."</span><span class="p">)</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span><span class="p">,</span> err <span class="o">=></span> <span class="p">{</span>
<span class="p">}</span><span class="p">,</span> err <span class="p">=></span> <span class="p">{</span>
console<span class="p">.</span>error<span class="p">(</span><span class="s2">"Installing the worker failed!"</span><span class="p">,</span> err<span class="p">)</span><span class="p">;</span>
<span class="p">}</span><span class="p">)</span><span class="p">;</span>
</pre>
Expand Down Expand Up @@ -1923,9 +1923,9 @@ <h4 class="heading settled" data-level="3.1.3" id="service-worker-postmessage"><
<li data-md="">
<p>Let <var>cloneRecord</var> be <a data-link-type="abstract-op" href="https://html.spec.whatwg.org/multipage/infrastructure.html#structuredclonewithtransfer">StructuredCloneWithTransfer</a>(<var>message</var>, <var>transfer</var>, <var>targetRealm</var>). If this <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-throw">throws</a> an exception, <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-throw">rethrow</a> that exception and abort these steps.</p>
<li data-md="">
<p>Let <var>clonedMessage</var> be <var>cloneRecord</var>.[[Clone]].</p>
<p>Let <var>clonedMessage</var> be <var>cloneRecord</var>.[[Deserialized]].</p>
<li data-md="">
<p>Let <var>newPorts</var> be a new <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-frozen-array-type">frozen array</a> consisting of all <code class="idl"><a data-link-type="idl" href="https://html.spec.whatwg.org/multipage/comms.html#messageport">MessagePort</a></code> objects in <var>cloneRecord</var>.[[TransferList]], if any, maintaining their relative order.</p>
<p>Let <var>newPorts</var> be a new <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-frozen-array-type">frozen array</a> consisting of all <code class="idl"><a data-link-type="idl" href="https://html.spec.whatwg.org/multipage/comms.html#messageport">MessagePort</a></code> objects in <var>cloneRecord</var>.[[TransferredValues]], if any, maintaining their relative order.</p>
<li data-md="">
<p><a data-link-type="dfn" href="https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-task">Queue a task</a> that runs the following steps:</p>
<ol>
Expand Down Expand Up @@ -2390,10 +2390,10 @@ <h2 class="heading settled" data-level="4" id="execution-context"><span class="s
<div class="example" id="example-744d6b8b">
<a class="self-link" href="#example-744d6b8b"></a> Serving Cached Resources:
<pre class="highlight"><span class="c1">// caching.js
</span>self<span class="p">.</span>addEventListener<span class="p">(</span><span class="s2">"install"</span><span class="p">,</span> event <span class="o">=></span> <span class="p">{</span>
</span>self<span class="p">.</span>addEventListener<span class="p">(</span><span class="s2">"install"</span><span class="p">,</span> event <span class="p">=></span> <span class="p">{</span>
event<span class="p">.</span>waitUntil<span class="p">(</span>
<span class="c1">// Open a cache of resources.
</span> caches<span class="p">.</span>open<span class="p">(</span><span class="s2">"shell-v1"</span><span class="p">)</span><span class="p">.</span>then<span class="p">(</span>cache <span class="o">=></span> <span class="p">{</span>
</span> caches<span class="p">.</span>open<span class="p">(</span><span class="s2">"shell-v1"</span><span class="p">)</span><span class="p">.</span>then<span class="p">(</span>cache <span class="p">=></span> <span class="p">{</span>
<span class="c1">// Begins the process of fetching them.
</span> <span class="c1">// The coast is only clear when all the resources are ready.
</span> <span class="k">return</span> cache<span class="p">.</span>addAll<span class="p">(</span><span class="p">[</span>
Expand All @@ -2407,16 +2407,16 @@ <h2 class="heading settled" data-level="4" id="execution-context"><span class="s
<span class="p">)</span><span class="p">;</span>
<span class="p">}</span><span class="p">)</span><span class="p">;</span>

self<span class="p">.</span>addEventListener<span class="p">(</span><span class="s2">"fetch"</span><span class="p">,</span> event <span class="o">=></span> <span class="p">{</span>
self<span class="p">.</span>addEventListener<span class="p">(</span><span class="s2">"fetch"</span><span class="p">,</span> event <span class="p">=></span> <span class="p">{</span>
<span class="c1">// No "fetch" events are dispatched to the service worker until it
</span> <span class="c1">// successfully installs and activates.
</span>
<span class="c1">// All operations on caches are async, including matching URLs, so we use
</span> <span class="c1">// promises heavily. e.respondWith() even takes promises to enable this:
</span> event<span class="p">.</span>respondWith<span class="p">(</span>
caches<span class="p">.</span>match<span class="p">(</span>e<span class="p">.</span>request<span class="p">)</span><span class="p">.</span>then<span class="p">(</span>response <span class="o">=></span> <span class="p">{</span>
caches<span class="p">.</span>match<span class="p">(</span>e<span class="p">.</span>request<span class="p">)</span><span class="p">.</span>then<span class="p">(</span>response <span class="p">=></span> <span class="p">{</span>
<span class="k">return</span> response <span class="o">||</span> fetch<span class="p">(</span>e<span class="p">.</span>request<span class="p">)</span><span class="p">;</span>
<span class="p">}</span><span class="p">)</span><span class="p">.</span><span class="k">catch</span><span class="p">(</span><span class="p">(</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
<span class="p">}</span><span class="p">)</span><span class="p">.</span><span class="k">catch</span><span class="p">(</span><span class="p">(</span><span class="p">)</span> <span class="p">=></span> <span class="p">{</span>
<span class="k">return</span> caches<span class="p">.</span>match<span class="p">(</span><span class="s2">"/fallback.html"</span><span class="p">)</span><span class="p">;</span>
<span class="p">}</span><span class="p">)</span>
<span class="p">)</span><span class="p">;</span>
Expand Down Expand Up @@ -2563,9 +2563,9 @@ <h4 class="heading settled" data-level="4.2.5" id="client-postmessage"><span cla
<li data-md="">
<p>Let <var>cloneRecord</var> be <a data-link-type="abstract-op" href="https://html.spec.whatwg.org/multipage/infrastructure.html#structuredclonewithtransfer">StructuredCloneWithTransfer</a>(<var>message</var>, <var>transfer</var>, <var>targetRealm</var>). If this <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-throw">throws</a> an exception, <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-throw">rethrow</a> that exception and abort these steps.</p>
<li data-md="">
<p>Let <var>clonedMessage</var> be <var>cloneRecord</var>.[[Clone]].</p>
<p>Let <var>clonedMessage</var> be <var>cloneRecord</var>.[[Deserialized]].</p>
<li data-md="">
<p>Let <var>newPorts</var> be a new <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-frozen-array-type">frozen array</a> consisting of all <code class="idl"><a data-link-type="idl" href="https://html.spec.whatwg.org/multipage/comms.html#messageport">MessagePort</a></code> objects in <var>cloneRecord</var>.[[TransferList]], if any, maintaining their relative order.</p>
<p>Let <var>newPorts</var> be a new <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-frozen-array-type">frozen array</a> consisting of all <code class="idl"><a data-link-type="idl" href="https://html.spec.whatwg.org/multipage/comms.html#messageport">MessagePort</a></code> objects in <var>cloneRecord</var>.[[TransferredValues]], if any, maintaining their relative order.</p>
<li data-md="">
<p>Add a <a data-link-type="dfn" href="https://html.spec.whatwg.org/multipage/webappapis.html#concept-task">task</a> that runs the following steps to <var>destination</var>’s <a data-link-type="dfn" href="#dfn-client-message-queue" id="ref-for-dfn-client-message-queue-6">client message queue</a>:</p>
<ol>
Expand Down Expand Up @@ -6550,7 +6550,7 @@ <h3 class="heading settled" id="service-worker-script-response"><span class="con
<a class="self-link" href="#example-bf118df6"></a> Default scope:
<pre class="highlight"><span class="c1">// Maximum allowed scope defaults to the path the script sits in
</span><span class="c1">// "/js" in this example
</span>navigator<span class="p">.</span>serviceWorker<span class="p">.</span>register<span class="p">(</span><span class="s2">"/js/sw.js"</span><span class="p">)</span><span class="p">.</span>then<span class="p">(</span><span class="p">(</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
</span>navigator<span class="p">.</span>serviceWorker<span class="p">.</span>register<span class="p">(</span><span class="s2">"/js/sw.js"</span><span class="p">)</span><span class="p">.</span>then<span class="p">(</span><span class="p">(</span><span class="p">)</span> <span class="p">=></span> <span class="p">{</span>
console<span class="p">.</span>log<span class="p">(</span><span class="s2">"Install succeeded with the default scope '/js'."</span><span class="p">)</span><span class="p">;</span>
<span class="p">}</span><span class="p">)</span><span class="p">;</span>
</pre>
Expand All @@ -6559,7 +6559,7 @@ <h3 class="heading settled" id="service-worker-script-response"><span class="con
<a class="self-link" href="#example-76c5aa8b"></a> Upper path without Service-Worker-Allowed header:
<pre class="highlight"><span class="c1">// Set the scope to an upper path of the script location
</span><span class="c1">// Response has no Service-Worker-Allowed header
</span>navigator<span class="p">.</span>serviceWorker<span class="p">.</span>register<span class="p">(</span><span class="s2">"/js/sw.js"</span><span class="p">,</span> <span class="p">{</span> scope<span class="o">:</span> <span class="s2">"/"</span> <span class="p">}</span><span class="p">)</span><span class="p">.</span><span class="k">catch</span><span class="p">(</span><span class="p">(</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
</span>navigator<span class="p">.</span>serviceWorker<span class="p">.</span>register<span class="p">(</span><span class="s2">"/js/sw.js"</span><span class="p">,</span> <span class="p">{</span> scope<span class="o">:</span> <span class="s2">"/"</span> <span class="p">}</span><span class="p">)</span><span class="p">.</span><span class="k">catch</span><span class="p">(</span><span class="p">(</span><span class="p">)</span> <span class="p">=></span> <span class="p">{</span>
console<span class="p">.</span>error<span class="p">(</span><span class="s2">"Install failed due to the path restriction violation."</span><span class="p">)</span><span class="p">;</span>
<span class="p">}</span><span class="p">)</span><span class="p">;</span>
</pre>
Expand All @@ -6568,7 +6568,7 @@ <h3 class="heading settled" id="service-worker-script-response"><span class="con
<a class="self-link" href="#example-49d267fe"></a> Upper path with Service-Worker-Allowed header:
<pre class="highlight"><span class="c1">// Set the scope to an upper path of the script location
</span><span class="c1">// Response included "Service-Worker-Allowed : /"
</span>navigator<span class="p">.</span>serviceWorker<span class="p">.</span>register<span class="p">(</span><span class="s2">"/js/sw.js"</span><span class="p">,</span> <span class="p">{</span> scope<span class="o">:</span> <span class="s2">"/"</span> <span class="p">}</span><span class="p">)</span><span class="p">.</span>then<span class="p">(</span><span class="p">(</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
</span>navigator<span class="p">.</span>serviceWorker<span class="p">.</span>register<span class="p">(</span><span class="s2">"/js/sw.js"</span><span class="p">,</span> <span class="p">{</span> scope<span class="o">:</span> <span class="s2">"/"</span> <span class="p">}</span><span class="p">)</span><span class="p">.</span>then<span class="p">(</span><span class="p">(</span><span class="p">)</span> <span class="p">=></span> <span class="p">{</span>
console<span class="p">.</span>log<span class="p">(</span><span class="s2">"Install succeeded as the max allowed scope was overriden to '/'."</span><span class="p">)</span><span class="p">;</span>
<span class="p">}</span><span class="p">)</span><span class="p">;</span>
</pre>
Expand All @@ -6577,7 +6577,7 @@ <h3 class="heading settled" id="service-worker-script-response"><span class="con
<a class="self-link" href="#example-7a18b25f"></a> A path restriction voliation even with Service-Worker-Allowed header:
<pre class="highlight"><span class="c1">// Set the scope to an upper path of the script location
</span><span class="c1">// Response included "Service-Worker-Allowed : /foo"
</span>navigator<span class="p">.</span>serviceWorker<span class="p">.</span>register<span class="p">(</span><span class="s2">"/foo/bar/sw.js"</span><span class="p">,</span> <span class="p">{</span> scope<span class="o">:</span> <span class="s2">"/"</span> <span class="p">}</span><span class="p">)</span><span class="p">.</span><span class="k">catch</span><span class="p">(</span><span class="p">(</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span>
</span>navigator<span class="p">.</span>serviceWorker<span class="p">.</span>register<span class="p">(</span><span class="s2">"/foo/bar/sw.js"</span><span class="p">,</span> <span class="p">{</span> scope<span class="o">:</span> <span class="s2">"/"</span> <span class="p">}</span><span class="p">)</span><span class="p">.</span><span class="k">catch</span><span class="p">(</span><span class="p">(</span><span class="p">)</span> <span class="p">=></span> <span class="p">{</span>
console<span class="p">.</span>error<span class="p">(</span><span class="s2">"Install failed as the scope is still out of the overriden maximum allowed scope."</span><span class="p">)</span><span class="p">;</span>
<span class="p">}</span><span class="p">)</span><span class="p">;</span>
</pre>
Expand Down

0 comments on commit 173475e

Please sign in to comment.