| 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 // The ResultQueue is a mechanism for passing messages back to the test |
| 8 // framework. |
| 7 var resultQueue = new ResultQueue(); | 9 var resultQueue = new ResultQueue(); |
| 8 | 10 |
| 9 var pushSubscriptionOptions = { | 11 var pushSubscriptionOptions = { |
| 10 userVisibleOnly: true | 12 userVisibleOnly: true |
| 11 }; | 13 }; |
| 12 | 14 |
| 13 // Sends data back to the test. This must be in response to an earlier | |
| 14 // request, but it's ok to respond asynchronously. The request blocks until | |
| 15 // the response is sent. | |
| 16 function sendResultToTest(result) { | |
| 17 console.log('sendResultToTest: ' + result); | |
| 18 if (window.domAutomationController) { | |
| 19 domAutomationController.send('' + result); | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 function sendErrorToTest(error) { | |
| 24 sendResultToTest(error.name + ' - ' + error.message); | |
| 25 } | |
| 26 | |
| 27 // Queue storing asynchronous results received from the Service Worker. Results | |
| 28 // are sent to the test when requested. | |
| 29 function ResultQueue() { | |
| 30 // Invariant: this.queue.length == 0 || this.pendingGets == 0 | |
| 31 this.queue = []; | |
| 32 this.pendingGets = 0; | |
| 33 } | |
| 34 | |
| 35 // Adds a data item to the queue. Will be sent to the test if there are | |
| 36 // pendingGets. | |
| 37 ResultQueue.prototype.push = function(data) { | |
| 38 if (this.pendingGets > 0) { | |
| 39 this.pendingGets--; | |
| 40 sendResultToTest(data); | |
| 41 } else { | |
| 42 this.queue.unshift(data); | |
| 43 } | |
| 44 }; | |
| 45 | |
| 46 // Called by native. Sends the next data item to the test if it is available. | |
| 47 // Otherwise increments pendingGets so it will be delivered when received. | |
| 48 ResultQueue.prototype.pop = function() { | |
| 49 if (this.queue.length) { | |
| 50 sendResultToTest(this.queue.pop()); | |
| 51 } else { | |
| 52 this.pendingGets++; | |
| 53 } | |
| 54 }; | |
| 55 | |
| 56 // Called by native. Immediately sends the next data item to the test if it is | |
| 57 // available, otherwise sends null. | |
| 58 ResultQueue.prototype.popImmediately = function() { | |
| 59 sendResultToTest(this.queue.length ? this.queue.pop() : null); | |
| 60 }; | |
| 61 | |
| 62 // Waits for the given ServiceWorkerRegistration to become ready. | 15 // Waits for the given ServiceWorkerRegistration to become ready. |
| 63 // Shim for https://github.com/w3c/ServiceWorker/issues/770. | 16 // Shim for https://github.com/w3c/ServiceWorker/issues/770. |
| 64 function swRegistrationReady(reg) { | 17 function swRegistrationReady(reg) { |
| 65 return new Promise((resolve, reject) => { | 18 return new Promise((resolve, reject) => { |
| 66 if (reg.active) { | 19 if (reg.active) { |
| 67 resolve(); | 20 resolve(); |
| 68 return; | 21 return; |
| 69 } | 22 } |
| 70 | 23 |
| 71 if (!reg.installing && !reg.waiting) { | 24 if (!reg.installing && !reg.waiting) { |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 }).catch(sendErrorToTest); | 211 }).catch(sendErrorToTest); |
| 259 } | 212 } |
| 260 | 213 |
| 261 navigator.serviceWorker.addEventListener('message', function(event) { | 214 navigator.serviceWorker.addEventListener('message', function(event) { |
| 262 var message = JSON.parse(event.data); | 215 var message = JSON.parse(event.data); |
| 263 if (message.type == 'push') | 216 if (message.type == 'push') |
| 264 resultQueue.push(message.data); | 217 resultQueue.push(message.data); |
| 265 else | 218 else |
| 266 sendResultToTest(message.data); | 219 sendResultToTest(message.data); |
| 267 }, false); | 220 }, false); |
| OLD | NEW |