From 067c5e2201a6dd5037479d67706ed387ddaaed50 Mon Sep 17 00:00:00 2001 From: Rayan Kanso Date: Thu, 18 Oct 2018 08:40:06 -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 Reviewed-on: https://chromium-review.googlesource.com/c/1283692 Commit-Queue: Rayan Kanso Reviewed-by: Peter Beverloo Cr-Commit-Position: refs/heads/master@{#600769} --- background-fetch/abort.https.window.js | 14 +++++++++++- background-fetch/service_workers/sw-abort.js | 23 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) 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}))); +});