From 76d55de619e51fd2cfa8833e47df5fb40ad475a7 Mon Sep 17 00:00:00 2001 From: Rayan Kanso Date: Mon, 22 Oct 2018 04:40:50 -0700 Subject: [PATCH] [Background Fetch] Fix match() logic. Bug: 896768 Change-Id: I5d82e68ddca157a240ceaaec8990d2553366fbbb --- background-fetch/fetch.https.window.js | 38 ++++++++++++++++++++++++++ background-fetch/service_workers/sw.js | 25 ++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/background-fetch/fetch.https.window.js b/background-fetch/fetch.https.window.js index c5fa18725a5dc3..aee777d986e4b3 100644 --- a/background-fetch/fetch.https.window.js +++ b/background-fetch/fetch.https.window.js @@ -282,3 +282,41 @@ backgroundFetchTest(async (test, backgroundFetch) => { assert_equals(nullResponse, null); }, 'Fetches with mixed content should fail.'); + +backgroundFetchTest(async (test, backgroundFetch) => { + const registrationId = 'matchexistingrequest'; + 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, 1); + + 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'); + +}, 'Matching to a single request should work'); + +backgroundFetchTest(async (test, backgroundFetch) => { + const registrationId = 'matchmissingrequest'; + 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, 0); + + assert_equals(eventRegistration.id, registration.id); + assert_equals(eventRegistration.result, 'success'); + assert_equals(eventRegistration.failureReason, ''); + +}, 'Matching to a non-existing request should work'); \ No newline at end of file diff --git a/background-fetch/service_workers/sw.js b/background-fetch/service_workers/sw.js index 1c4fb10f05260f..80b479291ea11d 100644 --- a/background-fetch/service_workers/sw.js +++ b/background-fetch/service_workers/sw.js @@ -13,10 +13,33 @@ async function getFetchResult(record) { } 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 'matchmissingrequest': + matchFunction = event.registration.match.bind( + event.registration, '/background-fetch/resources/missing.txt'); + break; + default: + matchFunction = event.registration.matchAll.bind(event.registration); + break; + } + event.waitUntil( - event.registration.matchAll() + matchFunction() + // Format `match(All)?` function results. + .then(records => { + if (!records) return []; // Nothing was matched. + if (!records.map) return [records]; // One entry was returned. + return records; // Already in a list. + }) + // Extract responses. .then(records => Promise.all(records.map(record => getFetchResult(record)))) + // Clone registration and send message. .then(results => { const registrationCopy = cloneRegistration(event.registration); sendMessageToDocument(