| 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 | 9 |
| 9 // 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 |
| 10 // request, but it's ok to respond asynchronously. The request blocks until | 11 // request, but it's ok to respond asynchronously. The request blocks until |
| 11 // the response is sent. | 12 // the response is sent. |
| 12 function sendResultToTest(result) { | 13 function sendResultToTest(result) { |
| 13 console.log('sendResultToTest: ' + result); | 14 console.log('sendResultToTest: ' + result); |
| 14 if (window.domAutomationController) { | 15 if (window.domAutomationController) { |
| 15 domAutomationController.send('' + result); | 16 domAutomationController.send('' + result); |
| 16 } | 17 } |
| 17 } | 18 } |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 return syncRegistration.tag; | 107 return syncRegistration.tag; |
| 107 }); | 108 }); |
| 108 sendResultToTest('ok - ' + tags.toString()); | 109 sendResultToTest('ok - ' + tags.toString()); |
| 109 }) | 110 }) |
| 110 .catch(sendErrorToTest); | 111 .catch(sendErrorToTest); |
| 111 } | 112 } |
| 112 | 113 |
| 113 function completeDelayedOneShot() { | 114 function completeDelayedOneShot() { |
| 114 navigator.serviceWorker.ready | 115 navigator.serviceWorker.ready |
| 115 .then(function(swRegistration) { | 116 .then(function(swRegistration) { |
| 116 swRegistration.active.postMessage('completeDelayedOneShot'); | 117 swRegistration.active.postMessage({action: 'completeDelayedOneShot'}); |
| 117 sendResultToTest('ok - delay completing'); | 118 sendResultToTest('ok - delay completing'); |
| 118 }) | 119 }) |
| 119 .catch(sendErrorToTest); | 120 .catch(sendErrorToTest); |
| 120 } | 121 } |
| 121 | 122 |
| 122 function rejectDelayedOneShot() { | 123 function rejectDelayedOneShot() { |
| 123 navigator.serviceWorker.ready | 124 navigator.serviceWorker.ready |
| 124 .then(function(swRegistration) { | 125 .then(function(swRegistration) { |
| 125 swRegistration.active.postMessage('rejectDelayedOneShot'); | 126 swRegistration.active.postMessage({action: 'rejectDelayedOneShot'}); |
| 126 sendResultToTest('ok - delay rejecting'); | 127 sendResultToTest('ok - delay rejecting'); |
| 127 }) | 128 }) |
| 128 .catch(sendErrorToTest); | 129 .catch(sendErrorToTest); |
| 129 } | 130 } |
| 130 | 131 |
| 132 function notifyWhenDoneOneShot(tag) { |
| 133 navigator.serviceWorker.ready |
| 134 .then(function(swRegistration) { |
| 135 swRegistration.active.postMessage({action: 'notifyWhenDone', tag: tag}); |
| 136 }) |
| 137 .catch(sendErrorToTest); |
| 138 } |
| 139 |
| 140 function notifyWhenDoneImmediateOneShot(tag) { |
| 141 if (registrationReference == null) { |
| 142 sendResultToTest('error - must call storeRegistration first'); |
| 143 return; |
| 144 } |
| 145 |
| 146 registrationReference.done |
| 147 .then(function(success) { |
| 148 sendResultToTest('ok - ' + registrationReference.tag + |
| 149 ' result: ' + success) |
| 150 }, function(err) { |
| 151 sendResultToTest('error - ' + registrationReference.tag + ' failed'); |
| 152 }) |
| 153 .catch(sendErrorToTest) |
| 154 } |
| 155 |
| 156 |
| 157 function storeRegistration(tag) { |
| 158 navigator.serviceWorker.ready |
| 159 .then(function(swRegistration) { |
| 160 return swRegistration.sync.getRegistration(tag); |
| 161 }) |
| 162 .then(function(syncRegistration) { |
| 163 registrationReference = syncRegistration; |
| 164 sendResultToTest('ok - ' + tag + ' stored'); |
| 165 }) |
| 166 .catch(sendErrorToTest); |
| 167 } |
| 168 |
| 131 // Queue storing asynchronous results received from the Service Worker. Results | 169 // Queue storing asynchronous results received from the Service Worker. Results |
| 132 // are sent to the test when requested. | 170 // are sent to the test when requested. |
| 133 function ResultQueue() { | 171 function ResultQueue() { |
| 134 // Invariant: this.queue.length == 0 || this.pendingGets == 0 | 172 // Invariant: this.queue.length == 0 || this.pendingGets == 0 |
| 135 this.queue = []; | 173 this.queue = []; |
| 136 this.pendingGets = 0; | 174 this.pendingGets = 0; |
| 137 } | 175 } |
| 138 | 176 |
| 139 // Adds a data item to the queue. Will be sent to the test if there are | 177 // Adds a data item to the queue. Will be sent to the test if there are |
| 140 // pendingGets. | 178 // pendingGets. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 161 // available, otherwise sends null. | 199 // available, otherwise sends null. |
| 162 ResultQueue.prototype.popImmediately = function() { | 200 ResultQueue.prototype.popImmediately = function() { |
| 163 sendResultToTest(this.queue.length ? this.queue.pop() : null); | 201 sendResultToTest(this.queue.length ? this.queue.pop() : null); |
| 164 }; | 202 }; |
| 165 | 203 |
| 166 navigator.serviceWorker.addEventListener('message', function(event) { | 204 navigator.serviceWorker.addEventListener('message', function(event) { |
| 167 var message = event.data; | 205 var message = event.data; |
| 168 if (message.type == 'sync') | 206 if (message.type == 'sync') |
| 169 resultQueue.push(message.data); | 207 resultQueue.push(message.data); |
| 170 }, false); | 208 }, false); |
| OLD | NEW |