Commit 238d82b2 authored by Matt Falkenhagen's avatar Matt Falkenhagen Committed by Commit Bot

Reland: service worker: Add WPT tests for resultingClientId.

Reland of r592363, which was reverted due to timeouts. The timeout issue
should be fixed by r594723, which changed the mechanism of sending the
"last url" message from the navigated frame.

Other revisions from the original patch:
- Removed the unused expected_tags parameter from the default
  variant test function.
- Removed the dummy message resolver for "last url" from
  client_variant_test, which is no longer needed after the change
  mentioned above.
- Fixed a bug in redirect-worker.js's getClients(), where
  we returned instead of continued to the next iteration.

Original commit description:
> This adds a ?client variant to navigation-redirect.https.html, which
> does a lot of redirect tests. This tests the following:
> - resultingClientId is the id of the client, if one was created via
>   that request.
> - get(resultingClientId) for requests that did not create a client
>   resolves with undefined.
> - resultingClientId is reset on cross-origin redirects.
> - Client.url is the creation URL.
>
> Bug: 778497
> Change-Id: I08387e96a97df4656d800637862b67b7c8466e14
> Reviewed-on: https://chromium-review.googlesource.com/1206054
> Reviewed-by: Makoto Shimazu <shimazu@chromium.org>
> Commit-Queue: Matt Falkenhagen <falken@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#592363}

Bug: 778497
Change-Id: I770f98cef7618c89c1b54245bc89a9c53a19bd5c
Reviewed-on: https://chromium-review.googlesource.com/1250306Reviewed-by: default avatarBen Kelly <wanderview@chromium.org>
Commit-Queue: Matt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595249}
parent 75d687cb
......@@ -11,6 +11,7 @@ FAIL SW-fallbacked redirect to same-origin same-scope with different hash fragme
PASS SW-fallbacked redirect to same-origin other-scope.
PASS SW-fallbacked redirect to other-origin out-scope.
PASS SW-fallbacked redirect to other-origin in-scope.
PASS SW-fallbacked redirect to other-origin and back to same-origin.
PASS SW-generated redirect to same-origin out-scope.
FAIL SW-generated redirect to same-origin out-scope with a hash fragment. assert_object_equals: Intercepted URLs should match. property "0" expected ["https://web-platform.test:8444/service-workers/service-worker/resources/navigation-redirect-scope1.py?sw=gen&url=https%3A%2F%2Fweb-platform.test%3A8444%2Fservice-workers%2Fservice-worker%2Fresources%2Fnavigation-redirect-out-scope.py%3F#ref"] got ["https://web-platform.test:8444/service-workers/service-worker/resources/navigation-redirect-scope1.py?sw=gen&url=https%3A%2F%2Fweb-platform.test%3A8444%2Fservice-workers%2Fservice-worker%2Fresources%2Fnavigation-redirect-out-scope.py%3F"]
FAIL SW-generated redirect to same-origin out-scope with different hash fragments. assert_object_equals: Intercepted URLs should match. property "0" expected ["https://web-platform.test:8444/service-workers/service-worker/resources/navigation-redirect-scope1.py?sw=gen&url=https%3A%2F%2Fweb-platform.test%3A8444%2Fservice-workers%2Fservice-worker%2Fresources%2Fnavigation-redirect-out-scope.py%3F%23ref2#ref"] got ["https://web-platform.test:8444/service-workers/service-worker/resources/navigation-redirect-scope1.py?sw=gen&url=https%3A%2F%2Fweb-platform.test%3A8444%2Fservice-workers%2Fservice-worker%2Fresources%2Fnavigation-redirect-out-scope.py%3F%23ref2"]
......
......@@ -44,6 +44,20 @@ function get_request_infos(worker) {
});
}
function get_clients(worker, actual_ids) {
return new Promise(function(resolve) {
var channel = new MessageChannel();
channel.port1.onmessage = (msg) => {
resolve(msg.data.clients);
};
worker.postMessage({
command: 'getClients',
actual_ids,
port: channel.port2
}, [channel.port2]);
});
}
window.addEventListener('message', on_message, false);
function on_message(e) {
......@@ -59,6 +73,11 @@ function on_message(e) {
.then(function(data) {
send_result(e.data.id, data);
});
} else if (command == 'get_clients') {
get_clients(worker, e.data.message.actual_ids)
.then(function(data) {
send_result(e.data.id, data);
});
} else if (command == 'unregister') {
registration.unregister()
.then(function() {
......
......@@ -5,6 +5,13 @@ var cacheName = 'urls-' + self.registration.scope;
var waitUntilPromiseList = [];
// Sends the requests seen by this worker. The output is:
// {
// requestInfos: [
// {url: url1, resultingClientId: id1},
// {url: url2, resultingClientId: id2},
// ]
// }
async function getRequestInfos(event) {
// Wait for fetch events to finish.
await Promise.all(waitUntilPromiseList);
......@@ -15,8 +22,11 @@ async function getRequestInfos(event) {
const requestList = await cache.keys();
const requestInfos = [];
for (let i = 0; i < requestList.length; i++) {
const response = await cache.match(requestList[i]);
const body = await response.json();
requestInfos[i] = {
url: requestList[i].url,
resultingClientId: body.resultingClientId
};
}
await caches.delete(cacheName);
......@@ -24,13 +34,50 @@ async function getRequestInfos(event) {
event.data.port.postMessage({requestInfos});
}
// Sends the results of clients.get(id) from this worker. The
// input is:
// {
// actual_ids: {a: id1, b: id2, x: id3}
// }
//
// The output is:
// {
// clients: {
// a: {found: false},
// b: {found: false},
// x: {
// id: id3,
// url: url1,
// found: true
// }
// }
// }
async function getClients(event) {
// |actual_ids| is like:
// {a: id1, b: id2, x: id3}
const actual_ids = event.data.actual_ids;
const result = {}
for (let key of Object.keys(actual_ids)) {
const id = actual_ids[key];
const client = await self.clients.get(id);
if (client === undefined)
result[key] = {found: false};
else
result[key] = {found: true, url: client.url, id: client.id};
}
event.data.port.postMessage({clients: result});
}
self.addEventListener('message', async function(event) {
if (event.data.command == 'getRequestInfos') {
event.waitUntil(getRequestInfos(event));
return;
}
// TODO(falken): Add a getClientInfos command to test Clients API.
if (event.data.command == 'getClients') {
event.waitUntil(getClients(event));
return;
}
});
function get_query_params(url) {
......@@ -49,7 +96,11 @@ function get_query_params(url) {
self.addEventListener('fetch', function(event) {
var waitUntilPromise = caches.open(cacheName).then(function(cache) {
return cache.put(event.request, new Response());
const responseBody = {};
responseBody['resultingClientId'] = event.resultingClientId;
const headers = new Headers({'Content-Type': 'application/json'});
const response = new Response(JSON.stringify(responseBody), {headers});
return cache.put(event.request, response);
});
event.waitUntil(waitUntilPromise);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment