Chromium Code Reviews| Index: third_party/WebKit/LayoutTests/external/wpt/service-workers/service-worker/resources/foreign-fetch-helpers.js |
| diff --git a/third_party/WebKit/LayoutTests/external/wpt/service-workers/service-worker/resources/foreign-fetch-helpers.js b/third_party/WebKit/LayoutTests/external/wpt/service-workers/service-worker/resources/foreign-fetch-helpers.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3115b9ce56366067366c508e631cf0a6327acaf3 |
| --- /dev/null |
| +++ b/third_party/WebKit/LayoutTests/external/wpt/service-workers/service-worker/resources/foreign-fetch-helpers.js |
| @@ -0,0 +1,46 @@ |
| +// Common helper functions for foreign fetch tests. |
| + |
| +// Installs a service worker on a different origin. Both |worker| and |scope| |
| +// are resolved relative to the /serviceworker/resources/ directory on a |
|
falken
2017/02/07 02:10:47
nit: /service-workers/service-worker/resources/ di
Marijn Kruisselbrink
2017/02/13 22:00:26
Done
|
| +// remote origin. |
| +function install_cross_origin_worker( |
| + t, worker, scope, origin = get_host_info().HTTPS_REMOTE_ORIGIN) { |
| + return with_iframe(origin + new URL('resources/install-worker-helper.html', location).pathname) |
| + .then(frame => new Promise((resolve, reject) => { |
| + var channel = new MessageChannel(); |
|
falken
2017/02/07 02:10:47
nit: let or const for consistency with fetch_from_
|
| + frame.contentWindow.postMessage({worker: worker, |
| + options: {scope: scope}, |
| + port: channel.port1}, |
|
falken
2017/02/07 02:10:47
nit: Is it possible to remove the port property? I
Marijn Kruisselbrink
2017/02/13 22:00:26
Yeah, here using event.source directly would indee
|
| + '*', [channel.port1]); |
| + channel.port2.onmessage = reply => { |
| + if (reply.data == 'success') resolve(); |
| + else reject(reply.data); |
| + }; |
| + })); |
| +} |
| + |
| +// Performs a fetch from a different origin. By default this performs a fetch |
| +// from a window on that origin, but if |worker_type| is 'dedicated' or 'shared' |
| +// the fetch is made from a worker on that origin instead. |
| +// This uses a window rather than an iframe because an iframe might get blocked |
| +// by mixed content checks. |
| +function fetch_from_different_origin(origin, url, worker_type) { |
| + var win = open(origin + new URL('resources/foreign-fetch-helper-iframe.html', location).pathname); |
| + return new Promise(resolve => { |
| + self.addEventListener('message', e => { |
| + if (e.source != win) return; |
| + resolve(); |
| + }); |
| + }) |
| + .then(() => new Promise((resolve) => { |
| + let channel = new MessageChannel(); |
|
falken
2017/02/07 02:10:47
const?
Marijn Kruisselbrink
2017/02/13 22:00:25
Done
|
| + win.postMessage({url: url, |
| + port: channel.port1, |
| + worker: worker_type}, |
| + '*', [channel.port1]); |
| + channel.port2.onmessage = reply => { |
| + win.close(); |
| + resolve(reply.data); |
| + }; |
| + })); |
| +} |