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 pushSubscription = null;
10 var pushSubscriptionOptions = {
14 // Sends data back to the test. This must be in response to an earlier
15 // request, but it's ok to respond asynchronously. The request blocks until
16 // the response is sent.
17 function sendResultToTest(result) {
18 console.log('sendResultToTest: ' + result);
19 if (window.domAutomationController) {
20 domAutomationController.send('' + result);
24 function sendErrorToTest(error) {
25 sendResultToTest(error.name + ' - ' + error.message);
28 // Queue storing asynchronous results received from the Service Worker. Results
29 // are sent to the test when requested.
30 function ResultQueue() {
31 // Invariant: this.queue.length == 0 || this.pendingGets == 0
36 // Adds a data item to the queue. Will be sent to the test if there are
38 ResultQueue.prototype.push = function(data) {
39 if (this.pendingGets > 0) {
41 sendResultToTest(data);
43 this.queue.unshift(data);
47 // Called by native. Sends the next data item to the test if it is available.
48 // Otherwise increments pendingGets so it will be delivered when received.
49 ResultQueue.prototype.pop = function() {
50 if (this.queue.length) {
51 sendResultToTest(this.queue.pop());
57 // Called by native. Immediately sends the next data item to the test if it is
58 // available, otherwise sends null.
59 ResultQueue.prototype.popImmediately = function() {
60 sendResultToTest(this.queue.length ? this.queue.pop() : null);
63 // Notification permission has been coalesced with Push permission. After
64 // this is granted, Push API subscription can succeed.
65 function requestNotificationPermission() {
66 Notification.requestPermission(function(permission) {
67 sendResultToTest('permission status - ' + permission);
71 function registerServiceWorker() {
72 // The base dir used to resolve service_worker.js and the scope depends on
73 // whether this script is included from an html file in ./, subscope1/, or
75 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then(
76 function(swRegistration) {
77 sendResultToTest('ok - service worker registered');
81 function unregisterServiceWorker() {
82 navigator.serviceWorker.getRegistration().then(function(swRegistration) {
83 swRegistration.unregister().then(function(result) {
84 sendResultToTest('service worker unregistration status: ' + result);
86 }).catch(sendErrorToTest);
89 function removeManifest() {
90 var element = document.querySelector('link[rel="manifest"]');
92 element.parentNode.removeChild(element);
93 sendResultToTest('manifest removed');
95 sendResultToTest('unable to find manifest element');
99 function subscribePush() {
100 navigator.serviceWorker.ready.then(function(swRegistration) {
101 return swRegistration.pushManager.subscribe(pushSubscriptionOptions)
102 .then(function(subscription) {
103 pushSubscription = subscription;
104 sendResultToTest(subscription.endpoint);
106 }).catch(sendErrorToTest);
109 function getCurve25519dh() {
110 navigator.serviceWorker.ready.then(function(swRegistration) {
111 return swRegistration.pushManager.getSubscription()
112 .then(function(subscription) {
113 sendResultToTest(btoa(String.fromCharCode.apply(null,
114 new Uint8Array(subscription.getKey('curve25519dh')))));
116 }).catch(sendErrorToTest);
119 function permissionState() {
120 navigator.serviceWorker.ready.then(function(swRegistration) {
121 return swRegistration.pushManager.permissionState(pushSubscriptionOptions)
122 .then(function(permission) {
123 sendResultToTest('permission status - ' + permission);
125 }).catch(sendErrorToTest);
128 function isControlled() {
129 if (navigator.serviceWorker.controller) {
130 sendResultToTest('true - is controlled');
132 sendResultToTest('false - is not controlled');
136 function unsubscribePush() {
137 if (!pushSubscription) {
138 sendResultToTest('unsubscribe error: no subscription');
142 pushSubscription.unsubscribe().then(function(result) {
143 sendResultToTest('unsubscribe result: ' + result);
145 sendResultToTest('unsubscribe error: ' + error.name + ': ' + error.message);
149 function hasSubscription() {
150 navigator.serviceWorker.ready.then(function(swRegistration) {
151 return swRegistration.pushManager.getSubscription();
152 }).then(function(subscription) {
153 sendResultToTest(subscription ? 'true - subscribed'
154 : 'false - not subscribed');
155 }).catch(sendErrorToTest);
158 navigator.serviceWorker.addEventListener('message', function(event) {
159 var message = JSON.parse(event.data);
160 if (message.type == 'push')
161 resultQueue.push(message.data);