OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 var resultQueue = new ResultQueue(); | 7 var resultQueue = new ResultQueue(); |
8 | 8 |
9 var pushSubscriptionOptions = { | 9 var pushSubscriptionOptions = { |
10 userVisibleOnly: true | 10 userVisibleOnly: true |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 this.pendingGets++; | 52 this.pendingGets++; |
53 } | 53 } |
54 }; | 54 }; |
55 | 55 |
56 // Called by native. Immediately sends the next data item to the test if it is | 56 // Called by native. Immediately sends the next data item to the test if it is |
57 // available, otherwise sends null. | 57 // available, otherwise sends null. |
58 ResultQueue.prototype.popImmediately = function() { | 58 ResultQueue.prototype.popImmediately = function() { |
59 sendResultToTest(this.queue.length ? this.queue.pop() : null); | 59 sendResultToTest(this.queue.length ? this.queue.pop() : null); |
60 }; | 60 }; |
61 | 61 |
| 62 // Waits for the given ServiceWorkerRegistration to become ready. |
| 63 // Shim for https://github.com/w3c/ServiceWorker/issues/770. |
| 64 function swRegistrationReady(reg) { |
| 65 return new Promise((resolve, reject) => { |
| 66 if (reg.active) { |
| 67 resolve(); |
| 68 return; |
| 69 } |
| 70 |
| 71 if (!reg.installing && !reg.waiting) { |
| 72 reject(Error('Install failed')); |
| 73 return; |
| 74 } |
| 75 |
| 76 (reg.installing || reg.waiting).addEventListener('statechange', function() { |
| 77 if (this.state == 'redundant') { |
| 78 reject(Error('Install failed')); |
| 79 } else if (this.state == 'activated') { |
| 80 resolve(); |
| 81 } |
| 82 }); |
| 83 }); |
| 84 } |
| 85 |
62 // Notification permission has been coalesced with Push permission. After | 86 // Notification permission has been coalesced with Push permission. After |
63 // this is granted, Push API subscription can succeed. | 87 // this is granted, Push API subscription can succeed. |
64 function requestNotificationPermission() { | 88 function requestNotificationPermission() { |
65 Notification.requestPermission(function(permission) { | 89 Notification.requestPermission(function(permission) { |
66 sendResultToTest('permission status - ' + permission); | 90 sendResultToTest('permission status - ' + permission); |
67 }); | 91 }); |
68 } | 92 } |
69 | 93 |
70 function registerServiceWorker() { | 94 function registerServiceWorker() { |
71 // The base dir used to resolve service_worker.js and the scope depends on | 95 // The base dir used to resolve service_worker.js and the scope depends on |
72 // whether this script is included from an html file in ./, subscope1/, or | 96 // whether this script is included from an html file in ./, subscope1/, or |
73 // subscope2/. | 97 // subscope2/. |
74 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then( | 98 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then( |
75 function(swRegistration) { | 99 function(swRegistration) { |
76 sendResultToTest('ok - service worker registered'); | 100 sendResultToTest('ok - service worker registered'); |
77 }, sendErrorToTest); | 101 }, sendErrorToTest); |
78 } | 102 } |
79 | 103 |
80 function unregisterServiceWorker() { | 104 function unregisterServiceWorker() { |
81 navigator.serviceWorker.getRegistration().then(function(swRegistration) { | 105 navigator.serviceWorker.getRegistration().then(function(swRegistration) { |
82 swRegistration.unregister().then(function(result) { | 106 swRegistration.unregister().then(function(result) { |
83 sendResultToTest('service worker unregistration status: ' + result); | 107 sendResultToTest('service worker unregistration status: ' + result); |
84 }) | 108 }) |
85 }).catch(sendErrorToTest); | 109 }).catch(sendErrorToTest); |
86 } | 110 } |
87 | 111 |
| 112 function replaceServiceWorker() { |
| 113 navigator.serviceWorker.register('service_worker_with_skipWaiting_claim.js', { |
| 114 scope: './' |
| 115 }).then(swRegistrationReady).then(() => { |
| 116 sendResultToTest('ok - service worker replaced'); |
| 117 }).catch(sendErrorToTest); |
| 118 } |
| 119 |
88 function removeManifest() { | 120 function removeManifest() { |
89 var element = document.querySelector('link[rel="manifest"]'); | 121 var element = document.querySelector('link[rel="manifest"]'); |
90 if (element) { | 122 if (element) { |
91 element.parentNode.removeChild(element); | 123 element.parentNode.removeChild(element); |
92 sendResultToTest('manifest removed'); | 124 sendResultToTest('manifest removed'); |
93 } else { | 125 } else { |
94 sendResultToTest('unable to find manifest element'); | 126 sendResultToTest('unable to find manifest element'); |
95 } | 127 } |
96 } | 128 } |
97 | 129 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 swRegistration.pushManager.getSubscription().then(function(pushSubscription) | 220 swRegistration.pushManager.getSubscription().then(function(pushSubscription) |
189 { | 221 { |
190 if (!pushSubscription) { | 222 if (!pushSubscription) { |
191 sendResultToTest('unsubscribe result: false'); | 223 sendResultToTest('unsubscribe result: false'); |
192 return; | 224 return; |
193 } | 225 } |
194 pushSubscription.unsubscribe().then(function(result) { | 226 pushSubscription.unsubscribe().then(function(result) { |
195 sendResultToTest('unsubscribe result: ' + result); | 227 sendResultToTest('unsubscribe result: ' + result); |
196 }, function(error) { | 228 }, function(error) { |
197 sendResultToTest('unsubscribe error: ' + error.message); | 229 sendResultToTest('unsubscribe error: ' + error.message); |
198 }) | 230 }); |
199 }) | 231 }) |
200 }); | 232 }); |
201 } | 233 } |
202 | 234 |
| 235 function storePushSubscription() { |
| 236 navigator.serviceWorker.ready.then(swRegistration => { |
| 237 swRegistration.pushManager.getSubscription().then(pushSubscription => { |
| 238 window.storedPushSubscription = pushSubscription; |
| 239 sendResultToTest('ok - stored'); |
| 240 }, sendErrorToTest); |
| 241 }, sendErrorToTest); |
| 242 } |
| 243 |
| 244 function unsubscribeStoredPushSubscription() { |
| 245 window.storedPushSubscription.unsubscribe().then(function(result) { |
| 246 sendResultToTest('unsubscribe result: ' + result); |
| 247 }, function(error) { |
| 248 sendResultToTest('unsubscribe error: ' + error.message); |
| 249 }); |
| 250 } |
| 251 |
203 function hasSubscription() { | 252 function hasSubscription() { |
204 navigator.serviceWorker.ready.then(function(swRegistration) { | 253 navigator.serviceWorker.ready.then(function(swRegistration) { |
205 return swRegistration.pushManager.getSubscription(); | 254 return swRegistration.pushManager.getSubscription(); |
206 }).then(function(subscription) { | 255 }).then(function(subscription) { |
207 sendResultToTest(subscription ? 'true - subscribed' | 256 sendResultToTest(subscription ? 'true - subscribed' |
208 : 'false - not subscribed'); | 257 : 'false - not subscribed'); |
209 }).catch(sendErrorToTest); | 258 }).catch(sendErrorToTest); |
210 } | 259 } |
211 | 260 |
212 navigator.serviceWorker.addEventListener('message', function(event) { | 261 navigator.serviceWorker.addEventListener('message', function(event) { |
213 var message = JSON.parse(event.data); | 262 var message = JSON.parse(event.data); |
214 if (message.type == 'push') | 263 if (message.type == 'push') |
215 resultQueue.push(message.data); | 264 resultQueue.push(message.data); |
216 else | 265 else |
217 sendResultToTest(message.data); | 266 sendResultToTest(message.data); |
218 }, false); | 267 }, false); |
OLD | NEW |