1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 var resultQueue = new ResultQueue();
8 var pushRegistration = null;
10 // Sends data back to the test. This must be in response to an earlier
11 // request, but it's ok to respond asynchronously. The request blocks until
12 // the response is sent.
13 function sendResultToTest(result) {
14 console.log('sendResultToTest: ' + result);
15 if (window.domAutomationController) {
16 domAutomationController.send('' + result);
20 function sendErrorToTest(error) {
21 sendResultToTest(error.name + ' - ' + error.message);
24 // Queue storing asynchronous results received from the Service Worker. Results
25 // are sent to the test when requested.
26 function ResultQueue() {
27 // Invariant: this.queue.length == 0 || this.pendingGets == 0
32 // Adds a data item to the queue. Will be sent to the test if there are
34 ResultQueue.prototype.push = function(data) {
35 if (this.pendingGets > 0) {
37 sendResultToTest(data);
39 this.queue.unshift(data);
43 // Called by native. Sends the next data item to the test if it is available.
44 // Otherwise increments pendingGets so it will be delivered when received.
45 ResultQueue.prototype.pop = function() {
46 if (this.queue.length) {
47 sendResultToTest(this.queue.pop());
53 // Called by native. Immediately sends the next data item to the test if it is
54 // available, otherwise sends null.
55 ResultQueue.prototype.popImmediately = function() {
56 sendResultToTest(this.queue.length ? this.queue.pop() : null);
59 // Notification permission has been coalesced with Push permission. After
60 // this is granted, Push API registration can succeed.
61 function requestNotificationPermission() {
62 Notification.requestPermission(function(permission) {
63 sendResultToTest('permission status - ' + permission);
67 function registerServiceWorker() {
68 // The base dir used to resolve service_worker.js and the scope depends on
69 // whether this script is included from an html file in ./, subscope1/, or
71 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then(
72 function(swRegistration) {
73 sendResultToTest('ok - service worker registered');
77 function unregisterServiceWorker() {
78 navigator.serviceWorker.getRegistration().then(function(swRegistration) {
79 swRegistration.unregister().then(function(result) {
80 sendResultToTest('service worker unregistration status: ' + result);
82 }).catch(sendErrorToTest);
85 function removeManifest() {
86 var element = document.querySelector('link[rel="manifest"]');
88 element.parentNode.removeChild(element);
89 sendResultToTest('manifest removed');
91 sendResultToTest('unable to find manifest element');
95 function registerPush() {
96 navigator.serviceWorker.ready.then(function(swRegistration) {
97 var registerMethodName =
98 swRegistration.pushManager.register ? 'register' : 'subscribe';
99 return swRegistration.pushManager[registerMethodName]()
100 .then(function(registration) {
101 pushRegistration = registration;
102 sendResultToTest(registration.endpoint + ' - ' +
103 (registration.registrationId || registration.subscriptionId));
105 }).catch(sendErrorToTest);
108 function hasPermission() {
109 navigator.serviceWorker.ready.then(function(swRegistration) {
110 return swRegistration.pushManager.hasPermission()
111 .then(function(permission) {
112 sendResultToTest('permission status - ' + permission);
114 }).catch(sendErrorToTest);
117 function isControlled() {
118 if (navigator.serviceWorker.controller) {
119 sendResultToTest('true - is controlled');
121 sendResultToTest('false - is not controlled');
125 function unregister() {
126 if (!pushRegistration) {
127 sendResultToTest('unregister error: no registration');
131 var unregisterMethodName =
132 pushRegistration.unregister ? 'unregister' : 'unsubscribe';
133 pushRegistration[unregisterMethodName]().then(function(result) {
134 sendResultToTest('unregister result: ' + result);
136 sendResultToTest('unregister error: ' + error.name + ': ' + error.message);
140 addEventListener('message', function(event) {
141 var message = JSON.parse(event.data);
142 if (message.type == 'push')
143 resultQueue.push(message.data);