| 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, | 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. | 6 // which would affect the scope of the importScripts call here. |
| 7 self.importScripts('/push_messaging/push_constants.js'); | 7 self.importScripts('/push_messaging/push_constants.js'); |
| 8 | 8 |
| 9 // Don't wait for clients of old SW to close before activating. | 9 // Don't wait for clients of old SW to close before activating. |
| 10 self.addEventListener('install', () => skipWaiting()); | 10 self.addEventListener('install', () => skipWaiting()); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 self.addEventListener('message', function handler (event) { | 48 self.addEventListener('message', function handler (event) { |
| 49 let pushSubscriptionOptions = { | 49 let pushSubscriptionOptions = { |
| 50 userVisibleOnly: true | 50 userVisibleOnly: true |
| 51 }; | 51 }; |
| 52 if (event.data.command == 'workerSubscribe') { | 52 if (event.data.command == 'workerSubscribe') { |
| 53 pushSubscriptionOptions.applicationServerKey = kApplicationServerKey.buffer; | 53 pushSubscriptionOptions.applicationServerKey = kApplicationServerKey.buffer; |
| 54 } else if (event.data.command == 'workerSubscribeWithNumericKey') { | 54 } else if (event.data.command == 'workerSubscribeWithNumericKey') { |
| 55 pushSubscriptionOptions.applicationServerKey = | 55 pushSubscriptionOptions.applicationServerKey = |
| 56 new TextEncoder().encode('1234567890'); | 56 new TextEncoder().encode(event.data.key); |
| 57 } else if (event.data.command == 'workerSubscribeNoKey') { | 57 } else if (event.data.command == 'workerSubscribeNoKey') { |
| 58 // Nothing to set up | 58 // Nothing to set up |
| 59 } else { | 59 } else { |
| 60 sendMessageToClients('message', 'error - unknown message request'); | 60 sendMessageToClients('message', 'error - unknown message request'); |
| 61 return; | 61 return; |
| 62 } | 62 } |
| 63 | 63 |
| 64 self.registration.pushManager.subscribe(pushSubscriptionOptions) | 64 self.registration.pushManager.subscribe(pushSubscriptionOptions) |
| 65 .then(function(subscription) { | 65 .then(function(subscription) { |
| 66 sendMessageToClients('message', subscription.endpoint); | 66 sendMessageToClients('message', subscription.endpoint); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 79 'data': data | 79 'data': data |
| 80 }); | 80 }); |
| 81 clients.matchAll().then(function(clients) { | 81 clients.matchAll().then(function(clients) { |
| 82 clients.forEach(function(client) { | 82 clients.forEach(function(client) { |
| 83 client.postMessage(message); | 83 client.postMessage(message); |
| 84 }); | 84 }); |
| 85 }, function(error) { | 85 }, function(error) { |
| 86 console.log(error); | 86 console.log(error); |
| 87 }); | 87 }); |
| 88 } | 88 } |
| OLD | NEW |