Do not announce robot account token before account ID is available
[chromium-blink-merge.git] / chrome / test / data / notifications / platform_notification_service.html
blob3fc27dc0c659d24c5ebcb40b343728e16cc8634e
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 messageStack = [],
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);
20 });
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.
32 if (registration)
33 return registration.unregister();
34 }).then(function () {
35 // Register the Service Worker again.
36 return navigator.serviceWorker.register(script, { scope: scope });
37 }).then(function (registration) {
38 if (registration.active) {
39 return registration;
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);
46 });
47 });
48 } else {
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);
57 });
59 registration.active.postMessage(channel.port2,
60 [ channel.port2 ]);
62 messagePort = channel.port1;
63 messagePort.start();
64 });
65 });
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',
72 location.pathname)
73 .then(function (registration) {
74 return registration.showNotification(title, {
75 body: 'Hello, world!',
76 icon: 'icon.png'
77 });
78 }).then(function () {
79 messagePort.addEventListener('message', function (event) {
80 if (expectingMessage)
81 domAutomationController.send(event.data);
82 else
83 messageStack.push(event.data);
84 });
86 domAutomationController.send('ok');
87 }).catch(function (error) {
88 domAutomationController.send('' + error);
89 });
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;
100 return;
103 domAutomationController.send('' + messageStack.pop());
105 </script>
106 </body>
107 </html>