[53] [DFN[[RUBYB[共有ワーカー]@en[shared worker]]]]は、[[起源]]単位で実行される[[ワーカー]]です。
[[ページ]] ([[閲覧文脈]]) を超えて共有されることからこの名前があります。

* 仕様書

[REFS[
- [2] '''[CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2015-05-06 10:42:35 +09:00]] 版) <https://html.spec.whatwg.org/#sharedworker>'''
- [98] [CITE@en-US-x-hixie[HTML Standard]], [TIME[2021-04-12T15:12:42.000Z]], [TIME[2021-04-13T08:04:32.751Z]] <https://html.spec.whatwg.org/#shared-worker-agent>
]REFS]

* 状態

[1] [CODE(DOMi)@en[[[SharedWorker]]]] には、
[CODE(DOMi)@en[[[EventTarget]]]] の状態に加えて、次の状態があります。
[FIG(list members)[
:関連付けられた[[ワーカー]]:
:[CODE(DOMa)@en[[[port]]]]:

[HISTORY[
: [F[応用キャッシュ]] :
]HISTORY]
]FIG]

[18] [CODE(DOMi)@en[[[SharedWorkerGlobalScope]]]] の状態は、
[CODE(DOMi)@en[[[WorkerGlobalScope]]]] 参照。

-*-*-

[90] [[利用者エージェント]]は、
[DFN[[RUBYB[[[共有ワーカー管理器]]]@en[shared worker manager]]]]を持ちます。
その値は、[[新しい並列キューの開始]]の結果です。 [SRC[[CITE[HTML Standard]]]]

;; [91] 仕様書上は、
[[利用者エージェント]]の開始時に作成され、以後終了まで動作し続ける唯一の[[並列キュー]]として規定されています。
実際上は[[起源]]ごとに存在する形にすることができます (違いは[[著者]]に直接観察されません)。

* メンバー

[FIG(short list)[ [79] [CODE(DOMi)@en[SharedWorkerGlobalScope]] の[[メンバー][インターフェイスメンバー]]

[CODE(DOMi)@en[WorkerGlobalScope]] の[[メンバー][インターフェイスメンバー]]に加えて:

[HISTORY[
- [CODE(DOMa)@en[applicationCache]]
]HISTORY]
]FIG]

* コンストラクター

[84] [[コンストラクター]]は、次の[[引数]]を受け取ります。

[FIG(list members)[
: [VAR[番地]] : [[共有ワーカー]]の[[スクリプト]]の [[URL]]。必須。
[[相対URL]]は、[[基底URL]]に対するものとして[[解決]]されます。
: [VAR[オプション群]] :
[CODE(DOMi)@en[WorkerOptions]] の[[辞書]]。
または、古い構文で、[[ワーカー名]]を表す[[文字列]]。
省略可能。
]FIG]

[3] [[コンストラクター]]は、次のようにしなければ[MUST[なりません]] [SRC[>>2]]。

[FIG(steps)[
= [4] [[引数]]として次のものが指定されたと解釈します。
[FIG(list members)[
: [VAR[番地]] : [CODE(DOMi)@en[USVString]] (必須)
: [VAR[オプション群]] : 
[CODE(DOMi)@en[DOMString]] または
[CODE(DOMi)@en[[[WorkerOptions]]]] (省略可能)
]FIG]
= [5] [[利用者エージェント]]の方針次第で、
== [25] [CODE(DOMe)@en[[[SecurityError]]]] [[例外]]を[[投げ]]、ここで停止します。
= [35] [VAR[オプション群]]が [CODE(DOMi)@en[DOMString]] の場合、
== [59] [VAR[オプション群]]を、新しい [CODE(DOMi)@en[WorkerOptions]] に設定します。
[FIG(list members)[ [83] [CODE(DOMi)@en[WorkerOptions]]
: [CODE(DOMa)@en[name][ワーカー名]] : [VAR[オプション群]]
]FIG]
= [10] [VAR[外側設定群]]を、[[現職設定群オブジェクト]]に設定します。
= [6] [VAR[URL]]を、[VAR[番地]]を[VAR[外側設定群]]に対して[[構文解析][URLの構文解析]]した結果に設定します [SRC[>>2]]。
= [7] [VAR[URL]]が[[失敗]]なら、
== [21] [CODE(DOMe)@en[[[SyntaxError]]]] [[例外]]を[[投げ]]、ここで停止します。
= [12] [VAR[ワーカー]]を、新しい [CODE(DOMi)@en[[[SharedWorker]]]] に設定します。
= [13] [VAR[外側ポート]]を、[[新しい[CODE(DOMi)@en[MessagePort]]を作成]]した結果に設定します。
[FIG(list members)[
: [VAR[所有子]] : [VAR[外側設定群]]
]FIG]
= [14] [VAR[ワーカー]]の [F[[CODE(DOMa)@en[port]]]] を、[VAR[外側ポート]]に設定します。
= [76] [VAR[外側ポート]]を[[出荷]]します。 [SRC[[[仕様書]]になし]]
= [68] [VAR[外側保安文脈か]]を、
[VAR[外側設定群]]について [[Is environment settings object a secure context?]]
を適用した結果に設定します。
= [89] [[利用者エージェント]]の[F[共有ワーカー管理器]]に、
続きの処理 (>>44) の[[手順群をキューに追加]]します。
= [51] [VAR[ワーカー]]を返します。
]FIG]

[44] 続きの処理とは、次のようなものです [SRC[>>2]]。

[FIG(steps)[
= [16] [VAR[ワーカー大域適用範囲]]を、次の条件を''すべて''満たす[[オブジェクト]]に設定します。
[FIG(list)[
- [17] [CODE(DOMi)@en[SharedWorkerGlobalScope]] である
- [19] [F[closing flag]] が[[偽]]
- [50] [F[構築器起源]]と[VAR[外側設定群]]の[F[起源]]が[[同じ起源]]
- [28] [F[構築器URL]]が[VAR[URL]]と[[等しい][URL等価]]
- [20] [F[ワーカーの名前]]が[VAR[オプション群]]の [CODE(DOMa)@en[name][ワーカー名]] と等しい
- [30] [VAR[外側設定群]]の[[スクリプト]]からアクセスされないよう[[利用者エージェント]]が設定されている (例えば[[開発モード]]) わけでは''ない''
]FIG]
= [22] [VAR[ワーカー大域適用範囲]]が [CODE[null]] でなければ、
== [64] [VAR[内側設定群]]を、[VAR[ワーカー大域適用範囲]]の[F[設定群オブジェクト]]に設定します。
== [63] [VAR[外側保安文脈か]]と[VAR[内側設定群]]について
[[Is environment settings object a secure context?]] を適用した結果が等しくなければ、
=== [70] [[タスクをキューに追加]]します。
[FIG(list members)[ [71] [[タスク]]
:[F[タスク源]]:[[DOM操作タスク源]]
:[F[操作]]:
[FIG(steps)[
= [72] [[単純イベントを発火]]します。
[FIG(list members middle)[ [73] [[単純イベント]]
:[F[イベント型]]:[CODE(DOMe)@en[error]]
: [VAR[[[対象][イベント対象]]]] :
@@ XXX
]FIG]
]FIG]
]FIG]
=== [65] ここで停止します。
== [36] [VAR[ワーカー]]と[VAR[ワーカー大域適用範囲]]を関連付けます。
== [37] [VAR[内側ポート]]を、
[VAR[所有者]]を[VAR[内側設定群]]として[[新しい[CODE(DOMi)@en[MessagePort]]オブジェクトを作成]]した結果に設定します。
== [38] [VAR[外側ポート]]と[VAR[内側ポート]]を [[entangle]] します。
== [77] [VAR[内側ポート]]を[[出荷]]します。 [SRC[[[仕様書]]になし]]
== [40] [[タスクをキューに追加]]します。
[FIG(list members)[ [69] [[タスク]]
:[F[タスク源]]:[[DOM操作タスク源]]
:[F[操作]]:
[FIG(steps)[
= [57] [[イベント]]を[[発火]]します。
[FIG(list members middle)[ [11] [[イベント]]
:[F[インターフェイス]]:[CODE(DOMi)@en[[[MessageEvent]]]]
:[F[[[イベント型]]]]:[CODE(DOMe)@en[[[connect]]]]
:[F[[[対象]]]]: [VAR[大域オブジェクト]]
:[F[[[trusted]]]]:[[真]]
:[F[[[bubbles]]]]:[[偽]]
:[F[[[取消可能]]]]:[[偽]]
:[F[[CODE(DOMa)@en[[[data]]]]]]:[[空文字列]]
:[F[[CODE(DOMa)@en[[[ports]]]]]]: [VAR[内側ポート]]のみの[[凍結配列]]
:[F[[CODE(DOMa)@en[[[source]]]]]]: [VAR[内側ポート]]
]FIG]
]FIG]
]FIG]
== [47] [VAR[所有子]]を、[VAR[外側設定群]]の[[追加する関連所有子]]に設定します。
== [41] [VAR[所有子]]を[VAR[ワーカー大域適用範囲]]の[F[所有子集合]]に[[追加][list append]]します。
== [42] [VAR[所有子]]が [CODE(DOMi)@en[WorkerGlobalScope]] の場合、
=== [43] [VAR[ワーカー大域適用範囲]]を[VAR[所有子]]の[F[ワーカー集合]]に[[追加][list append]]します。
= [32] それ以外なら、
== [54] [[ワーカーを走らせます][run a worker]]。
[FIG(list members middle)[
: [VAR[ワーカー]] : [VAR[ワーカー]]
: [VAR[URL]] : [VAR[URL]]
: [VAR[外側設定群]] : [VAR[外側設定群]]
: [VAR[外側ポート]] : [VAR[外側ポート]]
: [VAR[オプション群]] : [VAR[オプション群]]
]FIG]
]FIG]

[55] [[利用者エージェント]]の方針とは、この[[ページ]]では[[ワーカー]]を実行させない、
といった指定が想定されている [SRC[>>2]] ようです。

[81] [[Chrome]] の[[シークレットウィンドウ]]では、
[CODE[SharedWorker]] [[オブジェクト]]が返されて成功したように見えますが、
指定された[[スクリプトのfetch]] は行われていないようです。
[TIME[2017-04-23T06:36:07.300Z]]

[100] 
最近の [[Chrome]] の通常 (非 incognito) [[insecure context]] では、
[CODE[SharedWorker]] [[オブジェクト]]が返されて成功したように見えますが、
指定された[[スクリプトのfetch]]は [[DevTools]] によると (pending) 
のまま実行されず (エラーにもならず) 何も実行されません。
[[insecure context]] であることが理由なのかどうかは定かではないのですが、
同じ[[ドメイン]]でも [CODE[https:]] なら普通に [[fetch]], 実行できます。
[TIME[2022-11-06T12:35:34.00Z]]

[101] [[Chrome]] の[[タスク マネージャ]]には[[共有ワーカー]]タスクが出てくるので何らかの何かはしようとしているらしい、でも実際には何もなされない謎。


[29] [CODE(DOMi)@en[[[SharedWorkerGlobalScope]]]] は、
[DFN[[F[[RUBYB[[[構築器URL]]]@en[constructor url]]]]]]と[DFN[[F[[RUBYB[[[構築器起源]]]@en[constructor origin]]]]]]を持ちます [SRC[>>23]]。
これは以後の[[構築器]]呼び出し時の比較のために用いられます。

;; 通常は[[ワーカーのURL]]と一致しますが、[[リダイレクト]]等により異なる値となるかしれません。

* 共有の範囲

[56] [[共有ワーカー]]は、[[起源]]単位で共有されます。

[58] [[private browsing]] と通常モードは、異なる[[利用者エージェント]]として扱われ、
[[共有ワーカー]]もそれぞれ別途に実行されます。

[61] [[開発者向けなどの理由][開発モード]]で他の[[ページ]]と[[共有ワーカー]]を共有しない動作モードも想定されています (>>30)。

* 共有ワーカーエージェント

[97] 
[DFN[[RUBYB[[RUBY[共有][きょうゆう]]ワーカーエージェント][shared worker agent]]]]は、
[CODE[SharedWorkerGlobalScope]] を1つ含む[[エージェント]]です。
[SRC[>>98]]


* 歴史

[31] [CITE@en[Give workers a base URL and clean up shared workers · whatwg/html@b620471]] ([TIME[2015-11-06 20:41:00 +09:00]] 版) <https://github.com/whatwg/html/commit/b620471ff823e8e6507b077cf0785a530762f5e3>

[60] [CITE@en[Make shared workers always key on constructor url and name · whatwg/html@b261d16]] ([TIME[2015-11-06 22:29:32 +09:00]] 版) <https://github.com/whatwg/html/commit/b261d162b451a646d5597689f0b831a722a19182>

[8] [CITE@en[Fix #337: perform origin checks for workers in Fetch · whatwg/html@e86b2e4]] ([TIME[2015-11-19 16:22:56 +09:00]] 版) <https://github.com/whatwg/html/commit/e86b2e4b8fdef45404c61c1a85c40da7edf8b561>

[9] [CITE@en[Remove shared workers? · Issue #315 · whatwg/html]]
([TIME[2015-12-01 11:43:55 +09:00]] 版)
<https://github.com/whatwg/html/issues/315>

[24] [CITE@en[Rewrite global object initialization to delegate to ES · whatwg/html@cf0355d]]
([TIME[2015-12-16 20:12:48 +09:00]] 版)
<https://github.com/whatwg/html/commit/cf0355d7e0e229b98f7fbd51b8c7608010c787f5>

[26] [CITE@en[URLs are parsed and produce records · whatwg/html@30bc255]]
([TIME[2016-02-14 23:05:51 +09:00]] 版)
<https://github.com/whatwg/html/commit/30bc2557105ad62881ec9670f253febbc9761b44>

[27] [CITE@en[Add module workers · whatwg/html@e3a5bb7]]
([TIME[2016-02-15 23:07:44 +09:00]] 版)
<https://github.com/whatwg/html/commit/e3a5bb757f64374c37d8c4528c01298463ef0b2a>

[33] [CITE@en[Make script fetching more correct with regard to request destinations · whatwg/html@6dc9c9e]]
([TIME[2016-02-29 18:11:48 +09:00]] 版)
<https://github.com/whatwg/html/commit/6dc9c9e1efcc3122294fb015e4a4eea66456a611>

[34] [CITE@en[Use FrozenArray for Navigator#languages and MessageEvent#ports · whatwg/html@e4df68a]]
([TIME[2016-03-25 13:58:03 +09:00]] 版)
<https://github.com/whatwg/html/commit/e4df68a41b86753c7fcdd0d8ea4615f63ffc87e9>

[39] [CITE@en[Clarify settings object, realm, and global relationships · whatwg/html@0866f1b]]
([TIME[2016-03-28 00:43:39 +09:00]] 版)
<https://github.com/whatwg/html/commit/0866f1b3f4b4ea5a99a30909e9bbe557dea0b460>

[46] [CITE@en[Use outside settings object to fetch workers]]
( ([[domenic]]著, [TIME[2016-05-11 19:57:24 +09:00]]))
<https://github.com/whatwg/html/commit/4e2b0066cb88fdc7ac65cc6e6a7eb40ac39418bd>

[45] [CITE@en[Use USVString for all URLs]]
( ([[domenic]]著, [TIME[2016-05-20 22:02:29 +09:00]]))
<https://github.com/whatwg/html/commit/018b983b77b2cd908f6d00100e7e0abe893dd2c3>

[49] [CITE@en[Regression fix: add back origin check for shared workers]]
( ([[annevk]]著, [TIME[2016-05-28 13:26:06 +09:00]]))
<https://github.com/whatwg/html/commit/18c891f1a300a336d3df5b76fdae3b1d77b0b468>

[52] [CITE@en[Use current instead of incumbent + entry in worker constructors]]
([[domenic]]著, [TIME[2016-07-07 01:46:49 +09:00]])
<https://github.com/whatwg/html/commit/485c76a84611d094839b3b60d13e6e997594800b>

[62] [CITE@en[Upstream SharedWorker constructor changes from Secure Contexts]]
([[mikewest]]著, [TIME[2016-07-16 04:21:28 +09:00]])
<https://github.com/whatwg/html/commit/45e2322598e041bd7d0d57b71f04f68c409854ac>

[66] [CITE@en[SharedWorker is upstreamed.]]
([[mikewest]]著, [TIME[2016-07-16 14:55:16 +09:00]])
<https://github.com/w3c/webappsec-secure-contexts/commit/08a3c187153f3cbf9aedbabe40df94b6b632d8c1>

[67] [CITE@en[Run more of the SharedWorker constructor in parallel]]
([[annevk]]著, [TIME[2016-09-28 20:44:14 +09:00]])
<https://github.com/whatwg/html/commit/ca9b26bd84fda9c5ddcc8c736326fc3b43715e9e>

[74] [CITE@en[Do not throw from cross-process code in the SharedWorker constructor]]
([[annevk]]著, [TIME[2016-09-28 20:34:23 +09:00]])
<https://github.com/whatwg/html/commit/2bcde4279b32e7412e014628eaa0c96807345137>

[75] [CITE@en[Fix regression in setting up SharedWorkerGlobalScope]]
([[domenic]]著, [TIME[2016-10-05 02:29:37 +09:00]])
<https://github.com/whatwg/html/commit/17525591619f64962b181a4d4b3de7735c16c4c9>

[15] [CITE@en[Define how data URLs affect workers]]
([[annevk]]著, [TIME[2016-09-15 19:34:58 +09:00]])
<https://github.com/whatwg/html/commit/c592f62985ab9aa0e26c111a9823de5101d58c96>

[48] [CITE@en[Define how data URLs affect workers]]
([[annevk]]著, [TIME[2016-09-15 19:34:58 +09:00]])
<https://github.com/whatwg/html/commit/c592f62985ab9aa0e26c111a9823de5101d58c96>

[78] [CITE@en[Remove appcache from shared workers]]
([[domenic]]著, [TIME[2017-02-23 09:46:48 +09:00]])
<https://github.com/whatwg/html/commit/3f1c84d682da30be5c323a43dfcc8995aae2f731>

[80] [CITE@en[140344 – Remove support for SharedWorkers]]
([TIME[2017-04-02 20:26:21 +09:00]])
<https://bugs.webkit.org/show_bug.cgi?id=140344>

[82] [CITE@en[Define agents and agent clusters]]
([[annevk]]著, [TIME[2017-04-26 18:46:01 +09:00]])
<https://github.com/whatwg/html/commit/4db86547f1184e7ceb4f1009cb3bef28101eda4f>

[23] [CITE@en[Add a name to dedicated workers, for debugging]]
([[domenic]]著, [TIME[2017-05-12 12:43:56 +09:00]])
<https://github.com/whatwg/html/commit/3bb652169741d5dd94d7f1b390f6491e38d640cb>

[85] [CITE@en[28504 – Should SharedWorker have only Exposed=(Window) ?]]
([TIME[2017-07-23 13:12:55 +09:00]])
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=28504>

[86] [CITE@en[State of SharedWorker? · Issue #2887 · whatwg/html]]
([TIME[2017-08-06 21:37:42 +09:00]])
<https://github.com/whatwg/html/issues/2887>

[87] [CITE@en[Remove shared workers? · Issue #315 · whatwg/html]]
([TIME[2017-08-06 21:39:53 +09:00]])
<https://github.com/whatwg/html/issues/315>

[88] [CITE@en[Make shared worker creation deterministic]]
([[jakearchibald]]著, [TIME[2017-09-21 22:27:09 +09:00]])
<https://github.com/whatwg/html/commit/9fda90f44c7c9223de6e3755a043f67ea7c92bf5>

[92] [CITE@en[Introduce a concept named threads to better describe concurrency · Issue #1843 · whatwg/html]]
([TIME[2017-09-29 17:47:56 +09:00]])
<https://github.com/whatwg/html/issues/1843>

[93] [CITE@en[Cleaning up algorithms and issues.]]
([[mikewest]]著, [TIME[2017-10-18 15:59:03 +09:00]])
<https://github.com/w3c/webappsec-secure-contexts/commit/2cc0b2f634b5062f82d540349233d30cab84e0c3>

[94] [CITE@en[Editorial: Clarifying secure context integration with Shared Workers]]
([[mikewest]]著, [TIME[2017-11-21 00:18:56 +09:00]])
<https://github.com/whatwg/html/commit/6f9d81839133072355c15efa4b934beaf85ea259>

[95] [CITE@en[Editorial: Clarifying secure context integration with Shared Workers]]
([[mikewest]]著, [TIME[2017-11-21 00:18:56 +09:00]])
<https://github.com/whatwg/html/commit/6f9d81839133072355c15efa4b934beaf85ea259>

[96] [CITE@en[Editorial: Clarifying secure context integration with Shared Workers. by mikewest · Pull Request #3243 · whatwg/html]]
([TIME[2017-11-25 00:30:04 +09:00]])
<https://github.com/whatwg/html/pull/3243>

[99] [CITE@en[154571 - Enable shared workers on Chrome on Android - chromium]]
([TIME[2022-10-02T13:13:20.000Z]])
<https://monorail-prod.appspot.com/p/chromium/issues/detail?id=154571>