1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // The "onsync" event currently understands commands (passed as
6 // registration tags) coming from the test. Any other tag is
7 // passed through to the document unchanged.
9 // "delay" - Delays finishing the sync event with event.waitUntil.
10 // Send a postMessage of "completeDelayedOneShot" to finish the
15 var resolveCallback = null;
16 var rejectCallback = null;
18 this.onmessage = function(event) {
19 if (event.data === 'completeDelayedOneShot') {
20 if (resolveCallback === null) {
21 sendMessageToClients('sync', 'error - resolveCallback is null');
26 sendMessageToClients('sync', 'ok - delay completed');
30 if (event.data === 'rejectDelayedOneShot') {
31 if (rejectCallback === null) {
32 sendMessageToClients('sync', 'error - rejectCallback is null');
37 sendMessageToClients('sync', 'ok - delay rejected');
41 this.onsync = function(event) {
42 if (event.registration === undefined) {
43 sendMessageToClients('sync', 'error - event missing registration');
47 if (event.registration.tag === undefined) {
48 sendMessageToClients('sync', 'error - registration missing tag');
52 var tag = event.registration.tag;
54 if (tag === 'delay') {
55 var syncPromise = new Promise(function(resolve, reject) {
56 resolveCallback = resolve;
57 rejectCallback = reject;
59 event.waitUntil(syncPromise);
63 sendMessageToClients('sync', tag + ' fired');
66 function sendMessageToClients(type, data) {
67 clients.matchAll().then(function(clients) {
68 clients.forEach(function(client) {
69 client.postMessage({type, data});