From 71d65e14d431545e7fb36d51cccb09675707e82b Mon Sep 17 00:00:00 2001 From: Rayan Kanso Date: Thu, 18 Oct 2018 03:44:47 -0700 Subject: [PATCH] [Background Fetch] Simplify fetch storage workflow. In addition, MatchAll should return all records, including unprocessed ones. To support this, the following changes were made: - Creating a registration also stores all the requests with an empty response in the cache. - When an individual request is processed, the failure reason (if any) is stored in the metadata. - The logic of GetSettledFetchesTask was moved to MarkRegistrationForDeletionTask. It checks the metadata rather than the cache itself to find a failure reason (if any). - Match/MatchAll logic was moved to a new database task (MatchRequestsTask). A new API call was added to the cache storage to allow querying request/response pairs. If a response is found to be empty it will be exposed as a nullptr. Change-Id: I631a3ef3da95117aed759a675fe591da5201eeca --- background-fetch/abort.https.window.js | 11 +++++++---- background-fetch/fetch.https.window.js | 12 ++++++++---- background-fetch/service_workers/sw.js | 13 ++++++------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/background-fetch/abort.https.window.js b/background-fetch/abort.https.window.js index db01bf94b85fe9a..f08b788dd58b132 100644 --- a/background-fetch/abort.https.window.js +++ b/background-fetch/abort.https.window.js @@ -45,11 +45,14 @@ backgroundFetchTest(async (test, backgroundFetch) => { assert_equals(type, 'backgroundfetchabort'); + assert_equals(results.length, 2); + + const completedResult = results[0] || results[1]; // The abort might have gone through before the first result was persisted. - if (results.length === 1) { - assert_true(results[0].url.includes('resources/feature-name.txt')); - assert_equals(results[0].status, 200); - assert_equals(results[0].text, expectedResultText); + if (completedResult) { + assert_true(completedResult.url.includes('resources/feature-name.txt')); + assert_equals(completedResult.status, 200); + assert_equals(completedResult.text, expectedResultText); } resolve(); diff --git a/background-fetch/fetch.https.window.js b/background-fetch/fetch.https.window.js index 9ed7e94da3bf7fc..c5fa18725a5dc37 100644 --- a/background-fetch/fetch.https.window.js +++ b/background-fetch/fetch.https.window.js @@ -265,8 +265,8 @@ backgroundFetchTest(async (test, backgroundFetch) => { backgroundFetchTest(async (test, backgroundFetch) => { const registration = await backgroundFetch.fetch( - 'my-id', - ['https://example.com', 'http://example.com']); + 'my-id', + [location.origin, location.origin.replace('https', 'http')]); const {type, eventRegistration, results} = await getMessageFromServiceWorker(); @@ -274,7 +274,11 @@ backgroundFetchTest(async (test, backgroundFetch) => { assert_equals(eventRegistration.failureReason, 'fetch-error'); assert_equals(results.length, 2); - assert_true(results[0].url.includes('https://example.com')); - assert_equals(results[1].url, ''); + + const validResponse = results[0] ? results[0] : results[1]; + const nullResponse = !results[0] ? results[0] : results[1]; + + assert_true(validResponse.url.includes(location.origin)); + assert_equals(nullResponse, null); }, 'Fetches with mixed content should fail.'); diff --git a/background-fetch/service_workers/sw.js b/background-fetch/service_workers/sw.js index 2e3fbfff1a83e2d..1c4fb10f05260f2 100644 --- a/background-fetch/service_workers/sw.js +++ b/background-fetch/service_workers/sw.js @@ -2,9 +2,8 @@ importScripts('sw-helpers.js'); async function getFetchResult(record) { - response = await record.responseReady; - if (!response) - return Promise.resolve(null); + const response = await record.responseReady.catch(() => null); + if (!response) return null; return { url: response.url, @@ -13,7 +12,7 @@ async function getFetchResult(record) { }; } -function handleBackgroundFetchUpdateEvent(event) { +function handleBackgroundFetchEvent(event) { event.waitUntil( event.registration.matchAll() .then(records => @@ -25,6 +24,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);