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 permissionState() {
110 navigator
.serviceWorker
.ready
.then(function(swRegistration
) {
111 return swRegistration
.pushManager
.permissionState(pushSubscriptionOptions
)
112 .then(function(permission
) {
113 sendResultToTest('permission status - ' + permission
);
115 }).catch(sendErrorToTest
);
118 function isControlled() {
119 if (navigator
.serviceWorker
.controller
) {
120 sendResultToTest('true - is controlled');
122 sendResultToTest('false - is not controlled');
126 function unsubscribePush() {
127 if (!pushSubscription
) {
128 sendResultToTest('unsubscribe error: no subscription');
132 pushSubscription
.unsubscribe().then(function(result
) {
133 sendResultToTest('unsubscribe result: ' + result
);
135 sendResultToTest('unsubscribe error: ' + error
.name
+ ': ' + error
.message
);
139 function hasSubscription() {
140 navigator
.serviceWorker
.ready
.then(function(swRegistration
) {
141 return swRegistration
.pushManager
.getSubscription();
142 }).then(function(subscription
) {
143 sendResultToTest(subscription
? 'true - subscribed'
144 : 'false - not subscribed');
145 }).catch(sendErrorToTest
);
148 navigator
.serviceWorker
.addEventListener('message', function(event
) {
149 var message
= JSON
.parse(event
.data
);
150 if (message
.type
== 'push')
151 resultQueue
.push(message
.data
);