Skip to content

Commit

Permalink
Add ability for service worker to filter out frozen windows.
Browse files Browse the repository at this point in the history
Add code to support handling of frozen clients. Frozen clients do not
run their event loop, so postMessage to them just causes problems. To
allow service workers to continue working with frozen windows we expose
includeFrozen in the matchAll and the frozen attribute on the Client. If
a service worker calls focus on a client it will unfreeze the window when
it is moved to have focus. This feature is currently marked as
experimental and an intent to ship will be sent.

This is specified in https://wicg.github.io/page-lifecycle/ and
w3c/ServiceWorker#1442

BUG=957597

Change-Id: I6abe1882e88c65dac99250db5bb7fa8d3a4b2b1d
  • Loading branch information
dtapuska authored and chromium-wpt-export-bot committed Aug 21, 2019
1 parent c73e28f commit b86aa47
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<!DOCTYPE html>
<title>Service Worker: Clients.matchAll</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
var scope = 'resources/clients-frame-freeze.html';
var windows = [];
var expected_window_1 =
['visible', false, "frozen", new URL(scope + '#1', location).toString(), 'window', 'top-level'];
var expected_window_2 =
// visibilityState, focused, frozen, url, type, frameType
['visible', false, "active", new URL(scope + '#2', location).toString(), 'window', 'top-level'];
function with_window(url, name) {
return new Promise(function(resolve) {
var child = window.open(url, name);
window.onmessage = () => {resolve(child)};
});
}

promise_test(function(t) {
return service_worker_unregister_and_register(
t, 'resources/clients-matchall-worker.tentative.js', scope)
.then(function(registration) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

return wait_for_state(t, registration.installing, 'activated');
})
.then(function() { return with_window(scope + '#1', 'Child 1'); })
.then(function(window1) {
windows.push(window1);
return with_window(scope + '#2', 'Child 2');
})
.then(function(window2) {
windows.push(window2);
return new Promise(function(resolve) {
window.onmessage = resolve;
windows[0].postMessage('freeze');
});
})
.then(function() {
var channel = new MessageChannel();

return new Promise(function(resolve) {
channel.port1.onmessage = resolve;
windows[1].navigator.serviceWorker.controller.postMessage(
{port:channel.port2}, [channel.port2]);
});
})
.then(function(e) {
assert_equals(e.data.length, 1);
assert_array_equals(e.data[0], expected_window_2);
})
.then(function() {
var channel = new MessageChannel();

return new Promise(function(resolve) {
channel.port1.onmessage = resolve;
windows[1].navigator.serviceWorker.controller.postMessage(
{port:channel.port2, options: {lifecycleState: "all"}}, [channel.port2]);
});
})
.then(function(e) {
assert_equals(e.data.length, 2);
// No specific order is required, so support inversion.
if (e.data[0][3] == new URL(scope + '#2', location)) {
assert_array_equals(e.data[0], expected_window_2);
assert_array_equals(e.data[1], expected_window_1);
} else {
assert_array_equals(e.data[0], expected_window_1);
assert_array_equals(e.data[1], expected_window_2);
}
})
.then(function() {
var channel = new MessageChannel();

return new Promise(function(resolve) {
channel.port1.onmessage = resolve;
windows[1].navigator.serviceWorker.controller.postMessage(
{port:channel.port2, options: {lifecycleState: "frozen"}}, [channel.port2]);
});
})
.then(function(e) {
assert_equals(e.data.length, 1);
assert_array_equals(e.data[0], expected_window_1);
})
.then(function() {
var channel = new MessageChannel();

return new Promise(function(resolve) {
channel.port1.onmessage = resolve;
windows[1].navigator.serviceWorker.controller.postMessage(
{port:channel.port2, options: {lifecycleState: "active"}}, [channel.port2]);
});
})
.then(function(e) {
assert_equals(e.data.length, 1);
assert_array_equals(e.data[0], expected_window_2);
});
}, 'Test Clients.matchAll()');

</script>
15 changes: 15 additions & 0 deletions service-workers/service-worker/resources/clients-frame-freeze.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script>
document.addEventListener('freeze', () => {
opener.postMessage('frozen', "*");
});

window.onmessage = (e) => {
if (e.data == 'freeze') {
test_driver.freeze();
}
};
opener.postMessage('loaded', '*');
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
self.onmessage = function(e) {
var port = e.data.port;
var options = e.data.options;

e.waitUntil(self.clients.matchAll(options)
.then(function(clients) {
var message = [];
clients.forEach(function(client) {
var frame_type = client.frameType;
if (client.url.indexOf('clients-matchall-include-uncontrolled.https.html') > -1 &&
client.frameType == 'auxiliary') {
// The test tab might be opened using window.open() by the test framework.
// In that case, just pretend it's top-level!
frame_type = 'top-level';
}
message.push([client.visibilityState,
client.focused,
client.lifecycleState,
client.url,
client.type,
frame_type]);
});
// Sort by url
if (!e.data.disableSort) {
message.sort(function(a, b) { return a[2] > b[2] ? 1 : -1; });
}
port.postMessage(message);
})
.catch(e => {
port.postMessage('clients.matchAll() rejected: ' + e);
}));
};

0 comments on commit b86aa47

Please sign in to comment.