Chromium Code Reviews| 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 // NIST P-256 public key made available to tests. Must be an uncompressed | |
| 6 // point in accordance with SEC1 2.3.3 | |
| 7 var applicationServerKey = new Uint8Array([ | |
|
Michael van Ouwerkerk
2016/03/22 11:31:37
To avoid duplication of this inscrutable thing you
harkness
2016/03/24 16:56:23
Done.
| |
| 8 0x04, 0x55, 0x52, 0x6A, 0xA5, 0x6E, 0x8E, 0xAA, 0x47, 0x97, 0x36, 0x10, 0xC1, | |
| 9 0x66, 0x3C, 0x1E, 0x65, 0xBF, 0xA1, 0x7B, 0xEE, 0x48, 0xC9, 0xC6, 0xBB, 0xBF, | |
| 10 0x02, 0x18, 0x53, 0x72, 0x1D, 0x0C, 0x7B, 0xA9, 0xE3, 0x11, 0xB7, 0x03, 0x52, | |
| 11 0x21, 0xD3, 0x71, 0x90, 0x13, 0xA8, 0xC1, 0xCF, 0xED, 0x20, 0xF7, 0x1F, 0xD1, | |
| 12 0x7F, 0xF2, 0x76, 0xB6, 0x01, 0x20, 0xD8, 0x35, 0xA5, 0xD9, 0x3C, 0x43, 0xFD | |
| 13 ]); | |
| 14 var pushSubscriptionOptions = { | |
| 15 userVisibleOnly: true | |
| 16 }; | |
| 17 | |
| 5 // The "onpush" event currently understands two values as message payload | 18 // The "onpush" event currently understands two values as message payload |
| 6 // data coming from the test. Any other input is passed through to the | 19 // data coming from the test. Any other input is passed through to the |
| 7 // document unchanged. | 20 // document unchanged. |
| 8 // | 21 // |
| 9 // "shownotification" - Display a Web Notification with event.waitUntil(). | 22 // "shownotification" - Display a Web Notification with event.waitUntil(). |
| 10 // "shownotification-without-waituntil" | 23 // "shownotification-without-waituntil" |
| 11 // - Display a Web Notification without using event.waitUntil(). | 24 // - Display a Web Notification without using event.waitUntil(). |
| 12 this.onpush = function(event) { | 25 this.onpush = function(event) { |
| 13 if (event.data === null) { | 26 if (event.data === null) { |
| 14 sendMessageToClients('push', '[NULL]'); | 27 sendMessageToClients('push', '[NULL]'); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 31 return; | 44 return; |
| 32 } | 45 } |
| 33 | 46 |
| 34 event.waitUntil(result.then(function() { | 47 event.waitUntil(result.then(function() { |
| 35 sendMessageToClients('push', data); | 48 sendMessageToClients('push', data); |
| 36 }, function(ex) { | 49 }, function(ex) { |
| 37 sendMessageToClients('push', String(ex)); | 50 sendMessageToClients('push', String(ex)); |
| 38 })); | 51 })); |
| 39 }; | 52 }; |
| 40 | 53 |
| 54 self.addEventListener('message', function handler (event) { | |
| 55 if (event.data.command == 'workerSubscribe') { | |
| 56 pushSubscriptionOptions.applicationServerKey = applicationServerKey.buffer; | |
| 57 } else if (event.data.command == 'workerSubscribePermissionFail') { | |
| 58 pushSubscriptionOptions.applicationServerKey = applicationServerKey.buffer; | |
| 59 pushSubscriptionOptions.userVisibleOnly = false; | |
| 60 } else if (event.data.command == 'workerSubscribeNoKey') { | |
| 61 // Nothing to set up | |
| 62 } else { | |
| 63 sendMessageToClients('message', 'error - unknown message request'); | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 self.registration.pushManager.subscribe(pushSubscriptionOptions) | |
| 68 .then(function(subscription) { | |
| 69 sendMessageToClients('message', subscription.endpoint); | |
| 70 }, function(error) { | |
| 71 sendErrorToClients(error); | |
| 72 }); | |
| 73 }); | |
| 74 | |
| 75 function sendErrorToClients(error) { | |
| 76 sendMessageToClients('error', error.name + ' - ' + error.message); | |
| 77 } | |
| 78 | |
| 41 function sendMessageToClients(type, data) { | 79 function sendMessageToClients(type, data) { |
| 42 var message = JSON.stringify({ | 80 var message = JSON.stringify({ |
| 43 'type': type, | 81 'type': type, |
| 44 'data': data | 82 'data': data |
| 45 }); | 83 }); |
| 46 clients.matchAll().then(function(clients) { | 84 clients.matchAll().then(function(clients) { |
| 47 clients.forEach(function(client) { | 85 clients.forEach(function(client) { |
| 48 client.postMessage(message); | 86 client.postMessage(message); |
| 49 }); | 87 }); |
| 50 }, function(error) { | 88 }, function(error) { |
| 51 console.log(error); | 89 console.log(error); |
| 52 }); | 90 }); |
| 53 } | 91 } |
| OLD | NEW |