Skip to content

Commit

Permalink
[Background Fetch] Fix match() crash.
Browse files Browse the repository at this point in the history
Calling `match` was causing crashes in debug mode due to accessing
base::Optional<>::value without initialization. Calling match with
an unmatched request was crashing due to hitting a DCHECK. This returns
undefined now as per the spec.

Bug: 896768
Change-Id: I5d82e68ddca157a240ceaaec8990d2553366fbbb
Reviewed-on: https://chromium-review.googlesource.com/c/1289269
Commit-Queue: Rayan Kanso <[email protected]>
Reviewed-by: Peter Beverloo <[email protected]>
Cr-Commit-Position: refs/heads/master@{#601541}
  • Loading branch information
rayankans authored and chromium-wpt-export-bot committed Oct 22, 2018
1 parent 40e9266 commit f01a681
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
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

0 comments on commit f01a681

Please sign in to comment.