OLD | NEW |
1 importScripts('/resources/testharness-helpers.js'); | 1 importScripts('/resources/testharness-helpers.js'); |
2 | 2 |
| 3 // Copies the serializable attributes of |notification|. |
| 4 function cloneNotification(notification) { |
| 5 return JSON.parse(stringifyDOMObject(notification)); |
| 6 } |
| 7 |
3 // Allows a document to exercise the Notifications API within a service worker b
y sending commands. | 8 // Allows a document to exercise the Notifications API within a service worker b
y sending commands. |
4 var messagePort = null; | 9 var messagePort = null; |
5 | 10 |
6 addEventListener('message', function(workerEvent) { | 11 addEventListener('message', function(workerEvent) { |
7 messagePort = workerEvent.data; | 12 messagePort = workerEvent.data; |
8 | 13 |
9 // Listen to incoming commands on the message port. | 14 // Listen to incoming commands on the message port. |
10 messagePort.onmessage = function(event) { | 15 messagePort.onmessage = function(event) { |
11 if (typeof event.data != 'object' || !event.data.command) | 16 if (typeof event.data != 'object' || !event.data.command) |
12 return; | 17 return; |
13 | 18 |
14 switch (event.data.command) { | 19 switch (event.data.command) { |
15 case 'permission': | 20 case 'permission': |
16 messagePort.postMessage({ command: event.data.command, | 21 messagePort.postMessage({ command: event.data.command, |
17 value: Notification.permission }); | 22 value: Notification.permission }); |
18 break; | 23 break; |
19 | 24 |
20 case 'show': | 25 case 'show': |
21 registration.showNotification(event.data.title, event.data.optio
ns).then(function() { | 26 registration.showNotification(event.data.title, event.data.optio
ns).then(function() { |
22 messagePort.postMessage({ command: event.data.command, | 27 messagePort.postMessage({ command: event.data.command, |
23 success: true }); | 28 success: true }); |
24 }, function(error) { | 29 }, function(error) { |
25 messagePort.postMessage({ command: event.data.command, | 30 messagePort.postMessage({ command: event.data.command, |
26 success: false, | 31 success: false, |
27 message: error.message }); | 32 message: error.message }); |
28 }); | 33 }); |
29 break; | 34 break; |
30 | 35 |
| 36 case 'get': |
| 37 var filter = {}; |
| 38 if (typeof (event.data.filter) !== 'undefined') |
| 39 filter = event.data.filter; |
| 40 |
| 41 registration.getNotifications(filter).then(function(notification
s) { |
| 42 var clonedNotifications = []; |
| 43 for (var notification of notifications) |
| 44 clonedNotifications.push(cloneNotification(notification)
); |
| 45 |
| 46 messagePort.postMessage({ command: event.data.command, |
| 47 success: true, |
| 48 notifications: clonedNotifications
}); |
| 49 }, function(error) { |
| 50 messagePort.postMessage({ command: event.data.command, |
| 51 success: false, |
| 52 message: error.message }); |
| 53 }); |
| 54 break; |
| 55 |
31 default: | 56 default: |
32 messagePort.postMessage({ command: 'error', message: 'Invalid co
mmand: ' + event.data.command }); | 57 messagePort.postMessage({ command: 'error', message: 'Invalid co
mmand: ' + event.data.command }); |
33 break; | 58 break; |
34 } | 59 } |
35 }; | 60 }; |
36 | 61 |
37 // Notify the controller that the worker is now available. | 62 // Notify the controller that the worker is now available. |
38 messagePort.postMessage('ready'); | 63 messagePort.postMessage('ready'); |
39 }); | 64 }); |
40 | 65 |
41 addEventListener('notificationclick', function(event) { | 66 addEventListener('notificationclick', function(event) { |
42 // Copies the serializable attributes of the Notification instance on |event
|. | 67 var notificationCopy = cloneNotification(event.notification); |
43 var notificationCopy = JSON.parse(stringifyDOMObject(event.notification)); | |
44 | 68 |
45 // Notifications containing "ACTION:CLOSE" in their message will be closed | 69 // Notifications containing "ACTION:CLOSE" in their message will be closed |
46 // immediately by the Service Worker. | 70 // immediately by the Service Worker. |
47 if (event.notification.body.indexOf('ACTION:CLOSE') != -1) | 71 if (event.notification.body.indexOf('ACTION:CLOSE') != -1) |
48 event.notification.close(); | 72 event.notification.close(); |
49 | 73 |
50 // Notifications containing "ACTION:OPENWINDOW" in their message will attemp
t | 74 // Notifications containing "ACTION:OPENWINDOW" in their message will attemp
t |
51 // to open a new window for an example URL. | 75 // to open a new window for an example URL. |
52 if (event.notification.body.indexOf('ACTION:OPENWINDOW') != -1) | 76 if (event.notification.body.indexOf('ACTION:OPENWINDOW') != -1) |
53 event.waitUntil(clients.openWindow('https://example.com/')); | 77 event.waitUntil(clients.openWindow('https://example.com/')); |
54 | 78 |
55 messagePort.postMessage({ command: 'click', | 79 messagePort.postMessage({ command: 'click', |
56 notification: notificationCopy }); | 80 notification: notificationCopy }); |
57 }); | 81 }); |
OLD | NEW |