| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 var registrationReference = null; | 8 var registrationReference = null; |
| 9 | 9 |
| 10 // Sends data back to the test. This must be in response to an earlier | 10 // Sends data back to the test. This must be in response to an earlier |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 navigator.serviceWorker.ready | 36 navigator.serviceWorker.ready |
| 37 .then(function(swRegistration) { | 37 .then(function(swRegistration) { |
| 38 return swRegistration.sync.register({'tag': tag}); | 38 return swRegistration.sync.register({'tag': tag}); |
| 39 }) | 39 }) |
| 40 .then(function(syncRegistration) { | 40 .then(function(syncRegistration) { |
| 41 sendResultToTest('ok - ' + tag + ' registered'); | 41 sendResultToTest('ok - ' + tag + ' registered'); |
| 42 }) | 42 }) |
| 43 .catch(sendErrorToTest); | 43 .catch(sendErrorToTest); |
| 44 } | 44 } |
| 45 | 45 |
| 46 function registerOneShotFromServiceWorker(tag) { |
| 47 navigator.serviceWorker.ready |
| 48 .then(function(swRegistration) { |
| 49 swRegistration.active.postMessage({action: 'registerOneShot', tag: tag}); |
| 50 sendResultToTest('ok - ' + tag + ' register sent to SW'); |
| 51 }) |
| 52 .catch(sendErrorToTest); |
| 53 } |
| 54 |
| 46 function unregisterOneShot(tag) { | 55 function unregisterOneShot(tag) { |
| 47 navigator.serviceWorker.ready | 56 navigator.serviceWorker.ready |
| 48 .then(function(swRegistration) { | 57 .then(function(swRegistration) { |
| 49 return swRegistration.sync.getRegistration(tag); | 58 return swRegistration.sync.getRegistration(tag); |
| 50 }) | 59 }) |
| 51 .then(function(syncRegistration) { | 60 .then(function(syncRegistration) { |
| 52 if (!syncRegistration) { | 61 if (!syncRegistration) { |
| 53 sendResultToTest('error - ' + tag + ' not found'); | 62 sendResultToTest('error - ' + tag + ' not found'); |
| 54 return; | 63 return; |
| 55 } | 64 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 .then(function(syncRegistration) { | 99 .then(function(syncRegistration) { |
| 91 if (!syncRegistration) { | 100 if (!syncRegistration) { |
| 92 sendResultToTest('error - ' + tag + ' not found'); | 101 sendResultToTest('error - ' + tag + ' not found'); |
| 93 return; | 102 return; |
| 94 } | 103 } |
| 95 sendResultToTest('ok - ' + tag + ' found'); | 104 sendResultToTest('ok - ' + tag + ' found'); |
| 96 }) | 105 }) |
| 97 .catch(sendErrorToTest); | 106 .catch(sendErrorToTest); |
| 98 } | 107 } |
| 99 | 108 |
| 109 function getRegistrationOneShotFromServiceWorker(tag) { |
| 110 navigator.serviceWorker.ready |
| 111 .then(function(swRegistration) { |
| 112 swRegistration.active.postMessage( |
| 113 {action: 'getRegistrationOneShot', tag: tag}); |
| 114 sendResultToTest('ok - getRegistration sent to SW'); |
| 115 }) |
| 116 .catch(sendErrorToTest); |
| 117 } |
| 118 |
| 100 function getRegistrationsOneShot(tag) { | 119 function getRegistrationsOneShot(tag) { |
| 101 navigator.serviceWorker.ready | 120 navigator.serviceWorker.ready |
| 102 .then(function(swRegistration) { | 121 .then(function(swRegistration) { |
| 103 return swRegistration.sync.getRegistrations(); | 122 return swRegistration.sync.getRegistrations(); |
| 104 }) | 123 }) |
| 105 .then(function(syncRegistrations) { | 124 .then(function(syncRegistrations) { |
| 106 var tags = syncRegistrations.map(function(syncRegistration) { | 125 var tags = syncRegistrations.map(function(syncRegistration) { |
| 107 return syncRegistration.tag; | 126 return syncRegistration.tag; |
| 108 }); | 127 }); |
| 109 sendResultToTest('ok - ' + tags.toString()); | 128 sendResultToTest('ok - ' + tags.toString()); |
| 110 }) | 129 }) |
| 111 .catch(sendErrorToTest); | 130 .catch(sendErrorToTest); |
| 112 } | 131 } |
| 113 | 132 |
| 133 function getRegistrationsOneShotFromServiceWorker() { |
| 134 navigator.serviceWorker.ready |
| 135 .then(function(swRegistration) { |
| 136 swRegistration.active.postMessage({action: 'getRegistrationsOneShot'}); |
| 137 sendResultToTest('ok - getRegistrations sent to SW'); |
| 138 }) |
| 139 .catch(sendErrorToTest); |
| 140 } |
| 141 |
| 114 function completeDelayedOneShot() { | 142 function completeDelayedOneShot() { |
| 115 navigator.serviceWorker.ready | 143 navigator.serviceWorker.ready |
| 116 .then(function(swRegistration) { | 144 .then(function(swRegistration) { |
| 117 swRegistration.active.postMessage({action: 'completeDelayedOneShot'}); | 145 swRegistration.active.postMessage({action: 'completeDelayedOneShot'}); |
| 118 sendResultToTest('ok - delay completing'); | 146 sendResultToTest('ok - delay completing'); |
| 119 }) | 147 }) |
| 120 .catch(sendErrorToTest); | 148 .catch(sendErrorToTest); |
| 121 } | 149 } |
| 122 | 150 |
| 123 function rejectDelayedOneShot() { | 151 function rejectDelayedOneShot() { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 }; | 224 }; |
| 197 | 225 |
| 198 // Called by native. Immediately sends the next data item to the test if it is | 226 // Called by native. Immediately sends the next data item to the test if it is |
| 199 // available, otherwise sends null. | 227 // available, otherwise sends null. |
| 200 ResultQueue.prototype.popImmediately = function() { | 228 ResultQueue.prototype.popImmediately = function() { |
| 201 sendResultToTest(this.queue.length ? this.queue.pop() : null); | 229 sendResultToTest(this.queue.length ? this.queue.pop() : null); |
| 202 }; | 230 }; |
| 203 | 231 |
| 204 navigator.serviceWorker.addEventListener('message', function(event) { | 232 navigator.serviceWorker.addEventListener('message', function(event) { |
| 205 var message = event.data; | 233 var message = event.data; |
| 206 if (message.type == 'sync') | 234 if (message.type == 'sync' || message.type === 'register') |
| 207 resultQueue.push(message.data); | 235 resultQueue.push(message.data); |
| 208 }, false); | 236 }, false); |
| OLD | NEW |