Skip to content

Commit

Permalink
Implement ServiceWorkerContainer.onmessageerror.
Browse files Browse the repository at this point in the history
This patch implements ServiceWorkerContainer.onmessageerror and writes
WPT test for it.

chromestatus: https://chromestatus.com/feature/5699031592337408

Bug: 926102
Change-Id: I60e73dcb69de4a2876cc966dfa1809a30510b5f2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1864409
Reviewed-by: Kent Tamura <tkent@chromium.org>
Reviewed-by: Matt Falkenhagen <falken@chromium.org>
Commit-Queue: Wei4 Wang <wei4.wang@intel.com>
Cr-Commit-Position: refs/heads/master@{#718507}
  • Loading branch information
wangw-1991 authored and chromium-wpt-export-bot committed Nov 25, 2019
1 parent ff1ea7c commit c2ddea9
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";
self.importScripts("/resources/testharness.js");

let state = "start in worker";

self.onmessage = e => {
if (e.data === "start in window") {
assert_equals(state, "start in worker");
e.source.postMessage(state);
state = "waiting for message from the window";
} else if (e.data === "we are expecting a messageerror due to the worker sending us a SAB") {
assert_equals(state, "waiting for message from the window");
e.source.postMessage(new SharedArrayBuffer());
state = "done in worker";
} else {
e.source.postMessage(`worker onmessage was reached when in state "${state}" and data ${e.data}`);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cross-Origin-Embedder-Policy: require-corp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Test ServiceWorkerContainer.onmessageerror using SharedArrayBuffer</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>

<script>
"use strict";
promise_test(t => {
const scope = "resources/blank.html";
return service_worker_unregister_and_register(t, "resources/serviceworker-success.js", scope)
.then(reg => {
t.add_cleanup(() => service_worker_unregister(t, scope));
return wait_for_state(t, reg.installing, "activated");
})
.then(() => with_iframe(scope))
.then(iframe => {
t.add_cleanup(() => iframe.remove());
const sw = iframe.contentWindow.navigator.serviceWorker;
let state = "start in window";

return new Promise(resolve => {
sw.onmessage = t.step_func(e => {
if (e.data === "start in worker") {
assert_equals(state, "start in window");
state = "we are expecting a messageerror due to the worker sending us a SAB";
sw.controller.postMessage(state);
} else {
assert_unreached("Got an unexpected message from the service worker: " + e.data);
}
});

sw.onmessageerror = t.step_func(e => {
assert_equals(state, "we are expecting a messageerror due to the worker sending us a SAB");
assert_equals(e.data, null, "data");
assert_equals(e.origin, self.origin, "origin");
assert_not_equals(e.source, null, "source");
assert_equals(e.ports.length, 0, "ports length");
state = "done in window";
resolve();
});

sw.controller.postMessage(state);
});
});
});
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
19 changes: 19 additions & 0 deletions wasm/serialization/module/resources/serviceworker-success.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";
self.importScripts("/resources/testharness.js");
self.importScripts("./create-empty-wasm-module.js");

let state = "start in worker";

self.onmessage = e => {
if (e.data === "start in window") {
assert_equals(state, "start in worker");
e.source.postMessage(state);
state = "waiting for message from the window";
} else if (e.data === "we are expecting a messageerror due to the worker sending us a WebAssembly.Module") {
assert_equals(state, "waiting for message from the window");
e.source.postMessage(createEmptyWasmModule());
state = "done in worker";
} else {
e.source.postMessage(`worker onmessage was reached when in state "${state}" and data ${e.data}`);
}
};
49 changes: 49 additions & 0 deletions wasm/serialization/module/window-serviceworker-success.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Test ServiceWorkerContainer.onmessageerror using WebAssembly.Module</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
<script src="./resources/create-empty-wasm-module.js"></script>

<script>
"use strict";
promise_test(t => {
const scope = "resources/blank.html";
return service_worker_unregister_and_register(t, "resources/serviceworker-success.js", scope)
.then(reg => {
t.add_cleanup(() => service_worker_unregister(t, scope));
return wait_for_state(t, reg.installing, "activated");
})
.then(() => with_iframe(scope))
.then(iframe => {
t.add_cleanup(() => iframe.remove());
const sw = iframe.contentWindow.navigator.serviceWorker;
let state = "start in window";

return new Promise(resolve => {
sw.onmessage = t.step_func(e => {
if (e.data === "start in worker") {
assert_equals(state, "start in window");
state = "we are expecting a messageerror due to the worker sending us a WebAssembly.Module";
sw.controller.postMessage(state);
} else {
assert_unreached("Got an unexpected message from the service worker: " + e.data);
}
});

sw.onmessageerror = t.step_func(e => {
assert_equals(state, "we are expecting a messageerror due to the worker sending us a WebAssembly.Module");
assert_equals(e.data, null, "data");
assert_equals(e.origin, self.origin, "origin");
assert_not_equals(e.source, null, "source");
assert_equals(e.ports.length, 0, "ports length");
state = "done in window";
resolve();
});

sw.controller.postMessage(state);
});
});
});
</script>

0 comments on commit c2ddea9

Please sign in to comment.