Bug 1507325 [wpt PR 14060] - Add a list of BackgroundFetchRecord objects to BackgroundFetchRegistration., a=testonly

Automatic update from web-platform-testsAdd a list of BackgroundFetchRecord objects to BackgroundFetchRegistration.

When we allow access to active background fetches, BackgroundFetchRecords for
the fetch can be accessed from multiple places. It simplifies matters if
we return the same record for a request whenever it's accessed.

This CL adds a (private) list of BackgroundFetchRecords to the
BackgroundFetchRegistration object, and updates it every time match and
matchAll() are called.

The second change introduced here is to not immediately resolve
responseReady() if the fetch is active and a response for the request
isn't yet available. Once the fetch has completed, or there's a response
available for the request, we resolve pending promises. We also make sure to
return the same promise (resolved or unresolved) for a given record,
every time responseReady is called.

For a more detailed discussion, see the following doc:
https://docs.google.com/document/d/1CrbWrnnshhyp_SfiAeuODpnQX36GK3Bsi19rXQGez6Q/edit?usp=sharing

Bug: 875201

Change-Id: I8cb386efd19086c0993ad2be2fb2691ad90597ec
Reviewed-on: https://chromium-review.googlesource.com/c/1336151
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Reviewed-by: Rayan Kanso <rayankans@chromium.org>
Reviewed-by: Peter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610410}

--

wpt-commits: 12720fdc8914deec3e7cc627036a26217cc1e908
wpt-pr: 14060
This commit is contained in:
Mugdha Lakhani 2018-11-30 16:53:25 +00:00 коммит произвёл moz-wptsync-bot
Родитель dd569b435b
Коммит 6850e1c0d0
2 изменённых файлов: 34 добавлений и 2 удалений

Просмотреть файл

@ -300,3 +300,27 @@ backgroundFetchTest(async (test, backgroundFetch) => {
}, 'Matching to a non-existing request should work');
backgroundFetchTest(async (test, backgroundFetch) => {
const registrationId = 'matchexistingrequesttwice';
const registration =
await backgroundFetch.fetch(registrationId, 'resources/feature-name.txt');
assert_equals(registration.id, registrationId);
const {type, eventRegistration, results} = await getMessageFromServiceWorker();
assert_equals('backgroundfetchsuccess', type);
assert_equals(results.length, 2);
assert_equals(eventRegistration.id, registration.id);
assert_equals(eventRegistration.result, 'success');
assert_equals(eventRegistration.failureReason, '');
assert_true(results[0].url.includes('resources/feature-name.txt'));
assert_equals(results[0].status, 200);
assert_equals(results[0].text, 'Background Fetch');
assert_true(results[1].url.includes('resources/feature-name.txt'));
assert_equals(results[1].status, 200);
assert_equals(results[1].text, 'error');
}, 'Matching multiple times on the same request works as expected.');

Просмотреть файл

@ -8,17 +8,25 @@ async function getFetchResult(record) {
return {
url: response.url,
status: response.status,
text: await response.text(),
text: await response.text().catch(() => 'error'),
};
}
function handleBackgroundFetchEvent(event) {
let matchFunction = null;
switch (event.registration.id) {
case 'matchexistingrequest':
matchFunction = event.registration.match.bind(
event.registration, '/background-fetch/resources/feature-name.txt');
break;
case 'matchexistingrequesttwice':
matchFunction = (async () => {
const match1 = await event.registration.match('/background-fetch/resources/feature-name.txt');
const match2 = await event.registration.match('/background-fetch/resources/feature-name.txt');
return [match1, match2];
}).bind(event.registration);
break;
case 'matchmissingrequest':
matchFunction = event.registration.match.bind(
event.registration, '/background-fetch/resources/missing.txt');
@ -38,7 +46,7 @@ function handleBackgroundFetchEvent(event) {
})
// Extract responses.
.then(records =>
Promise.all(records.map(record => getFetchResult(record))))
Promise.all(records.map(record => getFetchResult(record))))
// Clone registration and send message.
.then(results => {
const registrationCopy = cloneRegistration(event.registration);