Skip to content

Commit

Permalink
[Background Fetch] Simplify fetch storage workflow.
Browse files Browse the repository at this point in the history
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
Reviewed-on: https://chromium-review.googlesource.com/c/1280851
Reviewed-by: Peter Beverloo <[email protected]>
Reviewed-by: Steven Holte <[email protected]>
Reviewed-by: Joshua Bell <[email protected]>
Reviewed-by: Ben Kelly <[email protected]>
Reviewed-by: Mugdha Lakhani <[email protected]>
Commit-Queue: Rayan Kanso <[email protected]>
Cr-Commit-Position: refs/heads/master@{#600768}
  • Loading branch information
rayankans authored and chromium-wpt-export-bot committed Oct 18, 2018
1 parent 8ca1dc4 commit 27ebdcb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
11 changes: 7 additions & 4 deletions background-fetch/abort.https.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 8 additions & 4 deletions background-fetch/fetch.https.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,20 @@ 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();

assert_equals('backgroundfetchfail', type);
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.');
13 changes: 6 additions & 7 deletions background-fetch/service_workers/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -13,7 +12,7 @@ async function getFetchResult(record) {
};
}

function handleBackgroundFetchUpdateEvent(event) {
function handleBackgroundFetchEvent(event) {
event.waitUntil(
event.registration.matchAll()
.then(records =>
Expand All @@ -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);

0 comments on commit 27ebdcb

Please sign in to comment.