Skip to content

Commit

Permalink
[BackgroundFetch] Part 2: Implement BackgroundFetchFailureReason.
Browse files Browse the repository at this point in the history
This is the second part, the plumbing on the blink side.

Bug: 876691, 869918
Change-Id: I62fe4aaf37f41b5ebd341ce4731f7fda1d486feb
Reviewed-on: https://chromium-review.googlesource.com/1185015
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Dmitry Gozman <dgozman@chromium.org>
Reviewed-by: Peter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#585906}
  • Loading branch information
Mugdha Lakhani authored and chromium-wpt-export-bot committed Aug 24, 2018
1 parent 8dd18af commit e5fed3c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
8 changes: 7 additions & 1 deletion background-fetch/fetch.https.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@ backgroundFetchTest(async (test, backgroundFetch) => {
assert_equals(registration.uploadTotal, 0);
assert_equals(registration.uploaded, 0);
assert_equals(registration.downloadTotal, 0);
assert_equals(registration.state, "pending");
assert_equals(registration.failureReason, "");
// Skip `downloaded`, as the transfer may have started already.

const {type, results} = await getMessageFromServiceWorker();
const {type, eventRegistration, results} = await getMessageFromServiceWorker();
assert_equals('backgroundfetchsuccess', type);
assert_equals(results.length, 1);

assert_equals(eventRegistration.id, registration.id);
assert_equals(eventRegistration.state, "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');
Expand Down
2 changes: 2 additions & 0 deletions background-fetch/get.https.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ backgroundFetchTest(async (test, backgroundFetch) => {
assert_equals(registration.uploadTotal, 0);
assert_equals(registration.uploaded, 0);
assert_equals(registration.downloadTotal, 1234);
assert_equals(registration.state, "pending");
assert_equals(registration.failureReason, "");
// Skip `downloaded`, as the transfer may have started already.

const secondRegistration = await backgroundFetch.get(registrationId);
Expand Down
19 changes: 19 additions & 0 deletions background-fetch/service_workers/sw-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ function sendMessageToDocument(msg) {
source.postMessage(msg);
}

// This is needed to create a local javascript object identical to the
// one returned by a BackgroundFetchEvent, so that it can be serialized
// and transmitted from the service worker context to the document.
function cloneRegistration(registration) {
function deepCopy(src) {
if (typeof src !== 'object' || src === null)
return src;
var dst = Array.isArray(src) ? [] : {};
for (var property in src) {
if (typeof src[property] === 'function')
continue;
dst[property] = deepCopy(src[property]);
}
return dst;
}

return deepCopy(registration);
}

// Notify the document that the SW is registered and ready.
self.addEventListener('message', event => {
source = event.source;
Expand Down
5 changes: 4 additions & 1 deletion background-fetch/service_workers/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ self.addEventListener('backgroundfetchsuccess', event => {
event.waitUntil(
event.registration.matchAll()
.then(records => Promise.all(records.map(record => getFetchResult(record))))
.then(results => sendMessageToDocument({ type: event.type, results })));
.then(results => {
const registrationCopy = cloneRegistration(event.registration);
sendMessageToDocument({ type: event.type, eventRegistration: registrationCopy, results })
}));
});

0 comments on commit e5fed3c

Please sign in to comment.