Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / data / push_messaging / service_worker.js
blobe884e899b39348d4e48e9446bfe1e55ebf6c5e63
1 // Copyright 2014 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 "onpush" event currently understands two values as message payload
6 // data coming from the test. Any other input is passed through to the
7 // document unchanged.
8 //
9 // "shownotification" - Display a Web Notification with event.waitUntil().
10 // "shownotification-without-waituntil"
11 // - Display a Web Notification without using event.waitUntil().
12 this.onpush = function(event) {
13 var data = event.data.text();
14 if (!data.startsWith('shownotification')) {
15 sendMessageToClients('push', data);
16 return;
19 var result = registration.showNotification('Push test title', {
20 body: 'Push test body',
21 tag: 'push_test_tag'
22 });
24 if (data == 'shownotification-without-waituntil') {
25 sendMessageToClients('push', 'immediate:' + data);
26 return;
29 event.waitUntil(result.then(function() {
30 sendMessageToClients('push', data);
31 }, function(ex) {
32 sendMessageToClients('push', String(ex));
33 }));
36 function sendMessageToClients(type, data) {
37 var message = JSON.stringify({
38 'type': type,
39 'data': data
40 });
41 clients.matchAll().then(function(clients) {
42 clients.forEach(function(client) {
43 client.postMessage(message);
44 });
45 }, function(error) {
46 console.log(error);
47 });