Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!doctype html> | |
| 2 <html lang="en"> | |
| 3 <head> | |
| 4 <meta charset="utf-8"> | |
| 5 <title>Platform Notification Service BrowserTest service page</title> | |
| 6 </head> | |
| 7 <body> | |
| 8 <!-- This page is intended to be used by the | |
| 9 PlatformNotificationServiceBrowserTest. --> | |
| 10 <script> | |
| 11 var messagePort = null, | |
| 12 messageQueue = [], | |
| 13 expectingMessage = false; | |
| 14 | |
| 15 // Requests permission to display Web Notifications. Will return the | |
| 16 // permission level to the DOM Automation Controller. | |
| 17 function RequestPermission() { | |
| 18 Notification.requestPermission(function (level) { | |
| 19 domAutomationController.send(level); | |
| 20 }); | |
| 21 } | |
| 22 | |
| 23 // Returns a promise that will be resolved with an activated Service | |
| 24 // Worker, or rejects when the Service Worker could not be started. There | |
| 25 // will be a message port to and from the worker in |messagePort|. | |
| 26 function GetActivatedServiceWorker(script, scope) { | |
|
Michael van Ouwerkerk
2014/12/11 16:41:55
This is nice, it would be useful to generalize as
Peter Beverloo
2014/12/11 19:02:00
Added a TODO.
| |
| 27 return navigator.serviceWorker.getRegistration(scope) | |
| 28 .then(function (registration) { | |
| 29 // Unregister any existing Service Worker. | |
| 30 if (registration) | |
| 31 return registration.unregister(); | |
| 32 }).then(function () { | |
| 33 // Register the Service Worker again. | |
| 34 return navigator.serviceWorker.register(script, { scope: scope }); | |
| 35 }).then(function (registration) { | |
| 36 if (registration.active) { | |
| 37 return Promise.resolve(registration); | |
|
dewittj
2014/12/11 17:18:50
If I recall correctly you can just return registra
Peter Beverloo
2014/12/11 19:02:00
Done.
| |
| 38 } else if (registration.waiting || registration.installing) { | |
| 39 var worker = registration.waiting || registration.installing; | |
| 40 return new Promise(function (resolve) { | |
| 41 worker.addEventListener('statechange', function () { | |
| 42 if (worker.state === 'activated') | |
| 43 resolve(registration); | |
| 44 }); | |
| 45 }); | |
| 46 } else { | |
| 47 return Promise.reject('Service Worker in invalid state.'); | |
| 48 } | |
| 49 }).then(function (registration) { | |
| 50 return new Promise(function (resolve) { | |
| 51 var channel = new MessageChannel(); | |
| 52 channel.port1.addEventListener('message', function (event) { | |
| 53 if (event.data == 'ready') | |
| 54 resolve(registration); | |
| 55 }); | |
| 56 | |
| 57 registration.active.postMessage(channel.port2, | |
| 58 [ channel.port2 ]); | |
| 59 | |
| 60 messagePort = channel.port1; | |
| 61 messagePort.start(); | |
| 62 }); | |
| 63 }); | |
| 64 } | |
| 65 | |
| 66 // Renews the registered Service Worker registration for this page, then | |
| 67 // displays a notification on the activated ServiceWorkerRegistration. | |
| 68 function DisplayPersistentNotification(title) { | |
| 69 GetActivatedServiceWorker('platform_notification_service.js', | |
|
Michael van Ouwerkerk
2014/12/11 16:41:55
nit: it would be clearer if this js file name had
Peter Beverloo
2014/12/11 19:02:00
Ack. I don't have a strong preference of one over
Michael van Ouwerkerk
2014/12/11 20:01:19
As I said, it's a readability nit. Don't do it if
| |
| 70 location.pathname) | |
| 71 .then(function (registration) { | |
| 72 return registration.showNotification(title, { | |
| 73 body: 'Hello, world!', | |
| 74 icon: 'icon.png' | |
| 75 }); | |
| 76 }).then(function () { | |
| 77 messagePort.addEventListener('message', function (event) { | |
| 78 if (expectingMessage) | |
| 79 domAutomationController.send(event.data); | |
| 80 else | |
| 81 messageQueue.push(event.data); | |
| 82 }); | |
| 83 | |
| 84 domAutomationController.send('ok'); | |
| 85 }).catch(function (error) { | |
| 86 domAutomationController.send('' + error); | |
| 87 }); | |
| 88 } | |
| 89 | |
| 90 // Returns the latest received message from the worker. If no message has | |
| 91 // been received, nothing will be done. For successfully registered | |
| 92 // Service Workers this is OK, however, since the "message" event handler | |
| 93 // in DisplayPersistentNotification will take care of notifying the DOM | |
| 94 // Automation Controller instead. | |
| 95 function GetMessageFromWorker() { | |
| 96 if (!messageQueue.length) { | |
| 97 expectingMessage = true; | |
| 98 return; | |
| 99 } | |
| 100 | |
| 101 domAutomationController.send('' + messageQueue.pop()); | |
|
Michael van Ouwerkerk
2014/12/11 16:41:55
Any particular reason why this is acting like a st
Peter Beverloo
2014/12/11 19:02:00
Done.
| |
| 102 } | |
| 103 </script> | |
| 104 </body> | |
| 105 </html> | |
| OLD | NEW |