Chromium Code Reviews| Index: chrome/test/data/notifications/platform_notification_service.html |
| diff --git a/chrome/test/data/notifications/platform_notification_service.html b/chrome/test/data/notifications/platform_notification_service.html |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4df3cba72b74b9b4783d333066b21c98eb362c6d |
| --- /dev/null |
| +++ b/chrome/test/data/notifications/platform_notification_service.html |
| @@ -0,0 +1,105 @@ |
| +<!doctype html> |
| +<html lang="en"> |
| + <head> |
| + <meta charset="utf-8"> |
| + <title>Platform Notification Service BrowserTest service page</title> |
| + </head> |
| + <body> |
| + <!-- This page is intended to be used by the |
| + PlatformNotificationServiceBrowserTest. --> |
| + <script> |
| + var messagePort = null, |
| + messageQueue = [], |
| + expectingMessage = false; |
| + |
| + // Requests permission to display Web Notifications. Will return the |
| + // permission level to the DOM Automation Controller. |
| + function RequestPermission() { |
| + Notification.requestPermission(function (level) { |
| + domAutomationController.send(level); |
| + }); |
| + } |
| + |
| + // Returns a promise that will be resolved with an activated Service |
| + // Worker, or rejects when the Service Worker could not be started. There |
| + // will be a message port to and from the worker in |messagePort|. |
| + 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.
|
| + return navigator.serviceWorker.getRegistration(scope) |
| + .then(function (registration) { |
| + // Unregister any existing Service Worker. |
| + if (registration) |
| + return registration.unregister(); |
| + }).then(function () { |
| + // Register the Service Worker again. |
| + return navigator.serviceWorker.register(script, { scope: scope }); |
| + }).then(function (registration) { |
| + if (registration.active) { |
| + 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.
|
| + } else if (registration.waiting || registration.installing) { |
| + var worker = registration.waiting || registration.installing; |
| + return new Promise(function (resolve) { |
| + worker.addEventListener('statechange', function () { |
| + if (worker.state === 'activated') |
| + resolve(registration); |
| + }); |
| + }); |
| + } else { |
| + return Promise.reject('Service Worker in invalid state.'); |
| + } |
| + }).then(function (registration) { |
| + return new Promise(function (resolve) { |
| + var channel = new MessageChannel(); |
| + channel.port1.addEventListener('message', function (event) { |
| + if (event.data == 'ready') |
| + resolve(registration); |
| + }); |
| + |
| + registration.active.postMessage(channel.port2, |
| + [ channel.port2 ]); |
| + |
| + messagePort = channel.port1; |
| + messagePort.start(); |
| + }); |
| + }); |
| + } |
| + |
| + // Renews the registered Service Worker registration for this page, then |
| + // displays a notification on the activated ServiceWorkerRegistration. |
| + function DisplayPersistentNotification(title) { |
| + 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
|
| + location.pathname) |
| + .then(function (registration) { |
| + return registration.showNotification(title, { |
| + body: 'Hello, world!', |
| + icon: 'icon.png' |
| + }); |
| + }).then(function () { |
| + messagePort.addEventListener('message', function (event) { |
| + if (expectingMessage) |
| + domAutomationController.send(event.data); |
| + else |
| + messageQueue.push(event.data); |
| + }); |
| + |
| + domAutomationController.send('ok'); |
| + }).catch(function (error) { |
| + domAutomationController.send('' + error); |
| + }); |
| + } |
| + |
| + // Returns the latest received message from the worker. If no message has |
| + // been received, nothing will be done. For successfully registered |
| + // Service Workers this is OK, however, since the "message" event handler |
| + // in DisplayPersistentNotification will take care of notifying the DOM |
| + // Automation Controller instead. |
| + function GetMessageFromWorker() { |
| + if (!messageQueue.length) { |
| + expectingMessage = true; |
| + return; |
| + } |
| + |
| + 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.
|
| + } |
| + </script> |
| + </body> |
| +</html> |