* 仕様書

[REFS[
- [1] '''[CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2015-04-25 04:40:19 +09:00]] 版) <https://html.spec.whatwg.org/#discard-a-document>'''
- [3] [CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2015-04-25 04:40:19 +09:00]] 版) <https://html.spec.whatwg.org/#unloading-document-cleanup-steps>
- [2] [CITE[File API]] ([TIME[2015-04-17 16:25:21 +09:00]] 版) <https://w3c.github.io/FileAPI/#BlobURLStore>
- [11] [CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2015-04-25 04:40:19 +09:00]] 版) <https://html.spec.whatwg.org/#the-worker's-lifetime:discard-a-document>
]REFS]

* 文書を捨てる

[7] [[閲覧文脈]]が[DFN[[RUBYB[[[文書]]を捨てる]@en[discard a [CODE(DOMi)@en[Document]]]]]]際は、
[VAR[文書]]について次のようにしなければ[MUST[なりません]] [SRC[>>1]]。

[FIG(steps)[
= [28] [VAR[文書]]の [F[''salvageable'']] を、[[偽]]に設定します。
= [29] [VAR[文書]]の [[unloading document cleanup steps]] を実行します。
= [30] [[[VAR[文書]]のabort]]を実行します。
= [31] [VAR[文書]]の[F[閲覧文脈]]の[F[イベントループ]]について[[[VAR[文書]]のタスクを削除]]します。
= [32] [VAR[文書]]の[F[[[子供閲覧文脈]]群]]の各[VAR[閲覧文脈]]について、
== [34] [[[VAR[閲覧文脈]]を捨てる]]処理を実行します。
= [35] [VAR[文書]]の[[閲覧文脈から文書への強い参照を破棄]]します。
= [36] 
[CODE[WorkerGlobalScope]] のうち、
[F[所有者集合]]に[VAR[文書]]が[[含まれる]]ものについて、
[F[所有者集合]]から[VAR[文書]]を[[削除][リストから削除]]します。
=- これにより[[ワーカー]]が [[permissible worker]], [[active needed worker]],
[[protected worker]], [[suspendable worker]] かどうかが変化するかもしれません。
]FIG]

[37] [[子供閲覧文脈]]の順序は[[仕様書]]に明記されていませんが、
影響は無いのでしょうか。 [CODE[unload]] の順序に影響しますから、
[[文書順]]か何かになっている必要がありそうですが。。。

[9] [[文書を捨てる]]処理は、[[閲覧文脈を捨てる]]処理から呼び出されることがあります。

[13] [[文書を捨てる]]処理は、[[unload a document]] から呼び出されることがあります。

[10] [[文書を捨てる]]処理は、[[セッション履歴]]の[[現在エントリー]]ではなく、
[[スクリプト]]からも参照されていない[[文書]]について、いつでも呼び出すことができます。

;; [[bfcache]] 参照。

;; [14] [[文書を捨てる]]処理が [[unload a document]] から呼び出される場合は、
[[文書]]は[[レンダリング]]中かもしれません。それ以外の場合は[[レンダリング]]中ではありません。

* unloading document cleanup steps

[4] [VAR[文書]]の [DFN[[[unloading document cleanup steps]]]] とは、次のものです。

[FIG(steps)[
= [16] [VAR[窓]]を、[VAR[文書]]の[F[[CODE(DOMi)@en[Window]]]]に設定します。
= [19] [VAR[窓]]の[F[[CODE(DOMi)@en[WebSocket]]群]]が空でなければ、
== [17] [VAR[窓]]の[F[[CODE(DOMi)@en[WebSocket]]群]]の各[VAR[オブジェクト]]について、
=== [18] [VAR[オブジェクト]]の [[make disappear]] を実行します [SRC[>>3]]。
== [20] [VAR[文書]]の[F[''salvageable'']]を、[[偽]]に設定します [SRC[>>3]]。
== [26] [VAR[窓]]の[F[[CODE(DOMi)@en[WebSocket]]群]]を、空にします。
= [21] [VAR[文書]]の[F[''salvageable'']]が[[偽]]なら、
== [22] [VAR[窓]]の[F[[CODE(DOMi)@en[EventSource]]群]]の各[VAR[オブジェクト]]について、
=== [23] [VAR[オブジェクト]]の [[forcibly close]] を実行します [SRC[>>3]]。
== [27] [VAR[窓]]の[F[[CODE(DOMi)@en[EventSource]]群]]を、空にします。
== [24] [VAR[文書]]の[F[活性タイマーのリスト]]を、空に設定します [SRC[>>3]]。
= [25] [VAR[文書]]の[F[[CODE(DOMi)@en[Window]]]] の [F[Blob URL Store]] の [[Blob URL]] をすべて削除します [SRC[>>2]]。
]FIG]

;; [6] [[WebRTC]] や [CODE(DOMm)@en[requestIdleCallback]] でも同様の処理が必要でしょうか?

[5] 本手順群は、 [[unload a document]] と[[文書を捨てる]]から呼び出されます。
[[unload a document]] は[[文書を捨てる]]を通して計2回本手順群を呼び出すことがあります。

;; 2回呼び出す場合には、その間で再帰的な [[unload a document]] の呼び出しがあるので、
そこで何かを破棄する必要が新たに生じているかもしれません。

* 歴史

[33] [CITE@en[Bug 12837 – Define unloading document cleanup steps]]
([TIME[2015-05-05 19:49:05 +09:00]] 版)
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=12837>

[8] [CITE@en[12837 – Define unloading document cleanup steps]]
([TIME[2016-08-13 11:48:00 +09:00]])
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=12837>

[15] [CITE@en[Clarify the unloading document cleanup steps]]
([[yuyokk]]著, [TIME[2016-12-29 05:13:30 +09:00]])
<https://github.com/whatwg/html/commit/ef139a459c3c84458ff232f099128b583157d220>

[38] [CITE@en[Editorial: restructure ownership of workers to parent-owners]]
([[annevk]]著, [TIME[2017-04-14 14:47:44 +09:00]])
<https://github.com/whatwg/html/commit/59a4750f475acd789ee436b4906972ba2081d8b3>

[39] [CITE@en[Editorial: move owner set manipulation into discarding a document]]
([[GloverDonovan]]著, [TIME[2018-02-21 05:07:06 +09:00]])
<https://github.com/whatwg/html/commit/a5586f115e4c982c22b4d9d583a20b26236f5991>

[12] 
>>39 以前は[F[所有者集合]]からの[[削除][リストから削除]]は別に規定されていました
[SRC[>>11]]。
[[ワーカー]]はメインの処理とは[[並列]]に動作していますから、
[[ワーカーの文書]]からの削除も[[文書を捨てる]]処理の他の手順とは[[非同期的]]に実行されるかもしれません。
(仕様書上で[[文書を捨てる]]処理の一部ではなく、[[ワーカー]]の章で別途規定されているのもそのためと考えられます。)
どのみち[[ワーカー]]の処理は[[並列]]に実行されていますから、
[[ワーカーの文書]]からの削除が同期的か非同期的かを[[著者]]が観測するのは困難 (不可能?)
と思われます。



[40] [CITE@en[Move owner set manipulation during document discarding · Issue #2533 · whatwg/html]]
([TIME[2018-02-21 10:34:59 +09:00]])
<https://github.com/whatwg/html/issues/2533>

[41] [CITE@en[Move owner set manipulation into discarding a document by GloverDonovan · Pull Request #3473 · whatwg/html]]
([TIME[2018-02-21 10:35:11 +09:00]])
<https://github.com/whatwg/html/pull/3473>

[42] [CITE@en[Editorial: modernize the style of various document-accepting algorithms]]
([[musgravejw]]著, [TIME[2018-05-03 01:20:53 +09:00]])
<https://github.com/whatwg/html/commit/8bd6fb3972aab1debd2c562b31617e7d3da2377b>

[43] [CITE@en[Various algorithms should take an explicit document argument · Issue #3638 · whatwg/html]]
([TIME[2018-05-03 11:19:57 +09:00]])
<https://github.com/whatwg/html/issues/3638>

[44] [CITE@en[Editorial: Add Document to argument list explicitly for document algorithms by musgravejw · Pull Request #3639 · whatwg/html]]
([TIME[2018-05-03 11:20:23 +09:00]])
<https://github.com/whatwg/html/pull/3639>

[45] [CITE@en[document.open() simplifications, part 1]]
([[TimothyGu]]著, [TIME[2018-08-17 07:20:44 +09:00]])
<https://github.com/whatwg/html/commit/6f769b8089a843066aa19f5991405bf4c84458b3>