5 <title>Platform Notification Service BrowserTest service page
</title>
8 <!-- This page is intended to be used by the
9 PlatformNotificationServiceBrowserTest. -->
11 var messagePort
= null,
13 expectingMessage
= false;
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
);
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 // TODO(peter): Generalize this in some sort of Service Worker utility
27 // JavaScript file so that other tests can re-use the same logic.
28 function GetActivatedServiceWorker(script
, scope
) {
29 return navigator
.serviceWorker
.getRegistration(scope
)
30 .then(function (registration
) {
31 // Unregister any existing Service Worker.
33 return registration
.unregister();
35 // Register the Service Worker again.
36 return navigator
.serviceWorker
.register(script
, { scope
: scope
});
37 }).then(function (registration
) {
38 if (registration
.active
) {
40 } else if (registration
.waiting
|| registration
.installing
) {
41 var worker
= registration
.waiting
|| registration
.installing
;
42 return new Promise(function (resolve
) {
43 worker
.addEventListener('statechange', function () {
44 if (worker
.state
=== 'activated')
45 resolve(registration
);
49 return Promise
.reject('Service Worker in invalid state.');
51 }).then(function (registration
) {
52 return new Promise(function (resolve
) {
53 var channel
= new MessageChannel();
54 channel
.port1
.addEventListener('message', function (event
) {
55 if (event
.data
== 'ready')
56 resolve(registration
);
59 registration
.active
.postMessage(channel
.port2
,
62 messagePort
= channel
.port1
;
68 // Renews the registered Service Worker registration for this page, then
69 // displays a notification on the activated ServiceWorkerRegistration.
70 function DisplayPersistentNotification(title
) {
71 GetActivatedServiceWorker('platform_notification_service.js',
73 .then(function (registration
) {
74 return registration
.showNotification(title
, {
75 body
: 'Hello, world!',
79 messagePort
.addEventListener('message', function (event
) {
81 domAutomationController
.send(event
.data
);
83 messageStack
.push(event
.data
);
86 domAutomationController
.send('ok');
87 }).catch(function (error
) {
88 domAutomationController
.send('' + error
);
92 // Returns the latest received message from the worker. If no message has
93 // been received, nothing will be done. For successfully registered
94 // Service Workers this is OK, however, since the "message" event handler
95 // in DisplayPersistentNotification will take care of notifying the DOM
96 // Automation Controller instead.
97 function GetMessageFromWorker() {
98 if (!messageStack
.length
) {
99 expectingMessage
= true;
103 domAutomationController
.send('' + messageStack
.pop());