From 97ab4b4388b1232cab4f0d7e9a9e9cdbc054fa83 Mon Sep 17 00:00:00 2001 From: Rayan Kanso Date: Wed, 17 Oct 2018 09:00:01 -0700 Subject: [PATCH] [Background Fetch] Throw DOMException for responseReady on abort. According to the spec, responseReady in BackgroundFetchRecord should throw an AbortError DOMException if the fetch was abandoned. https://wicg.github.io/background-fetch/#create-record-objects (2.4.3) Change-Id: Ieadf278acd061e05b8822014d0934f050fcac702 --- background-fetch/abort.https.window.js | 14 +++++++++++- background-fetch/service_workers/sw-abort.js | 23 ++++++++++++++++++++ background-fetch/service_workers/sw.js | 8 +++---- 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 background-fetch/service_workers/sw-abort.js diff --git a/background-fetch/abort.https.window.js b/background-fetch/abort.https.window.js index db01bf94b85fe9..7d35d7ec364b4a 100644 --- a/background-fetch/abort.https.window.js +++ b/background-fetch/abort.https.window.js @@ -56,4 +56,16 @@ backgroundFetchTest(async (test, backgroundFetch) => { }; }); -}, 'Calling BackgroundFetchRegistration.abort sets the correct fields and responses are still available'); \ No newline at end of file +}, 'Calling BackgroundFetchRegistration.abort sets the correct fields and responses are still available'); + +backgroundFetchTest(async (test, backgroundFetch) => { + const registration = await backgroundFetch.fetch( + uniqueId(), '/serviceworker/resources/slow-response.php'); + assert_true(await registration.abort()); + + const {results} = await getMessageFromServiceWorker(); + assert_equals(results.length, 1); + assert_false(results[0].response); + assert_equals(results[0].name, 'AbortError'); + +}, 'An aborted fetch throws a DOM exception when accessing an incomplete record', 'sw-abort.js'); \ No newline at end of file diff --git a/background-fetch/service_workers/sw-abort.js b/background-fetch/service_workers/sw-abort.js new file mode 100644 index 00000000000000..c61377758dff76 --- /dev/null +++ b/background-fetch/service_workers/sw-abort.js @@ -0,0 +1,23 @@ +importScripts('sw-helpers.js'); + +async function getFetchResult(record) { + try { + await record.responseReady; + } catch (e) { + return { + response: false, + name: e.name, + }; + } + + return { + response: true, + }; +} +self.addEventListener('backgroundfetchabort', event => { + event.waitUntil( + event.registration.matchAll() + .then(records => + Promise.all(records.map(record => getFetchResult(record)))) + .then(results => sendMessageToDocument({results}))); +}); diff --git a/background-fetch/service_workers/sw.js b/background-fetch/service_workers/sw.js index 2e3fbfff1a83e2..4878362c6490bd 100644 --- a/background-fetch/service_workers/sw.js +++ b/background-fetch/service_workers/sw.js @@ -13,7 +13,7 @@ async function getFetchResult(record) { }; } -function handleBackgroundFetchUpdateEvent(event) { +function handleBackgroundFetchEvent(event) { event.waitUntil( event.registration.matchAll() .then(records => @@ -25,6 +25,6 @@ function handleBackgroundFetchUpdateEvent(event) { })); } -self.addEventListener('backgroundfetchsuccess', handleBackgroundFetchUpdateEvent); -self.addEventListener('backgroundfetchfail', handleBackgroundFetchUpdateEvent); -self.addEventListener('backgroundfetchabort', handleBackgroundFetchUpdateEvent); +self.addEventListener('backgroundfetchsuccess', handleBackgroundFetchEvent); +self.addEventListener('backgroundfetchfail', handleBackgroundFetchEvent); +self.addEventListener('backgroundfetchabort', handleBackgroundFetchEvent);