aboutsummaryrefslogtreecommitdiffstats
path: root/tests/wpt/web-platform-tests/background-fetch/service_workers/sw-helpers.js
blob: e4c772135dba2610fc8a17119cf73b42aaba6746 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// The source to post setup and completion results to.
let source = null;

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;
  sendMessageToDocument('ready');
});