Skip to content

Commit d4a47f5

Browse files
dtapuskachromium-wpt-export-bot
authored andcommitted
Add ability for service worker to filter out frozen windows.
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 is specified in https://wicg.github.io/page-lifecycle/ and w3c/ServiceWorker#1442 BUG=957597 Change-Id: I6abe1882e88c65dac99250db5bb7fa8d3a4b2b1d
1 parent c73e28f commit d4a47f5

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<!DOCTYPE html>
2+
<title>Service Worker: Clients.matchAll</title>
3+
<script src="/resources/testharness.js"></script>
4+
<script src="/resources/testharnessreport.js"></script>
5+
<script src="resources/test-helpers.sub.js"></script>
6+
<script>
7+
var scope = 'resources/clients-frame-freeze.html';
8+
var windows = [];
9+
var expected_window_1 =
10+
['visible', false, "frozen", new URL(scope + '#1', location).toString(), 'window', 'top-level'];
11+
var expected_window_2 =
12+
// visibilityState, focused, frozen, url, type, frameType
13+
['visible', false, "active", new URL(scope + '#2', location).toString(), 'window', 'top-level'];
14+
function with_window(url, name) {
15+
return new Promise(function(resolve) {
16+
var child = window.open(url, name);
17+
window.onmessage = () => {resolve(child)};
18+
});
19+
}
20+
21+
promise_test(function(t) {
22+
return service_worker_unregister_and_register(
23+
t, 'resources/clients-matchall-worker.tentative.js', scope)
24+
.then(function(registration) {
25+
t.add_cleanup(function() {
26+
return service_worker_unregister(t, scope);
27+
});
28+
29+
return wait_for_state(t, registration.installing, 'activated');
30+
})
31+
.then(function() { return with_window(scope + '#1', 'Child 1'); })
32+
.then(function(window1) {
33+
windows.push(window1);
34+
return with_window(scope + '#2', 'Child 2');
35+
})
36+
.then(function(window2) {
37+
windows.push(window2);
38+
return new Promise(function(resolve) {
39+
window.onmessage = resolve;
40+
windows[0].postMessage('freeze');
41+
});
42+
})
43+
.then(function() {
44+
var channel = new MessageChannel();
45+
46+
return new Promise(function(resolve) {
47+
channel.port1.onmessage = resolve;
48+
windows[1].navigator.serviceWorker.controller.postMessage(
49+
{port:channel.port2}, [channel.port2]);
50+
});
51+
})
52+
.then(function(e) {
53+
assert_equals(e.data.length, 1);
54+
assert_array_equals(e.data[0], expected_window_2);
55+
})
56+
.then(function() {
57+
var channel = new MessageChannel();
58+
59+
return new Promise(function(resolve) {
60+
channel.port1.onmessage = resolve;
61+
windows[1].navigator.serviceWorker.controller.postMessage(
62+
{port:channel.port2, options: {lifecycleState: "all"}}, [channel.port2]);
63+
});
64+
})
65+
.then(function(e) {
66+
assert_equals(e.data.length, 2);
67+
// No specific order is required, so support inversion.
68+
if (e.data[0][3] == new URL(scope + '#2', location)) {
69+
assert_array_equals(e.data[0], expected_window_2);
70+
assert_array_equals(e.data[1], expected_window_1);
71+
} else {
72+
assert_array_equals(e.data[0], expected_window_1);
73+
assert_array_equals(e.data[1], expected_window_2);
74+
}
75+
})
76+
.then(function() {
77+
var channel = new MessageChannel();
78+
79+
return new Promise(function(resolve) {
80+
channel.port1.onmessage = resolve;
81+
windows[1].navigator.serviceWorker.controller.postMessage(
82+
{port:channel.port2, options: {lifecycleState: "frozen"}}, [channel.port2]);
83+
});
84+
})
85+
.then(function(e) {
86+
assert_equals(e.data.length, 1);
87+
assert_array_equals(e.data[0], expected_window_1);
88+
})
89+
.then(function() {
90+
var channel = new MessageChannel();
91+
92+
return new Promise(function(resolve) {
93+
channel.port1.onmessage = resolve;
94+
windows[1].navigator.serviceWorker.controller.postMessage(
95+
{port:channel.port2, options: {lifecycleState: "active"}}, [channel.port2]);
96+
});
97+
})
98+
.then(function(e) {
99+
assert_equals(e.data.length, 1);
100+
assert_array_equals(e.data[0], expected_window_2);
101+
});
102+
}, 'Test Clients.matchAll()');
103+
104+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<script src="/resources/testdriver.js"></script>
3+
<script src="/resources/testdriver-vendor.js"></script>
4+
<script>
5+
document.addEventListener('freeze', () => {
6+
opener.postMessage('frozen', "*");
7+
});
8+
9+
window.onmessage = (e) => {
10+
if (e.data == 'freeze') {
11+
test_driver.freeze();
12+
}
13+
};
14+
opener.postMessage('loaded', '*');
15+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
self.onmessage = function(e) {
2+
var port = e.data.port;
3+
var options = e.data.options;
4+
5+
e.waitUntil(self.clients.matchAll(options)
6+
.then(function(clients) {
7+
var message = [];
8+
clients.forEach(function(client) {
9+
var frame_type = client.frameType;
10+
if (client.url.indexOf('clients-matchall-include-uncontrolled.https.html') > -1 &&
11+
client.frameType == 'auxiliary') {
12+
// The test tab might be opened using window.open() by the test framework.
13+
// In that case, just pretend it's top-level!
14+
frame_type = 'top-level';
15+
}
16+
message.push([client.visibilityState,
17+
client.focused,
18+
client.lifecycleState,
19+
client.url,
20+
client.type,
21+
frame_type]);
22+
});
23+
// Sort by url
24+
if (!e.data.disableSort) {
25+
message.sort(function(a, b) { return a[2] > b[2] ? 1 : -1; });
26+
}
27+
port.postMessage(message);
28+
})
29+
.catch(e => {
30+
port.postMessage('clients.matchAll() rejected: ' + e);
31+
}));
32+
};

0 commit comments

Comments
 (0)