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 // 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 subscription 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 subscribePush() {
96 navigator
.serviceWorker
.ready
.then(function(swRegistration
) {
97 return swRegistration
.pushManager
.subscribe()
98 .then(function(subscription
) {
99 pushSubscription
= subscription
;
101 subscription
.endpoint
+ ' - ' + subscription
.subscriptionId
);
103 }).catch(sendErrorToTest
);
106 function permissionState() {
107 navigator
.serviceWorker
.ready
.then(function(swRegistration
) {
108 return swRegistration
.pushManager
.permissionState()
109 .then(function(permission
) {
110 sendResultToTest('permission status - ' + permission
);
112 }).catch(sendErrorToTest
);
115 function isControlled() {
116 if (navigator
.serviceWorker
.controller
) {
117 sendResultToTest('true - is controlled');
119 sendResultToTest('false - is not controlled');
123 function unsubscribePush() {
124 if (!pushSubscription
) {
125 sendResultToTest('unsubscribe error: no subscription');
129 pushSubscription
.unsubscribe().then(function(result
) {
130 sendResultToTest('unsubscribe result: ' + result
);
132 sendResultToTest('unsubscribe error: ' + error
.name
+ ': ' + error
.message
);
136 function hasSubscription() {
137 navigator
.serviceWorker
.ready
.then(function(swRegistration
) {
138 return swRegistration
.pushManager
.getSubscription();
139 }).then(function(subscription
) {
140 sendResultToTest(subscription
? 'true - subscribed'
141 : 'false - not subscribed');
142 }).catch(sendErrorToTest
);
145 addEventListener('message', function(event
) {
146 var message
= JSON
.parse(event
.data
);
147 if (message
.type
== 'push')
148 resultQueue
.push(message
.data
);