Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Background Fetch] Fix match() crash. #13603

Merged
merged 1 commit into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions background-fetch/fetch.https.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
25 changes: 24 additions & 1 deletion background-fetch/service_workers/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down