Chromium Code Reviews| 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 function swRegistrationReady(reg) { | |
|
Peter Beverloo
2016/09/30 14:26:07
nit: this definitely deserves some documentation :
johnme
2016/09/30 17:02:10
Done.
| |
| 63 return new Promise((resolve, reject) => { | |
| 64 if (reg.active) { | |
| 65 resolve(); | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 if (!reg.installing && !reg.waiting) { | |
| 70 reject(Error('Install failed')); | |
| 71 return; | |
| 72 } | |
| 73 | |
| 74 (reg.installing || reg.waiting).addEventListener('statechange', function() { | |
| 75 if (this.state == 'redundant') { | |
| 76 reject(Error('Install failed')); | |
| 77 } else if (this.state == 'activated') { | |
| 78 resolve(); | |
| 79 } | |
| 80 }); | |
| 81 }); | |
| 82 } | |
| 83 | |
| 62 // Notification permission has been coalesced with Push permission. After | 84 // Notification permission has been coalesced with Push permission. After |
| 63 // this is granted, Push API subscription can succeed. | 85 // this is granted, Push API subscription can succeed. |
| 64 function requestNotificationPermission() { | 86 function requestNotificationPermission() { |
| 65 Notification.requestPermission(function(permission) { | 87 Notification.requestPermission(function(permission) { |
| 66 sendResultToTest('permission status - ' + permission); | 88 sendResultToTest('permission status - ' + permission); |
| 67 }); | 89 }); |
| 68 } | 90 } |
| 69 | 91 |
| 70 function registerServiceWorker() { | 92 function registerServiceWorker() { |
| 71 // The base dir used to resolve service_worker.js and the scope depends on | 93 // 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 | 94 // whether this script is included from an html file in ./, subscope1/, or |
| 73 // subscope2/. | 95 // subscope2/. |
| 74 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then( | 96 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then( |
| 75 function(swRegistration) { | 97 function(swRegistration) { |
| 76 sendResultToTest('ok - service worker registered'); | 98 sendResultToTest('ok - service worker registered'); |
| 77 }, sendErrorToTest); | 99 }, sendErrorToTest); |
| 78 } | 100 } |
| 79 | 101 |
| 80 function unregisterServiceWorker() { | 102 function unregisterServiceWorker() { |
| 81 navigator.serviceWorker.getRegistration().then(function(swRegistration) { | 103 navigator.serviceWorker.getRegistration().then(function(swRegistration) { |
| 82 swRegistration.unregister().then(function(result) { | 104 swRegistration.unregister().then(function(result) { |
| 83 sendResultToTest('service worker unregistration status: ' + result); | 105 sendResultToTest('service worker unregistration status: ' + result); |
| 84 }) | 106 }) |
| 85 }).catch(sendErrorToTest); | 107 }).catch(sendErrorToTest); |
| 86 } | 108 } |
| 87 | 109 |
| 110 function replaceServiceWorker() { | |
| 111 navigator.serviceWorker.register('service_worker_with_skipWaiting_claim.js', { | |
| 112 scope: './' | |
| 113 }).then(swRegistrationReady).then(() => { | |
| 114 sendResultToTest('ok - service worker replaced'); | |
| 115 }).catch(sendErrorToTest); | |
| 116 } | |
| 117 | |
| 88 function removeManifest() { | 118 function removeManifest() { |
| 89 var element = document.querySelector('link[rel="manifest"]'); | 119 var element = document.querySelector('link[rel="manifest"]'); |
| 90 if (element) { | 120 if (element) { |
| 91 element.parentNode.removeChild(element); | 121 element.parentNode.removeChild(element); |
| 92 sendResultToTest('manifest removed'); | 122 sendResultToTest('manifest removed'); |
| 93 } else { | 123 } else { |
| 94 sendResultToTest('unable to find manifest element'); | 124 sendResultToTest('unable to find manifest element'); |
| 95 } | 125 } |
| 96 } | 126 } |
| 97 | 127 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 swRegistration.pushManager.getSubscription().then(function(pushSubscription) | 218 swRegistration.pushManager.getSubscription().then(function(pushSubscription) |
| 189 { | 219 { |
| 190 if (!pushSubscription) { | 220 if (!pushSubscription) { |
| 191 sendResultToTest('unsubscribe result: false'); | 221 sendResultToTest('unsubscribe result: false'); |
| 192 return; | 222 return; |
| 193 } | 223 } |
| 194 pushSubscription.unsubscribe().then(function(result) { | 224 pushSubscription.unsubscribe().then(function(result) { |
| 195 sendResultToTest('unsubscribe result: ' + result); | 225 sendResultToTest('unsubscribe result: ' + result); |
| 196 }, function(error) { | 226 }, function(error) { |
| 197 sendResultToTest('unsubscribe error: ' + error.message); | 227 sendResultToTest('unsubscribe error: ' + error.message); |
| 198 }) | 228 }); |
| 199 }) | 229 }) |
| 200 }); | 230 }); |
| 201 } | 231 } |
| 202 | 232 |
| 233 function storePushSubscription() { | |
| 234 navigator.serviceWorker.ready.then(swRegistration => { | |
| 235 swRegistration.pushManager.getSubscription().then(pushSubscription => { | |
| 236 window.storedPushSubscription = pushSubscription; | |
| 237 sendResultToTest('ok - stored'); | |
| 238 }, sendErrorToTest); | |
| 239 }, sendErrorToTest); | |
| 240 } | |
| 241 | |
| 242 function unsubscribeStoredPushSubscription() { | |
| 243 window.storedPushSubscription.unsubscribe().then(function(result) { | |
| 244 sendResultToTest('unsubscribe result: ' + result); | |
| 245 }, function(error) { | |
| 246 sendResultToTest('unsubscribe error: ' + error.message); | |
| 247 }); | |
| 248 } | |
| 249 | |
| 203 function hasSubscription() { | 250 function hasSubscription() { |
| 204 navigator.serviceWorker.ready.then(function(swRegistration) { | 251 navigator.serviceWorker.ready.then(function(swRegistration) { |
| 205 return swRegistration.pushManager.getSubscription(); | 252 return swRegistration.pushManager.getSubscription(); |
| 206 }).then(function(subscription) { | 253 }).then(function(subscription) { |
| 207 sendResultToTest(subscription ? 'true - subscribed' | 254 sendResultToTest(subscription ? 'true - subscribed' |
| 208 : 'false - not subscribed'); | 255 : 'false - not subscribed'); |
| 209 }).catch(sendErrorToTest); | 256 }).catch(sendErrorToTest); |
| 210 } | 257 } |
| 211 | 258 |
| 212 navigator.serviceWorker.addEventListener('message', function(event) { | 259 navigator.serviceWorker.addEventListener('message', function(event) { |
| 213 var message = JSON.parse(event.data); | 260 var message = JSON.parse(event.data); |
| 214 if (message.type == 'push') | 261 if (message.type == 'push') |
| 215 resultQueue.push(message.data); | 262 resultQueue.push(message.data); |
| 216 else | 263 else |
| 217 sendResultToTest(message.data); | 264 sendResultToTest(message.data); |
| 218 }, false); | 265 }, false); |
| OLD | NEW |