| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Use an absolute path since this could be loaded from a different scope, |
| 6 // which would affect the scope of the importScripts call here. |
| 7 self.importScripts('/push_messaging/push_constants.js'); |
| 8 |
| 9 var pushSubscriptionOptions = { |
| 10 userVisibleOnly: true |
| 11 }; |
| 12 |
| 5 // The "onpush" event currently understands two values as message payload | 13 // The "onpush" event currently understands two values as message payload |
| 6 // data coming from the test. Any other input is passed through to the | 14 // data coming from the test. Any other input is passed through to the |
| 7 // document unchanged. | 15 // document unchanged. |
| 8 // | 16 // |
| 9 // "shownotification" - Display a Web Notification with event.waitUntil(). | 17 // "shownotification" - Display a Web Notification with event.waitUntil(). |
| 10 // "shownotification-without-waituntil" | 18 // "shownotification-without-waituntil" |
| 11 // - Display a Web Notification without using event.waitUntil(). | 19 // - Display a Web Notification without using event.waitUntil(). |
| 12 this.onpush = function(event) { | 20 this.onpush = function(event) { |
| 13 if (event.data === null) { | 21 if (event.data === null) { |
| 14 sendMessageToClients('push', '[NULL]'); | 22 sendMessageToClients('push', '[NULL]'); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 31 return; | 39 return; |
| 32 } | 40 } |
| 33 | 41 |
| 34 event.waitUntil(result.then(function() { | 42 event.waitUntil(result.then(function() { |
| 35 sendMessageToClients('push', data); | 43 sendMessageToClients('push', data); |
| 36 }, function(ex) { | 44 }, function(ex) { |
| 37 sendMessageToClients('push', String(ex)); | 45 sendMessageToClients('push', String(ex)); |
| 38 })); | 46 })); |
| 39 }; | 47 }; |
| 40 | 48 |
| 49 self.addEventListener('message', function handler (event) { |
| 50 if (event.data.command == 'workerSubscribe') { |
| 51 pushSubscriptionOptions.applicationServerKey = kApplicationServerKey.buffer; |
| 52 } else if (event.data.command == 'workerSubscribeNoKey') { |
| 53 // Nothing to set up |
| 54 } else { |
| 55 sendMessageToClients('message', 'error - unknown message request'); |
| 56 return; |
| 57 } |
| 58 |
| 59 self.registration.pushManager.subscribe(pushSubscriptionOptions) |
| 60 .then(function(subscription) { |
| 61 sendMessageToClients('message', subscription.endpoint); |
| 62 }, function(error) { |
| 63 sendErrorToClients(error); |
| 64 }); |
| 65 }); |
| 66 |
| 67 function sendErrorToClients(error) { |
| 68 sendMessageToClients('error', error.name + ' - ' + error.message); |
| 69 } |
| 70 |
| 41 function sendMessageToClients(type, data) { | 71 function sendMessageToClients(type, data) { |
| 42 var message = JSON.stringify({ | 72 var message = JSON.stringify({ |
| 43 'type': type, | 73 'type': type, |
| 44 'data': data | 74 'data': data |
| 45 }); | 75 }); |
| 46 clients.matchAll().then(function(clients) { | 76 clients.matchAll().then(function(clients) { |
| 47 clients.forEach(function(client) { | 77 clients.forEach(function(client) { |
| 48 client.postMessage(message); | 78 client.postMessage(message); |
| 49 }); | 79 }); |
| 50 }, function(error) { | 80 }, function(error) { |
| 51 console.log(error); | 81 console.log(error); |
| 52 }); | 82 }); |
| 53 } | 83 } |
| OLD | NEW |