10. web worker
10.1. 序論
10.1.1. 視野
この節は規範的ではない。This section is non-normative.
この仕様は、 他の UI スクリプトから独立に,バックグラウンドでスクリプトを走らすための API を定義する。 This specification defines an API for running scripts in the background independently of any user interface scripts.
それは、 次を許容する :クリックその他 利用者によるやりとりに応答するスクリプトから中断されることなく, 長期に渡りスクリプトを走らせる — すなわち、 ページを即応可能に保ちながら,長いタスクを実行する。 This allows for long-running scripts that are not interrupted by scripts that respond to clicks or other user interactions, and allows long tasks to be executed without yielding to keep the page responsive.
worker (これらのバックグラウンドスクリプトの ここでの名称)は、 比較的 重いので,多数の同時利用は意図されていない。 例えば、 画像内に幾千万ある画素ごとに異なる worker を立ち上げるのは不適切である。 下の例に worker の適切な利用を示す: Workers (as these background scripts are called herein) are relatively heavy-weight, and are not intended to be used in large numbers. For example, it would be inappropriate to launch one worker for each pixel of a four megapixel image. The examples below show some appropriate uses of workers.
一般に,worker は長時間居残り続けるので、 開始時の処理能コスト, およびインスタンスごとのメモリーコストは,高くつくものと予期されている。 Generally, workers are expected to be long-lived, have a high start-up performance cost, and a high per-instance memory cost.
10.1.2. 例
この節は規範的ではない。This section is non-normative.
worker には様々な利用がある。 以下の各下位節に,その利用を示す。 There are a variety of uses that workers can be put to. The following subsections show various examples of this use.
10.1.2.1. バックグラウンドで数値計算し続ける worker
この節は規範的ではない。This section is non-normative.
worker の最も単純な利用は、 UI を中断せずに計算量の大きな仕事を遂行するものである。 The simplest use of workers is for performing a computationally expensive task without interrupting the user interface.
この例では、 メイン文書が worker を生み出して, (素朴な)素数の計算を行い, 見つかった最新の素数を漸進的に表示し続ける。 In this example, the main document spawns a worker to (naïvely) compute prime numbers, and progressively displays the most recently found prime number.
メインページは次で与えられる: The main page is as follows:
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>worker 例: 単コアによる計算</title> </head> <body> <p>これまでに見つかった最大の素数は:<output id="result"></output></p> <script> var worker = new Worker('worker.js'); worker.onmessage = function (event) { document.getElementById('result').textContent = event.data; }; </script> </body> </html>
new Worker()
構築子の call により,worker が作成され、[
その worker を表現し, その worker と通信する, Worker
オブジェクト
]が返される。
オブジェクトの onmessage
イベントハンドラには、
worker からメッセージを受信するコードを入れられる。
The Worker() constructor call creates a worker and returns a Worker object representing that worker, which is used to communicate with the worker. That object's onmessage event handler allows the code to receive messages from the worker.
worker 自身は次で与えられる: The worker itself is as follows:
var n = 1;
search: while (true) {
n += 1;
for (var i = 2; i <= Math.sqrt(n); i += 1)
if (n % i == 0)
continue search;
/*
素数発見!
found a prime! */
postMessage(n);
}
【
いきなり postMessage()
が大域関数として登場している所からも推察されるように、
ちょうど通常時におけるスクリプトの Window
オブジェクトにあたる,
worker 用のスクリプト実行環境を与える大域オブジェクト
(§ 大域スコープ)が,
(worker を生み出した側の実行環境とは別に)
存在している、
と考えれば,この仕様の残りの部分も理解し易い。
】
このコードは、
単純な,最適化されていない,素数の探索ルーチンである。
postMessage()
が、
見つかった素数をメッセージにしてページに返信するメソッドである。
The bulk of this code is simply an unoptimized search for a prime number. The postMessage() method is used to send a message back to the page when a prime is found.
この例をオンラインで見る View this example online.
10.1.2.2. JavaScript モジュールを worker として利用する
この節は規範的ではない。This section is non-normative.
これまでの どの例も古典スクリプトを走らす worker を示してきたが、 worker は,モジュールスクリプトを利用してインスタンス化することもできる — これには、 通例的に,次に挙げる便益がある:
-
JavaScript
import
文を利用して他のモジュールを import する能 - 既定で strict モードになる
- worker の大域スコープを汚さない,トップレベルの宣言
import
文が可用になったことに伴い、
importScripts()
メソッドは,モジュール worker の内側では自動的に失敗することになる。
As the import statement is available, the importScripts() method will automatically fail inside module workers.
この例では、 メイン文書が[ メインスレッドの外で画像操作を行う worker ]を利用する。 それは、 別のモジュールから利用されるフィルタを import する。 In this example, the main document uses a worker to do off-main-thread image manipulation. It imports the filters used from another module.
メインページは: The main page is as follows:
<!DOCTYPE html> <meta charset="utf-8"> <title>worker 例: 画像を復号する</title> <p> <label> 復号する画像の URL を入れてください <input type="url" id="image-url" list="image-list"> <datalist id="image-list"> <option value="https://html.spec.whatwg.org/images/drawImage.png"> <option value="https://html.spec.whatwg.org/images/robots.jpeg"> <option value="https://html.spec.whatwg.org/images/arcTo2.png"> </datalist> </label> </p> <p> <label> 適用するフィルタを選んでください <select id="filter"> <option value="none">none</option> <option value="grayscale">grayscale</option> <option value="brighten">brighten by 20%</option> </select> </label> </p> <div id="output"></div> <script type="module"> const worker = new Worker("worker.js", { type: "module" }); worker.onmessage = receiveFromWorker; const url = document.querySelector("#image-url"); const filter = document.querySelector("#filter"); const output = document.querySelector("#output"); url.oninput = updateImage; filter.oninput = sendToWorker; let imageData, context; function updateImage() { const img = new Image(); img.src = url.value; img.onload = () => { const canvas = document.createElement("canvas"); canvas.width = img.width; canvas.height = img.height; context = canvas.getContext("2d"); context.drawImage(img, 0, 0); imageData = context.getImageData(0, 0, canvas.width, canvas.height); sendToWorker(); output.replaceChildren(canvas); }; } function sendToWorker() { worker.postMessage({ imageData, filter: filter.value }); } function receiveFromWorker(e) { context.putImageData(e.data, 0, 0); } </script>
worker ファイルは: The worker file is then:
import * as filters from "./filters.js"; self.onmessage = e => { const { imageData, filter } = e.data; filters[filter](imageData); self.postMessage(imageData, [imageData.data.buffer]); };
これは、
次のファイル filters.js
を import する:
Which imports the file filters.js:
export function none() {}
export function grayscale({ data: d }) {
for (let i = 0; i < d.length; i += 4) {
const [r, g, b] = [d[i], d[i + 1], d[i + 2]];
/*
RGB に対する CIE 輝度
— 人の目は 赤, 青 に鈍感なので,それらを減衰する
CIE luminance for the RGB
— The human eye is bad at seeing red and blue, so we de-emphasize them. */
d[i] = d[i + 1] = d[i + 2] = 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
};
export function brighten({ data: d }) {
for (let i = 0; i < d.length; ++i) {
d[i] *= 1.2;
}
};
この例をオンラインで見る View this example online.
10.1.2.5. 委任
この節は規範的ではない。This section is non-normative.
複コア CPU の普及に伴い、 計算量の大きな仕事を複数の worker に分業させて, より良い処理能が得られるようになっている。 この例では、 1 〜 10,000,000 に付番された計算量の大きな仕事を 10 等分して, 10 個の下位 worker に遂行させる。 With multicore CPUs becoming prevalent, one way to obtain better performance is to split computationally expensive tasks amongst multiple workers. In this example, a computationally expensive task that is to be performed for every number from 1 to 10,000,000 is farmed out to ten subworkers.
メインページは、 単に結果を報告する: The main page is as follows, it just reports the result:
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>worker 例: 複コアによる計算</title> </head> <body> <p>Result: <output id="result"></output></p> <script> var worker = new Worker('worker.js'); worker.onmessage = function (event) { document.getElementById('result').textContent = event.data; }; </script> </body> </html>
worker 自身は: The worker itself is as follows:
/* 設定 settings */ var num_workers = 10; var items_per_worker = 1000000; /* worker を開始する start the workers */ var result = 0; var pending_workers = num_workers; for (var i = 0; i < num_workers; i += 1) { var worker = new Worker('core.js'); worker.postMessage(i * items_per_worker); worker.postMessage((i+1) * items_per_worker); worker.onmessage = storeResult; } /* 結果を取り扱う handle the results */ function storeResult(event) { result += 1*event.data; pending_workers -= 1; if (pending_workers <= 0) postMessage(result); /* 完成! finished! */ }
それは、 一連の下位 worker を開始させるループと, 各 下位 worker から返される応答を待機するハンドラからなる。 It consists of a loop to start the subworkers, and then a handler that waits for all the subworkers to respond.
下位 worker の実装は: The subworkers are implemented as follows:
var start;
onmessage = getStart;
function getStart(event) {
start = 1*event.data;
onmessage = getEnd;
}
var end;
function getEnd(event) {
end = 1*event.data;
onmessage = null;
work();
}
function work() {
var result = 0;
for (var i = start; i < end; i += 1) {
/*
何か複階的な計算がここで遂行される
perform some complex calculation here */
result += 1;
}
postMessage(result);
close();
}
これらはそれぞれ、 2 度のイベントで 2 個の番号を受信して, それにより指定される付番範囲の計算を遂行して, 結果を親に報告する。 They receive two numbers in two events, perform the computation for the range of numbers thus specified, and then report the result back to the parent.
この例をオンラインで見る View this example online.
10.1.2.6. ライブラリを供するとき
この節は規範的ではない。This section is non-normative.
次の 3 種のタスクを供する,ある暗号化ライブラリが可用であるとする: Suppose that a cryptography library is made available that provides three tasks:
- ( 公開鍵, 私用鍵 ) ペアを生成する: Generate a public/private key pair
- 渡されたポートに, 2 個のメッセージ — 最初に公開鍵,次に私用鍵 — を送信する。 Takes a port, on which it will send two messages, first the public key and then the private key.
- 所与の ( 平文, 公開鍵 ) から,対応する暗号文を返す: Given a plaintext and a public key, return the corresponding ciphertext
- 渡されたポートに,任意個数のメッセージ — 最初に公開鍵,以降は平文 — を送信する。 各 平文は、 暗号化されてから暗号文と同じチャネルに送信される。 内容を暗号化し終えたなら,ポートを close できる。 Takes a port, to which any number of messages can be sent, the first giving the public key, and the remainder giving the plaintext, each of which is encrypted and then sent on that same channel as the ciphertext. The user can close the port when it is done encrypting content.
- 所与の ( 暗号文, 私用鍵 ) 対応する平文を返す Given a ciphertext and a private key, return the corresponding plaintext
- 渡されたポートに,任意個数のメッセージ — 最初に私用鍵,以降は暗号文 — を送信する。 各 暗号文は、 復号されてから平文と同じチャネルに送信される。 内容を復号し終えたなら,ポートを close できる。 Takes a port, to which any number of messages can be sent, the first giving the private key, and the remainder giving the ciphertext, each of which is decrypted and then sent on that same channel as the plaintext. The user can close the port when it is done decrypting content.
ライブラリ自身は: The library itself is as follows:
function handleMessage(e) { if (e.data == "genkeys") genkeys(e.ports[0]); else if (e.data == "encrypt") encrypt(e.ports[0]); else if (e.data == "decrypt") decrypt(e.ports[0]); } function genkeys(p) { var keys = _generateKeyPair(); p.postMessage(keys[0]); p.postMessage(keys[1]); } function encrypt(p) { var key, state = 0; p.onmessage = function (e) { if (state == 0) { key = e.data; state = 1; } else { p.postMessage(_encrypt(key, e.data)); } }; } function decrypt(p) { var key, state = 0; p.onmessage = function (e) { if (state == 0) { key = e.data; state = 1; } else { p.postMessage(_decrypt(key, e.data)); } }; } /* 共用 worker , 専用 worker どちらとして利用されていても,サポートする support being used as a shared worker as well as a dedicated worker */ if ('onmessage' in this) /* 専用 worker */ onmessage = handleMessage; else /* 共用 worker */ onconnect = function (e) { e.port.onmessage = handleMessage; } /* “暗号” 関数: the "crypto" functions: */ function _generateKeyPair() { return [Math.random(), Math.random()]; } function _encrypt(k, s) { return 'encrypted-' + k + ' ' + s; } function _decrypt(k, s) { return s.substr(s.indexOf(' ')+1); }
ここでの暗号関数は、 単なるスタブであり,本当の暗号化は行わないことに注意。 Note that the crypto functions here are just stubs and don't do real cryptography.
このライブラリは、 次のようにも利用できる: This library could be used as follows:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Worker example: Crypto library</title>
<script>
const cryptoLib = new Worker('libcrypto-v1.js'); /*
あるいは 'libcrypto-v2.js' も利用できる
or could use 'libcrypto-v2.js' */
function startConversation(source, message) {
const messageChannel = new MessageChannel();
source.postMessage(message, [messageChannel.port2]);
return messageChannel.port1;
}
function getKeys() {
let state = 0;
startConversation(cryptoLib, "genkeys").onmessage = function (e) {
if (state === 0)
document.getElementById('public').value = e.data;
else if (state === 1)
document.getElementById('private').value = e.data;
state += 1;
};
}
function enc() {
const port = startConversation(cryptoLib, "encrypt");
port.postMessage(document.getElementById('public').value);
port.postMessage(document.getElementById('input').value);
port.onmessage = function (e) {
document.getElementById('input').value = e.data;
port.close();
};
}
function dec() {
const port = startConversation(cryptoLib, "decrypt");
port.postMessage(document.getElementById('private').value);
port.postMessage(document.getElementById('input').value);
port.onmessage = function (e) {
document.getElementById('input').value = e.data;
port.close();
};
}
</script>
<style>
textarea { display: block; }
</style>
</head>
<body onload="getKeys()">
<fieldset>
<legend>Keys</legend>
<p><label>Public Key: <textarea id="public"></textarea></label></p>
<p><label>Private Key: <textarea id="private"></textarea></label></p>
</fieldset>
<p><label>Input: <textarea id="input"></textarea></label></p>
<p><button onclick="enc()">Encrypt</button> <button onclick="dec()">Decrypt</button></p>
</body>
</html>
この API の後のバージョンでは、 すべての暗号作業を下位 worker に負荷を委譲するよう求まれるかもしれない。 これは、 次のように行うこともできる: A later version of the API, though, might want to offload all the crypto work onto subworkers. This could be done as follows:
function handleMessage(e) { if (e.data == "genkeys") genkeys(e.ports[0]); else if (e.data == "encrypt") encrypt(e.ports[0]); else if (e.data == "decrypt") decrypt(e.ports[0]); } function genkeys(p) { var generator = new Worker('libcrypto-v2-generator.js'); generator.postMessage('', [p]); } function encrypt(p) { p.onmessage = function (e) { var key = e.data; var encryptor = new Worker('libcrypto-v2-encryptor.js'); encryptor.postMessage(key, [p]); }; } function encrypt(p) { p.onmessage = function (e) { var key = e.data; var decryptor = new Worker('libcrypto-v2-decryptor.js'); decryptor.postMessage(key, [p]); }; } /* 共用 worker , 専用 worker どちらとして利用されていても,サポートする support being used as a shared worker as well as a dedicated worker */ if ('onmessage' in this) /* 専用 worker dedicated worker */ onmessage = handleMessage; else /* 共用 worker shared worker */ onconnect = function (e) { e.ports[0].onmessage = handleMessage };
小分けにされた下位 worker は: The little subworkers would then be as follows.
鍵ペアの生成用: For generating key pairs:
onmessage = function (e) { var k = _generateKeyPair(); e.ports[0].postMessage(k[0]); e.ports[0].postMessage(k[1]); close(); } function _generateKeyPair() { return [Math.random(), Math.random()]; }
暗号化用: For encrypting:
onmessage = function (e) { var key = e.data; e.ports[0].onmessage = function (e) { var s = e.data; postMessage(_encrypt(key, s)); } } function _encrypt(k, s) { return 'encrypted-' + k + ' ' + s; }
復号用: For decrypting:
onmessage = function (e) { var key = e.data; e.ports[0].onmessage = function (e) { var s = e.data; postMessage(_decrypt(key, s)); } } function _decrypt(k, s) { return s.substr(s.indexOf(' ')+1); }
API は変更されてないので、 API の利用者は,これが起きていることを知る必要すらない。 ライブラリは、 メッセージチャネルを利用してデータを受容しているが, 自身の API を変更することなく下位 worker に委任できる。 Notice how the users of the API don't have to even know that this is happening — the API hasn't changed; the library can delegate to subworkers without changing its API, even though it is accepting data using message channels.
この例をオンラインで見る 。 View this example online.
10.1.3. チュートリアル
10.1.3.1. 専用 worker の作成法
この節は規範的ではない。This section is non-normative.
worker を作成するためには JavaScript ファイルの URL を要する。
そのファイルの URL のみを引数として new Worker()
構築子を呼び出せば、
worker が作成されて返される:
Creating a worker requires a URL to a JavaScript file. The Worker() constructor is invoked with the URL to that file as its only argument; a worker is then created and returned:
var worker = new Worker('helper.js');
古典スクリプトでなくモジュールスクリプトとして解釈させたいときは、 記し方を少し違える必要がある: If you want your worker script to be interpreted as a module script instead of the default classic script, you need to use a slightly different signature:
var worker = new Worker('helper.mjs', { type: "module" });
10.1.3.2. 専用 worker との通信法
この節は規範的ではない。This section is non-normative.
専用 worker は暗黙的に MessagePort
オブジェクトを利用する。
したがって、[
有構造データの送信法, バイナリデータの転送法, 他のポートへの転送法
]など,
MessagePort
と同じ特能をサポートする。
Dedicated workers use MessagePort objects behind the scenes, and thus support all the same features, such as sending structured data, transferring binary data, and transferring other ports.
専用 worker からメッセージを受信するためには、
Worker
オブジェクトの onmessage
イベントハンドラ IDL 属性を利用する:
To receive messages from a dedicated worker, use the onmessage event handler IDL attribute on the Worker object:
worker.onmessage = function (event) { ... };
addEventListener()
メソッドも利用できる。
You can also use the addEventListener() method.
注記:
専用 worker に利用される暗黙的な MessagePort
には、
その作成時にポートメッセージキューが暗黙的に備わり,可能化される。
したがって、
Worker
インタフェースには,
MessagePort
インタフェースの start()
メソッドに等価なものはない。
The implicit MessagePort used by dedicated workers has its port message queue implicitly enabled when it is created, so there is no equivalent to the MessagePort interface's start() method on the Worker interface.
データを worker へ送信するためには、
postMessage()
メソッドを利用する。
有構造データもこの通信チャネルを通して送信できる。
(何個かの) ArrayBuffer
オブジェクトを効率的に送信する(クローンせずに転送する)ためには、
それらを配列にして 2 個目の引数に渡す。
To send data to a worker, use the postMessage() method. Structured data can be sent over this communication channel. To send ArrayBuffer objects efficiently (by transferring them rather than cloning them), list them in an array in the second argument.
worker.postMessage({
operation: 'find-edges',
input: buffer, /*
ArrayBuffer
オブジェクト
an ArrayBuffer object */
threshold: 0.6,
}, [buffer]);
worker の内側でメッセージを受信するときは、
onmessage
イベントハンドラ IDL 属性を利用する。
To receive a message inside the worker, the onmessage event handler IDL attribute is used.
onmessage = function (event) { ... };
ここでもまた addEventListener()
メソッドを利用できる。
You can again also use the addEventListener() method.
いずれの場合も,データはイベントオブジェクトの data
属性に供される。
In either case, the data is provided in the event object's data attribute.
メッセージの返信時にも, postMessage()
を利用する。
有構造データも同じようにサポートされる。
To send messages back, you again use postMessage(). It supports the structured data in the same manner.
postMessage(event.data.input, [event.data.input]); /*
バッファを返送
transfer the buffer back */
【この訳に特有な表記規約】
この訳において(主にアルゴリズム内で)利用される各種記号( ε, ← , IF, THROW, 等々)の意味や定義の詳細は、共通な慣用表現, アルゴリズムに共通して利用される表記を参照されたし。
10.2. 基盤
この仕様は、 2 種類の worker — 専用 worker , 共用 worker — を定義する。 専用 worker は,作成時にその作成元へリンクされるが、 メッセージポートを利用することで, 他の閲覧文脈や worker へも通信できる。 一方で、 共用 worker には,名前があり、 その作成後に それへの参照を得ておけば, 同じ生成元上で走っているどのスクリプトとも通信できる。 Service Workers は、 また別の種類の worker を定義する。 [SW] This standard defines two kinds of workers: dedicated workers, and shared workers. Dedicated workers, once created, are linked to their creator, but message ports can be used to communicate from a dedicated worker to multiple other browsing contexts or workers. Shared workers, on the other hand, are named, and once created any script running in the same origin can obtain a reference to that worker and communicate with it. Service Workers defines a third kind. [SW]
10.2.1. 大域スコープ
大域スコープが worker の “内側” である。 The global scope is the "inside" of a worker.
【
すなわち、
同じ worker が, “二つの顔”
— その内側で走っているスクリプトに公開する WorkerGlobalScope
オブジェクト(大域スコープ),
それを外側から利用するスクリプトに公開する Worker
オブジェクト —
を持つ
(共用 worker の場合、
後者は,当の worker を共有している大域環境の個数だけ複数あり得る)。
】
10.2.1.1. WorkerGlobalScope
共通インタフェース
[Exposed=Worker] interfaceWorkerGlobalScope
:EventTarget
{ readonly attributeWorkerGlobalScope
self
; readonly attributeWorkerLocation
location
; readonly attributeWorkerNavigator
navigator
; undefinedimportScripts
((TrustedScriptURL
or USVString)... urls); attributeOnErrorEventHandler
onerror
; attributeEventHandler
onlanguagechange
; attributeEventHandler
onoffline
; attributeEventHandler
ononline
; attributeEventHandler
onrejectionhandled
; attributeEventHandler
onunhandledrejection
; };
WorkerGlobalScope
は、
各種 worker 大域スコープオブジェクトの基底クラスとしてサーブする
— そのようなオブジェクトの型には、
特に次に挙げるものが含まれる
:DedicatedWorkerGlobalScope
,
SharedWorkerGlobalScope
,
ServiceWorkerGlobalScope
WorkerGlobalScope serves as the base class for specific types of worker global scope objects, including DedicatedWorkerGlobalScope, SharedWorkerGlobalScope, and ServiceWorkerGlobalScope.
各 WorkerGlobalScope
オブジェクトには、
次に挙げるものが結び付けられる:
↓
- 所有者集合 A WorkerGlobalScope object has an associated owner set\
-
[
Document
/WorkerGlobalScope
]オブジェクトたちが成す集合 — 初期時は空とする。 この worker を[ 作成する/得る ]ときに拡充される。 (a set of Document and WorkerGlobalScope objects). It is initially empty and populated when the worker is created or obtained. - 注記:
単独の所有者ではなく集合にされているのは、
SharedWorkerGlobalScope
オブジェクトに適応するためである。 It is a set, instead of a single owner, to accommodate SharedWorkerGlobalScope objects. - 種別 A WorkerGlobalScope object has an associated type\
-
作成時に,次のいずれかに設定される
:"
classic
" / "module
" ("classic" or "module"). It is set during creation. - URL A WorkerGlobalScope object has an associated url\
- null または URL — 初期時は null とする。 (null or a URL). It is initially null.
- 【 少なくとも,API からアクセスされる時点までには、 null 以外に設定されることになる。 】
- 名前 A WorkerGlobalScope object has an associated name\
- 文字列 — 作成時に設定される。 (a string). It is set during creation.
-
注記: 名前の意味論は、
WorkerGlobalScope
の下位クラスごとに異なり得る: The name can have different semantics for each subclass of WorkerGlobalScope.\-
DedicatedWorkerGlobalScope
用には、 単純に開発者が給する名前であり,ほぼデバッグ目的に限り有用になる。 For DedicatedWorkerGlobalScope instances, it is simply a developer-supplied name, useful mostly for debugging purposes.\ -
SharedWorkerGlobalScope
用には、 共通な共用 worker への参照をnew SharedWorker()
構築子を介して得ることを許容する。 For SharedWorkerGlobalScope instances, it allows obtaining a reference to a common shared worker via the SharedWorker() constructor.\ -
ServiceWorkerGlobalScope
用には、 意味を成さない (そのため、 JavaScript API を通して公開されることもない)。 For ServiceWorkerGlobalScope objects, it doesn't make sense (and as such isn't exposed through the JavaScript API at all).
-
- 施策コンテナ A WorkerGlobalScope object has an associated policy container\
- 施策コンテナ — 初期時は新たな施策コンテナとする。 (a policy container). It is initially a new policy container.
- 埋め込み元施策 A WorkerGlobalScope object has an associated embedder policy\
- 埋め込み元施策 (an embedder policy).
- モジュールマップ A WorkerGlobalScope object has an associated module map.\
- モジュールマップ — 初期時は空とする。 It is a module map, initially empty.
- 非同一生成元能力は隔離されるか A WorkerGlobalScope object has an associated cross-origin isolated capability\
- 真偽値 — 初期時は false とする。 boolean. It is initially false.
- workerGlobal.
self
- workerGlobal 自身を返す。 Returns workerGlobal.
- workerGlobal.
location
-
workerGlobal の
WorkerLocation
オブジェクトを返す。 Returns workerGlobal's WorkerLocation object. - workerGlobal.
navigator
-
workerGlobal の
WorkerNavigator
オブジェクトを返す。 Returns workerGlobal's WorkerNavigator object. - workerGlobal.
importScripts(...urls)
- urls を成す各 URL を — 渡された順に一つずつ — fetch して, 実行して, 結果を返す (あるいは、 どこかで不具合があれば例外を投出する)。 Fetches each URL in urls, executes them one-by-one in the order they are passed, and then returns (or throws if something went amiss).
self
取得子手続きは
:RETURN これ°
The self attribute must return the WorkerGlobalScope object itself.
location
取得子手続きは
- Assert: これ°の worker 所在オブジェクト ≠ ε
- RETURN これ°の worker 所在オブジェクト
注記:
WorkerLocation
オブジェクトが作成されるのは,
WorkerGlobalScope
オブジェクトより後になるが、
それはスクリプトからは観測し得ないので,問題にはならない。
WorkerGlobalScope
インタフェースを実装するオブジェクトにおいては、
次に挙げるイベントハンドラ(および,それらに対応するイベントハンドライベント型)をイベントハンドラ IDL 属性としてサポートするものとする:
The following are the event handlers (and their corresponding event handler event types) that must be supported, as event handler IDL attributes, by objects implementing the WorkerGlobalScope interface:
イベントハンドラ | イベントハンドライベント型 |
---|---|
onerror
| error
|
onlanguagechange
| languagechange
|
onoffline
| offline
|
ononline
| online
|
onrejectionhandled
| rejectionhandled
|
onunhandledrejection
| unhandledrejection
|
10.2.1.2. 専用 worker と DedicatedWorkerGlobalScope
インタフェース
[Global=(Worker,DedicatedWorker),Exposed=DedicatedWorker] interfaceDedicatedWorkerGlobalScope
:WorkerGlobalScope
{ [Replaceable] readonly attribute DOMStringname
; undefinedpostMessage
(any message, sequence<object> transfer); undefinedpostMessage
(any message, optionalStructuredSerializeOptions
options = {}); undefinedclose
();DedicatedWorkerGlobalScope
includesMessageEventTarget
; };
各 DedicatedWorkerGlobalScope
オブジェクトには、
内側ポート
が結び付けられる
— それは:
DedicatedWorkerGlobalScope objects have an associated inside port\
-
MessagePort
オブジェクトである。 (a MessagePort).\ - 当の worker 作成時に設定しておかれるチャネルの一部を成すが,公開されない。 This port is part of a channel that is set up when the worker is created, but it is not exposed.\
-
当の
DedicatedWorkerGlobalScope
オブジェクトより先にガーベジ収集されてはならない。 This object must never be garbage collected before the DedicatedWorkerGlobalScope object.
- dedicatedWorkerGlobal.
name
-
dedicatedWorkerGlobal の名前
— すなわち,
new Worker()
構築子に与えた値 — を返す。 主に、 デバッグ時に有用になる。 Returns dedicatedWorkerGlobal's name, i.e. the value given to the Worker constructor. Primarily useful for debugging. - dedicatedWorkerGlobal.
postMessage(message, transfer)
- dedicatedWorkerGlobal.
postMessage(message [, options ])
-
メッセージをクローンして,それを
dedicatedWorkerGlobal に結び付けられた
Worker
オブジェクトへ伝送する。 [ transfer / [ options のtransfer
メンバ ]]に、 クローンせずに転送するオブジェクトのリストを渡せる。 Clones message and transmits it to the Worker object associated with dedicatedWorkerGlobal. transfer can be passed as a list of objects that are to be transferred rather than cloned. - dedicatedWorkerGlobal.
close()
- dedicatedWorkerGlobal を中止する。 Aborts dedicatedWorkerGlobal.
name
取得子手続きは
:RETURN これ°の名前
The name getter steps are to return this's name.\
返される値は、
new Worker
構築子を利用して worker に与えた名前を表現する
— これは主として、
デバッグ目的に利用される。
Its value represents the name given to the worker using the Worker constructor, used primarily for debugging purposes.
postMessage(message, transfer)
メソッド手続きは
:RETURN これ°の内側ポート上の
postMessage
メソッド手続き( message, transfer )
postMessage(message, options)
メソッド手続きは
:RETURN これ°の内側ポート上の
postMessage
メソッド手続き( message, options )
worker を close する アルゴリズムは、 所与の ( worker 大域スコープ ) に対し: To close a worker, given a workerGlobal, run these steps:
- worker 大域スコープ に関連する agent のイベントループのタスクキューに追加されたタスクは、 すべて破棄する Discard any tasks that have been added to workerGlobal's relevant agent's event loop's task queues.
- worker 大域スコープ の close 中か ← true (これにより、 タスクがそれ以上キューされることはなくなる。) Set workerGlobal's closing flag to true. (This prevents any further tasks from being queued.)
close()
メソッド手続きは
:worker を close する( これ° )
The close() method steps are to close a worker given this.10.2.2. イベントループ
worker イベントループのタスクキュー内にあり得るタスクは、[ イベント, callback , ネットワーク処理活動 ]に限られる。 これらの worker イベントループは、 worker を走らすアルゴリズムにより作成される。 A worker event loop's task queues only have events, callbacks, and networking activity as tasks. These worker event loops are created by the run a worker algorithm.
各 WorkerGlobalScope
オブジェクトは、
真偽値をとる
close 中か
を有する:
Each WorkerGlobalScope object has a closing flag,\
- 初期時には false になるものとする。 下の処理モデル節のアルゴリズムにより, true にされ得る。 which must be initially false, but which can get set to true by the algorithms in the processing model section below.
- true にされたときは、 イベントループのタスクキューに追加される それ以降のタスクは,破棄するものとする (キュー内にすでにあるタスクは、 特に指定されない限り,影響されない)。 実質的には、 true になったら,タイマーは発火を止め, 処理待ちにあるすべてのバックグラウンド演算の通知は取り除かれる, 等々が行われることになる。 Once the WorkerGlobalScope's closing flag is set to true, the event loop's task queues must discard any further tasks that would be added to them (tasks already on the queue are unaffected except where otherwise specified). Effectively, once the closing flag is true, timers stop firing, notifications for all pending background operations are dropped, etc.
10.2.3. worker の存続期間
worker は、
メッセージチャネルとそれらの
MessagePort
オブジェクトを通して,他の worker や window と通信する。
Workers communicate with other workers and with Windows through message channels and their MessagePort objects.
各 WorkerGlobalScope
オブジェクト G は、
ポートリスト
を有する
— それは:
Each WorkerGlobalScope object worker global scope has a list of the worker's ports,\
-
次を満たす
MessagePort
オブジェクト P すべてからなる :[ P は別のMessagePort
オブジェクト Q と連絡されている ]∧[ G は P, Q のうち P のみを所有している† ] which consists of all the MessagePort objects that are entangled with another port and that have one (but only one) port owned by worker global scope.\【† おそらく,[ P に関連する大域オブジェクト = G ≠ Q に関連する大域オブジェクト ]を意味する。 】
-
専用 worker の事例では
【すなわち, G は
DedicatedWorkerGlobalScope
オブジェクトである】、 暗黙的なポート【 G の内側ポート】も含む。 【前項の条件を満たす限り。】 This list includes the implicit MessagePort in the case of dedicated workers.
所与の環境設定群オブジェクト O の下で worker を[ 作成する/得る ]ときに 関連する所有者として追加するもの は、 O により指定される大域オブジェクト G に応じて,次のいずれかになる: Given an environment settings object o when creating or obtaining a worker, the relevant owner to add depends on the type of global object specified by o.\
-
WorkerGlobalScope
である(すなわち,入れ子な専用 worker を作成している)場合 :G If o's global object is a WorkerGlobalScope object (i.e., if we are creating a nested dedicated worker), then the relevant owner is that global object.\ -
Window
である場合 :G に結び付けられた文書 Otherwise, o's global object is a Window object, and the relevant owner is that Window's associated Document.
worker は、
その WorkerGlobalScope
を G とするとき:
↓
-
∨↓ を満たしている間は、 許可可能 ( permissible )とされる: A worker is said to be a permissible worker if\
- G の所有者集合は空でない its WorkerGlobalScope's owner set is not empty or:
-
∧↓ ↓
- G の所有者集合は空であり続けているが、 実装定義な短い制限時間を過ぎていない its owner set has been empty for no more than a short implementation-defined timeout value,
-
G は
SharedWorkerGlobalScope
オブジェクトである(すなわち,worker は共用 worker である) its WorkerGlobalScope object is a SharedWorkerGlobalScope object (i.e., the worker is a shared worker), and - UA の あるナビ可能にて作動中な文書は、 まだ完全に読み込まれていない the user agent has a navigable whose active document is not completely loaded.
注記: 上の定義の 2 番目の項は、 読み込まれている短い間にページが共用 worker に再び繋がろうとしている下でも, 共用 worker が生き残れるようにする。 これを利用すれば,UA は、 利用者が あるサイトの中でページからページへナビゲートするときに, そのサイトが利用している共用 worker を開始し直すコストを避けれるようになる。 The second part of this definition allows a shared worker to survive for a short time while a page is loading, in case that page is going to contact the shared worker again. This can be used by user agents as a way to avoid the cost of restarting a shared worker used by a site when the user is navigating from page to page within that site.
- 次を満たしている間は、 作動中なため必要 ( active needed )とされる :G の所有者集合内に次を満たす[ 文書/worker ]が在る :[ 全部的に作動中/作動中なため必要 ]である A worker is said to be an active needed worker if any of its owners are either Document objects that are fully active or active needed workers.
-
∧↓ を満たしている間は、 保護され ている( protected )とされる: A worker is said to be a protected worker if\
- 作動中なため必要である it is an active needed worker\
-
∨↓: and either\
- 【 G 内に】待機中なタイマーがある it has outstanding timers,\
- 【 G 内に】データベーストランザクションがある【 [INDEXEDDB] 】 database transactions, or\
- 【 G 内に】ネットワーク接続がある network connections, or\
- G のポートリストは空でない its list of the worker's ports is not empty, or\
-
G は
SharedWorkerGlobalScope
オブジェクトである(すなわち,worker は共用 worker である) its WorkerGlobalScope is actually a SharedWorkerGlobalScope object (i.e., the worker is a shared worker).
-
∧↓ を満たしている間は、 休止可能 ( suspendable )とされる A worker is said to be a suspendable worker if\
10.2.4. 処理モデル
あるスクリプト用の
worker を走らす
アルゴリズムは、
所与の
:worker ( Worker
/ SharedWorker
オブジェクト ),
URL ( URL ),
外側設定群 ( 環境設定群オブジェクト ),
外側ポート ( MessagePort
オブジェクト ),
options ( WorkerOptions
辞書 )
に対し:
When a user agent is to run a worker for a script with Worker or SharedWorker object worker, URL url, environment settings object outside settings, MessagePort outside port, and a WorkerOptions dictionary options, it must run the following steps.
-
共用か :← IS[
worker は
SharedWorker
オブジェクトである ] Let is shared be true if worker is a SharedWorker object, and false otherwise. - 所有者 :← 外側設定群 から与えられる,関連する所有者として追加するもの Let owner be the relevant owner to add given outside settings.
- 安全でない worker 作成時刻 :← 安全でない共有される現在の時刻 Let unsafeWorkerCreationTime be the unsafe shared current time.
-
agent :← 専用/共用 worker agent を得る( 外側設定群, 共用か ) Let agent be the result of obtaining a dedicated/shared worker agent given outside settings and is shared.\
この手続きの以降は, agent 内で走らすとする Run the rest of these steps in that agent.
-
realm 実行文脈 :← 次のようにカスタム化する下で,
agent 内で新たな realm を作成する
:大域オブジェクト用には, 共用か に応じて次を作成する
:true ならば 新たな
SharedWorkerGlobalScope
オブジェクト / false ならば 新たなDedicatedWorkerGlobalScope
オブジェクト Let realm execution context be the result of creating a new realm given agent and the following customizations: • For the global object, if is shared is true, create a new SharedWorkerGlobalScope object. Otherwise, create a new DedicatedWorkerGlobalScope object. -
worker 大域スコープ :← realm 実行文脈 の大域オブジェクト成分 Let worker global scope be the global object of realm execution context's Realm component.
注記: これは、 前段で作成した[
SharedWorkerGlobalScope
/DedicatedWorkerGlobalScope
]オブジェクトになる。 This is the DedicatedWorkerGlobalScope or SharedWorkerGlobalScope object created in the previous step. - 内側設定群 :← worker の環境設定群オブジェクトを設定しておく( realm 実行環境, 外側設定群, 安全でない worker 作成時刻 ) Set up a worker environment settings object with realm execution context, outside settings, and unsafeWorkerCreationTime, and let inside settings be the result.
-
worker 大域スコープ の名前 ← options[ "
name
" ] Set worker global scope's name to the value of options's name member. - worker 大域スコープ の所有者集合に 所有者 を付加する Append owner to worker global scope's owner set.
-
IF[
共用か = true
]
:worker 大域スコープ の
:構築子生成元 ← 外側設定群 の生成元,
構築子 URL ← URL ,
種別 ← options[ "
type
" ], 資格情報 ← options[ "credentials
" ] If is shared is true, then: • Set worker global scope's constructor origin to outside settings's origin. • Set worker global scope's constructor url to url. • Set worker global scope's type to the value of options's type member. • Set worker global scope's credentials to the value of options's credentials member. -
行先 :← 共用か に応じて
:true ならば "
sharedworker
" / false ならば "worker
" Let destination be "sharedworker" if is shared is true, and "worker" otherwise. -
次に従ってスクリプトを得る — options[ "
type
" ] に応じて: Obtain script by switching on the value of options's type member:-
"
classic
" :worker 用古典スクリプトを fetch する( ↓ ) :URL , 外側設定群, 行先, 内側設定群, 完了時の手続き†, fetch を遂行する† "classic" • Fetch a classic worker script given url, outside settings, destination, inside settings, and with onComplete and performFetch as defined below. -
"
module
" :モジュール worker スクリプトグラフを fetch する( ↓ ) :URL , 外側設定群, 行先, options[ "credentials
" ], 内側設定群, 完了時の手続き†, fetch を遂行する† "module" • Fetch a module worker script graph given url, outside settings, destination, the value of the credentials member of options, inside settings, and with onComplete and performFetch as defined below.
† 上で利用される[ 完了時の手続き, fetch を遂行する ]は、 以下に定義される: ↑
fetch を遂行する は,fetch フックを遂行するアルゴリズムであり、 所与の ( 要請, トップレベルか, カスタム fetch 応答の処理 ) に対し,次を遂行する: In both cases, let performFetch be the following perform the fetch hook given request, isTopLevel and processCustomFetchResponse:
-
IF[ トップレベルか = false ]:
- 要請 を fetch する — 次を与える下で :応答の本体を消費する処理 ← カスタム fetch 応答の処理
- RETURN
- 要請 の予約済みクライアント ← 内側設定群 Set request's reserved client to inside settings.
-
要請 を fetch する — 次を与える下で: Fetch request with\
-
応答の本体を消費する処理 ← 所与の ( 応答 応答, [ null / 失敗 / バイト列 ] 本体バイト列 ) に対し,次を走らす手続き: processResponseConsumeBody set to the following steps given response response and null, failure, or a byte sequence bodyBytes:
- worker 大域スコープ の URL ← 応答 の URL Set worker global scope's url to response's url.
- worker 大域スコープの施策コンテナを初期化する( worker 大域スコープ, 応答, 内側設定群 ) Initialize worker global scope's policy container given worker global scope, response, and inside settings.
- IF[ 大域オブジェクト用に CSP 初期化を走らす( worker 大域スコープ ) = 阻止される [CSP] ] :応答 ← ネットワークエラー If the Run CSP initialization for a global object algorithm returns "Blocked" when executed upon worker global scope, set response to a network error. [CSP]
-
IF[ worker 大域スコープ の埋め込み元施策の値は非同一生成元隔離と互換である ]∧[ 共用か = true ] :agent の agent クラスタの非同一生成元隔離モード ← [ "
logical
" / "concrete
" ] — どちらが選ばれるかは、 実装定義とする If worker global scope's embedder policy's value is compatible with cross-origin isolation and is shared is true, then set agent's agent cluster's cross-origin isolation mode to "logical" or "concrete". The one chosen is implementation-defined.これは,本当は当の agent クラスタが作成されるとき設定されるべきだが、 この節を設計し直すことが要求される。 This really ought to be set when the agent cluster is created, which requires a redesign of this section.
- IF[ 大域オブジェクトの埋め込み元施策を検査する( worker 大域スコープ, 外側設定群, 応答 ) = false ] :応答 ← ネットワークエラー If the result of checking a global object's embedder policy with worker global scope, outside settings, and response is false, then set response to a network error.
-
IF[
agent の agent クラスタの非同一生成元隔離モード = "
concrete
" ] :worker 大域スコープ の非同一生成元能力は隔離されるか ← true Set worker global scope's cross-origin isolated capability to true if agent's agent cluster's cross-origin isolation mode is "concrete". - IF[ 共用か = false ]∧[ 所有者 の非同一生成元能力は隔離されるか? = false ] :worker 大域スコープ の非同一生成元能力は隔離されるか ← false If is shared is false and owner's cross-origin isolated capability is false, then set worker global scope's cross-origin isolated capability to false.
-
IF[ 共用か = false ]∧[ 応答 の URL のスキーム = "
data
" ] :worker 大域スコープ の非同一生成元能力は隔離されるか ← false If is shared is false and response's url's scheme is "data", then set worker global scope's cross-origin isolated capability to false.注記: これは、 今の所は,保守的な既定である。 worker が一般に — および[
data:
URL 【から構築された】worker (その所有者とは非同一生成元になる) ]が特に — 許可施策の文脈において,どう扱われることになるか解明されるまでの間の。 詳細は、 w3c/webappsec-permissions-policy 課題 #207 を見よ。 This is a conservative default for now, while we figure out how workers in general, and data: URL workers in particular (which are cross-origin from their owner), will be treated in the context of permissions policies. See w3c/webappsec-permissions-policy issue #207 for more details. - カスタム fetch 応答の処理( 応答, 本体バイト列 ) Run processCustomFetchResponse with response and bodyBytes.
-
完了時の手続き は、 所与の ( スクリプト ) に対し: In both cases, let onComplete given script be the following steps:
-
IF[ スクリプト = null ]∨[ スクリプト の再投出用エラー ≠ null ]: If script is null or if script's error to rethrow is non-null, then:
-
大域タスクをキューする( DOM 操作タスクソース, worker に関連する大域オブジェクト, 次の手続き )
Queue a global task on the DOM manipulation task source given worker's relevant global object to fire an event named error at worker. - 内側設定群 用の環境を破棄する手続き() Run the environment discarding steps for inside settings.
- RETURN Abort these steps.
-
- worker を worker 大域スコープ に結び付ける Associate worker with worker global scope.
-
内側ポート :← 新たなオブジェクト(
MessagePort
, 内側設定群 の realm ) Let inside port be a new MessagePort object in inside settings's realm. -
IF[ 共用か = false ]: If is shared is false, then:
- 内側ポート のメッセージイベントターゲット ← worker 大域スコープ Set inside port's message event target to worker global scope.
- worker 大域スコープ の内側ポート ← 内側ポート Set worker global scope's inside port to inside port.
- ポートを連絡する( 外側ポート, 内側ポート ) Entangle outside port and inside port.
-
worker 大域スコープ の worker 所在オブジェクト ← 新たな
WorkerLocation
オブジェクト Create a new WorkerLocation object and associate it with worker global scope. -
worker の監視を開始する: ↓
- 孤立 worker は close 中 :worker が保護されなくなり次第、 許可可能であり続ける間まで, worker 大域スコープ の close 中かは true にする Closing orphan workers: Start monitoring the worker such that no sooner than it stops being a protected worker, and no later than it stops being a permissible worker, worker global scope's closing flag is set to true.
- worker の休止 :次が満たされるようになったときは、 満たされなくなるまで,worker 内のスクリプトの実行を休止する :[ worker 大域スコープ の close 中か = false ]∧[ worker は休止可能である ] Suspending workers: Start monitoring the worker, such that whenever worker global scope's closing flag is false and the worker is a suspendable worker, the user agent suspends execution of script in that worker until such time as either the closing flag switches to true or the worker stops being a suspendable worker.
- 内側設定群 の実行は準備済みか ← true Set inside settings's execution ready flag.
-
スクリプト に応じて:
- 古典スクリプト :古典スクリプトを走らす( スクリプト )
- モジュールスクリプト :モジュールスクリプトを走らす( スクリプト )
注記: 通例の[ 値を返す, 例外による失敗 ]に加えて,これは、 worker が終了されたときも尚早に中止され得る。 In addition to the usual possibilities of returning a value or failing due to an exception, this could be prematurely aborted by the terminate a worker algorithm defined below.
- 外側ポート のポートメッセージキューを可能化する Enable outside port's port message queue.
- IF[ 共用か = false ] :worker の暗黙的なポート【 内側ポート 】のポートメッセージキューを可能化する If is shared is false, enable the port message queue of the worker's implicit port.
-
ELSE :大域タスクをキューする( DOM 操作タスクソース, worker 大域スコープ, 次の手続き )
手続きは :イベントを発火する( worker 大域スコープ,If is shared is true, then queue a global task on DOM manipulation task source given worker global scope to fire an event named connect at worker global scope, using MessageEvent, with the data attribute initialized to the empty string, the ports attribute initialized to a new frozen array containing inside port, and the source attribute initialized to inside port.connect
,MessageEvent
) — 次のように初期化して :data
属性 ← 空文字列,ports
属性 ← 内側ポート のみを包含している新たな凍結配列,source
属性 ← 内側ポート -
[
worker 大域スコープ に関連する設定群オブジェクトを,service worker クライアントとして結び付けている
ServiceWorkerContainer
オブジェクト ]のクライアントメッセージキューを可能化する Enable the client message queue of the ServiceWorkerContainer object whose associated service worker client is worker global scope's relevant settings object. -
[ 内側設定群 により指定される担当のイベントループ ]を,破壊されるまで走らす Event loop: Run the responsible event loop specified by inside settings until it is destroyed.
注記: イベントループが走らすタスクによる,イベントの取り扱いや callback の実行は、 worker が終了されたときは,尚早に中止され得る。 The handling of events or the execution of callbacks by tasks run by the event loop might get prematurely aborted by the terminate a worker algorithm defined below.
注記: この段における worker の処理モデルは、 イベントループ処理モデルに述べられるとおり,[ close 中かが true にされた後の,イベントループが破壊される ]まで残り続ける。 The worker processing model remains on this step until the event loop is destroyed, which happens after the closing flag is set to true, as described in the event loop processing model.
- worker 大域スコープ の作動中なタイマー群が成すマップをクリアする Clear the worker global scope's map of active timers.
- worker 大域スコープ のポートリストを成す 各( ポート ) に対し :ポート の連絡を断つ Disentangle all the ports in the list of the worker's ports.
- worker 大域スコープ の所有者集合を空にする Empty worker global scope's owner set.
-
"
UA が worker を終了させ るときは、 worker のメインループ — すなわち,上で定義した “worker を走らす” 処理モデル — とは並列的に,次の手続きを走らすものとする: When a user agent is to terminate a worker, it must run the following steps in parallel with the worker's main loop (the "run a worker" processing model defined above):
-
worker 大域スコープ :← 当の worker の
WorkerGlobalScope
オブジェクト ↓ - worker 大域スコープ の close 中か ← true Set the worker's WorkerGlobalScope object's closing flag to true.
- worker 大域スコープ に関連する agent のイベントループのタスクキューを成す 各( タスク タスク ) に対し :タスク を処理することなく,破棄する【当のタスクキューから除去する】 If there are any tasks queued in the WorkerGlobalScope object's relevant agent's event loop's task queues, discard them without processing them.
- worker 内で現在走っているスクリプトを中止する Abort the script currently running in the worker.
-
IF[
worker 大域スコープ は
DedicatedWorkerGlobalScope
オブジェクトである (すなわち,worker は専用 worker である) ] :worker の暗黙的なポート【 worker 大域スコープ の内側ポート】のポートメッセージキューを空にする If the worker's WorkerGlobalScope object is actually a DedicatedWorkerGlobalScope object (i.e. the worker is a dedicated worker), then empty the port message queue of the port that the worker's implicit port is entangled with.
UA は、 ∧↓ を満たす worker を終了させてもよい:
User agents may invoke the terminate a worker algorithm when a worker stops being an active needed worker and the worker continues executing even after its closing flag was set to true.10.2.5. 稼働時のスクリプトエラー
worker のスクリプトにおいて,
catch されない稼働時のスクリプトエラーが生じた場合、
それは[
それまでにスクリプトエラーを取り扱う間に生じたもの
]でないならば、
UA は,次を遂行することになる
:例外を報告する( 当のエラー, 当の worker の WorkerGlobalScope
オブジェクト )
Whenever an uncaught runtime script error occurs in one of the worker's scripts, if the error did not occur while handling a previous script error, the user agent will report it for the worker's WorkerGlobalScope object.
10.2.6. worker の作成法
10.2.6.1. AbstractWorker
mixin
interface mixinAbstractWorker
{ attributeEventHandler
onerror
; };
AbstractWorker
インタフェースを実装するオブジェクトにおいては、
次に挙げるイベントハンドラ(および,それらに対応するイベントハンドライベント型)をイベントハンドラ IDL 属性としてサポートするものとする:
The following are the event handlers (and their corresponding event handler event types) that must be supported, as event handler IDL attributes, by objects implementing the AbstractWorker interface:
イベントハンドラ | イベントハンドライベント型 |
---|---|
onerror
| error
|
10.2.6.2. worker 用のスクリプト設定群
worker の環境設定群オブジェクトを設定しておく アルゴリズムは、 所与の ( JavaScript 実行文脈 実行文脈, 環境設定群オブジェクト 外側設定群, 数 安全でない worker 作成時刻 ) に対し: To set up a worker environment settings object, given a JavaScript execution context execution context, an environment settings object outside settings, and a number unsafeWorkerCreationTime:
- 継承した生成元 :← 外側設定群 の生成元 Let inherited origin be outside settings's origin.
- realm :← 実行文脈 の Realm 成分の値 Let realm be the value of execution context's Realm component.
- worker 大域スコープ :← realm の大域オブジェクト Let worker global scope be realm's global object.
-
設定群オブジェクト :← 新たな環境設定群オブジェクト — その各種アルゴリズムは、 次に従って定義される: Let settings object be a new environment settings object whose algorithms are defined as follows:
- realm 実行環境 :RETURN 実行文脈 The realm execution context • Return execution context.
- モジュールマップ :RETURN worker 大域スコープ のモジュールマップ The module map • Return worker global scope's module map.
- API 用基底 URL :RETURN worker 大域スコープ の URL The API base URL • Return worker global scope's url.
-
生成元
:RETURN [
次が満たされるならば 不透明な生成元 /
他の場合は 継承した生成元
]
:worker 大域スコープ の URL のスキーム = "
data
" The origin • Return a unique opaque origin if worker global scope's url's scheme is "data", and inherited origin otherwise. - 施策コンテナ :RETURN worker 大域スコープ の施策コンテナ The policy container • Return worker global scope's policy container.
- 非同一生成元能力は隔離されるか? :RETURN worker 大域スコープ の非同一生成元能力は隔離されるか The cross-origin isolated capability • Return worker global scope's cross-origin isolated capability.
- 時刻起点 :RETURN 時刻を粗くする( 安全でない worker 作成時刻, worker 大域スコープ の非同一生成元能力は隔離されるか ) The time origin • Return the result of coarsening unsafeWorkerCreationTime with worker global scope's cross-origin isolated capability.
- 設定群オブジェクト の :ID ← 新たな一意かつ不透明な文字列, 作成時の URL ← worker 大域スコープ の URL , トップレベル作成時の URL ← null , ターゲット閲覧文脈 ← null , 作動中な service worker ← null Set settings object's id to a new unique opaque string, creation URL to worker global scope's url, top-level creation URL to null, target browsing context to null, and active service worker to null.
-
設定群オブジェクト のトップレベル生成元 ← worker 大域スコープ に応じて :
DedicatedWorkerGlobalScope
オブジェクトであるならば 外側設定群 のトップレベル生成元 / 他の場合は 実装定義な値 If worker global scope is a DedicatedWorkerGlobalScope object, then set settings object's top-level origin to outside settings's top-level origin.Otherwise, set settings object's top-level origin to an implementation-defined value.これを適正に定義している最新情報は、 クライアント側ストレージ仕切り法を見よ。 See Client-Side Storage Partitioning for the latest on properly defining this.
- realm の [[HostDefined]] フィールド ← 設定群オブジェクト Set realm's [[HostDefined]] field to settings object.
- RETURN 設定群オブジェクト Return settings object.
10.2.6.3. 専用 worker と Worker
インタフェース
[Exposed=(Window,DedicatedWorker,SharedWorker)] interfaceWorker
:EventTarget
{ constructor((TrustedScriptURL
or USVString) scriptURL, optionalWorkerOptions
options = {}); undefinedterminate
(); undefinedpostMessage
(any message, sequence<object> transfer); undefinedpostMessage
(any message, optionalStructuredSerializeOptions
options = {}); }; dictionaryWorkerOptions
{WorkerType
type = "classic";RequestCredentials
credentials = "same-origin"; // † DOMString name = ""; }; enumWorkerType
{ "classic", "module" };Worker
includesAbstractWorker
;Worker
includesMessageEventTarget
;
注記:†
credentials
(資格情報)は、[
type
(種別) = "module
"
]の場合に限り,利用される。
credentials is only used if type is "module"
- worker =
new Worker(scriptURL [, options ])
-
新たな
Worker
オブジェクトを返す。 scriptURL はバックグラウンドで fetch され, 実行され、 新たな大域環境が作成される。 返される worker は、 その大域環境への通信チャネルを表現する。 Returns a new Worker object. scriptURL will be fetched and executed in the background, creating a new global environment for which worker represents the communication channel.\ -
options を利用すれば: options can be used to\
-
name
メンバを介して この大域環境の名前を定義できる — これは主として、 デバッグ目的にある。 define the name of that global environment via the name option, primarily for debugging purposes.\ -
type
メンバに "module
" を指定すれば、 この新たな大域環境が JavaScript モジュールをサポートすることを確保できる — その場合、credentials
メンバを通して, scriptURL がどう fetch されるかも指定できる。 It can also ensure this new global environment supports JavaScript modules (specify type: "module"), and if that is specified, can also be used to specify how scriptURL is fetched through the credentials option.
-
- worker.
terminate()
- worker に結び付けられた大域環境を中止する。 Aborts worker's associated global environment.
- worker.
postMessage(message, transfer)
- worker.
postMessage(message [, options ])
-
message をクローンして worker の大域環境へ伝送する。
[
transfer / [
options の
transfer
メンバ ]]には,一連のオブジェクトからなるリストを渡すことができ、 それらはクローンされずに転送される。 Clones message and transmits it to worker's global environment. transfer can be passed as a list of objects that are to be transferred rather than cloned.
各 Worker
オブジェクトには、
外側ポート
が結び付けられる
— それは:
Each Worker object has an associated outside port\
-
MessagePort
オブジェクトである。 (a MessagePort).\ - 当の worker 作成時に設定しておかれるチャネルの一部を成すが,公開されない。 This port is part of a channel that is set up when the worker is created, but it is not exposed.\
-
当の
Worker
オブジェクトより先にガーベジ収集されてはならない。 This object must never be garbage collected before the Worker object.
terminate()
メソッド手続きは
:これ°を結び付けている worker 上で worker を終了させる
The terminate() method, when invoked, must cause the terminate a worker algorithm to be run on the worker with which the object is associated.
postMessage(message, transfer)
メソッド手続きは
:RETURN これ°の外側ポート上の
postMessage
メソッド手続き( message, transfer )
postMessage(message, options)
メソッド手続きは
:RETURN これ°の外側ポート上の
postMessage
メソッド手続き( message, options )
このメソッドの message 引数は有構造データでもよい: The postMessage() method's first argument can be structured data:
worker.postMessage({ opcode: 'activate', device: 1938, parameters: [23, 102] });
new Worker(scriptURL, options)
構築子手続きは:
When the Worker(scriptURL, options) constructor is invoked, the user agent must run the following steps:
-
準拠するスクリプト URL :← 信用済みな型に準拠する文字列を取得する( ↓ )
:
TrustedScriptURL
, これ°に関連する大域オブジェクト, scriptURL, "Worker constructor
", "script
" Let compliantScriptURL be the result of invoking the Get Trusted Type compliant string algorithm with TrustedScriptURL, this's relevant global object, scriptURL, "Worker constructor", and "script". - 外側設定群 :← 現在の設定群オブジェクト Let outside settings be the current settings object.
-
worker URL :← URL を符号化法の下で相対的に構文解析する( 準拠するスクリプト URL , 外側設定群 ) Let worker URL be the result of encoding-parsing a URL given compliantScriptURL, relative to outside settings.
注記:
blob:
URL も含め,同一生成元であれば どの URL も利用できる。data:
URL も利用できるが、 作成される worker には不透明な生成元が伴われることになる。 Any same-origin URL (including blob: URLs) can be used. data: URLs can also be used, but they create a worker with an opaque origin. -
IF[
worker URL = 失敗
]
:THROW
SyntaxError
If worker URL is failure, then throw a "SyntaxError" DOMException. -
外側ポート :← 新たなオブジェクト(
MessagePort
, 外側設定群 の realm ) Let worker be a new Worker object.Let outside port be a new MessagePort in outside settings's realm. - 外側ポート のメッセージイベントターゲット ← これ° Set outside port's message event target to worker.
- これ°の外側ポート ← 外側ポート Set worker's outside port to outside port.
- この段は、 並列的に走らす :worker を走らす( ↓ ) :これ°, worker URL , 外側設定群, 外側ポート, options Run this step in parallel: • Run a worker given worker, worker URL, outside settings, outside port, and options.Return worker.
10.3. worker から可用な API
10.3.1. スクリプト/ライブラリの import 法
WorkerGlobalScope
オブジェクトの
importScripts(...urls)
メソッド手続きは:
The importScripts(...urls) method steps are:
- URL 文字列群 :← « » Let urlStrings be « ».
-
urls を成す 各( URL ) に対し: For each url of urls:
-
URL 文字列群 に次の結果を付加する
:信用済みな型に準拠する文字列を取得する( ↓ )
:
TrustedScriptURL
, これ°に関連する大域オブジェクト, URL , "WorkerGlobalScope importScripts
", "script
" Append the result of invoking the Get Trusted Type compliant string algorithm with TrustedScriptURL, this's relevant global object, url, "WorkerGlobalScope importScripts", and "script" to urlStrings.
-
URL 文字列群 に次の結果を付加する
:信用済みな型に準拠する文字列を取得する( ↓ )
:
- スクリプトを worker 大域スコープの中へ import する( これ°, URL 文字列群 ) Import scripts into worker global scope given this and urlStrings.
スクリプトを worker 大域スコープの中へ import する
アルゴリズムは、
所与の
:WorkerGlobalScope
オブジェクト worker 大域スコープ,
スカラー値文字列のリスト URL 群,
fetch フックを遂行するアルゴリズム fetch を遂行する(省略時は ε )
に対し:
To import scripts into worker global scope, given a WorkerGlobalScope object worker global scope, a list of scalar value strings urls, and an optional perform the fetch hook performFetch:
-
IF[
worker 大域スコープ の種別 = "
module
" ] :THROWTypeError
If worker global scope's type is "module", throw a TypeError exception. - 設定群オブジェクト :← 現在の設定群オブジェクト Let settings object be the current settings object.
- IF[ URL 群 は空である ] :RETURN If urls is empty, return.
- URL レコード群 :← « » Let urlRecords be « ».
-
URL 群 を成す 各( URL 文字列 ) に対し: For each url of urls:
- URL レコード ← URL を符号化法の下で相対的に構文解析する( URL 文字列, 設定群オブジェクト ) Let urlRecord be the result of encoding-parsing a URL given url, relative to settings object.
-
IF[
URL レコード = 失敗
]
:THROW
SyntaxError
If urlRecord is failure, then throw a "SyntaxError" DOMException. - URL レコード群 に URL レコード を付加する Append urlRecord to urlRecords.
-
URL レコード群 を成す 各( URL レコード ) に対し: For each urlRecord of urlRecords:
- スクリプト :← worker が import した古典スクリプトを fetch する( URL レコード, 設定群オブジェクト, fetch を遂行する ) (この段は、 例外を投出し得る) Fetch a classic worker-imported script given urlRecord and settings object, passing along performFetch if provided. If this succeeds, let script be the result. Otherwise, rethrow the exception.
-
古典スクリプトを走らす( スクリプト, エラーは再投出するか ← true ) Run the classic script script, with the rethrow errors argument set to true.
注記: スクリプトは、 次のいずれかが生じるまで走らすことになる :普通に返った/ 構文解析に失敗した/ 投出された例外を catch しなかった/ worker が終了されたため,尚早に中止された script will run until it either returns, fails to parse, fails to catch an exception, or gets prematurely aborted by the terminate a worker algorithm defined above.
[ スクリプトから例外が投出された / スクリプトは尚早に中止された ]ときは 【普通に返らなかった場合】 :この手続きすべてを中止した上で、 call 元のスクリプトにて,その[ 例外/中止 ]の処理を継続させる If an exception was thrown or if the script was prematurely aborted, then abort all these steps, letting the exception or aborting continue to be processed by the calling script.
注記: Service Workers [SW] は、 自前の fetch フックを遂行するアルゴリズムを与えて このアルゴリズムを走らす仕様の例である。 Service Workers is an example of a specification that runs this algorithm with its own perform the fetch hook. [SW]
10.3.3. WorkerLocation
インタフェース
[Exposed=Worker] interfaceWorkerLocation
{ stringifier readonly attribute USVStringhref
; readonly attribute USVStringorigin
; readonly attribute USVStringprotocol
; readonly attribute USVStringhost
; readonly attribute USVStringhostname
; readonly attribute USVStringport
; readonly attribute USVStringpathname
; readonly attribute USVStringsearch
; readonly attribute USVStringhash
; };
各 WorkerGlobalScope
オブジェクトは、
worker 所在オブジェクト
を有する
— それは、
ある WorkerLocation
オブジェクトであり,初期時は ε とする。
A WorkerLocation object has an associated WorkerGlobalScope object (a WorkerGlobalScope object).
【
これらのオブジェクトは、
一対一に対応する。
】【
初期値 ε は、
形式上のものであり,アクセスされ得ない
— location
の注記を見よ。
】
この節を通して、
URL
は,[
worker 所在オブジェクトとして当の WorkerLocation
オブジェクトを有している WorkerGlobalScope
オブジェクト
]の URL を指すとする。
↓
href
取得子手続きは
:RETURN URL を直列化する( URL )
The href getter steps are to return this's WorkerGlobalScope object's url, serialized.origin
取得子手続きは
:RETURN 生成元を直列化する( URL の生成元 )
The origin getter steps are to return the serialization of this's WorkerGlobalScope object's url's origin.protocol
取得子手続きは
:RETURN 次を順に連結する
:URL のスキーム,
":
"
The protocol getter steps are to return this's WorkerGlobalScope object's url's scheme, followed by ":".
host
取得子手続きは:
- ホスト :← URL のホスト
- IF[ ホスト = null ] :RETURN 空文字列
- ホスト :← ホストを直列化する( ホスト )
- ポート :← URL のポート
- IF[ ポート = null ] :RETURN ホスト
- ポート ← 整数を直列化する( ポート )
-
RETURN 次を順に連結する :ホスト, "
:
", ポート
hostname
取得子手続きは:
The hostname getter steps are:
port
取得子手続きは:
The port getter steps are:
pathname
取得子手続きは
:RETURN URL パスを直列化する( URL )
The pathname getter steps are to return the result of URL path serializing this's WorkerGlobalScope object's url.
search
取得子手続きは:
The search getter steps are: